00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Core_Controller_Request_Http extends Zend_Controller_Request_Http
00036 {
00037 const XML_NODE_DIRECT_FRONT_NAMES = 'global/request/direct_front_name';
00038
00039
00040
00041
00042
00043 protected $_originalPathInfo= '';
00044 protected $_storeCode = null;
00045 protected $_requestString = '';
00046
00047 protected $_route;
00048
00049 protected $_directFrontNames = array();
00050 protected $_controllerModule = null;
00051
00052 public function __construct($uri = null)
00053 {
00054 parent::__construct($uri);
00055 $names = Mage::getConfig()->getNode(self::XML_NODE_DIRECT_FRONT_NAMES);
00056 if ($names) {
00057 $this->_directFrontNames = $names->asArray();
00058 }
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068 public function getOriginalPathInfo()
00069 {
00070 if (empty($this->_originalPathInfo)) {
00071 $this->setPathInfo();
00072 }
00073 return $this->_originalPathInfo;
00074 }
00075
00076 public function getStoreCodeFromPath()
00077 {
00078 if (!$this->_storeCode) {
00079
00080 if ($this->_canBeStoreCodeInUrl()) {
00081 $p = explode('/', trim($this->getPathInfo(), '/'));
00082 $storeCode = $p[0];
00083
00084 $stores = Mage::app()->getStores(true, true);
00085
00086 if ($storeCode !== '' && isset($stores[$storeCode])) {
00087 array_shift($p);
00088 $this->setPathInfo(implode('/', $p));
00089 $this->_storeCode = $storeCode;
00090 Mage::app()->setCurrentStore($storeCode);
00091 }
00092 else {
00093 $this->_storeCode = Mage::app()->getStore()->getCode();
00094 }
00095 } else {
00096 $this->_storeCode = Mage::app()->getStore()->getCode();
00097 }
00098
00099 }
00100 return $this->_storeCode;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110 public function setPathInfo($pathInfo = null)
00111 {
00112 if ($pathInfo === null) {
00113 $requestUri = $this->getRequestUri();
00114 if (null === $requestUri) {
00115 return $this;
00116 }
00117
00118
00119 if ($pos = strpos($requestUri, '?')) {
00120 $requestUri = substr($requestUri, 0, $pos);
00121 }
00122
00123 $baseUrl = $this->getBaseUrl();
00124 $pathInfo = substr($requestUri, strlen($baseUrl));
00125
00126 if ((null !== $baseUrl) && (false === $pathInfo)) {
00127 $pathInfo = '';
00128 } elseif (null === $baseUrl) {
00129 $pathInfo = $requestUri;
00130 }
00131
00132 if ($this->_canBeStoreCodeInUrl()) {
00133 $pathParts = explode('/', ltrim($pathInfo, '/'), 2);
00134 $storeCode = $pathParts[0];
00135
00136 if (!$this->isDirectAccessFrontendName($storeCode)) {
00137 $stores = Mage::app()->getStores(true, true);
00138 if ($storeCode!=='' && isset($stores[$storeCode])) {
00139 Mage::app()->setCurrentStore($storeCode);
00140 $pathInfo = '/'.(isset($pathParts[1]) ? $pathParts[1] : '');
00141 }
00142 elseif ($storeCode !== '') {
00143 $this->setActionName('noRoute');
00144 }
00145 }
00146 }
00147
00148 $this->_originalPathInfo = (string) $pathInfo;
00149
00150 $this->_requestString = $pathInfo . ($pos!==false ? substr($requestUri, $pos) : '');
00151 }
00152
00153 $this->_pathInfo = (string) $pathInfo;
00154 return $this;
00155 }
00156
00157
00158
00159
00160
00161
00162 protected function _canBeStoreCodeInUrl()
00163 {
00164 return Mage::isInstalled() && Mage::getStoreConfigFlag(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL);
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174 public function isDirectAccessFrontendName($code)
00175 {
00176 $names = $this->getDirectFrontNames();
00177 return isset($names[$code]);
00178 }
00179
00180
00181
00182
00183
00184
00185 public function getDirectFrontNames()
00186 {
00187 return $this->_directFrontNames;
00188 }
00189
00190 public function getOriginalRequest()
00191 {
00192 $request = new Zend_Controller_Request_Http();
00193 $request->setPathInfo($this->getOriginalPathInfo());
00194 return $request;
00195 }
00196
00197 public function getRequestString()
00198 {
00199 return $this->_requestString;
00200 }
00201
00202 public function getBasePath()
00203 {
00204 $path = parent::getBasePath();
00205 if (empty($path)) {
00206 $path = '/';
00207 } else {
00208 $path = str_replace('\\', '/', $path);
00209 }
00210 return $path;
00211 }
00212
00213 public function getBaseUrl()
00214 {
00215 $url = parent::getBaseUrl();
00216 $url = str_replace('\\', '/', $url);
00217 return $url;
00218 }
00219
00220 public function setRouteName($route)
00221 {
00222 $this->_route = $route;
00223 $router = Mage::app()->getFrontController()->getRouterByRoute($route);
00224 if (!$router) return $this;
00225 $module = $router->getFrontNameByRoute($route);
00226 if ($module) {
00227 $this->setModuleName($module);
00228 }
00229 return $this;
00230 }
00231
00232 public function getRouteName()
00233 {
00234 return $this->_route;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243 public function getHttpHost($trimPort = true)
00244 {
00245 if (!isset($_SERVER['HTTP_HOST'])) {
00246 return false;
00247 }
00248 if ($trimPort) {
00249 $host = split(':', $_SERVER['HTTP_HOST']);
00250 return $host[0];
00251 }
00252 return $_SERVER['HTTP_HOST'];
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 public function setPost($key, $value = null)
00264 {
00265 if (is_array($key)) {
00266 $_POST = $key;
00267 }
00268 else {
00269 $_POST[$key] = $value;
00270 }
00271 return $this;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 public function setControllerModule($module)
00281 {
00282 $this->_controllerModule = $module;
00283 return $this;
00284 }
00285
00286
00287
00288
00289
00290
00291 public function getControllerModule()
00292 {
00293 return $this->_controllerModule;
00294 }
00295 }