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

System::getCreationTypeId()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
/**
3
 * /classes/DomainMOD/System.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 System
25
{
26
    public $log;
27
28
    public function __construct()
29
    {
30
        $this->log = new Log('system.class');
31
    }
32
33
    public function db()
1 ignored issue
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
34
    {
35
        $pdo = new \PDO("mysql:host=" . DB_HOSTNAME . ";dbname=" . DB_NAME . ";charset=utf8", DB_USERNAME, DB_PASSWORD);
36
        $pdo->exec("SET NAMES utf8");
37
        $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
38
        $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);
39
        $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
40
        return $pdo;
41
    }
42
43
    public function installCheck()
44
    {
45
        $full_install_path = DIR_ROOT . '/install/';
46
47
        $result = $this->checkForSettingsTable();
48
49
        if (!$result && is_dir($full_install_path)) {
50
51
            $installation_mode = 1;
52
            $result_message = 'DomainMOD is not yet installed<BR>';
53
54
        } else {
55
56
            $installation_mode = 0;
57
            $result_message = '';
58
59
        }
60
61
        return array($installation_mode, $result_message);
62
    }
63
64
    public function checkForSettingsTable()
65
    {
66
        $pdo = $this->db();
67
        $stmt = $pdo->query("SHOW TABLES LIKE 'settings'");
68
        return $stmt->fetchColumn();
69
    }
70
71
    public function checkVersion($current_version)
72
    {
73
        $live_version = $this->getLiveVersion();
74
75
        if ($current_version < $live_version && $live_version != '') {
76
77
            $this->db()->query("UPDATE settings SET upgrade_available = '1'");
78
            $_SESSION['s_system_upgrade_available'] = '1';
79
            $message = $this->getUpgradeMessage();
80
81
        } else {
82
83
            $this->db()->query("UPDATE settings SET upgrade_available = '0'");
84
            $_SESSION['s_system_upgrade_available'] = '0';
85
            $message = 'No Upgrade Available';
86
87
        }
88
        return $message;
89
    }
90
91
    public function getLiveVersion()
92
    {
93
        $version_file = 'https://raw.githubusercontent.com/domainmod/domainmod/master/version.txt';
94
        $context = stream_context_create(array('https' => array('header' => 'Connection: close\r\n')));
95
        $version_fgc = file_get_contents($version_file, false, $context);
96
        if ($version_fgc) {
97
            $live_version = $version_fgc;
98
        } else {
99
            $handle = curl_init();
100
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
101
            curl_setopt($handle, CURLOPT_URL, $version_file);
102
            $result = curl_exec($handle);
103
            curl_close($handle);
104
            $live_version = $result;
105
        }
106
        return $live_version;
107
    }
108
109
    public function getDbVersion()
110
    {
111
        $pdo = $this->db();
112
        $stmt = $pdo->query("
113
            SELECT db_version
114
            FROM settings");
115
        return $stmt->fetchColumn();
116
    }
117
118
    public function getUpgradeMessage()
119
    {
120
        return "A new version of DomainMOD is available for download. <a target=\"_blank\"
121
                href=\"http://domainmod.org/upgrade/\">Click here for upgrade instructions</a>.<BR>";
122
    }
123
124
    public function pageTitle($page_title)
125
    {
126
        return SOFTWARE_TITLE . ' :: ' . $page_title;
127
    }
128
129
    public function checkExistingAssets()
130
    {
131
        $queryB = new QueryBuild();
132
133
        $sql = $queryB->singleAsset('registrars');
134
        $_SESSION['s_has_registrar'] = $this->checkForRows($sql);
135
        $sql = $queryB->singleAsset('registrar_accounts');
136
        $_SESSION['s_has_registrar_account'] = $this->checkForRows($sql);
137
        $sql = $queryB->singleAsset('domains');
138
        $_SESSION['s_has_domain'] = $this->checkForRows($sql);
139
        $sql = $queryB->singleAsset('ssl_providers');
140
        $_SESSION['s_has_ssl_provider'] = $this->checkForRows($sql);
141
        $sql = $queryB->singleAsset('ssl_accounts');
142
        $_SESSION['s_has_ssl_account'] = $this->checkForRows($sql);
143
        $sql = $queryB->singleAsset('ssl_certs');
144
        $_SESSION['s_has_ssl_cert'] = $this->checkForRows($sql);
145
    }
146
147
    public function checkForRows($sql)
148
    {
149
        $pdo = $this->db();
150
        $stmt = $pdo->query($sql);
151
        $result = $stmt->fetchColumn();
152
        if (!$result) {
153
            return '0';
154
        } else {
155
            return '1';
156
        }
157
    }
158
159
    public function authCheck()
160
    {
161
        if ($_SESSION['s_is_logged_in'] != 1) {
162
            $_SESSION['s_user_redirect'] = $_SERVER["REQUEST_URI"];
163
            $_SESSION['s_message_danger'] .= 'You must be logged in to access this area<BR>';
164
            header('Location: ' . WEB_ROOT . '/');
165
            exit;
166
        }
167
    }
168
169
    public function readOnlyCheck($redirect_url)
170
    {
171
        if ($_SESSION['s_read_only'] == '1') {
172
            $_SESSION['s_message_danger'] .= "You are not authorized to perform that action<BR>";
173
            $temp_redirect_url = urlencode($redirect_url);
174
            header('Location: ' . $temp_redirect_url);
175
            exit;
176
        }
177
    }
178
179
    public function loginCheck()
180
    {
181
        if ($_SESSION['s_is_logged_in'] == 1) {
182
            header('Location: ' . WEB_ROOT . '/dashboard/');
183
            exit;
184
        }
185
    }
186
187
    public function checkAdminUser($is_admin)
188
    {
189
        if ($is_admin !== 1) {
190
            header('Location: ' . WEB_ROOT . "/invalid.php");
191
            exit;
192
        }
193
    }
194
195
    public function getDebugMode()
196
    {
197
        $pdo = $this->db();
198
        $result = $this->checkForSettingsTable();
199
        if (!$result) return '0';
200
        $stmt = $pdo->query("SHOW COLUMNS FROM `settings` LIKE 'debug_mode'");
201
        if ($stmt === false) return '0';
202
        $result = $stmt->fetchColumn();
203
        if (!$result) {
204
            return '0';
205
        } else {
206
            $stmt2 = $pdo->query("SELECT debug_mode FROM settings");
207
            return $stmt2->fetchColumn();
208
        }
209
    }
210
211
    public function showMessageSuccess($result_message)
212
    {
213
        ob_start(); ?>
214
        <BR>
215
        <div class="alert alert-success alert-dismissible">
216
        <?php /* ?>
217
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
218
        <?php */ ?>
