Passed
Push — master ( f92a2b...39bc56 )
by Greg
03:14
created

System::getIpRemotely()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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-2018 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 $deeb;
27
    public $log;
28
    public $layout;
29
30
    public function __construct()
31
    {
32
        $this->deeb = Database::getInstance();
33
        $this->log = new Log('class.system');
34
        $this->layout = new Layout();
35
    }
36
37
    public function getRequirements()
38
    {
39
        list($req_text, $req_html) = $this->getReqServerSoft();
40
        list($req_text, $req_html) = $this->getReqExtensions($req_text, $req_html);
41
        list($req_text, $req_html) = $this->getReqSettings($req_text, $req_html);
42
        return array($req_text, $req_html);
43
    }
44
45
    public function getReqServerSoft()
46
    {
47
        $req_text = '';
48
        $req_html = '';
49
50
        // SERVER SOFTWARE
51
        $req_text .= 'Server Software: ';
52
        $req_html .= '<STRONG>Server Software</STRONG><BR>';
53
54
        // PHP
55
        $software = 'PHP v5.3.2+';
56
        $min_php_version = '5.3.2';
57
        $installed_php_version = phpversion();
58
59
        if ($installed_php_version >= $min_php_version) {
60
61
            $req_text .= $software . ': Pass, ';
62
            $req_html .= $software . ': ' . $this->layout->highlightText('green', 'Pass') . '<BR>';
63
64
        } else {
65
66
            $req_text .= $software . ': Fail, ';
67
            $req_html .= $software . ': ' . $this->layout->highlightText('red', 'Fail') . '<BR>';
68
69
        }
70
71
        // MySQL
72
        $software = 'MySQL';
73
        if (extension_loaded('pdo_mysql')) {
74
75
            $req_text .= $software . ': Pass';
76
            $req_html .= $software . ': ' . $this->layout->highlightText('green', 'Pass') . '<BR>';
77
78
        } else {
79
80
            $req_text .= $software . ': Fail';
81
            $req_html .= $software . ': ' . $this->layout->highlightText('red', 'Fail') . '<BR>';
82
83
        }
84
85
        return array($req_text, $req_html);
86
    }
87
88
    public function getReqExtensions($req_text, $req_html)
89
    {
90
        // PHP Extensions
91
        $req_text .= ' / PHP Extensions: ';
92
        $req_html .= '<BR><STRONG>PHP Extensions</STRONG><BR>';
93
94
        $extensions = array('pdo_mysql' => 'PDO (MySQL)',
95
                            'curl' => 'cURL',
96
                            'openssl' => 'OpenSSL');
97
98
        foreach ($extensions as $key => $value) {
99
100
            if (extension_loaded($key)) {
101
102
                $req_text .= $value . ': Enabled, ';
103
                $req_html .= $value . ': ' . $this->layout->highlightText('green', 'Enabled') . '<BR>';
104
105
            } else {
106
107
                $req_text .= $value . ': Disabled, ';
108
                $req_html .= $value . ': ' . $this->layout->highlightText('red', 'Disabled') . '<BR>';
109
110
            }
111
112
        }
113
114
        $req_text = substr($req_text, 0, -2);
115
116
        return array($req_text, $req_html);
117
    }
118
119
    public function getReqSettings($req_text, $req_html)
120
    {
121
        // PHP SETTINGS
122
        $req_text .= ' / PHP Settings: ';
123
        $req_html .= '<BR><STRONG>PHP Settings</STRONG><BR>';
124
125
        $settings = array('allow_url_fopen');
126
127
        foreach ($settings as $value) {
128
129
            if (ini_get($value)) {
130
131
                $req_text .= $value . ': Enabled, ';
132
                $req_html .= $value . ': ' . $this->layout->highlightText('green', 'Enabled') . '<BR>';
133
134
            } else {
135
136
                $req_text .= $value . ': Disabled, ';
137
                $req_html .= $value . ': ' . $this->layout->highlightText('red', 'Disabled') . '<BR>';
138
139
            }
140
141
        }
142
143
        $req_text = substr($req_text, 0, -2);
144
145
        return array($req_text, $req_html);
146
    }
147
148
    public function installMode()
149
    {
150
        $result = $this->checkForSettingsTable();
151
        $install_mode = !$result ? 1 : 0;
152
        return $install_mode;
153
    }
154
155
    public function checkForSettingsTable()
156
    {
157
        return $this->deeb->cnxx->query("SHOW TABLES LIKE 'settings'")->fetchColumn();
158
    }
159
160
    public function checkVersion($current_version)
