Completed
Push — dev ( e81fc3...06758b )
by Greg
02:50
created

Email::checkExpiring()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 8.3814
c 0
b 0
f 0
cc 6
nc 12
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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-2020 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 $deeb;
27
    public $log;
28
    public $time;
29
    public $full_url;
30
    public $from_address;
31
    public $number_of_days;
32
    public $email_signature;
33
    public $first_name;
34
    public $last_name;
35
    public $email_address;
36
    public $use_smtp;
37
38
    public function __construct()
39
    {
40
        $this->deeb = Database::getInstance();
41
        $this->log = new Log('class.email');
42
        $this->time = new Time();
43
        list($this->full_url, $this->from_address, $this->number_of_days, $this->email_signature, $this->use_smtp) = $this->getSettings();
44
        list($this->first_name, $this->last_name, $this->email_address) = $this->getEmailSignature();
45
    }
46
47
    public function send($email_title, $to_address, $subject, $message_html, $message_text)
48
    {
49
        if ($this->use_smtp == '1') {
50
51
            $smtp = new Smtp();
52
            $smtp->send($email_title, $to_address, $this->from_address, $subject, $message_html, $message_text);
53
54
        } else {
55
56
            $this->intPhpMail($email_title, $to_address, $subject, $message_html);
57
58
        }
59
    }
60
61
    public function intPhpMail($email_title, $to_address, $subject, $message)
62
    {
63
        $headers = $this->getHeaders();
64
        $log_extra = array(_('Method') => 'PHP mail()', _('To') => $to_address, _('From') => $this->from_address,
65
            _('Subject') => $subject);
66
67
        if (mail($to_address, $subject, $message, $headers, '-f' . $this->from_address)) {
68
69
            $log_message = $email_title . ' ' . _('Email :: SEND SUCCEEDED');
70
            $this->log->info($log_message, $log_extra);
71
            return true;
72
73 View Code Duplication
        } else {
74
75
            $log_message = $email_title . ' ' . _('Email :: SEND FAILED');
76
            $this->log->error($log_message, $log_extra);
77
            return false;
78
79
        }
80
    }
81
82
    public function sendExpirations($from_cron = false)
83
    {
84
        $timestamp_basic = $this->time->timeBasic();
85
        $timestamp_long = $this->time->timeLong();
86
87
        $send_to = $this->getExpEmRecip();
88
        $subject = _('Upcoming Expirations') . ' - ' . $timestamp_long;
89
90
        list($result_domains, $result_ssl) = $this->checkExpiring($from_cron);
91
        $message_html = '';
92
        $message_html .= $this->messageTopHtml($subject);
93
        $message_html .= $this->showDomainsHtml($result_domains, $timestamp_basic);
94
        $message_html .= $this->showSslHtml($result_ssl, $timestamp_basic);
95
        $message_html .= $this->messageBottomHtml();
96
97
        list($result_domains, $result_ssl) = $this->checkExpiring($from_cron);
98
        $message_text = $subject . "\n\n";
99
        $message_text .= $this->messageTopText();
100
        $message_text .= $this->showDomainsText($result_domains, $timestamp_basic);
101
        $message_text .= $this->showSslText($result_ssl, $timestamp_basic);
102
        $message_text .= $this->messageBottomText();
103
104
        foreach ($send_to as $row_recipients) {
105
106
            $this->send(_('Expiration'), $row_recipients->email_address, $subject, $message_html, $message_text);
107
            sleep(2);
108
109
        }
110
111
        $_SESSION['s_message_success'] .= _('Expiration Email Sent') . '<BR>';
112
    }
113
114
    public function getSettings()
