PageRenderTime 66ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class/error.class.php

http://viet-group.googlecode.com/
PHP | 430 lines | 401 code | 14 blank | 15 comment | 36 complexity | 6debc44dbbe516531cb3c5988318a5e3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @Project NUKEVIET 3.0
  4. * @Author VINADES.,JSC (contact@vinades.vn)
  5. * @Copyright (C) 2010 VINADES., JSC. All rights reserved
  6. * @Createdate 4/10/2010 19:43
  7. */
  8. if ( ! defined( 'E_STRICT' ) ) define( 'E_STRICT', 2048 ); //khong sua
  9. if ( ! defined( 'E_RECOVERABLE_ERROR' ) ) define( 'E_RECOVERABLE_ERROR', 4096 ); //khong sua
  10. if ( ! defined( 'E_DEPRECATED' ) ) define( 'E_DEPRECATED', 8192 ); //khong sua
  11. if ( ! defined( 'E_USER_DEPRECATED' ) ) define( 'E_USER_DEPRECATED', 16384 ); //khong sua
  12. if ( ! defined( 'NV_CURRENTTIME' ) ) define( 'NV_CURRENTTIME', time() );
  13. if ( ! defined( 'NV_ROOTDIR' ) ) define( 'NV_ROOTDIR', preg_replace( "/[\/]+$/", '', str_replace( '\\', '/', realpath( dirname( __file__ ) . '/../../' ) ) ) );
  14. /**
  15. * Error
  16. *
  17. * @package
  18. * @author NUKEVIET 3.0
  19. * @copyright VINADES
  20. * @version 2010
  21. * @access public
  22. */
  23. class Error
  24. {
  25. const INCORRECT_IP = 'Incorrect IP address specified';
  26. const LOG_FILE_NAME_DEFAULT = 'error_log'; //ten file log
  27. const LOG_FILE_EXT_DEFAULT = 'log'; //duoi file log
  28. private $log_errors_list;
  29. private $display_errors_list;
  30. private $send_errors_list;
  31. private $error_send_mail;
  32. private $error_log_path;
  33. private $error_log_tmp = false;
  34. private $error_log_filename;
  35. private $error_log_fileext;
  36. private $error_log_256;
  37. private $errno = false;
  38. private $errstr = false;
  39. private $errfile = false;
  40. private $errline = false;
  41. private $ip = false;
  42. private $useragent = false;
  43. private $request = false;
  44. private $day;
  45. private $month;
  46. private $error_date;
  47. private $errortype = array(
  48. E_ERROR => "Error", //
  49. E_WARNING => "Warning", //
  50. E_PARSE => "Parsing Error", //
  51. E_NOTICE => "Notice", //
  52. E_CORE_ERROR => "Core Error", //
  53. E_CORE_WARNING => "Core Warning", //
  54. E_COMPILE_ERROR => "Compile Error", //
  55. E_COMPILE_WARNING => "Compile Warning", //
  56. E_USER_ERROR => "User Error", //
  57. E_USER_WARNING => "User Warning", //
  58. E_USER_NOTICE => "User Notice", //
  59. E_STRICT => "Runtime Notice", //
  60. E_RECOVERABLE_ERROR => "Catchable fatal error", //
  61. E_DEPRECATED => "Run-time notices", //
  62. E_USER_DEPRECATED => "User-generated warning message"
  63. );
  64. /**
  65. * Error::__construct()
  66. *
  67. * @param mixed $config
  68. * @return
  69. */
  70. public function __construct ( $config )
  71. {
  72. $this->log_errors_list = $this->parse_error_num( ( int )$config['log_errors_list'] );
  73. $this->display_errors_list = $this->parse_error_num( ( int )$config['display_errors_list'] );
  74. $this->send_errors_list = $this->parse_error_num( ( int )$config['send_errors_list'] );
  75. $this->error_log_path = $this->get_error_log_path( ( string )$config['error_log_path'] );
  76. $this->error_send_mail = ( string )$config['error_send_email'];
  77. if ( isset( $config['error_log_filename'] ) and preg_match( "/[a-z0-9\_]+/i", $config['error_log_filename'] ) )
  78. {
  79. $this->error_log_filename = $config['error_log_filename'];
  80. }
  81. else
  82. {
  83. $this->error_log_filename = Error::LOG_FILE_NAME_DEFAULT;
  84. }
  85. if ( isset( $config['error_log_fileext'] ) and preg_match( "/[a-z]+/i", $config['error_log_fileext'] ) )
  86. {
  87. $this->error_log_fileext = $config['error_log_fileext'];
  88. }
  89. else
  90. {
  91. $this->error_log_fileext = Error::LOG_FILE_EXT_DEFAULT;
  92. }
  93. $this->day = date( "d-m-Y", NV_CURRENTTIME );
  94. $this->error_date = date( "r", NV_CURRENTTIME );
  95. $this->month = date( "m-Y", NV_CURRENTTIME );
  96. $ip = $this->get_Env( "REMOTE_ADDR" );
  97. $ip2long = ip2long( $ip );
  98. if ( $ip2long === - 1 and $ip2long === false ) die( Error::INCORRECT_IP );
  99. $this->ip = $ip;
  100. $request = $this->get_request();
  101. if ( ! empty( $request ) ) $this->request = substr( $request, 500 );
  102. $useragent = $this->get_Env( "HTTP_USER_AGENT" );
  103. if ( ! empty( $useragent ) ) $this->useragent = substr( $useragent, 0, 500 );
  104. $this->nv_set_ini();
  105. }
  106. /**
  107. * Error::nv_set_ini()
  108. *
  109. * @return
  110. */
  111. public function nv_set_ini ( )
  112. {
  113. $disable_functions = ( ini_get( "disable_functions" ) != "" and ini_get( "disable_functions" ) != false ) ? array_map( 'trim', preg_split( "/[\s,]+/", ini_get( "disable_functions" ) ) ) : array();
  114. if ( ( function_exists( 'ini_set' ) and ! in_array( 'ini_set', $disable_functions ) ) )
  115. {
  116. ini_set( 'display_startup_errors', 0 );
  117. ini_set( 'track_errors', 1 );
  118. ini_set( 'log_errors', 0 );
  119. ini_set( 'display_errors', 0 );
  120. }
  121. error_reporting( 0 );
  122. }
  123. /**
  124. * Error::get_Env()
  125. *
  126. * @param mixed $key
  127. * @return
  128. */
  129. private function get_Env ( $key )
  130. {
  131. if ( ! is_array( $key ) )
  132. {
  133. $key = array(
  134. $key
  135. );
  136. }
  137. foreach ( $key as $k )
  138. {
  139. if ( isset( $_SERVER[$k] ) ) return $_SERVER[$k];
  140. elseif ( isset( $_ENV[$k] ) ) return $_ENV[$k];
  141. elseif ( @getenv( $k ) ) return @getenv( $k );
  142. elseif ( function_exists( 'apache_getenv' ) && apache_getenv( $k, true ) ) return apache_getenv( $k, true );
  143. }
  144. return "";
  145. }
  146. /**
  147. * Error::get_error_log_path()
  148. *
  149. * @param mixed $path
  150. * @return
  151. */
  152. private function get_error_log_path ( $path )
  153. {
  154. $log_path = NV_ROOTDIR;
  155. $path = ltrim( rtrim( preg_replace( array(
  156. "/\\\\/", "/\/{2,}/"
  157. ), "/", $path ), "/" ), "/" );
  158. if ( ! empty( $path ) )
  159. {
  160. $e = explode( "/", $path );
  161. $cp = '';
  162. foreach ( $e as $p )
  163. {
  164. if ( preg_match( "#[^a-zA-Z0-9\_]#", $p ) )
  165. {
  166. $cp = '';
  167. break;
  168. }
  169. if ( ! is_dir( NV_ROOTDIR . '/' . $cp . $p ) )
  170. {
  171. if ( ! @mkdir( NV_ROOTDIR . '/' . $cp . $p, 0777 ) )
  172. {
  173. $cp = '';
  174. break;
  175. }
  176. }
  177. $cp .= $p . '/';
  178. }
  179. $log_path .= '/' . $path;
  180. }
  181. if ( ! is_dir( $log_path . '/tmp' ) )
  182. {
  183. //@mkdir( $log_path . '/tmp', 0777 );
  184. @mkdir( $log_path . '/tmp' );
  185. }
  186. if ( is_dir( $log_path . '/tmp' ) )
  187. {
  188. $this->error_log_tmp = $log_path . '/tmp';
  189. }
  190. if ( ! is_dir( $log_path . '/errors256' ) )
  191. {
  192. @mkdir( $log_path . '/errors256' );
  193. }
  194. if ( is_dir( $log_path . '/errors256' ) )
  195. {
  196. $this->error_log_256 = $log_path . '/errors256';
  197. }
  198. if ( ! is_dir( $log_path . '/old' ) )
  199. {
  200. @mkdir( $log_path . '/old' );
  201. }
  202. return $log_path;
  203. }
  204. /**
  205. * Error::parse_error_num()
  206. *
  207. * @param mixed $num
  208. * @return
  209. */
  210. private function parse_error_num ( $num )
  211. {
  212. if ( $num > E_ALL + E_STRICT ) $num = E_ALL + E_STRICT;
  213. if ( $num < 0 ) $num = 0;
  214. $result = array();
  215. $n = 1;
  216. while ( $num > 0 )
  217. {
  218. if ( $num & 1 == 1 )
  219. {
  220. $result[$n] = $this->errortype[$n];
  221. }
  222. $n *= 2;
  223. $num >>= 1;
  224. }
  225. return $result;
  226. }
  227. function get_request ( )
  228. {
  229. $request = array();
  230. if ( count( $_GET ) )
  231. {
  232. foreach ( $_GET as $key => $value )
  233. {
  234. if ( preg_match( "/^[a-zA-Z0-9\_]+$/", $key ) and ! is_numeric( $key ) )
  235. {
  236. $value = strip_tags( stripslashes( $value ) );
  237. $value = preg_replace( "/[\'|\"|\t|\r|\n|\.\.\/]+/", "", $value );
  238. $value = str_replace( array(
  239. "'", '"', "&"
  240. ), array(
  241. "&rsquo;", "&quot;", "&amp;"
  242. ), $value );
  243. if ( ! empty( $value ) ) $request[$key] = $value;
  244. }
  245. }
  246. }
  247. $request = ! empty( $request ) ? '?' . http_build_query( $request ) : "";
  248. $request = $this->get_Env( "PHP_SELF" ) . $request;
  249. return $request;
  250. }
  251. private function info_die ( )
  252. {
  253. $error_code = md5( $this->errno . ( string )$this->errfile . ( string )$this->errline . $this->ip );
  254. $error_code2 = md5( $error_code );
  255. $error_file = $this->error_log_256 . '/' . $this->month . '__' . $error_code2 . '__' . $error_code . '.' . $this->error_log_fileext;
  256. if ( ! file_exists( $error_file ) )
  257. {
  258. $content = "TIME: " . $this->error_date . "\r\n";
  259. if ( ! empty( $this->ip ) ) $content .= "IP: " . $this->ip . "\r\n";
  260. $content .= "INFO: " . $this->errortype[$this->errno] . "(" . $this->errno . "): " . $this->errstr . "\r\n";
  261. if ( ! empty( $this->errfile ) ) $content .= "FILE: " . $this->errfile . "\r\n";
  262. if ( ! empty( $this->errline ) ) $content .= "LINE: " . $this->errline . "\r\n";
  263. if ( ! empty( $this->request ) ) $content .= "REQUEST: " . $this->request . "\r\n";
  264. if ( ! empty( $this->useragent ) ) $content .= "USER-AGENT: " . $this->useragent . "\r\n";
  265. file_put_contents( $error_file, $content, FILE_APPEND );
  266. }
  267. $strEncodedEmail = "";
  268. for ( $i = 0; $i < strlen( $this->error_send_mail ); $i ++ )
  269. {
  270. $strEncodedEmail .= "&#" . ord( substr( $this->error_send_mail, $i ) ) . ";";
  271. }
  272. $size = @getimagesize( NV_ROOTDIR . '/images/' . $this->site_logo );
  273. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n";
  274. echo "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
  275. echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
  276. echo "<head>\n";
  277. echo " <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n";
  278. echo " <meta http-equiv=\"expires\" content=\"0\" />\n";
  279. echo "<title>" . $this->errortype[$this->errno] . "</title>\n";
  280. echo "</head>\n\n";
  281. echo "<body>\n";
  282. echo " <div style=\"width: 400px; margin-right: auto; margin-left: auto; margin-top: 20px; margin-bottom: 20px; color: #dd3e31; text-align: center;\"><span style=\"font-weight: bold;\">" . $this->errortype[$this->errno] . "</span><br />\n";
  283. echo " <span style=\"color: #1a264e;font-weight: bold;\">" . $this->errstr . "</span><br />\n";
  284. echo " <span style=\"color: #1a264e;\">(Code: " . $error_code2 . ")</span></div>\n";
  285. echo " <div style=\"width: 400px; margin-right: auto; margin-left: auto;text-align:center\">\n";
  286. echo " If you have any questions about this site,<br />please <a href=\"mailto:" . $strEncodedEmail . "\">contact</a> the site administrator for more information</div>\n";
  287. echo "</body>\n";
  288. echo "</html>";
  289. die();
  290. }
  291. private function _log ( )
  292. {
  293. $content = "[" . $this->error_date . "]";
  294. if ( ! empty( $this->ip ) ) $content .= " [" . $this->ip . "]";
  295. $content .= " [" . $this->errortype[$this->errno] . "(" . $this->errno . "): " . $this->errstr . "]";
  296. if ( ! empty( $this->errfile ) ) $content .= " [FILE: " . $this->errfile . "]";
  297. if ( ! empty( $this->errline ) ) $content .= " [LINE: " . $this->errline . "]";
  298. if ( ! empty( $this->request ) ) $content .= " [REQUEST: " . $this->request . "]";
  299. $content .= "\r\n";
  300. $error_log_file = $this->error_log_path . '/' . $this->day . '_' . $this->error_log_filename . '.' . $this->error_log_fileext;
  301. error_log( $content, 3, $error_log_file );
  302. }
  303. private function _send ( )
  304. {
  305. $content = "[" . $this->error_date . "]";
  306. if ( ! empty( $this->ip ) ) $content .= " [" . $this->ip . "]";
  307. $content .= " [" . $this->errortype[$this->errno] . "(" . $this->errno . "): " . $this->errstr . "]";
  308. if ( ! empty( $this->errfile ) ) $content .= " [FILE: " . $this->errfile . "]";
  309. if ( ! empty( $this->errline ) ) $content .= " [LINE: " . $this->errline . "]";
  310. if ( ! empty( $this->request ) ) $content .= " [REQUEST: " . $this->request . "]";
  311. if ( ! empty( $this->useragent ) ) $content .= " [AGENT: " . $this->useragent . "]";
  312. $content .= "\r\n";
  313. $error_log_file = $this->error_log_path . '/sendmail.' . $this->error_log_fileext;
  314. error_log( $content, 3, $error_log_file );
  315. }
  316. private function _display ( )
  317. {
  318. global $error_info;
  319. $info = $this->errstr;
  320. if ( $this->errno != E_USER_ERROR && $this->errno != E_USER_WARNING && $this->errno != E_USER_NOTICE )
  321. {
  322. if ( ! empty( $this->errfile ) ) $info .= " in file " . $this->errfile;
  323. if ( ! empty( $this->errline ) ) $info .= " on line " . $this->errline;
  324. }
  325. $error_info[] = array(
  326. 'errno' => $this->errno, 'info' => $info
  327. );
  328. }
  329. /**
  330. * Error::error_handler()
  331. *
  332. * @param mixed $errno
  333. * @param mixed $errstr
  334. * @param mixed $errfile
  335. * @param mixed $errline
  336. * @return
  337. */
  338. public function error_handler ( $errno, $errstr, $errfile, $errline )
  339. {
  340. if ( empty( $errno ) ) return;
  341. if ( ! empty( $errno ) ) $this->errno = $errno;
  342. if ( isset( $errstr ) and ! empty( $errstr ) ) $this->errstr = $errstr;
  343. if ( isset( $errfile ) and ! empty( $errfile ) ) $this->errfile = str_replace( NV_ROOTDIR, "", str_replace( '\\', '/', $errfile ) );
  344. if ( isset( $errline ) and ! empty( $errline ) ) $this->errline = $errline;
  345. $track_errors = $this->day . '_' . md5( $this->errno . ( string )$this->errfile . ( string )$this->errline . $this->ip );
  346. $track_errors = $this->error_log_tmp . '/' . $track_errors . '.' . $this->error_log_fileext;
  347. if ( ! file_exists( $track_errors ) )
  348. {
  349. file_put_contents( $track_errors, '', FILE_APPEND );
  350. if ( ! empty( $this->log_errors_list ) and isset( $this->log_errors_list[$errno] ) )
  351. {
  352. $this->_log();
  353. }
  354. if ( ! empty( $this->send_errors_list ) and isset( $this->send_errors_list[$errno] ) )
  355. {
  356. $this->_send();
  357. }
  358. if ( ! empty( $this->display_errors_list ) and isset( $this->display_errors_list[$errno] ) )
  359. {
  360. $this->_display();
  361. }
  362. }
  363. if ( $this->errno == 256 )
  364. {
  365. $this->info_die();
  366. }
  367. }
  368. }
  369. ?>