161
    {
162
        $pdo = $this->deeb->cnxx;
163
        $live_version = $this->getLiveVersion();
164
165
        if ($current_version < $live_version && $live_version != '') {
166
167
            $pdo->query("UPDATE settings SET upgrade_available = '1'");
168
            $_SESSION['s_system_upgrade_available'] = '1';
169
            $message = $this->getUpgradeMessage();
170
171
        } else {
172
173
            $pdo->query("UPDATE settings SET upgrade_available = '0'");
174
            $_SESSION['s_system_upgrade_available'] = '0';
175
            $message = 'No Upgrade Available';
176
177
        }
178
        return $message;
179
    }
180
181
    public function getLiveVersion()
182
    {
183
        $version_file = 'https://raw.githubusercontent.com/domainmod/domainmod/master/version.txt';
184
        return $this->getFileContents('Get Live Version', 'error', $version_file);
185
    }
186
187
    public function getDbVersion()
188
    {
189
        return $this->deeb->cnxx->query("
190
            SELECT db_version
191
            FROM settings")->fetchColumn();
192
    }
193
194
    public function getUpgradeMessage()
195
    {
196
        return "A new version of DomainMOD is available for download. <a target=\"_blank\"
197
                href=\"http://domainmod.org/upgrade/\">Click here for upgrade instructions</a>.<BR>";
198
    }
199
200
    public function pageTitle($page_title)
201
    {
202
        return SOFTWARE_TITLE . ' :: ' . $page_title;
203
    }
204
205
    public function checkExistingAssets()
206
    {
207
        $queryB = new QueryBuild();
208
209
        $sql = $queryB->singleAsset('registrars');
210
        $_SESSION['s_has_registrar'] = $this->checkForRows($sql);
211
        $sql = $queryB->singleAsset('registrar_accounts');
212
        $_SESSION['s_has_registrar_account'] = $this->checkForRows($sql);
213
        $sql = $queryB->singleAsset('domains');
214
        $_SESSION['s_has_domain'] = $this->checkForRows($sql);
215
        $sql = $queryB->singleAsset('ssl_providers');
216
        $_SESSION['s_has_ssl_provider'] = $this->checkForRows($sql);
217
        $sql = $queryB->singleAsset('ssl_accounts');
218
        $_SESSION['s_has_ssl_account'] = $this->checkForRows($sql);
219
        $sql = $queryB->singleAsset('ssl_certs');
220
        $_SESSION['s_has_ssl_cert'] = $this->checkForRows($sql);
221
    }
222
223
    public function checkForRows($sql)
224
    {
225
        $result = $this->deeb->cnxx->query($sql)->fetchColumn();
226
        if (!$result) {
227
            return '0';
228
        } else {
229
            return '1';
230
        }
231
    }
232
233
    public function authCheck()
234
    {
235
        if ($_SESSION['s_is_logged_in'] != 1) {
236
            $_SESSION['s_user_redirect'] = $_SERVER["REQUEST_URI"];
237
            $_SESSION['s_message_danger'] .= 'You must be logged in to access this area<BR>';
238
            header('Location: ' . WEB_ROOT . '/');
239
            exit;
240
        }
241
    }
242
243
    public function installCheck()
244
    {
245
        if ($this->installMode() === 0) {
246
            $_SESSION['s_message_danger'] .= SOFTWARE_TITLE . " is already installed<BR><BR>You should delete the /install/ folder<BR>";
247
            header('Location: ' . WEB_ROOT . '/');
248
            exit;
249
        }
250
    }
251
252
    public function readOnlyCheck($redirect_url)
253
    {
254
        if ($_SESSION['s_read_only'] == '1') {
255
            $_SESSION['s_message_danger'] .= "You are not authorized to perform that action<BR>";
256
            $temp_redirect_url = urlencode($redirect_url);
257
            header('Location: ' . $temp_redirect_url);
258
            exit;
259
        }
260
    }
261
262
    public function loginCheck()
263
    {
264
        if ($_SESSION['s_is_logged_in'] == 1) {
265
            header('Location: ' . WEB_ROOT . '/dashboard/');
266
            exit;
267
        }
268
    }
269
270
    public function checkAdminUser($is_admin)
271
    {
272
        if ($is_admin !== 1) {
273
            header('Location: ' . WEB_ROOT . "/invalid.php");
274
            exit;
275
        }
276
    }
277
278
    public function getDebugMode()
279
    {
280
        $pdo = $this->deeb->cnxx;
281
        $result = $this->checkForSettingsTable();
282
        if (!$result) return '0';
283
        $stmt = $pdo->query("SHOW COLUMNS FROM `settings` LIKE 'debug_mode'");
284
        if ($stmt === false) return '0';
285
        $result = $stmt->fetchColumn();
286
        if (!$result) {
287
            return '0';
288
        } else {
289
            return $pdo->query("SELECT debug_mode FROM settings")->fetchColumn();
290
        }
291
    }
292
293
    public function showMessageSuccess($result_message)
294
    {
295
        ob_start(); ?>
296
        <BR>
297
        <div class="alert alert-success alert-dismissible">
298
        <?php /* ?>
299
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
300
        <?php */ ?>
301
            <h4><i class="icon fa fa-check"></i> Success</h4>
302
            <?php echo $result_message; ?>
303
        </div><?php
304
        return ob_get_clean();
305
    }
306
307
    public function showMessageDanger($result_message)
308
    {
309
        ob_start(); ?>
310
        <BR>
311
        <div class="alert alert-danger alert-dismissible">
312
            <h4><i class="icon fa fa-exclamation-circle"></i> Alert!</h4>
313
            <?php echo $result_message; ?>
314
        </div><?php
315
        return ob_get_clean();
316
    }
317
318
    public function showMaintenanceTable($result_message)
319
    {
320
        ob_start(); ?>
321
        <BR>
322
        <div class="alert alert-warning alert-dismissible">
323
            <h4><i class="icon fa fa-exclamation-triangle"></i> Attention Required!</h4>
324
            <?php echo $result_message; ?>
325
        </div><?php
326
        return ob_get_clean();
327
    }
328
329
    public function showDebugTable($result_message)
330
    {
331
        ob_start(); ?>
332
        <BR>
333
        <div class="alert alert-info alert-dismissible bg-aqua-active">
334
            <h4><i class="icon fa fa-info-circle"></i> Info</h4>
335
            <?php echo $result_message; ?>
336
        </div><?php
337
        return ob_get_clean();
338
    }
339
340
    public function getCreationType($creation_type_id)
341
    {
342
        $pdo = $this->deeb->cnxx;
343
        $stmt = $pdo->prepare("
344
            SELECT `name`
345
            FROM creation_types
346
            WHERE id = :creation_type_id");
347
        $stmt->bindValue('creation_type_id', $creation_type_id, \PDO::PARAM_INT);
348
        $stmt->execute();
349
        $result = $stmt->fetchColumn();
350
351
        if (!$result) {
352
353
            $log_message = 'Unable to retrieve creation type';
354
            $log_extra = array('Creation Type ID' => $creation_type_id);
355
            $this->log->critical($log_message, $log_extra);
356
            return $log_message;
357
358
        } else {
359
360
            return $result;
361
362
        }
363
    }
364
365
    public function getCreationTypeId($creation_type)
366
    {
367
        $pdo = $this->deeb->cnxx;
368
        $stmt = $pdo->prepare("
369
            SELECT id
370
            FROM creation_types
371
            WHERE `name` = :creation_type");
372
        $stmt->bindValue('creation_type', $creation_type, \PDO::PARAM_STR);
373
        $stmt->execute();
374
        $result = $stmt->fetchColumn();
375
376
        if (!$result) {
377
378
            $log_message = 'Unable to retrieve creation type ID';
379
            $log_extra = array('Creation Type' => $creation_type, 'Result' => $result);
380
            $this->log->critical($log_message, $log_extra);
381
            return $log_message;
382
383
        } else {
384
385
            return $result;
386
387
        }
388
    }
389
390
    public function getFileContents($file_title, $log_severity, $filename)
391
    {
392
393
        if (ini_get('allow_url_fopen') && extension_loaded('openssl')) {
394
395
            $file_contents = $this->getFileContFopen($filename);
396
397
        } elseif (extension_loaded('curl')) {
398
399
            $file_contents = $this->getFileContCurl($filename);
400
401
        } else {
402
403
            $log_message = 'Unable to get file contents';
404
            list($requirements, $null) = $this->getRequirements();
405
            $log_extra = array('File Title' => $file_title, 'Requirements' => $requirements);
406
            $this->log->{$log_severity}($log_message, $log_extra);
407
            $file_contents = '';
408
409
        }
410
411
        return $file_contents;
412
    }
413
414
    public function getFileContFopen($filename)
415
    {
416
        $context = stream_context_create(array('https' => array('header' => 'Connection: close\r\n')));
417
        return file_get_contents($filename, false, $context);
418
    }
419
420
    public function getFileContCurl($filename)
421
    {
422
        $handle = curl_init();
423
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
424
        curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
425
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
426
        curl_setopt($handle, CURLOPT_URL, $filename);
427
        $result = curl_exec($handle);
428
        curl_close($handle);
429
        return $result;
430
    }
431
432
    public function getIpRemotely()
433
    {
434
        return $this->getFileContents('External IP API Call (ipify)', 'warning', 'https://api.ipify.org');
435
    }
436
437
} //@formatter:on
438