예제 #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';
             }
         }
     }
 }
예제 #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;
                 }
             }
         }
     }
 }
예제 #3
0
 /**
  * 获取path_info
  *
  * @return string
  */
 private static function _get_pathinfo()
 {
     # 处理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['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);
     }
     if (!isset($_SERVER["PATH_INFO"])) {
         $_SERVER["PATH_INFO"] = $pathinfo;
     }
     $pathinfo = trim($pathinfo);
     return $pathinfo;
 }