PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/site/onyx2/load.php

https://bitbucket.org/nemunaire/polkmap
PHP | 166 lines | 121 code | 43 blank | 2 comment | 24 complexity | 57d3da6ae71079126d39cc03df4435b9 MD5 | raw file
  1. <?php
  2. $chrono_start = microtime(TRUE);
  3. if(version_compare(PHP_VERSION, '5.1.0', '<')) trigger_error('Version de php non supportee',E_USER_ERROR);
  4. define('ONYX_VERSION','2.0');
  5. define('FILE',$_SERVER["SCRIPT_FILENAME"]);
  6. define('ONYX',dirname(__FILE__).'/');
  7. if(!is_readable(ONYX."ban.list")) trigger_error('Fichier de ban introuvable',E_USER_ERROR);
  8. if(in_array($_SERVER['REMOTE_ADDR'], file(ONYX."ban.list",FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES))) die('banni');
  9. require_once('require/parse.php');
  10. require_once('require/cache.php');
  11. require_once('require/str.php');
  12. require_once('require/env.php');
  13. # *** Chargement de la configuration ***
  14. $cached = Cache::read('_configuration_'.FILE);
  15. if($cached['check'] == md5_file(ONYX.'config/root.xml').md5_file(ONYX.'modules/modules.xml'))
  16. {
  17. $VAR = $cached['var'];
  18. $ENV = $cached['env'];
  19. define('ROOT',$cached['root']);
  20. foreach($cached['modules'] as $name)
  21. {
  22. if(isset($cached['opt'][$name]))
  23. {
  24. $OPT = $cached['opt'][$name];
  25. include_once(ONYX.'modules/'.$name.'/main.php');
  26. }
  27. }
  28. define('CACHED',TRUE);
  29. }
  30. else
  31. {
  32. $xml = new DOMDocument();
  33. $xml->load(ONYX.'config/root.xml') or trigger_error('Erreur du fichier de configuration',E_USER_ERROR);
  34. $xml->normalizeDocument();
  35. //$xml->validate();
  36. if($root = $xml->documentElement->getAttribute('root'))
  37. {
  38. if($root == substr(FILE,0,strlen($root)))
  39. {
  40. define('ROOT',$root);
  41. $search = substr(FILE,strlen($root));
  42. $paths = explode('/',$search);
  43. $paths2 = $paths;
  44. foreach($paths as $key => $path)
  45. {
  46. for($i = $key - 1; $i >= 0; $i--)
  47. {
  48. $paths2[$key] = $paths[$i] . '/' . $paths2[$key];
  49. }
  50. if($key != count($paths) - 1) $paths2[$key] .= '/';
  51. }
  52. foreach($xml->getElementsByTagName('config') as $value)
  53. {
  54. $config[$value->getAttribute('match')] = $value;
  55. }
  56. $VAR = $ENV = $modules = array();
  57. array_unshift($paths2,'*');
  58. foreach($paths2 as $path)
  59. {
  60. if(isset($config[$path]))
  61. {
  62. $VAR = array_merge($VAR,parse_config($config[$path]));
  63. $ENV = array_merge($ENV,parse_config($config[$path],'env','option'));
  64. foreach($config[$path]->getElementsByTagName('module') as $module)
  65. {
  66. $modules[$module->getAttribute('name')] = $module;
  67. }
  68. }
  69. }
  70. $xml_modules = new DOMDocument();
  71. $xml_modules->load(ONYX.'modules/modules.xml') or trigger_error('Erreur du fichier de modules',E_USER_ERROR);
  72. $xml_modules->normalizeDocument();
  73. foreach($xml_modules->getElementsByTagName('module') as $module)
  74. {
  75. if(!ctype_alnum($module->getAttribute('name'))) trigger_error('Le nom du module contient des caracteres illegaux',E_USER_ERROR);
  76. $all_modules[$module->getAttribute('name')] = $module;
  77. }
  78. $all_opt = $load_modules = array();
  79. foreach($modules as $name => $module)
  80. {
  81. if(isset($all_modules[$name]) && !in_array($name,$load_modules))
  82. {
  83. if($require = $all_modules[$name]->getAttribute('require'))
  84. if(!in_array($require,$load_modules))
  85. {
  86. trigger_error("Module '$name' requiert '$require'",E_USER_WARNING);
  87. continue;
  88. }
  89. $OPT = parse_config($module,'option');
  90. $OPT = array_merge(parse_config($all_modules[$name]->getElementsByTagName('default')->item(0),'option'), $OPT);
  91. include_once(ONYX.'modules/'.$name.'/main.php');
  92. $all_opt[$name] = $OPT;
  93. $load_modules[] = $name;
  94. }
  95. }
  96. if($xml->documentElement->getAttribute('cache'))
  97. Cache::set('_configuration_'.FILE,array('var' => $VAR,'env' => $ENV, 'root' => $root,'modules' => $load_modules,'opt' => $all_opt, 'check' => md5_file(ONYX.'config/root.xml').md5_file(ONYX.'modules/modules.xml')));
  98. else Cache::del('_configuration_'.FILE);
  99. }
  100. else trigger_error('Fichier hors de la racine',E_USER_ERROR);
  101. }
  102. else trigger_error('Configuration erronnee',E_USER_ERROR);
  103. define('CACHED',FALSE);
  104. }
  105. error_reporting($ENV['error_level']);
  106. set_include_path(get_include_path() . PATH_SEPARATOR . ONYX.'include/');
  107. ignore_user_abort($ENV['ignore_user_abort']);
  108. setLocale(LC_ALL,$ENV['locale']);
  109. date_default_timezone_set($ENV["timezone"]);
  110. if(isset($VAR['chrono']))
  111. {
  112. function view_chrono()
  113. {
  114. global $chrono_start,$VAR;
  115. echo str_replace('$',round(microtime(TRUE) - $chrono_start,4),$VAR['chrono']);
  116. }
  117. register_shutdown_function('view_chrono');
  118. }
  119. if($ENV['log_php']) ini_set('error_log',ONYX.'log/php.log');
  120. foreach($ENV['ini'] as $name => $value) ini_set($name,$value);
  121. foreach($ENV['header'] as $name => $value) header($name.': '.$value);
  122. unset($xml,$root,$search,$paths,$paths2,$path,$key,$i,$value,$config,$modules,$module,$load_modules,$xml_modules,$all_modules,$name,$require,$OPT,$all_opt,$dir,$file,$ENV,$cached);
  123. ?>