1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
24:
25: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
26:
27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37:
38: function mr_strNewTree(array $data) {
39: global $lang;
40:
41: ModRewriteDebugger::log($data, 'mr_strNewTree $data');
42:
43: if ((int) $data['newcategoryid'] > 0) {
44: $mrCatAlias = (trim($data['categoryalias']) !== '') ? trim($data['categoryalias']) : trim($data['categoryname']);
45:
46: ModRewrite::setCatWebsafeName($mrCatAlias, $data['newcategoryid'], $lang);
47: ModRewrite::setCatUrlPath($data['newcategoryid'], $lang);
48: }
49:
50: return $data;
51: }
52:
53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63:
64: function mr_strNewCategory(array $data) {
65: global $lang;
66:
67: ModRewriteDebugger::log($data, 'mr_strNewCategory $data');
68:
69: if ((int) $data['newcategoryid'] > 0) {
70: $mrCatAlias = (trim($data['categoryalias']) !== '') ? trim($data['categoryalias']) : trim($data['categoryname']);
71:
72: ModRewrite::setCatWebsafeName($mrCatAlias, $data['newcategoryid'], $lang);
73: ModRewrite::setCatUrlPath($data['newcategoryid'], $lang);
74: }
75:
76: return $data;
77: }
78:
79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92:
93: function mr_strRenameCategory(array $data) {
94: ModRewriteDebugger::log($data, 'mr_strRenameCategory $data');
95:
96:
97:
98: $recursion = (is_int($data['recursion'])) ? $data['recursion'] : 1;
99: if ($recursion > 50) {
100: exit("#20100201-1503: sorry - maximum function nesting level of " . $recursion . " reached");
101: }
102:
103: $mrCatAlias = (trim($data['newcategoryalias']) !== '') ? trim($data['newcategoryalias']) : trim($data['newcategoryname']);
104: if ($mrCatAlias != '') {
105:
106: ModRewrite::setCatWebsafeName($mrCatAlias, $data['idcat'], $data['lang']);
107: ModRewrite::setCatUrlPath($data['idcat'], $data['lang']);
108: }
109:
110:
111:
112: $str = 'parentid=' . $data['idcat'];
113: $oCatColl = new cApiCategoryCollection($str);
114:
115: while ($oCat = $oCatColl->next()) {
116:
117: $str = 'idcat=' . $oCat->get('idcat') . ' AND idlang=' . (int) $data['lang'];
118: $oCatLanColl = new cApiCategoryLanguageCollection($str);
119: if ($oCatLan = $oCatLanColl->next()) {
120:
121: $childData = array(
122: 'idcat' => $oCat->get('idcat'),
123: 'lang' => (int) $data['lang'],
124: 'newcategoryname' => $oCatLan->get('name'),
125: 'newcategoryalias' => $oCatLan->get('urlname'),
126: 'recursion' => $recursion + 1
127: );
128:
129: $resData = mr_strRenameCategory($childData);
130: }
131: }
132:
133: return $data;
134: }
135:
136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149:
150: function mr_strMoveUpCategory($idcat) {
151: ModRewriteDebugger::log($idcat, 'mr_strMoveUpCategory $idcat');
152:
153:
154: $cat = new cApiCategory((int) $idcat);
155: if (!$cat->get('preid')) {
156: return;
157: }
158:
159:
160: $aIdLang = ModRewrite::getCatLanguages($idcat);
161:
162:
163: foreach ($aIdLang as $iIdLang) {
164:
165: $sCatname = ModRewrite::getCatName($idcat, $iIdLang);
166:
167: ModRewrite::setCatWebsafeName($sCatname, $idcat, $iIdLang);
168: }
169:
170: return $idcat;
171: }
172:
173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186:
187: function mr_strMovedownCategory($idcat) {
188: ModRewriteDebugger::log($idcat, 'mr_strMovedownCategory $idcat');
189:
190:
191: $cat = new cApiCategory((int) $idcat);
192: if (!$cat->get('id')) {
193: return;
194: }
195:
196:
197: $aIdLang = ModRewrite::getCatLanguages($idcat);
198:
199: foreach ($aIdLang as $iIdLang) {
200:
201: $sCatname = ModRewrite::getCatName($idcat, $iIdLang);
202:
203: ModRewrite::setCatWebsafeName($sCatname, $idcat, $iIdLang);
204: }
205:
206: return $idcat;
207: }
208:
209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220:
221: function mr_strMoveSubtree(array $data) {
222: ModRewriteDebugger::log($data, 'mr_strMoveSubtree $data');
223:
224:
225: if ((int) $data['idcat'] <= 0) {
226: return;
227: }
228:
229:
230: $cat = new cApiCategory($data['idcat']);
231: if (!$cat->get('idcat')) {
232: return;
233: }
234:
235:
236: $aIdLang = ModRewrite::getCatLanguages($data['idcat']);
237:
238: foreach ($aIdLang as $iIdLang) {
239:
240: $sCatname = ModRewrite::getCatName($data['idcat'], $iIdLang);
241:
242: ModRewrite::setCatWebsafeName($sCatname, $data['idcat'], $iIdLang);
243: ModRewrite::setCatUrlPath($data['idcat'], $iIdLang);
244: }
245:
246:
247: $oCatColl = new cApiCategoryCollection('parentid=' . $data['idcat']);
248: while ($oCat = $oCatColl->next()) {
249: mr_strMoveSubtree(array('idcat' => $oCat->get('idcat')));
250: }
251:
252: return $data;
253: }
254:
255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265:
266: function mr_strCopyCategory(array $data) {
267: ModRewriteDebugger::log($data, 'mr_strCopyCategory $data');
268:
269: $idcat = (int) $data['newcat']->get('idcat');
270: if ($idcat <= 0) {
271: return $data;
272: }
273:
274:
275: $aIdLang = ModRewrite::getCatLanguages($idcat);
276:
277: foreach ($aIdLang as $iIdLang) {
278:
279: $sCatname = ModRewrite::getCatName($idcat, $iIdLang);
280:
281: ModRewrite::setCatWebsafeName($sCatname, $idcat, $iIdLang);
282: ModRewrite::setCatUrlPath($idcat, $iIdLang);
283: }
284: }
285:
286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297:
298: function mr_strSyncCategory(array $data) {
299: ModRewriteDebugger::log($data, 'mr_strSyncCategory $data');
300: ModRewrite::setCatUrlPath($data['idcat'], $data['idlang']);
301: return $data;
302: }
303:
304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314:
315: function mr_conSaveArticle(array $data) {
316: global $tmp_firstedit, $client;
317:
318: ModRewriteDebugger::log($data, 'mr_conSaveArticle $data');
319:
320: if ((int) $data['idart'] == 0) {
321: return $data;
322: }
323:
324: if (cString::getStringLength(trim($data['urlname'])) == 0) {
325: $data['urlname'] = $data['title'];
326: }
327:
328: if (1 == $tmp_firstedit) {
329:
330: $aLanguages = getLanguagesByClient($client);
331:
332: foreach ($aLanguages as $iLang) {
333: ModRewrite::setArtWebsafeName($data['urlname'], $data['idart'], $iLang, $data['idcat']);
334: }
335: } else {
336:
337: $aArticle = ModRewrite::getArtIdByArtlangId($data['idartlang']);
338:
339: if (isset($aArticle['idart']) && isset($aArticle['idlang'])) {
340: ModRewrite::setArtWebsafeName($data['urlname'], $aArticle['idart'], $aArticle['idlang'], $data['idcat']);
341: }
342: }
343:
344: return $data;
345: }
346:
347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357:
358: function mr_conMoveArticles($data) {
359: ModRewriteDebugger::log($data, 'mr_conMoveArticles $data');
360:
361:
362: if (!is_array($data)) {
363: return $data;
364: } elseif (!isset($data['idartlang'])) {
365: return $data;
366: } elseif (!isset($data['idart'])) {
367: return $data;
368: }
369:
370: $arr_art = ModRewrite::getArtIds($data['idartlang']);
371: if (count($arr_art) == 2) {
372: ModRewrite::setArtWebsafeName($arr_art["urlname"], $data['idart'], $arr_art["idlang"]);
373: }
374:
375: return $data;
376: }
377:
378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388:
389: function mr_conCopyArtLang($data) {
390: ModRewriteDebugger::log($data, 'mr_conCopyArtLang $data');
391:
392:
393: if (!is_array($data)) {
394: return $data;
395: } elseif (!isset($data['title'])) {
396: return $data;
397: } elseif (!isset($data['idart'])) {
398: return $data;
399: } elseif (!isset($data['idlang'])) {
400: return $data;
401: }
402:
403: ModRewrite::setArtWebsafeName($data['title'], $data['idart'], $data['idlang']);
404:
405: return $data;
406: }
407:
408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424:
425: function mr_conSyncArticle($data) {
426: ModRewriteDebugger::log($data, 'mr_conSyncArticle $data');
427:
428:
429: if (!is_array($data)) {
430: return $data;
431: } elseif (!isset($data['src_art_lang']) || !is_array($data['src_art_lang'])) {
432: return $data;
433: } elseif (!isset($data['dest_art_lang']) || !is_array($data['dest_art_lang'])) {
434: return $data;
435: } elseif (!isset($data['dest_art_lang']['idart'])) {
436: return $data;
437: } elseif (!isset($data['dest_art_lang']['idlang'])) {
438: return $data;
439: }
440:
441: if (!isset($data['src_art_lang']['urlname'])) {
442: $artLang = new cApiArticleLanguage($data['src_art_lang']['idartlang']);
443: $urlname = $artLang->get('urlname');
444: } else {
445: $urlname = $data['src_art_lang']['urlname'];
446: }
447:
448: if ($urlname) {
449: ModRewrite::setArtWebsafeName($urlname, $data['dest_art_lang']['idart'], $data['dest_art_lang']['idlang']);
450: }
451:
452: return $data;
453: }
454:
455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466:
467: function mr_buildNewUrl($url) {
468: global $lang;
469:
470: ModRewriteDebugger::add($url, 'mr_buildNewUrl() in -> $url');
471:
472: $oUrl = cUri::getInstance();
473: $aUrl = $oUrl->parse($url);
474:
475:
476: if (!isset($aUrl['params']['lang'])) {
477: $aUrl['params']['lang'] = $lang;
478: }
479:
480:
481: $newUrl = $oUrl->build($aUrl['params']);
482:
483:
484: if (isset($aUrl['fragment'])) {
485: $newUrl .= '#' . $aUrl['fragment'];
486: }
487:
488: $arr = array(
489: 'in' => $url,
490: 'out' => $newUrl,
491: );
492: ModRewriteDebugger::add($arr, 'mr_buildNewUrl() in -> out');
493:
494: return $newUrl;
495: }
496:
497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507:
508: function mr_buildGeneratedCode($code) {
509: global $client, $cfgClient;
510:
511: ModRewriteDebugger::add($code, 'mr_buildGeneratedCode() in');
512:
513: $sseStartTime = getmicrotime();
514:
515:
516: if (ModRewrite::isEnabled()) {
517:
518:
519: $code = preg_replace_callback("/<a([^>]*)href\s*=\s*[\"|\'][\/]#(.?|.+?)[\"|\']([^>]*)>/i", function($match) {
520: return ModRewrite::rewriteHtmlAnchor($match);
521: }, $code);
522:
523:
524: $code = str_replace("'", "'", $code);
525:
526:
527:
528:
529:
530:
531:
532: $baseUri = cRegistry::getFrontendUrl();
533: $baseUri = cApiCecHook::executeAndReturn("Contenido.Frontend.BaseHrefGeneration", $baseUri);
534:
535:
536: $code = preg_replace_callback("/([\"|\'|=])upload\/(.?|.+?)([\"|\'|>])/i", function($match) use ($baseUri) {
537: return stripslashes($match[1] . $baseUri . 'upload/' . $match[2] . $match[3]);
538: }, $code);
539:
540:
541:
542: $aPattern = array(
543: '/([\"|\'|=])\/front_content\.php(.?|.+?)([\"|\'|>])/i',
544: '/([\"|\'|=])\.\/front_content\.php(.?|.+?)([\"|\'|>])/i'
545: );
546:
547: $aReplace = array(
548: '\1front_content.php\2\3',
549: '\1front_content.php\2\3'
550: );
551:
552:
553: $code = preg_replace($aPattern, $aReplace, $code);
554:
555:
556: $oMRUrlStack = ModRewriteUrlStack::getInstance();
557: $oMRUrlStack->add('front_content.php');
558:
559: $matches = NULL;
560: preg_match_all("/([\"|\'|=])front_content\.php(.?|.+?)([\"|\'|>])/i", $code, $matches, PREG_SET_ORDER);
561: foreach ($matches as $val) {
562: $oMRUrlStack->add('front_content.php' . $val[2]);
563: }
564:
565:
566: $code = str_replace('"front_content.php"', '"' . mr_buildNewUrl('front_content.php') . '"', $code);
567: $code = str_replace("'front_content.php'", "'" . mr_buildNewUrl('front_content.php') . "'", $code);
568: $code = preg_replace_callback("/([\"|\'|=])front_content\.php(.?|.+?)([\"|\'|>])/i", function($match) {
569: return $match[1] . mr_buildNewUrl('front_content.php' . $match[2]) . $match[3];
570: }, $code);
571:
572: ModRewriteDebugger::add($code, 'mr_buildGeneratedCode() out');
573:
574: } else {
575:
576: $code = preg_replace_callback("/<a([^>]*)href\s*=\s*[\"|\'][\/]#(.?|.+?)[\"|\']([^>]*)>/i", function($match) {
577: return ModRewrite::contenidoHtmlAnchor($match, $GLOBALS['is_XHTML']);
578: }, $code);
579: }
580:
581: $sseEndTime = getmicrotime();
582:
583: ModRewriteDebugger::add(($sseEndTime - $sseStartTime), 'mr_buildGeneratedCode() total spend time');
584:
585: if ($debug = mr_debugOutput(false)) {
586: $code = cString::iReplaceOnce("</body>", $debug . "\n</body>", $code);
587: }
588:
589: return $code;
590:
591: }
592:
593: 594: 595: 596: 597: 598: 599:
600: function mr_setClientLanguageId($client) {
601: global $lang, $load_lang, $cfg;
602:
603: if ((int) $lang > 0) {
604:
605: return;
606: } elseif ($load_lang) {
607:
608: $lang = $load_lang;
609: return;
610: }
611:
612:
613: $sql = "SELECT B.idlang FROM "
614: . $cfg['tab']['clients_lang'] . " AS A, "
615: . $cfg['tab']['lang'] . " AS B "
616: . "WHERE "
617: . "A.idclient='" . ((int) $client) . "' AND A.idlang=B.idlang"
618: . "LIMIT 0,1";
619:
620: if ($aData = mr_queryAndNextRecord($sql)) {
621: $lang = $aData['idlang'];
622: }
623: }
624:
625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637:
638: function mr_loadConfiguration($clientId, $forceReload = false) {
639: global $cfg;
640: static $aLoaded;
641:
642: $clientId = (int) $clientId;
643: if (!isset($aLoaded)) {
644: $aLoaded = array();
645: } elseif (isset($aLoaded[$clientId]) && $forceReload == false) {
646: return;
647: }
648:
649: $mrConfig = mr_getConfiguration($clientId);
650:
651: if (is_array($mrConfig)) {
652:
653: $cfg = array_merge($cfg, $mrConfig);
654: } else {
655:
656: $backendPath = cRegistry::getBackendPath();
657: include_once($backendPath . $cfg['path']['plugins'] . 'mod_rewrite/includes/config.mod_rewrite_default.php');
658: }
659:
660: $aLoaded[$clientId] = true;
661: }
662:
663: 664: 665: 666: 667: 668: 669: 670: 671:
672: function mr_getConfigurationFilePath($clientId) {
673: $clientConfig = cRegistry::getClientConfig((int) $clientId);
674: $fePath = $clientConfig['path']['frontend'];
675: return $fePath . 'data/config/' . CON_ENVIRONMENT . '/config.mod_rewrite.php';
676: }
677:
678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688:
689: function mr_getConfiguration($clientId) {
690: global $cfg;
691:
692: $clientId = (int) $clientId;
693:
694: $file = mr_getConfigurationFilePath($clientId);
695:
696: if (!is_file($file) || !is_readable($file)) {
697: $backendPath = cRegistry::getBackendPath();
698: $file = $backendPath . $cfg['path']['plugins'] . 'mod_rewrite/includes/config.mod_rewrite_' . $clientId . '.php';
699: }
700:
701: if (!is_file($file) || !is_readable($file)) {
702: return NULL;
703: }
704: if ($content = cFileHandler::read($file)) {
705: return unserialize($content);
706: } else {
707: return NULL;
708: }
709: }
710:
711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722:
723: function mr_setConfiguration($clientId, array $config) {
724: global $cfg;
725:
726: $clientId = (int) $clientId;
727:
728: $file = mr_getConfigurationFilePath($clientId);
729: $result = cFileHandler::write($file, serialize($config));
730:
731: $backendPath = cRegistry::getBackendPath();
732: $file = $backendPath . $cfg['path']['plugins'] . 'mod_rewrite/includes/config.mod_rewrite_' . $clientId . '.php';
733: if (is_file($file) && is_writeable($file)) {
734: cFileHandler::remove($file, serialize($config));
735: }
736:
737: return ($result) ? true : false;
738: }
739:
740: 741: 742: 743: 744: 745: 746: 747: 748:
749: function mr_runFrontendController() {
750: $iStartTime = getmicrotime();
751:
752: plugin_include('mod_rewrite', 'includes/config.plugin.php');
753:
754: if (ModRewrite::isEnabled() == true) {
755: plugin_include('mod_rewrite', 'includes/front_content_controller.php');
756:
757: $totalTime = sprintf('%.4f', (getmicrotime() - $iStartTime));
758: ModRewriteDebugger::add($totalTime, 'mr_runFrontendController() total time');
759: }
760:
761: return true;
762: }
763:
764: 765: 766: 767: 768: 769: 770:
771: function mr_removeMultipleChars($char, $string) {
772: while (cString::findFirstPos($string, $char . $char) !== false) {
773: $string = str_replace($char . $char, $char, $string);
774: }
775: return $string;
776: }
777:
778: 779: 780: 781: 782: 783:
784: function mr_i18n($key) {
785: global $lngAMR;
786: return (is_array($lngAMR) && isset($lngAMR[$key])) ? $lngAMR[$key] : 'n. a.';
787: }
788:
789:
790:
791:
792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813: 814:
815: function mr_queryAndNextRecord($query) {
816: static $db;
817: if (!isset($db)) {
818: $db = cRegistry::getDb();
819: }
820: if (!$db->query($query)) {
821: return NULL;
822: }
823: return ($db->nextRecord()) ? $db->getRecord() : NULL;
824: }
825:
826: 827: 828: 829: 830: 831: 832: 833: 834: 835: 836: 837: 838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848: 849: 850: 851: 852: 853: 854: 855: 856:
857: function mr_arrayValue($array, $key, $default = NULL) {
858: if (!is_array($array)) {
859: return $default;
860: } elseif (!isset($array[$key])) {
861: return $default;
862: } else {
863: return $array[$key];
864: }
865: }
866:
867: 868: 869: 870: 871: 872: 873: 874: 875: 876: 877: 878: 879: 880: 881: 882: 883: 884: 885: 886: 887: 888: 889: 890:
891: function mr_requestCleanup(&$data, $options = NULL) {
892: if (!mr_arrayValue($options, 'filter')) {
893: $options['filter'] = array('trim', 'strip_tags', 'stripslashes');
894: }
895:
896: if (is_array($data)) {
897: foreach ($data as $p => $v) {
898: $data[$p] = mr_requestCleanup($v, $options);
899: }
900: } else {
901: foreach ($options['filter'] as $filter) {
902: if ($filter == 'trim') {
903: $data = trim($data);
904: } elseif ($filter == 'strip_tags') {
905: $data = strip_tags($data);
906: } elseif ($filter == 'stripslashes') {
907: $data = stripslashes($data);
908: } elseif (function_exists($filter)) {
909: $data = call_user_func($filter, $data);
910: }
911: }
912: }
913: return $data;
914: }
915:
916: 917: 918: 919: 920: 921: 922: 923: 924:
925: function mr_getRequest($key, $default = NULL) {
926: static $cache;
927: if (!isset($cache)) {
928: $cache = array();
929: }
930: if (isset($cache[$key])) {
931: return $cache[$key];
932: }
933: if (isset($_GET[$key])) {
934: $val = $_GET[$key];
935: } elseif (isset($_POST[$key])) {
936: $val = $_POST[$key];
937: } else {
938: $val = $default;
939: }
940: $cache[$key] = strip_tags(trim($val));
941: return $cache[$key];
942: }
943:
944: 945: 946: 947: 948: 949:
950: function ($header) {
951: header($header);
952:
953:
954:
955:
956:
957:
958:
959:
960:
961: }
962:
963: 964: 965: 966: 967: 968: 969: 970:
971: function mr_debugOutput($print = true) {
972: global $DB_Contenido_QueryCache;
973: if (isset($DB_Contenido_QueryCache) && is_array($DB_Contenido_QueryCache) &&
974: count($DB_Contenido_QueryCache) > 0) {
975: ModRewriteDebugger::add($DB_Contenido_QueryCache, 'sql statements');
976:
977:
978: $timeTotal = 0;
979: foreach ($DB_Contenido_QueryCache as $pos => $item) {
980: $timeTotal += $item['time'];
981: }
982: ModRewriteDebugger::add($timeTotal, 'sql total time');
983: }
984:
985: $sOutput = ModRewriteDebugger::getAll();
986: if ($print == true) {
987: echo $sOutput;
988: } else {
989: return $sOutput;
990: }
991: }
992: