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