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

/lib/start.php

https://github.com/dreamhackcrew/API
PHP | 70 lines | 50 code | 17 blank | 3 comment | 8 complexity | 8335d005815188fd3695c0b79222904a MD5 | raw file
  1. <?php
  2. function __autoload($class){
  3. if( is_file('lib/'.$class.'.php') )
  4. return require_once('lib/'.$class.'.php');
  5. }
  6. set_error_handler('errorHandler');
  7. // Connect to the database
  8. db::getInstance(true)->connect(config::dbServer, config::dbUser, config::dbPasswd,config::dbDatabase);
  9. session_start();
  10. //new session();
  11. function errorHandler($errno, $errstr, $errfile, $errline) {
  12. if ( !($errno & E_WARNING||$errno & E_ERROR || $errno & E_CORE_ERROR || $errno & E_COMPILE_ERROR || $errno & E_USER_ERROR || $errno & E_USER_WARNING || $errno & E_USER_NOTICE) )
  13. return true;
  14. if ( preg_match('/Headers and client library minor version mismatch/',$errstr) )
  15. return true;
  16. header('HTTP/1.0 500 Server error');
  17. $trace = debug_backtrace(false);
  18. unset($trace[0]);
  19. foreach($trace as $key => $line)
  20. $trace[$key] = array_intersect_key(
  21. $line,
  22. array(
  23. 'file'=>'',
  24. 'line'=>'',
  25. 'function'=>'',
  26. 'class'=>'',
  27. 'type'=>''
  28. )
  29. );
  30. $data = array(
  31. 'error'=>$errstr,
  32. 'no' => $errno,
  33. 'backtrace' => $trace
  34. );
  35. file_put_contents('error_log', print_r(apache_request_headers(),true).print_r($_GET,true).print_r($_POST,true)."\n".(json_encode($data)).print_r(debug_backtrace(false),true)."\n-----------------------------------\n\n" ,FILE_APPEND);
  36. response($data);
  37. }
  38. function response($data) {
  39. global $ext;
  40. // file_put_contents('log', print_r(apache_request_headers(),true).print_r($_GET,true).print_r($_POST,true)."\n".(json_encode($data))."\n-----------------------------------\n\n" ,FILE_APPEND);
  41. switch($ext) {
  42. case 'xml':
  43. die(xml::toXml($data));
  44. case 'url':
  45. die(http_build_query($data));
  46. case 'json':
  47. default:
  48. header('Content-type: application/json');
  49. die(json_encode($data));
  50. }
  51. die();
  52. }
  53. ?>