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

Maintenance::performCleanup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * /classes/DomainMOD/Maintenance.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 Maintenance
25
{
26
    public $deeb;
27
    public $log;
28
    public $time;
29
30
    public function __construct()
31
    {
32
        $this->deeb = Database::getInstance();
33
        $this->log = new Log('class.maintenance');
34
        $this->time = new Time();
35
    }
36
37
    public function performCleanup()
38
    {
39
        $this->lowercaseDomains();
40
        $this->lowercaseTlds();
41
        $this->updateTlds();
42
        $this->updateSegments();
43
        $this->updateAllFees();
44
        $this->deleteUnusedFees('fees', 'domains');
45
        $this->deleteUnusedFees('ssl_fees', 'ssl_certs');
46
        $this->zeroInvalidIpIds();
47
48
        $result_message = _('Maintenance Completed') . '<BR>';
49
50
        return $result_message;
51
    }
52
53
    public function lowercaseDomains()
54
    {
55
        $this->deeb->cnxx->query("UPDATE domains SET domain = LOWER(domain)");
56
    }
57
58
    public function lowercaseTlds()
59
    {
60
        $this->deeb->cnxx->query("UPDATE fees SET tld = LOWER(tld)");
61
    }
62
63
    public function updateTlds()
64
    {
65
        $pdo = $this->deeb->cnxx;
66
67
        $result = $pdo->query("SELECT id, domain FROM domains")->fetchAll();
68
69
        if ($result) {
70
71
            $pdo = $this->deeb->cnxx;
72
            $stmt = $pdo->prepare("
73
                UPDATE domains
74
                SET tld = :tld
75
                WHERE id = :id");
76
            $stmt->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
77
            $stmt->bindParam('id', $bind_id, \PDO::PARAM_INT);
78
79
            foreach ($result as $row) {
80
81
                $bind_tld = $this->getTld($row->domain);
82
                $bind_id = $row->id;
83
                $stmt->execute();
84
85
            }
86
87
        }
88
    }
89
90
    public function getTld($domain)
91
    {
92
        return preg_replace("/^((.*?)\.)(.*)$/", "\\3", $domain);
93
    }
94
95 View Code Duplication
    public function updateSegments()
96
    {
97
        $pdo = $this->deeb->cnxx;
98
        $pdo->query("
99
            UPDATE segment_data
100
            SET active = '0',
101
                inactive = '0',
102
                missing = '0',
103
                filtered = '0'");
104
105
        $pdo->query("
106
            UPDATE segment_data
107
            SET active = '1'
108
            WHERE domain IN (SELECT domain FROM domains WHERE active NOT IN ('0', '10'))");
109
110
        $pdo->query("
111
            UPDATE segment_data
112
            SET inactive = '1'
113
            WHERE domain IN (SELECT domain FROM domains WHERE active IN ('0', '10'))");
114
115
        $pdo->query("
116
            UPDATE segment_data
117
            SET missing = '1'
118
            WHERE domain NOT IN (SELECT domain FROM domains)");
119
    }
120
121
    public function updateAllFees()
122
    {
123
        $this->updateDomainFees();
124
        $this->updateSslFees();
125
    }
126
127
    public function updateDomainFees()
128
    {
129
        $pdo = $this->deeb->cnxx;
130
131
        $pdo->query("UPDATE domains SET fee_fixed = '0'");
132
133
        $stmt = $pdo->prepare("
134
            UPDATE fees
135
            SET fee_fixed = '0',
136
                update_time = :update_time");
137
        $timestamp = $this->time->stamp();
138
        $stmt->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
139
        $stmt->execute();
140
141
        $result = $pdo->query("
142
            SELECT id, registrar_id, tld
143
            FROM fees
144
            WHERE fee_fixed = '0'")->fetchAll();
145
146 View Code Duplication
        if ($result) {
147
148
            $stmt = $pdo->prepare("
149
                UPDATE domains
150
                SET fee_id = :fee_id
151
                WHERE registrar_id = :registrar_id
152
                  AND tld = :tld
153
                  AND fee_fixed = '0'");
154
            $stmt->bindParam('fee_id', $bind_fee_id, \PDO::PARAM_INT);
155
            $stmt->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
156
            $stmt->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
157
158
            $stmt2 = $pdo->prepare("
159
                UPDATE domains d
160
                JOIN fees f ON d.fee_id = f.id
161
                SET d.fee_fixed = '1',
162
                    d.total_cost = f.renewal_fee + f.privacy_fee + f.misc_fee
163
                WHERE d.registrar_id = :registrar_id
164
                  AND d.tld = :tld
165
                  AND d.privacy = '1'");
166
            $stmt2->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
167
            $stmt2->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
168
169
            $stmt3 = $pdo->prepare("
170
                UPDATE domains d
171
                JOIN fees f ON d.fee_id = f.id
172
                SET d.fee_fixed = '1',
173
                    d.total_cost = f.renewal_fee + f.misc_fee
174
                WHERE d.registrar_id = :registrar_id
175
                  AND d.tld = :tld
176
                  AND d.privacy = '0'");
177
            $stmt3->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
178
            $stmt3->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
179
180
            $stmt4 = $pdo->prepare("
181
                UPDATE fees
182
                SET fee_fixed = '1',
183
                    update_time = :update_time
184
                WHERE registrar_id = :registrar_id
185
                  AND tld = :tld");
186
            $timestamp = $this->time->stamp();
187
            $stmt4->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
188
            $stmt4->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
189
            $stmt4->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
190
191
            foreach ($result as $row) {
192
193
                $bind_fee_id = $row->id;
194
                $bind_registrar_id = $row->registrar_id;
195
                $bind_tld = $row->tld;
196
197
                $stmt->execute();
198
199
                $stmt2->execute();
200
201
                $stmt3->execute();
202
203
                $stmt4->execute();
204
205
            }
206
207
        }
208
    }
209
210
    public function updateDomainFee($domain_id)
211
    {
212
        $pdo = $this->deeb->cnxx;
213
214
        $stmt = $pdo->prepare("
215
            SELECT registrar_id, tld
216
            FROM domains
217
            WHERE id = :domain_id");
218
        $stmt->bindValue('domain_id', $domain_id, \PDO::PARAM_INT);
219
        $stmt->execute();
220
        $result = $stmt->fetch();
221
        $stmt->closeCursor();
222
223
        if ($result) {
224
225
            $registrar_id = $result->registrar_id;
226
            $tld = $result->tld;
227
228
        }
229
230
        $stmt = $pdo->prepare("
231
            UPDATE domains
232
            SET fee_fixed = '0'
233
            WHERE id = :domain_id");
234
        $stmt->bindValue('domain_id', $domain_id, \PDO::PARAM_INT);
235
        $stmt->execute();
236
237
        $stmt = $pdo->prepare("
238
            UPDATE fees
239
            SET fee_fixed = '0',
240
                update_time = :update_time
241
            WHERE registrar_id = :registrar_id
242
              AND tld = :tld");
243
        $timestamp = $this->time->stamp();
244
        $stmt->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
245
        $stmt->bindValue('registrar_id', $registrar_id, \PDO::PARAM_INT);
246
        $stmt->bindValue('tld', $tld, \PDO::PARAM_STR);
247
        $stmt->execute();
248
249
        $stmt = $pdo->prepare("
250
            SELECT id, registrar_id, tld
251
            FROM fees
252
            WHERE fee_fixed = '0'
253
              AND registrar_id = :registrar_id
254
              AND tld = :tld");
255
        $stmt->bindValue('registrar_id', $registrar_id, \PDO::PARAM_INT);
256
        $stmt->bindValue('tld', $tld, \PDO::PARAM_STR);
257
        $stmt->execute();
258
259
        $result = $stmt->fetchAll();
260
261 View Code Duplication
        if ($result) {
262
263
            $stmt = $pdo->prepare("
264
                UPDATE domains
265
                SET fee_id = :fee_id
266
                WHERE registrar_id = :registrar_id
267
                  AND tld = :tld
268
                  AND fee_fixed = '0'");
269
            $stmt->bindParam('fee_id', $bind_fee_id, \PDO::PARAM_INT);
270
            $stmt->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
271
            $stmt->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
272
273
            $stmt2 = $pdo->prepare("
274
                UPDATE domains d
275
                JOIN fees f ON d.fee_id = f.id
276
                SET d.fee_fixed = '1',
277
                    d.total_cost = f.renewal_fee + f.privacy_fee + f.misc_fee
278
                WHERE d.registrar_id = :registrar_id
279
                  AND d.tld = :tld
280
                  AND d.privacy = '1'");
281
            $stmt2->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
282
            $stmt2->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
283
284
            $stmt3 = $pdo->prepare("
285
                UPDATE domains d
286
                JOIN fees f ON d.fee_id = f.id
287
                SET d.fee_fixed = '1',
288
                    d.total_cost = f.renewal_fee + f.misc_fee
289
                WHERE d.registrar_id = :registrar_id
290
                  AND d.tld = :tld
291
                  AND d.privacy = '0'");
292
            $stmt3->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
293
            $stmt3->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
294
295
            $stmt4 = $pdo->prepare("
296
                UPDATE fees
297
                SET fee_fixed = '1',
298
                    update_time = :update_time
299
                WHERE registrar_id = :registrar_id
300
                  AND tld = :tld");
301
            $timestamp = $this->time->stamp();
302
            $stmt4->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
303
            $stmt4->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
304
            $stmt4->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
0 ignored issues
show
Bug introduced by
The variable $bind_tld seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
305
306
            foreach ($result as $row) {
307
308
                $bind_fee_id = $row->id;
309
                $bind_registrar_id = $row->registrar_id;
310
                $bind_tld = $row->tld;
311
312
                $stmt->execute();
313
314
                $stmt2->execute();
315
316
                $stmt3->execute();
317
318
                $stmt4->execute();
319
320
            }
321
322
        }
323
    }
324
325
    public function updateSslFees()
326
    {
327
        $pdo = $this->deeb->cnxx;
328
329
        $pdo->query("UPDATE ssl_certs SET fee_fixed = '0'");
330
331
        $stmt = $pdo->prepare("
332
            UPDATE ssl_fees
333
            SET fee_fixed = '0',
334
                update_time = :update_time");
335
        $timestamp = $this->time->stamp();
336
        $stmt->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
337
        $stmt->execute();
338
339
        $result = $pdo->query("
340
            SELECT id, ssl_provider_id, type_id
341
            FROM ssl_fees
342
            WHERE fee_fixed = '0'")->fetchAll();
343
344
        if ($result) {
345
346
            $stmt = $pdo->prepare("
347
                UPDATE ssl_certs
348
                SET fee_id = :fee_id
349
                WHERE ssl_provider_id = :ssl_provider_id
350
                  AND type_id = :type_id
351
                  AND fee_fixed = '0'");
352
            $stmt->bindParam('fee_id', $bind_fee_id, \PDO::PARAM_INT);
353
            $stmt->bindParam('ssl_provider_id', $bind_ssl_provider_id, \PDO::PARAM_INT);
354
            $stmt->bindParam('type_id', $bind_type_id, \PDO::PARAM_INT);
355
356
            $stmt2 = $pdo->prepare("
357
                UPDATE ssl_certs sslc
358
                JOIN ssl_fees sslf ON sslc.fee_id = sslf.id
359
                SET sslc.fee_fixed = '1',
360
                    sslc.total_cost = sslf.renewal_fee + sslf.misc_fee
361
                WHERE sslc.ssl_provider_id = :ssl_provider_id
362
                  AND sslc.type_id = :type_id");
363
            $stmt2->bindParam('ssl_provider_id', $bind_ssl_provider_id, \PDO::PARAM_INT);
364
            $stmt2->bindParam('type_id', $bind_type_id, \PDO::PARAM_INT);
365
366
            $stmt3 = $pdo->prepare("
367
                UPDATE ssl_fees
368
                SET fee_fixed = '1',
369
                    update_time = :update_time
370
                WHERE ssl_provider_id = :ssl_provider_id
371
                  AND type_id = :type_id");
372
            $timestamp = $this->time->stamp();
373
            $stmt3->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
374
            $stmt3->bindParam('ssl_provider_id', $bind_ssl_provider_id, \PDO::PARAM_INT);
375
            $stmt3->bindParam('type_id', $bind_type_id, \PDO::PARAM_INT);
376
377
            foreach ($result as $row) {
378
379
                $bind_fee_id = $row->id;
380
                $bind_ssl_provider_id = $row->ssl_provider_id;
381
                $bind_type_id = $row->type_id;
382
383
                $stmt->execute();
384
385
                $stmt2->execute();
386
387
                $stmt3->execute();
388
389
            }
390
391
        }
392
    }
393
394
    public function deleteUnusedFees($fee_table, $compare_table)
395
    {
396
        $this->deeb->cnxx->query("
397
            DELETE FROM " . $fee_table . "
398
            WHERE id NOT IN (
399
                             SELECT fee_id
400
                             FROM " . $compare_table . "
401
                            )");
402
    }
403
404
    public function zeroInvalidIpIds()
405
    { // This zeroes out API IP address IDs in the registrar_account table that are no longer valid. For example, if an
406
      // IP has been deleted.
407
        $pdo = $this->deeb->cnxx;
408
409
        $result = $pdo->query("
410
            SELECT id
411
            FROM ip_addresses")->fetchAll();
412
413
        if ($result) {
414
415
            $id_array = array();
416
417
            foreach ($result as $row) {
418
419
                $id_array[] = $row->id;
420
421
            }
422
423
            $in_list = str_repeat('?, ', count($id_array) - 1) . '?';
424
            $sql = "UPDATE registrar_accounts
425
                    SET api_ip_id = '0'
426
                    WHERE api_ip_id NOT IN (" . $in_list . ")";
427
            $stmt = $pdo->prepare($sql);
428
            $stmt->execute($id_array);
429
430
        }
431
432
    }
433
434
} //@formatter:on
435