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: class Mage_Core_Controller_Varien_Router_Standard extends Mage_Core_Controller_Varien_Router_Abstract
28: {
29: protected $_modules = array();
30: protected $_routes = array();
31: protected $_dispatchData = array();
32:
33: public function collectRoutes($configArea, $useRouterName)
34: {
35: $routers = array();
36: $routersConfigNode = Mage::getConfig()->getNode($configArea.'/routers');
37: if($routersConfigNode) {
38: $routers = $routersConfigNode->children();
39: }
40: foreach ($routers as $routerName=>$routerConfig) {
41: $use = (string)$routerConfig->use;
42: if ($use == $useRouterName) {
43: $modules = array((string)$routerConfig->args->module);
44: if ($routerConfig->args->modules) {
45: foreach ($routerConfig->args->modules->children() as $customModule) {
46: if ($customModule) {
47: if ($before = $customModule->getAttribute('before')) {
48: $position = array_search($before, $modules);
49: if ($position === false) {
50: $position = 0;
51: }
52: array_splice($modules, $position, 0, (string)$customModule);
53: } elseif ($after = $customModule->getAttribute('after')) {
54: $position = array_search($after, $modules);
55: if ($position === false) {
56: $position = count($modules);
57: }
58: array_splice($modules, $position+1, 0, (string)$customModule);
59: } else {
60: $modules[] = (string)$customModule;
61: }
62: }
63: }
64: }
65:
66: $frontName = (string)$routerConfig->args->frontName;
67: $this->addModule($frontName, $modules, $routerName);
68: }
69: }
70: }
71:
72: public function fetchDefault()
73: {
74: $this->getFront()->setDefault(array(
75: 'module' => 'core',
76: 'controller' => 'index',
77: 'action' => 'index'
78: ));
79: }
80:
81: 82: 83: 84: 85:
86: protected function _beforeModuleMatch()
87: {
88: if (Mage::app()->getStore()->isAdmin()) {
89: return false;
90: }
91: return true;
92: }
93:
94: 95: 96: 97: 98:
99: protected function _afterModuleMatch()
100: {
101: return true;
102: }
103:
104: 105: 106: 107: 108: 109:
110: public function match(Zend_Controller_Request_Http $request)
111: {
112:
113:
114: if (!$this->_beforeModuleMatch()) {
115: return false;
116: }
117:
118: $this->fetchDefault();
119:
120: $front = $this->getFront();
121: $path = trim($request->getPathInfo(), '/');
122:
123: if ($path) {
124: $p = explode('/', $path);
125: } else {
126: $p = explode('/', $this->_getDefaultPath());
127: }
128:
129:
130: if ($request->getModuleName()) {
131: $module = $request->getModuleName();
132: } else {
133: if (!empty($p[0])) {
134: $module = $p[0];
135: } else {
136: $module = $this->getFront()->getDefault('module');
137: $request->setAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS, '');
138: }
139: }
140: if (!$module) {
141: if (Mage::app()->getStore()->isAdmin()) {
142: $module = 'admin';
143: } else {
144: return false;
145: }
146: }
147:
148: 149: 150:
151: $modules = $this->getModuleByFrontName($module);
152:
153: if ($modules === false) {
154: return false;
155: }
156:
157:
158: if (!$this->_afterModuleMatch()) {
159: return false;
160: }
161:
162: 163: 164:
165: $found = false;
166: foreach ($modules as $realModule) {
167: $request->setRouteName($this->getRouteByFrontName($module));
168:
169:
170: if ($request->getControllerName()) {
171: $controller = $request->getControllerName();
172: } else {
173: if (!empty($p[1])) {
174: $controller = $p[1];
175: } else {
176: $controller = $front->getDefault('controller');
177: $request->setAlias(
178: Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
179: ltrim($request->getOriginalPathInfo(), '/')
180: );
181: }
182: }
183:
184:
185: if (empty($action)) {
186: if ($request->getActionName()) {
187: $action = $request->getActionName();
188: } else {
189: $action = !empty($p[2]) ? $p[2] : $front->getDefault('action');
190: }
191: }
192:
193:
194: $this->_checkShouldBeSecure($request, '/'.$module.'/'.$controller.'/'.$action);
195:
196: $controllerClassName = $this->_validateControllerClassName($realModule, $controller);
197: if (!$controllerClassName) {
198: continue;
199: }
200:
201:
202: $controllerInstance = Mage::getControllerInstance($controllerClassName, $request, $front->getResponse());
203:
204: if (!$controllerInstance->hasAction($action)) {
205: continue;
206: }
207:
208: $found = true;
209: break;
210: }
211:
212: 213: 214:
215: if (!$found) {
216: if ($this->_noRouteShouldBeApplied()) {
217: $controller = 'index';
218: $action = 'noroute';
219:
220: $controllerClassName = $this->_validateControllerClassName($realModule, $controller);
221: if (!$controllerClassName) {
222: return false;
223: }
224:
225:
226: $controllerInstance = Mage::getControllerInstance($controllerClassName, $request,
227: $front->getResponse());
228:
229: if (!$controllerInstance->hasAction($action)) {
230: return false;
231: }
232: } else {
233: return false;
234: }
235: }
236:
237:
238: $request->setModuleName($module);
239: $request->setControllerName($controller);
240: $request->setActionName($action);
241: $request->setControllerModule($realModule);
242:
243:
244: for ($i = 3, $l = sizeof($p); $i < $l; $i += 2) {
245: $request->setParam($p[$i], isset($p[$i+1]) ? urldecode($p[$i+1]) : '');
246: }
247:
248:
249: $request->setDispatched(true);
250: $controllerInstance->dispatch($action);
251:
252: return true;
253: }
254:
255: 256: 257: 258:
259: protected function _getDefaultPath()
260: {
261: return Mage::getStoreConfig('web/default/front');
262: }
263:
264: 265: 266: 267: 268:
269: protected function _noRouteShouldBeApplied()
270: {
271: return false;
272: }
273:
274: 275: 276: 277: 278: 279:
280: protected function _validateControllerClassName($realModule, $controller)
281: {
282: $controllerFileName = $this->getControllerFileName($realModule, $controller);
283: if (!$this->validateControllerFileName($controllerFileName)) {
284: return false;
285: }
286:
287: $controllerClassName = $this->getControllerClassName($realModule, $controller);
288: if (!$controllerClassName) {
289: return false;
290: }
291:
292:
293: if (!$this->_includeControllerClass($controllerFileName, $controllerClassName)) {
294: return false;
295: }
296:
297: return $controllerClassName;
298: }
299:
300:
301: 302: 303: 304:
305: protected function _inludeControllerClass($controllerFileName, $controllerClassName)
306: {
307: return $this->_includeControllerClass($controllerFileName, $controllerClassName);
308: }
309:
310: 311: 312: 313: 314: 315: 316:
317: protected function _includeControllerClass($controllerFileName, $controllerClassName)
318: {
319: if (!class_exists($controllerClassName, false)) {
320: if (!file_exists($controllerFileName)) {
321: return false;
322: }
323: include $controllerFileName;
324:
325: if (!class_exists($controllerClassName, false)) {
326: throw Mage::exception('Mage_Core', Mage::helper('core')->__('Controller file was loaded but class does not exist'));
327: }
328: }
329: return true;
330: }
331:
332: public function addModule($frontName, $moduleName, $routeName)
333: {
334: $this->_modules[$frontName] = $moduleName;
335: $this->_routes[$routeName] = $frontName;
336: return $this;
337: }
338:
339: public function getModuleByFrontName($frontName)
340: {
341: if (isset($this->_modules[$frontName])) {
342: return $this->_modules[$frontName];
343: }
344: return false;
345: }
346:
347: public function getModuleByName($moduleName, $modules)
348: {
349: foreach ($modules as $module) {
350: if ($moduleName === $module || (is_array($module)
351: && $this->getModuleByName($moduleName, $module))) {
352: return true;
353: }
354: }
355: return false;
356: }
357:
358: public function getFrontNameByRoute($routeName)
359: {
360: if (isset($this->_routes[$routeName])) {
361: return $this->_routes[$routeName];
362: }
363: return false;
364: }
365:
366: public function getRouteByFrontName($frontName)
367: {
368: return array_search($frontName, $this->_routes);
369: }
370:
371: public function getControllerFileName($realModule, $controller)
372: {
373: $parts = explode('_', $realModule);
374: $realModule = implode('_', array_splice($parts, 0, 2));
375: $file = Mage::getModuleDir('controllers', $realModule);
376: if (count($parts)) {
377: $file .= DS . implode(DS, $parts);
378: }
379: $file .= DS.uc_words($controller, DS).'Controller.php';
380: return $file;
381: }
382:
383: public function validateControllerFileName($fileName)
384: {
385: if ($fileName && is_readable($fileName) && false===strpos($fileName, '//')) {
386: return true;
387: }
388: return false;
389: }
390:
391: public function getControllerClassName($realModule, $controller)
392: {
393: $class = $realModule.'_'.uc_words($controller).'Controller';
394: return $class;
395: }
396:
397: public function rewrite(array $p)
398: {
399: $rewrite = Mage::getConfig()->getNode('global/rewrite');
400: if ($module = $rewrite->{$p[0]}) {
401: if (!$module->children()) {
402: $p[0] = trim((string)$module);
403: }
404: }
405: if (isset($p[1]) && ($controller = $rewrite->{$p[0]}->{$p[1]})) {
406: if (!$controller->children()) {
407: $p[1] = trim((string)$controller);
408: }
409: }
410: if (isset($p[2]) && ($action = $rewrite->{$p[0]}->{$p[1]}->{$p[2]})) {
411: if (!$action->children()) {
412: $p[2] = trim((string)$action);
413: }
414: }
415:
416: return $p;
417: }
418:
419: 420: 421: 422: 423: 424: 425: 426:
427: protected function _checkShouldBeSecure($request, $path = '')
428: {
429: if (!Mage::isInstalled() || $request->getPost()) {
430: return;
431: }
432:
433: if ($this->_shouldBeSecure($path) && !$request->isSecure()) {
434: $url = $this->_getCurrentSecureUrl($request);
435: if ($request->getRouteName() != 'adminhtml' && Mage::app()->getUseSessionInUrl()) {
436: $url = Mage::getSingleton('core/url')->getRedirectUrl($url);
437: }
438:
439: Mage::app()->getFrontController()->getResponse()
440: ->setRedirect($url)
441: ->sendResponse();
442: exit;
443: }
444: }
445:
446: protected function _getCurrentSecureUrl($request)
447: {
448: if ($alias = $request->getAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS)) {
449: return Mage::getBaseUrl('link', true).ltrim($alias, '/');
450: }
451:
452: return Mage::getBaseUrl('link', true).ltrim($request->getPathInfo(), '/');
453: }
454:
455: 456: 457: 458: 459: 460:
461: protected function _shouldBeSecure($path)
462: {
463: return substr(Mage::getStoreConfig('web/unsecure/base_url'), 0, 5) === 'https'
464: || Mage::getStoreConfigFlag('web/secure/use_in_frontend')
465: && substr(Mage::getStoreConfig('web/secure/base_url'), 0, 5) == 'https'
466: && Mage::getConfig()->shouldUrlBeSecure($path);
467: }
468: }
469: