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