-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_check.php
220 lines (195 loc) · 6.64 KB
/
system_check.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
require './inc/header.php';
require './inc/DatabaseHandler.php';
$test_result = [];
// ==================
// Quick directory check
// ==================
$dirs = ['./gen/', './inc/', './sql/'];
$missing_dir = [];
foreach ($dirs as $dir) {
if (!file_exists($dir) || !is_dir($dir)) {
$missing_dir[] = $dir;
}
}
$test_result[0] = new TestItem("Folders exist");
$test_result[1] = new TestItem("./gen/ folder writable");
if (empty($missing_dir)) {
if (!is_writable('./gen/')) {
$test_result[1]->setError("Cannot write!");
}
} else {
$test_result[0]->setError("Directories do not exist: " . implode(", ", $missing_dir));
$test_result[1]->setUndefined();
}
// =================
// Test for config file
// =================
unset($config);
$test_result[2] = new TestItem("./gen/config.php exists");
$config_ok = false;
if (file_exists('./gen/config.php')) {
include './gen/config.php';
if (!isset($config) || !is_array($config)) {
$test_result[2]->setError("File exists but could not find config data."
. " Please reset the config <a href=\"edit_config.php\">here</a>.");
}
} else {
$test_result[2]->setError("File ./gen/config.php does not exist! Please run "
. " the <a href=\"edit_config.php\">config module</a>.");
}
// ==================
// Test database
// ==================
$test_result[3] = new TestItem("Database login");
$test_result[4] = new TestItem("Retrieve database info");
$test_key = 0;
try {
if ($test_result[2]->cssClass !== 'testgood') {
throw new PDOException("noconfig", -12345);
}
$dbh = new DatabaseHandler($config);
$test_result[3]->message = 'Can login';
$total = $dbh->getTotalShows();
$test_result[4]->message = 'Could get shows. (Found ' . $total . ' shows)';
} catch (PDOException $ex) {
$test_key = empty($test_result[3]->message) ? 3 : 4;
register_db_error($test_result[$test_key], $ex);
if ($test_key == 3)
$test_result[4]->setUndefined();
else
$test_result[4]->message .= '<br>» <a href="?maketables">Create tables</a>';
}
// ==================
// Create tables if desired
// ==================
$message = '';
if (isset($_GET['maketables'])) {
if ($test_key == 3) {
$message = '<span class="error">Error! Cannot run database installation'
. ' because the login/database details are not correct.';
} else {
$sql = file_get_contents('./sql/create_tables.sql');
$dbh->getDbh()->exec($sql);
$message = 'Created any missing tables. Please run the '
. '<a href="?">status check</a> to verify that everything works.';
Template::displayTemplate('inc/system_check/system_check.html', [
'message' => $message,
'table' => '',
'show_footer' => false
]);
exit;
}
}
// ==================
// Test exec dot
// ==================
$test_result[5] = new TestItem("<code>exec()</code> is enabled");
$test_result[6] = new TestItem("Can use <code>dot</code> command");
if (!function_exists('exec')) {
$test_result[5]->setError("Function is disabled!");
$test_result[6]->setUndefined();
} else if (!isset($config['graph_command'])) {
$test_result[6]->setError("Could not find the config data to run command!");
} else {
try {
exec($config['graph_command'] . ' -?', $output, $code);
if ($code !== 0) {
$test_result[6]->setError("Command <code>" . htmlspecialchars($config['graph_command'])
. " -?</code> returned code " . $code . "; please ensure that "
. "the dot command is configured correctly in config.php");
}
} catch (Exception $ex) {
$test_result[6]->setError("Encountered exception " . $ex->getMessage());
}
}
// ==================
// Test cURL
// ==================
$test_result[7] = new TestItem("cURL extension is loaded");
$test_result[8] = new TestItem("Can connect to IMDb.com");
if (!function_exists('curl_init')) {
$test_result[7]->setError("cURL is not loaded! Please add the extension to PHP."
. "<br><small>You can still use this script but you cannot save any new show data.</small>");
$test_result[8]->setUndefined();
} else {
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.imdb.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
$info = curl_getinfo($ch);
if ($info['http_code'] !== 200) {
$test_result[8]->setError("Encountered HTTP code " . $info['http_code']
. " instead of expected 200! (Bad PHP setting? No internet connection? IMDb down?)"
. "<br><small>You can still use this script but you cannot save any new show data.</small>");
}
curl_close($ch);
} catch (Exception $ex) {
$test_result[8]->setError("Encountered unexpected exception: <br>"
. $ex->getMessage());
}
}
// ------------------
// Output tests
// ------------------
$table = '<table class="overview">';
foreach ($test_result as $key => $test) {
$bar = ($key % 2 === 1 && $key > 1) ? ' class="bar"' : '';
$table .= "\n <tr$bar><td>{$test->testName}</td>"
. "<td class=\"{$test->cssClass}\">" . $test->getMessage() . '</td></tr>';
}
$table .= '</table>';
Template::displayTemplate('inc/system_check/system_check.html', [
'table' => $table,
'message' => $message,
'show_footer' => true
]);
function register_db_error(TestItem $test_item, PDOException $ex) {
global $config;
switch ($ex->getCode()) {
case -12345:
$test_item->setUndefined();
return;
case 1049:
$test_item->setError("Could not find database <b>"
. htmlspecialchars($config['db_name']) . "</b>. Please create it or change config.php");
break;
case 1044:
$test_item->setError("Invalid login with user <b>"
. htmlspecialchars($config['db_user']) . "</b>. Please verify the details in config.php");
break;
case '42S02':
case '42S22':
$test_item->setError("Database or column does not exist!"
. " Please run database installation.");
break;
default:
$test_item->setError("Encountered an error!");
}
$test_item->message .= "<br><small>(" . $ex->getMessage() . ")</small>";
}
class TestItem {
public $testName;
public $message;
public $cssClass;
function __construct($testName) {
$this->testName = $testName;
$this->cssClass = 'testgood';
}
function setError($err) {
$this->message = $err;
$this->cssClass = 'testbad';
}
function getMessage() {
return ($this->cssClass === "testgood" ? '✓ ' : '✘ ')
. (empty($this->message) ? 'Yes' : $this->message);
}
function setUndefined() {
$this->message = 'Cannot evaluate (fix other error)';
$this->cssClass = 'testvoid';
}
}