These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | |||
3 | require_once dirname(__FILE__) . '/config.php'; |
||
4 | require_once dirname(__FILE__) . '/base.php'; |
||
5 | |||
6 | function checkConfiguration () { |
||
7 | 5 | global $config; |
|
8 | |||
9 | 5 | if (is_null ($config['cops_mail_configuration']) || |
|
10 | 4 | !is_array ($config['cops_mail_configuration']) || |
|
11 | 3 | empty ($config['cops_mail_configuration']["smtp.host"]) || |
|
12 | 5 | empty ($config['cops_mail_configuration']["address.from"])) { |
|
13 | 4 | return "NOK. bad configuration."; |
|
14 | } |
||
15 | 1 | return False; |
|
16 | } |
||
17 | |||
18 | function checkRequest ($idData, $emailDest) { |
||
19 | 3 | if (empty ($idData)) { |
|
20 | 1 | return 'No data sent.'; |
|
21 | } |
||
22 | 2 | if (empty ($emailDest)) { |
|
23 | 1 | return 'No email sent.'; |
|
24 | } |
||
25 | 1 | return False; |
|
26 | } |
||
27 | |||
28 | if (php_sapi_name() === 'cli') { return; } |
||
29 | |||
30 | global $config; |
||
31 | |||
32 | if ($error = checkConfiguration ()) { |
||
33 | echo $error; |
||
34 | exit; |
||
35 | } |
||
36 | |||
37 | $idData = $_REQUEST["data"]; |
||
38 | $emailDest = $_REQUEST["email"]; |
||
39 | if ($error = checkRequest ($idData, $emailDest)) { |
||
40 | echo $error; |
||
41 | exit; |
||
42 | } |
||
43 | |||
44 | $book = Book::getBookByDataId($idData); |
||
45 | $data = $book->getDataById ($idData); |
||
46 | |||
47 | if (filesize ($data->getLocalPath ()) > 10 * 1024 * 1024) { |
||
48 | echo 'Attachment too big'; |
||
49 | exit; |
||
50 | } |
||
51 | |||
52 | $mail = new PHPMailer; |
||
53 | |||
54 | $mail->IsSMTP(); |
||
55 | $mail->Timeout = 30; // 30 seconds as some files can be big |
||
56 | $mail->Host = $config['cops_mail_configuration']["smtp.host"]; |
||
57 | View Code Duplication | if (!empty ($config['cops_mail_configuration']["smtp.secure"])) { |
|
58 | $mail->SMTPSecure = $config['cops_mail_configuration']["smtp.secure"]; |
||
59 | $mail->Port = 465; |
||
60 | } |
||
61 | $mail->SMTPAuth = !empty ($config['cops_mail_configuration']["smtp.username"]); |
||
62 | if (!empty ($config['cops_mail_configuration']["smtp.username"])) $mail->Username = $config['cops_mail_configuration']["smtp.username"]; |
||
63 | if (!empty ($config['cops_mail_configuration']["smtp.password"])) $mail->Password = $config['cops_mail_configuration']["smtp.password"]; |
||
64 | View Code Duplication | if (!empty ($config['cops_mail_configuration']["smtp.secure"])) $mail->SMTPSecure = $config['cops_mail_configuration']["smtp.secure"]; |
|
65 | if (!empty ($config['cops_mail_configuration']["smtp.port"])) $mail->Port = $config['cops_mail_configuration']["smtp.port"]; |
||
66 | |||
67 | $mail->From = $config['cops_mail_configuration']["address.from"]; |
||
68 | $mail->FromName = $config['cops_title_default']; |
||
69 | |||
70 | foreach (explode (";", $emailDest) as $emailAddress) { |
||
71 | if (empty ($emailAddress)) { continue; } |
||
72 | $mail->AddAddress($emailAddress); |
||
73 | } |
||
74 | |||
75 | $mail->AddAttachment($data->getLocalPath ()); |
||
76 | |||
77 | $mail->IsHTML(true); |
||
78 | $mail->CharSet = "UTF-8"; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
UTF-8 does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. ![]() |
|||
79 | $mail->Subject = 'Sent by COPS : '; |
||
80 | if (!empty ($config['cops_mail_configuration']["subject"])) { |
||
81 | $mail->Subject = $config['cops_mail_configuration']["subject"]; |
||
82 | } |
||
83 | $mail->Subject .= $data->getUpdatedFilename (); |
||
84 | $mail->Body = "<h1>" . $book->title . "</h1><h2>" . $book->getAuthorsName () . "</h2>" . $book->getComment (); |
||
85 | $mail->AltBody = "Sent by COPS"; |
||
86 | |||
87 | if (!$mail->Send()) { |
||
88 | echo localize ("mail.messagenotsent"); |
||
89 | echo 'Mailer Error: ' . $mail->ErrorInfo; |
||
90 | exit; |
||
91 | } |
||
92 | |||
93 | echo localize ("mail.messagesent"); |
||
94 | |||
95 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.