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