config.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. error_reporting(0);
  3. ob_start();
  4. // START ERROR HANDLING
  5. function shutdown_handler(){
  6. $error = error_get_last();
  7. if(!is_null($error)){
  8. ob_end_clean();
  9. switch($error['type']){
  10. case E_USER_ERROR:$errnostr='Fatal Error';break;
  11. case E_USER_WARNING:$errnostr='Warning';break;
  12. case E_USER_NOTICE:$errnostr='Notice';break;
  13. case E_ERROR:$errnostr='Error';break;
  14. case E_PARSE:$errnostr='Parse Error';break;
  15. case E_CORE_ERROR:$errnostr='Core Error';break;
  16. case E_CORE_WARNING:$errnostr='Core Warning';break;
  17. case E_COMPILE_ERROR:$errnostr='Compile Error';break;
  18. case E_COMPILE_WARNING:$errnostr='Compile Warning';break;
  19. //case E_:$errnostr='';break;
  20. default:$errnostr='Unkown Error';
  21. }
  22. echo json_encode(array(
  23. 'error'=>"\n{$errnostr}: {$error['message']} on {$error['file']}[{$error['line']}]"
  24. ));
  25. }else{
  26. ob_end_flush();
  27. }
  28. echo PHP_EOL;
  29. }
  30. register_shutdown_function('shutdown_handler');
  31. // END ERROR HANDLING
  32. if(session_id() == ''){
  33. @session_start();
  34. }
  35. define('PATH_ROOT',realpath(dirname(__FILE__)).'/../');
  36. define('PATH_CONFIG',PATH_ROOT.'config.json');
  37. define('PATH_DEFAULT_CONFIG',PATH_ROOT.'config.default.json');
  38. define('PATH_PHP',PATH_ROOT.'php/');
  39. define('PATH_JS',PATH_ROOT.'js/');
  40. define('PATH_CSS',PATH_ROOT.'css/');
  41. define('PATH_DATA',PATH_ROOT.'data/');
  42. global $config;
  43. if(file_exists(PATH_CONFIG)){
  44. $config = objectToarray(json_decode(file_get_contents(PATH_CONFIG),true));
  45. }else{
  46. $config = array();
  47. }
  48. $config = array_merge($config,objectToarray(json_decode(file_get_contents(PATH_DEFAULT_CONFIG),true)));
  49. function get($setting){
  50. global $config;
  51. if(isset($config[$setting])){
  52. return $config[$setting];
  53. }else{
  54. return false;
  55. }
  56. }
  57. function set($setting,$value){
  58. global $config;
  59. $config[$setting] = $value;
  60. file_put_contents(PAT_CONFIG,json_encode($config));
  61. return $value;
  62. }
  63. function objectToarray($d){
  64. if(is_object($d)){
  65. // Gets the properties of the given object
  66. // with get_object_vars function
  67. $d = get_object_vars($d);
  68. }
  69. if(is_array($d)){
  70. /*
  71. * Return array converted to object
  72. * Using __FUNCTION__ (Magic constant)
  73. * for recursive call
  74. */
  75. return array_map(__FUNCTION__, $d);
  76. }else{
  77. // Return array
  78. return $d;
  79. }
  80. }
  81. function arrayToObject($d){
  82. if(is_array($d)){
  83. /*
  84. * Return array converted to object
  85. * Using __FUNCTION__ (Magic constant)
  86. * for recursive call
  87. */
  88. return (object)array_map(__FUNCTION__, $d);
  89. }else{
  90. // Return object
  91. return $d;
  92. }
  93. }
  94. ?>