|
1
|
|
|
<?php |
|
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
|
|
|
|
|
66
|
|
|
$mail->From = $config['cops_mail_configuration']["address.from"]; |
|
67
|
|
|
$mail->FromName = $config['cops_title_default']; |
|
68
|
|
|
|
|
69
|
|
|
foreach (explode (";", $emailDest) as $emailAddress) { |
|
70
|
|
|
if (empty ($emailAddress)) { continue; } |
|
71
|
|
|
$mail->AddAddress($emailAddress); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$mail->AddAttachment($data->getLocalPath ()); |
|
75
|
|
|
|
|
76
|
|
|
$mail->IsHTML(true); |
|
77
|
|
|
$mail->CharSet = "UTF-8"; |
|
78
|
|
|
$mail->Subject = 'Sent by COPS : '; |
|
79
|
|
|
if (!empty ($config['cops_mail_configuration']["subject"])) { |
|
80
|
|
|
$mail->Subject = $config['cops_mail_configuration']["subject"]; |
|
81
|
|
|
} |
|
82
|
|
|
$mail->Subject .= $data->getUpdatedFilename (); |
|
83
|
|
|
$mail->Body = "<h1>" . $book->title . "</h1><h2>" . $book->getAuthorsName () . "</h2>" . $book->getComment (); |
|
84
|
|
|
$mail->AltBody = "Sent by COPS"; |
|
85
|
|
|
|
|
86
|
|
|
if (!$mail->Send()) { |
|
87
|
|
|
echo localize ("mail.messagenotsent"); |
|
88
|
|
|
echo 'Mailer Error: ' . $mail->ErrorInfo; |
|
89
|
|
|
exit; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
echo localize ("mail.messagesent"); |
|
93
|
|
|
|
|
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.