| 1 | <?php
|
|---|
| 2 | /**
|
|---|
| 3 | * Debugging functions
|
|---|
| 4 | *
|
|---|
| 5 | * PHP version 5
|
|---|
| 6 | *
|
|---|
| 7 | * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
|
|---|
| 8 | * modify it under the terms of the GNU General Public License as
|
|---|
| 9 | * published by the Free Software Foundation, either version 3 of
|
|---|
| 10 | * the License, or (at your option) any later version.
|
|---|
| 11 | *
|
|---|
| 12 | * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
|
|---|
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|---|
| 14 | * FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 15 | *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License along
|
|---|
| 17 | * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
|
|---|
| 18 | *
|
|---|
| 19 | * @category Content Management System
|
|---|
| 20 | * @package HotaruCMS
|
|---|
| 21 | * @author Nick Ramsay <admin@hotarucms.org>
|
|---|
| 22 | * @copyright Copyright (c) 2009, Hotaru CMS
|
|---|
| 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
|
|---|
| 24 | * @link http://www.hotarucms.org/
|
|---|
| 25 | */
|
|---|
| 26 | class Debug
|
|---|
| 27 | {
|
|---|
| 28 | protected $fh = array(); // file handlers
|
|---|
| 29 | protected $log = array(); // file paths
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Shows number of database queries and the time it takes for a page to load
|
|---|
| 33 | */
|
|---|
| 34 | public function showQueriesAndTime($h)
|
|---|
| 35 | {
|
|---|
| 36 | if ($h->isDebug) {
|
|---|
| 37 |
|
|---|
| 38 | $mysql_version = $h->db->get_var("SELECT VERSION() AS VE");
|
|---|
| 39 |
|
|---|
| 40 | echo "<p class='debug'>";
|
|---|
| 41 | echo $h->lang['main_hotaru_db_queries'] . $h->db->num_queries . " | ";
|
|---|
| 42 | echo $h->lang['main_hotaru_page_load_time'] . timer_stop(1) . $h->lang['main_times_secs'] . " | ";
|
|---|
| 43 | echo $h->lang['main_hotaru_memory_usage'] . display_filesize(memory_get_usage()) . " | ";
|
|---|
| 44 | echo $h->lang['main_hotaru_php_version'] . phpversion() . " | ";
|
|---|
| 45 | echo $h->lang['main_hotaru_mysql_version'] . $mysql_version . " | ";
|
|---|
| 46 | echo $h->lang['main_hotaru_hotaru_version'] . $h->version;
|
|---|
| 47 | echo "</p>";
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | if ($h->currentUser->loggedIn) {echo "<span id='loggedIn' class='loggedIn_true'/>"; } else {"<span id='loggedIn' class='loggedIn_false'/>";}
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Open file for logging
|
|---|
| 56 | *
|
|---|
| 57 | * @param string $type "speed", "error", etc.
|
|---|
| 58 | * @param string $mode e.g. 'a' or 'w'.
|
|---|
| 59 | * @link http://php.net/manual/en/function.fopen.php
|
|---|
| 60 | */
|
|---|
| 61 | public function openLog($type = 'debug', $mode = 'a+')
|
|---|
| 62 | {
|
|---|
| 63 | $this->log[$type] = CACHE . "debug_logs/" . $type . ".php";
|
|---|
| 64 |
|
|---|
| 65 | // delete file if over 1MB
|
|---|
| 66 | if (file_exists($this->log[$type]) && (filesize($this->log[$type]) > 1000000)) {
|
|---|
| 67 | unlink($this->log[$type]);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | // If doesn't exist or rewriting, create a new file with die() at the top
|
|---|
| 71 | if (!file_exists($this->log[$type]) || ($mode != 'a' && $mode != 'a+')) {
|
|---|
| 72 | $this->fh[$type] = fopen($this->log[$type], $mode) or die("Sorry, I can't open cache/debug_logs/" . $type . ".php");
|
|---|
| 73 | fwrite($this->fh[$type], "<?php die(); ?>\r\n");
|
|---|
| 74 | } else {
|
|---|
| 75 | // open existing file:
|
|---|
| 76 | $this->fh[$type] = fopen($this->log[$type], $mode) or die("can't open file");
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Log performance and errors
|
|---|
| 83 | *
|
|---|
| 84 | * @param string $type "error", "speed", etc.
|
|---|
| 85 | */
|
|---|
| 86 | public function writeLog($type = 'debug', $string = '')
|
|---|
| 87 | {
|
|---|
| 88 | if ($string) {
|
|---|
| 89 | $string = date('d M Y H:i:s', time()) . " " . $string . "\n";
|
|---|
| 90 | fwrite($this->fh[$type], $string);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Close log file
|
|---|
| 97 | *
|
|---|
| 98 | * @param string $type "speed", "error", etc.
|
|---|
| 99 | */
|
|---|
| 100 | public function closeLog($type = 'debug')
|
|---|
| 101 | {
|
|---|
| 102 | if (isset($this->fh[$type])) { fclose($this->fh[$type]); }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Generate a System Report
|
|---|
| 108 | *
|
|---|
| 109 | * @param string $type 'log' or 'object'
|
|---|
| 110 | */
|
|---|
| 111 | public function generateReport($h, $type = 'log')
|
|---|
| 112 | {
|
|---|
| 113 | $report = $this->getSystemData($h);
|
|---|
| 114 |
|
|---|
| 115 | if ($type == 'object') { return $report; }
|
|---|
| 116 |
|
|---|
| 117 | $h->openLog('system_report', 'w');
|
|---|
| 118 |
|
|---|
| 119 | // convert object to text
|
|---|
| 120 | $output = $this->logSystemReport($h, $report);
|
|---|
| 121 | if ($output) {
|
|---|
| 122 | $h->writeLog('system_report', $output);
|
|---|
| 123 | $h->closeLog('system_report');
|
|---|
| 124 |
|
|---|
| 125 | $h->message = $h->lang['admin_maintenance_system_report_success'];
|
|---|
| 126 | $h->messageType = 'green';
|
|---|
| 127 | } else {
|
|---|
| 128 | $h->message = $h->lang['admin_maintenance_system_report_failure'];
|
|---|
| 129 | $h->messageType = 'red';
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Get system data
|
|---|
| 135 | *
|
|---|
| 136 | * @param string $type 'log' or 'object'
|
|---|
| 137 | * @return object
|
|---|
| 138 | */
|
|---|
| 139 | public function getSystemData($h)
|
|---|
| 140 | {
|
|---|
| 141 | // essentials:
|
|---|
| 142 |
|
|---|
| 143 | $report['hotaru_site_name'] = SITE_NAME;
|
|---|
| 144 | $report['hotaru_baseurl'] = BASEURL;
|
|---|
| 145 |
|
|---|
| 146 | $report['php_version'] = phpversion();
|
|---|
| 147 | $report['mysql_version'] = $h->db->get_var("SELECT VERSION() AS VE");
|
|---|
| 148 | $report['hotaru_version'] = $h->version;
|
|---|
| 149 |
|
|---|
| 150 | $sql = "SELECT miscdata_value FROM " . TABLE_MISCDATA . " WHERE miscdata_key = %s";
|
|---|
| 151 | $report['hotaru_version_db'] = $h->db->get_var($h->db->prepare($sql, 'hotaru_version'));
|
|---|
| 152 |
|
|---|
| 153 | // default permissions
|
|---|
| 154 |
|
|---|
| 155 | $sql = "SELECT miscdata_value FROM " . TABLE_MISCDATA . " WHERE miscdata_key = %s";
|
|---|
| 156 | $report['hotaru_permissions'] = $h->db->get_var($h->db->prepare($sql, 'permissions'));
|
|---|
| 157 |
|
|---|
| 158 | // default user settings
|
|---|
| 159 |
|
|---|
| 160 | $sql = "SELECT miscdata_value FROM " . TABLE_MISCDATA . " WHERE miscdata_key = %s";
|
|---|
| 161 | $report['hotaru_user_settings'] = $h->db->get_var($h->db->prepare($sql, 'user_settings'));
|
|---|
| 162 |
|
|---|
| 163 | // plugins: folder, enabled, version, order
|
|---|
| 164 |
|
|---|
| 165 | $sql = "SELECT plugin_folder, plugin_enabled, plugin_version, plugin_order FROM " . TABLE_PLUGINS . " ORDER BY plugin_order";
|
|---|
| 166 | $plugins = $h->db->get_results($h->db->prepare($sql));
|
|---|
| 167 | if ($plugins) {
|
|---|
| 168 | foreach ($plugins as $plugin) {
|
|---|
| 169 | $report['hotaru_plugins'][$plugin->plugin_folder]['enabled'] = $plugin->plugin_enabled;
|
|---|
| 170 | $report['hotaru_plugins'][$plugin->plugin_folder]['version'] = $plugin->plugin_version;
|
|---|
| 171 | $report['hotaru_plugins'][$plugin->plugin_folder]['order'] = $plugin->plugin_order;
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | // plugin hooks: id, folder, hook name
|
|---|
| 176 |
|
|---|
| 177 | $sql = "SELECT phook_id, plugin_folder, plugin_hook FROM " . TABLE_PLUGINHOOKS;
|
|---|
| 178 | $plugins = $h->db->get_results($h->db->prepare($sql));
|
|---|
| 179 | if ($plugins) {
|
|---|
| 180 | foreach ($plugins as $plugin) {
|
|---|
| 181 | $report['hotaru_plugin_hooks'][$plugin->phook_id]['folder'] = $plugin->plugin_folder;
|
|---|
| 182 | $report['hotaru_plugin_hooks'][$plugin->phook_id]['hook'] = $plugin->plugin_hook;
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | // plugin settings: folder, setting (can't use value because might include passwords)
|
|---|
| 187 |
|
|---|
| 188 | $sql = "SELECT plugin_folder, plugin_setting, plugin_value FROM " . TABLE_PLUGINSETTINGS;
|
|---|
| 189 | $plugins = $h->db->get_results($h->db->prepare($sql));
|
|---|
| 190 | if ($plugins) {
|
|---|
| 191 | // foreach ($plugins as $plugin) {
|
|---|
| 192 | // if (is_serialized($plugin->plugin_value)) {
|
|---|
| 193 | // $plugin->plugin_value = unserialize($plugin->plugin_value);
|
|---|
| 194 | // if (is_array($plugin->plugin_value)) {
|
|---|
| 195 | // foreach ($plugin->plugin_value as $key => $value) {
|
|---|
| 196 | // if (is_array($value)) {
|
|---|
| 197 | // foreach ($value as $k => $v) {
|
|---|
| 198 | // if (is_array($v)) {
|
|---|
| 199 | // $v = serialize($v);
|
|---|
| 200 | // $plugin->plugin_value[$key][$k] = $this->aaa($v);
|
|---|
| 201 | // } else {
|
|---|
| 202 | // $plugin->plugin_value[$key][$k] = preg_replace("/[a-zA-Z0-9]/", "*", $v);
|
|---|
| 203 | // }
|
|---|
| 204 | // }
|
|---|
| 205 | // } else {
|
|---|
| 206 | // $plugin->plugin_value[$key] = preg_replace("/[a-zA-Z0-9]/", "*", $value);
|
|---|
| 207 | // }
|
|---|
| 208 | // }
|
|---|
| 209 | // $plugin->plugin_value = serialize($plugin->plugin_value);
|
|---|
| 210 | // }
|
|---|
| 211 | // } else {
|
|---|
| 212 | // $plugin->plugin_value = preg_replace("/[a-zA-Z0-9]/", "*", $plugin->plugin_value);
|
|---|
| 213 | // }
|
|---|
| 214 | // $report['hotaru_plugin_settings'][$plugin->plugin_folder][$plugin->plugin_setting] = $plugin->plugin_value;
|
|---|
| 215 | // }
|
|---|
| 216 |
|
|---|
| 217 | print_r ($this->loop_through_array($h, $plugins));
|
|---|
| 218 |
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | // Settings: Name, value (excluding SMTP PASSWORD)
|
|---|
| 222 |
|
|---|
| 223 | $sql = "SELECT settings_name, settings_value FROM " . TABLE_SETTINGS;
|
|---|
| 224 | $settings = $h->db->get_results($h->db->prepare($sql));
|
|---|
| 225 | if ($settings) {
|
|---|
| 226 | foreach ($settings as $setting) {
|
|---|
| 227 | // mask sensitive data
|
|---|
| 228 | switch ($setting->settings_name) {
|
|---|
| 229 | case 'SITE_EMAIL':
|
|---|
| 230 | case 'SMTP_HOST':
|
|---|
| 231 | case 'SMTP_PORT':
|
|---|
| 232 | case 'SMTP_USERNAME':
|
|---|
| 233 | case 'SMTP_PASSWORD':
|
|---|
| 234 | $setting->settings_value = preg_replace("/[a-zA-Z0-9]/", "*", $setting->settings_value);
|
|---|
| 235 | break;
|
|---|
| 236 | }
|
|---|
| 237 | $report['hotaru_settings'][$setting->settings_name] = $setting->settings_value;
|
|---|
| 238 | }
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | // Widgets: plugin, function, args
|
|---|
| 242 |
|
|---|
| 243 | $sql = "SELECT widget_plugin, widget_function, widget_args FROM " . TABLE_WIDGETS;
|
|---|
| 244 | $widgets = $h->db->get_results($h->db->prepare($sql));
|
|---|
| 245 | if ($widgets) {
|
|---|
| 246 | foreach ($widgets as $widget) {
|
|---|
| 247 | $report['hotaru_widgets'][$widget->widget_plugin]['function'] = $widget->widget_function;
|
|---|
| 248 | $report['hotaru_widgets'][$widget->widget_plugin]['args'] = $widget->widget_args;
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | // Counts for all tables
|
|---|
| 253 |
|
|---|
| 254 | foreach ( $h->db->get_col("SHOW TABLES",0) as $table_name )
|
|---|
| 255 | {
|
|---|
| 256 | $report['hotaru_table_count'][$table_name] = $h->db->get_var("SELECT COUNT(*) FROM " . $table_name);
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | return $report;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | function loop_through_array ($h, $array, &$out = array () ) {
|
|---|
| 263 | foreach ( (array) $array as $key => $value ) {
|
|---|
| 264 | if ( ! is_array ( $value ) ) {
|
|---|
| 265 | $value = serialize($value);
|
|---|
| 266 | array_push ( $out, preg_replace("/[a-zA-Z0-9]/", "*", $value) );
|
|---|
| 267 | }
|
|---|
| 268 | else {
|
|---|
| 269 | $this->loop_through_array ( $value, &$out );
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 | return $out;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 | /**
|
|---|
| 277 | * Convert report object to text for logging to file
|
|---|
| 278 | *
|
|---|
| 279 | * @param object $report
|
|---|
| 280 | */
|
|---|
| 281 | public function logSystemReport($h, $report = NULL)
|
|---|
| 282 | {
|
|---|
| 283 | $output = "\n\n";
|
|---|
| 284 |
|
|---|
| 285 | $output .= "Name: " . $report['hotaru_site_name'] . "\n";
|
|---|
| 286 | $output .= "URL: " . $report['hotaru_baseurl'] . "\n";
|
|---|
| 287 | $output .= "Hotaru version: " . $report['hotaru_version'] . "\n";
|
|---|
| 288 | $output .= "Hotaru version in database: " . $report['hotaru_version_db'] . "\n";
|
|---|
| 289 | $output .= "PHP version: " . $report['php_version'] . "\n";
|
|---|
| 290 | $output .= "MySQL version: " . $report['mysql_version'] . "\n";
|
|---|
| 291 |
|
|---|
| 292 | $output .= "\n";
|
|---|
| 293 |
|
|---|
| 294 | $output .= "Default site permissions: \n";
|
|---|
| 295 | $perms = unserialize($report['hotaru_permissions']);
|
|---|
| 296 | unset($perms['options']); // don't need to display these
|
|---|
| 297 | foreach ($perms as $key => $value) {
|
|---|
| 298 | $output .= $key . " => (";
|
|---|
| 299 | foreach ($value as $k => $v) {
|
|---|
| 300 | $output .= $k . ": " . $v . ", ";
|
|---|
| 301 | }
|
|---|
| 302 | $output = rtrim($output, ", ");
|
|---|
| 303 | $output .= ")\n";
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | $output .= "\n";
|
|---|
| 307 |
|
|---|
| 308 | $output .= "Default user settings: \n";
|
|---|
| 309 | $user_settings = unserialize($report['hotaru_user_settings']);
|
|---|
| 310 | foreach ($user_settings as $key => $value) {
|
|---|
| 311 | $output .= $key . " => " . $value . "\n";
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | $output .= "\n";
|
|---|
| 315 |
|
|---|
| 316 | $output .= "Plugins: \n";
|
|---|
| 317 | if (isset($report['hotaru_plugins'])) {
|
|---|
| 318 | foreach ($report['hotaru_plugins'] as $key => $value) {
|
|---|
| 319 | $output .= $value['order'] . ". " . $key . " v." . $value['version'] . " ";
|
|---|
| 320 | if ($value['enabled']) { $output .= "[enabled] \n"; } else { $output .= "[disabled] \n"; }
|
|---|
| 321 | }
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | $output .= "\n";
|
|---|
| 325 |
|
|---|
| 326 | $output .= "Plugin Hooks: \n";
|
|---|
| 327 | if (isset($report['hotaru_plugin_hooks'])) {
|
|---|
| 328 | foreach ($report['hotaru_plugin_hooks'] as $key => $value) {
|
|---|
| 329 | $output .= $key . ". " . $value['folder'] . " => " . $value['hook'] . " \n";
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | $output .= "\n";
|
|---|
| 334 |
|
|---|
| 335 | $output .= "Plugin Settings: \n\n";
|
|---|
| 336 | if (isset($report['hotaru_plugin_settings'])) {
|
|---|
| 337 | foreach ($report['hotaru_plugin_settings'] as $key => $value) {
|
|---|
| 338 | foreach ($value as $k => $v) {
|
|---|
| 339 | if (!is_serialized($v)) {
|
|---|
| 340 | $output .= "Plugin settings for " . $key . ":\n" . $k . " = " . $v . " \n";
|
|---|
| 341 | } else {
|
|---|
| 342 | $output .= "Plugin settings for " . $key . ":\n";
|
|---|
| 343 | $v = unserialize($v);
|
|---|
| 344 | foreach ($v as $y => $z) {
|
|---|
| 345 | $output .= "..... " . $y . ": " . $z . " \n";
|
|---|
| 346 | }
|
|---|
| 347 | $output .= " \n";
|
|---|
| 348 | }
|
|---|
| 349 | }
|
|---|
| 350 | }
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | $output .= "\n";
|
|---|
| 354 |
|
|---|
| 355 | $output .= "Hotaru Settings: \n";
|
|---|
| 356 | if (isset($report['hotaru_settings'])) {
|
|---|
| 357 | foreach ($report['hotaru_settings'] as $key => $value) {
|
|---|
| 358 | $output .= $key . " => " . $value . " \n";
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | $output .= "\n";
|
|---|
| 363 |
|
|---|
| 364 | $output .= "Widgets: \n";
|
|---|
| 365 | if (isset($report['hotaru_widgets'])) {
|
|---|
| 366 | foreach ($report['hotaru_widgets'] as $key => $value) {
|
|---|
| 367 | $output .= $key . " => " . $value['function'];
|
|---|
| 368 | if ($value['args']) { $output .= " (args: " . $value['args'] . ")"; }
|
|---|
| 369 | $output .= "\n";
|
|---|
| 370 | }
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | $output .= "\n";
|
|---|
| 374 |
|
|---|
| 375 | $output .= "Number of rows in each table: \n";
|
|---|
| 376 | if (isset($report['hotaru_table_count'])) {
|
|---|
| 377 | foreach ($report['hotaru_table_count'] as $key => $value) {
|
|---|
| 378 | $output .= $key . " => " . $value . " \n";
|
|---|
| 379 | }
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | return $output;
|
|---|
| 383 | }
|
|---|
| 384 | }
|
|---|
| 385 | ?>
|
|---|