1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Core
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Url rewrite helper
29: *
30: * @category Mage
31: * @package Mage_Core
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Core_Helper_Url_Rewrite extends Mage_Core_Helper_Abstract
35: {
36: /**
37: * Validation error constants
38: */
39: const VERR_MANYSLASHES = 1; // Too many slashes in a row of request path, e.g. '///foo//'
40: const VERR_ANCHOR = 2; // Anchor is not supported in request path, e.g. 'foo#bar'
41:
42: /**
43: * Core func to validate request path
44: * If something is wrong with a path it throws localized error message and error code,
45: * that can be checked to by wrapper func to alternate error message
46: *
47: * @return bool
48: */
49: protected function _validateRequestPath($requestPath)
50: {
51: if (strpos($requestPath, '//') !== false) {
52: throw new Exception($this->__('Two and more slashes together are not permitted in request path'), self::VERR_MANYSLASHES);
53: }
54: if (strpos($requestPath, '#') !== false) {
55: throw new Exception($this->__('Anchor symbol (#) is not supported in request path'), self::VERR_ANCHOR);
56: }
57: return true;
58: }
59:
60: /**
61: * Validates request path
62: * Either returns TRUE (success) or throws error (validation failed)
63: *
64: * @return bool
65: */
66: public function validateRequestPath($requestPath)
67: {
68: try {
69: $this->_validateRequestPath($requestPath);
70: } catch (Exception $e) {
71: Mage::throwException($e->getMessage());
72: }
73: return true;
74: }
75:
76: /**
77: * Validates suffix for url rewrites to inform user about errors in it
78: * Either returns TRUE (success) or throws error (validation failed)
79: *
80: * @return bool
81: */
82: public function validateSuffix($suffix)
83: {
84: try {
85: $this->_validateRequestPath($suffix); // Suffix itself must be a valid request path
86: } catch (Exception $e) {
87: // Make message saying about suffix, not request path
88: switch ($e->getCode()) {
89: case self::VERR_MANYSLASHES:
90: Mage::throwException($this->__('Two and more slashes together are not permitted in url rewrite suffix'));
91: case self::VERR_ANCHOR:
92: Mage::throwException($this->__('Anchor symbol (#) is not supported in url rewrite suffix'));
93: }
94: }
95: return true;
96: }
97: }
98: