PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/concreteOLD/libraries/request.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 409 lines | 264 code | 46 blank | 99 comment | 68 complexity | 8bf12905c992b7e8583e217f9031d721 MD5 | raw file
  1. <?php defined('C5_EXECUTE') or die("Access Denied.");
  2. /**
  3. * @package Core
  4. * @category Concrete
  5. * @author Andrew Embler <andrew@concrete5.org>
  6. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  7. * @license http://www.concrete5.org/license/ MIT License
  8. *
  9. */
  10. /**
  11. * An object that represents a particular request to the Concrete-powered website. The request object then determines what is being requested, based on the path, and presents itself to the rest of the dispatcher (which loads the page, etc...)
  12. * @package Core
  13. * @author Andrew Embler <andrew@concrete5.org>
  14. * @category Concrete
  15. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  16. * @license http://www.concrete5.org/license/ MIT License
  17. *
  18. */
  19. class Request {
  20. private $currentPage;
  21. private $requestPath;
  22. private $task;
  23. private $params;
  24. private $includeType;
  25. private $btHandle;
  26. private $filename;
  27. private $cID;
  28. private $cPath;
  29. private $pkgHandle;
  30. private $auxData;
  31. // parses the current request and returns an
  32. // object with tasks, tools, etc... defined in them
  33. // for use in the dispatcher
  34. // Thanks to Code Igniter for some of this code (in terms of getenv(), etc...)
  35. private static function parsePathFromRequest($var) {
  36. $path = (isset($_SERVER[$var])) ? $_SERVER[$var] : @getenv($var);
  37. if (!$path) {
  38. return false;
  39. }
  40. // Allow for special handling
  41. // for each path var type.
  42. switch ( $var ) {
  43. case 'PATH_INFO':
  44. // DIR_REL not in path; do nothing.
  45. break;
  46. case 'REQUEST_URI':
  47. $path = str_replace($_SERVER['QUERY_STRING'], '', $path);
  48. $path = trim($path, '?');
  49. default:
  50. // if the path starts off with dir_rel, we remove it:
  51. if (DIR_REL != '') {
  52. $dr = trim(DIR_REL, '/');
  53. $path = trim($path, '/');
  54. if (strpos($path, $dr) === 0) {
  55. $path = substr($path, strlen($dr));
  56. }
  57. }
  58. break;
  59. }
  60. $path = trim($path, '/');
  61. if (stripos($path, DISPATCHER_FILENAME) === 0) {
  62. $path = substr($path, strlen(DISPATCHER_FILENAME));
  63. }
  64. $path = trim($path, '/');
  65. if (defined('ENABLE_CMS_FOR_PATH') && ENABLE_CMS_FOR_PATH != '') {
  66. $path = ENABLE_CMS_FOR_PATH . '/' . $path;
  67. }
  68. return $path;
  69. }
  70. public function __construct($path) {
  71. $this->requestPath = $path;
  72. $this->parse();
  73. }
  74. /**
  75. * Gets a request object for the current request. Parses PATH_INFO as necessary.
  76. * @return Request
  77. */
  78. public static function get() {
  79. static $req;
  80. if (!isset($req) || C5_ENVIRONMENT_ONLY) {
  81. $path = false;
  82. if (defined('SERVER_PATH_VARIABLE')) {
  83. $path = Request::parsePathFromRequest(SERVER_PATH_VARIABLE);
  84. }
  85. if (!$path) {
  86. $path = Request::parsePathFromRequest('PATH_INFO');
  87. }
  88. if (!$path) {
  89. $path = Request::parsePathFromRequest('REDIRECT_URL');
  90. }
  91. if (!$path) {
  92. $path = Request::parsePathFromRequest('REQUEST_URI');
  93. }
  94. if (!$path) {
  95. $path = Request::parsePathFromRequest('ORIG_PATH_INFO');
  96. }
  97. if (!$path) {
  98. $path = Request::parsePathFromRequest('SCRIPT_NAME');
  99. }
  100. $req = new Request($path);
  101. }
  102. return $req;
  103. }
  104. /**
  105. * our new MVC way of doing things. Parses the collection path using like to find
  106. * where the path stops and the parameters start. Enables us to use urls without a
  107. * task/param separator in them
  108. */
  109. public function getRequestedPage() {
  110. $path = $this->getRequestCollectionPath();
  111. $origPath = $path;
  112. $r = Cache::get('request_path_page', $path);
  113. if ($r == false) {
  114. $r = array();
  115. $db = Loader::db();
  116. $cID = false;
  117. while ((!$cID) && $path) {
  118. $cID = $db->GetOne('select cID from PagePaths where cPath = ?', $path);
  119. if ($cID) {
  120. $cPath = $path;
  121. break;
  122. }
  123. $path = substr($path, 0, strrpos($path, '/'));
  124. }
  125. /*
  126. // Get the longest path (viz most specific match) that is contained
  127. // within the request path
  128. $db = Loader::db();
  129. $r = $db->Execute("select cID,cPath from PagePaths where ? LIKE CONCAT(replace(cPath, '_','\_'),'%') ORDER BY LENGTH(cPath) DESC LIMIT 0,1", array($this->getRequestCollectionPath()));
  130. $r = $r->FetchRow();
  131. */
  132. if ($cID && $cPath) {
  133. $r['cID'] = $cID;
  134. $r['cPath'] = $cPath;
  135. Cache::set('request_path_page', $origPath, $r);
  136. }
  137. }
  138. if (is_array($r)) {
  139. $req = Request::get();
  140. $cPath = $r['cPath'];
  141. $cID = $r['cID'];
  142. $req->setCollectionPath($cPath);
  143. $c = Page::getByID($cID, false);
  144. } else {
  145. $c = new Page();
  146. $c->loadError(COLLECTION_NOT_FOUND);
  147. }
  148. return $c;
  149. }
  150. private function parse() {
  151. $path = $this->requestPath;
  152. if (isset($_REQUEST['cID']) && intval($_REQUEST['cID']) > 0) {
  153. $this->cID = $_REQUEST['cID'];
  154. } else {
  155. $this->cID = HOME_CID;
  156. }
  157. // home page w/param and task
  158. if (defined('ENABLE_LEGACY_CONTROLLER_URLS') && ENABLE_LEGACY_CONTROLLER_URLS == true) {
  159. if (preg_match("/^\-\/(.[^\/]*)\/(.*)/i", $path, $matches)) {
  160. $this->task = $matches[1];
  161. $this->params = $matches[2];
  162. return;
  163. }
  164. // home page w/just task
  165. if (preg_match("/^\-\/(.[^\/]*)/i", $path, $matches)) {
  166. $this->task = $matches[1];
  167. return;
  168. }
  169. // path + task + params
  170. if (preg_match("/^(.*)\/\-\/(.[^\/]*)\/(.*)/i", $path, $matches)) {
  171. $this->cPath = $matches[1];
  172. $this->task = $matches[2];
  173. $this->params = $matches[3];
  174. return;
  175. }
  176. // path + task
  177. if (preg_match("/^(.*)\/\-\/(.[^\/]*)/i", $path, $matches)) {
  178. $this->cPath = $matches[1];
  179. $this->task = $matches[2];
  180. return;
  181. }
  182. }
  183. // tools
  184. $exploded = explode('/', $path);
  185. if($exploded[0] == 'tools') {
  186. if($exploded[1] == 'blocks') {
  187. $this->btHandle = $exploded[2];
  188. unset($exploded[0]);
  189. unset($exploded[1]);
  190. unset($exploded[2]);
  191. $imploded = implode('/', $exploded);
  192. if(substr($imploded, -4) == '.php') {
  193. $this->filename = $imploded;
  194. } else {
  195. $this->filename = $imploded . '.php';
  196. }
  197. $this->includeType = 'BLOCK_TOOL';
  198. return;
  199. }
  200. if($exploded[1] == 'css' && $exploded[2] == 'themes') {
  201. unset($exploded[0]);
  202. unset($exploded[1]);
  203. unset($exploded[2]);
  204. $this->filename = 'css.php';
  205. $this->auxData = new stdClass;
  206. $this->auxData->theme = $exploded[3];
  207. unset($exploded[3]);
  208. $imploded = implode('/', $exploded);
  209. if(substr($imploded, -4) == '.css') {
  210. $this->auxData->file = $imploded;
  211. } else {
  212. $this->auxData->file = $imploded . '.css';
  213. }
  214. $this->includeType = 'CONCRETE_TOOL';
  215. return;
  216. }
  217. if($exploded[1] == 'packages') {
  218. $this->pkgHandle = $exploded[2];
  219. unset($exploded[0]);
  220. unset($exploded[1]);
  221. unset($exploded[2]);
  222. $imploded = implode('/', $exploded);
  223. if(substr($imploded, -4) == '.php') {
  224. $this->filename = $imploded;
  225. } else {
  226. $this->filename = $imploded . '.php';
  227. }
  228. $this->includeType = 'PACKAGE_TOOL';
  229. return;
  230. }
  231. if($exploded[1] == 'required') {
  232. unset($exploded[0]);
  233. unset($exploded[1]);
  234. $imploded = implode('/', $exploded);
  235. if(substr($imploded, -4) == '.php') {
  236. $this->filename = $imploded;
  237. } else {
  238. $this->filename = $imploded . '.php';
  239. }
  240. $this->includeType = 'CONCRETE_TOOL';
  241. return;
  242. }
  243. unset($exploded[0]);
  244. $imploded = implode('/', $exploded);
  245. if(substr($imploded, -4) == '.php') {
  246. $this->filename = $imploded;
  247. } else {
  248. $this->filename = $imploded . '.php';
  249. }
  250. $this->includeType = 'TOOL';
  251. return;
  252. }
  253. // just path
  254. if ($path != '') {
  255. $this->cPath = $path;
  256. return;
  257. }
  258. }
  259. /**
  260. * Gets the path of the current request
  261. */
  262. public function getRequestPath() {
  263. return $this->requestPath;
  264. }
  265. /**
  266. * Gets the current collection path as contained in the current request
  267. */
  268. public function getRequestCollectionPath() {
  269. // I think the regexps take care of the trimming for us but just to be sure..
  270. $cPath = trim($this->cPath, '/');
  271. if ($cPath != '') {
  272. return '/' . $cPath;
  273. }
  274. return '';
  275. }
  276. /**
  277. * Gets page ID of the current request
  278. * @return int
  279. */
  280. public function getRequestCollectionID() {
  281. return $this->cID;
  282. }
  283. /**
  284. * Gets the current MVC task of the request
  285. * @return string
  286. */
  287. public function getRequestTask() {
  288. return $this->task;
  289. }
  290. /**
  291. * Gets the array of parameters for this current MVC task
  292. */
  293. public function getRequestTaskParameters() {
  294. return $this->params;
  295. }
  296. /**
  297. * Returns whether this request wants to include a file (typically a tool)
  298. * @return bool
  299. */
  300. public function isIncludeRequest() {
  301. return $this->includeType != null;
  302. }
  303. /**
  304. * Gets the include type of the current request
  305. */
  306. public function getIncludeType() {
  307. return $this->includeType;
  308. }
  309. /**
  310. * If the current request wants to include a file, this returns the filename it wants to include
  311. */
  312. public function getFilename() {
  313. return $this->filename;
  314. }
  315. /**
  316. * Gets the block requested by the current request
  317. */
  318. public function getBlock() {
  319. return $this->btHandle;
  320. }
  321. /**
  322. * Auxiliary data is anything that the request specifies that doesn't really fit inside the request object, but gets passed along anyway
  323. */
  324. public function getAuxiliaryData() {
  325. return $this->auxData;
  326. }
  327. /**
  328. * Gets the package requested by the current request
  329. */
  330. public function getPackageHandle() {
  331. return $this->pkgHandle;
  332. }
  333. /**
  334. * Sets the controller task, used when the Page object identifies
  335. * the actual path.
  336. * @param string $task the name of the task function
  337. */
  338. public function setRequestTask($task) {
  339. $this->task = $task;
  340. }
  341. public function setCurrentPage($page) {
  342. $this->currentPage = $page;
  343. }
  344. public function getCurrentPage() {
  345. return $this->currentPage;
  346. }
  347. /**
  348. * Sets the controller params, used when the Page object identifies
  349. * the actual path.
  350. * @param string $params List of params, separated by "/"
  351. */
  352. public function setRequestTaskParameters($params) {
  353. $this->params = $params;
  354. }
  355. /**
  356. * Sets the request path, used when the Page object identifies
  357. * the actual path.
  358. * @param string $path The path
  359. */
  360. public function setCollectionPath($path) {
  361. $this->cPath = $path;
  362. }
  363. }