219
            <h4><i class="icon fa fa-check"></i> Success</h4>
220
            <?php echo $result_message; ?>
221
        </div><?php
222
        return ob_get_clean();
223
    }
224
225
    public function showMessageDanger($result_message)
226
    {
227
        ob_start(); ?>
228
        <BR>
229
        <div class="alert alert-danger alert-dismissible">
230
            <h4><i class="icon fa fa-exclamation-circle"></i> Alert!</h4>
231
            <?php echo $result_message; ?>
232
        </div><?php
233
        return ob_get_clean();
234
    }
235
236
    public function showMaintenanceTable($result_message)
237
    {
238
        ob_start(); ?>
239
        <BR>
240
        <div class="alert alert-warning alert-dismissible">
241
            <h4><i class="icon fa fa-exclamation-triangle"></i> Attention Required!</h4>
242
            <?php echo $result_message; ?>
243
        </div><?php
244
        return ob_get_clean();
245
    }
246
247
    public function showDebugTable($result_message)
248
    {
249
        ob_start(); ?>
250
        <BR>
251
        <div class="alert alert-info alert-dismissible bg-aqua-active">
252
            <h4><i class="icon fa fa-info-circle"></i> Info</h4>
253
            <?php echo $result_message; ?>
254
        </div><?php
255
        return ob_get_clean();
256
    }
257
258
    public function dynamicQuery($dbcon, $query, $params1, $params2, $binding)
259
    {
260
        $error = new Error();
261
        $qrun = $dbcon->stmt_init();
262
        if ($qrun->prepare($query)) {
263
264
            call_user_func_array(array($qrun, 'bind_param'), array_merge(array($params1), $params2));
265
            $qrun->execute();
266
            $qrun->store_result();
267
            call_user_func_array(array($qrun, 'bind_result'), $binding);
268
269
        } else $error->outputSqlError($dbcon, '1', 'ERROR');
270
        return $qrun;
271
    }
272
273 View Code Duplication
    public function getCreationType($creation_type_id)
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...
274
    {
275
        $pdo = $this->db();
276
        $stmt = $pdo->prepare("
277
            SELECT `name`
278
            FROM creation_types
279
            WHERE id = :creation_type_id");
280
        $stmt->bindValue('creation_type_id', $creation_type_id, \PDO::PARAM_INT);
281
        $stmt->execute();
282
        $result = $stmt->fetchColumn();
283
284
        if (!$result) {
285
286
            $log_message = 'Unable to retrieve creation type';
287
            $log_extra = array('Creation Type ID' => $creation_type_id);
288
            $this->log->error($log_message, $log_extra);
0 ignored issues
show
Documentation introduced by
$log_extra is of type array<string,?,{"Creation Type ID":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
289
            return $log_message;
290
291
        } else {
292
293
            return $result;
294
295
        }
296
    }
297
298 View Code Duplication
    public function getCreationTypeId($creation_type)
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...
299
    {
300
        $pdo = $this->db();
301
        $stmt = $pdo->prepare("
302
            SELECT id
303
            FROM creation_types
304
            WHERE `name` = :creation_type");
305
        $stmt->bindValue('creation_type', $creation_type, \PDO::PARAM_STR);
306
        $stmt->execute();
307
        $result = $stmt->fetchColumn();
308
309
        if (!$result) {
310
311
            $log_message = 'Unable to retrieve creation type ID';
312
            $log_extra = array('Creation Type' => $creation_type, 'Result' => $result);
313
            $this->log->error($log_message, $log_extra);
0 ignored issues
show
Documentation introduced by
$log_extra is of type array<string,?,{"Creatio...ype":"?","Result":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
314
            return $log_message;
315
316
        } else {
317
318
            return $result;
319
320
        }
321
    }
322
323
} //@formatter:on
324