Guest User

sp-api-error.php | Simple Press 5.0

a guest
Feb 29th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.64 KB | None | 0 0
  1. <?php
  2. /*
  3. Simple:Press
  4. Desc:
  5. $LastChangedDate: 2012-02-12 09:42:32 -0700 (Sun, 12 Feb 2012) $
  6. $Rev: 7935 $
  7. */
  8.  
  9. if (preg_match('#'.basename(__FILE__).'#', $_SERVER['PHP_SELF'])) die('Access denied - you cannot directly call this file');
  10.  
  11. # ==================================================================
  12. #
  13. #   CORE: This file is loaded at CORE
  14. #   SP Error Handling and reporting
  15. #
  16. #   If the constant SPSHOWERRORS is set to true, non E_STRICT
  17. #   errors will also echo to the screen. E_NOTICE 'undefined'
  18. #   errors are curtrently ignored.
  19. #
  20. # ==================================================================
  21.  
  22. # ------------------------------------------------------------------
  23. # sp_construct_database_error()
  24. #
  25. # DATABASE ERROR MESSAGE CONSTRUCTOR
  26. #
  27. # Creates database error message and sends to error log function
  28. #
  29. #   $sql:       the original sql statement
  30. #   $sqlerror:  the reported mysql error text
  31. # ------------------------------------------------------------------
  32. function sp_construct_database_error($sql, $sqlerror) {
  33.     $mess = '';
  34.     $trace = debug_backtrace();
  35.     $traceitem = $trace[2];
  36.     $mess.= 'file: '.$traceitem['file'].'<br />';
  37.     $mess.= 'line: '.$traceitem['line'].'<br />';
  38.     $mess.= 'function: '.$traceitem['function'].'<br />';
  39.     $mess.= "error: $sqlerror<br /><br />";
  40.     $mess.= $sql;
  41.  
  42.     # write to error log
  43.     sp_write_error('database', $mess);
  44.  
  45.     # create display message
  46.     include_once(SPAPI.'sp-api-cache.php');
  47.     sp_notify(1, sp_text('Invalid database query'));
  48. }
  49.  
  50. # ------------------------------------------------------------------
  51. # sp_construct_php_error()
  52. #
  53. # PHP ERROR MESSAGE CONSTRUCTOR (at least those catchable ones)
  54. #
  55. # Creates php error message and sends to error log function
  56. #
  57. #   $errno:     Error Type
  58. #   $errstr:    Error message text
  59. #   $errfile:   Error File
  60. #   $errline:   Error Line Number in file
  61. # ------------------------------------------------------------------
  62. function sp_construct_php_error($errno, $errstr, $errfile, $errline) {
  63.     global $SPPATHS;
  64.  
  65.     # only interested in SP errors
  66.     $errfile = str_replace('\\','/',$errfile); # sanitize for Win32 installs
  67.     $pos = strpos($errfile, '/plugins/simple-press/');
  68.     $pos1 = strpos($errfile, $SPPATHS['plugins']);
  69.     if ($pos === false && $pos1 === false) return false;
  70.  
  71.     # For now remove the 'undefined' (index/variable) notices
  72.     if($errno == E_NOTICE && substr($errstr, 0, 9) == 'Undefined') return false;
  73.  
  74.     $errortype = array (
  75.         E_ERROR              => 'Error',
  76.         E_WARNING            => 'Warning',
  77.         E_PARSE              => 'Parsing Error',
  78.         E_NOTICE             => 'Notice',
  79.         E_CORE_ERROR         => 'Core Error',
  80.         E_CORE_WARNING       => 'Core Warning',
  81.         E_COMPILE_ERROR      => 'Compile Error',
  82.         E_COMPILE_WARNING    => 'Compile Warning',
  83.         E_USER_ERROR         => 'User Error',
  84.         E_USER_WARNING       => 'User Warning',
  85.         E_USER_NOTICE        => 'User Notice',
  86.         E_STRICT             => 'Runtime Notice',
  87.         E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'
  88.     );
  89.  
  90.     if ($errno==E_NOTICE || $errno==E_RECOVERABLE_ERROR || $errno==E_WARNING) {
  91.         $mess = '';
  92.         $trace = debug_backtrace();
  93.         $traceitem = $trace[1];
  94.         unset($trace);
  95.         if ($traceitem['function'] == 'spHandleShutdown') $traceitem['function'] ='Unavailable';
  96.  
  97.         $mess.= 'file: '.substr($errfile, $pos+8, strlen($errfile)).'<br />';
  98.         $mess.= "line: $errline<br />";
  99.         $mess.= 'function: '.$traceitem['function'].'<br />';
  100.         $mess.= $errortype[$errno].' | '.$errstr;
  101.  
  102.         # write out error to our toolbox log
  103.         sp_write_error('php', $mess);
  104.  
  105.         # wrtie error out to php error log (its still supressed from the screen)
  106.         error_log('PHP '.$errortype[$errno].':  '.$errstr, 0);
  107.  
  108.         # if we arent showing SP errors, dont let php error handler run
  109.         if (!defined('SPSHOWERRORS') || SPSHOWERRORS == false) return true;
  110.     }
  111.     return false;
  112. }
  113.  
  114. # ------------------------------------------------------------------
  115. # spHandleShutdown()
  116. #
  117. # FATAL (CRASH) ERROR RECORDING HANDLER
  118. #
  119. # Creates fatal error warning and passes to main error handler
  120. # ------------------------------------------------------------------
  121. register_shutdown_function('spHandleShutdown');
  122. function spHandleShutdown() {
  123.     $error = error_get_last();
  124.     if ($error !== NULL) sp_construct_php_error($error['type'], $error['message'], $error['file'], $error['line']);
  125. }
  126.  
  127. # ------------------------------------------------------------------
  128. # sp_write_error()
  129. #
  130. # ERROR RECORDING HANDLER
  131. #
  132. # Creates entry in table sferrorlog
  133. #
  134. #   $errortyoe: 'database'
  135. #   $errortext: pre-formatted error details
  136. # ------------------------------------------------------------------
  137. function sp_write_error($errortype, $errortext) {
  138.     global $SPSTATUS, $sfvars;
  139.  
  140.     if ($SPSTATUS != 'ok') return;
  141.     if(mysql_ping() == false) return;
  142.  
  143.     # checlk error log exists as it won't until installed.
  144.     $success = spdb_select('var', "SHOW TABLES LIKE '".SFERRORLOG."'");
  145.     if ($success == false) return;
  146.  
  147.     $now = "'".current_time('mysql')."'";
  148.     $sql = 'INSERT INTO '.SFERRORLOG;
  149.     $sql.= ' (error_date, error_type, error_text) ';
  150.     $sql.= 'VALUES (';
  151.     $sql.= $now.", ";
  152.     $sql.= "'".$errortype."', ";
  153.     $sql.= "'".esc_sql($errortext)."')";
  154.     spdb_query($sql);
  155.  
  156.     # leave just last 50 entries
  157.     if ($sfvars['insertid'] > 51) {
  158.         $sql = 'DELETE FROM '.SFERRORLOG.' WHERE id < '.($sfvars['insertid']-50);
  159.         spdb_query($sql);
  160.     }
  161. }
  162.  
  163. # ------------------------------------------------------------------
  164. # sp_gis_error()
  165. #
  166. # Handles GetImageSize calls and produces error in failure
  167. # ------------------------------------------------------------------
  168.  
  169. function sp_gis_error($errno, $errstr, $errfile, $errline, $errcontext) {
  170.     global $gis_error;
  171.     if ($errno == E_WARNING || $errno == E_NOTICE) $gis_error = sp_text('Unable to validate image details');
  172. }
  173.  
  174.  
  175. ?>
Advertisement
Add Comment
Please, Sign In to add comment