123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
- namespace Qii\Request\Url;
- abstract class Base
- {
- const VERSION = '1.2';
- /**
- * URL 模式
- *
- * @var String
- */
- protected $_mode = 'Normal';
- /**
- * 链接字符
- *
- * @var String
- */
- private $_symbol = '&';
- /**
- * 文件后缀
- *
- * @var String
- */
- private $_extenstion = '.html';
- /**
- * 允许设置的URL模式
- *
- * @var Array
- */
- private $_allowMode = array('Normal', 'Middle', 'Short');
- /**
- * 是否去掉文件名称,如果rewrite不在根目录的时候将此设为true,避免生成URL错误.
- *
- * @var Bool
- */
- private $_fileNameTrim = false;
- /**
- * URL中匹配到的参数
- */
- private $params = null;
-
- /**
- * 文件扩展名
- */
- private $sysfileExtension;
- /**
- * 存储路径字段
- */
- private $pathArgs;
- public $host;
- /**
- * 初始化模式
- * @param string $mode 模式
- */
- public function __construct($mode)
- {
- $this->checkMode($mode);
- $this->_mode = $mode;
- if (in_array($mode, array('Short', 'Middle'))) {
- $this->_symbol = '/';
- }
- $this->params = $this->getParams();
- $this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
- }
- /**
- * 设置参数的值
- *
- * @param array $params 参数
- */
- public function setParams($params)
- {
- if($this->params == null) {
- $this->params = $params;
- }else{
- $this->params = array_merge($this->params, $params);
- }
- }
- /**
- * 返回所有params参数
- *
- */
- public function params()
- {
- return $this->params;
- }
- /**
- * 根据给定的参数创建URL
- * @param Array $array {controller:controllerName, action : actionName}
- * @param string $fileName
- * @param string $extenstion
- * @param string $trimExtension
- * @return string
- */
- public function bulidURI($params, $fileName = '', $extenstion = '', $trimExtension = false)
- {
- $this->checkMode($this->_mode);
- if (empty($fileName)) {
- $fileName = $_SERVER['SCRIPT_NAME'];
- }
- if (sizeof($params) == 0) {
- return '';
- }
- //2012-03-25新增 path,如果path==$fileName就去掉path,从而避免多个rewrite的时候调用此方法生成的URL错误。
- $path = $this->getPathInfo();
- //去掉文件名并保留路径 去掉加路径出现的bug
- if ($this->_fileNameTrim) {
- $fileName = rtrim(str_replace(basename($fileName), '', $fileName), "/");
- }
- $notAllowPath = array($fileName, "\\");
- if (in_array($path, $notAllowPath)) {
- $path = "";
- }
- $realPath = rtrim(str_replace('//', '/', $path . $fileName), '/');
- if ($this->_mode == 'normal') {
- return $fileName . '?' . $this->{$this->_mode}($params);
- }
- if (!empty($extenstion)) {
- return $realPath . $this->_symbol . $this->{$this->_mode}($params) . ($trimExtension ? '' : $extenstion);
- }
- return $realPath . $this->_symbol . $this->{$this->_mode}($array) . ($trimExtension ? '' : $this->_extenstion);
- }
- /**
- * 获取当前网址
- */
- public function getWebHost()
- {
- if (IS_CLI) return '';
- $prefix = 'http://';
- if ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || $_SERVER['SERVER_PORT'] == 443) $prefix = 'https://';
- return $prefix . rtrim(rtrim(str_replace('//', '/', $this->host), '/'), "\\");
- }
- /**
- * 获取当前域名
- */
- public function getDomain()
- {
- if (IS_CLI) return '';
- return rtrim(rtrim(str_replace('//', '/', $this->host), '/'), "\\");
- }
- /**
- *
- * 获取当前页面URL
- */
- public function getCurrentURL()
- {
- if (IS_CLI) return '';
- return 'http://' . $this->host . $_SERVER['REQUEST_URI'];
- }
- /**
- *
- * 获取当前页面的路径相关信息
- * @param String $path
- * @param String $index
- */
- public function getPathInfo($path = '', $index = 'dirname')
- {
- if (empty($path)) $path = $_SERVER['SCRIPT_NAME'];
- //Array ( [dirname] => /Qii [basename] => index.php [extension] => php [filename] => index )
- $pathInfo = pathinfo($path);
- $pathInfo['dirname'] = str_replace("\\", "/", $pathInfo['dirname']);
- if (!empty($index)) {
- return $pathInfo[$index];
- }
- return $pathInfo;
- }
- /**
- *
- * 获取当前路径
- * @param String $path
- */
- public function getPath($path = '')
- {
- if (empty($path)) {
- $path = $_SERVER['SCRIPT_NAME'];
- }
- return substr($path, 0, (strrpos($path, '/')));
- }
- /**
- *
- * 设置是否去掉文件名字
- * @param Bool $trim
- */
- public function setTrim($trim = false)
- {
- $this->_fileNameTrim = $trim;
- }
- /**
- * 设置文件后缀名字
- *
- * @param String $extenstion
- */
- public function setExtenstion($extenstion)
- {
- if (!empty($extenstion)) $this->_extenstion = $extenstion;
- }
- /**
- * 设置连接字符串
- *
- * @param String $symbol
- */
- public function setSymbol($symbol)
- {
- if (!empty($symbol)) $this->_symbol = $symbol;
- }
- /**
- * 设置URI模式
- *
- * @param String $mode
- */
- public function setMode($mode)
- {
- $this->checkMode($mode);
- $this->_mode = $mode;
- return $this;
- }
- /**
- * 获取本类中的私有属性
- *
- * @param String $key
- * @return Mix
- */
- public function get($key = null, $default= null)
- {
- if ($key === null) return $this->params;
- return isset($this->params[$key]) ? $this->params[$key] : $default;
- }
- /**
- * 获取POST数据
- */
- public function post($name = null, $default = null)
- {
- if ($name === null) return $_POST;
- return isset($_POST[$name]) ? $_POST[$name] : $default;
- }
- /**
- * Cli模式下数据的传输
- *
- * @param string $key
- */
- public function CliParams($key = '')
- {
- $argv = array();
- if (isset($_SERVER['argv'])) $argv = $_SERVER['argv'];
- //修正部分服务器Rewrite 后再加参数不识别的问题(直接进入命令行的模式)
- if ($argv && $_SERVER['PHP_SELF'] == $_SERVER['SCRIPT_NAME']) {
- if (count($argv) == 1) return;
- array_shift($argv);
- $path = $argv[0];
- if($query = stristr($path, '?')) {
- $path = str_replace(stristr($path, '?'), '', $path);
- }
- $args = (array)$this->parseArgs($path);
- //处理GET或POST方法 数据结构 key1=value1 key2=value2 键和值中间不能有空格
- if($query && strlen($query) > 1) {
- $queryArr = explode("&", substr($query, 1));
- foreach ($queryArr as $value) {
- list($index, $val) = explode('=', $value, 2);
- $args[$index] = $val;
- }
- }
- if ($_SERVER['argc'] > 2) {
- for ($i = 1; $i < $_SERVER['argc'] - 1; $i++) {
- list($index, $val) = explode('=', $argv[$i], 2);
- $args[$index] = $val;
- }
- }
-
- $this->pathArgs = $args;
- if ($args && $key != '') {
- return isset($args[$key]) ? $args[$key] : '';
- }
- return $args;
- }
- return array();
- }
- public function getCliPathArgs() {
- $argv = array();
- $args = array();
- if (isset($_SERVER['argv'])) $argv = $_SERVER['argv'];
- //修正部分服务器Rewrite 后再加参数不识别的问题(直接进入命令行的模式)
- if ($argv && $_SERVER['PHP_SELF'] == $_SERVER['SCRIPT_NAME']) {
- if (count($argv) == 1) return $args;
- array_shift($argv);
- $path = $argv[0];
- if(stristr($path, '?')) {
- $path = str_replace(stristr($path, '?'), '', $path);
- }
- $args = (array)$this->parseArgs($path);
- }
- return $args;
- }
- /**
- * 获取参数
- *
- * @param String $fileName
- * @param String $url
- * @param String $key
- * @return Mix
- */
- public function getParams($key = '', $url = '', $fileName = '')
- {
- if ($this->params != null) return $this->params;
- $this->checkMode($this->_mode);
- //如果是命令行模式
- if (IS_CLI) return $this->CliParams($key);
- if ($this->_mode == 'Normal') {
- if (empty($key)) return $_GET;
- return $_GET[$key];
- }
- if (!isset($_SERVER['PATH_INFO'])) $_SERVER['PATH_INFO'] = '';
- if (empty($url)) {
- //修正Rewrite以指定目录开头的bug 将$url = $_SERVER['REQUEST_URI'];替换成以下的
- $url = (($_SERVER['PATH_INFO'] != '' && $_SERVER['PATH_INFO'] != '/') ? $_SERVER['PATH_INFO'] : $_SERVER['REQUEST_URI']);
- }
- if (empty($fileName)) {
- $fileName = $_SERVER['SCRIPT_NAME'];
- }
- //取fileName后边的内容
- $url = str_replace($fileName, "", $url);
- $param = parse_url($url);
- //如果到?号的URL则取query,没有?则取path的basename($path);
- if (isset($param['path']) && substr($param['path'], 0, 1) == '?') {
- //拆分字符
- if (empty($param['query'])) {
- return '';
- }
- $query = $param['query'];
- } else {
- $query = isset($param['path']) ? $param['path'] : '';
- }
- $query = $this->comparePath($query, $fileName);
- //添加系统扩展名到返回数组中 2011-10-14 15:26
- preg_match("/(.*)\.(.*)$/", $query, $extenstion);
- //去掉文件后缀名称,修改时间2010-09-03 22:33,以便指定任意后缀名。
- if (!isset($extenstion[2])) $extenstion[2] = '';
- $extenstion[2] = str_replace('/', '\/', $extenstion[2]);
- $query = preg_replace("/\.{$extenstion[2]}$/", "", $query);
- $paramArray = explode($this->_symbol, $query);
- $this->pathArgs = $v = $this->decodeArgs($paramArray);
- //添加系统扩展名到返回数组中 2011-10-14 15:26
- $this->sysfileExtension = $extension[2] ?? '';
- if ($_GET) $v = array_merge($v, $_GET);
- if ($key != '' || is_int($key)) {
- return $v[$key];
- }
- return $v;
- }
-
- public function getPathArgs()
- {
- return $this->pathArgs;
- }
- /**
- * 对比转发文件的路径
- *
- * @param String $path
- * @param String $scriptName
- * @return String
- */
- public function comparePath($path, $f)
- {
- //去掉basename($f)再进行比较,比较的时候将字符串转换成小写;
- $basename = basename($f);
- $path = str_replace($basename, "", $path);
- $f = str_replace($basename, "", $f);
- //对比parseURL后的Path
- $pathArray = explode('/', ltrim(rtrim($path, '/'), '/'));
- $fArray = explode('/', ltrim(rtrim($f, '/'), '/'));
- $fCount = count($fArray);
- $tmpArray = array();
- $tmpArray = $pathArray;
- for ($i = 0; $i < $fCount; $i++) {
- if (strtolower($pathArray[$i]) != strtolower($fArray[$i])) {
- break;
- }
- array_shift($tmpArray);
- }
- return join("/", $tmpArray);
- }
- /**
- * 检查生成链接模式
- *
- * @param String $mode
- */
- public function checkMode($mode)
- {
- if (!in_array($mode, $this->_allowMode)) {
- throw new \Qii\Exceptions\Unsupported("链接模式错误,链接格式只能为 '<u><font color=\"green\">" . join("', '", $this->_allowMode) . "</font></u>',当前模式为 '<font color=\"red\">" . $mode . "</font>'", __LINE__);
- }
- }
- /**
- * 页面重定向
- * @param string $url 重定向的URL
- */
- public function redirect($url)
- {
- ob_clean();
- header('Location:'. $url);
- }
- public function __call($method, $args)
- {
- //防止掉用不存在的方法
- }
- }
|