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