| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class responsible for storing the data in debug log. |
| 5 |
*/ |
| 6 |
class InstapageCmsPluginDebugLogModel { |
| 7 |
|
| 8 |
/** |
| 9 |
* @var object Class instance. |
| 10 |
*/ |
| 11 |
private static $debugLogModel = null; |
| 12 |
|
| 13 |
/** |
| 14 |
* Gets the class instance. |
| 15 |
* |
| 16 |
* @return object Class instance. |
| 17 |
*/ |
| 18 |
public static function getInstance() { |
| 19 |
if (self::$debugLogModel === null) { |
| 20 |
self::$debugLogModel = new InstapageCmsPluginDebugLogModel(); |
| 21 |
} |
| 22 |
|
| 23 |
return self::$debugLogModel; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Checks if Diagnostic mode is on. |
| 28 |
* |
| 29 |
* @return bool True if Diagnostic mode is on. |
| 30 |
*/ |
| 31 |
public function isDiagnosticMode() { |
| 32 |
return InstapageCmsPluginHelper::getOption('diagnostics', false); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Wtites an entry in the debug log. |
| 37 |
* |
| 38 |
* @param string $value Message to be written in the log. |
| 39 |
* @param string $name Additional name for the written value. Default: ''. |
| 40 |
* @param bool $addCaller Do you want to include the stack trace to an antry? Default: true. |
| 41 |
*/ |
| 42 |
public function write($value, $name = '', $addCaller = true) { |
| 43 |
try { |
| 44 |
if (is_array($value) || is_object($value)) { |
| 45 |
$value = print_r($value, true); |
| 46 |
} |
| 47 |
|
| 48 |
$caller = ''; |
| 49 |
|
| 50 |
if ($addCaller) { |
| 51 |
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
| 52 |
$traceLength = 3; |
| 53 |
$callerArr = array(); |
| 54 |
|
| 55 |
for ($i = 1; $i <= $traceLength; ++$i) { |
| 56 |
$caller = isset($trace[$i]) ? $trace[$i] : null; |
| 57 |
$callerFunction = isset($caller['function']) ? $caller['function'] : null; |
| 58 |
|
| 59 |
if ($callerFunction == 'writeLog' || $callerFunction == 'writeDiagnostics') { |
| 60 |
$traceLength = 4; |
| 61 |
|
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
$callerClass = isset($caller['class']) ? $caller['class'] . ' :: ' : null; |
| 66 |
|
| 67 |
if ($caller === null) { |
| 68 |
break; |
| 69 |
} |
| 70 |
|
| 71 |
$callerArr[] = $callerClass . $callerFunction; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
$caller = implode("\r\n", $callerArr); |
| 76 |
$db = InstapageCmsPluginDBModel::getInstance(); |
| 77 |
$sql = 'INSERT INTO ' . $db->debugTable . ' VALUES(NULL, %s, %s, %s, %s)'; |
| 78 |
$db->query($sql, date('Y-m-d H:i:s'), $value, $caller, $name); |
| 79 |
} catch (Exception $e) { |
| 80 |
echo $e->getMessage(); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Clears the debug log. |
| 86 |
*/ |
| 87 |
public function clear() { |
| 88 |
$db = InstapageCmsPluginDBModel::getInstance(); |
| 89 |
$sql = 'DELETE FROM ' . $db->debugTable; |
| 90 |
$db->query($sql); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Gets the entries from debug log. |
| 95 |
* |
| 96 |
* @return array List of entries. |
| 97 |
*/ |
| 98 |
public function read() { |
| 99 |
$db = InstapageCmsPluginDBModel::getInstance(); |
| 100 |
$sql = 'SELECT * FROM ' . $db->debugTable; |
| 101 |
$results = $db->getResults($sql); |
| 102 |
|
| 103 |
return $results; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Gets the HTML with debug log. Template for th og is in /templates/log.php file. |
| 108 |
* |
| 109 |
* @return string Log in HTML format. |
| 110 |
*/ |
| 111 |
public function getLogHTML() { |
| 112 |
if (InstapageCmsPluginConnector::currentUserCanManage() && $this->isDiagnosticMode()) { |
| 113 |
try { |
| 114 |
$pluginsHtml = InstapageCmsPluginConnector::getSelectedConnector()->getPluginsDebugHTML(); |
| 115 |
$optionsHtml = InstapageCmsPluginConnector::getSelectedConnector()->getOptionsDebugHTML(); |
| 116 |
$phpinfoHtml = $this->getPhpInfoHTML(); |
| 117 |
$rows = $this->read(); |
| 118 |
$view = InstapageCmsPluginViewModel::getInstance(); |
| 119 |
$view->init(INSTAPAGE_PLUGIN_PATH . '/templates/log.php'); |
| 120 |
$view->rows = $rows; |
| 121 |
$view->currentDate = date("Ymd_His"); |
| 122 |
$view->dbStructure = $this->getDbStructure(); |
| 123 |
$view->pluginsHtml = $pluginsHtml; |
| 124 |
$view->optionsHtml = $optionsHtml; |
| 125 |
$view->phpinfoHtml = $phpinfoHtml; |
| 126 |
$html = $view->fetch(); |
| 127 |
|
| 128 |
return $html; |
| 129 |
|
| 130 |
} catch (Exception $e) { |
| 131 |
throw $e; |
| 132 |
} |
| 133 |
} else { |
| 134 |
throw new Exception(__('Instapage log can be downloaded only in diagnostic mode.')); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Gets phpinfo and formats it. |
| 140 |
* |
| 141 |
* @return string Info about PHP in HTML format. |
| 142 |
*/ |
| 143 |
private function getPhpInfoHTML() { |
| 144 |
ob_start(); |
| 145 |
phpinfo(INFO_GENERAL | INFO_CREDITS | INFO_CONFIGURATION | INFO_MODULES | INFO_ENVIRONMENT | INFO_VARIABLES); |
| 146 |
$contents = ob_get_contents(); |
| 147 |
ob_end_clean(); |
| 148 |
|
| 149 |
$pattern = '/<style.*?style>/s'; |
| 150 |
$contents = preg_replace($pattern, '', $contents); |
| 151 |
$contents = '<div class="phpinfo">' . $contents . '</div>'; |
| 152 |
|
| 153 |
return $contents; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Gets database structure |
| 158 |
* |
| 159 |
* @uses InstapageCmsPluginDBModel::getInstance() |
| 160 |
* |
| 161 |
* @return array Array of table structure descriptions |
| 162 |
*/ |
| 163 |
protected function getDbStructure() { |
| 164 |
$db = InstapageCmsPluginDBModel::getInstance(); |
| 165 |
$tablesDescriptions = array(); |
| 166 |
|
| 167 |
// get description of tables |
| 168 |
$sql = 'DESCRIBE ' . $db->pagesTable; |
| 169 |
$tablesDescriptions[] = [ |
| 170 |
'tableName' => $db->pagesTable, |
| 171 |
'description' => $db->getResults($sql) |
| 172 |
]; |
| 173 |
|
| 174 |
$sql = 'DESCRIBE ' . $db->optionsTable; |
| 175 |
$tablesDescriptions[] = [ |
| 176 |
'tableName' => $db->optionsTable, |
| 177 |
'description' => $db->getResults($sql) |
| 178 |
]; |
| 179 |
|
| 180 |
$sql = 'DESCRIBE ' . $db->debugTable; |
| 181 |
$tablesDescriptions[] = [ |
| 182 |
'tableName' => $db->debugTable, |
| 183 |
'description' => $db->getResults($sql) |
| 184 |
]; |
| 185 |
|
| 186 |
return $tablesDescriptions; |
| 187 |
} |
| 188 |
} |
| 189 |
|