1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36: class cUriBuilderMR extends cUriBuilder {
37:
38: 39: 40: 41: 42:
43: private static $_instance;
44:
45: 46: 47: 48: 49: 50: 51: 52:
53: private static $_cachedRootDir;
54:
55: 56: 57: 58: 59:
60: private $_sAmp = '&';
61:
62: 63: 64: 65: 66:
67: private $_bIsXHTML = false;
68:
69: 70: 71: 72: 73:
74: private $_bMREnabled = false;
75:
76: 77: 78: 79: 80:
81: private $_aMrCfg = NULL;
82:
83: 84: 85:
86: private function __construct() {
87: $this->sHttpBasePath = '';
88: if (ModRewrite::isEnabled()) {
89: $this->_aMrCfg = ModRewrite::getConfig();
90: $this->_bMREnabled = true;
91: $this->_bIsXHTML = (getEffectiveSetting('generator', 'xhtml', 'false') == 'false') ? false : true;
92: $this->_sAmp = ($this->_bIsXHTML) ? '&' : '&';
93: }
94: }
95:
96: 97: 98: 99: 100:
101: public static function getInstance() {
102: if (self::$_instance == NULL) {
103: self::$_instance = new self();
104: }
105: return self::$_instance;
106: }
107:
108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118:
119: public function buildUrl(array $params, $bUseAbsolutePath = false) {
120: ModRewriteDebugger::add($params, 'cUriBuilderMR::buildUrl() $params');
121: $urlDebug = array();
122: $urlDebug['in'] = $params;
123:
124: $url = self::_buildUrl($params);
125:
126: $urlPrefix = '';
127: if ($bUseAbsolutePath) {
128: $hmlPath = cRegistry::getFrontendUrl();
129: $aComp = parse_url($hmlPath);
130: $urlPrefix = $aComp['scheme'] . '://' . $aComp['host'];
131: if (mr_arrayValue($aComp, 'port', '') !== '') {
132: $urlPrefix .= ':' . $aComp['port'];
133: }
134: }
135:
136: $this->sUrl = $urlPrefix . $url;
137:
138: $urlDebug['out'] = $this->sUrl;
139: ModRewriteDebugger::add($urlDebug, 'cUriBuilderMR::buildUrl() in -> out');
140: }
141:
142: 143: 144: 145: 146: 147:
148: private function _buildUrl(array $aParams) {
149:
150: if (isset($aParams['changelang'])) {
151: $aParams['lang'] = $aParams['changelang'];
152: }
153:
154:
155: $sQuery = http_build_query($aParams);
156:
157:
158: $oMRUrlStack = ModRewriteUrlStack::getInstance();
159: $aPretty = $oMRUrlStack->getPrettyUrlParts('front_content.php?' . $sQuery);
160:
161:
162: $sQuery = $this->_createUrlQueryPart($aParams);
163:
164:
165: $aParts = array();
166:
167:
168: if ($param = $this->_getClientParameter($aParams)) {
169: $aParts[] = $param;
170: }
171:
172:
173: if ($param = $this->_getLanguageParameter($aParams)) {
174: $aParts[] = $param;
175: }
176:
177:
178: $sPath = $this->_getPath($aPretty);
179: if ($sPath !== '') {
180: $aParts[] = $sPath;
181: }
182: $sPath = implode('/', $aParts) . '/';
183:
184:
185: $sArticle = $this->_getArticleName($aPretty, $aParams);
186:
187: if ($sArticle !== '') {
188: $sFileExt = $this->_aMrCfg['file_extension'];
189: } else {
190: $sFileExt = '';
191: }
192:
193: $sPathAndArticle = $sPath . $sArticle . $sFileExt;
194: if ($this->_aMrCfg['use_lowercase_uri'] == 1) {
195:
196: $sPathAndArticle = strtolower($sPathAndArticle);
197: }
198:
199:
200: $sUrl = $sPathAndArticle . $sQuery;
201:
202:
203: $sUrl = mr_removeMultipleChars('/', $sUrl);
204: if (substr($sUrl, -2) == '?=') {
205: $sUrl = substr_replace($sUrl, '', -2);
206: }
207:
208:
209: $sUrl = ModRewriteUrlUtil::getInstance()->toModRewriteUrl($sUrl);
210:
211:
212:
213:
214: $sUrl = self::getMultiClientRootDir($this->_aMrCfg['rootdir']) . $sUrl;
215:
216:
217: $sUrl = mr_removeMultipleChars('/', $sUrl);
218:
219: return $sUrl;
220: }
221:
222: 223: 224: 225: 226: 227: 228: 229:
230: public static function getMultiClientRootDir($configuredRootDir) {
231:
232:
233: if (isset(self::$_cachedRootDir)) {
234: return self::$_cachedRootDir;
235: }
236:
237:
238: $props = cRegistry::getClient()->getProperties();
239:
240:
241:
242: if (!is_array($props)) {
243: self::$_cachedRootDir = $configuredRootDir;
244: return $configuredRootDir;
245: }
246:
247: foreach ($props as $prop) {
248:
249: if ($prop['type'] != 'client') {
250: continue;
251: }
252:
253:
254: if (false === strstr($prop['name'], 'frontend_path')) {
255: continue;
256: }
257:
258:
259: $httpHost = $_SERVER['HTTP_HOST'];
260: $httpPath = $_SERVER['REQUEST_URI'];
261:
262:
263: $propHost = parse_url($prop['value'], PHP_URL_HOST);
264: $propPath = parse_url($prop['value'], PHP_URL_PATH);
265:
266:
267:
268: if ($propHost != $httpHost && ('www.' . $propHost) != $httpHost && $propHost != 'www.' . $httpHost) {
269: continue;
270: }
271:
272:
273: if (0 !== strpos($httpPath, $propPath)) {
274: continue;
275: }
276:
277:
278: self::$_cachedRootDir = $propPath;
279: return $propPath;
280: }
281:
282:
283: self::$_cachedRootDir = $configuredRootDir;
284: return $configuredRootDir;
285: }
286:
287: 288: 289: 290: 291: 292: 293: 294:
295: private function _createUrlQueryPart(array $aArgs) {
296:
297:
298: $aIgnoredParams = array(
299: 'idcat',
300: 'idart',
301: 'lang',
302: 'client',
303: 'idcatart',
304: 'idartlang'
305: );
306: if ($this->_aMrCfg['use_language'] == 1) {
307: $aIgnoredParams[] = 'changelang';
308: }
309: if ($this->_aMrCfg['use_client'] == 1) {
310: $aIgnoredParams[] = 'changeclient';
311: }
312:
313:
314: $sQuery = '';
315: foreach ($aArgs as $p => $v) {
316: if (!in_array($p, $aIgnoredParams)) {
317:
318:
319: $p = urlencode(urldecode($p));
320: if (is_array($v)) {
321:
322: foreach ($v as $p2 => $v2) {
323: $p2 = urlencode(urldecode($p2));
324: $v2 = urlencode(urldecode($v2));
325: $sQuery .= $p . '[' . $p2 . ']=' . $v2 . $this->_sAmp;
326: }
327: } else {
328: $v = urlencode(urldecode($v));
329: $sQuery .= $p . '=' . $v . $this->_sAmp;
330: }
331: }
332: }
333: if (strlen($sQuery) > 0) {
334: $sQuery = '?' . substr($sQuery, 0, -strlen($this->_sAmp));
335: }
336: return $sQuery;
337: }
338:
339: 340: 341: 342: 343: 344:
345: private function _getClientParameter(array $aArgs) {
346: global $client;
347:
348:
349: if ($this->_aMrCfg['use_client'] == 1) {
350: $iChangeClient = (isset($aArgs['changeclient'])) ? (int) $aArgs['changeclient'] : 0;
351: $idclient = ($iChangeClient > 0) ? $iChangeClient : $client;
352: if ($this->_aMrCfg['use_client_name'] == 1) {
353: return urlencode(ModRewrite::getClientName($idclient));
354: } else {
355: return $idclient;
356: }
357: }
358: return NULL;
359: }
360:
361: 362: 363: 364: 365: 366:
367: private function _getLanguageParameter(array $aArgs) {
368: global $lang;
369:
370:
371: if ($this->_aMrCfg['use_language'] == 1) {
372: $iChangeLang = (isset($aArgs['changelang'])) ? (int) $aArgs['changelang'] : 0;
373: $idlang = ($iChangeLang > 0) ? $iChangeLang : $lang;
374: if ($this->_aMrCfg['use_language_name'] == 1) {
375: return urlencode(ModRewrite::getLanguageName($idlang));
376: } else {
377: return $idlang;
378: }
379: }
380: return NULL;
381: }
382:
383: 384: 385: 386: 387: 388:
389: private function _getPath(array $aPretty) {
390: $sPath = (isset($aPretty['urlpath'])) ? $aPretty['urlpath'] : '';
391:
392:
393: if ($this->_aMrCfg['startfromroot'] == 0 && (strlen($sPath) > 0)) {
394:
395: $aCategories = explode('/', $sPath);
396:
397:
398: array_shift($aCategories);
399:
400:
401: $sPath = implode('/', $aCategories);
402: }
403:
404: return $sPath;
405: }
406:
407: 408: 409: 410: 411: 412: 413:
414: private function _getArticleName(array $aPretty, array $aArgs) {
415: $sArticle = (isset($aPretty['urlname'])) ? $aPretty['urlname'] : '';
416:
417: $iIdCat = (isset($aArgs['idcat'])) ? (int) $aArgs['idcat'] : 0;
418: $iIdCatLang = (isset($aArgs['idcatlang'])) ? (int) $aArgs['idcatlang'] : 0;
419: $iIdCatArt = (isset($aArgs['idcatart'])) ? (int) $aArgs['idcatart'] : 0;
420: $iIdArt = (isset($aArgs['idart'])) ? (int) $aArgs['idart'] : 0;
421: $iIdArtLang = (isset($aArgs['idartlang'])) ? (int) $aArgs['idartlang'] : 0;
422:
423:
424: if (($iIdCat > 0 || $iIdCatLang > 0) && $iIdCatArt == 0 && $iIdArt == 0 && $iIdArtLang == 0) {
425: $sArticle = '';
426: if ($this->_aMrCfg['add_startart_name_to_url']) {
427: if ($this->_aMrCfg['default_startart_name'] !== '') {
428:
429: $sArticle = $this->_aMrCfg['default_startart_name'];
430: } else {
431: $sArticle = (isset($aPretty['urlname'])) ? $aPretty['urlname'] : '';
432: }
433: } else {
434:
435: $sArticle = '';
436: }
437: }
438:
439: return $sArticle;
440: }
441: }
442: