Skip to content

Commit

Permalink
Error handling, Database and Backup improvments
Browse files Browse the repository at this point in the history
* Error messages now has more details (including backlog)
* Basic functionality with broken database (backup section in CP)
* Backup section now can be used to restore .sql files and daily backups
  • Loading branch information
sergejey committed Aug 3, 2017
1 parent 8ce2040 commit 16a3dfc
Show file tree
Hide file tree
Showing 15 changed files with 1,736 additions and 1,607 deletions.
16 changes: 6 additions & 10 deletions lib/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,17 +990,15 @@ function checkAccess($object_type, $object_id)
function registerError($code = 'custom', $details = '')
{

DebMes("Error registered (type: $code): ".$details);
$e = new \Exception;
$backtrace=$e->getTraceAsString();

DebMes("Error registered (type: $code):\n".$details."\nBacktrace:\n".$backtrace,'error');
$code = trim($code);

if ($code == 'sql') {
return 0;
}

if (!$code)
{
$code = 'custom';
}

$error_rec = SQLSelectOne("SELECT * FROM system_errors WHERE CODE LIKE '" . DBSafe($code) . "'");

Expand All @@ -1012,13 +1010,12 @@ function registerError($code = 'custom', $details = '')
}

$error_rec['LATEST_UPDATE'] = date('Y-m-d H:i:s');
$error_rec['ACTIVE'] = (int)$error_rec['ACTIVE'] + 1;
@$error_rec['ACTIVE'] = (int)$error_rec['ACTIVE'] + 1;
SQLUpdate('system_errors', $error_rec);

$history_rec = array();

$history_rec['ERROR_ID'] = $error_rec['ID'];
$history_rec['COMMENTS'] = $details;
$history_rec['COMMENTS'] = $details."\nBacktrace:\n".$backtrace;
$history_rec['ADDED'] = $error_rec['LATEST_UPDATE'];

//Temporary disabled
Expand All @@ -1031,7 +1028,6 @@ function registerError($code = 'custom', $details = '')
$history_rec['EVENTS_DATA'] = getURL($xrayUrl . 'events', 0);
$history_rec['DEBUG_DATA'] = getURL($xrayUrl . 'debmes', 0);
*/

$history_rec['ID'] = SQLInsert('system_errors_data', $history_rec);

if (!$error_rec['KEEP_HISTORY'])
Expand Down
88 changes: 35 additions & 53 deletions lib/errors.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,67 +39,49 @@ class custom_error
* @param int $short Short (default 0)
* @return void
*/
public function __construct($description, $stop = 0, $short = 0)
public function __construct($description, $stop = 0)
{
$script = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$description = $script . "\nError:\n" . $description;

//$log = getLogger($this);
//$log->error($description);
//$backtrace=debug_backtrace();
//print_r($backtrace);exit;
DebMes($description.' ('.__FILE__.')');

if (defined("DEBUG_MODE"))
{
if (!$short)
{
$this->alert(nl2br($description));
}
else
{
echo nl2br($description);
}
if (!mb_detect_encoding($description, 'UTF-8', true)) {
$description = iconv('windows-1251','UTF-8',$description);
}
else
{
if (!$short)
{
$this->alert("");
}
else
{
echo "Warning...<br>";
}

$e = new \Exception;
if (defined("DEBUG_MODE")) {
$content=<<<FF
<html>
<head>
<title>Error</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<h1>Error</h1>
<h3>Details</h3>
<div class="alert alert-danger">$script<br/>$description</div>
<h3>Backtrace</h3>
<div><pre>{$e->getTraceAsString()}</pre></div>
<div>
<a href="#" class="btn btn-default" onclick="window.history.go(-1);return false;">&lt;&lt;&lt; Back</a>
<a href="#" class="btn btn-default" onclick="window.location.reload();return false;">Reload page</a>
<a href="/admin.php?md=panel&action=saverestore" class="btn btn-default">Go to Backup section</a>
</div>
</div>
</body>
</html>
FF;
echo $content;
}

sendmail("errors@" . PROJECT_DOMAIN, PROJECT_BUGTRACK, "Error reporting: $script", $description);

//sendmail("errors@" . PROJECT_DOMAIN, PROJECT_BUGTRACK, "Error reporting: $script", $description);
if ($stop) exit;
}

/**
* Error processing
* used to show and log error/warning message
*
* @access private
*
* @param string $description error description
* @return void
*/
public function alert($description)
{
echo "<html><head><style>body {font-family:tahoma, arial}</style></head><body>";
echo "&nbsp;<br>";
echo "<table border=0 cellspacing=2 cellpadding=15 bgcolor=#FF0000 align=center width=600>";
echo "<tr><td bgcolor='#FFFFFF'>";
echo "<p align=center><font color=red><b>Sorry, page is temporary unavailable.<br>";
echo "<br>Please try again later.</b></font></p>";
echo "<p align='center'><a href='#' onClick='history.go(-1);'>&lt;&lt;&lt; Back to previous page</a></p>";
echo "</td></tr>";
echo "<tr><td bgcolor='#FFFFFF'><p align=center><font color=red><b>$description</b></font></p></td></tr>";
echo "</table></body></html>";
}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/module.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,10 @@ public function dbInstall($data)
SQLExec($sql);

$result = SQLGetFields($table);
foreach($result as $row) {
$tbl_fields[$table][$row['Field']]=1;
if (is_array($result)) {
foreach($result as $row) {
$tbl_fields[$table][$row['Field']]=1;
}
}

}
Expand Down

0 comments on commit 16a3dfc

Please sign in to comment.