PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/spFunctions.php

http://speedphp.googlecode.com/
PHP | 354 lines | 193 code | 16 blank | 145 comment | 82 complexity | c5b3a6c0dc8c363760d4f7e5f3d39ac8 MD5 | raw file
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. // SpeedPHP??PHP??, Copyright (C) 2008 - 2010 SpeedPHP.com //
  4. /////////////////////////////////////////////////////////////////
  5. /**
  6. * spRun ??????
  7. */
  8. function spRun(){
  9. GLOBAL $__controller, $__action;
  10. // ?????????????
  11. spLaunch("router_prefilter");
  12. // ???????????????
  13. $handle_controller = spClass($__controller, null, $GLOBALS['G_SP']["controller_path"].'/'.$__controller.".php");
  14. // ??????????????????
  15. if(!is_object($handle_controller) || !method_exists($handle_controller, $__action)){
  16. eval($GLOBALS['G_SP']["dispatcher_error"]);
  17. exit;
  18. }
  19. // ?????????
  20. $handle_controller->$__action();
  21. // ???????????????????
  22. if(FALSE != $GLOBALS['G_SP']['view']['auto_display']){
  23. $__tplname = $__controller.$GLOBALS['G_SP']['view']['auto_display_sep'].
  24. $__action.$GLOBALS['G_SP']['view']['auto_display_suffix']; // ??????
  25. $handle_controller->auto_display($__tplname);
  26. }
  27. // ???????????
  28. spLaunch("router_postfilter");
  29. }
  30. /**
  31. * dump ?????????
  32. *
  33. * @param vars ??
  34. * @param output ???????
  35. * @param show_trace ?????spError?????????
  36. */
  37. function dump($vars, $output = TRUE, $show_trace = FALSE){
  38. // ?????????????????????????
  39. if(TRUE != SP_DEBUG && TRUE != $GLOBALS['G_SP']['allow_trace_onrelease'])return;
  40. if( TRUE == $show_trace ){ // ????????
  41. $content = spError(htmlspecialchars(print_r($vars, true)), TRUE, FALSE);
  42. }else{
  43. $content = "<div align=left><pre>\n" . htmlspecialchars(print_r($vars, true)) . "\n</pre></div>\n";
  44. }
  45. if(TRUE != $output) { return $content; } // ?????????
  46. echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>{$content}</body></html>";
  47. return;
  48. }
  49. /**
  50. * import ??????
  51. *
  52. * @param filename ??????????????
  53. * @param auto_search ????????????????????????????????????? -> ????Model?? -> sp????????
  54. * @param auto_error ?????????????
  55. */
  56. function import($sfilename, $auto_search = TRUE, $auto_error = FALSE){
  57. if(isset($GLOBALS['G_SP']["import_file"][md5($sfilename)]))return TRUE; // ????????
  58. // ??$sfilename??????
  59. if( TRUE == @is_readable($sfilename) ){
  60. require($sfilename); // ????
  61. $GLOBALS['G_SP']['import_file'][md5($sfilename)] = TRUE; // ????????????
  62. return TRUE;
  63. }else{
  64. if(TRUE == $auto_search){ // ??????
  65. // ?“???????? -> ????Model?? -> sp????????”???????
  66. foreach(array_merge( $GLOBALS['G_SP']['include_path'], array($GLOBALS['G_SP']['model_path']), $GLOBALS['G_SP']['sp_include_path'] ) as $sp_include_path){
  67. // ???????????????????
  68. if(isset($GLOBALS['G_SP']["import_file"][md5($sp_include_path.'/'.$sfilename)]))return TRUE;
  69. if( is_readable( $sp_include_path.'/'.$sfilename ) ){
  70. require($sp_include_path.'/'.$sfilename);// ????
  71. $GLOBALS['G_SP']['import_file'][md5($sp_include_path.'/'.$sfilename)] = TRUE;// ????????????
  72. return TRUE;
  73. }
  74. }
  75. }
  76. }
  77. if( TRUE == $auto_error )spError("???????{$sfilename}???");
  78. return FALSE;
  79. }
  80. /**
  81. * spAccess ?????????
  82. *
  83. * @param method ?????????"w"????????"r"???????"c"?????
  84. * @param name ???????
  85. * @param value ?????????????????????NULL
  86. * @param life_time ???????????????
  87. */
  88. function spAccess($method, $name, $value = NULL, $life_time = -1){
  89. // ??function_access???
  90. if( $launch = spLaunch("function_access", array('method'=>$method, 'name'=>$name, 'value'=>$value, 'life_time'=>$life_time), TRUE) )return $launch;
  91. // ?????????????????????$name?MD5???????php
  92. if(!is_dir($GLOBALS['G_SP']['sp_cache']))__mkdirs($GLOBALS['G_SP']['sp_cache']);
  93. $sfile = $GLOBALS['G_SP']['sp_cache'].'/'.$GLOBALS['G_SP']['sp_app_id'].md5($name).".php";
  94. // ?$method???????????????
  95. if('w' == $method){
  96. // ?????$life_time?-1???????$life_time???$life_time???
  97. $life_time = ( -1 == $life_time ) ? '300000000' : $life_time;
  98. // ??????????????????PHP?die();???????????
  99. $value = '<?php die();?>'.( time() + $life_time ).serialize($value); // ?????????
  100. return file_put_contents($sfile, $value);
  101. }elseif('c' == $method){
  102. // ??????????????
  103. return @unlink($sfile);
  104. }else{
  105. // ??????????????????????????????
  106. if( !is_readable($sfile) )return FALSE;
  107. $arg_data = file_get_contents($sfile);
  108. // ???????$life_time?????????
  109. if( substr($arg_data, 14, 10) < time() ){
  110. @unlink($sfile); // ????????????FALSE
  111. return FALSE;
  112. }
  113. return unserialize(substr($arg_data, 24)); // ?????????
  114. }
  115. }
  116. /**
  117. * spClass ?????? ????????????????????
  118. *
  119. * @param class_name ???
  120. * @param args ???????????????
  121. * @param sdir ????????????????+????????????????sdir?????import()????
  122. * @param force_inst ???????????
  123. */
  124. function spClass($class_name, $args = null, $sdir = null, $force_inst = FALSE){
  125. // ????????????????????????
  126. if(preg_match('/[^a-z0-9\-_.]/i', $class_name))spError($class_name."??????????");
  127. // ?????????????????????????????
  128. if(TRUE != $force_inst)if(isset($GLOBALS['G_SP']["inst_class"][$class_name]))return $GLOBALS['G_SP']["inst_class"][$class_name];
  129. // ??$sdir?????????????
  130. if(null != $sdir && !import($sdir) && !import($sdir.'/'.$class_name.'.php'))return FALSE;
  131. $has_define = FALSE;
  132. // ?????????
  133. if(class_exists($class_name, false) || interface_exists($class_name, false)){
  134. $has_define = TRUE;
  135. }else{
  136. if( TRUE == import($class_name.'.php')){
  137. $has_define = TRUE;
  138. }
  139. }
  140. if(FALSE != $has_define){
  141. $argString = '';$comma = '';
  142. if(null != $args)for ($i = 0; $i < count($args); $i ++) { $argString .= $comma . "\$args[$i]"; $comma = ', '; }
  143. eval("\$GLOBALS['G_SP']['inst_class'][\$class_name]= new \$class_name($argString);");
  144. return $GLOBALS['G_SP']["inst_class"][$class_name];
  145. }
  146. spError($class_name."???????????");
  147. }
  148. /**
  149. * spError ????????????
  150. *
  151. * @param msg ????
  152. * @param output ????
  153. * @param stop ??????
  154. */
  155. function spError($msg, $output = TRUE, $stop = TRUE){
  156. if($GLOBALS['G_SP']['sp_error_throw_exception'])throw new Exception($msg);
  157. if(TRUE != SP_DEBUG){error_log($msg);if(TRUE == $stop)exit;}
  158. $traces = debug_backtrace();
  159. $bufferabove = ob_get_clean();
  160. require_once($GLOBALS['G_SP']['sp_notice_php']);
  161. if(TRUE == $stop)exit;
  162. }
  163. /**
  164. * spLaunch ??????
  165. *
  166. * @param configname ?????????
  167. * @param launchargs ????
  168. * @param return ?????????????????????????????
  169. */
  170. function spLaunch($configname, $launchargs = null, $returns = FALSE ){
  171. if( isset($GLOBALS['G_SP']['launch'][$configname]) && is_array($GLOBALS['G_SP']['launch'][$configname]) ){
  172. foreach( $GLOBALS['G_SP']['launch'][$configname] as $launch ){
  173. if( is_array($launch) ){
  174. $reval = spClass($launch[0])->{$launch[1]}($launchargs);
  175. }else{
  176. $reval = call_user_func_array($launch, $launchargs);
  177. }
  178. if( TRUE == $returns )return $reval;
  179. }
  180. }
  181. return false;
  182. }
  183. /**
  184. *
  185. * T
  186. *
  187. * ??????????
  188. *
  189. * @param w ???????
  190. *
  191. */
  192. function T($w) {
  193. $method = $GLOBALS['G_SP']["lang"][spController::getLang()];
  194. if(!isset($method) || 'default' == $method){
  195. return $w;
  196. }elseif( function_exists($method) ){
  197. return ( $tmp = call_user_func($method, $w) ) ? $tmp : $w;
  198. }elseif( is_array($method) ){
  199. return ( $tmp = spClass($method[0])->{$method[1]}($w) ) ? $tmp : $w;
  200. }elseif( file_exists($method) ){
  201. $dict = require($method);
  202. return isset($dict[$w]) ? $dict[$w] : $w;
  203. }else{
  204. return $w;
  205. }
  206. }
  207. /**
  208. *
  209. * spUrl
  210. *
  211. * URL???????
  212. *
  213. * @param controller ???????????'default_controller'
  214. * @param action ??????????'default_action'
  215. * @param args ??????????
  216. * @param anchor ????
  217. * @param no_sphtml ????spHtml????FALSE???????spHtml???
  218. */
  219. function spUrl($controller = null, $action = null, $args = null, $anchor = null, $no_sphtml = FALSE) {
  220. if(TRUE == $GLOBALS['G_SP']['html']["enabled"] && TRUE != $no_sphtml){
  221. // ???HTML???????HTML???????????
  222. $realhtml = spHtml::getUrl($controller, $action, $args, $anchor);if(isset($realhtml[0]))return $realhtml[0];
  223. }
  224. $controller = ( null != $controller ) ? $controller : $GLOBALS['G_SP']["default_controller"];
  225. $action = ( null != $action ) ? $action : $GLOBALS['G_SP']["default_action"];
  226. // ?????
  227. if( $launch = spLaunch("function_url", array('controller'=>$controller, 'action'=>$action, 'args'=>$args, 'anchor'=>$anchor, 'no_sphtml'=>$no_sphtml), TRUE ))return $launch;
  228. if( TRUE == $GLOBALS['G_SP']['url']["url_path_info"] ){ // ??path_info??
  229. $url = $GLOBALS['G_SP']['url']["url_path_base"]."/{$controller}/{$action}";
  230. if(null != $args)foreach($args as $key => $arg) $url .= "/{$key}/{$arg}";
  231. }else{
  232. $url = $GLOBALS['G_SP']['url']["url_path_base"]."?". $GLOBALS['G_SP']["url_controller"]. "={$controller}&";
  233. $url .= $GLOBALS['G_SP']["url_action"]. "={$action}";
  234. if(null != $args)foreach($args as $key => $arg) $url .= "&{$key}={$arg}";
  235. }
  236. if(null != $anchor) $url .= "#".$anchor;
  237. return $url;
  238. }
  239. /**
  240. * __mkdirs
  241. *
  242. * ???????????
  243. *
  244. * @param dir ????
  245. * @param mode ????
  246. */
  247. function __mkdirs($dir, $mode = 0777)
  248. {
  249. if (!is_dir($dir)) {
  250. __mkdirs(dirname($dir), $mode);
  251. return @mkdir($dir, $mode);
  252. }
  253. return true;
  254. }
  255. /**
  256. * spExt
  257. *
  258. * ????????????
  259. *
  260. * @param ext_node_name ?????
  261. */
  262. function spExt($ext_node_name)
  263. {
  264. return (empty($GLOBALS['G_SP']['ext'][$ext_node_name])) ? FALSE : $GLOBALS['G_SP']['ext'][$ext_node_name];
  265. }
  266. /**
  267. * spAddViewFunction
  268. *
  269. * ?????????????????????????????????
  270. *
  271. * @param alias ?????????
  272. * @param callback_function ????????
  273. */
  274. function spAddViewFunction($alias, $callback_function)
  275. {
  276. return $GLOBALS['G_SP']["view_registered_functions"][$alias] = $callback_function;
  277. }
  278. /**
  279. * spDB ??????SpeedPHP DataBase?????????????
  280. *
  281. * spDB????????spModel???????????spModel??????????????(spModel???)???
  282. * spDB???spModel???????????????????spModel??????????????????????
  283. *
  284. * ?????????
  285. * 1. ?????spModel????????????????
  286. * 2. ????????spModel???????
  287. *
  288. * @param tbl_name ??? ? ?????????????db_spdb_full_tblname??????????????
  289. * ????????db_spdb_full_tblname = true?tbl_name??????? + ????
  290. * ????db_spdb_full_tblname = false?????????db???????prefix?
  291. * @param pk ??????????????????????????????????
  292. */
  293. function spDB($tbl_name, $pk = null){
  294. $modelObj = spClass("spModel");
  295. $modelObj->tbl_name = (TRUE == $GLOBALS['G_SP']["db_spdb_full_tblname"]) ? $tbl_name : $GLOBALS['G_SP']['db']['prefix'] . $tbl_name;
  296. if( !$pk ){ // ?????????getTable???
  297. @list($pk) = $modelObj->_db->getTable($modelObj->tbl_name);$pk = $pk['Field'];
  298. }
  299. $modelObj->pk = $pk;
  300. return $modelObj;
  301. }
  302. /**
  303. * json_decode/json_encode
  304. *
  305. * ??????JSON????????Services_JSON?
  306. *
  307. */
  308. if ( !function_exists('json_decode') ){
  309. function json_decode($content, $assoc=false){
  310. if ( $assoc ){
  311. return spClass("Services_JSON", array(16))->decode($content);
  312. } else {
  313. return spClass("Services_JSON")->decode($content);
  314. }
  315. }
  316. }
  317. if ( !function_exists('json_encode') ){
  318. function json_encode($content){return spClass("Services_JSON")->encode($content);}
  319. }
  320. /**
  321. * spConfigReady ????????????????
  322. *
  323. * @param preconfig ????
  324. * @param useconfig ????
  325. */
  326. function spConfigReady( $preconfig, $useconfig = null){
  327. $nowconfig = $preconfig;
  328. if (is_array($useconfig)){
  329. foreach ($useconfig as $key => $val){
  330. if (is_array($useconfig[$key])){
  331. @$nowconfig[$key] = is_array($nowconfig[$key]) ? spConfigReady($nowconfig[$key], $useconfig[$key]) : $useconfig[$key];
  332. }else{
  333. @$nowconfig[$key] = $val;
  334. }
  335. }
  336. }
  337. return $nowconfig;
  338. }