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: class ModRewriteController extends ModRewriteBase {
32:
33:
34: const ERROR_CLIENT = 1;
35: const ERROR_LANGUAGE = 2;
36: const ERROR_CATEGORY = 3;
37: const ERROR_ARTICLE = 4;
38: const ERROR_POST_VALIDATION = 5;
39: const FRONT_CONTENT = 'front_content.php';
40:
41: 42: 43: 44: 45:
46: private $_aParts;
47:
48: 49: 50: 51: 52:
53: private $_sArtName;
54:
55: 56: 57: 58: 59:
60: private $_sPath;
61:
62: 63: 64: 65: 66:
67: private $_sIncommingUrl;
68:
69: 70: 71: 72: 73:
74: private $_sResolvedUrl;
75:
76: 77: 78: 79: 80:
81: private $_iClientMR;
82:
83: 84: 85: 86: 87:
88: private $_iLangMR;
89:
90: 91: 92: 93: 94:
95: private $_bError = false;
96:
97: 98: 99: 100: 101:
102: private $_iError = 0;
103:
104: 105: 106: 107: 108:
109: private $_bRoutingFound = false;
110:
111: 112: 113: 114: 115:
116: public function __construct($incommingUrl) {
117:
118:
119:
120: if (1 == $this->getConfig('use_lowercase_uri')) {
121: $incommingUrl = strtolower($incommingUrl);
122: }
123:
124: $this->_sIncommingUrl = $incommingUrl;
125: $this->_aParts = array();
126: $this->_sArtName = '';
127: }
128:
129: 130: 131: 132: 133:
134: public function getClient() {
135: return $GLOBALS['client'];
136: }
137:
138: 139: 140: 141: 142:
143: public function getChangeClient() {
144: return $GLOBALS['changeclient'];
145: }
146:
147: 148: 149: 150: 151:
152: public function getIdArt() {
153: return $GLOBALS['idart'];
154: }
155:
156: 157: 158: 159: 160:
161: public function getIdCat() {
162: return $GLOBALS['idcat'];
163: }
164:
165: 166: 167: 168: 169:
170: public function getLang() {
171: return $GLOBALS['lang'];
172: }
173:
174: 175: 176: 177: 178:
179: public function getChangeLang() {
180: return $GLOBALS['changelang'];
181: }
182:
183: 184: 185: 186: 187:
188: public function getPath() {
189: return $this->_sPath;
190: }
191:
192: 193: 194: 195: 196:
197: public function getResolvedUrl() {
198: return $this->_sResolvedUrl;
199: }
200:
201: 202: 203: 204: 205:
206: public function getRoutingFoundState() {
207: return $this->_bRoutingFound;
208: }
209:
210: 211: 212: 213: 214:
215: public function errorOccured() {
216: return $this->_bError;
217: }
218:
219: 220: 221: 222: 223:
224: public function getError() {
225: return $this->_iError;
226: }
227:
228: 229: 230: 231: 232: 233:
234: public function execute() {
235: if (parent::isEnabled() == false) {
236: return;
237: }
238:
239: $this->_extractRequestUri();
240:
241: $this->_initializeClientId();
242:
243: $this->_setClientId();
244:
245: mr_loadConfiguration($this->_iClientMR);
246:
247: $this->_setLanguageId();
248:
249:
250: $this->_extractRequestUri(true);
251:
252: $this->_setPathresolverSetting();
253:
254: $this->_setIdart();
255:
256: ModRewriteDebugger::add($this->_aParts, 'ModRewriteController::execute() _setIdart');
257:
258: $this->_postValidation();
259: }
260:
261: 262: 263: 264: 265: 266:
267: private function _extractRequestUri($secondCall = false) {
268: global $client;
269:
270:
271: $requestUri = $_SERVER['REQUEST_URI'];
272:
273:
274: if (1 == $this->getConfig('use_lowercase_uri')) {
275: $requestUri = strtolower($requestUri);
276: }
277:
278:
279:
280: $rootdir = cUriBuilderMR::getMultiClientRootDir(parent::getConfig('rootdir'));
281: if ('/' !== $rootdir && 0 === strpos($requestUri, $this->_sIncommingUrl)) {
282: $this->_sIncommingUrl = str_replace($rootdir, '/', $this->_sIncommingUrl);
283: }
284:
285: $aUrlComponents = $this->_parseUrl($this->_sIncommingUrl);
286: if (isset($aUrlComponents['path'])) {
287: if (parent::getConfig('rootdir') !== '/' && strpos($aUrlComponents['path'], parent::getConfig('rootdir')) === 0) {
288: $aUrlComponents['path'] = str_replace(parent::getConfig('rootdir'), '/', $aUrlComponents['path']);
289: }
290:
291: if ($secondCall == true) {
292:
293:
294:
295: $routings = parent::getConfig('routing');
296: if (is_array($routings) && isset($routings[$aUrlComponents['path']])) {
297: $aUrlComponents['path'] = $routings[$aUrlComponents['path']];
298: if (strpos($aUrlComponents['path'], self::FRONT_CONTENT) !== false) {
299:
300:
301: $this->_bRoutingFound = true;
302:
303:
304: mr_setClientLanguageId($client);
305:
306:
307: $url = mr_buildNewUrl($aUrlComponents['path']);
308:
309: $aUrlComponents = $this->_parseUrl($url);
310:
311:
312: if (isset($aUrlComponents['query'])) {
313: $vars = NULL;
314: parse_str($aUrlComponents['query'], $vars);
315: $_GET = array_merge($_GET, $vars);
316: }
317:
318: $this->_aParts = array();
319: }
320: } else {
321: return;
322: }
323: }
324:
325: $aPaths = explode('/', $aUrlComponents['path']);
326: foreach ($aPaths as $p => $item) {
327: if (!empty($item)) {
328:
329: $arr = explode('.', $item);
330: $count = count($arr);
331: if ($count > 0 && '.' . strtolower($arr[$count - 1]) == parent::getConfig('file_extension')) {
332: array_pop($arr);
333: $this->_sArtName = trim(implode('.', $arr));
334: } else {
335: $this->_aParts[] = $item;
336: }
337: }
338: }
339:
340: if ($secondCall == true) {
341:
342: $this->_setClientId();
343: mr_loadConfiguration($this->_iClientMR);
344: $this->_setLanguageId();
345: }
346: }
347: ModRewriteDebugger::add($this->_aParts, 'ModRewriteController::_extractRequestUri() $this->_aParts');
348:
349:
350: if ($this->_hasPartArrayItems()) {
351: foreach ($this->_aParts as $p => $item) {
352: if ($item == self::FRONT_CONTENT) {
353: unset($this->_aParts[$p]);
354: }
355: }
356: }
357: }
358:
359: 360: 361: 362:
363: private function _initializeClientId() {
364: global $client, $changeclient, $load_client;
365:
366: $iClient = (isset($client) && (int) $client > 0) ? $client : 0;
367: $iChangeClient = (isset($changeclient) && (int) $changeclient > 0) ? $changeclient : 0;
368: $iLoadClient = (isset($load_client) && (int) $load_client > 0) ? $load_client : 0;
369:
370: $this->_iClientMR = 0;
371: if ($iClient > 0 && $iChangeClient == 0) {
372: $this->_iClientMR = $iClient;
373: } elseif ($iChangeClient > 0) {
374: $this->_iClientMR = $iChangeClient;
375: } else {
376: $this->_iClientMR = $iLoadClient;
377: }
378:
379: if ((int) $this->_iClientMR > 0) {
380:
381: $client = (int) $this->_iClientMR;
382: }
383: }
384:
385: 386: 387:
388: private function _initializeLanguageId() {
389: global $lang, $changelang, $load_lang;
390:
391: $iLang = (isset($lang) && (int) $lang > 0) ? $lang : 0;
392: $iChangeLang = (isset($changelang) && (int) $changelang > 0) ? $changelang : 0;
393: $iLoadLang = (isset($load_lang) && (int) $load_lang > 0) ? $load_lang : 0;
394:
395: $this->_iLangMR = 0;
396: if ($iLang > 0 && $iChangeLang == 0) {
397: $this->_iLangMR = $iLang;
398: } elseif ($iChangeLang > 0) {
399: $this->_iLangMR = $iChangeLang;
400: } else {
401: $this->_iLangMR = $iLoadLang;
402: }
403:
404: if ((int) $this->_iLangMR > 0) {
405:
406: $lang = (int) $this->_iLangMR;
407: }
408: }
409:
410: 411: 412:
413: private function _setClientId() {
414: global $client;
415:
416: if ($this->_bError) {
417: return;
418: } elseif ($this->_isRootRequest()) {
419:
420: return;
421: } elseif (parent::getConfig('use_client') !== 1) {
422: return;
423: }
424:
425: if (parent::getConfig('use_client_name') == 1) {
426: $detectedClientId = (int) ModRewrite::getClientId(array_shift($this->_aParts));
427: } else {
428: $detectedClientId = (int) array_shift($this->_aParts);
429: if ($detectedClientId > 0 && !ModRewrite::languageIdExists($detectedClientId)) {
430: $detectedClientId = 0;
431: }
432: }
433:
434: if ($detectedClientId > 0) {
435:
436: $this->_iClientMR = $detectedClientId;
437: $client = $detectedClientId;
438: } else {
439: $this->_setError(self::ERROR_CLIENT);
440: }
441: }
442:
443: 444: 445:
446: private function _setLanguageId() {
447: global $lang;
448:
449: if ($this->_bError) {
450: return;
451: } elseif ($this->_isRootRequest()) {
452:
453: return;
454: } elseif (parent::getConfig('use_language') !== 1) {
455: return;
456: }
457:
458: if (parent::getConfig('use_language_name') == 1) {
459:
460: $languageName = array_shift($this->_aParts);
461: $detectedLanguageId = (int) ModRewrite::getLanguageId($languageName, $this->_iClientMR);
462: } else {
463: $detectedLanguageId = (int) array_shift($this->_aParts);
464: if ($detectedLanguageId > 0 && !ModRewrite::clientIdExists($detectedLanguageId)) {
465: $detectedLanguageId = 0;
466: }
467: }
468:
469: if ($detectedLanguageId > 0) {
470:
471: $this->_iLangMR = $detectedLanguageId;
472: $lang = $detectedLanguageId;
473: } else {
474: $this->_setError(self::ERROR_LANGUAGE);
475: }
476: }
477:
478: 479: 480:
481: private function _setPathresolverSetting() {
482: global $client, $lang, $load_lang, $idcat;
483:
484: if ($this->_bError) {
485: return;
486: } elseif (!$this->_hasPartArrayItems()) {
487: return;
488: }
489:
490: $this->_sPath = '/' . implode('/', $this->_aParts) . '/';
491:
492: if (!isset($lang) || (int) $lang <= 0) {
493: if ((int) $load_lang > 0) {
494:
495: $lang = (int) $load_lang;
496: } else {
497:
498: $clCol = new cApiClientLanguageCollection();
499: $clCol->setWhere('idclient', $client);
500: $clCol->query();
501: if (false !== $clItem = $clCol->next()) {
502: $lang = $clItem->get('idlang');
503: }
504: }
505: }
506:
507: $idcat = (int) ModRewrite::getCatIdByUrlPath($this->_sPath);
508:
509: if ($idcat == 0) {
510:
511: $this->_setError(self::ERROR_CATEGORY);
512: $idcat = NULL;
513: } else {
514:
515: unset($this->_sPath);
516: }
517:
518: ModRewriteDebugger::add($idcat, 'ModRewriteController->_setPathresolverSetting $idcat');
519: ModRewriteDebugger::add($this->_sPath, 'ModRewriteController->_setPathresolverSetting $this->_sPath');
520: }
521:
522: 523: 524:
525: private function _setIdart() {
526: global $idcat, $idart, $lang;
527:
528: if ($this->_bError) {
529: return;
530: } else if ($this->_isRootRequest()) {
531: return;
532: }
533:
534: $iIdCat = (isset($idcat) && (int) $idcat > 0) ? $idcat : 0;
535: $iIdArt = (isset($idart) && (int) $idart > 0) ? $idart : 0;
536: $detectedIdart = 0;
537: $defaultStartArtName = parent::getConfig('default_startart_name');
538: $currArtName = $this->_sArtName;
539:
540:
541: if (parent::getConfig('add_startart_name_to_url') && !empty($currArtName)) {
542: if ($currArtName == $defaultStartArtName) {
543:
544:
545: $currArtName = '';
546: }
547: }
548:
549:
550: if ($iIdCat == 0 && $iIdArt == 0 && empty($currArtName)) {
551:
552:
553: return;
554: }
555:
556: if ($iIdCat > 0 && $iIdArt == 0 && !empty($currArtName)) {
557:
558: $detectedIdart = (int) ModRewrite::getArtIdByWebsafeName($currArtName, $iIdCat, $lang);
559: } elseif ($iIdCat > 0 && $iIdArt == 0 && empty($currArtName)) {
560: if (parent::getConfig('add_startart_name_to_url') && ($currArtName == '' || $defaultStartArtName == '' || $defaultStartArtName == $this->_sArtName)) {
561:
562: $catLangColl = new cApiCategoryLanguageCollection();
563: $detectedIdart = (int) $catLangColl->getStartIdartByIdcatAndIdlang($iIdCat, $lang);
564: }
565: } elseif ($iIdCat == 0 && $iIdArt == 0 && !empty($currArtName)) {
566:
567: $detectedIdart = (int) ModRewrite::getArtIdByWebsafeName($currArtName, $iIdCat, $lang);
568: }
569:
570: if ($detectedIdart > 0) {
571: $idart = $detectedIdart;
572: } elseif (!empty($currArtName)) {
573: $this->_setError(self::ERROR_ARTICLE);
574: }
575:
576: ModRewriteDebugger::add($detectedIdart, 'ModRewriteController->_setIdart $detectedIdart');
577: }
578:
579: 580: 581: 582: 583: 584:
585: private function _postValidation() {
586: global $idcat, $idart, $client;
587:
588: if ($this->_bError || $this->_bRoutingFound || !$this->_hasPartArrayItems()) {
589: return;
590: }
591:
592: if (parent::getConfig('startfromroot') == 1 && parent::getConfig('prevent_duplicated_content') == 1) {
593:
594:
595:
596: $idcat = (isset($idcat) && (int) $idcat > 0) ? $idcat : NULL;
597: $idart = (isset($idart) && (int) $idart > 0) ? $idart : NULL;
598:
599:
600: $param = '';
601: if ($idcat) {
602: $param .= 'idcat=' . (int) $idcat;
603: }
604: if ($idart) {
605: $param .= ($param !== '') ? '&idart=' . (int) $idart : 'idart=' . (int) $idart;
606: }
607:
608: if ($param == '') {
609: return;
610: }
611:
612:
613: mr_setClientLanguageId($client);
614:
615:
616: $url = mr_buildNewUrl(self::FRONT_CONTENT . '?' . $param);
617:
618: $aUrlComponents = @parse_url($this->_sIncommingUrl);
619: $incommingUrl = (isset($aUrlComponents['path'])) ? $aUrlComponents['path'] : '';
620:
621: ModRewriteDebugger::add($url, 'ModRewriteController->_postValidation validate url');
622: ModRewriteDebugger::add($incommingUrl, 'ModRewriteController->_postValidation incommingUrl');
623:
624:
625: if ($incommingUrl !== $url) {
626: $this->_setError(self::ERROR_POST_VALIDATION);
627: $idcat = NULL;
628: }
629: }
630: }
631:
632: 633: 634: 635: 636: 637:
638: private function _parseUrl($url) {
639: $this->_sResolvedUrl = $url;
640:
641: $oMrUrlUtil = ModRewriteUrlUtil::getInstance();
642: $url = $oMrUrlUtil->toContenidoUrl($url);
643:
644: return @parse_url($url);
645: }
646:
647: 648: 649: 650: 651:
652: private function _hasPartArrayItems() {
653: return (!empty($this->_aParts));
654: }
655:
656: 657: 658: 659: 660:
661: private function _isRootRequest() {
662: return ($this->_sIncommingUrl == '/' || $this->_sIncommingUrl == '');
663: }
664:
665: 666: 667: 668:
669: private function _setError($errCode) {
670: $this->_iError = (int) $errCode;
671: $this->_bError = ((int) $errCode > 0);
672: }
673:
674: }