1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Core_Controller_Request_Http extends Zend_Controller_Request_Http
36: {
37: const XML_NODE_DIRECT_FRONT_NAMES = 'global/request/direct_front_name';
38: const DEFAULT_HTTP_PORT = 80;
39: const DEFAULT_HTTPS_PORT = 443;
40:
41: 42: 43: 44:
45: protected $_originalPathInfo= '';
46: protected $_storeCode = null;
47: protected $_requestString = '';
48:
49: 50: 51: 52: 53:
54: protected $_rewritedPathInfo= null;
55: protected $_requestedRouteName = null;
56: protected $_routingInfo = array();
57:
58: protected $_route;
59:
60: protected $_directFrontNames = null;
61: protected $_controllerModule = null;
62:
63: 64: 65: 66: 67: 68:
69: protected $_isStraight = false;
70:
71: 72: 73: 74: 75:
76: protected $_beforeForwardInfo = array();
77:
78: 79: 80: 81: 82: 83: 84:
85: public function getOriginalPathInfo()
86: {
87: if (empty($this->_originalPathInfo)) {
88: $this->setPathInfo();
89: }
90: return $this->_originalPathInfo;
91: }
92:
93: public function getStoreCodeFromPath()
94: {
95: if (!$this->_storeCode) {
96:
97: if ($this->_canBeStoreCodeInUrl()) {
98: $p = explode('/', trim($this->getPathInfo(), '/'));
99: $storeCode = $p[0];
100:
101: $stores = Mage::app()->getStores(true, true);
102:
103: if ($storeCode !== '' && isset($stores[$storeCode])) {
104: array_shift($p);
105: $this->setPathInfo(implode('/', $p));
106: $this->_storeCode = $storeCode;
107: Mage::app()->setCurrentStore($storeCode);
108: }
109: else {
110: $this->_storeCode = Mage::app()->getStore()->getCode();
111: }
112: } else {
113: $this->_storeCode = Mage::app()->getStore()->getCode();
114: }
115:
116: }
117: return $this->_storeCode;
118: }
119:
120: 121: 122: 123: 124: 125: 126:
127: public function setPathInfo($pathInfo = null)
128: {
129: if ($pathInfo === null) {
130: $requestUri = $this->getRequestUri();
131: if (null === $requestUri) {
132: return $this;
133: }
134:
135:
136: $pos = strpos($requestUri, '?');
137: if ($pos) {
138: $requestUri = substr($requestUri, 0, $pos);
139: }
140:
141: $baseUrl = $this->getBaseUrl();
142: $pathInfo = substr($requestUri, strlen($baseUrl));
143:
144: if ((null !== $baseUrl) && (false === $pathInfo)) {
145: $pathInfo = '';
146: } elseif (null === $baseUrl) {
147: $pathInfo = $requestUri;
148: }
149:
150: if ($this->_canBeStoreCodeInUrl()) {
151: $pathParts = explode('/', ltrim($pathInfo, '/'), 2);
152: $storeCode = $pathParts[0];
153:
154: if (!$this->isDirectAccessFrontendName($storeCode)) {
155: $stores = Mage::app()->getStores(true, true);
156: if ($storeCode!=='' && isset($stores[$storeCode])) {
157: Mage::app()->setCurrentStore($storeCode);
158: $pathInfo = '/'.(isset($pathParts[1]) ? $pathParts[1] : '');
159: }
160: elseif ($storeCode !== '') {
161: $this->setActionName('noRoute');
162: }
163: }
164: }
165:
166: $this->_originalPathInfo = (string) $pathInfo;
167:
168: $this->_requestString = $pathInfo . ($pos!==false ? substr($requestUri, $pos) : '');
169: }
170:
171: $this->_pathInfo = (string) $pathInfo;
172: return $this;
173: }
174:
175: 176: 177: 178: 179: 180: 181:
182: public function rewritePathInfo($pathInfo)
183: {
184: if (($pathInfo != $this->getPathInfo()) && ($this->_rewritedPathInfo === null)) {
185: $this->_rewritedPathInfo = explode('/', trim($this->getPathInfo(), '/'));
186: }
187: $this->setPathInfo($pathInfo);
188: return $this;
189: }
190:
191: 192: 193: 194: 195:
196: protected function _canBeStoreCodeInUrl()
197: {
198: return Mage::isInstalled() && Mage::getStoreConfigFlag(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL);
199: }
200:
201: 202: 203: 204: 205: 206: 207:
208: public function isDirectAccessFrontendName($code)
209: {
210: $names = $this->getDirectFrontNames();
211: return isset($names[$code]);
212: }
213:
214: 215: 216: 217: 218:
219: public function getDirectFrontNames()
220: {
221: if (is_null($this->_directFrontNames)) {
222: $names = Mage::getConfig()->getNode(self::XML_NODE_DIRECT_FRONT_NAMES);
223: if ($names) {
224: $this->_directFrontNames = $names->asArray();
225: } else {
226: return array();
227: }
228: }
229: return $this->_directFrontNames;
230: }
231:
232: public function getOriginalRequest()
233: {
234: $request = new Zend_Controller_Request_Http();
235: $request->setPathInfo($this->getOriginalPathInfo());
236: return $request;
237: }
238:
239: public function getRequestString()
240: {
241: return $this->_requestString;
242: }
243:
244: public function getBasePath()
245: {
246: $path = parent::getBasePath();
247: if (empty($path)) {
248: $path = '/';
249: } else {
250: $path = str_replace('\\', '/', $path);
251: }
252: return $path;
253: }
254:
255: public function getBaseUrl()
256: {
257: $url = parent::getBaseUrl();
258: $url = str_replace('\\', '/', $url);
259: return $url;
260: }
261:
262: public function setRouteName($route)
263: {
264: $this->_route = $route;
265: $router = Mage::app()->getFrontController()->getRouterByRoute($route);
266: if (!$router) return $this;
267: $module = $router->getFrontNameByRoute($route);
268: if ($module) {
269: $this->setModuleName($module);
270: }
271: return $this;
272: }
273:
274: public function getRouteName()
275: {
276: return $this->_route;
277: }
278:
279: 280: 281: 282: 283: 284:
285: public function getHttpHost($trimPort = true)
286: {
287: if (!isset($_SERVER['HTTP_HOST'])) {
288: return false;
289: }
290: if ($trimPort) {
291: $host = explode(':', $_SERVER['HTTP_HOST']);
292: return $host[0];
293: }
294: return $_SERVER['HTTP_HOST'];
295: }
296:
297: 298: 299: 300: 301: 302: 303: 304:
305: public function setPost($key, $value = null)
306: {
307: if (is_array($key)) {
308: $_POST = $key;
309: }
310: else {
311: $_POST[$key] = $value;
312: }
313: return $this;
314: }
315:
316: 317: 318: 319: 320: 321:
322: public function setControllerModule($module)
323: {
324: $this->_controllerModule = $module;
325: return $this;
326: }
327:
328: 329: 330: 331: 332:
333: public function getControllerModule()
334: {
335: return $this->_controllerModule;
336: }
337:
338: 339: 340: 341: 342:
343: public function getModuleName()
344: {
345: return $this->_module;
346: }
347: 348: 349: 350: 351:
352: public function getControllerName()
353: {
354: return $this->_controller;
355: }
356: 357: 358: 359: 360:
361: public function getActionName()
362: {
363: return $this->_action;
364: }
365:
366: 367: 368: 369: 370: 371: 372: 373:
374: public function getAlias($name)
375: {
376: $aliases = $this->getAliases();
377: if (isset($aliases[$name])) {
378: return $aliases[$name];
379: }
380: return null;
381: }
382:
383: 384: 385: 386: 387:
388: public function getAliases()
389: {
390: if (isset($this->_routingInfo['aliases'])) {
391: return $this->_routingInfo['aliases'];
392: }
393: return parent::getAliases();
394: }
395:
396: 397: 398: 399: 400:
401: public function getRequestedRouteName()
402: {
403: if (isset($this->_routingInfo['requested_route'])) {
404: return $this->_routingInfo['requested_route'];
405: }
406: if ($this->_requestedRouteName === null) {
407: if ($this->_rewritedPathInfo !== null && isset($this->_rewritedPathInfo[0])) {
408: $fronName = $this->_rewritedPathInfo[0];
409: $router = Mage::app()->getFrontController()->getRouterByFrontName($fronName);
410: $this->_requestedRouteName = $router->getRouteByFrontName($fronName);
411: } else {
412:
413: return $this->getRouteName();
414: }
415: }
416: return $this->_requestedRouteName;
417: }
418:
419: 420: 421: 422: 423:
424: public function getRequestedControllerName()
425: {
426: if (isset($this->_routingInfo['requested_controller'])) {
427: return $this->_routingInfo['requested_controller'];
428: }
429: if (($this->_rewritedPathInfo !== null) && isset($this->_rewritedPathInfo[1])) {
430: return $this->_rewritedPathInfo[1];
431: }
432: return $this->getControllerName();
433: }
434:
435: 436: 437: 438: 439:
440: public function getRequestedActionName()
441: {
442: if (isset($this->_routingInfo['requested_action'])) {
443: return $this->_routingInfo['requested_action'];
444: }
445: if (($this->_rewritedPathInfo !== null) && isset($this->_rewritedPathInfo[2])) {
446: return $this->_rewritedPathInfo[2];
447: }
448: return $this->getActionName();
449: }
450:
451: 452: 453: 454: 455: 456:
457: public function setRoutingInfo($data)
458: {
459: if (is_array($data)) {
460: $this->_routingInfo = $data;
461: }
462: return $this;
463: }
464:
465: 466: 467: 468: 469: 470:
471: public function initForward()
472: {
473: if (empty($this->_beforeForwardInfo)) {
474: $this->_beforeForwardInfo = array(
475: 'params' => $this->getParams(),
476: 'action_name' => $this->getActionName(),
477: 'controller_name' => $this->getControllerName(),
478: 'module_name' => $this->getModuleName()
479: );
480: }
481:
482: return $this;
483: }
484:
485: 486: 487: 488: 489: 490: 491: 492:
493: public function getBeforeForwardInfo($name = null)
494: {
495: if (is_null($name)) {
496: return $this->_beforeForwardInfo;
497: } elseif (isset($this->_beforeForwardInfo[$name])) {
498: return $this->_beforeForwardInfo[$name];
499: }
500:
501: return null;
502: }
503:
504: 505: 506: 507: 508: 509:
510: public function isStraight($flag = null)
511: {
512: if ($flag !== null) {
513: $this->_isStraight = $flag;
514: }
515: return $this->_isStraight;
516: }
517:
518: 519: 520: 521: 522:
523: public function isAjax()
524: {
525: if ($this->isXmlHttpRequest()) {
526: return true;
527: }
528: if ($this->getParam('ajax') || $this->getParam('isAjax')) {
529: return true;
530: }
531: return false;
532: }
533: }
534: