-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
admin_debug.php
335 lines (260 loc) · 9.99 KB
/
admin_debug.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
require_once "inc_all_admin.php";
require_once "database_version.php";
require_once "config.php";
$folderPath = 'uploads';
function countFilesInDirectory($dir) {
$count = 0;
$size = 0;
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$filePath = $dir . '/' . $file;
if (is_file($filePath)) {
$count++;
$size += filesize($filePath);
} elseif (is_dir($filePath)) {
$result = countFilesInDirectory($filePath);
$count += $result['count'];
$size += $result['size'];
}
}
return [
'count' => $count,
'size' => $size
];
}
// Function to compare two arrays recursively and return the differences
function arrayDiffRecursive($array1, $array2) {
$diff = array();
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if (!isset($array2[$key]) || !is_array($array2[$key])) {
$diff[$key] = $value;
} else {
$recursiveDiff = arrayDiffRecursive($value, $array2[$key]);
if (!empty($recursiveDiff)) {
$diff[$key] = $recursiveDiff;
}
}
} else {
if (!isset($array2[$key]) || $array2[$key] !== $value) {
$diff[$key] = $value;
}
}
}
return $diff;
}
// Function to load the table structures from an SQL dump file URL
function loadTableStructuresFromSQLDumpURL($fileURL) {
$context = stream_context_create(array('http' => array('header' => 'Accept: application/octet-stream')));
$fileContent = file_get_contents($fileURL, false, $context);
if ($fileContent === false) {
return null;
}
$structure = array();
$queries = explode(";", $fileContent);
foreach ($queries as $query) {
$query = trim($query);
if (!empty($query)) {
if (preg_match("/^CREATE TABLE `(.*)` \((.*)\)$/s", $query, $matches)) {
$tableName = $matches[1];
$tableStructure = $matches[2];
$structure[$tableName] = array('structure' => $tableStructure);
}
}
}
return $structure;
}
// Function to fetch the database structure from the MySQL server
function fetchDatabaseStructureFromServer() {
global $mysqli;
$tables = array();
// Fetch table names
$result = $mysqli->query("SHOW TABLES");
if ($result->num_rows > 0) {
while ($row = $result->fetch_row()) {
$tableName = $row[0];
$tables[$tableName] = array();
}
}
// Fetch table structures
foreach ($tables as $tableName => &$table) {
$result = $mysqli->query("SHOW CREATE TABLE `$tableName`");
if ($result->num_rows > 0) {
$row = $result->fetch_row();
$table['structure'] = $row[1];
}
}
return $tables;
}
//function to get current crontab and return it as an array
function get_crontab() {
$crontab = shell_exec('crontab -l');
$crontab = explode(PHP_EOL, $crontab);
return $crontab;
}
// URL to the SQL dump file
$fileURL = "https://raw.githubusercontent.com/itflow-org/itflow/master/db.sql";
// Load the desired table structures from the SQL dump file URL
$desiredStructure = loadTableStructuresFromSQLDumpURL($fileURL);
if ($desiredStructure === null) {
die("Failed to load the desired table structures from the SQL dump file URL.");
}
// Fetch the current database structure from the MySQL server
$currentStructure = fetchDatabaseStructureFromServer();
if ($currentStructure === null) {
die("Failed to fetch the current database structure from the server.");
}
// Compare the structures and display the differences
$differences = arrayDiffRecursive($desiredStructure, $currentStructure);
//DB Stats
// Query to fetch the number of tables
$tablesQuery = "SHOW TABLES";
$tablesResult = $mysqli->query($tablesQuery);
$numTables = $tablesResult->num_rows;
$numFields = 0;
$numRows = 0;
// Loop through each table
while ($row = $tablesResult->fetch_row()) {
$tableName = $row[0];
// Query to fetch the number of fields
$fieldsQuery = "DESCRIBE `$tableName`";
$fieldsResult = $mysqli->query($fieldsQuery);
// Check if the query was successful
if ($fieldsResult) {
$numFields += $fieldsResult->num_rows;
// Query to fetch the number of rows
$rowsQuery = "SELECT COUNT(*) FROM `$tableName`";
$rowsResult = $mysqli->query($rowsQuery);
// Check if the query was successful
if ($rowsResult) {
$numRows += $rowsResult->fetch_row()[0];
} else {
echo "Error executing query: " . $mysqli->error;
}
} else {
echo "Error executing query: " . $mysqli->error;
}
}
//Get loaded PHP modules
$loadedModules = get_loaded_extensions();
//Get Server Info / Service versions
$phpVersion = phpversion();
$databaseInfo = mysqli_get_server_info($mysqli) . " / " . $mysqli->server_version;
$operatingSystem = php_uname();
$webServer = $_SERVER['SERVER_SOFTWARE'];
$errorLog = ini_get('error_log') ?: "Debian/Ubuntu default is usually /var/log/apache2/error.log";
$updates = fetchUpdates();
?>
<div class="card card-dark">
<div class="card-header py-3">
<h3 class="card-title"><i class="fas fa-fw fa-bug mr-2"></i>Debug</h3>
</div>
<div class="card-body">
<h2>Debugging</h2>
<ul>
<li>If you are experiencing a problem with ITFlow you may be directed to this page to gather server/app info.</li>
<li>When creating forum posts / support requests ensure you share the information under <i>Server Info</i>, <i>ITFlow app</i> and <i>Database stats</i>.</li>
<li><a class="text-danger text-bold">Caution:</a> Be careful when sharing the full debug output - it contains your PHP session variables/cookies ("PHPSESSID") which could allow anyone to login to your ITFlow instance</li>
<li>Note: Sometimes you might need to gather <a href="https://docs.itflow.org/gathering_logs#error_logs">PHP error logs</a> as well</li>
</ul>
<br>
<h3>Server Info</h3>
<?php
echo "PHP version: " . $phpVersion . "<br>";
echo "Database Version: " . $databaseInfo . "<br>";
echo "Operating System: " . $operatingSystem . "<br>";
echo "Web Server: " . $webServer . "<br>";
echo "Apache/PHP Error Log: " . $errorLog
?>
<hr>
<h3>File System</h3>
<?php
$result = countFilesInDirectory($folderPath);
$totalFiles = $result['count'];
$totalSizeMB = round($result['size'] / (1024 * 1024), 2);
echo "Total number of files in $folderPath and its subdirectories: " . $totalFiles . "<br>";
echo "Total size of files in $folderPath and its subdirectories: " . $totalSizeMB . " MB";
?>
<hr>
<h3>ITFlow app</h3>
<?php
echo "App Version: " . $updates->current_version . "<br>";
echo "Cron enabled: " . $config_enable_cron . "<br>";
echo "App Timezone: " . $config_timezone;
?>
<hr>
<h3>Database Structure Check</h3>
<h4>Database stats</h4>
<?php
echo "Number of tables: " . $numTables . "<br>";
echo "Total number of fields: " . $numFields . "<br>";
echo "Total number of rows: " . $numRows . "<br>";
echo "Current Database Version: " . CURRENT_DATABASE_VERSION . "<br>";
?>
<hr>
<h4>Table Stats</h4>
<?php
// Fetch all table names from the database
$tables = array();
$result = mysqli_query($mysqli, "SHOW TABLES");
while ($row = mysqli_fetch_array($result)) {
$tables[] = $row[0];
}
// Generate an HTML table to display the results
?>
<table class="table table-sm">
<tr>
<th>Table Name</th>
<th>Number of Fields</th>
<th>Number of Rows</th>
</tr>
<?php
foreach ($tables as $table) {
// Count the number of fields and rows for each table
$columns_result = mysqli_query($mysqli, "SHOW COLUMNS FROM `$table`");
$columns = mysqli_num_rows($columns_result);
$rows_result = mysqli_query($mysqli, "SELECT COUNT(*) FROM `$table`");
$rows = mysqli_fetch_array($rows_result)[0];
?>
<tr>
<td><?php echo $table; ?></td>
<td><?php echo $columns; ?></td>
<td><?php echo $rows; ?></td>
</tr>
<?php
}
?>
</table>
<hr>
<h3>PHP Modules Installed</h3>
<?php
foreach ($loadedModules as $module) {
echo $module . "<br>";
}
?>
<hr>
<h3>PHP Info</h3>
<?php
//Output phpinfo, but in a way that doesnt mess up the page
ob_start();
phpinfo();
$phpinfo = ob_get_contents();
ob_end_clean();
//Remove everything before the body tag
$phpinfo = preg_replace('%^.*<body>(.*)</body>.*$%ms', '$1', $phpinfo);
//Remove everything after the body tag
$phpinfo = preg_replace('%^(.*)</body>.*$%ms', '$1', $phpinfo);
//Remove the body tag itself
$phpinfo = preg_replace('%^<body>(.*)$%ms', '$1', $phpinfo);
//Output the result
echo $phpinfo;
?>
<hr>
</div>
</div>
<?php
require_once "footer.php";