Request.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. <?php
  2. namespace Qii\Base;
  3. abstract class Request
  4. {
  5. const SCHEME_HTTP = 'http';
  6. const SCHEME_HTTPS = 'https';
  7. public $host = '';
  8. public $module;
  9. public $controller;
  10. public $action;
  11. public $method;
  12. public $dispatcher;
  13. protected $params;
  14. protected $language;
  15. protected $_exception;
  16. protected $_baseUri = '';
  17. protected $uri = '';
  18. protected $dispatched = false;
  19. protected $routed = false;
  20. protected $forward = false;
  21. /**
  22. * @var Qii_Request_Url url
  23. */
  24. public $url;
  25. /**
  26. * 初始化参数,获取链接中对应的参数并存放到$this->params中
  27. *
  28. */
  29. public function __construct()
  30. {
  31. foreach ($_GET AS $key => $val) {
  32. $this->setParam($key, $val);
  33. }
  34. $rewriteRule = \Qii::getInstance()->appConfigure(\Qii\Config\Consts::APP_SITE_METHOD);
  35. $this->url = new \Qii\Request\Url($rewriteRule);
  36. $this->host = IS_CLI ? '' : $_SERVER['HTTP_HOST'];
  37. $params = (array)$this->url->getParams();
  38. if(count($params) > 0) $this->params = array_merge($this->params, $params);
  39. //处理url中的数据
  40. if(ucwords($rewriteRule) == 'Short'){
  41. $this->setControllerName(
  42. isset($this->params[0]) && $this->params[0] != '' ?
  43. $this->params[0] :
  44. $this->defaultController());
  45. $this->setActionName(
  46. isset($this->params[1]) && $this->params[1] != '' ?
  47. $this->params[1] :
  48. $this->defaultAction());
  49. }
  50. return $this;
  51. }
  52. /**
  53. * 获取POST数据
  54. */
  55. public function post($name = null, $default = null)
  56. {
  57. return call_user_func_array(array($this->url, 'post'), func_get_args());
  58. }
  59. /**
  60. * 默认controller
  61. */
  62. public function defaultController()
  63. {
  64. return \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER, 'index');
  65. }
  66. /**
  67. * 默认action
  68. */
  69. public function defaultAction()
  70. {
  71. return \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_ACTION, 'index');
  72. }
  73. /**
  74. * 获取当前数据处理
  75. */
  76. public function uri()
  77. {
  78. return $this->uri;
  79. }
  80. public function redirect($url)
  81. {
  82. ob_clean();
  83. header('Location:'. $url);
  84. }
  85. /**
  86. * isGet
  87. *
  88. * @param void
  89. * @return boolean
  90. */
  91. public function isGet()
  92. {
  93. return (strtoupper($this->method) == 'GET');
  94. }
  95. /**
  96. * isPost
  97. *
  98. * @param void
  99. * @return boolean
  100. */
  101. public function isPost()
  102. {
  103. return (strtoupper($this->method) == 'POST');
  104. }
  105. /**
  106. * isPut
  107. *
  108. * @param void
  109. * @return boolean
  110. */
  111. public function isPut()
  112. {
  113. return (strtoupper($this->method) == 'PUT');
  114. }
  115. /**
  116. * isHead
  117. *
  118. * @param void
  119. * @return boolean
  120. */
  121. public function isHead()
  122. {
  123. return (strtoupper($this->method) == 'HEAD');
  124. }
  125. /**
  126. * isOptions
  127. *
  128. * @param void
  129. * @return boolean
  130. */
  131. public function isOptions()
  132. {
  133. return (strtoupper($this->method) == 'OPTIONS');
  134. }
  135. /**
  136. * isCli
  137. *
  138. * @param void
  139. * @return boolean
  140. */
  141. public function isCli()
  142. {
  143. return (strtoupper($this->method) == 'CLI');
  144. }
  145. /**
  146. * isXmlHttpRequest
  147. *
  148. * @param void
  149. * @return boolean
  150. */
  151. public function isXmlHttpRequest()
  152. {
  153. return false;
  154. }
  155. /**
  156. * getServer
  157. *
  158. * @param string $name
  159. * @param mixed $default
  160. * @return mixed
  161. */
  162. public function getServer($name = null, $default = null)
  163. {
  164. if (is_null($name)) {
  165. return $_SERVER;
  166. } elseif (isset($_SERVER[$name])) {
  167. return $_SERVER[$name];
  168. }
  169. return $default;
  170. }
  171. /**
  172. * getEnv
  173. *
  174. * @param string $name
  175. * @param mixed $default
  176. * @return mixed
  177. */
  178. public function getEnv($name = null, $default = null)
  179. {
  180. if (is_null($name)) {
  181. return $_ENV;
  182. } elseif (isset($_ENV[$name])) {
  183. return $_ENV[$name];
  184. }
  185. return $default;
  186. }
  187. /**
  188. * setParam
  189. *
  190. * @param mixed $name
  191. * @param mixed $value
  192. * @return boolean | Qii_Request_Abstract
  193. */
  194. public function setParam($name, $value = null)
  195. {
  196. if (is_null($value)) {
  197. if (is_array($name)) {
  198. $this->params = array_merge($this->params, $name);
  199. return $this;
  200. }
  201. } elseif (is_string($name)) {
  202. $this->params[$name] = $value;
  203. return $this;
  204. }
  205. return false;
  206. }
  207. /**
  208. * getParam
  209. *
  210. * @param string $name
  211. * @param mixed $default
  212. * @return mixed
  213. */
  214. public function getParam($name, $dafault = null)
  215. {
  216. if (isset($this->params[$name])) {
  217. return $this->params[$name];
  218. }
  219. return $dafault;
  220. }
  221. /**
  222. * setParams
  223. *
  224. * @param array
  225. * @return boolean | Qii_Request_Abstract
  226. */
  227. public function setParams($params)
  228. {
  229. if (is_array($params)) {
  230. $this->params = $params;
  231. return $this;
  232. }
  233. return false;
  234. }
  235. /**
  236. * getParams
  237. *
  238. * @param void
  239. * @return array
  240. */
  241. public function getParams()
  242. {
  243. return $this->params;
  244. }
  245. /**
  246. * setException
  247. *
  248. * @param Exception $exception
  249. * @return boolean | Qii_Request_Abstract
  250. */
  251. public function setException($exception)
  252. {
  253. if (is_object($exception)
  254. && ($exception instanceof Exception)
  255. ) {
  256. $this->_exception = $exception;
  257. return $this;
  258. }
  259. return false;
  260. }
  261. /**
  262. * getException
  263. *
  264. * @param void
  265. * @return Exception
  266. */
  267. public function getException()
  268. {
  269. if (is_object($this->_exception)
  270. && ($this->_exception instanceof Exception)
  271. ) {
  272. return $this->_exception;
  273. }
  274. return null;
  275. }
  276. /**
  277. * getModuleName
  278. *
  279. * @param void
  280. * @return string
  281. */
  282. public function getModuleName()
  283. {
  284. return $this->module;
  285. }
  286. /**
  287. * getControllerName
  288. *
  289. * @param void
  290. * @return string
  291. */
  292. public function getControllerName()
  293. {
  294. return $this->controller;
  295. }
  296. /**
  297. * getActionName
  298. *
  299. * @param void
  300. * @return string
  301. */
  302. public function getActionName()
  303. {
  304. return $this->action;
  305. }
  306. /**
  307. * setModuleName
  308. *
  309. * @param string $name
  310. * @return boolean | Qii_Request_Abstract
  311. */
  312. public function setModuleName($name)
  313. {
  314. if (!is_string($name)) {
  315. trigger_error('Expect a string module name', E_USER_WARNING);
  316. return false;
  317. }
  318. $this->module = $name;
  319. return $this;
  320. }
  321. /**
  322. * setControllerName
  323. *
  324. * @param string $name
  325. * @return boolean | Qii_Request_Abstract
  326. */
  327. public function setControllerName($name)
  328. {
  329. if (!is_string($name)) {
  330. trigger_error('Expect a string controller name', E_USER_WARNING);
  331. return $this;
  332. }
  333. $this->controller = $name;
  334. return $this;
  335. }
  336. /**
  337. * setActionName
  338. *
  339. * @param string $name
  340. * @return boolean | Qii_Request_Abstract
  341. */
  342. public function setActionName($name)
  343. {
  344. if (!is_string($name)) {
  345. trigger_error('Expect a string action name', E_USER_WARNING);
  346. return false;
  347. }
  348. $this->action = $name;
  349. return $this;
  350. }
  351. /**
  352. * getMethod
  353. *
  354. * @param void
  355. * @return string
  356. */
  357. public function getMethod()
  358. {
  359. return $this->method;
  360. }
  361. /**
  362. * getLanguage
  363. *
  364. * @param void
  365. * @return string
  366. */
  367. public function getLanguage()
  368. {
  369. return $this->language;
  370. }
  371. /**
  372. * setBaseUri
  373. *
  374. * @param string $baseUri
  375. * @return boolean | Qii_Request_Abstract
  376. */
  377. public function setBaseUri($baseUri)
  378. {
  379. if ($baseUri && is_string($baseUri)) {
  380. $this->_baseUri = $baseUri;
  381. return $this;
  382. }
  383. return false;
  384. }
  385. /**
  386. * getBaseUri
  387. *
  388. * @param void
  389. * @return string
  390. */
  391. public function getBaseUri()
  392. {
  393. return $this->_baseUri;
  394. }
  395. /**
  396. * setRequestUri
  397. *
  398. * @param string $uri
  399. * @return boolean | Qii_Request_Abstract
  400. */
  401. public function setRequestUri($uri)
  402. {
  403. if (is_string($uri)) {
  404. $this->uri = $uri;
  405. return $this;
  406. }
  407. return false;
  408. }
  409. /**
  410. * getRequestUri
  411. *
  412. * @param void
  413. * @return string
  414. */
  415. public function getRequestUri()
  416. {
  417. return $this->uri;
  418. }
  419. /**
  420. * isDispatched
  421. *
  422. * @param void
  423. * @return boolean
  424. */
  425. public function isDispatched()
  426. {
  427. return (boolean)$this->dispatched;
  428. }
  429. /**
  430. * setDispatched
  431. *
  432. * @param boolean $flag
  433. * @return boolean | Qii_Request_Abstract
  434. */
  435. public function setDispatched($flag = true)
  436. {
  437. if (is_bool($flag)) {
  438. $this->dispatched = $flag;
  439. return $this;
  440. }
  441. return false;
  442. }
  443. /**
  444. * 设置dispatcher
  445. *
  446. * @param Qii_Controller_Dispatcher $dispatcher
  447. */
  448. public function setDispatcher(\Qii\Base\Dispatcher $dispatcher)
  449. {
  450. $this->dispatcher = $dispatcher;
  451. }
  452. /**
  453. * isRouted
  454. *
  455. * @param void
  456. * @return boolean
  457. */
  458. public function isRouted()
  459. {
  460. return $this->routed;
  461. }
  462. /**
  463. * setRouted
  464. *
  465. * @param boolean $flag
  466. * @return boolean | Qii_Request_Abstract
  467. */
  468. public function setRouted($flag = true)
  469. {
  470. if (is_bool($flag)) {
  471. $this->routed = $flag;
  472. return $this;
  473. }
  474. return false;
  475. }
  476. /**
  477. * 是否需要转发
  478. */
  479. public function isForward()
  480. {
  481. return $this->forward;
  482. }
  483. /**
  484. * 设置是否需要转发
  485. * @param bool $flag
  486. * @return $this
  487. */
  488. public function setForward($flag = true)
  489. {
  490. if (is_bool($flag)) {
  491. $this->forward = $flag;
  492. return $this;
  493. }
  494. return $this;
  495. }
  496. /**
  497. * __setbaseUri
  498. *
  499. * @param string $baseUri
  500. * @param string $request_uri
  501. * @return boolean
  502. */
  503. protected function _setbaseUri($baseUri, $request_uri = null)
  504. {
  505. if ($baseUri && is_string($baseUri)) {
  506. $this->_baseUri = $baseUri;
  507. return true;
  508. } elseif ($request_uri && is_string($request_uri)) {
  509. $scriptFileName = $this->getServer('SCRIPT_FILENAME');
  510. do {
  511. if ($scriptFileName && is_string($scriptFileName)) {
  512. $fileName = basename($scriptFileName, \Qii::getInstance()->appConfigure('ext', '.php'));
  513. $fileNameLen = strlen($fileName);
  514. $script_name = $this->getServer('SCRIPT_NAME');
  515. if ($script_name && is_string($script_name)) {
  516. $script = basename($script_name);
  517. if (strncmp($fileName, $script, $fileNameLen) == 0) {
  518. $basename = $script_name;
  519. break;
  520. }
  521. }
  522. $phpself_name = $this->getServer('PHP_SELF');
  523. if ($phpself_name && is_string($phpself_name)) {
  524. $phpself = basename($phpself_name);
  525. if (strncmp($fileName, $phpself, $fileNameLen) == 0) {
  526. $basename = $phpself_name;
  527. break;
  528. }
  529. }
  530. $orig_name = $this->getServer('ORIG_SCRIPT_NAME');
  531. if ($orig_name && is_string($orig_name)) {
  532. $orig = basename($orig_name);
  533. if (strncmp($fileName, $orig, $fileNameLen) == 0) {
  534. $basename = $orig_name;
  535. break;
  536. }
  537. }
  538. }
  539. } while (0);
  540. if ($basename && strstr($request_uri, $basename) == $request_uri) {
  541. $this->_baseUri = rtrim($basename, '/');
  542. return true;
  543. } elseif ($basename) {
  544. $dirname = rtrim(dirname($basename), '/');
  545. if ($dirname) {
  546. if (strstr($request_uri, $dirname) == $request_uri) {
  547. $this->_baseUri = $dirname;
  548. return true;
  549. }
  550. }
  551. }
  552. $this->_baseUri = '';
  553. return true;
  554. }
  555. return false;
  556. }
  557. /**
  558. * 对于不存在的方法默认调用url中的方法
  559. */
  560. public function __call($method, $args)
  561. {
  562. return call_user_func_array(array($this->url, $method), $args);
  563. }
  564. }