Esempio n. 1
0
 /**
  * 根据URL初始化
  */
 private static function setup_by_url(&$request_mode)
 {
     # 处理base_url
     if (null === self::$base_url && isset($_SERVER["SCRIPT_NAME"]) && $_SERVER["SCRIPT_NAME"]) {
         $base_url_len = strrpos($_SERVER["SCRIPT_NAME"], '/');
         if ($base_url_len) {
             $base_url = substr($_SERVER["SCRIPT_NAME"], 0, $base_url_len);
             if (preg_match('#^(.*)/wwwroot$#', $base_url, $m)) {
                 # 特殊处理wwwroot目录
                 $base_url = $m[1];
                 $base_url_len = strlen($base_url);
             }
             if (strtolower(substr($_SERVER['REQUEST_URI'], 0, $base_url_len)) == strtolower($base_url)) {
                 self::$base_url = $base_url;
             }
         }
     }
     if (isset($_SERVER['PATH_INFO'])) {
         $pathinfo = $_SERVER["PATH_INFO"];
     } else {
         if (isset($_SERVER["PATH_TRANSLATED"])) {
             list($null, $pathinfo) = explode('index' . EXT, $_SERVER["PATH_TRANSLATED"], 2);
         } elseif (isset($_SERVER['REQUEST_URI'])) {
             $request_uri = $_SERVER['REQUEST_URI'];
             if (self::$base_url) {
                 $request_uri = substr($request_uri, strlen(self::$base_url));
             }
             // 移除查询参数
             list($pathinfo) = explode('?', $request_uri, 2);
         } elseif (isset($_SERVER['PHP_SELF'])) {
             $pathinfo = $_SERVER['PHP_SELF'];
         } elseif (isset($_SERVER['REDIRECT_URL'])) {
             $pathinfo = $_SERVER['REDIRECT_URL'];
         } else {
             $pathinfo = false;
         }
     }
     # 过滤pathinfo传入进来的服务器默认页
     if (false !== $pathinfo && ($indexpagelen = strlen(self::$config['core']['server_index_page'])) && substr($pathinfo, -1 - $indexpagelen) == '/' . self::$config['core']['server_index_page']) {
         $pathinfo = substr($pathinfo, 0, -$indexpagelen);
     }
     $pathinfo = trim($pathinfo);
     if (!isset($_SERVER["PATH_INFO"])) {
         $_SERVER["PATH_INFO"] = $pathinfo;
     }
     self::$path_info = $pathinfo;
     $get_path_info = function (&$url) {
         static $protocol = null, $protocol_len = 0;
         if (null === $protocol) {
             if (!empty($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) {
                 $protocol = 'https://';
                 $protocol_len = 8;
             } else {
                 $protocol = 'http://';
                 $protocol_len = 7;
             }
         }
         $url = strtolower($url);
         # 结尾补/
         if (substr($url, -1) != '/') {
             $url .= '/';
         }
         if (substr($url, 0, $protocol_len) == $protocol) {
             $len = strlen($url);
             if (strtolower(substr($_SERVER["SCRIPT_URI"], 0, $len)) == $url) {
                 # 匹配到项目
                 return '/' . substr($_SERVER["SCRIPT_URI"], $len);
             }
         } else {
             # 开头补/
             if (substr($url, 0, 1) != '/') {
                 $url = '/' . $url;
             }
             $len = strlen($url);
             if (strtolower(substr(Bootstrap::$path_info, 0, $len)) == $url) {
                 # 匹配到项目
                 return '/' . substr(Bootstrap::$path_info, $len);
             }
         }
         return false;
     };
     # 项目相关设置
     if (isset(self::$config['core']['projects']) && is_array(self::$config['core']['projects']) && self::$config['core']['projects']) {
         # 处理项目
         foreach (self::$config['core']['projects'] as $project => $item) {
             if (!preg_match('#^[a-z0-9_]+$#i', $project)) {
                 continue;
             }
             $admin_url = array();
             if (isset($item['admin_url']) && $item['admin_url']) {
                 if (!is_array($item['admin_url'])) {
                     $item['admin_url'] = array($item['admin_url']);
                 }
                 foreach ($item['admin_url'] as $tmp_url) {
                     if (preg_match('#^http(s)?\\://#i', $tmp_url)) {
                         if (($path_info_admin = $get_path_info($tmp_url)) != false) {
                             self::$project = $project;
                             self::$path_info = $path_info_admin;
                             self::$base_url = $tmp_url;
                             $request_mode = 'admin';
                             break 2;
                         }
                     } else {
                         # /开头的后台URL
                         $admin_url[] = $admin_url;
                     }
                 }
             }
             if ($item['url']) {
                 if (!is_array($item['url'])) {
                     $item['url'] = array($item['url']);
                 }
                 foreach ($item['url'] as $url) {
                     if (($path_info = $get_path_info($url)) != false) {
                         self::$project = $project;
                         self::$path_info = $path_info;
                         self::$base_url = $url;
                         if ($admin_url) {
                             foreach ($admin_url as $url2) {
                                 # 处理后台URL不是 http:// 或 https:// 开头的形式
                                 if (($path_info_admin = $get_path_info($url2)) != false) {
                                     self::$path_info = $path_info_admin;
                                     self::$base_url .= ltrim($url2, '/');
                                     $request_mode = 'admin';
                                     break 3;
                                 }
                             }
                         }
                         break 2;
                     }
                 }
             }
         }
     }
     if (self::$project) {
         $project_dir = DIR_PROJECT . self::$project . DS;
         if (!is_dir($project_dir)) {
             self::show_error('not found the project: :project', array(':project' => self::$project));
         }
         # 根据URL寻找到了项目
         self::$include_path = array_merge(array('\\project\\' . self::$project . '\\' => $project_dir), self::$include_path);
     } else {
         if (isset(self::$config['core']['url']['admin']) && self::$config['core']['url']['admin'] && ($path_info = $get_path_info(self::$config['core']['url']['admin'])) != false) {
             self::$path_info = $path_info;
             self::$base_url = self::$config['core']['url']['admin'];
             $request_mode = 'admin';
         } else {
             if (isset(self::$config['core']['apps_url']) && is_array(self::$config['core']['apps_url']) && self::$config['core']['apps_url']) {
                 foreach (self::$config['core']['apps_url'] as $app => $urls) {
                     if (!$urls) {
                         continue;
                     }
                     if (!preg_match('#^[a-z0-9_]+//[a-z0-9]+$#i', $app)) {
                         continue;
                     }
                     if (!is_array($urls)) {
                         $urls = array($urls);
                     }
                     foreach ($urls as $url) {
                         if (($path_info = $get_path_info($url)) != false) {
                             self::$app = $app;
                             self::$path_info = $path_info;
                             self::$base_url = $url;
                             break 2;
                         }
                     }
                 }
             }
             if (null === self::$app) {
                 # 没有相关应用
                 if (isset(self::$config['core']['url']['apps']) && self::$config['core']['url']['apps']) {
                     if (($path_info = $get_path_info(self::$config['core']['url']['apps'])) != false) {
                         # 匹配到应用默认目录
                         $path_info = trim($path_info, '/');
                         self::$app = true;
                         if ($path_info) {
                             $path_info_arr = explode('/', $path_info);
                             if (count($path_info_arr) >= 2) {
                                 $app = array_shift($path_info_arr) . '/' . array_shift($path_info_arr);
                                 if (preg_match('#^[a-z0-9_]+//[a-z0-9]+$#i', $app)) {
                                     $path_info = '/' . implode('/', $path_info_arr);
                                     self::$app = $app;
                                 }
                             }
                         }
                         self::$path_info = $path_info;
                         self::$base_url = self::$config['core']['url']['apps'];
                         $request_mode = 'app';
                     }
                 }
             }
             if (self::$app && true !== self::$app) {
                 # 已获取到APP
                 $app_dir = DIR_APPS . self::$app . DS;
                 if (!is_dir($app_dir)) {
                     self::show_error('can not found the app: :app', array(':app' => self::$app));
                 }
                 $request_mode = 'app';
             }
         }
     }
 }
Esempio n. 2
0
 /**
  * 根据URL初始化
  */
 private static function setup_by_url(&$request_mode)
 {
     # 当没有$_SERVER["SCRIPT_URL"] 时拼接起来
     if (!isset($_SERVER['SCRIPT_URL'])) {
         $tmp_uri = explode('?', $_SERVER['REQUEST_URI'], 2);
         $_SERVER['SCRIPT_URL'] = $tmp_uri[0];
     }
     # 处理BASE_URL
     if (isset(self::$core_config['root_path']) && self::$core_config['root_path']) {
         self::$base_url = rtrim(self::$core_config['root_path'], '/');
     } else {
         if (null === self::$base_url && isset($_SERVER["SCRIPT_NAME"]) && $_SERVER["SCRIPT_NAME"]) {
             $base_url_len = strrpos($_SERVER["SCRIPT_NAME"], '/');
             if (false !== $base_url_len) {
                 $base_url_len += 1;
                 # 截取到最后一个/的位置
                 $base_url = substr($_SERVER["SCRIPT_NAME"], 0, $base_url_len);
                 if (preg_match('#^(.*)/wwwroot/$#', $base_url, $m)) {
                     # 特殊处理wwwroot目录
                     $base_url = $m[1] . '/';
                     $base_url_len = strlen($base_url);
                 }
                 if (strtolower(substr($_SERVER['REQUEST_URI'], 0, $base_url_len)) == strtolower($base_url)) {
                     self::$base_url = $base_url;
                 }
             }
         }
     }
     # 当没有$_SERVER["SCRIPT_URI"] 时拼接起来
     if (!isset($_SERVER['SCRIPT_URI'])) {
         $_SERVER['SCRIPT_URI'] = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . (isset($_SERVER['SCRIPT_URL']) ? $_SERVER['SCRIPT_URL'] : $_SERVER["REQUEST_URI"]);
     }
     if (isset($_SERVER['PATH_INFO'])) {
         if (substr($_SERVER['PATH_INFO'], 0, 9) === '/wwwroot/') {
             $pathinfo = substr($_SERVER['PATH_INFO'], 8);
         } else {
             $pathinfo = $_SERVER['PATH_INFO'];
         }
     } else {
         if (isset($_SERVER['REQUEST_URI'])) {
             $request_uri = str_replace('\\', '/', $_SERVER['REQUEST_URI']);
             $root_uri = '/' . ltrim(str_replace('\\', '/', substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT']))), '/');
             $index_file = 'index' . EXT;
             if (substr($root_uri, -strlen($index_file)) == $index_file) {
                 $root_uri = substr($root_uri, 0, -strlen($index_file));
             }
             if ($root_uri && $root_uri != '/') {
                 $request_uri = substr($request_uri, strlen($root_uri));
             }
             list($pathinfo) = explode('?', $request_uri, 2);
             $pathinfo = '/' . ltrim($pathinfo, '/');
         } elseif (isset($_SERVER['PHP_SELF'])) {
             $pathinfo = $_SERVER['PHP_SELF'];
         } elseif (isset($_SERVER['REDIRECT_URL'])) {
             $pathinfo = $_SERVER['REDIRECT_URL'];
         } else {
             $pathinfo = false;
         }
     }
     # 过滤pathinfo传入进来的服务器默认页
     if (false !== $pathinfo && ($indexpagelen = strlen(self::$core_config['server_index_page'])) && substr($pathinfo, -1 - $indexpagelen) == '/' . self::$core_config['server_index_page']) {
         $pathinfo = substr($pathinfo, 0, -$indexpagelen);
     }
     $pathinfo = trim($pathinfo);
     if (!isset($_SERVER["PATH_INFO"])) {
         $_SERVER["PATH_INFO"] = $pathinfo;
     }
     self::$path_info = $pathinfo;
     # 处理项目
     foreach (self::$core_config['projects'] as $project => $item) {
         if (!preg_match('#^[a-z0-9_]+$#i', $project)) {
             continue;
         }
         $rest_url = array();
         $admin_url = array();
         if (isset($item['dir']) && $item['dir']) {
             $project_dir = $item['dir'];
         } else {
             $project_dir = $project;
         }
         if (isset($item['url_rest']) && $item['url_rest']) {
             foreach ((array) $item['url_rest'] as $tmp_url) {
                 if (preg_match('#^http(s)?\\://#i', $tmp_url)) {
                     if (($url_path_info = self::_get_pathinfo($tmp_url)) !== false) {
                         self::$project = $project;
                         self::$project_dir = $project_dir;
                         self::$path_info = $url_path_info;
                         self::$base_url = $tmp_url;
                         $request_mode = 'rest';
                         break 2;
                     }
                 } else {
                     # /开头的后台URL
                     $rest_url[] = $tmp_url;
                 }
             }
         }
         if (isset($item['url_admin']) && $item['url_admin']) {
             foreach ((array) $item['url_admin'] as $tmp_url) {
                 if (preg_match('#^http(s)?\\://#i', $tmp_url)) {
                     if (($url_path_info = self::_get_pathinfo($tmp_url)) !== false) {
                         self::$project = $project;
                         self::$project_dir = $project_dir;
                         self::$path_info = $url_path_info;
                         self::$base_url = $tmp_url;
                         $request_mode = 'admin';
                         break 2;
                     }
                 } else {
                     # /开头的后台URL
                     $admin_url[] = $tmp_url;
                 }
             }
         }
         if ($item['url']) {
             foreach ((array) $item['url'] as $url) {
                 if (($path_info = self::_get_pathinfo($url)) !== false) {
                     self::$project = $project;
                     self::$project_dir = $project_dir;
                     self::$path_info = $path_info;
                     self::$base_url = $url;
                     if ($rest_url) {
                         foreach ($rest_url as $tmp_url) {
                             # 处理后台URL不是 http:// 或 https:// 开头的形式
                             if (($url_path_info = self::_get_pathinfo($tmp_url)) !== false) {
                                 self::$path_info = $url_path_info;
                                 self::$base_url .= ltrim($tmp_url, '/');
                                 $request_mode = 'rest';
                                 break 3;
                             }
                         }
                     }
                     if ($admin_url) {
                         foreach ($admin_url as $tmp_url) {
                             # 处理后台URL不是 http:// 或 https:// 开头的形式
                             if (($url_path_info = self::_get_pathinfo($tmp_url)) !== false) {
                                 self::$path_info = $url_path_info;
                                 self::$base_url .= ltrim($tmp_url, '/');
                                 $request_mode = 'admin';
                                 break 3;
                             }
                         }
                     }
                     break 2;
                 }
             }
         }
     }
 }
Esempio n. 3
0
 /**
  * 系统启动函数
  */
 public static function setup($auto_run = true)
 {
     static $run = null;
     if (true === $run) {
         if (true === $auto_run) {
             Core::setup(true);
         }
         return true;
     }
     $run = true;
     # 读取系统配置
     if (!is_file(DIR_SYSTEM . 'config' . EXT)) {
         self::_throw_sys_error_msg(__('Please rename the file config.new:EXT to config:EXT', array(':EXT' => EXT)));
     }
     self::_include_config_file(self::$config['core'], DIR_SYSTEM . 'config' . EXT);
     # 本地调试模式
     if (isset(self::$config['core']['local_debug_cfg']) && self::$config['core']['local_debug_cfg']) {
         # 判断是否开启了本地调试
         if (function_exists('get_cfg_var')) {
             $open_debug = get_cfg_var(self::$config['core']['local_debug_cfg']) ? 1 : 0;
         } else {
             $open_debug = 0;
         }
     } else {
         $open_debug = 0;
     }
     # 读Debug配置
     if ($open_debug && is_file(DIR_SYSTEM . 'debug.config' . EXT)) {
         # 本地debug配置打开
         self::_include_config_file(self::$config['core'], DIR_SYSTEM . 'debug.config' . EXT);
     }
     # 在线调试
     if (self::is_online_debug()) {
         $open_debug = 1 << 1 | $open_debug;
     }
     /**
      * 是否开启DEBUG模式
      *
      *     if (IS_DEBUG>>1)
      *     {
      *         //开启了在线调试
      *     }
      *
      *     if (IS_DEBUG & 1)
      *     {
      *         //本地调试打开
      *     }
      *
      *     if (IS_DEBUG)
      *     {
      *         // 开启了调试
      *     }
      *
      * @var int
      */
     define('IS_DEBUG', $open_debug);
     if (!IS_CLI) {
         # 输出文件头
         header('Content-Type: text/html;charset=' . self::$config['core']['charset']);
     }
     if (!isset(self::$config['core']['projects']) || !self::$config['core']['projects']) {
         self::_throw_sys_error_msg(__('Please create a new project.'));
     }
     if (isset(self::$config['core']['base_url']) && null !== self::$config['core']['base_url']) {
         self::$base_url = self::$config['core']['base_url'];
     }
     if (isset(self::$config['core']['url']['assets'])) {
         $assets_url = self::$config['core']['url']['assets'];
     } else {
         $assets_url = '/asstes/';
     }
     define('URL_ASSETS', $assets_url);
     unset($assets_url);
     $now_project = null;
     if (IS_CLI) {
         if (isset($_SERVER['OS']) && $_SERVER['OS'] == 'Windows_NT') {
             # 切换到UTF-8编码显示状态
             exec('chcp 65001');
         }
         if (!isset($_SERVER["argv"])) {
             exit('Err Argv');
         }
         $argv = $_SERVER["argv"];
         //$argv[0]为文件名
         if (isset($argv[1])) {
             $project = $argv[1];
             if (isset(self::$config['core']['projects'][$project])) {
                 $now_project = $project;
                 $project = self::$config['core']['projects'][$project];
             } else {
                 $project = false;
             }
         } else {
             $project = false;
         }
         array_shift($argv);
         //将文件名移除
         array_shift($argv);
         //将项目名移除
         self::$path_info = trim(implode('/', $argv));
     } else {
         self::$path_info = self::_get_pathinfo();
         $project_url = false;
         foreach (self::$config['core']['projects'] as $k => &$item) {
             if (!isset($item['url'])) {
                 $item['url'] = array();
             }
             if (!is_array($item['url'])) {
                 $item['url'] = array((string) $item['url']);
             }
             $tmp_pathinfo = self::$path_info;
             foreach ($item['url'] as $index => $u) {
                 if (self::_check_is_this_url($u, self::$path_info)) {
                     $project_url = $u;
                     self::$curren_uri_index = $index;
                     break;
                 }
             }
             if (false !== $project_url) {
                 if (isset($item['url_admin']) && $item['url_admin']) {
                     if (!strpos($item['url_admin'], '://')) {
                         $tmp_pathinfo = self::$path_info;
                     }
                     if (self::_check_is_this_url($item['url_admin'], $tmp_pathinfo)) {
                         self::$path_info = $tmp_pathinfo;
                         self::$is_admin_url = true;
                     }
                 }
                 self::$project_url = $project_url;
                 $project_config = $item;
                 $now_project = $k;
                 break;
             }
         }
     }
     if (!$now_project) {
         if (IS_CLI) {
             # 命令行下执行
             echo 'use:' . CRLF;
             foreach (self::$config['core']['projects'] as $k => $item) {
                 if (isset($item['isuse']) && !$item['isuse']) {
                     continue;
                 }
                 echo "    " . $k . CRLF;
             }
             return true;
         }
         if (isset(self::$config['core']['projects']['default'])) {
             $now_project = 'default';
         } else {
             self::_throw_sys_error_msg(__('not found the project: :project', array(':project' => $now_project)));
         }
     }
     /**
      * 初始项目名
      * @var string
      */
     define('INITIAL_PROJECT_NAME', $now_project);
     self::set_project($now_project);
     # 注册自动加载类
     spl_autoload_register(array('Bootstrap', 'auto_load'));
     # 加载系统核心
     Core::setup($auto_run);
 }