1: <?php
2: /**
3: * AMR url utility class
4: *
5: * @package Plugin
6: * @subpackage ModRewrite
7: * @id $Id$:
8: * @author Murat Purc <murat@purc.de>
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * Mod Rewrite url utility class. Handles convertion of Urls from CONTENIDO core
19: * based url composition pattern to AMR (Advanced Mod Rewrite) url composition
20: * pattern and vice versa.
21: *
22: * @author Murat Purc <murat@purc.de>
23: * @package Plugin
24: * @subpackage ModRewrite
25: */
26: class ModRewriteUrlUtil extends ModRewriteBase {
27:
28: /**
29: * Self instance (singleton implementation)
30: * @var ModRewriteUrlUtil
31: */
32: private static $_instance;
33:
34: /**
35: * CONTENIDO category word separator
36: * @var string
37: */
38: private $_catWordSep = '-';
39:
40: /**
41: * AMR category word separator
42: * @var string
43: */
44: private $_mrCatWordSep;
45:
46: /**
47: * CONTENIDO category separator
48: * @var string
49: */
50: private $_catSep = '/';
51:
52: /**
53: * AMR category separator
54: * @var string
55: */
56: private $_mrCatSep;
57:
58: /**
59: * CONTENIDO article separator
60: * @var string
61: */
62: private $_artSep = '/';
63:
64: /**
65: * AMR article separator
66: * @var string
67: */
68: private $_mrArtSep;
69:
70: /**
71: * CONTENIDO article word separator
72: * @var string
73: */
74: private $_artWordSep = '-';
75:
76: /**
77: * AMR article word separator
78: * @var string
79: */
80: private $_mrArtWordSep;
81:
82: /**
83: * AMR extension used for articlenames (e. g. .html)
84: * @var string
85: */
86: private $_mrExt;
87:
88: /**
89: * Constructor, sets some AMR configuration related properties
90: */
91: private function __construct() {
92: $aCfg = parent::getConfig();
93: $this->_mrCatWordSep = $aCfg['category_word_seperator'];
94: $this->_mrCatSep = $aCfg['category_seperator'];
95: $this->_mrArtSep = $aCfg['article_seperator'];
96: $this->_mrArtWordSep = $aCfg['article_word_seperator'];
97: $this->_mrExt = $aCfg['file_extension'];
98: }
99:
100: /**
101: * Prevent cloning
102: */
103: private function __clone() {
104:
105: }
106:
107: /**
108: * Returns self instance (singleton pattern)
109: * @return ModRewriteUrlUtil
110: */
111: public static function getInstance() {
112: if (self::$_instance == NULL) {
113: self::$_instance = new ModRewriteUrlUtil();
114: }
115: return self::$_instance;
116: }
117:
118: /**
119: * Converts passed AMR url path to CONTENIDO url path.
120: *
121: * @param string $urlPath AMR url path
122: * @return string CONTENIDO url path
123: */
124: public function toContenidoUrlPath($urlPath) {
125: $newUrlPath = $this->_toUrlPath(
126: $urlPath, $this->_mrCatSep, $this->_catSep, $this->_mrCatWordSep, $this->_catWordSep, $this->_mrArtSep, $this->_artSep
127: );
128: return $newUrlPath;
129: }
130:
131: /**
132: * Converts passed CONTENIDO url path to AMR url path.
133: *
134: * @param string $urlPath CONTENIDO url path
135: * @return string AMR url path
136: */
137: public function toModRewriteUrlPath($urlPath) {
138: $newUrlPath = $this->_toUrlPath(
139: $urlPath, $this->_catSep, $this->_mrCatSep, $this->_catWordSep, $this->_mrCatWordSep, $this->_artSep, $this->_mrArtSep
140: );
141: return $newUrlPath;
142: }
143:
144: /**
145: * Converts passed url path to a another url path (CONTENIDO to AMR and vice versa).
146: *
147: * @param string $urlPath Source url path
148: * @param string $fromCatSep Source category seperator
149: * @param string $toCatSep Destination category seperator
150: * @param string $fromCatWordSep Source category word seperator
151: * @param string $toCatWordSep Destination category word seperator
152: * @param string $fromArtSep Source article seperator
153: * @param string $toArtSep Destination article seperator
154: * @return string Destination url path
155: */
156: private function _toUrlPath($urlPath, $fromCatSep, $toCatSep, $fromCatWordSep, $toCatWordSep, $fromArtSep, $toArtSep) {
157: if ((string) $urlPath == '') {
158: return $urlPath;
159: }
160:
161: if (substr($urlPath, -1) == $fromArtSep) {
162: $urlPath = substr($urlPath, 0, -1) . '{TAS}';
163: }
164:
165: // pre replace category word seperator and category seperator
166: $urlPath = str_replace($fromCatWordSep, '{CWS}', $urlPath);
167: $urlPath = str_replace($fromCatSep, '{CS}', $urlPath);
168:
169: // replace category word seperator
170: $urlPath = str_replace('{CWS}', $toCatWordSep, $urlPath);
171: $urlPath = str_replace('{CS}', $toCatSep, $urlPath);
172:
173: $urlPath = str_replace('{TAS}', $toArtSep, $urlPath);
174:
175: return $urlPath;
176: }
177:
178: /**
179: * Converts passed AMR url name to CONTENIDO url name.
180: *
181: * @param string $urlName AMR url name
182: * @return string CONTENIDO url name
183: */
184: public function toContenidoUrlName($urlName) {
185: $newUrlName = $this->_toUrlName($urlName, $this->_mrArtWordSep, $this->_artWordSep);
186: return $newUrlName;
187: }
188:
189: /**
190: * Converts passed CONTENIDO url name to AMR url name.
191: *
192: * @param string $urlName CONTENIDO url name
193: * @return string AMR url name
194: */
195: public function toModRewriteUrlName($urlName) {
196: $newUrlName = $this->_toUrlName($urlName, $this->_artWordSep, $this->_mrArtWordSep);
197: return $newUrlName;
198: }
199:
200: /**
201: * Converts passed url name to a another url name (CONTENIDO to AMR and vice versa).
202: *
203: * @param string $urlName Source url name
204: * @param string $fromArtWordSep Source article word seperator
205: * @param string $toArtWordSep Destination article word seperator
206: * @return string Destination url name
207: */
208: private function _toUrlName($urlName, $fromArtWordSep, $toArtWordSep) {
209: if ((string) $urlName == '') {
210: return $urlName;
211: }
212:
213: $urlName = str_replace($this->_mrExt, '{EXT}', $urlName);
214:
215: // replace article word seperator
216: $urlName = str_replace($fromArtWordSep, $toArtWordSep, $urlName);
217:
218: $urlName = str_replace('{EXT}', $this->_mrExt, $urlName);
219:
220: return $urlName;
221: }
222:
223: /**
224: * Converts passed AMR url to CONTENIDO url.
225: *
226: * @param string $url AMR url
227: * @return string CONTENIDO url
228: */
229: public function toContenidoUrl($url) {
230: if (strpos($url, $this->_mrExt) === false) {
231: $newUrl = $this->toContenidoUrlPath($url);
232: } else {
233: // replace category word and article word seperator
234: $path = substr($url, 0, strrpos($url, $this->_mrArtSep) + 1);
235: $name = substr($url, strrpos($url, $this->_mrArtSep) + 1);
236: $newUrl = $this->toContenidoUrlPath($path) . $this->toContenidoUrlName($name);
237: }
238: return $newUrl;
239: }
240:
241: /**
242: * Converts passed AMR url to CONTENIDO url.
243: *
244: * @param string $url AMR url
245: * @return string CONTENIDO url
246: */
247: public function toModRewriteUrl($url) {
248: if (strpos($url, $this->_mrExt) === false) {
249: $newUrl = $this->toModRewriteUrlPath($url);
250: } else {
251: // replace category word and article word seperator
252: $path = substr($url, 0, strrpos($url, $this->_artSep) + 1);
253: $name = substr($url, strrpos($url, $this->_artSep) + 1);
254: $newUrl = $this->toModRewriteUrlPath($path) . $this->toModRewriteUrlName($name);
255: }
256: return $newUrl;
257: }
258: }