Completed
Push — master ( b35b0f...acc097 )
by Greg
05:21
created

classes/DomainMOD/Email.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * /classes/DomainMOD/Email.php
4
 *
5
 * This file is part of DomainMOD, an open source domain and internet asset manager.
6
 * Copyright (c) 2010-2017 Greg Chetcuti <[email protected]>
7
 *
8
 * Project: http://domainmod.org   Author: http://chetcuti.com
9
 *
10
 * DomainMOD is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
11
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * DomainMOD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along with DomainMOD. If not, see
18
 * http://www.gnu.org/licenses/.
19
 *
20
 */
21
//@formatter:off
22
namespace DomainMOD;
23
24
class Email
25
{
26
    public $log;
27
    public $system;
28
    public $time;
29
30
    public function __construct()
31
    {
32
        $this->log = new Log('email.class');
33
        $this->system = new System();
34
        $this->time = new Time();
35
    }
36
37
    public function sendExpirations($from_cron)
38
    {
39
        $timestamp_basic = $this->time->timeBasic();
40
        $timestamp_long = $this->time->timeLong();
41
42
        list($full_url, $from_address, $number_of_days, $use_smtp) = $this->getSettings();
43
        $send_to = $this->getRecipients();
44
        $subject = "Upcoming Expirations - " . $timestamp_long;
45
        $headers = $this->getHeaders($from_address);
46
47
        list($result_domains, $result_ssl) = $this->checkExpiring($number_of_days, $from_cron);
48
        $message_html = '';
49
        $message_html .= $this->messageTopHtml($full_url, $subject, $number_of_days);
50
        $message_html .= $this->showDomainsHtml($result_domains, $full_url, $timestamp_basic);
51
        $message_html .= $this->showSslHtml($result_ssl, $full_url, $timestamp_basic);
52
        $message_html .= $this->messageBottomHtml($full_url);
53
54
        list($result_domains, $result_ssl) = $this->checkExpiring($number_of_days, $from_cron);
55
        $message_text = '';
56
        $message_text = $subject . "\n\n";
57
        $message_text .= $this->messageTopText($number_of_days);
58
        $message_text .= $this->showDomainsText($result_domains, $timestamp_basic);
59
        $message_text .= $this->showSslText($result_ssl, $timestamp_basic);
60
        $message_text .= $this->messageBottomText($full_url);
61
62
        foreach ($send_to as $row_recipients) {
1 ignored issue
show
The expression $send_to of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
63
64
            $full_to = '"' . $row_recipients->first_name . ' ' . $row_recipients->last_name . '"' . ' <' . $row_recipients->email_address . '>';
65
66
            if ($use_smtp != '1') {
67
68
                mail($full_to, $subject, $message_html, $headers, '-f' . $from_address);
69
70
            } else {
71
72
                $smtp = new Smtp();
73
                $smtp->send($from_address, $row_recipients->email_address, $row_recipients->first_name . ' ' .
74
                    $row_recipients->last_name, $subject, $message_html, $message_text);
75
76
            }
77
            sleep(2);
78
79
            $_SESSION['s_message_success'] .= 'Expiration Email Sent<BR>';
80
        }
81
    }
82
83
    public function getSettings()
84
    {
85
        $url = '';
86
        $email = '';
87
        $days = '';
88
        $use_smtp = '';
89
90
        $tmpq = $this->system->db()->query("
91
            SELECT full_url, email_address, expiration_days, use_smtp
92
            FROM settings");
93
        $result = $tmpq->fetch();
94
95
        if (!$result) {
96
97
            $log_message = 'Unable to retrieve email settings';
98
            $this->log->error($log_message);
99
100
        } else {
101
102
            $url = $result->full_url;
103
            $email = $result->email_address;
104
            $days = $result->expiration_days;
105
            $use_smtp = $result->use_smtp;
106
107
        }
108
        return array($url, $email, $days, $use_smtp);
109
    }
110
111
    public function checkExpiring($days, $from_cron)
112
    {
113
        $date = $this->time->timeBasicPlusDays($days);
114
115
        $tmpq = $this->system->db()->prepare("
116
            SELECT id, expiry_date, domain
117
            FROM domains
118
            WHERE active NOT IN ('0', '10')
119
              AND expiry_date <= :date
120
            ORDER BY expiry_date, domain");
121
        $tmpq->execute(array('date' => $date));
122
        $result = $tmpq->fetchAll();
123
124
        if (!$result) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
125
126
            $domains_expiring = '0';
127
128
        } else {
129
130
            $domains_expiring = $result;
131
132
        }
133
134
        $tmpq = $this->system->db()->prepare("
135
            SELECT sslc.id, sslc.expiry_date, sslc.name, sslt.type
136
            FROM ssl_certs AS sslc, ssl_cert_types AS sslt
137
            WHERE sslc.type_id = sslt.id
138
              AND sslc.active NOT IN ('0')
139
              AND sslc.expiry_date <= :date
140
            ORDER BY sslc.expiry_date, sslc.name");
141
        $tmpq->execute(array('date' => $date));
142
        $result = $tmpq->fetchAll();
143
144
        if (!$result) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
145
146
            $ssl_expiring = '0';
147
148
        } else {
149
150
            $ssl_expiring = $result;
151
152
        }
153
154
        if ($domains_expiring != '0' || $ssl_expiring != '0') {
155
            return array($domains_expiring, $ssl_expiring);
156
        } else {
157
            $_SESSION['s_message_success'] .= 'No Upcoming Expirations<BR>';
158
            if ($from_cron == '1') exit;
159
            return false;
160
        }
161
    }
162
163
    public function getRecipients()
164
    {
165
        $tmpq = $this->system->db()->query("
166
            SELECT u.email_address, u.first_name, u.last_name
167
            FROM users AS u, user_settings AS us
168
            WHERE u.id = us.user_id
169
              AND u.active = '1'
170
              AND us.expiration_emails = '1'");
171
        $result = $tmpq->fetchAll();
172
173
        if (!$result) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
174
175
            $_SESSION['s_message_danger'] .= 'No Users Are Subscribed<BR>';
176
            return false;
177
178
        } else {
179
180
            return $result;
181
182
        }
183
    }
184
185
    public function getHeaders($from_address)
186
    {
187
        $headers = '';
188
        $headers .= 'MIME-Version: 1.0' . "\r\n";
189
        $headers .= 'Content-type: text/html; charset=' . EMAIL_ENCODING_TYPE . "\r\n";
190
        $headers .= 'From: "' . SOFTWARE_TITLE . '" <' . $from_address . ">\r\n";
191
        $headers .= 'Return-Path: ' . $from_address . "\r\n";
192
        $headers .= 'Reply-to: ' . $from_address . "\r\n";
193
        $version = phpversion();
194
        $headers .= 'X-Mailer: PHP/' . $version . "\r\n";
195
        return $headers;
196
    }
197
198
    public function messageTopHtml($full_url, $subject, $number_of_days)
199
    {
200
        ob_start(); ?>
201
        <html>
202
        <head><title><?php echo $subject; ?></title></head>
203
        <body bgcolor="#FFFFFF">
204
        <table width="100%" cellspacing="0" cellpadding="0" border="0" bgcolor="#FFFFFF">
205
        <tr>
206
        <td width="100%" bgcolor="#FFFFFF">
207
        <font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
208
        <a title="<?php echo SOFTWARE_TITLE; ?>" href="<?php echo $full_url; ?>/"><img border="0" alt="<?php
209
            echo SOFTWARE_TITLE; ?>" src="<?php echo $full_url; ?>/images/logo.png"></a><BR><BR>Below is a
210
        list of all the Domains & SSL Certificates in <?php echo SOFTWARE_TITLE; ?> that are expiring in the next
211
        <?php echo $number_of_days; ?> days.<BR> <BR>If you would like to change the frequency of this email
212
        notification please contact your <?php echo SOFTWARE_TITLE; ?> administrator.<BR><BR><?php
213
        return ob_get_clean();
214
    }
215
216
    public function messageTopText($number_of_days)
217
    {
218
        $message = "Below is a list of all the Domains & SSL Certificates in " . SOFTWARE_TITLE . " that are expiring in the next " . $number_of_days . " days.\n\nIf you would like to change the frequency of this email notification please contact your " . SOFTWARE_TITLE . " administrator.\n\n";
219
        return $message;
220
    }
221
222
    public function showDomainsHtml($result_domains, $full_url, $timestamp_basic)
223
    {
224
        ob_start();
225
        if ($result_domains) { ?>
226
            <strong><u>Domains</u></strong><BR><?php
227
            foreach ($result_domains as $row_domains) {
228
                if ($row_domains->expiry_date < $timestamp_basic) { ?>
229
230
                    <font color="#CC0000"><?php echo $row_domains->expiry_date; ?></font>&nbsp;&nbsp;<a
231
                        href="<?php echo $full_url; ?>/edit/domain.php?did=<?php echo $row_domains->id;
232
                        ?>"><?php echo $row_domains->domain; ?></a>&nbsp;&nbsp;<font
233
                        color="#CC0000">*EXPIRED*</font><BR><?php
234
                } else { ?>
235
236
                    <?php echo $row_domains->expiry_date; ?>&nbsp;&nbsp;<a href="<?php echo $full_url;
237
                    ?>/edit/domain.php?did=<?php echo $row_domains->id; ?>"><?php echo $row_domains->domain;
238
                        ?></a><BR><?php
239
                }
240
            }
241
        }
242
        return ob_get_clean();
243
    }
244
245
    public function showDomainsText($result_domains, $timestamp_basic)
246
    {
247
        $message = '';
248
        if ($result_domains) {
249
            $message .= "[DOMAINS]\n";
250
            foreach ($result_domains as $row_domains) {
251
                if ($row_domains->expiry_date < $timestamp_basic) {
252
                    $message .= $row_domains->expiry_date . " - " . $row_domains->domain . " *EXPIRED*\n";
253
                } else {
254
                    $message .= $row_domains->expiry_date . " - " . $row_domains->domain . "\n";
255
                }
256
            }
257
            $message .= "\n";
258
        }
259
        return $message;
260
    }
261
262
    public function showSslHtml($result_ssl, $full_url, $timestamp_basic)
263
    {
264
        ob_start();
265
        if ($result_ssl) { ?>
266
            <BR><strong><u>SSL Certificates</u></strong><BR><?php
267
            foreach ($result_ssl as $row_ssl) {
268
                if ($row_ssl->expiry_date < $timestamp_basic) { ?>
269
                    <font color="#CC0000"><?php echo $row_ssl->expiry_date; ?></font>&nbsp;&nbsp;<a
270
                        href="<?php echo $full_url; ?>/edit/ssl-cert.php?sslcid=<?php echo $row_ssl->id;
271
                        ?>"><?php echo $row_ssl->name; ?> (<?php echo $row_ssl->type; ?>)</a>&nbsp;&nbsp;<font
272
                        color="#CC0000">*EXPIRED*</font><BR><?php
273
                } else { ?>
274
                    <?php echo $row_ssl->expiry_date; ?>&nbsp;&nbsp;<a href="<?php echo $full_url;
275
                    ?>/edit/ssl-cert.php?sslcid=<?php echo $row_ssl->id; ?>"><?php echo $row_ssl->name; ?>
276
                        (<?php echo $row_ssl->type; ?>)</a><BR><?php
277
                }
278
            }
279
        }
280
        return ob_get_clean();
281
    }
282
283
    public function showSslText($result_ssl, $timestamp_basic)
284
    {
285
        $message = '';
286
        if ($result_ssl) {
287
            $message .= "[SSL CERTIFICATES]\n";
288
            foreach ($result_ssl as $row_ssl) {
289
                if ($row_ssl->expiry_date < $timestamp_basic) {
290
                    $message .= $row_ssl->expiry_date . " - " . $row_ssl->name . " (" . $row_ssl->type . ") *EXPIRED*\n";
291
                } else {
292
                    $message .= $row_ssl->expiry_date . " - " . $row_ssl->name . " (" . $row_ssl->type . ")\n";
293
                }
294
            }
295
            $message .= "\n";
296
        }
297
        return $message;
298
    }
299
300
    public function messageBottomHtml($full_url)
301
    {
302
        ob_start(); ?>
303
        <BR>Best Regards,<BR><BR>Greg Chetcuti<BR><a
304
            target="_blank" href="mailto:[email protected]">[email protected]</a><BR>
305
        </font>
306
        </td></tr>
307
        </table>
308
        <table width="575" cellspacing="0" cellpadding="0" border="0" bgcolor="#FFFFFF"><tr>
309
        <td width="100%"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
310
        <BR><hr width="100%" size="2" noshade>You've received this email because you're currently subscribed to receive
311
        expiration notifications from the <?php echo SOFTWARE_TITLE; ?> installation located at: <a target="_blank"
312
        href="<?php echo $full_url; ?>/"><?php echo $full_url; ?>/</a><BR><BR>To unsubscribe from these notifications
313
        please visit: <BR><a target="_blank" href="<?php echo $full_url; ?>/settings/email/"><?php echo $full_url;
314
        ?>/settings/email/</a><BR><BR></font>
315
        </td></tr>
316
        </table>
317
        </body>
318
        </html><?php
319
        return ob_get_clean();
320
    }
321
322
    public function messageBottomText($full_url)
323
    {
324
        $message = '';
325
        $message .= "Best Regards,\n";
326
        $message .= "\n";
327
        $message .= "Greg Chetcuti\n";
328
        $message .= "[email protected]\n\n";
329
        $message .= "---\n\n";
330
        $message .= "You've received this email because you're currently subscribed to receive expiration notifications from the " . SOFTWARE_TITLE . " installation located at: " . $full_url . "\n\n";
331
        $message .= "To unsubscribe from these notifications please visit: " . $full_url . "/settings/email/";
332
        return $message;
333
    }
334
335
} //@formatter:on
336