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 ($secondCall = false) {
268: global $client;
269:
270:
271: $requestUri = $_SERVER['REQUEST_URI'];
272:
273: if (array_key_exists('REDIRECT_URL', $_SERVER)) {
274: $requestUri = $_SERVER['REDIRECT_URL'];
275: if (array_key_exists('REDIRECT_QUERY_STRING', $_SERVER)) {
276: $requestUri .= '?' . $_SERVER['REDIRECT_QUERY_STRING'];
277: }
278: }
279:
280:
281: if (1 == $this->getConfig('use_lowercase_uri')) {
282: $requestUri = strtolower($requestUri);
283: }
284:
285:
286: if (parent::getConfig('rootdir') !== '/' && strpos($requestUri, $this->_sIncommingUrl) === 0) {
287: $this->_sIncommingUrl = str_replace(parent::getConfig('rootdir'), '/', $this->_sIncommingUrl);
288: }
289:
290: $aUrlComponents = $this->_parseUrl($this->_sIncommingUrl);
291: if (isset($aUrlComponents['path'])) {
292: if (parent::getConfig('rootdir') !== '/' && strpos($aUrlComponents['path'], parent::getConfig('rootdir')) === 0) {
293: $aUrlComponents['path'] = str_replace(parent::getConfig('rootdir'), '/', $aUrlComponents['path']);
294: }
295:
296: if ($secondCall == true) {
297:
298:
299:
300: $routings = parent::getConfig('routing');
301: if (is_array($routings) && isset($routings[$aUrlComponents['path']])) {
302: $aUrlComponents['path'] = $routings[$aUrlComponents['path']];
303: if (strpos($aUrlComponents['path'], self::FRONT_CONTENT) !== false) {
304:
305:
306: $this->_bRoutingFound = true;
307:
308:
309: mr_setClientLanguageId($client);
310:
311:
312: $url = mr_buildNewUrl($aUrlComponents['path']);
313:
314: $aUrlComponents = $this->_parseUrl($url);
315:
316:
317: if (isset($aUrlComponents['query'])) {
318: $vars = null;
319: parse_str($aUrlComponents['query'], $vars);
320: $_GET = array_merge($_GET, $vars);
321: }
322:
323: $this->_aParts = array();
324: }
325: } else {
326: return;
327: }
328: }
329:
330: $aPaths = explode('/', $aUrlComponents['path']);
331: foreach ($aPaths as $p => $item) {
332: if (!empty($item)) {
333:
334: $arr = explode('.', $item);
335: $count = count($arr);
336: if ($count > 0 && '.' . strtolower($arr[$count - 1]) == parent::getConfig('file_extension')) {
337: array_pop($arr);
338: $this->_sArtName = trim(implode('.', $arr));
339: } else {
340: $this->_aParts[] = $item;
341: }
342: }
343: }
344:
345: if ($secondCall == true) {
346:
347: $this->_setClientId();
348: mr_loadConfiguration($this->_iClientMR);
349: $this->_setLanguageId();
350: }
351: }
352: ModRewriteDebugger::add($this->_aParts, 'ModRewriteController::_extractRequestUri() $this->_aParts');
353:
354:
355: if ($this->_hasPartArrayItems()) {
356: foreach ($this->_aParts as $p => $item) {
357: if ($item == self::FRONT_CONTENT) {
358: unset($this->_aParts[$p]);
359: }
360: }
361: }
362: }
363:
364: 365: 366: 367:
368: private function _initializeClientId() {
369: global $client, $changeclient, $load_client;
370:
371: $iClient = (isset($client) && (int) $client > 0) ? $client : 0;
372: $iChangeClient = (isset($changeclient) && (int) $changeclient > 0) ? $changeclient : 0;
373: $iLoadClient = (isset($load_client) && (int) $load_client > 0) ? $load_client : 0;
374:
375: $this->_iClientMR = 0;
376: if ($iClient > 0 && $iChangeClient == 0) {
377: $this->_iClientMR = $iClient;
378: } elseif ($iChangeClient > 0) {
379: $this->_iClientMR = $iChangeClient;
380: } else {
381: $this->_iClientMR = $iLoadClient;
382: }
383:
384: if ((int) $this->_iClientMR > 0) {
385:
386: $client = (int) $this->_iClientMR;
387: }
388: }
389:
390: 391: 392:
393: private function _initializeLanguageId() {
394: global $lang, $changelang, $load_lang;
395:
396: $iLang = (isset($lang) && (int) $lang > 0) ? $lang : 0;
397: $iChangeLang = (isset($changelang) && (int) $changelang > 0) ? $changelang : 0;
398: $iLoadLang = (isset($load_lang) && (int) $load_lang > 0) ? $load_lang : 0;
399:
400: $this->_iLangMR = 0;
401: if ($iLang > 0 && $iChangeLang == 0) {
402: $this->_iLangMR = $iLang;
403: } elseif ($iChangeLang > 0) {
404: $this->_iLangMR = $iChangeLang;
405: } else {
406: $this->_iLangMR = $iLoadLang;
407: }
408:
409: if ((int) $this->_iLangMR > 0) {
410:
411: $lang = (int) $this->_iLangMR;
412: }
413: }
414:
415: 416: 417:
418: private function _setClientId() {
419: global $client;
420:
421: if ($this->_bError) {
422: return;
423: } elseif ($this->_isRootRequest()) {
424:
425: return;
426: } elseif (parent::getConfig('use_client') !== 1) {
427: return;
428: }
429:
430: if (parent::getConfig('use_client_name') == 1) {
431: $detectedClientId = (int) ModRewrite::getClientId(array_shift($this->_aParts));
432: } else {
433: $detectedClientId = (int) array_shift($this->_aParts);
434: if ($detectedClientId > 0 && !ModRewrite::languageIdExists($detectedClientId)) {
435: $detectedClientId = 0;
436: }
437: }
438:
439: if ($detectedClientId > 0) {
440:
441: $this->_iClientMR = $detectedClientId;
442: $client = $detectedClientId;
443: } else {
444: $this->_setError(self::ERROR_CLIENT);
445: }
446: }
447:
448: 449: 450:
451: private function _setLanguageId() {
452: global $lang;
453:
454: if ($this->_bError) {
455: return;
456: } elseif ($this->_isRootRequest()) {
457:
458: return;
459: } elseif (parent::getConfig('use_language') !== 1) {
460: return;
461: }
462:
463: if (parent::getConfig('use_language_name') == 1) {
464:
465: $languageName = array_shift($this->_aParts);
466: $detectedLanguageId = (int) ModRewrite::getLanguageId($languageName, $this->_iClientMR);
467: } else {
468: $detectedLanguageId = (int) array_shift($this->_aParts);
469: if ($detectedLanguageId > 0 && !ModRewrite::clientIdExists($detectedLanguageId)) {
470: $detectedLanguageId = 0;
471: }
472: }
473:
474: if ($detectedLanguageId > 0) {
475:
476: $this->_iLangMR = $detectedLanguageId;
477: $lang = $detectedLanguageId;
478: } else {
479: $this->_setError(self::ERROR_LANGUAGE);
480: }
481: }
482:
483: 484: 485:
486: private function _setPathresolverSetting() {
487: global $client, $lang, $load_lang, $idcat;
488:
489: if ($this->_bError) {
490: return;
491: } elseif (!$this->_hasPartArrayItems()) {
492: return;
493: }
494:
495: $this->_sPath = '/' . implode('/', $this->_aParts) . '/';
496:
497: if (!isset($lang) || (int) $lang <= 0) {
498: if ((int) $load_lang > 0) {
499:
500: $lang = (int) $load_lang;
501: } else {
502:
503: $clCol = new cApiClientLanguageCollection();
504: $clCol->setWhere('idclient', $client);
505: $clCol->query();
506: if (false !== $clItem = $clCol->next()) {
507: $lang = $clItem->get('idlang');
508: }
509: }
510: }
511:
512: $idcat = (int) ModRewrite::getCatIdByUrlPath($this->_sPath);
513:
514: if ($idcat == 0) {
515:
516: $this->_setError(self::ERROR_CATEGORY);
517: $idcat = null;
518: } else {
519:
520: unset($this->_sPath);
521: }
522:
523: ModRewriteDebugger::add($idcat, 'ModRewriteController->_setPathresolverSetting $idcat');
524: ModRewriteDebugger::add($this->_sPath, 'ModRewriteController->_setPathresolverSetting $this->_sPath');
525: }
526:
527: 528: 529:
530: private function _setIdart() {
531: global $idcat, $idart, $lang;
532:
533: if ($this->_bError) {
534: return;
535: } else if ($this->_isRootRequest()) {
536: return;
537: }
538:
539: $iIdCat = (isset($idcat) && (int) $idcat > 0) ? $idcat : 0;
540: $iIdArt = (isset($idart) && (int) $idart > 0) ? $idart : 0;
541: $detectedIdart = 0;
542: $defaultStartArtName = parent::getConfig('default_startart_name');
543: $currArtName = $this->_sArtName;
544:
545:
546: if (parent::getConfig('add_startart_name_to_url') && !empty($currArtName)) {
547: if ($currArtName == $defaultStartArtName) {
548:
549:
550: $currArtName = '';
551: }
552: }
553:
554:
555: if ($iIdCat == 0 && $iIdArt == 0 && empty($currArtName)) {
556:
557:
558: return;
559: }
560:
561: if ($iIdCat > 0 && $iIdArt == 0 && !empty($currArtName)) {
562:
563: $detectedIdart = (int) ModRewrite::getArtIdByWebsafeName($currArtName, $iIdCat, $lang);
564: } elseif ($iIdCat > 0 && $iIdArt == 0 && empty($currArtName)) {
565: if (parent::getConfig('add_startart_name_to_url') && ($currArtName == '' || $defaultStartArtName == '' || $defaultStartArtName == $this->_sArtName)) {
566:
567: $catLangColl = new cApiCategoryLanguageCollection();
568: $detectedIdart = (int) $catLangColl->getStartIdartByIdcatAndIdlang($iIdCat, $lang);
569: }
570: } elseif ($iIdCat == 0 && $iIdArt == 0 && !empty($currArtName)) {
571:
572: $detectedIdart = (int) ModRewrite::getArtIdByWebsafeName($currArtName, $iIdCat, $lang);
573: }
574:
575: if ($detectedIdart > 0) {
576: $idart = $detectedIdart;
577: } elseif (!empty($currArtName)) {
578: $this->_setError(self::ERROR_ARTICLE);
579: }
580:
581: ModRewriteDebugger::add($detectedIdart, 'ModRewriteController->_setIdart $detectedIdart');
582: }
583:
584: 585: 586: 587: 588: 589:
590: private function _postValidation() {
591: global $idcat, $idart, $client;
592:
593: if ($this->_bError || $this->_bRoutingFound || !$this->_hasPartArrayItems()) {
594: return;
595: }
596:
597: if (parent::getConfig('startfromroot') == 1 && parent::getConfig('prevent_duplicated_content') == 1) {
598:
599:
600:
601: $idcat = (isset($idcat) && (int) $idcat > 0) ? $idcat : null;
602: $idart = (isset($idart) && (int) $idart > 0) ? $idart : null;
603:
604:
605: $param = '';
606: if ($idcat) {
607: $param .= 'idcat=' . (int) $idcat;
608: }
609: if ($idart) {
610: $param .= ($param !== '') ? '&idart=' . (int) $idart : 'idart=' . (int) $idart;
611: }
612:
613: if ($param == '') {
614: return;
615: }
616:
617:
618: mr_setClientLanguageId($client);
619:
620:
621: $url = mr_buildNewUrl(self::FRONT_CONTENT . '?' . $param);
622:
623: $aUrlComponents = @parse_url($this->_sIncommingUrl);
624: $incommingUrl = (isset($aUrlComponents['path'])) ? $aUrlComponents['path'] : '';
625:
626: ModRewriteDebugger::add($url, 'ModRewriteController->_postValidation validate url');
627: ModRewriteDebugger::add($incommingUrl, 'ModRewriteController->_postValidation incommingUrl');
628:
629:
630: if ($incommingUrl !== $url) {
631: $this->_setError(self::ERROR_POST_VALIDATION);
632: $idcat = null;
633: }
634: }
635: }
636:
637: 638: 639: 640: 641: 642:
643: private function _parseUrl($url) {
644: $this->_sResolvedUrl = $url;
645:
646: $oMrUrlUtil = ModRewriteUrlUtil::getInstance();
647: $url = $oMrUrlUtil->toContenidoUrl($url);
648:
649: return @parse_url($url);
650: }
651:
652: 653: 654: 655: 656:
657: private function _hasPartArrayItems() {
658: return (!empty($this->_aParts));
659: }
660:
661: 662: 663: 664: 665:
666: private function _isRootRequest() {
667: return ($this->_sIncommingUrl == '/' || $this->_sIncommingUrl == '');
668: }
669:
670: 671: 672: 673:
674: private function _setError($errCode) {
675: $this->_iError = (int) $errCode;
676: $this->_bError = ((int) $errCode > 0);
677: }
678:
679: }