public function run(Request $request) { $reject_request = true; if (array_key_exists('HTTP_HOST', $_SERVER)) { $host_name = \Norma\Support\Evn::getHost(); // [ need to cater for `host:port` since some "buggy" SAPI(s) have been known to return the port too, see http://goo.gl/bFrbCO $strpos = strpos($host_name, ':'); if ($strpos !== false) { $host_name = substr($host_name, $strpos); } // ] $enable_domain_whitelist = \Norma\Config::get('enable_domain_whitelist'); //如果启用白名单机制 if ($enable_domain_whitelist) { $domain_whitelist = \Norma\Config::get('domain_whitelist'); //允许的域名 if (!empty($domain_whitelist)) { // [ for dynamic verification, replace this chunk with db/file/curl queries $reject_request = false === array_search($host_name, explode(',', $domain_whitelist), true); // ] } else { $reject_request = false; } } else { $reject_request = false; } } if ($reject_request) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request'); exit('非法的请求域'); } }
/** * Exception Handler * @param \Exception|\Throwable $e */ public static function appException($e) { if (!$e instanceof \Exception) { $e = new ThrowableError($e); } self::getExceptionHandler()->report($e); if (\Norma\Support\Evn::isCli()) { //self::getExceptionHandler()->renderForConsole(new ConsoleOutput, $e); } else { self::getExceptionHandler()->render($e)->output(); } }
/** * 获取错误信息 * ErrorException则使用错误级别作为错误编码 * @param \Exception $exception * @return string 错误信息 */ protected function getMessage(Exception $exception) { $message = $exception->getMessage(); if (\Norma\Support\Evn::isCli()) { return $message; } // 导入语言包 if (Config::get('lang_switch')) { Lang::load(THINK_PATH . '/lang' . DS . Lang::detect() . EXT); } if (strpos($message, ':')) { $name = strstr($message, ':', true); return $message; } else { return $message; } }
/** * 当前的请求类型 * @access public * @param bool $method true 获取原始请求类型 * @return string */ public function method($method = false) { if (true === $method) { // 获取原始请求类型 return \Norma\Support\Evn::isCli() ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']); } elseif (!$this->method) { if (isset($_POST[Config::get('var_method')])) { $this->method = strtoupper($_POST[Config::get('var_method')]); } elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { $this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']); } else { $this->method = \Norma\Support\Evn::isCli() ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']); } } return $this->method; }
version_compare(PHP_VERSION, $min_version = "5.6") === -1 and exit('当前PHP运行版本[' . PHP_VERSION . "]低于[" . $min_version . "]!"); // 拒绝标志 define('IS_NORMA', true); // 开始运行时间 define('START_TIME', microtime(true)); // 内存使用 define('START_MEM', memory_get_usage()); // 框架路径 defined('FRAME_PATH') or define('FRAME_PATH', __DIR__ . DIRECTORY_SEPARATOR); // 应用目录 defined('APP_PATH') or define('APP_PATH', dirname(__DIR__) . '/manage'); // 应用前缀 defined('APP_UUID') or define('APP_UUID', substr(md5(APP_PATH), 5, 6)); require FRAME_PATH . '/bootstrap/autoload.php'; // 编译文件库 if (file_exists($compiledPath = APP_PATH . '/cache/compiled.php')) { require $compiledPath; } //如果不处于单元测试 if (strpos($_SERVER['PHP_SELF'], 'phpunit') === false) { switch (strtoupper(\Norma\Support\Evn::DetectAccessMode())) { case 'CLI': \Norma\Task::Using($argc, $argv)->Running(); break; case 'WEB': default: \Norma\App::listen(new \Norma\Request()); //->send(); break; } }
// +---------------------------------------------------------------------- // | Author: LunnLew <*****@*****.**> // +---------------------------------------------------------------------- // 加载函数库 require FRAME_PATH . '/functions.php'; // 注册框架类加载器 require FRAME_PATH . '/Support/Traits/LoaderHelper.php'; require __DIR__ . '/Loader.php'; // 注册类加载器 $loader = new \Norma\Loader(); $loader->register(); $loader->addNamespace('Norma', FRAME_PATH); \Norma\App::$loader = $loader; // 注册错误和异常处理机制 \Norma\Error::register(); // 加载Composer库 defined('COMPOSER_VENDOR_PATH') and (require_once COMPOSER_VENDOR_PATH . '/autoload.php'); // 加载默认全局配置文件 \Norma\Config::load(FRAME_PATH . '/Config/Global-default.php'); // 加载应用配置文件 \Norma\Config::load(APP_PATH . '/Config/Global.php'); // 平台兼容支持 \Norma\Constant::LoadDefineWith([\Norma\Support\Evn::DetectOS(), function () { \Norma\Support\Evn::DetectEngine(); if (!empty(\Norma\Support\Evn::$runEngineEx)) { \Norma\Constant::LoadDefineWith(\Norma\Support\Evn::$runEngineEx, FRAME_PATH . '/Compatibility'); } return \Norma\Support\Evn::$runEngine; }, \Norma\Support\Evn::DetectAccessMode()], FRAME_PATH . '/Compatibility'); // 加载插件 \Norma\Hook::loadPlugin(FRAME_PATH . '/Plugin');
public function deafultAppInitialize() { \Norma\Support\Evn::isWeb() && \Norma\Hook::loadPlugin(APP_LIB_PATH . '/Plugin', ''); }