/spFunctions.php
PHP | 354 lines | 193 code | 16 blank | 145 comment | 82 complexity | c5b3a6c0dc8c363760d4f7e5f3d39ac8 MD5 | raw file
- <?php
- /////////////////////////////////////////////////////////////////
- // SpeedPHP??PHP??, Copyright (C) 2008 - 2010 SpeedPHP.com //
- /////////////////////////////////////////////////////////////////
-
- /**
- * spRun ??????
- */
- function spRun(){
- GLOBAL $__controller, $__action;
- // ?????????????
- spLaunch("router_prefilter");
- // ???????????????
- $handle_controller = spClass($__controller, null, $GLOBALS['G_SP']["controller_path"].'/'.$__controller.".php");
- // ??????????????????
- if(!is_object($handle_controller) || !method_exists($handle_controller, $__action)){
- eval($GLOBALS['G_SP']["dispatcher_error"]);
- exit;
- }
- // ?????????
- $handle_controller->$__action();
- // ???????????????????
- if(FALSE != $GLOBALS['G_SP']['view']['auto_display']){
- $__tplname = $__controller.$GLOBALS['G_SP']['view']['auto_display_sep'].
- $__action.$GLOBALS['G_SP']['view']['auto_display_suffix']; // ??????
- $handle_controller->auto_display($__tplname);
- }
- // ???????????
- spLaunch("router_postfilter");
- }
-
- /**
- * dump ?????????
- *
- * @param vars ??
- * @param output ???????
- * @param show_trace ?????spError?????????
- */
- function dump($vars, $output = TRUE, $show_trace = FALSE){
- // ?????????????????????????
- if(TRUE != SP_DEBUG && TRUE != $GLOBALS['G_SP']['allow_trace_onrelease'])return;
- if( TRUE == $show_trace ){ // ????????
- $content = spError(htmlspecialchars(print_r($vars, true)), TRUE, FALSE);
- }else{
- $content = "<div align=left><pre>\n" . htmlspecialchars(print_r($vars, true)) . "\n</pre></div>\n";
- }
- if(TRUE != $output) { return $content; } // ?????????
- echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>{$content}</body></html>";
- return;
- }
-
- /**
- * import ??????
- *
- * @param filename ??????????????
- * @param auto_search ????????????????????????????????????? -> ????Model?? -> sp????????
- * @param auto_error ?????????????
- */
- function import($sfilename, $auto_search = TRUE, $auto_error = FALSE){
- if(isset($GLOBALS['G_SP']["import_file"][md5($sfilename)]))return TRUE; // ????????
- // ??$sfilename??????
- if( TRUE == @is_readable($sfilename) ){
- require($sfilename); // ????
- $GLOBALS['G_SP']['import_file'][md5($sfilename)] = TRUE; // ????????????
- return TRUE;
- }else{
- if(TRUE == $auto_search){ // ??????
- // ????????? -> ????Model?? -> sp???????????????
- foreach(array_merge( $GLOBALS['G_SP']['include_path'], array($GLOBALS['G_SP']['model_path']), $GLOBALS['G_SP']['sp_include_path'] ) as $sp_include_path){
- // ???????????????????
- if(isset($GLOBALS['G_SP']["import_file"][md5($sp_include_path.'/'.$sfilename)]))return TRUE;
- if( is_readable( $sp_include_path.'/'.$sfilename ) ){
- require($sp_include_path.'/'.$sfilename);// ????
- $GLOBALS['G_SP']['import_file'][md5($sp_include_path.'/'.$sfilename)] = TRUE;// ????????????
- return TRUE;
- }
- }
- }
- }
- if( TRUE == $auto_error )spError("???????{$sfilename}???");
- return FALSE;
- }
-
- /**
- * spAccess ?????????
- *
- * @param method ?????????"w"????????"r"???????"c"?????
- * @param name ???????
- * @param value ?????????????????????NULL
- * @param life_time ???????????????
- */
- function spAccess($method, $name, $value = NULL, $life_time = -1){
- // ??function_access???
- if( $launch = spLaunch("function_access", array('method'=>$method, 'name'=>$name, 'value'=>$value, 'life_time'=>$life_time), TRUE) )return $launch;
- // ?????????????????????$name?MD5???????php
- if(!is_dir($GLOBALS['G_SP']['sp_cache']))__mkdirs($GLOBALS['G_SP']['sp_cache']);
- $sfile = $GLOBALS['G_SP']['sp_cache'].'/'.$GLOBALS['G_SP']['sp_app_id'].md5($name).".php";
- // ?$method???????????????
- if('w' == $method){
- // ?????$life_time?-1???????$life_time???$life_time???
- $life_time = ( -1 == $life_time ) ? '300000000' : $life_time;
- // ??????????????????PHP?die();???????????
- $value = '<?php die();?>'.( time() + $life_time ).serialize($value); // ?????????
- return file_put_contents($sfile, $value);
- }elseif('c' == $method){
- // ??????????????
- return @unlink($sfile);
- }else{
- // ??????????????????????????????
- if( !is_readable($sfile) )return FALSE;
- $arg_data = file_get_contents($sfile);
- // ???????$life_time?????????
- if( substr($arg_data, 14, 10) < time() ){
- @unlink($sfile); // ????????????FALSE
- return FALSE;
- }
- return unserialize(substr($arg_data, 24)); // ?????????
- }
- }
-
- /**
- * spClass ?????? ????????????????????
- *
- * @param class_name ???
- * @param args ???????????????
- * @param sdir ????????????????+????????????????sdir?????import()????
- * @param force_inst ???????????
- */
- function spClass($class_name, $args = null, $sdir = null, $force_inst = FALSE){
- // ????????????????????????
- if(preg_match('/[^a-z0-9\-_.]/i', $class_name))spError($class_name."??????????");
- // ?????????????????????????????
- if(TRUE != $force_inst)if(isset($GLOBALS['G_SP']["inst_class"][$class_name]))return $GLOBALS['G_SP']["inst_class"][$class_name];
- // ??$sdir?????????????
- if(null != $sdir && !import($sdir) && !import($sdir.'/'.$class_name.'.php'))return FALSE;
-
- $has_define = FALSE;
- // ?????????
- if(class_exists($class_name, false) || interface_exists($class_name, false)){
- $has_define = TRUE;
- }else{
- if( TRUE == import($class_name.'.php')){
- $has_define = TRUE;
- }
- }
- if(FALSE != $has_define){
- $argString = '';$comma = '';
- if(null != $args)for ($i = 0; $i < count($args); $i ++) { $argString .= $comma . "\$args[$i]"; $comma = ', '; }
- eval("\$GLOBALS['G_SP']['inst_class'][\$class_name]= new \$class_name($argString);");
- return $GLOBALS['G_SP']["inst_class"][$class_name];
- }
- spError($class_name."???????????");
- }
-
- /**
- * spError ????????????
- *
- * @param msg ????
- * @param output ????
- * @param stop ??????
- */
- function spError($msg, $output = TRUE, $stop = TRUE){
- if($GLOBALS['G_SP']['sp_error_throw_exception'])throw new Exception($msg);
- if(TRUE != SP_DEBUG){error_log($msg);if(TRUE == $stop)exit;}
- $traces = debug_backtrace();
- $bufferabove = ob_get_clean();
- require_once($GLOBALS['G_SP']['sp_notice_php']);
- if(TRUE == $stop)exit;
- }
-
- /**
- * spLaunch ??????
- *
- * @param configname ?????????
- * @param launchargs ????
- * @param return ?????????????????????????????
- */
- function spLaunch($configname, $launchargs = null, $returns = FALSE ){
- if( isset($GLOBALS['G_SP']['launch'][$configname]) && is_array($GLOBALS['G_SP']['launch'][$configname]) ){
- foreach( $GLOBALS['G_SP']['launch'][$configname] as $launch ){
- if( is_array($launch) ){
- $reval = spClass($launch[0])->{$launch[1]}($launchargs);
- }else{
- $reval = call_user_func_array($launch, $launchargs);
- }
- if( TRUE == $returns )return $reval;
- }
- }
- return false;
- }
- /**
- *
- * T
- *
- * ??????????
- *
- * @param w ???????
- *
- */
- function T($w) {
- $method = $GLOBALS['G_SP']["lang"][spController::getLang()];
- if(!isset($method) || 'default' == $method){
- return $w;
- }elseif( function_exists($method) ){
- return ( $tmp = call_user_func($method, $w) ) ? $tmp : $w;
- }elseif( is_array($method) ){
- return ( $tmp = spClass($method[0])->{$method[1]}($w) ) ? $tmp : $w;
- }elseif( file_exists($method) ){
- $dict = require($method);
- return isset($dict[$w]) ? $dict[$w] : $w;
- }else{
- return $w;
- }
- }
-
- /**
- *
- * spUrl
- *
- * URL???????
- *
- * @param controller ???????????'default_controller'
- * @param action ??????????'default_action'
- * @param args ??????????
- * @param anchor ????
- * @param no_sphtml ????spHtml????FALSE???????spHtml???
- */
- function spUrl($controller = null, $action = null, $args = null, $anchor = null, $no_sphtml = FALSE) {
- if(TRUE == $GLOBALS['G_SP']['html']["enabled"] && TRUE != $no_sphtml){
- // ???HTML???????HTML???????????
- $realhtml = spHtml::getUrl($controller, $action, $args, $anchor);if(isset($realhtml[0]))return $realhtml[0];
- }
- $controller = ( null != $controller ) ? $controller : $GLOBALS['G_SP']["default_controller"];
- $action = ( null != $action ) ? $action : $GLOBALS['G_SP']["default_action"];
- // ?????
- if( $launch = spLaunch("function_url", array('controller'=>$controller, 'action'=>$action, 'args'=>$args, 'anchor'=>$anchor, 'no_sphtml'=>$no_sphtml), TRUE ))return $launch;
- if( TRUE == $GLOBALS['G_SP']['url']["url_path_info"] ){ // ??path_info??
- $url = $GLOBALS['G_SP']['url']["url_path_base"]."/{$controller}/{$action}";
- if(null != $args)foreach($args as $key => $arg) $url .= "/{$key}/{$arg}";
- }else{
- $url = $GLOBALS['G_SP']['url']["url_path_base"]."?". $GLOBALS['G_SP']["url_controller"]. "={$controller}&";
- $url .= $GLOBALS['G_SP']["url_action"]. "={$action}";
- if(null != $args)foreach($args as $key => $arg) $url .= "&{$key}={$arg}";
- }
- if(null != $anchor) $url .= "#".$anchor;
- return $url;
- }
-
-
- /**
- * __mkdirs
- *
- * ???????????
- *
- * @param dir ????
- * @param mode ????
- */
- function __mkdirs($dir, $mode = 0777)
- {
- if (!is_dir($dir)) {
- __mkdirs(dirname($dir), $mode);
- return @mkdir($dir, $mode);
- }
- return true;
- }
-
- /**
- * spExt
- *
- * ????????????
- *
- * @param ext_node_name ?????
- */
- function spExt($ext_node_name)
- {
- return (empty($GLOBALS['G_SP']['ext'][$ext_node_name])) ? FALSE : $GLOBALS['G_SP']['ext'][$ext_node_name];
- }
-
- /**
- * spAddViewFunction
- *
- * ?????????????????????????????????
- *
- * @param alias ?????????
- * @param callback_function ????????
- */
- function spAddViewFunction($alias, $callback_function)
- {
- return $GLOBALS['G_SP']["view_registered_functions"][$alias] = $callback_function;
- }
-
- /**
- * spDB ??????SpeedPHP DataBase?????????????
- *
- * spDB????????spModel???????????spModel??????????????(spModel???)???
- * spDB???spModel???????????????????spModel??????????????????????
- *
- * ?????????
- * 1. ?????spModel????????????????
- * 2. ????????spModel???????
- *
- * @param tbl_name ??? ? ?????????????db_spdb_full_tblname??????????????
- * ????????db_spdb_full_tblname = true?tbl_name??????? + ????
- * ????db_spdb_full_tblname = false?????????db???????prefix?
- * @param pk ??????????????????????????????????
- */
- function spDB($tbl_name, $pk = null){
- $modelObj = spClass("spModel");
- $modelObj->tbl_name = (TRUE == $GLOBALS['G_SP']["db_spdb_full_tblname"]) ? $tbl_name : $GLOBALS['G_SP']['db']['prefix'] . $tbl_name;
- if( !$pk ){ // ?????????getTable???
- @list($pk) = $modelObj->_db->getTable($modelObj->tbl_name);$pk = $pk['Field'];
- }
- $modelObj->pk = $pk;
- return $modelObj;
- }
-
- /**
- * json_decode/json_encode
- *
- * ??????JSON????????Services_JSON?
- *
- */
- if ( !function_exists('json_decode') ){
- function json_decode($content, $assoc=false){
- if ( $assoc ){
- return spClass("Services_JSON", array(16))->decode($content);
- } else {
- return spClass("Services_JSON")->decode($content);
- }
- }
- }
- if ( !function_exists('json_encode') ){
- function json_encode($content){return spClass("Services_JSON")->encode($content);}
- }
-
- /**
- * spConfigReady ????????????????
- *
- * @param preconfig ????
- * @param useconfig ????
- */
- function spConfigReady( $preconfig, $useconfig = null){
- $nowconfig = $preconfig;
- if (is_array($useconfig)){
- foreach ($useconfig as $key => $val){
- if (is_array($useconfig[$key])){
- @$nowconfig[$key] = is_array($nowconfig[$key]) ? spConfigReady($nowconfig[$key], $useconfig[$key]) : $useconfig[$key];
- }else{
- @$nowconfig[$key] = $val;
- }
- }
- }
- return $nowconfig;
- }