base.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * base.php PHPCMS框架入口文件
  4. *
  5. * @copyright (C) 2005-2010 PHPCMS
  6. * @license http://www.phpcms.cn/license/
  7. * @lastmodify 2010-6-7
  8. */
  9. define('IN_PHPCMS', true);
  10. //PHPCMS框架路径
  11. define('PC_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
  12. if(!defined('PHPCMS_PATH')) define('PHPCMS_PATH', PC_PATH.'..'.DIRECTORY_SEPARATOR);
  13. //缓存文件夹地址
  14. define('CACHE_PATH', PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR);
  15. //主机协议
  16. define('SITE_PROTOCOL', isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://');
  17. //当前访问的主机名
  18. define('SITE_URL', (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''));
  19. //来源
  20. define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
  21. //系统开始时间
  22. define('SYS_START_TIME', microtime());
  23. //加载公用函数库
  24. pc_base::load_sys_func('global');
  25. pc_base::load_sys_func('extention');
  26. pc_base::auto_load_func();
  27. pc_base::load_config('system','errorlog') ? set_error_handler('my_error_handler') : error_reporting(E_ERROR | E_WARNING | E_PARSE);
  28. //设置本地时差
  29. function_exists('date_default_timezone_set') && date_default_timezone_set(pc_base::load_config('system','timezone'));
  30. define('CHARSET' ,pc_base::load_config('system','charset'));
  31. //输出页面字符集
  32. header('Content-type: text/html; charset='.CHARSET);
  33. define('SYS_TIME', time());
  34. //定义网站根路径
  35. define('WEB_PATH',pc_base::load_config('system','web_path'));
  36. //js 路径
  37. define('JS_PATH',pc_base::load_config('system','js_path'));
  38. //css 路径
  39. define('CSS_PATH',pc_base::load_config('system','css_path'));
  40. //img 路径
  41. define('IMG_PATH',pc_base::load_config('system','img_path'));
  42. //动态程序路径
  43. define('APP_PATH',pc_base::load_config('system','app_path'));
  44. //自定义 js 路径
  45. define('MY_JS_PATH',pc_base::load_config('system','my_js_path'));
  46. //自定义 css 路径
  47. define('MY_CSS_PATH',pc_base::load_config('system','my_css_path'));
  48. //自定义 img 路径
  49. define('MY_IMG_PATH',pc_base::load_config('system','my_img_path'));
  50. define('MY_PATH',pc_base::load_config('system','my_path'));
  51. //自定义手机版的APP_PATH
  52. define('WAP_APP_PATH',pc_base::load_config('system','wap_app_path'));
  53. //应用静态文件路径
  54. define('PLUGIN_STATICS_PATH',WEB_PATH.'statics/plugin/');
  55. if(pc_base::load_config('system','gzip') && function_exists('ob_gzhandler')) {
  56. ob_start('ob_gzhandler');
  57. } else {
  58. ob_start();
  59. }
  60. class pc_base {
  61. /**
  62. * 初始化应用程序
  63. */
  64. public static function creat_app() {
  65. return self::load_sys_class('application');
  66. }
  67. /**
  68. * 加载系统类方法
  69. * @param string $classname 类名
  70. * @param string $path 扩展地址
  71. * @param intger $initialize 是否初始化
  72. */
  73. public static function load_sys_class($classname, $path = '', $initialize = 1) {
  74. return self::_load_class($classname, $path, $initialize);
  75. }
  76. /**
  77. * 加载应用类方法
  78. * @param string $classname 类名
  79. * @param string $m 模块
  80. * @param intger $initialize 是否初始化
  81. */
  82. public static function load_app_class($classname, $m = '', $initialize = 1) {
  83. $m = empty($m) && defined('ROUTE_M') ? ROUTE_M : $m;
  84. if (empty($m)) return false;
  85. return self::_load_class($classname, 'modules'.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR.'classes', $initialize);
  86. }
  87. /**
  88. * 加载数据模型
  89. * @param string $classname 类名
  90. */
  91. public static function load_model($classname) {
  92. return self::_load_class($classname,'model');
  93. }
  94. /**
  95. * 加载类文件函数
  96. * @param string $classname 类名
  97. * @param string $path 扩展地址
  98. * @param intger $initialize 是否初始化
  99. */
  100. private static function _load_class($classname, $path = '', $initialize = 1) {
  101. static $classes = array();
  102. if (empty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'classes';
  103. $key = md5($path.$classname);
  104. if (isset($classes[$key])) {
  105. if (!empty($classes[$key])) {
  106. return $classes[$key];
  107. } else {
  108. return true;
  109. }
  110. }
  111. if (file_exists(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) {
  112. include PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php';
  113. $name = $classname;
  114. if ($my_path = self::my_path(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) {
  115. include $my_path;
  116. $name = 'MY_'.$classname;
  117. }
  118. if ($initialize) {
  119. $classes[$key] = new $name;
  120. } else {
  121. $classes[$key] = true;
  122. }
  123. return $classes[$key];
  124. } else {
  125. return false;
  126. }
  127. }
  128. /**
  129. * 加载系统的函数库
  130. * @param string $func 函数库名
  131. */
  132. public static function load_sys_func($func) {
  133. return self::_load_func($func);
  134. }
  135. /**
  136. * 自动加载autoload目录下函数库
  137. * @param string $func 函数库名
  138. */
  139. public static function auto_load_func($path='') {
  140. return self::_auto_load_func($path);
  141. }
  142. /**
  143. * 加载应用函数库
  144. * @param string $func 函数库名
  145. * @param string $m 模型名
  146. */
  147. public static function load_app_func($func, $m = '') {
  148. $m = empty($m) && defined('ROUTE_M') ? ROUTE_M : $m;
  149. if (empty($m)) return false;
  150. return self::_load_func($func, 'modules'.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR.'functions');
  151. }
  152. /**
  153. * 加载插件类库
  154. */
  155. public static function load_plugin_class($classname, $identification = '' ,$initialize = 1) {
  156. $identification = empty($identification) && defined('PLUGIN_ID') ? PLUGIN_ID : $identification;
  157. if (empty($identification)) return false;
  158. return pc_base::load_sys_class($classname, 'plugin'.DIRECTORY_SEPARATOR.$identification.DIRECTORY_SEPARATOR.'classes', $initialize);
  159. }
  160. /**
  161. * 加载插件函数库
  162. * @param string $func 函数文件名称
  163. * @param string $identification 插件标识
  164. */
  165. public static function load_plugin_func($func,$identification) {
  166. static $funcs = array();
  167. $identification = empty($identification) && defined('PLUGIN_ID') ? PLUGIN_ID : $identification;
  168. if (empty($identification)) return false;
  169. $path = 'plugin'.DIRECTORY_SEPARATOR.$identification.DIRECTORY_SEPARATOR.'functions'.DIRECTORY_SEPARATOR.$func.'.func.php';
  170. $key = md5($path);
  171. if (isset($funcs[$key])) return true;
  172. if (file_exists(PC_PATH.$path)) {
  173. include PC_PATH.$path;
  174. } else {
  175. $funcs[$key] = false;
  176. return false;
  177. }
  178. $funcs[$key] = true;
  179. return true;
  180. }
  181. /**
  182. * 加载插件数据模型
  183. * @param string $classname 类名
  184. */
  185. public static function load_plugin_model($classname,$identification) {
  186. $identification = empty($identification) && defined('PLUGIN_ID') ? PLUGIN_ID : $identification;
  187. $path = 'plugin'.DIRECTORY_SEPARATOR.$identification.DIRECTORY_SEPARATOR.'model';
  188. return self::_load_class($classname,$path);
  189. }
  190. /**
  191. * 加载函数库
  192. * @param string $func 函数库名
  193. * @param string $path 地址
  194. */
  195. private static function _load_func($func, $path = '') {
  196. static $funcs = array();
  197. if (empty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'functions';
  198. $path .= DIRECTORY_SEPARATOR.$func.'.func.php';
  199. $key = md5($path);
  200. if (isset($funcs[$key])) return true;
  201. if (file_exists(PC_PATH.$path)) {
  202. include PC_PATH.$path;
  203. } else {
  204. $funcs[$key] = false;
  205. return false;
  206. }
  207. $funcs[$key] = true;
  208. return true;
  209. }
  210. /**
  211. * 加载函数库
  212. * @param string $func 函数库名
  213. * @param string $path 地址
  214. */
  215. private static function _auto_load_func($path = '') {
  216. if (empty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'functions'.DIRECTORY_SEPARATOR.'autoload';
  217. $path .= DIRECTORY_SEPARATOR.'*.func.php';
  218. $auto_funcs = glob(PC_PATH.DIRECTORY_SEPARATOR.$path);
  219. if(!empty($auto_funcs) && is_array($auto_funcs)) {
  220. foreach($auto_funcs as $func_path) {
  221. include $func_path;
  222. }
  223. }
  224. }
  225. /**
  226. * 是否有自己的扩展文件
  227. * @param string $filepath 路径
  228. */
  229. public static function my_path($filepath) {
  230. $path = pathinfo($filepath);
  231. if (file_exists($path['dirname'].DIRECTORY_SEPARATOR.'MY_'.$path['basename'])) {
  232. return $path['dirname'].DIRECTORY_SEPARATOR.'MY_'.$path['basename'];
  233. } else {
  234. return false;
  235. }
  236. }
  237. /**
  238. * 加载配置文件
  239. * @param string $file 配置文件
  240. * @param string $key 要获取的配置荐
  241. * @param string $default 默认配置。当获取配置项目失败时该值发生作用。
  242. * @param boolean $reload 强制重新加载。
  243. */
  244. public static function load_config($file, $key = '', $default = '', $reload = false) {
  245. static $configs = array();
  246. if (!$reload && isset($configs[$file])) {
  247. if (empty($key)) {
  248. return $configs[$file];
  249. } elseif (isset($configs[$file][$key])) {
  250. return $configs[$file][$key];
  251. } else {
  252. return $default;
  253. }
  254. }
  255. $path = CACHE_PATH.'configs'.DIRECTORY_SEPARATOR.$file.'.php';
  256. if (file_exists($path)) {
  257. $configs[$file] = include $path;
  258. }
  259. if (empty($key)) {
  260. return $configs[$file];
  261. } elseif (isset($configs[$file][$key])) {
  262. return $configs[$file][$key];
  263. } else {
  264. return $default;
  265. }
  266. }
  267. }