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