Completed
Push — master ( b35b0f...acc097 )
by Greg
05:21
created

classes/DomainMOD/Maintenance.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
46
        $result_message = 'Maintenance Completed<BR>';
47
48
        return $result_message;
49
    }
50
51
    public function lowercaseDomains()
52
    {
53
        $this->system->db()->query("UPDATE domains SET domain = LOWER(domain)");
54
    }
55
56
    public function updateTlds()
57
    {
58
        $tmpq = $this->system->db()->query("SELECT id, domain FROM domains");
59
        $result = $tmpq->fetchAll();
60
61
        if ($result) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
62
63
            $tmpq = $this->system->db()->prepare("
64
                UPDATE domains
65
                SET tld = :tld
66
                WHERE id = :id");
67
68
            foreach ($result as $row) {
69
70
                $tld = $this->getTld($row->domain);
71
72
                $tmpq->execute(array(
73
                               'tld' => $tld,
74
                               'id' => $row->id));
75
76
            }
77
78
        }
79
    }
80
81
    public function getTld($domain)
82
    {
83
        return preg_replace("/^((.*?)\.)(.*)$/", "\\3", $domain);
84
    }
85
86 View Code Duplication
    public function updateSegments()
87
    {
88
        $this->system->db()->query("
89
            UPDATE segment_data
90
            SET active = '0',
91
                inactive = '0',
92
                missing = '0',
93
                filtered = '0'");
94
95
        $this->system->db()->query("
96
            UPDATE segment_data
97
            SET active = '1'
98
            WHERE domain IN (SELECT domain FROM domains WHERE active NOT IN ('0', '10'))");
99
100
        $this->system->db()->query("
101
            UPDATE segment_data
102
            SET inactive = '1'
103
            WHERE domain IN (SELECT domain FROM domains WHERE active IN ('0', '10'))");
104
105
        $this->system->db()->query("
106
            UPDATE segment_data
107
            SET missing = '1'
108
            WHERE domain NOT IN (SELECT domain FROM domains)");
109
    }
110
111
    public function updateAllFees()
112
    {
113
        $this->updateDomainFees();
114
        $this->updateSslFees();
115
    }
116
117
    public function updateDomainFees()
118
    {
119
        $this->system->db()->query("UPDATE domains SET fee_fixed = '0'");
120
121
        $tmpq = $this->system->db()->prepare("
122
            UPDATE fees
123
            SET fee_fixed = '0',
124
                update_time = :update_time");
125
        $tmpq->execute(array('update_time' => $this->time->stamp()));
126
127
        $tmpq = $this->system->db()->query("
128
            SELECT id, registrar_id, tld
129
            FROM fees
130
            WHERE fee_fixed = '0'");
131
        $result = $tmpq->fetchAll();
132
133 View Code Duplication
        if ($result) {
134
135
            $tmpq = $this->system->db()->prepare("
136
                UPDATE domains
137
                SET fee_id = :id
138
                WHERE registrar_id = :registrar_id
139
                  AND tld = :tld
140
                  AND fee_fixed = '0'");
141
142
            $tmpq2 = $this->system->db()->prepare("
143
                UPDATE domains d
144
                JOIN fees f ON d.fee_id = f.id
145
                SET d.fee_fixed = '1',
146
                    d.total_cost = f.renewal_fee + f.privacy_fee + f.misc_fee
147
                WHERE d.registrar_id = :registrar_id
148
                  AND d.tld = :tld
149
                  AND d.privacy = '1'");
150
151
            $tmpq3 = $this->system->db()->prepare("
152
                UPDATE domains d
153
                JOIN fees f ON d.fee_id = f.id
154
                SET d.fee_fixed = '1',
155
                    d.total_cost = f.renewal_fee + f.misc_fee
156
                WHERE d.registrar_id = :registrar_id
157
                  AND d.tld = :tld
158
                  AND d.privacy = '0'");
159
160
            $tmpq4 = $this->system->db()->prepare("
161
                UPDATE fees
162
                SET fee_fixed = '1',
163
                    update_time = :update_time
164
                WHERE registrar_id = :registrar_id
165
                  AND tld = :tld");
166
167
            foreach ($result as $row) {
168
169
                $tmpq->execute(array(
170
                               'id' => $row->id,
171
                               'registrar_id' => $row->registrar_id,
172
                               'tld' => $row->tld));
173
174
                $tmpq2->execute(array(
175
                                'registrar_id' => $row->registrar_id,
176
                                'tld' => $row->tld));
177
178
                $tmpq3->execute(array(
179
                                'registrar_id' => $row->registrar_id,
180
                                'tld' => $row->tld));
181
182
                $tmpq4->execute(array(
183
                                'update_time' => $this->time->stamp(),
184
                                'registrar_id' => $row->registrar_id,
185
                                'tld' => $row->tld));
186
187
            }
188
189
        }
190
    }
191
192
    public function updateDomainFee($domain_id)
193
    {
194
        $tmpq = $this->system->db()->prepare("
195
            SELECT registrar_id, tld
196
            FROM domains
197
            WHERE id = :domain_id");
198
        $tmpq->execute(array('domain_id' => $domain_id));
199
        $result = $tmpq->fetch();
200
201
        if ($result) {
202
203
            $registrar_id = $result->registrar_id;
204
            $tld = $result->tld;
205
206
        }
207
208
        $tmpq = $this->system->db()->prepare("
209
            UPDATE domains
210
            SET fee_fixed = '0'
211
            WHERE id = :domain_id");
212
        $tmpq->execute(array('domain_id' => $domain_id));
213
214
        $tmpq = $this->system->db()->prepare("
215
            UPDATE fees
216
            SET fee_fixed = '0',
217
                update_time = :update_time
218
            WHERE registrar_id = :registrar_id
219
              AND tld = :tld");
220
        $tmpq->execute(array(
221
                       'update_time' => $this->time->stamp(),
222
                       'registrar_id' => $registrar_id,
223
                       'tld' => $tld));
224
225
        $tmpq = $this->system->db()->prepare("
226
            SELECT id, registrar_id, tld
227
            FROM fees
228
            WHERE fee_fixed = '0'
229
              AND registrar_id = :registrar_id
230
              AND tld = :tld");
231
        $tmpq->execute(array(
232
                       'registrar_id' => $registrar_id,
233
                       'tld' => $tld));
234
        $result = $tmpq->fetchAll();
235
236 View Code Duplication
        if ($result) {
237
238
            $tmpq = $this->system->db()->prepare("
239
                UPDATE domains
240
                SET fee_id = :fee_id
241
                WHERE registrar_id = :registrar_id
242
                  AND tld = :tld
243
                  AND fee_fixed = '0'");
244
245
            $tmpq2 = $this->system->db()->prepare("
246
                UPDATE domains d
247
                JOIN fees f ON d.fee_id = f.id
248
                SET d.fee_fixed = '1',
249
                    d.total_cost = f.renewal_fee + f.privacy_fee + f.misc_fee
250
                WHERE d.registrar_id = :registrar_id
251
                  AND d.tld = :tld
252
                  AND d.privacy = '1'");
253
254
            $tmpq3 = $this->system->db()->prepare("
255
                UPDATE domains d
256
                JOIN fees f ON d.fee_id = f.id
257
                SET d.fee_fixed = '1',
258
                    d.total_cost = f.renewal_fee + f.misc_fee
259
                WHERE d.registrar_id = :registrar_id
260
                  AND d.tld = :tld
261
                  AND d.privacy = '0'");
262
263
            $tmpq4 = $this->system->db()->prepare("
264
                UPDATE fees
265
                SET fee_fixed = '1',
266
                    update_time = :update_time
267
                WHERE registrar_id = :registrar_id
268
                  AND tld = :tld");
269
270
            foreach ($result as $row) {
271
272
                $tmpq->execute(array(
273
                               'fee_id' => $row->id,
274
                               'registrar_id' => $row->registrar_id,
275
                               'tld' => $row->tld));
276
277
                $tmpq2->execute(array(
278
                                'registrar_id' => $row->registrar_id,
279
                                'tld' => $row->tld));
280
281
                $tmpq3->execute(array(
282
                                'registrar_id' => $row->registrar_id,
283
                                'tld' => $row->tld));
284
285
                $tmpq4->execute(array(
286
                                'update_time' => $this->time->stamp(),
287
                                'registrar_id' => $row->registrar_id,
288
                                'tld' => $row->tld));
289
290
            }
291
292
        }
293
    }
294
295
    public function updateSslFees()
296
    {
297
        $this->system->db()->query("UPDATE ssl_certs SET fee_fixed = '0'");
298
299
        $tmpq = $this->system->db()->prepare("
300
            UPDATE ssl_fees
301
            SET fee_fixed = '0',
302
                update_time = :update_time");
303
        $tmpq->execute(array('update_time' => $this->time->stamp()));
304
305
        $tmpq = $this->system->db()->query("
306
            SELECT id, ssl_provider_id, type_id
307
            FROM ssl_fees
308
            WHERE fee_fixed = '0'");
309
        $result = $tmpq->fetchAll();
310
311
        if ($result) {
312
313
            $tmpq = $this->system->db()->prepare("
314
                UPDATE ssl_certs
315
                SET fee_id = :id
316
                WHERE ssl_provider_id = :ssl_provider_id
317
                  AND type_id = :type_id
318
                  AND fee_fixed = '0'");
319
320
            $tmpq2 = $this->system->db()->prepare("
321
                UPDATE ssl_certs sslc
322
                JOIN ssl_fees sslf ON sslc.fee_id = sslf.id
323
                SET sslc.fee_fixed = '1',
324
                    sslc.total_cost = sslf.renewal_fee + sslf.misc_fee
325
                WHERE sslc.ssl_provider_id = :ssl_provider_id
326
                  AND sslc.type_id = :type_id");
327
328
            $tmpq3 = $this->system->db()->prepare("
329
                UPDATE ssl_fees
330
                SET fee_fixed = '1',
331
                    update_time = :update_time
332
                WHERE ssl_provider_id = :ssl_provider_id
333
                  AND type_id = :type_id");
334
335
            foreach ($result as $row) {
336
337
                $tmpq->execute(array(
338
                               'id' => $row->id,
339
                               'ssl_provider_id' => $row->ssl_provider_id,
340
                               'type_id' => $row->type_id));
341
342
                $tmpq2->execute(array(
343
                                'ssl_provider_id' => $row->ssl_provider_id,
344
                                'type_id' => $row->type_id));
345
346
                $tmpq3->execute(array(
347
                                'update_time' => $this->time->stamp(),
348
                                'ssl_provider_id' => $row->ssl_provider_id,
349
                                'type_id' => $row->type_id));
350
351
            }
352
353
        }
354
    }
355
356
    public function deleteUnusedFees($fee_table, $compare_table)
357
    {
358
        $this->system->db()->query("
359
            DELETE FROM " . $fee_table . "
360
            WHERE id NOT IN (
361
                             SELECT fee_id
362
                             FROM " . $compare_table . "
363
                            )");
364
    }
365
366
} //@formatter:on
367