Passed
Push — master ( f14fa1...c29029 )
by Greg
02:55
created

System::showMessageInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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