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