115
    {
116
        $url = '';
117
        $email = '';
118
        $days = '';
119
        $signature = '';
120
        $use_smtp = '';
121
122
        $pdo = $this->deeb->cnxx;
123
124
        $stmt = $pdo->prepare("
125
            SELECT full_url, email_address, expiration_days, email_signature, use_smtp
126
            FROM settings");
127
        $stmt->execute();
128
        $result = $stmt->fetch();
129
        $stmt->closeCursor();
130
131
        if (!$result) {
132
133
            $log_message = 'Unable to retrieve email settings';
134
            $this->log->critical($log_message);
135
136
        } else {
137
138
            $url = $result->full_url;
139
            $email = $result->email_address;
140
            $days = $result->expiration_days;
141
            $signature = $result->email_signature;
142
            $use_smtp = $result->use_smtp;
143
144
        }
145
        return array($url, $email, $days, $signature, $use_smtp);
146
    }
147
148
    public function getEmailSignature()
149
    {
150
        $first_name = '';
151
        $last_name = '';
152
        $email_address = '';
153
154
        $pdo = $this->deeb->cnxx;
155
156
        $stmt = $pdo->prepare("
157
            SELECT first_name, last_name, email_address
158
            FROM users
159
            WHERE id = :user_id");
160
        $stmt->bindValue('user_id', $this->email_signature, \PDO::PARAM_INT);
161
        $stmt->execute();
162
        $result = $stmt->fetch();
163
        $stmt->closeCursor();
164
165
        if (!$result) {
166
167
            $log_message = 'Unable to retrieve email signature user ID';
168
            $this->log->critical($log_message);
169
170
        } else {
171
172
            $first_name = $result->first_name;
173
            $last_name = $result->last_name;
174
            $email_address = $result->email_address;
175
176
        }
177
        return array($first_name, $last_name, $email_address);
178
    }
179
180
    public function checkExpiring($from_cron = false)
181
    {
182
        $date = $this->time->timeBasicPlusDays($this->number_of_days);
183
        $pdo = $this->deeb->cnxx;
184
185
        $stmt = $pdo->prepare("
186
            SELECT id, expiry_date, domain
187
            FROM domains
188
            WHERE active NOT IN ('0', '10')
189
              AND expiry_date <= :date
190
            ORDER BY expiry_date, domain");
191
        $stmt->bindValue('date', $date, \PDO::PARAM_STR);
192
        $stmt->execute();
193
        $result = $stmt->fetchAll();
194
195
        if (!$result) {
196
197
            $domains_expiring = '0';
198
199
        } else {
200
201
            $domains_expiring = $result;
202
203
        }
204
205
        $stmt = $pdo->prepare("
206
            SELECT sslc.id, sslc.expiry_date, sslc.name, sslt.type
207
            FROM ssl_certs AS sslc, ssl_cert_types AS sslt
208
            WHERE sslc.type_id = sslt.id
209
              AND sslc.active NOT IN ('0')
210
              AND sslc.expiry_date <= :date
211
            ORDER BY sslc.expiry_date, sslc.name");
212
        $stmt->bindValue('date', $date, \PDO::PARAM_STR);
213
        $stmt->execute();
214
        $result = $stmt->fetchAll();
215
216
        if (!$result) {
217
218
            $ssl_expiring = '0';
219
220
        } else {
221
222
            $ssl_expiring = $result;
223
224
        }
225
226
        if ($domains_expiring != '0' || $ssl_expiring != '0') {
227
            return array($domains_expiring, $ssl_expiring);
228
        } else {
229
            $_SESSION['s_message_success'] .= _('No Upcoming Expirations') . '<BR>';
230
            if ($from_cron === true) exit;
231
            return false;
232
        }
233
    }
234
235
    public function getExpEmRecip()
236
    {
237
        $result = $this->deeb->cnxx->query("
238
            SELECT u.email_address
239
            FROM users AS u, user_settings AS us
240
            WHERE u.id = us.user_id
241
              AND u.active = '1'
242
              AND us.expiration_emails = '1'")->fetchAll();
243
244
        if (!$result) {
245
246
            $_SESSION['s_message_danger'] .= _('No Users Are Subscribed') . '<BR>';
247
            return false;
248
249
        } else {
250
251
            return $result;
252
253
        }
254
    }
255
256
    public function getHeaders()
257
    {
258
        $headers = '';
259
        $headers .= 'MIME-Version: 1.0' . "\r\n";
260
        $headers .= 'Content-type: text/html; charset=' . EMAIL_ENCODING_TYPE . "\r\n";
261
        $headers .= 'From: "' . SOFTWARE_TITLE . '" <' . $this->from_address . ">\r\n";
262
        $headers .= 'Return-Path: ' . $this->from_address . "\r\n";
263
        $headers .= 'Reply-to: ' . $this->from_address . "\r\n";
264
        $version = phpversion();
265
        $headers .= 'X-Mailer: PHP/' . $version . "\r\n";
266
        return $headers;
267
    }
268
269
    public function messageTopHtml($subject)
270
    {
271
        ob_start(); ?>
272
        <html>
273
        <head><title><?php echo $subject; ?></title></head>
274
        <body bgcolor="#FFFFFF">
275
        <table width="100%" cellspacing="0" cellpadding="0" border="0" bgcolor="#FFFFFF">
276
        <tr>
277
        <td width="100%" bgcolor="#FFFFFF">
278
        <font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
279
        <a title="<?php echo SOFTWARE_TITLE; ?>" href="<?php echo $this->full_url; ?>/"><img border="0" alt="<?php echo SOFTWARE_TITLE; ?>" src="<?php echo $this->full_url; ?>/images/logo.png"></a><BR>
280
        <BR>
281
        <?php echo sprintf(_('Below is a list of all the Domains & SSL Certificates in %s that are expiring in the next %s days.'), SOFTWARE_TITLE, $this->number_of_days); ?><BR>
282
        <BR>
283
        <?php echo sprintf(_('If you would like to change the frequency of this email notification please contact your %s administrator.'), SOFTWARE_TITLE); ?><BR>
284
        <BR><?php
285
        return ob_get_clean();
286
    }
287
288
    public function messageTopText()
289
    {
290
        $message = sprintf(_('Below is a list of all the Domains & SSL Certificates in %s that are expiring in the next %s days.'), SOFTWARE_TITLE, $this->number_of_days) . "\n\n";
291
        $message .= sprintf(_('If you would like to change the frequency of this email notification please contact your %s administrator.'), SOFTWARE_TITLE) . "\n\n";
292
        return $message;
293
    }
294
295
    public function showDomainsHtml($result_domains, $timestamp_basic)
296
    {
297
        ob_start();
298
        if ($result_domains) { ?>
299
            <strong><u><?php echo _('Domains'); ?></u></strong><BR><?php
300
            foreach ($result_domains as $row_domains) {
301
                if ($row_domains->expiry_date < $timestamp_basic) { ?>
302
303
                    <font color="#CC0000"><?php echo $row_domains->expiry_date; ?></font>&nbsp;&nbsp;<a
304
                        href="<?php echo $this->full_url; ?>/domains/edit.php?did=<?php echo $row_domains->id;
305
                        ?>"><?php echo $row_domains->domain; ?></a>&nbsp;&nbsp;<font
306
                        color="#CC0000">*<?php echo strtoupper(_('Expired')); ?>*</font><BR><?php
307
                } else { ?>
308
309
                    <?php echo $row_domains->expiry_date; ?>&nbsp;&nbsp;<a href="<?php echo $this->full_url;
310
                    ?>/domains/edit.php?did=<?php echo $row_domains->id; ?>"><?php echo $row_domains->domain;
311
                        ?></a><BR><?php
312
                }
313
            }
314
        }
315
        return ob_get_clean();
316
    }
317
318
    public function showDomainsText($result_domains, $timestamp_basic)
319
    {
320
        $message = '';
321
        if ($result_domains) {
322
            $message .= "[" . strtoupper(_('Domains')) . "]\n";
323
            foreach ($result_domains as $row_domains) {
324
                if ($row_domains->expiry_date < $timestamp_basic) {
325
                    $message .= $row_domains->expiry_date . " - " . $row_domains->domain . " *" . strtoupper(_('Expired')) . "*\n";
326
                } else {
327
                    $message .= $row_domains->expiry_date . " - " . $row_domains->domain . "\n";
328
                }
329
            }
330
            $message .= "\n";
331
        }
332
        return $message;
333
    }
334
335
    public function showSslHtml($result_ssl, $timestamp_basic)
336
    {
337
        ob_start();
338
        if ($result_ssl) { ?>
339
            <BR><strong><u><?php echo _('SSL Certificates'); ?></u></strong><BR><?php
340
            foreach ($result_ssl as $row_ssl) {
341
                if ($row_ssl->expiry_date < $timestamp_basic) { ?>
342
                    <font color="#CC0000"><?php echo $row_ssl->expiry_date; ?></font>&nbsp;&nbsp;<a
343
                        href="<?php echo $this->full_url; ?>/ssl/edit.php?sslcid=<?php echo $row_ssl->id;
344
                        ?>"><?php echo $row_ssl->name; ?> (<?php echo $row_ssl->type; ?>)</a>&nbsp;&nbsp;<font
345
                        color="#CC0000">*<?php echo strtoupper(_('Expired')); ?>*</font><BR><?php
346
                } else { ?>
347
                    <?php echo $row_ssl->expiry_date; ?>&nbsp;&nbsp;<a href="<?php echo $this->full_url;
348
                    ?>/ssl/edit.php?sslcid=<?php echo $row_ssl->id; ?>"><?php echo $row_ssl->name; ?>
349
                        (<?php echo $row_ssl->type; ?>)</a><BR><?php
350
                }
351
            }
352
        }
353
        return ob_get_clean();
354
    }
355
356
    public function showSslText($result_ssl, $timestamp_basic)
357
    {
358
        $message = '';
359
        if ($result_ssl) {
360
            $message .= "[" . _('SSL Certificates') . "]\n";
361
            foreach ($result_ssl as $row_ssl) {
362
                if ($row_ssl->expiry_date < $timestamp_basic) {
363
                    $message .= $row_ssl->expiry_date . " - " . $row_ssl->name . " (" . $row_ssl->type . ") *" . strtoupper(_('Expired')) . "*\n";
364
                } else {
365
                    $message .= $row_ssl->expiry_date . " - " . $row_ssl->name . " (" . $row_ssl->type . ")\n";
366
                }
367
            }
368
            $message .= "\n";
369
        }
370
        return $message;
371
    }
372
373
    public function messageBottomHtml()
374
    {
375
        ob_start(); ?>
376
        <BR><?php echo _('Best Regards'); ?>,<BR><BR><?php echo $this->first_name . ' ' . $this->last_name; ?><BR><a
377
            target="_blank" href="mailto:<?php echo $this->email_address; ?>"><?php echo $this->email_address; ?></a><BR>
378
        </font>
379
        </td></tr>
380
        </table>
381
        <table width="575" cellspacing="0" cellpadding="0" border="0" bgcolor="#FFFFFF"><tr>
382
        <td width="100%"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
383
        <BR><hr width="100%" size="2" noshade><?php echo sprintf(_("You've received this email because you're currently subscribed to receive expiration notifications from the %s installation located at"), SOFTWARE_TITLE) . ':'; ?><a target="_blank" href="<?php echo $this->full_url; ?>/"><?php echo $this->full_url; ?>/</a><BR><BR><?php echo _('To unsubscribe from these notifications please visit') . ':'; ?><BR><a target="_blank" href="<?php echo $this->full_url; ?>/settings/profile/"><?php echo $this->full_url; ?>/settings/profile/</a><BR><BR></font>
384
        </td></tr>
385
        </table>
386
        </body>
387
        </html><?php
388
        return ob_get_clean();
389
    }
390
391
    public function messageBottomText()
392
    {
393
        $message = '';
394
        $message .= _('Best Regards') . ",\n";
395
        $message .= "\n";
396
        $message .= $this->first_name . ' ' . $this->last_name . "\n";
397
        $message .= $this->email_address . "\n\n";
398
        $message .= "---\n\n";
399
        $message .= sprintf(_("You've received this email because you're currently subscribed to receive expiration notifications from the %s installation located at"), SOFTWARE_TITLE) . ': ' . $this->full_url . "\n\n";
400
        $message .= _('To unsubscribe from these notifications please visit') . ':'  . $this->full_url . "/settings/profile/";
401
        return $message;
402
    }
403
404
} //@formatter:on
405