2016-09-26 00:22:33 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
|
|
|
|
* Atom feeds for websites that don't have one.
|
|
|
|
*
|
|
|
|
* For the full license information, please view the UNLICENSE file distributed
|
|
|
|
* with this source code.
|
|
|
|
*
|
|
|
|
* @package Core
|
|
|
|
* @license http://unlicense.org/ UNLICENSE
|
|
|
|
* @link https://github.com/rss-bridge/rss-bridge
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws an exception when called.
|
|
|
|
*
|
2018-11-18 18:53:21 +03:00
|
|
|
* @throws \Exception when called
|
2018-11-16 23:48:59 +03:00
|
|
|
* @param string $message The error message
|
|
|
|
* @param int $code The HTTP error code
|
|
|
|
* @link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes List of HTTP
|
|
|
|
* status codes
|
|
|
|
*/
|
2016-09-26 00:22:33 +03:00
|
|
|
function returnError($message, $code)
|
|
|
|
{
|
2018-11-18 18:53:21 +03:00
|
|
|
throw new \Exception($message, $code);
|
2016-09-26 00:22:33 +03:00
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Returns HTTP Error 400 (Bad Request) when called.
|
|
|
|
*
|
|
|
|
* @param string $message The error message
|
|
|
|
*/
|
2016-09-26 00:22:33 +03:00
|
|
|
function returnClientError($message)
|
|
|
|
{
|
2016-11-09 21:10:40 +03:00
|
|
|
returnError($message, 400);
|
2016-09-26 00:22:33 +03:00
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Returns HTTP Error 500 (Internal Server Error) when called.
|
|
|
|
*
|
|
|
|
* @param string $message The error message
|
|
|
|
*/
|
2016-09-26 00:22:33 +03:00
|
|
|
function returnServerError($message)
|
|
|
|
{
|
2016-11-09 21:10:40 +03:00
|
|
|
returnError($message, 500);
|
2016-09-26 00:22:33 +03:00
|
|
|
}
|
2019-10-31 20:49:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores bridge-specific errors in a cache file.
|
|
|
|
*
|
|
|
|
* @param string $bridgeName The name of the bridge that failed.
|
|
|
|
* @param int $code The error code
|
|
|
|
*
|
|
|
|
* @return int The total number the same error has appeared
|
|
|
|
*/
|
|
|
|
function logBridgeError($bridgeName, $code)
|
|
|
|
{
|
2022-07-06 13:14:04 +03:00
|
|
|
$cacheFactory = new CacheFactory();
|
2019-10-31 20:49:45 +03:00
|
|
|
|
2022-07-06 13:14:04 +03:00
|
|
|
$cache = $cacheFactory->create();
|
2019-10-31 20:49:45 +03:00
|
|
|
$cache->setScope('error_reporting');
|
|
|
|
$cache->setkey($bridgeName . '_' . $code);
|
|
|
|
$cache->purgeCache(86400); // 24 hours
|
|
|
|
|
|
|
|
if ($report = $cache->loadData()) {
|
|
|
|
$report = json_decode($report, true);
|
|
|
|
$report['time'] = time();
|
|
|
|
$report['count']++;
|
|
|
|
} else {
|
|
|
|
$report = [
|
|
|
|
'error' => $code,
|
|
|
|
'time' => time(),
|
|
|
|
'count' => 1,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$cache->saveData(json_encode($report));
|
|
|
|
|
|
|
|
return $report['count'];
|
|
|
|
}
|
2022-07-08 21:39:13 +03:00
|
|
|
|
|
|
|
function create_sane_stacktrace(\Throwable $e): array
|
|
|
|
{
|
|
|
|
$frames = array_reverse($e->getTrace());
|
|
|
|
$frames[] = [
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
'line' => $e->getLine(),
|
|
|
|
];
|
|
|
|
$stackTrace = [];
|
|
|
|
foreach ($frames as $i => $frame) {
|
|
|
|
$file = $frame['file'] ?? '(no file)';
|
|
|
|
$line = $frame['line'] ?? '(no line)';
|
|
|
|
$stackTrace[] = sprintf(
|
|
|
|
'#%s %s:%s',
|
|
|
|
$i,
|
|
|
|
trim_path_prefix($file),
|
|
|
|
$line,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $stackTrace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trim path prefix for privacy/security reasons
|
|
|
|
*
|
|
|
|
* Example: "/var/www/rss-bridge/index.php" => "index.php"
|
|
|
|
*/
|
|
|
|
function trim_path_prefix(string $filePath): string
|
|
|
|
{
|
|
|
|
return mb_substr($filePath, mb_strlen(dirname(__DIR__)) + 1);
|
|
|
|
}
|