/** * 解析一个静态资源的内容 * */ public static function parseResourceFile() { $pathinfo = Route::getPathInfo(); array_shift($pathinfo); $resource = implode('/', $pathinfo); if ($GLOBALS['debug'] && CML_IS_MULTI_MODULES) { $pos = strpos($resource, '/'); $file = CML_APP_MODULES_PATH . DIRECTORY_SEPARATOR . substr($resource, 0, $pos) . DIRECTORY_SEPARATOR . Config::get('modules_static_path_name') . substr($resource, $pos); if (is_file($file)) { Response::sendContentTypeBySubFix(substr($resource, strrpos($resource, '.') + 1)); exit(file_get_contents($file)); } else { Response::sendHttpStatus(404); } } }
/** * 运行对应的控制器 * * @return void */ public final function runAppController() { //检测csrf跨站攻击 Secure::checkCsrf(Config::get('check_csrf')); // 关闭GPC过滤 防止数据的正确性受到影响 在db层防注入 if (get_magic_quotes_gpc()) { Secure::stripslashes($_GET); Secure::stripslashes($_POST); Secure::stripslashes($_COOKIE); Secure::stripslashes($_REQUEST); //在程序中对get post cookie的改变不影响 request的值 } //session保存方式自定义 if (Config::get('session_user')) { Session::init(); } else { ini_get('session.auto_start') || session_start(); //自动开启session } header('Cache-control: ' . Config::get('http_cache_control')); // 页面缓存控制 //如果有子类中有init()方法 执行Init() eg:做权限控制 if (method_exists($this, "init")) { $this->init(); } //根据动作去找对应的方法 $method = Route::$urlParams['action']; if (method_exists($this, $method)) { $this->{$method}(); } elseif ($GLOBALS['debug']) { Cml::montFor404Page(); throwException(Lang::get('_ACTION_NOT_FOUND_', Route::$urlParams['action'])); } else { Cml::montFor404Page(); Response::show404Page(); } }
/** *输出分页 */ public function show() { if ($this->totalRows == 0) { return ''; } $nowCoolPage = ceil($this->nowPage / $this->barShowPage); $delimiter = Config::get('url_pathinfo_depr'); $params = array_merge($this->param, [$this->pageShowVarName => '__PAGE__']); $paramsString = ''; foreach ($params as $key => $val) { $paramsString == '' || ($paramsString .= '/'); $paramsString .= $key . '/' . $val; } if ($this->url) { $url = rtrim(Response::url($this->url . '/' . $paramsString, false), $delimiter); } else { $url = rtrim(Response::url(Cml::getContainer()->make('cml_route')->getFullPathNotContainSubDir() . '/' . $paramsString, false), $delimiter); } $upRow = $this->nowPage - 1; $downRow = $this->nowPage + 1; $upPage = $upRow > 0 ? '<li><a href = "' . str_replace('__PAGE__', $upRow, $url) . '">' . $this->config['prev'] . '</a></li>' : ''; $downPage = $downRow <= $this->totalPages ? '<li><a href="' . str_replace('__PAGE__', $downRow, $url) . '">' . $this->config['next'] . '</a></li>' : ''; // << < > >> if ($nowCoolPage == 1) { $theFirst = $prePage = ''; } else { $preRow = $this->nowPage - $this->barShowPage; $prePage = '<li><a href="' . str_replace('__PAGE__', $preRow, $url) . '">上' . $this->barShowPage . '页</a></li>'; $theFirst = '<li><a href="' . str_replace('__PAGE__', 1, $url) . '">' . $this->config['first'] . '</a></li>'; } if ($nowCoolPage == $this->coolPages) { $nextPage = $theEnd = ''; } else { $nextRow = $this->nowPage + $this->barShowPage; $theEndRow = $this->totalPages; $nextPage = '<li><a href="' . str_replace('__PAGE__', $nextRow, $url) . '">下' . $this->barShowPage . '页</a></li>'; $theEnd = '<li><a href="' . str_replace('__PAGE__', $theEndRow, $url) . '">' . $this->config['last'] . '</a></li>'; } //1 2 3 4 5 $linkPage = ''; for ($i = 1; $i <= $this->barShowPage; $i++) { $page = ($nowCoolPage - 1) * $this->barShowPage + $i; if ($page != $this->nowPage) { if ($page <= $this->totalPages) { $linkPage .= ' <li><a href="' . str_replace('__PAGE__', $page, $url) . '"> ' . $page . ' </a></li>'; } else { break; } } else { if ($this->totalPages != 1) { $linkPage .= ' <li class="active"><a>' . $page . '</a></li>'; } } } $pageStr = str_replace(['%header%', '%nowPage%', '%totalRow%', '%totalPage%', '%upPage%', '%downPage%', '%first%', '%prePage%', '%linkPage%', '%nextPage%', '%end%'], [$this->config['header'], $this->nowPage, $this->totalRows, $this->totalPages, $upPage, $downPage, $theFirst, $prePage, $linkPage, $nextPage, $theEnd], $this->config['theme']); return '<ul>' . $pageStr . '</ul>'; }
/** * 防止csrf跨站攻击 * * @param int $type 检测类型 0不检查,1、只检查post,2、post get都检查 */ public static function checkCsrf($type = 1) { if ($type !== 0 && isset($_SERVER['HTTP_REFERER']) && !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])) { if ($type == 1) { if (!empty($_POST)) { Response::sendHttpStatus(403); throw new \UnexpectedValueException(Lang::get('_ILLEGAL_REQUEST_')); } } else { Response::sendHttpStatus(403); throw new \UnexpectedValueException(Lang::get('_ILLEGAL_REQUEST_')); } } }
/** * 启动应用 * * @param callable $initDi 注入依赖 */ public static function runApp(callable $initDi) { //系统初始化 self::init($initDi); Plugin::hook('cml.before_run_controller'); $controllerAction = Cml::getContainer()->make('cml_route')->getControllerAndAction(); if ($controllerAction) { Cml::$debug && Debug::addTipInfo(Lang::get('_CML_ACTION_CONTROLLER_', $controllerAction['class'])); $controller = new $controllerAction['class'](); call_user_func([$controller, "runAppController"], $controllerAction['action']); //运行 } else { self::montFor404Page(); if (self::$debug) { throw new ControllerNotFoundException(Lang::get('_CONTROLLER_NOT_FOUND_')); } else { Response::show404Page(); } } //输出Debug模式的信息 self::cmlStop(); }
/** *输出分页 */ public function show() { if ($this->totalRows == 0) { return ''; } $p = $this->varPage; $nowCoolPage = ceil($this->nowPage / $this->rollPage); $depr = \Cml\Config::get('url_pathinfo_depr'); if ($this->url) { $url = rtrim(\Cml\Http\Response::Url($this->url . '/__PAGE__', $this->param, false), $depr); } else { $addUrl = \Cml\Config::get('APP_MODULE') ? CML_MODULE_NAME . '/' : ''; $this->param = array_merge($this->param, array($p => '__PAGE__')); $url = rtrim(\Cml\Http\Response::Url($addUrl . Route::$urlParams['controller'] . '/' . Route::$urlParams['action'], $this->param, false), $depr); } $upRow = $this->nowPage - 1; $downRow = $this->nowPage + 1; $upPage = $upRow > 0 ? '<li><a href = "' . str_replace('__PAGE__', $upRow, $url) . '">' . $this->config['prev'] . '</a></li>' : ''; $downPage = $downRow <= $this->totalPages ? '<li><a href="' . str_replace('__PAGE__', $downRow, $url) . '">' . $this->config['next'] . '</a></li>' : ''; // << < > >> if ($nowCoolPage == 1) { $theFirst = $prePage = ''; } else { $preRow = $this->nowPage - $this->rollPage; $prePage = '<li><a href="' . str_replace('__PAGE__', $preRow, $url) . '">上' . $this->rollPage . '页</a></li>'; $theFirst = '<li><a href="' . str_replace('__PAGE__', 1, $url) . '">' . $this->config['first'] . '</a></li>'; } if ($nowCoolPage == $this->coolPages) { $nextPage = $theEnd = ''; } else { $nextRow = $this->nowPage + $this->rollPage; $theEndRow = $this->totalPages; $nextPage = '<li><a href="' . str_replace('__PAGE__', $nextRow, $url) . '">下' . $this->rollPage . '页</a></li>'; $theEnd = '<li><a href="' . str_replace('__PAGE__', $theEndRow, $url) . '">' . $this->config['last'] . '</a></li>'; } //1 2 3 4 5 $linkPage = ''; for ($i = 1; $i <= $this->rollPage; $i++) { $page = ($nowCoolPage - 1) * $this->rollPage + $i; if ($page != $this->nowPage) { if ($page <= $this->totalPages) { $linkPage .= ' <li><a href="' . str_replace('__PAGE__', $page, $url) . '"> ' . $page . ' </a></li>'; } else { break; } } else { if ($this->totalPages != 1) { $linkPage .= ' <li class="active"><a>' . $page . '</a></li>'; } } } $pageStr = str_replace(array('%header%', '%nowPage%', '%totalRow%', '%totalPage%', '%upPage%', '%downPage%', '%first%', '%prePage%', '%linkPage%', '%nextPage%', '%end%'), array($this->config['header'], $this->nowPage, $this->totalRows, $this->totalPages, $upPage, $downPage, $theFirst, $prePage, $linkPage, $nextPage, $theEnd), $this->config['theme']); return '<ul>' . $pageStr . '</ul>'; }
/** * 启动框架 * */ public static function runApp() { //系统初始化 self::init(); //控制器所在路径 $actionController = CML_APP_CONTROLLER_PATH . Route::$urlParams['controller'] . 'Controller.php'; $GLOBALS['debug'] && Debug::addTipInfo(Lang::get('_CML_ACTION_CONTROLLER_', $actionController)); Plugin::hook('cml.before_run_controller'); if (is_file($actionController)) { $className = Route::$urlParams['controller'] . 'Controller'; $className = (CML_IS_MULTI_MODULES ? '' : '\\Controller') . Route::$urlParams['path'] . (CML_IS_MULTI_MODULES ? 'Controller' . DIRECTORY_SEPARATOR : '') . "{$className}"; $className = str_replace('/', '\\', $className); $controller = new $className(); call_user_func(array($controller, "runAppController")); //运行 } else { self::montFor404Page(); if ($GLOBALS['debug']) { throwException(Lang::get('_CONTROLLER_NOT_FOUND_', CML_APP_CONTROLLER_PATH, Route::$urlParams['controller'], str_replace('/', '\\', Route::$urlParams['path']) . Route::$urlParams['controller'])); } else { Response::show404Page(); } } //输出Debug模式的信息 self::cmlStop(); }
/** * 解析一个静态资源的内容 * */ public static function parseResourceFile() { if (Cml::$debug) { $pathInfo = Route::getPathInfo(); array_shift($pathInfo); $resource = implode('/', $pathInfo); $appName = $file = ''; $i = 0; $routeAppHierarchy = Config::get('route_app_hierarchy', 1); while (true) { $resource = ltrim($resource, '/'); $pos = strpos($resource, '/'); $appName = ($appName == '' ? '' : $appName . DIRECTORY_SEPARATOR) . substr($resource, 0, $pos); $resource = substr($resource, $pos); $file = Cml::getApplicationDir('apps_path') . DIRECTORY_SEPARATOR . $appName . DIRECTORY_SEPARATOR . Cml::getApplicationDir('app_static_path_name') . $resource; if (is_file($file) || ++$i >= $routeAppHierarchy) { break; } } if (is_file($file)) { Response::sendContentTypeBySubFix(substr($resource, strrpos($resource, '.') + 1)); exit(file_get_contents($file)); } else { Response::sendHttpStatus(404); } } }