Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cUriBuilderMR
  • ModRewrite
  • ModRewrite_ContentController
  • ModRewrite_ContentExpertController
  • ModRewrite_ContentTestController
  • ModRewrite_ControllerAbstract
  • ModRewriteBase
  • ModRewriteController
  • ModRewriteDebugger
  • ModRewriteTest
  • ModRewriteUrlStack
  • ModRewriteUrlUtil

Functions

  • mr_arrayValue
  • mr_buildGeneratedCode
  • mr_buildNewUrl
  • mr_conCopyArtLang
  • mr_conMoveArticles
  • mr_conSaveArticle
  • mr_conSyncArticle
  • mr_debugOutput
  • mr_getConfiguration
  • mr_getRequest
  • mr_header
  • mr_i18n
  • mr_loadConfiguration
  • mr_queryAndNextRecord
  • mr_removeMultipleChars
  • mr_requestCleanup
  • mr_runFrontendController
  • mr_setClientLanguageId
  • mr_setConfiguration
  • mr_strCopyCategory
  • mr_strMovedownCategory
  • mr_strMoveSubtree
  • mr_strMoveUpCategory
  • mr_strNewCategory
  • mr_strNewTree
  • mr_strRenameCategory
  • mr_strSyncCategory
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
   1: <?php
   2: /**
   3:  * AMR Mod Rewrite helper class
   4:  *
   5:  * @package     Plugin
   6:  * @subpackage  ModRewrite
   7:  * @id          $Id$:
   8:  * @author      Stefan Seifarth / stese
   9:  * @author      Murat Purc <murat@purc.de>
  10:  * @copyright   www.polycoder.de
  11:  * @copyright   four for business AG <www.4fb.de>
  12:  * @license     http://www.contenido.org/license/LIZENZ.txt
  13:  * @link        http://www.4fb.de
  14:  * @link        http://www.contenido.org
  15:  */
  16: 
  17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
  18: 
  19: /**
  20:  * Class to create websafe names, it also provides several helper functions
  21:  *
  22:  * @author      Stefan Seifarth / stese
  23:  * @author      Murat Purc <murat@purc.de>
  24:  * @package     Plugin
  25:  * @subpackage  ModRewrite
  26:  */
  27: class ModRewrite extends ModRewriteBase {
  28: 
  29:     /**
  30:      * Database instance
  31:      *
  32:      * @var  cDb
  33:      */
  34:     private static $_db;
  35: 
  36:     /**
  37:      * Lookup table to cache some internal data such as db query results
  38:      *
  39:      * @var  array
  40:      */
  41:     protected static $_lookupTable;
  42: 
  43:     /**
  44:      * Initialization, is to call at least once, also possible to call multible
  45:      * times, if different client configuration is to load.
  46:      *
  47:      * Loads configuration of passed client and sets some properties.
  48:      *
  49:      * @param  int  $clientId  Client id
  50:      */
  51:     public static function initialize($clientId) {
  52:         mr_loadConfiguration($clientId, true);
  53:         self::$_db = cRegistry::getDb();
  54:         self::$_lookupTable = array();
  55:     }
  56: 
  57:     /**
  58:      * Check categories on websafe name
  59:      *
  60:      * Check all categories in the main parent category on existing same websafe name
  61:      *
  62:      * @param   string  $sName    Websafe name to check
  63:      * @param   int     $iCatId   Current category id
  64:      * @param   int     $iLangId  Current language id
  65:      * @return  bool    True if websafename already exists, false if not
  66:      */
  67:     public static function isInCategories($sName = '', $iCatId = 0, $iLangId = 0) {
  68:         global $cfg;
  69: 
  70:         $sName = self::$_db->escape($sName);
  71:         $iCatId = (int) $iCatId;
  72:         $iLangId = (int) $iLangId;
  73: 
  74:         // get parentid
  75:         $iParentId = 0;
  76:         $sql = "SELECT parentid FROM " . $cfg['tab']['cat'] . " WHERE idcat = " . $iCatId;
  77:         if ($aData = mr_queryAndNextRecord($sql)) {
  78:             $iParentId = ($aData['parentid'] > 0) ? (int) $aData['parentid'] : 0;
  79:         }
  80: 
  81:         // check if websafe name is in this category
  82:         $sql = "SELECT count(cl.idcat) as numcats FROM " . $cfg['tab']['cat_lang'] . " cl "
  83:                 . "LEFT JOIN " . $cfg['tab']['cat'] . " c ON cl.idcat = c.idcat WHERE "
  84:                 . "c.parentid = '$iParentId' AND cl.idlang = " . $iLangId . " AND "
  85:                 . "LOWER(cl.urlname) = LOWER('" . $sName . "') AND cl.idcat <> " . $iCatId;
  86:         ModRewriteDebugger::log($sql, 'ModRewrite::isInCategories $sql');
  87: 
  88:         if ($aData = mr_queryAndNextRecord($sql)) {
  89:             return ($aData['numcats'] > 0) ? true : false;
  90:         }
  91: 
  92:         return false;
  93:     }
  94: 
  95:     /**
  96:      * Check articles on websafe name.
  97:      *
  98:      * Check all articles in the current category on existing same websafe name.
  99:      *
 100:      * @param    string  $sName    Websafe name to check
 101:      * @param    int     $iArtId   Current article id
 102:      * @param    int     $iLangId  Current language id
 103:      * @param   int     $iCatId   Category id
 104:      * @return     bool    True if websafename already exists, false if not
 105:      */
 106:     public static function isInCatArticles($sName = '', $iArtId = 0, $iLangId = 0, $iCatId = 0) {
 107:         global $cfg;
 108: 
 109:         $sName = self::$_db->escape($sName);
 110:         $iArtId = (int) $iArtId;
 111:         $iLangId = (int) $iLangId;
 112:         $iCatId = (int) $iCatId;
 113: 
 114:         // handle multipages
 115:         if ($iCatId == 0) {
 116:             // get category id if not set
 117:             $sql = "SELECT idcat FROM " . $cfg['tab']['cat_art'] . " WHERE idart = " . $iArtId;
 118:             if ($aData = mr_queryAndNextRecord($sql)) {
 119:                 $iCatId = ($aData['idcat'] > 0) ? (int) $aData['idcat'] : 0;
 120:             }
 121:         }
 122: 
 123:         // check if websafe name is in this category
 124:         $sql = "SELECT count(al.idart) as numcats FROM " . $cfg['tab']['art_lang'] . " al "
 125:                 . "LEFT JOIN " . $cfg['tab']['cat_art'] . " ca ON al.idart = ca.idart WHERE "
 126:                 . " ca.idcat='$iCatId' AND al.idlang=" . $iLangId . " AND "
 127:                 . "LOWER(al.urlname) = LOWER('" . $sName . "') AND al.idart <> " . $iArtId;
 128:         if ($aData = mr_queryAndNextRecord($sql)) {
 129:             return ($aData['numcats'] > 0) ? true : false;
 130:         }
 131: 
 132:         return false;
 133:     }
 134: 
 135:     /**
 136:      * Set websafe name in article list.
 137:      *
 138:      * Insert new websafe name in article list
 139:      *
 140:      * @param   string  $sName    Original name (will be converted)
 141:      * @param   int     $iArtId   Current article id
 142:      * @param   int     $iLangId  Current language id
 143:      * @param   int     $iCatId   Category id
 144:      * @return     bool    True if insert was successfully
 145:      */
 146:     public static function setArtWebsafeName($sName = '', $iArtId = 0, $iLangId = 0, $iCatId = 0) {
 147:         global $cfg;
 148: 
 149:         $iArtId = (int) $iArtId;
 150:         $iLangId = (int) $iLangId;
 151:         $iCatId = (int) $iCatId;
 152: 
 153:         // get websafe name
 154:         $sNewName = cString::cleanURLCharacters(conHtmlEntityDecode($sName));
 155: 
 156:         // remove double or more separators
 157:         $sNewName = mr_removeMultipleChars('-', $sNewName);
 158: 
 159:         $sNewName = self::$_db->escape($sNewName);
 160: 
 161:         // check if websafe name already exists
 162:         if (self::isInCatArticles($sNewName, $iArtId, $iLangId, $iCatId)) {
 163:             // create new websafe name if exists
 164:             $sNewName = $sNewName . $iArtId;
 165:         }
 166: 
 167:         // check again - and set name
 168:         if (!self::isInCatArticles($sNewName, $iArtId, $iLangId, $iCatId)) {
 169:             // insert websafe name in article list
 170:             $sql = "UPDATE " . $cfg['tab']['art_lang'] . " SET urlname = '$sNewName' "
 171:                     . "WHERE idart = " . $iArtId . " AND idlang = " . $iLangId;
 172:             return self::$_db->query($sql);
 173:         } else {
 174:             return false;
 175:         }
 176:     }
 177: 
 178:     /**
 179:      * Set websafe name in category list.
 180:      *
 181:      * Insert new websafe name in category list.
 182:      *
 183:      * @param   string  $sName    Original name (will be converted) or alias
 184:      * @param   int     $iCatId   Category id
 185:      * @param   int     $iLangId  Language id
 186:      * @return  bool    True if insert was successfully
 187:      */
 188:     public static function setCatWebsafeName($sName = '', $iCatId = 0, $iLangId = 0) {
 189:         global $cfg;
 190: 
 191:         $iCatId = (int) $iCatId;
 192:         $iLangId = (int) $iLangId;
 193: 
 194:         // create websafe name
 195:         $sNewName = cString::cleanURLCharacters(conHtmlEntityDecode($sName));
 196: 
 197:         // remove double or more separators
 198:         $sNewName = mr_removeMultipleChars('-', $sNewName);
 199: 
 200:         $sNewName = self::$_db->escape($sNewName);
 201: 
 202:         // check if websafe name already exists
 203:         if (self::isInCategories($sNewName, $iCatId, $iLangId)) {
 204:             // create new websafe name if exists
 205:             $sNewName = $sNewName . $iCatId;
 206:         }
 207: 
 208:         // check again - and set name
 209:         if (!self::isInCategories($sNewName, $iCatId, $iLangId)) {
 210:             // insert websafe name in article list
 211:             $sql = "UPDATE " . $cfg['tab']['cat_lang'] . " SET urlname = '$sNewName' "
 212:                     . "WHERE idcat = " . $iCatId . " AND idlang = " . $iLangId;
 213: 
 214:             ModRewriteDebugger::log(array(
 215:                 'sName' => $sName,
 216:                 'iCatId' => $iCatId,
 217:                 'iLangId' => $iLangId,
 218:                 'sNewName' => $sNewName
 219:             ), 'ModRewrite::setCatWebsafeName $data');
 220: 
 221:             return self::$_db->query($sql);
 222:         } else {
 223:             return false;
 224:         }
 225:     }
 226: 
 227:     /**
 228:      * Set urlpath of category
 229:      *
 230:      * @param   int     $iCatId   Category id
 231:      * @param   int     $iLangId  Language id
 232:      * @return  bool    True if insert was successfully
 233:      */
 234:     public static function setCatUrlPath($iCatId = 0, $iLangId = 0) {
 235:         global $cfg;
 236: 
 237:         $sPath = self::buildRecursivPath($iCatId, $iLangId);
 238: 
 239:         $iCatId = (int) $iCatId;
 240:         $iLangId = (int) $iLangId;
 241: 
 242:         // insert websafe name in article list
 243:         $sql = "UPDATE " . $cfg['tab']['cat_lang'] . " SET urlpath = '$sPath' "
 244:                 . "WHERE idcat = " . $iCatId . " AND idlang = " . $iLangId;
 245: 
 246:         ModRewriteDebugger::log(array(
 247:             'iCatId' => $iCatId,
 248:             'iLangId' => $iLangId,
 249:             'sPath' => $sPath
 250:         ), 'ModRewrite::setCatUrlPath $data');
 251: 
 252:         return self::$_db->query($sql);
 253:     }
 254: 
 255:     /**
 256:      * Get article id and language id from article language id
 257:      *
 258:      * @param   int    $iArtlangId  Current article id
 259:      * @return  array  Array with idart and idlang of current article
 260:      */
 261:     public static function getArtIdByArtlangId($iArtlangId = 0) {
 262:         global $cfg;
 263: 
 264:         $iArtlangId = (int) $iArtlangId;
 265:         $sql = "SELECT idart, idlang FROM " . $cfg['tab']['art_lang'] . " WHERE idartlang = " . $iArtlangId;
 266:         if ($aData = mr_queryAndNextRecord($sql)) {
 267:             return $aData;
 268:         }
 269:         return array();
 270:     }
 271: 
 272:     /**
 273:      * Get article id by article websafe name
 274:      *
 275:      * @param   string    $sArtName  Websafe name
 276:      * @param   int       $iCatId    Category id
 277:      * @param   int       $iLangId   Language id
 278:      * @return  int|NULL  Recent article id or NULL
 279:      */
 280:     public static function getArtIdByWebsafeName($sArtName = '', $iCatId = 0, $iLangId = 0) {
 281:         global $cfg, $lang;
 282: 
 283:         $sArtName = self::$_db->escape($sArtName);
 284:         $iCatId = (int) $iCatId;
 285:         $iLangId = (int) $iLangId;
 286:         if (0 === $iLangId && is_int($lang)) {
 287:             $iLangId = $lang;
 288:         }
 289: 
 290:         $sWhere = '';
 291:         if ($iLangId !== 0) {
 292:             $sWhere = ' AND al.idlang = ' . $iLangId;
 293:         }
 294:         // only article name were given
 295:         if ($iCatId == 0) {
 296:             // get all basic category ids with parentid=0
 297:             $aCatIds = array();
 298:             $sql = "SELECT idcat FROM " . $cfg['tab']['cat'] . " WHERE parentid = 0";
 299:             self::$_db->query($sql);
 300:             while (self::$_db->nextRecord()) {
 301:                 $aCatIds[] = "idcat = " . (int) self::$_db->f('idcat');
 302:             }
 303:             $sWhere .= " AND (" . join(" OR ", $aCatIds) . ")";
 304:         } else {
 305:             $sWhere .= " AND ca.idcat = " . $iCatId;
 306:         }
 307: 
 308:         $sql = "SELECT al.idart FROM " . $cfg['tab']['art_lang'] . " al "
 309:                 . "LEFT JOIN " . $cfg['tab']['cat_art'] . " ca ON al.idart = ca.idart "
 310:                 . "WHERE LOWER(al.urlname) = LOWER('$sArtName')" . $sWhere;
 311: 
 312:         if ($aData = mr_queryAndNextRecord($sql)) {
 313:             return $aData['idart'];
 314:         } else {
 315:             return NULL;
 316:         }
 317:     }
 318: 
 319:     /**
 320:      * Get category name from category id and language id.
 321:      *
 322:      * @param   int     $iCatId   Category id
 323:      * @param   int     $iLangId  Language id
 324:      * @return  string  Category name
 325:      */
 326:     public static function getCatName($iCatId = 0, $iLangId = 0) {
 327:         global $cfg;
 328: 
 329:         $iCatId = (int) $iCatId;
 330:         $iLangId = (int) $iLangId;
 331:         $key = 'catname_by_catid_idlang_' . $iCatId . '_' . $iLangId;
 332: 
 333:         if (isset(self::$_lookupTable[$key])) {
 334:             return self::$_lookupTable[$key];
 335:         }
 336: 
 337:         $sql = "SELECT name FROM " . $cfg['tab']['cat_lang'] . " WHERE idcat = " . $iCatId . " AND idlang = " . $iLangId;
 338:         if ($aData = mr_queryAndNextRecord($sql)) {
 339:             $catName = $aData['name'];
 340:         } else {
 341:             $catName = '';
 342:         }
 343: 
 344:         self::$_lookupTable[$key] = $catName;
 345:         return $catName;
 346:     }
 347: 
 348:     /**
 349:      * Funcion to return cat id by path.
 350:      *
 351:      * Caches the paths at first call to provode faster processing at further calls.
 352:      *
 353:      * @param   string  $path  Category path
 354:      * @return  int  Category id
 355:      */
 356:     public static function getCatIdByUrlPath($path) {
 357:         global $cfg, $client, $lang;
 358: 
 359:         if (strpos($path, '/') === 0) {
 360:             $path = substr($path, 1);
 361:         }
 362:         if (strrpos($path, '/') === strlen($path) - 1) {
 363:             $path = substr($path, 0, -1);
 364:         }
 365: 
 366:         $catSeperator = '/';
 367:         $startFromRoot = parent::getConfig('startfromroot');
 368:         $urls2lowercase = parent::getConfig('use_lowercase_uri');
 369: 
 370:         $path = str_replace('/', parent::getConfig('category_seperator'), $path);
 371: 
 372:         $key = 'cat_ids_and_urlpath_' . $client . '_' . $lang;
 373: 
 374:         if (isset(self::$_lookupTable[$key])) {
 375:             $aPathsCache = self::$_lookupTable[$key];
 376:         } else {
 377:             $aPathsCache = array();
 378:         }
 379: 
 380:         if (count($aPathsCache) == 0) {
 381:             $sql = "SELECT cl.idcat, cl.urlpath FROM " . $cfg['tab']['cat_lang']
 382:                     . " AS cl, " . $cfg['tab']['cat'] . " AS c WHERE c.idclient = " . $client
 383:                     . " AND c.idcat = cl.idcat AND cl.idlang = " . $lang;
 384: 
 385:             self::$_db->query($sql);
 386:             while (self::$_db->nextRecord()) {
 387:                 $urlPath = self::$_db->f('urlpath');
 388:                 if ($startFromRoot == 0 && strpos($urlPath, $catSeperator) > 0) {
 389:                     // paths are stored with prefixed main category, but created
 390:                     // urls doesn't contain the main cat, remove it...
 391:                     $urlPath = substr($urlPath, strpos($urlPath, $catSeperator) + 1);
 392:                 }
 393:                 if ($urls2lowercase) {
 394:                     $urlPath = strtolower($urlPath);
 395:                 }
 396: 
 397:                 // store path
 398:                 $aPathsCache[self::$_db->f('idcat')] = $urlPath;
 399:             }
 400:         }
 401:         self::$_lookupTable[$key] = $aPathsCache;
 402: 
 403:         // compare paths using the similar_text algorithm
 404:         $fPercent = 0;
 405:         $aResults = array();
 406:         foreach ($aPathsCache as $id => $pathItem) {
 407:             similar_text($path, $pathItem, $fPercent);
 408:             $aResults[$id] = $fPercent;
 409:         }
 410: 
 411:         arsort($aResults, SORT_NUMERIC);
 412:         reset($aResults);
 413: 
 414:         ModRewriteDebugger::add($path, 'ModRewrite::getCatIdByUrlPath() $path');
 415:         ModRewriteDebugger::add($aPathsCache, 'ModRewrite::getCatIdByUrlPath() $aPathsCache');
 416:         ModRewriteDebugger::add($aResults, 'ModRewrite::getCatIdByUrlPath() $aResults');
 417: 
 418:         $iMinPercentage = (int) parent::getConfig('category_resolve_min_percentage', 0);
 419:         $catId = key($aResults);
 420:         if ($iMinPercentage > 0 && $aResults[$catId] < $iMinPercentage) {
 421:             return 0;
 422:         } else {
 423:             return $catId;
 424:         }
 425:     }
 426: 
 427:     /**
 428:      * Get article name from article id and language id
 429:      *
 430:      * @NOTE: seems to be not used???
 431:      *
 432:      * @param   int     $iArtId   Article id
 433:      * @param   int     $iLangId  Language id
 434:      * @return  string  Article name
 435:      */
 436:     public static function getArtTitle($iArtId = 0, $iLangId = 0) {
 437:         global $cfg;
 438: 
 439:         $iArtId = (int) $iArtId;
 440:         $iLangId = (int) $iLangId;
 441: 
 442:         $sql = "SELECT title FROM " . $cfg['tab']['art_lang'] . " WHERE "
 443:                 . "idart = " . $iArtId . " AND idlang = " . $iLangId;
 444:         if ($aData = mr_queryAndNextRecord($sql)) {
 445:             return $aData['title'];
 446:         }
 447:         return '';
 448:     }
 449: 
 450:     /**
 451:      * Get language ids from category id
 452:      *
 453:      * @param   int    $iCatId  Category id
 454:      * @return  array  Used language ids
 455:      */
 456:     public static function getCatLanguages($iCatId = 0) {
 457:         global $cfg;
 458: 
 459:         $iCatId = (int) $iCatId;
 460:         $key = 'cat_idlang_by_catid_' . $iCatId;
 461: 
 462:         if (isset(self::$_lookupTable[$key])) {
 463:             return self::$_lookupTable[$key];
 464:         }
 465: 
 466:         $aLanguages = array();
 467: 
 468:         $sql = "SELECT idlang FROM " . $cfg['tab']['cat_lang'] . " WHERE idcat = " . $iCatId;
 469:         self::$_db->query($sql);
 470:         while (self::$_db->nextRecord()) {
 471:             $aLanguages[] = self::$_db->f('idlang');
 472:         }
 473: 
 474:         self::$_lookupTable[$key] = $aLanguages;
 475:         return $aLanguages;
 476:     }
 477: 
 478:     /**
 479:      * Get article urlname and language id
 480:      *
 481:      * @param   int    $iArtlangId  idartlang
 482:      * @return  array  Urlname, idlang of empty array
 483:      */
 484:     public static function getArtIds($iArtlangId = 0) {
 485:         global $cfg;
 486: 
 487:         $iArtlangId = (int) $iArtlangId;
 488:         $sql = "SELECT urlname, idlang FROM " . $cfg['tab']['art_lang'] . " WHERE idartlang = " . $iArtlangId;
 489:         if ($aData = mr_queryAndNextRecord($sql)) {
 490:             return $aData;
 491:         }
 492:         return array();
 493:     }
 494: 
 495:     /**
 496:      * Build a recursiv path for mod_rewrite rule like server directories
 497:      * (dir1/dir2/dir3)
 498:      *
 499:      * @param   int     $iCatId   Latest category id
 500:      * @param   int     $iLangId  Language id
 501:      * @param   int     $iLastId  Last category id
 502:      * @return     string    linkpath with correct uri
 503:      */
 504:     public static function buildRecursivPath($iCatId = 0, $iLangId = 0, $iLastId = 0) {
 505:         global $cfg;
 506: 
 507:         $aDirectories = array();
 508:         $bFinish = false;
 509:         $iTmpCatId = (int) $iCatId;
 510:         $iLangId = (int) $iLangId;
 511:         $iLastId = (int) $iLastId;
 512: 
 513:         while ($bFinish == false) {
 514:             $sql = "SELECT cl.urlname, c.parentid FROM " . $cfg['tab']['cat_lang'] . " cl "
 515:                     . "LEFT JOIN " . $cfg['tab']['cat'] . " c ON cl.idcat = c.idcat "
 516:                     . "WHERE cl.idcat = " . $iTmpCatId . " AND cl.idlang = " . $iLangId;
 517:             if ($aData = mr_queryAndNextRecord($sql)) {
 518:                 $aDirectories[] = $aData['urlname'];
 519:                 $iTmpCatId = (int) $aData['parentid'];
 520: 
 521:                 if ($aData['parentid'] == 0 || $aData['parentid'] == $iLastId) {
 522:                     $bFinish = true;
 523:                 }
 524:             } else {
 525:                 $bFinish = true;
 526:             }
 527:         }
 528: 
 529:         // reverse array entries and create directory string
 530:         $sPath = join('/', array_reverse($aDirectories));
 531: 
 532:         return $sPath;
 533:     }
 534: 
 535:     /**
 536:      * Return full CONTENIDO url from single anchor
 537:      *
 538:      * @param   array   $aMatches [0] = complete anchor, [1] = pre arguments, [2] = anchor name, [3] = post arguments
 539:      * @return  string  New anchor
 540:      */
 541:     public static function rewriteHtmlAnchor(array $aMatches = array()) {
 542:         global $artname, $sess, $idart, $idcat, $client, $lang;
 543: 
 544:         // set article name
 545:         $sArtParam = '';
 546:         if (isset($artname) && strlen($artname) > 0) {
 547:             $sArtParam = '&idart=' . (int) $idart;
 548:         }
 549: 
 550:         // check for additional parameter in url
 551:         $aParamsToIgnore = array('idcat', 'idart', 'lang', 'client', 'idcatart', 'changelang', 'changeclient', 'idartlang', 'parts', 'artname');
 552:         $sOtherParams = '';
 553: 
 554:         if (isset($_GET) && count($_GET) > 0) {
 555:             foreach ($_GET as $key => $value) {
 556:                 if (!in_array($key, $aParamsToIgnore) && strlen(trim($value)) > 0) {
 557:                     $aNoAnchor = explode('#', $value);
 558:                     $sOtherParams .= '&' . urlencode(urldecode($key)) . '=' . urlencode(urldecode($value));
 559:                 }
 560:             }
 561:         }
 562: 
 563:         $url = $sess->url(
 564:                 'front_content.php?' . 'idcat=' . (int) $idcat . '&client=' . (int) $client .
 565:                 '&changelang=' . (int) $lang . $sArtParam . $sOtherParams . '#' . $aMatches[2]
 566:         );
 567: 
 568:         $sNewUrl = '<a' . $aMatches[1] . 'href="' . $url . '"' . $aMatches[3] . '>';
 569: 
 570:         return $sNewUrl;
 571:     }
 572: 
 573:     /**
 574:      * Return full CONTENIDO url from single anchor
 575:      *
 576:      * @param   array   $aMatches [0] = complete anchor, [1] = pre arguments, [2] = anchor name, [3] = post arguments
 577:      * @param   bool    $bXHTML  Flag to return XHTML valid url
 578:      * @return  string  New anchor
 579:      */
 580:     public static function contenidoHtmlAnchor(array $aMatches = array(), $bXHTML = true) {
 581:         global $sess;
 582: 
 583:         $aParams = array();
 584:         $sAmpersand = $bXHTML ? '&amp;' : '&';
 585: 
 586:         foreach ($_GET as $key => $value) {
 587:             $aNoAnchor = explode('#', $value);
 588:             $aParams[] = urlencode(urldecode($key)) . '=' . urlencode(urldecode($aNoAnchor[0]));
 589:         }
 590: 
 591:         $url = $sess->url('front_content.php?' . implode($sAmpersand, $aParams) . '#' . $aMatches[2]);
 592:         $sNewUrl = '<a' . $aMatches[1] . 'href="' . $url . '"' . $aMatches[3] . '>';
 593: 
 594:         return $sNewUrl;
 595:     }
 596: 
 597:     /**
 598:      * Get article websafe name from article id and language id.
 599:      *
 600:      * @param    int     $iArtId   Article id
 601:      * @param    int     $iLangId  Language id
 602:      * @return     string    Article websafe name
 603:      */
 604:     public static function getArtWebsafeName($iArtId = 0, $iLangId = 0) {
 605:         global $cfg;
 606: 
 607:         $iArtId = (int) $iArtId;
 608:         $iLangId = (int) $iLangId;
 609:         $sql = "SELECT urlname FROM " . $cfg['tab']['art_lang'] . " WHERE "
 610:                 . "idart = " . $iArtId . " AND idlang = " . $iLangId;
 611:         if ($aData = mr_queryAndNextRecord($sql)) {
 612:             return $aData['urlname'];
 613:         }
 614:         return NULL;
 615:     }
 616: 
 617:     /**
 618:      * Get article websafe name from idartlang.
 619:      *
 620:      * @param    int     $iArtLangId  idartlang
 621:      * @return     string    Article websafe name
 622:      */
 623:     public static function getArtLangWebsafeName($iArtLangId = 0) {
 624:         global $cfg;
 625: 
 626:         $iArtLangId = (int) $iArtLangId;
 627:         $sql = "SELECT urlname FROM " . $cfg['tab']['art_lang'] . " WHERE idartlang = " . $iArtLangId;
 628:         if ($aData = mr_queryAndNextRecord($sql)) {
 629:             return $aData['urlname'];
 630:         }
 631:         return NULL;
 632:     }
 633: 
 634:     /**
 635:      * Get name of client by id.
 636:      *
 637:      * @param   int     $clientId  Client id
 638:      * @return  string  Client name
 639:      */
 640:     public static function getClientName($clientId = 0) {
 641:         global $cfg;
 642: 
 643:         $clientId = (int) $clientId;
 644:         $key = 'clientname_by_clientid_' . $clientId;
 645: 
 646:         if (isset(self::$_lookupTable[$key])) {
 647:             return self::$_lookupTable[$key];
 648:         }
 649: 
 650:         $sql = "SELECT name FROM " . $cfg['tab']['clients'] . " WHERE idclient = " . $clientId;
 651:         if ($aData = mr_queryAndNextRecord($sql)) {
 652:             $clientName = $aData['name'];
 653:         } else {
 654:             $clientName = '';
 655:         }
 656: 
 657:         self::$_lookupTable[$key] = $clientName;
 658:         return $clientName;
 659:     }
 660: 
 661:     /**
 662:      * Get client id from client name
 663:      *
 664:      * @param   string   $sClientName  Client name
 665:      * @return  int  Client id
 666:      */
 667:     public static function getClientId($sClientName = '') {
 668:         global $cfg;
 669: 
 670:         $sClientName = strtolower(self::$_db->escape($sClientName));
 671:         $key = 'clientid_by_name_' . $sClientName;
 672: 
 673:         if (isset(self::$_lookupTable[$key])) {
 674:             return self::$_lookupTable[$key];
 675:         }
 676: 
 677:         $sql = "SELECT idclient FROM " . $cfg['tab']['clients'] . " WHERE LOWER(name) = '" . $sClientName . "' OR LOWER(name) = '" . urldecode($sClientName) . "'";
 678: 
 679:         if ($aData = mr_queryAndNextRecord($sql)) {
 680:             $clientId = $aData['idclient'];
 681:         } else {
 682:             $clientId = false;
 683:         }
 684: 
 685:         self::$_lookupTable[$key] = $clientId;
 686:         return $clientId;
 687:     }
 688: 
 689:     /**
 690:      * Checks if client id exists
 691:      *
 692:      * @param   int  $clientId
 693:      * @return  bool
 694:      */
 695:     public static function clientIdExists($clientId) {
 696:         global $cfg;
 697: 
 698:         $clientId = (int) $clientId;
 699:         $key = 'clientid_exists_' . $clientId;
 700: 
 701:         if (isset(self::$_lookupTable[$key])) {
 702:             return self::$_lookupTable[$key];
 703:         }
 704: 
 705:         $sql = "SELECT idclient FROM " . $cfg['tab']['clients'] . " WHERE idclient = " . $clientId;
 706:         if ($aData = mr_queryAndNextRecord($sql)) {
 707:             $exists = true;
 708:         } else {
 709:             $exists = false;
 710:         }
 711: 
 712:         self::$_lookupTable[$key] = $exists;
 713:         return $exists;
 714:     }
 715: 
 716:     /**
 717:      * Returns name of language by id.
 718:      *
 719:      * @param   int     $languageId  Language id
 720:      * @return  string  Lanuage name
 721:      */
 722:     public static function getLanguageName($languageId = 0) {
 723:         global $cfg;
 724: 
 725:         $languageId = (int) $languageId;
 726:         $key = 'languagename_by_id_' . $languageId;
 727: 
 728:         if (isset(self::$_lookupTable[$key])) {
 729:             return self::$_lookupTable[$key];
 730:         }
 731: 
 732:         $sql = "SELECT name FROM " . $cfg['tab']['lang'] . " WHERE idlang = " . $languageId;
 733:         if ($aData = mr_queryAndNextRecord($sql)) {
 734:             $languageName = $aData['name'];
 735:         } else {
 736:             $languageName = '';
 737:         }
 738: 
 739:         self::$_lookupTable[$key] = $languageName;
 740:         return $languageName;
 741:     }
 742: 
 743:     /**
 744:      * Checks if language id exists
 745:      *
 746:      * @param   int     $languageId  Language id
 747:      * @return  bool
 748:      */
 749:     public static function languageIdExists($languageId) {
 750:         global $cfg;
 751: 
 752:         $languageId = (int) $languageId;
 753:         $key = 'languageid_exists_' . $languageId;
 754: 
 755:         if (isset(self::$_lookupTable[$key])) {
 756:             return self::$_lookupTable[$key];
 757:         }
 758: 
 759:         $sql = "SELECT idlang FROM " . $cfg['tab']['lang'] . " WHERE idlang = " . $languageId;
 760:         if ($aData = mr_queryAndNextRecord($sql)) {
 761:             $exists = true;
 762:         } else {
 763:             $exists = false;
 764:         }
 765: 
 766:         self::$_lookupTable[$key] = $exists;
 767:         return $exists;
 768:     }
 769: 
 770:     /**
 771:      * Get language id from language name thanks to Nicolas Dickinson for multi
 772:      * Client/Language BugFix
 773:      *
 774:      * @param  string   $sLanguageName  Language name
 775:      * @param  int      $iClientId      Client id
 776:      * @return int  Language id
 777:      */
 778:     public static function getLanguageId($sLanguageName = '', $iClientId = 1) {
 779:         global $cfg;
 780: 
 781:         $sLanguageName = strtolower(self::$_db->escape($sLanguageName));
 782:         $iClientId = (int) $iClientId;
 783:         $key = 'langid_by_langname_clientid_' . $sLanguageName . '_' . $iClientId;
 784: 
 785:         if (isset(self::$_lookupTable[$key])) {
 786:             return self::$_lookupTable[$key];
 787:         }
 788: 
 789:         $sql = "SELECT l.idlang FROM " . $cfg['tab']['lang'] . " as l "
 790:                 . "LEFT JOIN " . $cfg['tab']['clients_lang'] . " AS cl ON l.idlang = cl.idlang "
 791:                 . "WHERE cl.idclient = " . $iClientId . " AND (LOWER(l.name) = '" . $sLanguageName . "' "
 792:                 . "OR LOWER(l.name) = '" . urldecode($sLanguageName) . "')";
 793:         if ($aData = mr_queryAndNextRecord($sql)) {
 794:             $languageId = $aData['idlang'];
 795:         } else {
 796:             $languageId = 0;
 797:         }
 798: 
 799:         self::$_lookupTable[$key] = $languageId;
 800:         return $languageId;
 801:     }
 802: 
 803:     /**
 804:      * Splits passed argument into scheme://host and path/query.
 805:      *
 806:      * Example:
 807:      * input  = http://host/front_content.php?idcat=123
 808:      * return = array('htmlpath' => 'http://host', 'url' => 'front_content.php?idcat=123')
 809:      *
 810:      * @param  string  $url  URL to split
 811:      * @return array   Assoziative array including the two parts:
 812:      *                 - array('htmlpath' => $path, 'url' => $url)
 813:      */
 814:     public static function getClientFullUrlParts($url) {
 815:         $clientPath = cRegistry::getFrontendUrl();
 816: 
 817:         if (stristr($url, $clientPath) !== false) {
 818:             // url includes full html path (scheme host path, etc.)
 819:             $url = str_replace($clientPath, '', $url);
 820:             $htmlPath = $clientPath;
 821:             $aComp = parse_url($htmlPath);
 822: 
 823:             // check if path matches to defined rootdir from mod_rewrite conf
 824:             if (isset($aComp['path']) && $aComp['path'] !== parent::getConfig('rootdir')) {
 825:                 // replace not matching path agaings configured one
 826:                 // this will replace e. g. "http://host/cms/" against "http://host/"
 827:                 $htmlPath = str_replace($aComp['path'], parent::getConfig('rootdir'), $htmlPath);
 828:                 if (substr($htmlPath, strlen($htmlPath) - 1) == '/') {
 829:                     // remove last slash
 830:                     $htmlPath = substr($htmlPath, 0, strlen($htmlPath) - 1);
 831:                 }
 832:             }
 833:         } else {
 834:             $htmlPath = '';
 835:         }
 836:         return array('htmlpath' => $htmlPath, 'url' => $url);
 837:     }
 838: 
 839:     /**
 840:      * Function to preclean a url.
 841:      *
 842:      * Removes absolute path declaration '/front_content.php' or relative path
 843:      * definition to actual dir './front_content.php', ampersand entities '&amp;'
 844:      * and returns a url like 'front_content.php?idart=12&idlang=1'
 845:      *
 846:      * @param   string  $url  Url to clean
 847:      * @return  string  Cleaned Url
 848:      */
 849:     public static function urlPreClean($url) {
 850:         // some preparation of different front_content.php occurence
 851:         if (strpos($url, './front_content.php') === 0) {
 852:             $url = str_replace('./front_content.php', 'front_content.php', $url);
 853:         } elseif (strpos($url, '/front_content.php') === 0) {
 854:             $url = str_replace('/front_content.php', 'front_content.php', $url);
 855:         }
 856:         $url = str_replace('&amp;', '&', $url);
 857:         return $url;
 858:     }
 859: 
 860:     /**
 861:      * Recreates all or only empty aliases in categories table.
 862:      *
 863:      * @param  bool  $bOnlyEmpty  Flag to reset only empty items
 864:      */
 865:     public static function recreateCategoriesAliases($bOnlyEmpty = false) {
 866:         global $cfg;
 867: 
 868:         $db = cRegistry::getDb();
 869: 
 870:         $aCats = array();
 871: 
 872:         // get all or only empty categories
 873:         $sql = "SELECT name, idcat, idlang FROM " . $cfg['tab']['cat_lang'];
 874:         if ($bOnlyEmpty === true) {
 875:             $sql .= " WHERE urlname IS NULL OR urlname = '' OR urlpath IS NULL OR urlpath = ''";
 876:         }
 877: 
 878:         $db->query($sql);
 879:         while ($db->nextRecord()) {
 880:             //set new alias
 881:             self::setCatWebsafeName($db->f('name'), $db->f('idcat'), $db->f('idlang'));
 882:             $aCats[] = array('idcat' => $db->f('idcat'), 'idlang' => $db->f('idlang'));
 883:         }
 884: 
 885:         foreach ($aCats as $p => $item) {
 886:             self::setCatUrlPath($item['idcat'], $item['idlang']);
 887:         }
 888: 
 889:         unset($db, $aCats);
 890:     }
 891: 
 892:     /**
 893:      * Returns list of all empty category aliases
 894:      *
 895:      * @param bool $bOnlyNumber
 896:      * @return array|int
 897:      */
 898:     public static function getEmptyCategoriesAliases($bOnlyNumber = true) {
 899:         global $cfg;
 900: 
 901:         $db = cRegistry::getDb();
 902:         $return = ($bOnlyNumber) ? 0 : array();
 903: 
 904:         // get all empty categories
 905:         $sql = "SELECT name, idcat, idlang FROM " . $cfg['tab']['cat_lang'];
 906:         $sql .= " WHERE urlname IS NULL OR urlname = '' OR urlpath IS NULL OR urlpath = ''";
 907: 
 908:         $db->query($sql);
 909: 
 910:         if ($bOnlyNumber) {
 911:             $return = (int) $db->numRows();
 912:         } else {
 913:             while ($db->nextRecord()) {
 914:                 $return[] = array($db->f('name'), $db->f('idcat'), $db->f('idlang'));
 915:             }
 916:         }
 917: 
 918:         unset($db);
 919:         return $return;
 920:     }
 921: 
 922:     /**
 923:      * Recreates all or only empty urlname entries in art_lang table.
 924:      *
 925:      * @param  bool  $bOnlyEmpty  Flag to reset only empty items
 926:      */
 927:     public static function recreateArticlesAliases($bOnlyEmpty = false) {
 928:         global $cfg;
 929: 
 930:         $db = cRegistry::getDb();
 931: 
 932:         // get all or only empty articles
 933:         $sql = "SELECT title, idart, idlang FROM " . $cfg['tab']['art_lang'];
 934:         if ($bOnlyEmpty === true) {
 935:             $sql .= " WHERE urlname IS NULL OR urlname = ''";
 936:         }
 937:         $db->query($sql);
 938: 
 939:         while ($db->nextRecord()) {
 940:             //set new alias
 941:             self::setArtWebsafeName($db->f('title'), $db->f('idart'), $db->f('idlang'));
 942:         }
 943: 
 944:         unset($db);
 945:     }
 946: 
 947:     /**
 948:      * Returns list of all empty article aliases
 949:      *
 950:      * @param bool $bOnlyNumber
 951:      * @return array|int
 952:      */
 953:     public static function getEmptyArticlesAliases($bOnlyNumber = true) {
 954:         global $cfg;
 955: 
 956:         $db = cRegistry::getDb();
 957:         $return = ($bOnlyNumber) ? 0 : array();
 958: 
 959:         // get all empty articles
 960:         $sql  = "SELECT title, idart, idlang FROM " . $cfg['tab']['art_lang'];
 961:         $sql .= " WHERE urlname IS NULL OR urlname = ''";
 962: 
 963:         $db->query($sql);
 964:         if ($bOnlyNumber) {
 965:             $return = (int) $db->numRows();
 966:         } else {
 967:             while ($db->nextRecord()) {
 968:                 $return[] = array($db->f('title'), $db->f('idart'), $db->f('idlang'));
 969:             }
 970:         }
 971: 
 972:         unset($db);
 973:         return $return;
 974:     }
 975: 
 976:     /**
 977:      * Method to reset all aliases (categories and articles).
 978:      *
 979:      * Shortcut to recreateCategoriesAliases() and recreateArticlesAliases()
 980:      */
 981:     public static function resetAliases() {
 982:         self::recreateCategoriesAliases();
 983:         self::recreateArticlesAliases();
 984:     }
 985: 
 986:     /**
 987:      * Recreate all or only empty aliases (categories and articles).
 988:      *
 989:      * Shortcut to recreateCategoriesAliases() and recreateArticlesAliases()
 990:      *
 991:      * @param  bool  $bOnlyEmpty  Flag to reset only empty items
 992:      */
 993:     public static function recreateAliases($bOnlyEmpty = false) {
 994:         self::recreateCategoriesAliases($bOnlyEmpty);
 995:         self::recreateArticlesAliases($bOnlyEmpty);
 996:     }
 997: 
 998:     /**
 999:      * Returns .htaccess related assoziative info array
1000:      *
1001:      * @return  array
1002:      */
1003:     public static function getHtaccessInfo() {
1004: 
1005:         $arr = array(
1006:             'contenido_full_path' => str_replace('\\', '/', realpath(cRegistry::getBackendPath() . '../') . '/'),
1007:             'client_full_path' => cRegistry::getFrontendPath(),
1008:         );
1009:         $arr['in_contenido_path'] = is_file($arr['contenido_full_path'] . '.htaccess');
1010:         $arr['in_client_path'] = is_file($arr['client_full_path'] . '.htaccess');
1011:         $arr['has_htaccess'] = ($arr['in_contenido_path'] || $arr['in_client_path']);
1012: 
1013:         return $arr;
1014:     }
1015: 
1016: }
1017: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0