runtime.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. // +--------------------------------------------------------------------------
  3. // | Senthot [ DEVELOPED BY ME ]
  4. // +--------------------------------------------------------------------------
  5. // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
  6. // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // | Author: ms134n ( [email protected] )
  8. // +--------------------------------------------------------------------------
  9. /**
  10. * Senthot Runtime files Compiled longer loaded
  11. * @category Sen
  12. * @package Common
  13. * @author ms134n <[email protected]>
  14. */
  15. defined('SEN_PATH') or exit();
  16. if(version_compare(PHP_VERSION,'5.2.0','<')) die('require PHP > 5.2.0 !');
  17. // Version Information
  18. define('SEN_VERSION', '2.2.2');
  19. // System Information
  20. if(version_compare(PHP_VERSION,'5.4.0','<')) {
  21. ini_set('magic_quotes_runtime',0);
  22. define('MAGIC_QUOTES_GPC',get_magic_quotes_gpc()?True:False);
  23. }else{
  24. define('MAGIC_QUOTES_GPC',false);
  25. }
  26. define('IS_CGI',substr(PHP_SAPI, 0,3)=='cgi' ? 1 : 0 );
  27. define('IS_WIN',strstr(PHP_OS, 'WIN') ? 1 : 0 );
  28. define('IS_CLI',PHP_SAPI=='cli'? 1 : 0);
  29. // Project Name
  30. defined('APP_NAME') or define('APP_NAME', basename(dirname($_SERVER['SCRIPT_FILENAME'])));
  31. if(!IS_CLI) {
  32. // Current file name
  33. if(!defined('_PHP_FILE_')) {
  34. if(IS_CGI) {
  35. //CGI/FASTCGIMode under
  36. $_temp = explode('.php',$_SERVER['PHP_SELF']);
  37. define('_PHP_FILE_', rtrim(str_replace($_SERVER['HTTP_HOST'],'',$_temp[0].'.php'),'/'));
  38. }else {
  39. define('_PHP_FILE_', rtrim($_SERVER['SCRIPT_NAME'],'/'));
  40. }
  41. }
  42. if(!defined('__ROOT__')) {
  43. // Site URL root
  44. if( strtoupper(APP_NAME) == strtoupper(basename(dirname(_PHP_FILE_))) ) {
  45. $_root = dirname(dirname(_PHP_FILE_));
  46. }else {
  47. $_root = dirname(_PHP_FILE_);
  48. }
  49. define('__ROOT__', (($_root=='/' || $_root=='\\')?'':$_root));
  50. }
  51. //Support the URLMode
  52. define('URL_COMMON', 0); //Normal mode
  53. define('URL_PATHINFO', 1); //PATHINFOMode
  54. define('URL_REWRITE', 2); //REWRITEMode
  55. define('URL_COMPAT', 3); // Compatibility Mode
  56. }
  57. // Path settings Can be redefined in the import documents All paths must be in constant/ End
  58. defined('CORE_PATH') or define('CORE_PATH', SEN_PATH.'Lib/'); // Core class library System catalog
  59. defined('ADDONS_PATH') or define('ADDONS_PATH', SEN_PATH.'Addons/'); // System extensions directory
  60. defined('MODE_PATH') or define('MODE_PATH', ADDONS_PATH.'Mode/'); // Mode extension directory
  61. defined('ENGINE_PATH') or define('ENGINE_PATH', ADDONS_PATH.'Engine/'); // Engine extensions directory
  62. defined('VENDOR_PATH') or define('VENDOR_PATH', ADDONS_PATH.'Vendor/'); // List of third-party class libraries
  63. defined('LIBRARY_PATH') or define('LIBRARY_PATH', ADDONS_PATH.'Library/'); // Addonsing class library directory
  64. defined('COMMON_PATH') or define('COMMON_PATH', APP_PATH.'Common/'); // Public directory
  65. defined('LIB_PATH') or define('LIB_PATH', APP_PATH.'Lib/'); // Project class library directory
  66. defined('CONF_PATH') or define('CONF_PATH', APP_PATH.'Conf/'); // List of project configurations
  67. defined('LANG_PATH') or define('LANG_PATH', APP_PATH.'Lang/'); // Project language pack directory
  68. defined('TMPL_PATH') or define('TMPL_PATH', APP_PATH.'Tpl/'); // Project template directory
  69. defined('HTML_PATH') or define('HTML_PATH', APP_PATH.'Html/'); // Project static directory
  70. defined('LOG_PATH') or define('LOG_PATH', RUNTIME_PATH.'Logs/'); // Project log directory
  71. defined('TEMP_PATH') or define('TEMP_PATH', RUNTIME_PATH.'Temp/'); // Project cache directory
  72. defined('DATA_PATH') or define('DATA_PATH', RUNTIME_PATH.'Data/'); // Project data directory
  73. defined('CACHE_PATH') or define('CACHE_PATH', RUNTIME_PATH.'Cache/'); // Project template cache directory
  74. // In order to facilitate import third-party class libraries Set up Vendor Directory to the include_path
  75. set_include_path(get_include_path() . PATH_SEPARATOR . VENDOR_PATH);
  76. // Load the file you need to run And is responsible for the automatic list generation
  77. function load_runtime_file() {
  78. // Loading SystemBasic function library
  79. require SEN_PATH.'Common/common.php';
  80. // Read the core file list
  81. $list = array(
  82. CORE_PATH.'Core/Sen.class.php',
  83. CORE_PATH.'Core/SenException.class.php', // Exception class
  84. CORE_PATH.'Core/Behavior.class.php',
  85. );
  86. // Loading Mode file list
  87. foreach ($list as $key=>$file){
  88. if(is_file($file)) require_cache($file);
  89. }
  90. // System class library is loaded the alias definition
  91. alias_import(include SEN_PATH.'Conf/alias.php');
  92. // Check the project directory structure If it does not exist it is created automatically
  93. if(!is_dir(LIB_PATH)) {
  94. // To create the project directory structure
  95. build_app_dir();
  96. }elseif(!is_dir(CACHE_PATH)){
  97. // Checking cache directory
  98. check_runtime();
  99. }elseif(APP_DEBUG){
  100. // Toggle Debugging Mode delete the compiler cache
  101. if(is_file(RUNTIME_FILE)) unlink(RUNTIME_FILE);
  102. }
  103. }
  104. // Checking cache directory(Runtime) If it does not exist it is created automatically
  105. function check_runtime() {
  106. if(!is_dir(RUNTIME_PATH)) {
  107. mkdir(RUNTIME_PATH);
  108. }elseif(!is_writeable(RUNTIME_PATH)) {
  109. header('Content-Type:text/html; charset=utf-8');
  110. exit('Directory [ '.RUNTIME_PATH.' ] Is not writable!');
  111. }
  112. mkdir(CACHE_PATH); // Template cache directory
  113. if(!is_dir(LOG_PATH)) mkdir(LOG_PATH); // Log directory
  114. if(!is_dir(TEMP_PATH)) mkdir(TEMP_PATH); // Data cache directory
  115. if(!is_dir(DATA_PATH)) mkdir(DATA_PATH); // Data file directory
  116. return true;
  117. }
  118. // Create a compiler cache
  119. function build_runtime_cache($append='') {
  120. // Generates compiled files
  121. $defs = get_defined_constants(TRUE);
  122. $content = '$GLOBALS[\'_beginTime\'] = microtime(TRUE);';
  123. if(defined('RUNTIME_DEF_FILE')) { // Outside compiled constants file introduction
  124. file_put_contents(RUNTIME_DEF_FILE,'<?php '.array_define($defs['user']));
  125. $content .= 'require \''.RUNTIME_DEF_FILE.'\';';
  126. }else{
  127. $content .= array_define($defs['user']);
  128. }
  129. $content .= 'set_include_path(get_include_path() . PATH_SEPARATOR . VENDOR_PATH);';
  130. // Core reading list compiled file
  131. $list = array(
  132. SEN_PATH.'Common/common.php',
  133. CORE_PATH.'Core/Sen.class.php',
  134. CORE_PATH.'Core/SenException.class.php',
  135. CORE_PATH.'Core/Behavior.class.php',
  136. );
  137. foreach ($list as $file){
  138. $content .= compile($file);
  139. }
  140. // Addonsed File System behavior to compile
  141. $content .= build_tags_cache();
  142. $alias = include SEN_PATH.'Conf/alias.php';
  143. $content .= 'alias_import('.var_export($alias,true).');';
  144. // Compile the framework Default Language packs and configuration parameters
  145. $content .= $append."\nL(".var_export(L(),true).");C(".var_export(C(),true).');G(\'loadTime\');Sen::Start();';
  146. file_put_contents(RUNTIME_FILE,strip_whitespace('<?php '.str_replace("defined('SEN_PATH') or exit();",' ',$content)));
  147. }
  148. // Compilation System extension class library
  149. function build_tags_cache() {
  150. $tags = C('extends');
  151. $content = '';
  152. foreach ($tags as $tag=>$item){
  153. foreach ($item as $key=>$name) {
  154. $content .= is_int($key)?compile(CORE_PATH.'Behavior/'.$name.'Behavior.class.php'):compile($name);
  155. }
  156. }
  157. return $content;
  158. }
  159. // To create the project directory structure
  160. function build_app_dir() {
  161. // Failed to create project directory is created automatically
  162. if(!is_dir(APP_PATH)) mkdir(APP_PATH,0755,true);
  163. if(is_writeable(APP_PATH)) {
  164. $dirs = array(
  165. LIB_PATH,
  166. RUNTIME_PATH,
  167. CONF_PATH,
  168. COMMON_PATH,
  169. LANG_PATH,
  170. CACHE_PATH,
  171. TMPL_PATH,
  172. TMPL_PATH.C('DEFAULT_THEME').'/',
  173. LOG_PATH,
  174. TEMP_PATH,
  175. DATA_PATH,
  176. LIB_PATH.'Model/',
  177. LIB_PATH.'Action/',
  178. LIB_PATH.'Behavior/',
  179. LIB_PATH.'Widget/',
  180. );
  181. foreach ($dirs as $dir){
  182. if(!is_dir($dir)) mkdir($dir,0755,true);
  183. }
  184. // Write to directory security file
  185. build_dir_secure($dirs);
  186. // Write the initial configuration file
  187. if(!is_file(CONF_PATH.'config.php'))
  188. file_put_contents(CONF_PATH.'config.php',"<?php\nreturn array(\n\t//'Configuration item'=>'Configuration values'\n);\n?>");
  189. // Written test Action
  190. if(!is_file(LIB_PATH.'Action/IndexAction.class.php'))
  191. build_first_action();
  192. }else{
  193. header('Content-Type:text/html; charset=utf-8');
  194. exit('The project directory is not writable directory cannot be automatically generated!<BR>Please use the project Builder or manually generated list of projects~');
  195. }
  196. }
  197. // Create a test Action
  198. function build_first_action() {
  199. $content = file_get_contents(SEN_PATH.'Tpl/default_index.tpl');
  200. file_put_contents(LIB_PATH.'Action/IndexAction.class.php',$content);
  201. }
  202. // Build directory security file
  203. function build_dir_secure($dirs='') {
  204. // Write directory security
  205. if(defined('BUILD_DIR_SECURE') && BUILD_DIR_SECURE) {
  206. defined('DIR_SECURE_FILENAME') or define('DIR_SECURE_FILENAME', 'index.html');
  207. defined('DIR_SECURE_CONTENT') or define('DIR_SECURE_CONTENT', ' ');
  208. // Automatic write to directory security file
  209. $content = DIR_SECURE_CONTENT;
  210. $files = explode(',', DIR_SECURE_FILENAME);
  211. foreach ($files as $filename){
  212. foreach ($dirs as $dir)
  213. file_put_contents($dir.$filename,$content);
  214. }
  215. }
  216. }
  217. // Required to load the runtime files
  218. load_runtime_file();
  219. // Records the time loading files
  220. G('loadTime');
  221. // Execute the entry
  222. Sen::Start();