Completed
Push — master ( 7db7a3...0359ff )
by Greg
05:08
created

Maintenance::zeroInvalidIpIds()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 13
nc 3
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 $log;
27
    public $system;
28
    public $time;
29
30
    public function __construct()
31
    {
32
        $this->log = new Log('maintenance.class');
33
        $this->system = new System();
34
        $this->time = new Time();
35
    }
36
37
    public function performCleanup()
38
    {
39
        $this->lowercaseDomains();
40
        $this->updateTlds();
41
        $this->updateSegments();
42
        $this->updateAllFees();
43
        $this->deleteUnusedFees('fees', 'domains');
44
        $this->deleteUnusedFees('ssl_fees', 'ssl_certs');
45
        $this->zeroInvalidIpIds();
46
47
        $result_message = 'Maintenance Completed<BR>';
48
49
        return $result_message;
50
    }
51
52
    public function lowercaseDomains()
53
    {
54
        $this->system->db()->query("UPDATE domains SET domain = LOWER(domain)");
55
    }
56
57
    public function updateTlds()
58
    {
59
        $pdo = $this->system->db();
60
61
        $stmt = $pdo->query("SELECT id, domain FROM domains");
62
        $result = $stmt->fetchAll();
63
64
        if ($result) {
65
66
            $pdo = $this->system->db();
67
            $stmt = $pdo->prepare("
68
                UPDATE domains
69
                SET tld = :tld
70
                WHERE id = :id");
71
            $stmt->bindParam('tld', $tld, \PDO::PARAM_STR);
72
            $stmt->bindParam('id', $bind_id, \PDO::PARAM_INT);
73
74
            foreach ($result as $row) {
75
76
                $tld = $this->getTld($row->domain);
77
                $bind_id = $row->id;
78
                $stmt->execute();
79
80
            }
81
82
        }
83
    }
84
85
    public function getTld($domain)
86
    {
87
        return preg_replace("/^((.*?)\.)(.*)$/", "\\3", $domain);
88
    }
89
90 View Code Duplication
    public function updateSegments()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $this->system->db()->query("
93
            UPDATE segment_data
94
            SET active = '0',
95
                inactive = '0',
96
                missing = '0',
97
                filtered = '0'");
98
99
        $this->system->db()->query("
100
            UPDATE segment_data
101
            SET active = '1'
102
            WHERE domain IN (SELECT domain FROM domains WHERE active NOT IN ('0', '10'))");
103
104
        $this->system->db()->query("
105
            UPDATE segment_data
106
            SET inactive = '1'
107
            WHERE domain IN (SELECT domain FROM domains WHERE active IN ('0', '10'))");
108
109
        $this->system->db()->query("
110
            UPDATE segment_data
111
            SET missing = '1'
112
            WHERE domain NOT IN (SELECT domain FROM domains)");
113
    }
114
115
    public function updateAllFees()
116
    {
117
        $this->updateDomainFees();
118
        $this->updateSslFees();
119
    }
120
121
    public function updateDomainFees()
122
    {
123
        $pdo = $this->system->db();
124
125
        $pdo->query("UPDATE domains SET fee_fixed = '0'");
126
127
        $stmt = $pdo->prepare("
128
            UPDATE fees
129
            SET fee_fixed = '0',
130
                update_time = :update_time");
131
        $timestamp = $this->time->stamp();
132
        $stmt->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
133
        $stmt->execute();
134
135
136
        $stmt = $pdo->query("
137
            SELECT id, registrar_id, tld
138
            FROM fees
139
            WHERE fee_fixed = '0'");
140
        $result = $stmt->fetchAll();
141
142 View Code Duplication
        if ($result) {
143
144
            $stmt = $pdo->prepare("
145
                UPDATE domains
146
                SET fee_id = :fee_id
147
                WHERE registrar_id = :registrar_id
148
                  AND tld = :tld
149
                  AND fee_fixed = '0'");
150
            $stmt->bindParam('fee_id', $bind_fee_id, \PDO::PARAM_INT);
151
            $stmt->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
152
            $stmt->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
153
154
            $stmt2 = $pdo->prepare("
155
                UPDATE domains d
156
                JOIN fees f ON d.fee_id = f.id
157
                SET d.fee_fixed = '1',
158
                    d.total_cost = f.renewal_fee + f.privacy_fee + f.misc_fee
159
                WHERE d.registrar_id = :registrar_id
160
                  AND d.tld = :tld
161
                  AND d.privacy = '1'");
162
            $stmt2->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
163
            $stmt2->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
164
165
            $stmt3 = $pdo->prepare("
166
                UPDATE domains d
167
                JOIN fees f ON d.fee_id = f.id
168
                SET d.fee_fixed = '1',
169
                    d.total_cost = f.renewal_fee + f.misc_fee
170
                WHERE d.registrar_id = :registrar_id
171
                  AND d.tld = :tld
172
                  AND d.privacy = '0'");
173
            $stmt3->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
174
            $stmt3->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
175
176
            $stmt4 = $pdo->prepare("
177
                UPDATE fees
178
                SET fee_fixed = '1',
179
                    update_time = :update_time
180
                WHERE registrar_id = :registrar_id
181
                  AND tld = :tld");
182
            $timestamp = $this->time->stamp();
183
            $stmt4->bindValue('update_time', $timestamp, \PDO::PARAM_STR);
184
            $stmt4->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
185
            $stmt4->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
186
187
            foreach ($result as $row) {
188
189
                $bind_registrar_id = $row->registrar_id;
190
                $bind_tld = $row->tld;
191
                $bind_fee_id = $row->id;
192
193
                $stmt->execute();
194
195
                $stmt2->execute();
196
197
                $stmt3->execute();
198
199
                $stmt4->execute();
200
201
            }
202
203
        }
204
    }
205
206
    public function updateDomainFee($domain_id)
207
    {
208
        $pdo = $this->system->db();
209
210
        $stmt = $pdo->prepare("
211
            SELECT registrar_id, tld
212
            FROM domains
213
            WHERE id = :domain_id");
214
        $stmt->bindValue('domain_id', $domain_id, \PDO::PARAM_INT);
215
        $stmt->execute();
216
        $result = $stmt->fetch();
217
218
        if ($result) {
219
220
            $registrar_id = $result->registrar_id;
221
            $tld = $result->tld;
222
223
        }
224
225
        $stmt = $pdo->prepare("
226
            UPDATE domains
227
            SET fee_fixed = '0'
228
            WHERE id = :domain_id");
229
        $stmt->bindValue('domain_id', $domain_id, \PDO::PARAM_INT);
230
        $stmt->execute();
231
232
        $stmt = $pdo->prepare("
233
            UPDATE fees
234
            SET fee_fixed = '0',
235
                update_time = :update_time
236
            WHERE registrar_id = :registrar_id
237
              AND tld = :tld");
238
        $bind_timestamp = $this->time->stamp();
239
        $stmt->bindValue('update_time', $bind_timestamp, \PDO::PARAM_STR);
240
        $stmt->bindValue('registrar_id', $registrar_id, \PDO::PARAM_INT);
241
        $stmt->bindValue('tld', $tld, \PDO::PARAM_STR);
242
        $stmt->execute();
243
244
        $stmt = $pdo->prepare("
245
            SELECT id, registrar_id, tld
246
            FROM fees
247
            WHERE fee_fixed = '0'
248
              AND registrar_id = :registrar_id
249
              AND tld = :tld");
250
        $stmt->bindValue('registrar_id', $registrar_id, \PDO::PARAM_INT);
251
        $stmt->bindValue('tld', $tld, \PDO::PARAM_STR);
252
        $stmt->execute();
253
254
        $result = $stmt->fetchAll();
255
256 View Code Duplication
        if ($result) {
257
258
            $stmt = $pdo->prepare("
259
                UPDATE domains
260
                SET fee_id = :fee_id
261
                WHERE registrar_id = :registrar_id
262
                  AND tld = :tld
263
                  AND fee_fixed = '0'");
264
            $stmt->bindParam('fee_id', $bind_fee_id, \PDO::PARAM_INT);
265
            $stmt->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
266
            $stmt->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
267
268
            $stmt2 = $pdo->prepare("
269
                UPDATE domains d
270
                JOIN fees f ON d.fee_id = f.id
271
                SET d.fee_fixed = '1',
272
                    d.total_cost = f.renewal_fee + f.privacy_fee + f.misc_fee
273
                WHERE d.registrar_id = :registrar_id
274
                  AND d.tld = :tld
275
                  AND d.privacy = '1'");
276
            $stmt2->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
277
            $stmt2->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
278
279
            $stmt3 = $pdo->prepare("
280
                UPDATE domains d
281
                JOIN fees f ON d.fee_id = f.id
282
                SET d.fee_fixed = '1',
283
                    d.total_cost = f.renewal_fee + f.misc_fee
284
                WHERE d.registrar_id = :registrar_id
285
                  AND d.tld = :tld
286
                  AND d.privacy = '0'");
287
            $stmt3->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
288
            $stmt3->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
289
290
            $stmt4 = $pdo->prepare("
291
                UPDATE fees
292
                SET fee_fixed = '1',
293
                    update_time = :update_time
294
                WHERE registrar_id = :registrar_id
295
                  AND tld = :tld");
296
            $bind_timestamp = $this->time->stamp();
297
            $stmt4->bindValue('update_time', $bind_timestamp, \PDO::PARAM_STR);
298
            $stmt4->bindParam('registrar_id', $bind_registrar_id, \PDO::PARAM_INT);
299
            $stmt4->bindParam('tld', $bind_tld, \PDO::PARAM_STR);
300
301
            foreach ($result as $row) {
302
303
                $bind_fee_id = $row->id;
304
                $bind_registrar_id = $row->registrar_id;
305
                $bind_tld = $row->tld;
306
307
                $stmt->execute();
308
309
                $stmt2->execute();
310
311
                $stmt3->execute();
312
313
                $stmt->execute();
314
315
            }
316
317
        }
318
    }
319
320
    public function updateSslFees()
321
    {
322
        $pdo = $this->system->db();
323
324
        $pdo->query("UPDATE ssl_certs SET fee_fixed = '0'");
325
326
        $stmt = $pdo->prepare("
327
            UPDATE ssl_fees
328
            SET fee_fixed = '0',
329
                update_time = :update_time");
330
        $bind_timestamp = $this->time->stamp();
331
        $stmt->bindValue('update_time', $bind_timestamp, \PDO::PARAM_STR);
332
        $stmt->execute();
333
334
        $stmt = $pdo->query("
335
            SELECT id, ssl_provider_id, type_id
336
            FROM ssl_fees
337
            WHERE fee_fixed = '0'");
338
        $result = $stmt->fetchAll();
339
340
        if ($result) {
341
342
            $stmt = $pdo->prepare("
343
                UPDATE ssl_certs
344
                SET fee_id = :fee_id
345
                WHERE ssl_provider_id = :ssl_provider_id
346
                  AND type_id = :type_id
347
                  AND fee_fixed = '0'");
348
            $stmt->bindParam('fee_id', $bind_fee_id, \PDO::PARAM_INT);
349
            $stmt->bindParam('ssl_provider_id', $bind_ssl_provider_id, \PDO::PARAM_INT);
350
            $stmt->bindParam('type_id', $bind_type_id, \PDO::PARAM_INT);
351
352
            $stmt2 = $pdo->prepare("
353
                UPDATE ssl_certs sslc
354
                JOIN ssl_fees sslf ON sslc.fee_id = sslf.id
355
                SET sslc.fee_fixed = '1',
356
                    sslc.total_cost = sslf.renewal_fee + sslf.misc_fee
357
                WHERE sslc.ssl_provider_id = :ssl_provider_id
358
                  AND sslc.type_id = :type_id");
359
            $stmt2->bindParam('ssl_provider_id', $bind_ssl_provider_id, \PDO::PARAM_INT);
360
            $stmt2->bindParam('type_id', $bind_type_id, \PDO::PARAM_INT);
361
362
            $stmt3 = $pdo->prepare("
363
                UPDATE ssl_fees
364
                SET fee_fixed = '1',
365
                    update_time = :update_time
366
                WHERE ssl_provider_id = :ssl_provider_id
367
                  AND type_id = :type_id");
368
            $bind_timestamp = $this->time->stamp();
369
            $stmt3->bindValue('update_time', $bind_timestamp, \PDO::PARAM_STR);
370
            $stmt3->bindParam('ssl_provider_id', $bind_ssl_provider_id, \PDO::PARAM_INT);
371
            $stmt3->bindParam('type_id', $bind_type_id, \PDO::PARAM_INT);
372
373
            foreach ($result as $row) {
374
375
                $bind_fee_id = $row->id;
376
                $bind_ssl_provider_id = $row->ssl_provider_id;
377
                $bind_type_id = $row->type_id;
378
379
                $stmt->execute();
380
381
                $stmt2->execute();
382
383
                $stmt3->execute();
384
385
            }
386
387
        }
388
    }
389
390
    public function deleteUnusedFees($fee_table, $compare_table)
391
    {
392
        $this->system->db()->query("
393
            DELETE FROM " . $fee_table . "
394
            WHERE id NOT IN (
395
                             SELECT fee_id
396
                             FROM " . $compare_table . "
397
                            )");
398
    }
399
400
    public function zeroInvalidIpIds()
401
    { // This zeroes out API IP address IDs in the registrar_account table that are no longer valid. For example, if an
402
      // IP has been deleted.
403
        $pdo = $this->system->db();
404
        $stmt = $pdo->query("
405
            SELECT id
406
            FROM ip_addresses");
407
        $result = $stmt->fetchAll();
408
409
        if ($result) {
410
411
            $id_array = array();
412
413
            foreach ($result as $row) {
414
415
                $id_array[] = $row->id;
416
417
            }
418
419
            $in_list = str_repeat('?, ', count($id_array) - 1) . '?';
420
            $sql = "UPDATE registrar_accounts
421
                    SET api_ip_id = '0'
422
                    WHERE api_ip_id NOT IN (" . $in_list . ")";
423
            $stmt = $pdo->prepare($sql);
424
            $stmt->execute($id_array);
425
426
        }
427
428
    }
429
430
} //@formatter:on
431