/** * 重写MYSQL中的QUERY,对SQL语句进行监控 * @param string $sql */ public function query($sql, $is_set_default = true) { $this->get_link_id($sql); //link_id获取 $InitPHP_conf = InitPHP::getConfig(); if ($InitPHP_conf['is_debug'] == true) { $start = microtime(); } $query = $this->db->query($sql); if ($InitPHP_conf['is_debug'] == true) { $end = microtime(); } //sql query debug if ($InitPHP_conf['is_debug'] == true) { $k = count($InitPHP_conf['sqlcontrolarr']); $InitPHP_conf['sqlcontrolarr'][$k]['sql'] = $sql; $costTime = substr($end - $start, 0, 7); $InitPHP_conf['sqlcontrolarr'][$k]['queryTime'] = $costTime; $InitPHP_conf['sqlcontrolarr'][$k]['affectedRows'] = $this->affected_rows(); InitPHP::setConfig('sqlcontrolarr', $InitPHP_conf['sqlcontrolarr']); } if ($this->db->error()) { InitPHP::initError($this->db->error()); } if ($is_set_default) { $this->set_default_link_id(); } //设置默认的link_id return $query; }
/** * Error机制 私有函数,error输出 * @param string $error_type 错误类型 * @return object */ private function display($error_type) { $InitPHP_conf = InitPHP::getConfig(); if ($error_type == 'text') { $error = implode("\r\t", $this->error_data); exit($error); } elseif ($error_type == 'json') { exit(json_encode($this->error_data)); } elseif ($error_type == 'xml') { $xml = '<?xml version="1.0" encoding="utf-8"?>'; $xml .= '<return>'; foreach ($this->error_data as $v) { $xml .= '<error>' . $v . '</error>'; } $xml .= '</return>'; exit($xml); } elseif ($error_type == 'array') { exit(var_export($this->error_data)); } elseif ($error_type == 'html') { $error = $this->error_data; //$template = InitPHP::getAppPath($InitPHP_conf['error']['template']); $template = $InitPHP_conf['error']['template']; if ($template) { if (!file_exists($template)) { InitPHP::initError('error template is not exist'); } @(include $template); } else { InitPHP::initError('please set error template in initphp.conf.php'); } //扩展HTML错误输出 exit; } }
/** * 获取当前路径 带参数 */ function current_url_query($query_string = FALSE, $remove = '') { $config = InitPHP::getConfig(); $current_url = trim($config['url'], '/'); // BEGIN MODIFICATION if ($query_string === TRUE) { $gets = $_GET; if (!empty($remove)) { $newGets = array(); foreach ($gets as $key => $value) { if ($key == $remove) { continue; } $newGets[$key] = $value; } $gets = $newGets; } // Use your preferred method of fetching the query string if (!empty($gets)) { $current_url .= '?' . http_build_query($gets); } } // END MODIFICATION return $current_url; }
/** * 获取link_id 数据库链接资源符 * @param string $sql SQL语句进行分析 * @return object */ protected function get_link_id($sql = "") { $InitPHP_conf = InitPHP::getConfig(); $db_type = $InitPHP_conf['Db'][$this->dbModel]['db_type']; //如果sql语句为空,则直接返回link_id if ($sql == "") { $this->db->link_id = self::$dbArr[$this->dbModel]['link_id']; return $this->db->link_id; } if (isset($InitPHP_conf['issqlcontrol']) && $InitPHP_conf['issqlcontrol'] == 1) { $InitPHP_conf['sqlcontrolarr'][] = $sql; InitPHP::setConfig('sqlcontrolarr', $InitPHP_conf['sqlcontrolarr']); } if ($db_type == 1) { //主从 if ($this->is_insert($sql)) { $this->db->link_id = self::$dbArr[$this->dbModel]['master']['link_id']; } else { $this->db->link_id = self::$dbArr[$this->dbModel]['salver']['link_id']; } } else { $this->db->link_id = self::$dbArr[$this->dbModel]['link_id']; } return $this->db->link_id; }
/** * 获取Nosql对象 * @param string $type */ public function init($type = 'MONGO', $server = 'default') { $InitPHP_conf = InitPHP::getConfig(); //需要设置文件缓存目录 $type = strtoupper($type); $type = in_array($type, $this->nosql_type) ? $type : 'MONGO'; switch ($type) { case 'MONGO': $instance_name = 'mongo_' . $server; if (isset(nosqlInit::$instance[$instance_name])) { return nosqlInit::$instance[$instance_name]; } $mongo = $this->load_nosql('mongo.Init.php', 'mongoInit', $server); $mongo->init($InitPHP_conf['mongo'][$server]); nosqlInit::$instance[$instance_name] = $mongo; return $mongo; break; case 'REDIS': $instance_name = 'redis_' . $server; if (isset(nosqlInit::$instance[$instance_name])) { return nosqlInit::$instance[$instance_name]; } $redis = $this->load_nosql('redis.Init.php', 'redisInit', $server); $redis->init($InitPHP_conf['redis'][$server]); nosqlInit::$instance[$instance_name] = $redis; return $redis; break; } }
/** * 后置拦截器,在所有操作进行完毕之后进行拦截 */ public function postHandle() { $config = InitPHP::getConfig(); if ($config['is_xhprof']) { $xhprof_data = xhprof_disable(); // save raw data for this profiler run using default // implementation of iXHProfRuns. $xhprof_runs = new XHProfRuns_Default(); // save the run under a namespace "xhprof_foo" $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); } }
/** * 数据库语句监控器-结束点 * 使用方法:$this->getUtil('sqlcontrol')->end(); * @return string */ public function end() { $InitPHP_conf = InitPHP::getConfig(); if (isset($InitPHP_conf['sqlcontrolarr']) && is_array($InitPHP_conf['sqlcontrolarr'])) { $i = 1; echo '<div style=" border:1px #000000 dotted; width:100%; background-color:#EEEEFF">'; foreach ($InitPHP_conf['sqlcontrolarr'] as $k => $v) { echo '<div style=" height:20px; text-align:left; font-size:14px; margin-left:10px;margin-top:5px;"><span>' . $i . '. </span>' . $v . '</div>'; $i++; } echo '</div>'; } }
/** * 【私有】验证请求是否合法 * 1. 如果请求参数m,c,a都为空,则走默认的 */ private function checkRequest() { $InitPHP_conf = InitPHP::getConfig(); $controller = $_GET['c']; $action = $_GET['a']; if ($InitPHP_conf['ismodule'] == true) { $module = $_GET['m']; if ($module == "" && $controller == "" && $action == "") { $module = $_GET['m'] = $this->default_module; $controller = $_GET['c'] = $this->default_controller; $action = $_GET['a'] = $this->default_action; } //如果module不在白名单中,则直接返回404 if (!in_array($module, $this->module_list) || empty($module)) { return $this->return404(); } $module = $module . '/'; } else { if ($controller == "" && $action == "") { $controller = $_GET['c'] = $this->default_controller; $action = $_GET['a'] = $this->default_action; } $module = ''; } //controller处理,如果导入Controller文件失败,则返回404 $path = rtrim($InitPHP_conf['controller']['path'], '/') . '/'; $controllerClass = $controller . $this->controller_postfix; $controllerFilePath = $path . $module . $controllerClass . '.php'; if (!InitPHP::import($controllerFilePath)) { return $this->return404(); } $controllerObj = InitPHP::loadclass($controllerClass); //处理Action,如果方法不存在,则直接返回404 list($whiteList, $methodList) = $this->parseWhiteList($controllerObj->initphp_list); if ($action != $this->default_action) { if (!in_array($action, $whiteList)) { return $this->return404(); //如果Action不在白名单中 } else { if ($methodList[$action]) { $method = strtolower($_SERVER['REQUEST_METHOD']); if (!in_array($method, $methodList[$action])) { //检查提交的HTTP METHOD return $this->return405(); //如果请求Method不正确,则返回405 } } } } return $controllerObj; }
/** * 具体解析 * @param unknown_type $isPre */ private function parse($isPre = true) { $InitPHP_conf = InitPHP::getConfig(); $interceptor = $InitPHP_conf['interceptor']; $filePath = $interceptor['path']; //文件路径 $return = true; if (is_array($interceptor['rule']) && count($interceptor['rule']) > 0) { foreach ($interceptor['rule'] as $k => $v) { $file = ltrim($filePath, "/") . "/" . $v['file'] . $interceptor['postfix'] . '.php'; $class = $v['file'] . $interceptor['postfix']; //处理正则匹配 $regular = $v['regular']; if ($regular['m'] != "" && $regular['m'] != '*') { if (!preg_match($regular['m'], $this->m)) { continue; } } if ($regular['c'] != "" && $regular['c'] != '*') { if (!preg_match($regular['c'], $this->c)) { continue; } } if ($regular['a'] != "" && $regular['a'] != '*') { if (!preg_match($regular['a'], $this->a)) { continue; } } if (file_exists(InitPHP::getAppPath($file))) { InitPHP::import($file); $obj = InitPHP::loadclass($class); if ($isPre == true) { $ret = $obj->preHandle(); if ($ret == false) { $return = false; break; } } else { $obj->postHandle(); } } } } return $return; }
/** * 获取search对象 * @param string $type */ public function init($type = 'SPHINX', $server = 'default') { $InitPHP_conf = InitPHP::getConfig(); //需要设置文件缓存目录 $type = strtoupper($type); $type = in_array($type, $this->search_type) ? $type : 'SPHINX'; switch ($type) { case 'SPHINX': $instance_name = 'sphinx_' . $server; if (isset(searchInit::$instance[$instance_name])) { return searchInit::$instance[$instance_name]; } $search = $this->load_search('sphinx.init.php', 'sphinxInit', $server); $search->setServer($InitPHP_conf['sphinx'][$server]['server'], $InitPHP_conf['sphinx'][$server]['port']); searchInit::$instance[$instance_name] = $search; return $search; break; } }
/** * 异常模板 * @param $e */ public static function errorTpl($e) { $InitPHP_conf = InitPHP::getConfig(); $msg = $e->message; $mainErrorCode = self::getLineCode($e->getFile(), $e->getLine()); self::_recordError($msg, $e->getFile(), $e->getLine(), trim($mainErrorCode)); if (!$InitPHP_conf['is_debug'] && $e->code == 10000) { $msg = '系统繁忙,请稍后再试'; } if (self::is_ajax()) { //此处将返回的关键字与我当前的JS代码对应 $arr = array('result' => 0, 'msg' => $msg, 'data' => array('code' => $e->code)); echo json_encode($arr); } else { //如果debug关闭,则不显示debug错误信息 if (!$InitPHP_conf['is_debug']) { return InitPHP::return500(); } //网页500 header('HTTP/1.1 500 Internal Server Error'); header("status: 500 Internal Server Error"); $trace = $e->getTrace(); $runTrace = $e->getTrace(); krsort($runTrace); $traceMessageHtml = null; $k = 1; foreach ($runTrace as $v) { $traceMessageHtml .= '<tr class="bg1"><td>' . $k . '</td><td>' . $v['file'] . '</td><td>' . $v['line'] . '</td><td>' . self::getLineCode($v['file'], $v['line']) . '</td></tr>'; $k++; } unset($k); unset($trace); unset($runTrace); unset($trace); if (isset($InitPHP_conf['sqlcontrolarr']) && is_array($InitPHP_conf['sqlcontrolarr'])) { $sqlTraceHtml = ''; foreach ($InitPHP_conf['sqlcontrolarr'] as $k => $v) { $sqlTraceHtml .= '<tr class="bg1"><td>' . ($k + 1) . '</td><td>' . $v['sql'] . '</td><td>' . $v['queryTime'] . 's</td><td>' . $v['affectedRows'] . '</td></tr>'; } } echo ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html><head><title>' . $_SERVER['HTTP_HOST'] . ' - PHP Error</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="ROBOTS" content="NOINDEX,NOFOLLOW,NOARCHIVE" /> <style type="text/css"> <!-- body { background-color: white; color: black; font: 9pt/11pt verdana, arial, sans-serif;} #container { width: 90%;margin-left:auto;margin-right:auto; } #message { width: 90%; color: black; } .red {color: red;} a:link { font: 9pt/11pt verdana, arial, sans-serif; color: red; } a:visited { font: 9pt/11pt verdana, arial, sans-serif; color: #4e4e4e; } h1 { color: #FF0000; font: 18pt "Verdana"; margin-bottom: 0.5em;} .bg1{ background-color: #FFFFCC;} .bg2{ background-color: #EEEEEE;} .table {background: #AAAAAA; font: 11pt Menlo,Consolas,"Lucida Console"} .info {background: none repeat scroll 0 0 #F3F3F3;border: 0px solid #aaaaaa;border-radius: 10px 10px 10px 10px;color: #000000;font-size: 11pt;line-height: 160%;margin-bottom: 1em;padding: 1em;} .help { background: #F3F3F3;border-radius: 10px 10px 10px 10px;font: 12px verdana, arial, sans-serif;text-align: center;line-height: 160%;padding: 1em;} .mind { background: none repeat scroll 0 0 #FFFFCC; border: 1px solid #aaaaaa; color: #000000; font: arial, sans-serif; font-size: 9pt; line-height: 160%; margin-top: 1em; padding: 4px;} --> </style></head><body><div id="container"><h1>InitPHP DEBUG</h1><div class="info">(1146)' . $msg . '</div><div class="info"><p><strong>PHP Trace</strong></p><table cellpadding="5" cellspacing="1" width="100%" class="table"><tr class="bg2"><td style="width:2%">No.</td><td style="width:45%">File</td><td style="width:5%">Line</td><td style="width:48%">Code</td></tr>' . $traceMessageHtml . '</table><p><strong>SQL Query</strong></p><table cellpadding="5" cellspacing="1" width="100%" class="table"><tr class="bg2"><td style="width:2%">No.</td><td style="width:73%">SQL</td><td style="width:10%">Cost Time</td><td style="width:15%">Affected Rows</td></tr>' . $sqlTraceHtml . '</table></div> <div class="help"><a href="http://' . $_SERVER['HTTP_HOST'] . '">' . $_SERVER['HTTP_HOST'] . '</a> 已经将此出错信息详细记录, 由此给您带来的访问不便我们深感歉意.</div></div></body></html>'; exit; } }
/** * 模板-显示视图 * 1. 在Controller中需要显示模板,就必须调用该函数 * 2. 模板解析可以设置 $InitPHP_conf['isviewfilter'] 值,对变量进行过滤 * Controller中使用方法:$this->view->display(); * @return array */ public function display($template = '') { if ($template != '') { $this->set_tpl($template); } $InitPHP_conf = InitPHP::getConfig(); if (is_array($this->view)) { if ($InitPHP_conf['isviewfilter']) { $this->out_put($this->view); } foreach ($this->view as $key => $val) { ${$key} = $val; } } $this->template_arr = $this->parse_template_arr($this->template_arr); //模板设置 foreach ($this->template_arr as $file_name) { if (in_array($file_name, $this->remove_tpl_arr)) { continue; } $complie_file_name = $this->template_run($file_name); //模板编译 if (!file_exists($complie_file_name)) { InitPHP::initError($complie_file_name . ' is not exist!'); } include_once $complie_file_name; } }
/** * record error log * @param string $msg * @param string $file * @param int $line * @param string $code */ private function _recordError($msg, $file, $line, $code) { $config = InitPHP::getConfig(); $errorLogPaTh = $config['log_dir']; if (!is_dir($errorLogPath)) { mkdir($errorLogPath); } $errorLogFilePath = $errorLogPaTh . $this->_errorLogFileName(); $string .= '[' . date('Y-m-d h:i:s') . ']msg:' . $msg . ';file:' . $file . ';line:' . $line . ';code:' . $code . ''; error_log($string, 3, $errorLogFilePath, ''); }
/** * 写日志-获取文件日志名称 * @return string */ private function get_file_log_name() { $config = InitPHP::getConfig(); return $config['log_dir'] . $this->_errorLogFileName(); }
/** * 初始化 */ public function __construct() { parent::__construct(); $InitPHP_conf = InitPHP::getConfig(); $this->controller = $this->load('controller', 'c'); //导入Controller $this->view = $this->load('view', 'v'); //导入View $this->view->set_template_config($InitPHP_conf['template']); //设置模板 $this->view->assign('init_token', $this->controller->get_token()); //全局输出init_token标记 //注册全局变量,这样在Service和Dao中通过$this->common也能调用Controller中的类 $this->register_global('common', $this->controller); }
/** * 后台公用URL组装函数 * @param string $c * @param string $a */ public function getUrl($c, $a) { $config = InitPHP::getConfig(); return InitPHP::url($c . '|' . $a); //return InitPHP::url($config['url'] . ADMIN_FILE . '?c='.$c.'&a=' . $a); }
/** * 解析html方式的路由 * 1. 解析user-add.htm?uid=100 * 2. 解析成数组,array() * @param string $request */ private function parseHtmlUri($request) { $InitPHP_conf = InitPHP::getConfig(); if (!$request) { return false; } $request = trim($request, '/'); $request = str_replace('.htm', '', $request); if ($request == '') { return false; } $request = explode('-', $request); if (!is_array($request) || count($request) == 0) { return false; } if ($InitPHP_conf['ismodule'] == true) { //是否开启模型模式 if (isset($request[0])) { $_GET['m'] = $request[0]; } if (isset($request[1])) { $_GET['c'] = $request[1]; } if (isset($request[2])) { $_GET['a'] = $request[2]; } } else { if (isset($request[0])) { $_GET['c'] = $request[0]; } if (isset($request[1])) { $_GET['a'] = $request[1]; } } return $request; }
/** * 获取文件目录下所有文件 * @return object */ private function get_all_file() { $InitPHP_conf = InitPHP::getConfig(); $temp = array(); $path = InitPHP::getAppPath($InitPHP_conf['unittesting']['path']); if (is_dir($path)) { if ($dh = opendir($path)) { while (($file = readdir($dh)) !== false) { if ($file == '.' || $file == '..') { continue; } $temp[] = str_replace($InitPHP_conf['unittesting']['test_postfix'] . '.php', '', $file); } closedir($dh); } } return $temp; }
/** * 缓存工厂-获取不同缓存类型的对象句柄 * @param string $type 缓存类型 * @return obj */ private function get_cache_handle($type) { $InitPHP_conf = InitPHP::getConfig(); //需要设置文件缓存目录 $type = strtoupper($type); $type = in_array($type, $this->cache_type) ? $type : 'FILE'; switch ($type) { case 'FILE': if (isset(cacheInit::$instance['filecache'])) { return cacheInit::$instance['filecache']; } $filecache = $this->load_cache('filecache.Init.php', 'filecacheInit'); $filepath = InitPHP::getAppPath($InitPHP_conf['cache']['filepath']); $filecache->set_cache_path($filepath); cacheInit::$instance['filecache'] = $filecache; return $filecache; break; case 'MEM': if (isset(cacheInit::$instance['memcache'])) { return cacheInit::$instance['memcache']; } $mem = $this->load_cache('memcached.Init.php', 'memcachedInit'); $mem->add_server($InitPHP_conf['memcache']); //添加服务器 cacheInit::$instance['memcache'] = $mem; return $mem; break; case 'MYSQL': if (isset(cacheInit::$instance['mysqlcache'])) { return cacheInit::$instance['mysqlcache']; } $mysqlcache = $this->load_cache('mysqlcache.Init.php', 'mysqlcacheInit'); $mysqlcache->set_sql_handler($this->db_handle); cacheInit::$instance['mysqlcache'] = $mysqlcache; return $mysqlcache; break; case 'APC': if (isset(cacheInit::$instance['apc'])) { return cacheInit::$instance['apc']; } $filecache = $this->load_cache('apc.Init.php', 'apcInit'); break; case 'XCACHE': if (isset(cacheInit::$instance['xcache'])) { return cacheInit::$instance['xcache']; } $filecache = $this->load_cache('xcache.Init.php', 'xcacheInit'); break; case 'WINCACHE': if (isset(cacheInit::$instance['wincache'])) { return cacheInit::$instance['wincache']; } $filecache = $this->load_cache('wincache.Init.php', 'wincacheInit'); break; } }
public function handleError($errorCode, $msg = '', $errorFile = 'unkwon', $errorLine = 0) { $InitPHP_conf = InitPHP::getConfig(); restore_error_handler(); if ($errorCode & error_reporting()) { InitPHP::log("[error_code]:" . $errorCode . " [msg]:" . $msg, ERROR); } if ($InitPHP_conf['is_debug'] == true) { var_dump($msg); } else { return InitPHP::return500(); } }
/** * 获取a * 全局使用方法:$this->getA() * @return */ public static function getA() { $InitPHP_conf = InitPHP::getConfig(); if ($_GET['a'] == '') { return $InitPHP_conf['Controller']['default_action']; } return $_GET['a']; }
/** * 写日志-获取文件日志名称 * @return string */ private function get_file_log_name() { $config = InitPHP::getConfig(); return $config['log_dir'] . $this->default_file_name; }