*/
class Utils
{
/**
* Parse the emoticons.
*
* @param string $text String to parse emoticons from.
*
* @return string Parsed text.
*/
public static function parseEmotes($text)
{
// Get emoticons from the database
$emotes = Database::fetch('emoticons');
// Do the replacements
foreach ($emotes as $emote) {
$text = str_replace(
$emote['emote_string'],
'',
$text
);
}
// Return the parsed text
return $text;
}
/**
* Verify a ReCaptcha
*
* @param string $response The user response.
*
* @return array The response from the ReCaptcha API.
*/
public static function verifyCaptcha($response)
{
// Attempt to get the response
$resp = @file_get_contents(
'https://www.google.com/recaptcha/api/siteverify?secret='
. Config::get('recaptcha_private')
. '&response='
. $response
);
// In the highly unlikely case that it failed to get anything forge a false
if (!$resp) {
return false;
}
// Decode the response JSON from the servers
$resp = json_decode($resp, true);
// Return shit
return $resp;
}
/**
* The error handler.
*
* @param int $errno The error ID.
* @param string $errstr Quick description of the event.
* @param string $errfile File the error occurred in.
* @param int $errline Line the error occurred on.
*/
public static function errorHandler($errno, $errstr, $errfile, $errline)
{
// Remove ROOT path from the error string and file location
$errstr = str_replace(ROOT, '', $errstr);
$errfile = str_replace(ROOT, '', $errfile);
// Attempt to log the error to the database
if (Database::$database !== null) {
// Encode backtrace data
$backtrace = base64_encode(json_encode(debug_backtrace()));
// Check if this error has already been logged in the past
if ($past = Database::fetch(
'error_log',
false,
[
'error_backtrace' => [$backtrace, '=', true],
'error_string' => [$errstr, '='],
'error_line' => [$errline, '='],
]
)) {
// If so assign the errid
$errid = $past['error_id'];
} else {
// Create an error ID
$errid = substr(md5(microtime()), rand(0, 22), 10);
// Log the error
Database::insert('error_log', [
'error_id' => $errid,
'error_timestamp' => date("r"),
'error_revision' => SAKURA_VERSION,
'error_type' => $errno,
'error_line' => $errline,
'error_string' => $errstr,
'error_file' => $errfile,
'error_backtrace' => $backtrace,
]);
}
}
switch ($errno) {
case E_ERROR:
case E_USER_ERROR:
$error = 'FATAL ERROR: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
break;
case E_WARNING:
case E_USER_WARNING:
$error = 'WARNING: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
break;
case E_NOTICE:
case E_USER_NOTICE:
$error = 'NOTICE: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
break;
default:
$error = 'Unknown error type [' . $errno . ']: ' . $errstr . ' on line ' . $errline
. ' in ' . $errfile;
}
// Truncate all previous outputs
ob_clean();
ob_end_clean();
// Check if this request was made through the ajax thing
if (isset($_REQUEST['ajax'])) {
die('An error occurred while executing the script.|1|javascript:alert("' . (isset($errid) ? 'Error Log ID: '. $errid : 'Failed to log.') . '");');
}
// Check for dev mode
$detailed = Config::local('dev', 'show_errors');
// Build page
$errorPage = '
To prevent potential security risks or data loss Sakura has stopped execution of the script.
'; if (isset($errid)) { $errorPage .= 'The error and surrounding data has been logged.
' . $errid . ''; } else { $errorPage .= '
Sakura was not able to log this error which could mean that there was an error with the database connection. If you\'re the system administrator check the database credentials and make sure the server is running and if you\'re not please let the system administrator know about this error if it occurs again.
'; } if ($detailed) { $errorPage .= '' . $error . '
'; foreach ($trace as $key => $val) { $errorPage .= str_pad( '[' . $key . ']', 12 ) . '=> ' . ( is_array($val) || is_object($val) ? json_encode($val) : $val ) . "\r\n"; } $errorPage .= ''; } } $errorPage .= '
tags.
*
* @param string $text Dirty
*
* @return string Clean
*/
public static function fixCodeTags($text)
{
$parts = explode('', $text);
$newStr = '';
if (count($parts) > 1) {
foreach ($parts as $p) {
$parts2 = explode('
', $p);
if (count($parts2) > 1) {
$code = str_replace('
', '', $parts2[0]);
$code = str_replace('
', '', $code);
$code = str_replace('
', '', $code);
$code = str_replace('<', '<', $code);
$newStr .= ''.$code.'
';
$newStr .= $parts2[1];
} else {
$newStr .= $p;
}
}
} else {
$newStr = $text;
}
return $newStr;
}
}