Completed
Push — master ( c7b6c0...9c4d6d )
by Sébastien
05:36
created

sendtomail.php ➔ checkConfiguration()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5
Metric Value
cc 5
eloc 8
nc 2
nop 0
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 5
rs 8.8571
1
<?php
2
3
require_once ("config.php");
4
require_once "resources/PHPMailer/class.phpmailer.php";
5
require_once "book.php";
6
7
function checkConfiguration () {
8 5
    global $config;
9
10 5
    if (is_null ($config['cops_mail_configuration']) ||
11 4
        !is_array ($config['cops_mail_configuration']) ||
12 3
        empty ($config['cops_mail_configuration']["smtp.host"]) ||
13 5
        empty ($config['cops_mail_configuration']["address.from"])) {
14 4
        return "NOK. bad configuration.";
15
    }
16 1
    return False;
17
}
18
19
function checkRequest ($idData, $emailDest) {
20 3
    if (empty ($idData)) {
21 1
        return 'No data sent.';
22
    }
23 2
    if (empty ($emailDest)) {
24 1
        return 'No email sent.';
25
    }
26 1
    return False;
27
}
28
29
if (php_sapi_name() === 'cli') { return; }
30
31
global $config;
32
33
if ($error = checkConfiguration ()) {
34
    echo $error;
35
    exit;
36
}
37
38
$idData = $_REQUEST["data"];
39
$emailDest = $_REQUEST["email"];
40
if ($error = checkRequest ($idData, $emailDest)) {
41
    echo $error;
42
    exit;
43
}
44
45
$book = Book::getBookByDataId($idData);
46
$data = $book->getDataById ($idData);
47
48
if (filesize ($data->getLocalPath ()) > 10 * 1024 * 1024) {
49
    echo 'Attachment too big';
50
    exit;
51
}
52
53
$mail = new PHPMailer;
54
55
$mail->IsSMTP();
56
$mail->Timeout = 30; // 30 seconds as some files can be big
57
$mail->Host = $config['cops_mail_configuration']["smtp.host"];
58
if (!empty ($config['cops_mail_configuration']["smtp.secure"])) {
59
    $mail->SMTPSecure = $config['cops_mail_configuration']["smtp.secure"];
60
    $mail->Port = 465;
61
}
62
$mail->SMTPAuth = !empty ($config['cops_mail_configuration']["smtp.username"]);
63
if (!empty ($config['cops_mail_configuration']["smtp.username"])) $mail->Username = $config['cops_mail_configuration']["smtp.username"];
64
if (!empty ($config['cops_mail_configuration']["smtp.password"])) $mail->Password = $config['cops_mail_configuration']["smtp.password"];
65
if (!empty ($config['cops_mail_configuration']["smtp.secure"])) $mail->SMTPSecure = $config['cops_mail_configuration']["smtp.secure"];
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->Subject = 'Sent by COPS : ' . $data->getUpdatedFilename ();
79
$mail->Body    = "<h1>" . $book->title . "</h1><h2>" . $book->getAuthorsName () . "</h2>" . $book->getComment ();
80
$mail->AltBody = "Sent by COPS";
81
82
if (!$mail->Send()) {
83
   echo localize ("mail.messagenotsent");
84
   echo 'Mailer Error: ' . $mail->ErrorInfo;
85
   exit;
86
}
87
88
echo localize ("mail.messagesent");
89
90