Completed
Branch dev (623cfe)
by Greg
03:59
created

classes/DomainMOD/Domain.php (5 issues)

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/Domain.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 Domain
25
{
26
    public $log;
27
    public $system;
28
    public $time;
29
30
    public function __construct()
31
    {
32
        $this->log = new Log('domain.class');
33
        $this->system = new System();
34
        $this->time = new Time();
35
    }
36
37
    public function findInvalidDomains($lines)
38
    {
39
        $invalid_to_display = 5;
40
        $invalid_domains = 0;
41
        $invalid_count = 0;
42
        $result_message = '';
43
44
        while (list($key, $domain) = each($lines)) {
45
46
            if (!$this->checkFormat($domain)) {
47
48
                if ($invalid_count < $invalid_to_display) {
49
50
                    $result_message .= "Line " . number_format($key + 1) . " contains an invalid domain<BR>";
51
52
                }
53
54
                $invalid_domains = 1;
55
                $invalid_count++;
56
57
            }
58
59
        }
60
61
        return array($invalid_to_display, $invalid_domains, $invalid_count, $result_message);
62
    }
63
64
    public function checkFormat($input_domain)
65
    {
66
/*
67
        if (preg_match('/^[A-Z0-9.-]+\.[A-Z0-9-]{2,50}$/i', $input_domain, $output_domain)) {
68
69
            return $output_domain;
70
71
        } else {
72
73
            return false;
74
75
        }
76
*/
77
        return $input_domain;
78
    }
79
80
    public function renew($domain, $renewal_years, $notes)
1 ignored issue
show
Coding Style Naming introduced by
The parameter $renewal_years is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
81
    {
82
        $expiry_date = $this->getExpiry($domain);
83
        $new_expiry = $this->getNewExpiry($expiry_date, $renewal_years);
84
        $this->writeNewExpiry($domain, $new_expiry, $notes);
85
    }
86
87 View Code Duplication
    public function getDomain($domain_id)
1 ignored issue
show
Coding Style Naming introduced by
The parameter $domain_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
88
    {
89
        $pdo = $this->system->db();
90
91
        $stmt = $pdo->prepare("
92
            SELECT domain
93
            FROM domains
94
            WHERE id = :domain_id");
95
        $stmt->bindValue('domain_id', $domain_id, \PDO::PARAM_INT);
96
        $stmt->execute();
97
        $result = $stmt->fetchColumn();
98
99
        if (!$result) {
100
101
            $log_message = "Unable to retrieve domain";
102
            $log_extra = array('Domain ID' => $domain_id);
103
            $this->log->error($log_message, $log_extra);
104
            return $log_message;
105
106
        } else {
107
108
            return $result;
109
110
        }
111
    }
112
113 View Code Duplication
    public function getExpiry($domain)
114
    {
115
        $pdo = $this->system->db();
116
117
        $stmt = $pdo->prepare("
118
            SELECT expiry_date
119
            FROM domains
120
            WHERE domain = :domain");
121
        $stmt->bindValue('domain', $domain, \PDO::PARAM_STR);
122
        $stmt->execute();
123
        $result = $stmt->fetchColumn();
124
125
        if (!$result) {
126
127
            $log_message = "Unable to retrieve domain's expiry date";
128
            $log_extra = array('Domain' => $domain);
129
            $this->log->error($log_message, $log_extra);
130
            return $log_message;
131
132
        } else {
133
134
            return $result;
135
136
        }
137
    }
138
139
    public function getNewExpiry($expiry_date, $renewal_years)
2 ignored issues
show
Coding Style Naming introduced by
The parameter $expiry_date is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $renewal_years is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
140
    {
141
        $expiry_pieces = explode("-", $expiry_date);
142
        return $expiry_pieces[0] + $renewal_years . "-" . $expiry_pieces[1] . "-" . $expiry_pieces[2];
143
    }
144
145
    public function writeNewExpiry($domain, $new_expiry, $notes)
1 ignored issue
show
Coding Style Naming introduced by
The parameter $new_expiry is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
146
    {
147
        $pdo = $this->system->db();
148
149
        if ($notes != '') {
150
151
            $stmt = $pdo->prepare("
152
                UPDATE domains
153
                SET expiry_date = :new_expiry,
154
                    notes = CONCAT(:notes, '\r\n\r\n', notes),
155
                    update_time = :update_time
156
                WHERE domain = :domain");
157
            $stmt->bindValue('new_expiry', $new_expiry, \PDO::PARAM_STR);
158
            $stmt->bindValue('notes', $notes, \PDO::PARAM_LOB);
159
            $bind_timestamp = $this->time->stamp();
160
            $stmt->bindValue('update_time', $bind_timestamp, \PDO::PARAM_STR);
161
            $stmt->bindValue('domain', $domain, \PDO::PARAM_STR);
162
            $stmt->execute();
163
164 View Code Duplication
        } else {
165
166
            $stmt = $pdo->prepare("
167
                UPDATE domains
168
                SET expiry_date = :new_expiry,
169
                    update_time = :update_time
170
                WHERE domain = :domain");
171
            $stmt->bindValue('new_expiry', $new_expiry, \PDO::PARAM_STR);
172
            $bind_timestamp = $this->time->stamp();
173
            $stmt->bindValue('update_time', $bind_timestamp, \PDO::PARAM_STR);
174
            $stmt->bindValue('domain', $domain, \PDO::PARAM_STR);
175
            $stmt->execute();
176
177
        }
178
    }
179
180
    public function getTld($domain)
181
    {
182
        return preg_replace("/^((.*?)\.)(.*)$/", "\\3", $domain);
183
    }
184
185
    public function getDomainPart($domain)
186
    {
187
        $temp = preg_replace("/^((.*?)\.)(.*)$/", "\\1", $domain);
188
        return rtrim($temp, ".");
189
    }
190
191
} //@formatter:on
192