Passed
Push — master ( 3b3500...0bff11 )
by Nils
04:16
created

sendEmailsNotSent()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 53
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 35
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 53
rs 9.0488

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
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This library is distributed in the hope that it will be useful,
6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8
 * ---
9
 *
10
 * @project   Teampass
11
 * @version   
12
 * @file      background_tasks___sending_emails.php
13
 * ---
14
 *
15
 * @author    Nils Laumaillé ([email protected])
16
 *
17
 * @copyright 2009-2023 Teampass.net
18
 *
19
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
20
 * ---
21
 *
22
 * @see       https://www.teampass.net
23
 */
24
25
require_once __DIR__.'/../sources/SecureHandler.php';
26
session_name('teampass_session');
27
session_start();
28
$_SESSION['CPM'] = 1;
29
30
// Load config
31
require_once __DIR__.'/../includes/config/tp.config.php';
32
33
// increase the maximum amount of time a script is allowed to run
34
set_time_limit($SETTINGS['task_maximum_run_time']);
35
36
// Do checks
37
require_once $SETTINGS['cpassman_dir'].'/includes/config/include.php';
38
require_once $SETTINGS['cpassman_dir'].'/includes/config/settings.php';
39
header('Content-type: text/html; charset=utf-8');
40
header('Cache-Control: no-cache, must-revalidate');
41
require_once $SETTINGS['cpassman_dir'].'/sources/main.functions.php';
42
43
// Connect to mysql server
44
require_once $SETTINGS['cpassman_dir'].'/includes/libraries/Database/Meekrodb/db.class.php';
45
if (defined('DB_PASSWD_CLEAR') === false) {
46
    define('DB_PASSWD_CLEAR', defuseReturnDecrypted(DB_PASSWD, $SETTINGS));
47
}
48
DB::$host = DB_HOST;
49
DB::$user = DB_USER;
50
DB::$password = DB_PASSWD_CLEAR;
51
DB::$dbName = DB_NAME;
52
DB::$port = DB_PORT;
53
DB::$encoding = DB_ENCODING;
54
DB::$ssl = DB_SSL;
55
DB::$connect_options = DB_CONNECT_OPTIONS;
56
57
58
// Manage emails to send in queue.
59
// Only manage 10 emails at time
60
DB::debugmode(false);
61
$rows = DB::query(
62
    'SELECT *
63
    FROM ' . prefixTable('processes') . '
64
    WHERE is_in_progress = %i AND process_type = %s
65
    ORDER BY increment_id ASC LIMIT 0,10',
66
    0,
67
    'send_email'
68
);
69
foreach ($rows as $record) {
70
    // get email properties
71
    $email = json_decode($record['arguments'], true);
72
73
    // send email
74
    sendEmail(
75
        $email['subject'],
76
        $email['body'],
77
        $email['receivers'],
78
        $SETTINGS,
79
        null,
80
        true,
81
        true
82
    );
83
84
    // update DB
85
    DB::update(
86
        prefixTable('processes'),
87
        array(
88
            'updated_at' => time(),
89
            'finished_at' => time(),
90
            'is_in_progress' => -1,
91
        ),
92
        'increment_id = %i',
93
        $record['increment_id']
94
    );
95
}
96
97
// Now send statitics
98
if (isset($SETTINGS['send_stats']) === true && (int) $SETTINGS['send_stats'] === 1) {
99
    require_once $SETTINGS['cpassman_dir'].'/sources/main.queries.php';
100
    sendingStatistics($SETTINGS);
101
}
102
103
// Now send waiting emails - TODO - remove this in the future
104
sendEmailsNotSent(
105
    $SETTINGS
106
);
107
108
109
function sendEmailsNotSent(
110
    array $SETTINGS
111
)
112
{
113
    if ((int) $SETTINGS['enable_send_email_on_user_login'] === 1) {
114
        $row = DB::queryFirstRow(
115
            'SELECT valeur FROM ' . prefixTable('misc') . ' WHERE type = %s AND intitule = %s',
116
            'cron',
117
            'sending_emails'
118
        );
119
120
        if ((int) (time() - $row['valeur']) >= 300 || (int) $row['valeur'] === 0) {
121
            $rows = DB::query(
122
                'SELECT *
123
                FROM ' . prefixTable('emails') .
124
                ' WHERE status != %s',
125
                'sent'
126
            );
127
            foreach ($rows as $record) {
128
                // Send email
129
                $ret = json_decode(
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
130
                    sendEmail(
131
                        $record['subject'],
132
                        $record['body'],
133
                        $record['receivers'],
134
                        $SETTINGS,
135
                        null,
136
                        true,
137
                        true
138
                    ),
139
                    true
140
                );
141
142
                // update item_id in files table
143
                DB::update(
144
                    prefixTable('emails'),
145
                    array(
146
                        'status' => 'sent',
147
                    ),
148
                    'increment_id = %i',
149
                    $record['increment_id']
150
                );
151
            }
152
        }
153
        // update cron time
154
        DB::update(
155
            prefixTable('misc'),
156
            array(
157
                'valeur' => time(),
158
            ),
159
            'intitule = %s AND type = %s',
160
            'sending_emails',
161
            'cron'
162
        );
163
    }
164
}