Base.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. namespace Qii\Request\Url;
  3. abstract class Base
  4. {
  5. const VERSION = '1.2';
  6. /**
  7. * URL 模式
  8. *
  9. * @var String
  10. */
  11. protected $_mode = 'Normal';
  12. /**
  13. * 链接字符
  14. *
  15. * @var String
  16. */
  17. private $_symbol = '&';
  18. /**
  19. * 文件后缀
  20. *
  21. * @var String
  22. */
  23. private $_extenstion = '.html';
  24. /**
  25. * 允许设置的URL模式
  26. *
  27. * @var Array
  28. */
  29. private $_allowMode = array('Normal', 'Middle', 'Short');
  30. /**
  31. * 是否去掉文件名称,如果rewrite不在根目录的时候将此设为true,避免生成URL错误.
  32. *
  33. * @var Bool
  34. */
  35. private $_fileNameTrim = false;
  36. /**
  37. * URL中匹配到的参数
  38. */
  39. private $params = null;
  40. /**
  41. * 文件扩展名
  42. */
  43. private $sysfileExtension;
  44. /**
  45. * 存储路径字段
  46. */
  47. private $pathArgs;
  48. public $host;
  49. /**
  50. * 初始化模式
  51. * @param string $mode 模式
  52. */
  53. public function __construct($mode)
  54. {
  55. $this->checkMode($mode);
  56. $this->_mode = $mode;
  57. if (in_array($mode, array('Short', 'Middle'))) {
  58. $this->_symbol = '/';
  59. }
  60. $this->params = $this->getParams();
  61. $this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
  62. }
  63. /**
  64. * 设置参数的值
  65. *
  66. * @param array $params 参数
  67. */
  68. public function setParams($params)
  69. {
  70. if($this->params == null) {
  71. $this->params = $params;
  72. }else{
  73. $this->params = array_merge($this->params, $params);
  74. }
  75. }
  76. /**
  77. * 返回所有params参数
  78. *
  79. */
  80. public function params()
  81. {
  82. return $this->params;
  83. }
  84. /**
  85. * 根据给定的参数创建URL
  86. * @param Array $array {controller:controllerName, action : actionName}
  87. * @param string $fileName
  88. * @param string $extenstion
  89. * @param string $trimExtension
  90. * @return string
  91. */
  92. public function bulidURI($params, $fileName = '', $extenstion = '', $trimExtension = false)
  93. {
  94. $this->checkMode($this->_mode);
  95. if (empty($fileName)) {
  96. $fileName = $_SERVER['SCRIPT_NAME'];
  97. }
  98. if (sizeof($params) == 0) {
  99. return '';
  100. }
  101. //2012-03-25新增 path,如果path==$fileName就去掉path,从而避免多个rewrite的时候调用此方法生成的URL错误。
  102. $path = $this->getPathInfo();
  103. //去掉文件名并保留路径 去掉加路径出现的bug
  104. if ($this->_fileNameTrim) {
  105. $fileName = rtrim(str_replace(basename($fileName), '', $fileName), "/");
  106. }
  107. $notAllowPath = array($fileName, "\\");
  108. if (in_array($path, $notAllowPath)) {
  109. $path = "";
  110. }
  111. $realPath = rtrim(str_replace('//', '/', $path . $fileName), '/');
  112. if ($this->_mode == 'normal') {
  113. return $fileName . '?' . $this->{$this->_mode}($params);
  114. }
  115. if (!empty($extenstion)) {
  116. return $realPath . $this->_symbol . $this->{$this->_mode}($params) . ($trimExtension ? '' : $extenstion);
  117. }
  118. return $realPath . $this->_symbol . $this->{$this->_mode}($array) . ($trimExtension ? '' : $this->_extenstion);
  119. }
  120. /**
  121. * 获取当前网址
  122. */
  123. public function getWebHost()
  124. {
  125. if (IS_CLI) return '';
  126. $prefix = 'http://';
  127. if ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || $_SERVER['SERVER_PORT'] == 443) $prefix = 'https://';
  128. return $prefix . rtrim(rtrim(str_replace('//', '/', $this->host), '/'), "\\");
  129. }
  130. /**
  131. * 获取当前域名
  132. */
  133. public function getDomain()
  134. {
  135. if (IS_CLI) return '';
  136. return rtrim(rtrim(str_replace('//', '/', $this->host), '/'), "\\");
  137. }
  138. /**
  139. *
  140. * 获取当前页面URL
  141. */
  142. public function getCurrentURL()
  143. {
  144. if (IS_CLI) return '';
  145. return 'http://' . $this->host . $_SERVER['REQUEST_URI'];
  146. }
  147. /**
  148. *
  149. * 获取当前页面的路径相关信息
  150. * @param String $path
  151. * @param String $index
  152. */
  153. public function getPathInfo($path = '', $index = 'dirname')
  154. {
  155. if (empty($path)) $path = $_SERVER['SCRIPT_NAME'];
  156. //Array ( [dirname] => /Qii [basename] => index.php [extension] => php [filename] => index )
  157. $pathInfo = pathinfo($path);
  158. $pathInfo['dirname'] = str_replace("\\", "/", $pathInfo['dirname']);
  159. if (!empty($index)) {
  160. return $pathInfo[$index];
  161. }
  162. return $pathInfo;
  163. }
  164. /**
  165. *
  166. * 获取当前路径
  167. * @param String $path
  168. */
  169. public function getPath($path = '')
  170. {
  171. if (empty($path)) {
  172. $path = $_SERVER['SCRIPT_NAME'];
  173. }
  174. return substr($path, 0, (strrpos($path, '/')));
  175. }
  176. /**
  177. *
  178. * 设置是否去掉文件名字
  179. * @param Bool $trim
  180. */
  181. public function setTrim($trim = false)
  182. {
  183. $this->_fileNameTrim = $trim;
  184. }
  185. /**
  186. * 设置文件后缀名字
  187. *
  188. * @param String $extenstion
  189. */
  190. public function setExtenstion($extenstion)
  191. {
  192. if (!empty($extenstion)) $this->_extenstion = $extenstion;
  193. }
  194. /**
  195. * 设置连接字符串
  196. *
  197. * @param String $symbol
  198. */
  199. public function setSymbol($symbol)
  200. {
  201. if (!empty($symbol)) $this->_symbol = $symbol;
  202. }
  203. /**
  204. * 设置URI模式
  205. *
  206. * @param String $mode
  207. */
  208. public function setMode($mode)
  209. {
  210. $this->checkMode($mode);
  211. $this->_mode = $mode;
  212. return $this;
  213. }
  214. /**
  215. * 获取本类中的私有属性
  216. *
  217. * @param String $key
  218. * @return Mix
  219. */
  220. public function get($key = null, $default= null)
  221. {
  222. if ($key === null) return $this->params;
  223. return isset($this->params[$key]) ? $this->params[$key] : $default;
  224. }
  225. /**
  226. * 获取POST数据
  227. */
  228. public function post($name = null, $default = null)
  229. {
  230. if ($name === null) return $_POST;
  231. return isset($_POST[$name]) ? $_POST[$name] : $default;
  232. }
  233. /**
  234. * Cli模式下数据的传输
  235. *
  236. * @param string $key
  237. */
  238. public function CliParams($key = '')
  239. {
  240. $argv = array();
  241. if (isset($_SERVER['argv'])) $argv = $_SERVER['argv'];
  242. //修正部分服务器Rewrite 后再加参数不识别的问题(直接进入命令行的模式)
  243. if ($argv && $_SERVER['PHP_SELF'] == $_SERVER['SCRIPT_NAME']) {
  244. if (count($argv) == 1) return;
  245. array_shift($argv);
  246. $path = $argv[0];
  247. if($query = stristr($path, '?')) {
  248. $path = str_replace(stristr($path, '?'), '', $path);
  249. }
  250. $args = (array)$this->parseArgs($path);
  251. //处理GET或POST方法 数据结构 key1=value1 key2=value2 键和值中间不能有空格
  252. if($query && strlen($query) > 1) {
  253. $queryArr = explode("&", substr($query, 1));
  254. foreach ($queryArr as $value) {
  255. list($index, $val) = explode('=', $value, 2);
  256. $args[$index] = $val;
  257. }
  258. }
  259. if ($_SERVER['argc'] > 2) {
  260. for ($i = 1; $i < $_SERVER['argc'] - 1; $i++) {
  261. list($index, $val) = explode('=', $argv[$i], 2);
  262. $args[$index] = $val;
  263. }
  264. }
  265. $this->pathArgs = $args;
  266. if ($args && $key != '') {
  267. return isset($args[$key]) ? $args[$key] : '';
  268. }
  269. return $args;
  270. }
  271. return array();
  272. }
  273. public function getCliPathArgs() {
  274. $argv = array();
  275. $args = array();
  276. if (isset($_SERVER['argv'])) $argv = $_SERVER['argv'];
  277. //修正部分服务器Rewrite 后再加参数不识别的问题(直接进入命令行的模式)
  278. if ($argv && $_SERVER['PHP_SELF'] == $_SERVER['SCRIPT_NAME']) {
  279. if (count($argv) == 1) return $args;
  280. array_shift($argv);
  281. $path = $argv[0];
  282. if(stristr($path, '?')) {
  283. $path = str_replace(stristr($path, '?'), '', $path);
  284. }
  285. $args = (array)$this->parseArgs($path);
  286. }
  287. return $args;
  288. }
  289. /**
  290. * 获取参数
  291. *
  292. * @param String $fileName
  293. * @param String $url
  294. * @param String $key
  295. * @return Mix
  296. */
  297. public function getParams($key = '', $url = '', $fileName = '')
  298. {
  299. if ($this->params != null) return $this->params;
  300. $this->checkMode($this->_mode);
  301. //如果是命令行模式
  302. if (IS_CLI) return $this->CliParams($key);
  303. if ($this->_mode == 'Normal') {
  304. if (empty($key)) return $_GET;
  305. return $_GET[$key];
  306. }
  307. if (!isset($_SERVER['PATH_INFO'])) $_SERVER['PATH_INFO'] = '';
  308. if (empty($url)) {
  309. //修正Rewrite以指定目录开头的bug 将$url = $_SERVER['REQUEST_URI'];替换成以下的
  310. $url = (($_SERVER['PATH_INFO'] != '' && $_SERVER['PATH_INFO'] != '/') ? $_SERVER['PATH_INFO'] : $_SERVER['REQUEST_URI']);
  311. }
  312. if (empty($fileName)) {
  313. $fileName = $_SERVER['SCRIPT_NAME'];
  314. }
  315. //取fileName后边的内容
  316. $url = str_replace($fileName, "", $url);
  317. $param = parse_url($url);
  318. //如果到?号的URL则取query,没有?则取path的basename($path);
  319. if (isset($param['path']) && substr($param['path'], 0, 1) == '?') {
  320. //拆分字符
  321. if (empty($param['query'])) {
  322. return '';
  323. }
  324. $query = $param['query'];
  325. } else {
  326. $query = isset($param['path']) ? $param['path'] : '';
  327. }
  328. $query = $this->comparePath($query, $fileName);
  329. //添加系统扩展名到返回数组中 2011-10-14 15:26
  330. preg_match("/(.*)\.(.*)$/", $query, $extenstion);
  331. //去掉文件后缀名称,修改时间2010-09-03 22:33,以便指定任意后缀名。
  332. if (!isset($extenstion[2])) $extenstion[2] = '';
  333. $extenstion[2] = str_replace('/', '\/', $extenstion[2]);
  334. $query = preg_replace("/\.{$extenstion[2]}$/", "", $query);
  335. $paramArray = explode($this->_symbol, $query);
  336. $this->pathArgs = $v = $this->decodeArgs($paramArray);
  337. //添加系统扩展名到返回数组中 2011-10-14 15:26
  338. $this->sysfileExtension = $extension[2] ?? '';
  339. if ($_GET) $v = array_merge($v, $_GET);
  340. if ($key != '' || is_int($key)) {
  341. return $v[$key];
  342. }
  343. return $v;
  344. }
  345. public function getPathArgs()
  346. {
  347. return $this->pathArgs;
  348. }
  349. /**
  350. * 对比转发文件的路径
  351. *
  352. * @param String $path
  353. * @param String $scriptName
  354. * @return String
  355. */
  356. public function comparePath($path, $f)
  357. {
  358. //去掉basename($f)再进行比较,比较的时候将字符串转换成小写;
  359. $basename = basename($f);
  360. $path = str_replace($basename, "", $path);
  361. $f = str_replace($basename, "", $f);
  362. //对比parseURL后的Path
  363. $pathArray = explode('/', ltrim(rtrim($path, '/'), '/'));
  364. $fArray = explode('/', ltrim(rtrim($f, '/'), '/'));
  365. $fCount = count($fArray);
  366. $tmpArray = array();
  367. $tmpArray = $pathArray;
  368. for ($i = 0; $i < $fCount; $i++) {
  369. if (strtolower($pathArray[$i]) != strtolower($fArray[$i])) {
  370. break;
  371. }
  372. array_shift($tmpArray);
  373. }
  374. return join("/", $tmpArray);
  375. }
  376. /**
  377. * 检查生成链接模式
  378. *
  379. * @param String $mode
  380. */
  381. public function checkMode($mode)
  382. {
  383. if (!in_array($mode, $this->_allowMode)) {
  384. throw new \Qii\Exceptions\Unsupported("链接模式错误,链接格式只能为 '<u><font color=\"green\">" . join("', '", $this->_allowMode) . "</font></u>',当前模式为 '<font color=\"red\">" . $mode . "</font>'", __LINE__);
  385. }
  386. }
  387. /**
  388. * 页面重定向
  389. * @param string $url 重定向的URL
  390. */
  391. public function redirect($url)
  392. {
  393. ob_clean();
  394. header('Location:'. $url);
  395. }
  396. public function __call($method, $args)
  397. {
  398. //防止掉用不存在的方法
  399. }
  400. }