Completed
Push — master ( 6ef077...1ad84f )
by Greg
04:21
created

Maintenance::lowercaseTlds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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-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 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
222
        if ($result) {
223
224
            $registrar_id = $result->registrar_id;
225
            $tld = $result->tld;
226
227
        }
228
229
        $stmt = $pdo->prepare("
230
            UPDATE domains
231
            SET fee_fixed = '0'
232
            WHERE id = :domain_id");
233
        $stmt->bindValue('domain_id', $domain_id, \PDO::PARAM_INT);
234
        $stmt->execute();
235
236
        $stmt = $pdo->prepare("
237
            UPDATE fees
238
            SET fee_fixed = '0',
239
                update_time = :update_time
240
            WHERE registrar_id = :registrar_id
241
              AND tld = :tld");
242
        $timestamp = $this->time->stamp();
243
        $stmt->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
244
        $stmt->bindValue('registrar_id', $registrar_id, \PDO::PARAM_INT);
245
        $stmt->bindValue('tld', $tld, \PDO::PARAM_STR);
246
        $stmt->execute();
247
248
        $stmt = $pdo->prepare("
249
            SELECT id, registrar_id, tld
250
            FROM fees
251
            WHERE fee_fixed = '0'
252
              AND registrar_id = :registrar_id
253
              AND tld = :tld");
254
        $stmt->bindValue('registrar_id', $registrar_id, \PDO::PARAM_INT);
255
        $stmt->bindValue('tld', $tld, \PDO::PARAM_STR);
256
        $stmt->execute();
257
258
        $result = $stmt->fetchAll();
259
260 View Code Duplication
        if ($result) {
261
262
            $stmt = $pdo->prepare("
263
                UPDATE domains
264
                SET fee_id = :fee_id
265
                WHERE registrar_id = :registrar_id
266
                  AND tld = :tld
267
                  AND fee_fixed = '0'");
268
            $stmt->bindParam('fee_id', $bind_fee_id, \PDO::PARAM_INT);
269
            $stmt->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
270
            $stmt->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
271
272
            $stmt2 = $pdo->prepare("
273
                UPDATE domains d
274
                JOIN fees f ON d.fee_id = f.id
275
                SET d.fee_fixed = '1',
276
                    d.total_cost = f.renewal_fee + f.privacy_fee + f.misc_fee
277
                WHERE d.registrar_id = :registrar_id
278
                  AND d.tld = :tld
279
                  AND d.privacy = '1'");
280
            $stmt2->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
281
            $stmt2->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
282
283
            $stmt3 = $pdo->prepare("
284
                UPDATE domains d
285
                JOIN fees f ON d.fee_id = f.id
286
                SET d.fee_fixed = '1',
287
                    d.total_cost = f.renewal_fee + f.misc_fee
288
                WHERE d.registrar_id = :registrar_id
289
                  AND d.tld = :tld
290
                  AND d.privacy = '0'");
291
            $stmt3->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
292
            $stmt3->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
293
294
            $stmt4 = $pdo->prepare("
295
                UPDATE fees
296
                SET fee_fixed = '1',
297
                    update_time = :update_time
298
                WHERE registrar_id = :registrar_id
299
                  AND tld = :tld");
300
            $timestamp = $this->time->stamp();
301
            $stmt4->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
302
            $stmt4->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
303
            $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...
304
305
            foreach ($result as $row) {
306
307
                $bind_fee_id = $row->id;
308
                $bind_registrar_id = $row->registrar_id;
309
                $bind_tld = $row->tld;
310
311
                $stmt->execute();
312
313
                $stmt2->execute();
314
315
                $stmt3->execute();
316
317
                $stmt4->execute();
318
319
            }
320
321
        }
322
    }
323
324
    public function updateSslFees()
325
    {
326
        $pdo = $this->deeb->cnxx;
327
328
        $pdo->query("UPDATE ssl_certs SET fee_fixed = '0'");
329
330
        $stmt = $pdo->prepare("
331
            UPDATE ssl_fees
332
            SET fee_fixed = '0',
333
                update_time = :update_time");
334
        $timestamp = $this->time->stamp();
335
        $stmt->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
336
        $stmt->execute();
337
338
        $result = $pdo->query("
339
            SELECT id, ssl_provider_id, type_id
340
            FROM ssl_fees
341
            WHERE fee_fixed = '0'")->fetchAll();
342
343
        if ($result) {
344
345
            $stmt = $pdo->prepare("
346
                UPDATE ssl_certs
347
                SET fee_id = :fee_id
348
                WHERE ssl_provider_id = :ssl_provider_id
349
                  AND type_id = :type_id
350
                  AND fee_fixed = '0'");
351
            $stmt->bindParam('fee_id', $bind_fee_id, \PDO::PARAM_INT);
352
            $stmt->bindParam('ssl_provider_id', $bind_ssl_provider_id, \PDO::PARAM_INT);
353
            $stmt->bindParam('type_id', $bind_type_id, \PDO::PARAM_INT);
354
355
            $stmt2 = $pdo->prepare("
356
                UPDATE ssl_certs sslc
357
                JOIN ssl_fees sslf ON sslc.fee_id = sslf.id
358
                SET sslc.fee_fixed = '1',
359
                    sslc.total_cost = sslf.renewal_fee + sslf.misc_fee
360
                WHERE sslc.ssl_provider_id = :ssl_provider_id
361
                  AND sslc.type_id = :type_id");
362
            $stmt2->bindParam('ssl_provider_id', $bind_ssl_provider_id, \PDO::PARAM_INT);
363
            $stmt2->bindParam('type_id', $bind_type_id, \PDO::PARAM_INT);
364
365
            $stmt3 = $pdo->prepare("
366
                UPDATE ssl_fees
367
                SET fee_fixed = '1',
368
                    update_time = :update_time
369
                WHERE ssl_provider_id = :ssl_provider_id
370
                  AND type_id = :type_id");
371
            $timestamp = $this->time->stamp();
372
            $stmt3->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
373
            $stmt3->bindParam('ssl_provider_id', $bind_ssl_provider_id, \PDO::PARAM_INT);
374
            $stmt3->bindParam('type_id', $bind_type_id, \PDO::PARAM_INT);
375
376
            foreach ($result as $row) {
377
378
                $bind_fee_id = $row->id;
379
                $bind_ssl_provider_id = $row->ssl_provider_id;
380
                $bind_type_id = $row->type_id;
381
382
                $stmt->execute();
383
384
                $stmt2->execute();
385
386
                $stmt3->execute();
387
388
            }
389
390
        }
391
    }
392
393
    public function deleteUnusedFees($fee_table, $compare_table)
394
    {
395
        $this->deeb->cnxx->query("
396
            DELETE FROM " . $fee_table . "
397
            WHERE id NOT IN (
398
                             SELECT fee_id
399
                             FROM " . $compare_table . "
400
                            )");
401
    }
402
403
    public function zeroInvalidIpIds()
404
    { // This zeroes out API IP address IDs in the registrar_account table that are no longer valid. For example, if an
405
      // IP has been deleted.
406
        $pdo = $this->deeb->cnxx;
407
408
        $result = $pdo->query("
409
            SELECT id
410
            FROM ip_addresses")->fetchAll();
411
412
        if ($result) {
413
414
            $id_array = array();
415
416
            foreach ($result as $row) {
417
418
                $id_array[] = $row->id;
419
420
            }
421
422
            $in_list = str_repeat('?, ', count($id_array) - 1) . '?';
423
            $sql = "UPDATE registrar_accounts
424
                    SET api_ip_id = '0'
425
                    WHERE api_ip_id NOT IN (" . $in_list . ")";
426
            $stmt = $pdo->prepare($sql);
427
            $stmt->execute($id_array);
428
429
        }
430
431
    }
432
433
} //@formatter:on
434