[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/ -> ezurlalias.php (source)

   1  <?php
   2  //
   3  // Definition of eZURLAlias class
   4  //
   5  // Created on: <01-Aug-2003 16:44:56 bf>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezurlalias.php
  30  */
  31  
  32  /*!
  33    \class eZURLAlias ezurlalias.php
  34    \brief Handles URL aliases in eZ publish
  35  
  36    URL aliases are different names for existing URLs in eZ publish.
  37    Using URL aliases allows for having better looking urls on the webpage
  38    as well as having fixed URLs pointing to various locations.
  39  
  40    This class handles storing, fetching, moving and subtree updates on eZ publish URL aliases.
  41  
  42    Creating a new alias is done by using the create() function and passing the correct parameters.
  43    Fetching an url can either be done with it's ID using fetch() or by it's URL string by using fetchBySourceURL.
  44    To fetch the original URL you must use the translate() function.
  45  */
  46  
  47  include_once ( "kernel/classes/ezpersistentobject.php" );
  48  
  49  define( 'EZURLALIAS_CACHE_FUNCTION', 'eZURLAliasWilcardTranslate' );
  50  
  51  define( 'EZ_URLALIAS_WILDCARD_TYPE_NONE', 0 );
  52  define( 'EZ_URLALIAS_WILDCARD_TYPE_FORWARD', 1 );
  53  define( 'EZ_URLALIAS_WILDCARD_TYPE_DIRECT', 2 );
  54  
  55  class eZURLAlias extends eZPersistentObject
  56  {
  57      /*!
  58       Initializes a new URL alias.
  59      */
  60      function eZURLAlias( $row )
  61      {
  62          $this->eZPersistentObject( $row );
  63      }
  64  
  65      /*!
  66       \reimp
  67      */
  68      function definition()
  69      {
  70          return array( "fields" => array( "id" => array( 'name' => 'ID',
  71                                                          'datatype' => 'integer',
  72                                                          'default' => 0,
  73                                                          'required' => true ),
  74                                           "source_url" => array( 'name' => "SourceURL",
  75                                                                  'datatype' => 'string',
  76                                                                  'default' => '',
  77                                                                  'required' => true ),
  78                                           "source_md5" => array( 'name' => "SourceMD5",
  79                                                                  'datatype' => 'string',
  80                                                                  'default' => '',
  81                                                                  'required' => true ),
  82                                           "destination_url" => array( 'name' => "DestinationURL",
  83                                                                  'datatype' => 'string',
  84                                                                  'default' => '',
  85                                                                  'required' => true ),
  86                                           "is_internal" => array( 'name' => "IsInternal",
  87                                                                   'datatype' => 'integer',
  88                                                                   'default' => '0',
  89                                                                   'required' => true ),
  90                                           "is_wildcard" => array( 'name' => "IsWildcard",
  91                                                                   'datatype' => 'integer',
  92                                                                   'default' => '0',
  93                                                                   'required' => true ),
  94                                           "forward_to_id" => array( 'name' => "ForwardToID",
  95                                                                     'datatype' => 'integer',
  96                                                                     'default' => '0',
  97                                                                     'required' => true ) ),
  98                        "keys" => array( "id" ),
  99                        'function_attributes' => array( 'forward_url' => 'forwardURL' ),
 100                        "increment_key" => "id",
 101                        "class_name" => "eZURLAlias",
 102                        "name" => "ezurlalias" );
 103      }
 104  
 105      /*!
 106       \return the url alias object as an associative array with all the attribute values.
 107      */
 108      function asArray()
 109      {
 110          return array( 'id' => $this->attribute( 'id' ),
 111                        'source_url' => $this->attribute( 'source_url' ),
 112                        'source_md5' => $this->attribute( 'source_md5' ),
 113                        'destination_url' => $this->attribute( 'destination_url' ),
 114                        'is_internal' => $this->attribute( 'is_internal' ),
 115                        'is_wildcard' => $this->attribute( 'is_wildcard' ),
 116                        'forward_to_id' => $this->attribute( 'forward_to_id' ) );
 117      }
 118  
 119      /*!
 120       \return the URL alias object this URL alias points to or \c null if no such URL exists.
 121      */
 122      function &forwardURL()
 123      {
 124          $url = null;
 125          if ( $this->attribute( 'forward_to_id' ) != 0 )
 126              $url = eZURLAlias::fetch( $this->attribute( 'forward_to_id' ) );
 127          else
 128              $url = null;
 129          return $url;
 130      }
 131  
 132      /*!
 133       \static
 134       Creates a new URL alias with the new URL \a $sourceURL and the original URL \a $destinationURL
 135       \param $isInternal decides if the url is internal or not (user created).
 136       \return the URL alias object
 137      */
 138      function create( $sourceURL, $destinationURL, $isInternal = true, $forwardToID = false, $isWildcard = EZ_URLALIAS_WILDCARD_TYPE_NONE )
 139      {
 140          if ( !$forwardToID )
 141              $forwardToID = 0;
 142          $sourceURL = eZURLAlias::cleanURL( $sourceURL );
 143          $destinationURL = eZURLAlias::cleanURL( $destinationURL );
 144          $row = array( 'source_url' => $sourceURL,
 145                        'destination_url' => $destinationURL,
 146                        'is_internal' => $isInternal,
 147                        'is_wildcard' => $isWildcard,
 148                        'forward_to_id' => $forwardToID );
 149          $alias = new eZURLAlias( $row );
 150          return $alias;
 151      }
 152  
 153      /*!
 154       Creates a new URL alias which will forward the to this URL alias, the url which will
 155       be forwarded is \a $forwardURL. The forwarding URL is usually an old url you
 156       want to work with your new and renamed url.
 157       The difference between a forwarding and translation is that forwarding will the browser
 158       and crawlers that the url is no longer in use and give the new one.
 159       \return the URL alias object
 160      */
 161      function &createForForwarding( $forwardURL )
 162      {
 163          $alias = eZURLAlias::create( $forwardURL, $this->attribute( 'destination_url' ),
 164                                        $this->attribute( 'is_internal' ), $this->attribute( 'id' ) );
 165          return $alias;
 166      }
 167  
 168      /*!
 169       Generates the md5 for the alias and stores the values.
 170       \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
 171       the calls within a db transaction; thus within db->begin and db->commit.
 172      */
 173      function store()
 174      {
 175          $this->SourceURL = eZURLAlias::cleanURL( $this->SourceURL );
 176          $this->DestinationURL = eZURLAlias::cleanURL( $this->DestinationURL );
 177          $this->SourceMD5 = md5( $this->SourceURL );
 178          eZPersistentObject::store();
 179      }
 180  
 181      /*!
 182       Removes this url alias as well as all other aliases that relate to it,
 183       for instance forwarding aliases.
 184       \note If you want to remove just this alias you must use remove()
 185      */
 186      function cleanup()
 187      {
 188          $id = $this->attribute( 'id' );
 189          $db =& eZDB::instance();
 190          $db->begin();
 191          $sql = "DELETE FROM ezurlalias WHERE forward_to_id = '" . $db->escapeString( $id ) . "'";
 192          $db->query( $sql );
 193          $this->remove();
 194          $db->commit();
 195      }
 196  
 197      /*!
 198       \static
 199       Makes sure all aliases which are children of the alias \a $oldPathString is updated
 200       to have the correct \a $newPathString.
 201       \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
 202       the calls within a db transaction; thus within db->begin and db->commit.
 203      */
 204      function updateChildAliases( $newPathString, $oldPathString )
 205      {
 206          $oldPathStringLength = strlen( $oldPathString );
 207          $db =& eZDB::instance();
 208          $newPathStringText = $db->escapeString( $newPathString );
 209          $oldPathStringText = $db->escapeString( $oldPathString );
 210          $subStringQueryPart = $db->subString( 'source_url', $oldPathStringLength + 1 );
 211          $newPathStringQueryPart = $db->concatString( array( "'$newPathStringText'", $subStringQueryPart ) );
 212          $sql = "UPDATE ezurlalias
 213  SET
 214      source_url = $newPathStringQueryPart
 215  WHERE
 216      is_wildcard = 0 AND
 217      source_url LIKE '$oldPathStringText/%'";
 218  
 219          $db->begin();
 220          $db->query( $sql );
 221  
 222          $subStringQueryPart = $db->subString( 'source_url', $oldPathStringLength + 1 );
 223          $newPathStringQueryPart = $db->concatString( array( "'$newPathStringText'", $subStringQueryPart ) );
 224          $destSubStringQueryPart = $db->subString( 'destination_url', $oldPathStringLength + 1 );
 225          $newDestPathStringQueryPart = $db->concatString( array( "'$newPathStringText'", $destSubStringQueryPart ) );
 226          $sql = "UPDATE ezurlalias
 227  SET
 228      source_url = $newPathStringQueryPart, destination_url = $newDestPathStringQueryPart
 229  WHERE
 230      is_wildcard != 0 AND
 231      source_url LIKE '$oldPathStringText/%/*' AND
 232      destination_url LIKE '$oldPathStringText/%/{1}'";
 233  
 234          $db->query( $sql );
 235  
 236          $md5QueryPart = $db->md5( 'source_url' );
 237          $sql = "UPDATE ezurlalias
 238  SET
 239      source_md5 = $md5QueryPart
 240  WHERE
 241      source_url like '$newPathStringText%'";
 242  
 243          $db->query( $sql );
 244          $db->commit();
 245      }
 246  
 247      /*!
 248       Removes all wildcards that matches the base URL \a $baseURL.
 249       \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
 250       the calls within a db transaction; thus within db->begin and db->commit.
 251      */
 252      function cleanupWildcards( $baseURL )
 253      {
 254          $db =& eZDB::instance();
 255          $baseURLText = $db->escapeString( $baseURL . "/*" );
 256          $sql = "DELETE FROM ezurlalias
 257  WHERE
 258       source_url = '$baseURLText' AND
 259       is_wildcard IN ( " . EZ_URLALIAS_WILDCARD_TYPE_FORWARD . ", " . EZ_URLALIAS_WILDCARD_TYPE_DIRECT . ")";
 260          $db->query( $sql );
 261      }
 262  
 263      /*!
 264       Removes forwarding urls where source_url match \a $oldURL.
 265       \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
 266       the calls within a db transaction; thus within db->begin and db->commit.
 267      */
 268      function cleanupForwardingURLs( $oldURL )
 269      {
 270          $db =& eZDB::instance();
 271          $oldURLText = $db->escapeString( $oldURL );
 272          $sql = "DELETE FROM ezurlalias
 273  WHERE
 274       source_url = '$oldURLText' AND
 275       forward_to_id != 0";
 276          $db->query( $sql );
 277      }
 278  
 279      /*!
 280       Updates all forwards urls that originally points to \a $oldForwardID
 281       to point to correct url \a $newForardID.
 282       \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
 283       the calls within a db transaction; thus within db->begin and db->commit.
 284      */
 285      function updateForwardID( $newForwardID, $oldForwardID )
 286      {
 287          $db =& eZDB::instance();
 288          $oldForwardIDText = $db->escapeString( $oldForwardID );
 289          $newForwardIDText = $db->escapeString( $newForwardID );
 290          $sql = "UPDATE ezurlalias
 291  SET
 292      forward_to_id = '$newForwardIDText'
 293  WHERE
 294      forward_to_id = '$oldForwardIDText'";
 295  
 296          $db->query( $sql );
 297      }
 298  
 299      /*!
 300       \static
 301        Fetches the URL alias by ID.
 302      */
 303      function fetch( $id, $asObject = true )
 304      {
 305          return eZPersistentObject::fetchObject( eZURLAlias::definition(),
 306                                                  null,
 307                                                  array( "id" => $id ),
 308                                                  $asObject );
 309      }
 310  
 311      /*!
 312       \static
 313        Fetches the URL alias by source URL \a $url.
 314        \param $isInternal boolean which controls whether internal or external urls are fetched.
 315        \param $noForwardID boolean which controls whether to only fetch urls without forward id
 316                            or if forward id it should be ignored.
 317        \return the URL alias object or \c null
 318      */
 319      function fetchBySourceURL( $url, $isInternal = true, $asObject = true, $noForwardID = true )
 320      {
 321          $url = eZURLAlias::cleanURL( $url );
 322          $conditions = array( "source_url" => $url,
 323                               'is_wildcard' => 0,
 324                               'is_internal' => $isInternal );
 325          if ( $noForwardID )
 326              $conditions['forward_to_id'] = 0;
 327          return eZPersistentObject::fetchObject( eZURLAlias::definition(),
 328                                                  null,
 329                                                  $conditions,
 330                                                  $asObject );
 331      }
 332  
 333      /*!
 334       \static
 335        Fetches the URL alias by destination URL \a $url.
 336        \param $isInternal boolean which controls whether internal or external urls are fetched.
 337        \return the URL alias object or \c null
 338      */
 339      function fetchByDestinationURL( $url, $isInternal = true, $asObject = true )
 340      {
 341          $url = eZURLAlias::cleanURL( $url );
 342          $isInternal = $isInternal ? 1 : 0;
 343          return eZPersistentObject::fetchObject( eZURLAlias::definition(),
 344                                                  null,
 345                                                  array( "destination_url" => $url,
 346                                                         'forward_to_id' => 0,
 347                                                         'is_wildcard' => 0,
 348                                                         'is_internal' => $isInternal ),
 349                                                  $asObject );
 350      }
 351  
 352      /*!
 353       \static
 354        Fetches non-internal URL alias by offset and limit
 355      */
 356      function fetchByOffset( $offset, $limit, $asObject = true )
 357      {
 358          return eZPersistentObject::fetchObjectList( eZURLAlias::definition(),
 359                                                      null,
 360                                                      array( "is_internal" => 0 ),
 361                                                      null,
 362                                                      array( 'offset' => $offset, 'length' => $limit ),
 363                                                      $asObject );
 364      }
 365  
 366      /*!
 367       \static
 368        Fetches all wildcards from DB.
 369      */
 370      function fetchWildcards( $asObject = true )
 371      {
 372          return eZPersistentObject::fetchObjectList( eZURLAlias::definition(),
 373                                                      null,
 374                                                      array( "is_wildcard" => array( array( EZ_URLALIAS_WILDCARD_TYPE_FORWARD, EZ_URLALIAS_WILDCARD_TYPE_DIRECT ) ) ),
 375                                                      null,
 376                                                      null,
 377                                                      $asObject );
 378      }
 379  
 380      /*!
 381       \return array with information on the wildcard cache.
 382  
 383       The array containst the following keys
 384       - dir - The directory for the cache
 385       - file - The filename for the cache
 386       - path - The entire path (including filename) for the cache
 387       - keys - Array with key values which is used to uniquely identify the cache
 388      */
 389      function cacheInfo()
 390      {
 391          $cacheDir = eZSys::cacheDirectory();
 392          $ini =& eZINI::instance();
 393          $keys = array( 'implementation' => $ini->variable( 'DatabaseSettings', 'DatabaseImplementation' ),
 394                         'server' => $ini->variable( 'DatabaseSettings', 'Server' ),
 395                         'database' => $ini->variable( 'DatabaseSettings', 'Database' ) );
 396          $wildcardKey = md5( implode( "\n", $keys ) );
 397          $wildcardCacheDir = "$cacheDir/wildcard";
 398          $wildcardCacheFile = "wildcard_$wildcardKey.php";
 399          $wildcardCachePath = "$wildcardCacheDir/$wildcardCacheFile";
 400          return array( 'dir' => $wildcardCacheDir,
 401                        'file' => $wildcardCacheFile,
 402                        'path' => $wildcardCachePath,
 403                        'keys' => $keys );
 404      }
 405  
 406      /*!
 407       Sets the various cache information to the parameters.
 408       \sa cacheInfo
 409      */
 410      function cacheInfoDirectories( &$wildcardCacheDir, &$wildcardCacheFile, &$wildcardCachePath, &$wildcardKeys )
 411      {
 412          $info = eZURLAlias::cacheInfo();
 413          $wildcardCacheDir = $info['dir'];
 414          $wildcardCacheFile = $info['file'];
 415          $wildcardCachePath = $info['path'];
 416          $wildcardKeys = $info['keys'];
 417      }
 418  
 419      /*!
 420       Goes trough all wildcards in the database and creates the wildcard match cache.
 421       \sa cacheInfo
 422      */
 423      function createWildcardMatches()
 424      {
 425          eZURLAlias::cacheInfoDirectories( $wildcardCacheDir, $wildcardCacheFile, $wildcardCachePath, $wildcardKeys );
 426          if ( !file_exists( $wildcardCacheDir ) )
 427          {
 428              eZDir::mkdir( $wildcardCacheDir, eZDir::directoryPermission(), true );
 429          }
 430  
 431          // VS-DBFILE
 432  
 433          include_once ( 'lib/ezutils/classes/ezphpcreator.php' );
 434          $phpCache = new eZPHPCreator( $wildcardCacheDir, $wildcardCacheFile, '', array( 'clustering' => 'wirldcard-cache' ) );
 435  
 436          foreach ( $wildcardKeys as $wildcardKey => $wildcardKeyValue )
 437          {
 438              $phpCache->addComment( "$wildcardKey = $wildcardKeyValue" );
 439          }
 440          $phpCache->addSpace();
 441  
 442          $phpCode = "function " . EZURLALIAS_CACHE_FUNCTION . "( &\$uri, &\$urlAlias )\n{\n";
 443  
 444          $wildcards = eZURLAlias::fetchWildcards();
 445          $counter = 0;
 446          foreach ( $wildcards as $wildcard )
 447          {
 448              $matchWilcard = $wildcard->attribute( 'source_url' );
 449              $matchWilcardList = explode( "*", $matchWilcard );
 450              $matchWildcardCount = count( $matchWilcardList ) - 1;
 451              $regexpList = array();
 452              foreach ( $matchWilcardList as $matchWilcardItem )
 453              {
 454                  $regexpList[] = preg_quote( $matchWilcardItem, '#' );
 455              }
 456              $matchRegexp = implode( '(.*)', $regexpList );
 457  
 458              $replaceWildcard = $wildcard->attribute( 'destination_url' );
 459              $replaceWildcardList = preg_split( "#{([0-9]+)}#", $replaceWildcard, false, PREG_SPLIT_DELIM_CAPTURE );
 460              $regexpList = array();
 461              $replaceCounter = 0;
 462              $replaceCode = "\$uri = ";
 463              foreach ( $replaceWildcardList as $replaceWildcardItem )
 464              {
 465                  if ( $replaceCounter > 0 )
 466                      $replaceCode .= " . ";
 467                  if ( ( $replaceCounter % 2 ) == 0 )
 468                  {
 469                      $replaceWildcardItemText = $phpCache->variableText( $replaceWildcardItem, 0 );
 470                      $replaceCode .= "$replaceWildcardItemText";
 471                  }
 472                  else
 473                  {
 474                      $replaceCode .= "\$matches[$replaceWildcardItem]";
 475                  }
 476                  ++$replaceCounter;
 477              }
 478              $replaceRegexp = implode( '', $regexpList );
 479  
 480              $wildcardArray = $wildcard->asArray();
 481  
 482              $phpCode .= "    ";
 483              $phpCode .= "if ( preg_match( \"#^$matchRegexp#\", \$uri, \$matches ) )\n    {\n";
 484              $phpCode .= "        $replaceCode;\n";
 485              $phpCode .= "        \$urlAlias = " . $phpCache->variableText( $wildcardArray, 8 + 12, 0, false ) . ";\n";
 486              $phpCode .= "        return true;\n";
 487              $phpCode .= "    }\n";
 488  
 489              ++$counter;
 490          }
 491          $phpCode .= "    return false;\n";
 492  
 493          $phpCode .= "}\n";
 494  
 495          $phpCache->addCodePiece( $phpCode );
 496          $phpCache->store( true );
 497      }
 498  
 499      /*!
 500       \return true if the wildcard cache is expired.
 501      */
 502      function &isWildcardExpired( $timestamp )
 503      {
 504          $retVal = false;
 505          include_once ( 'lib/ezutils/classes/ezexpiryhandler.php' );
 506          $handler =& eZExpiryHandler::instance();
 507          if ( !$handler->hasTimestamp( 'urlalias-wildcard' ) )
 508              return $retVal;
 509          $expiryTime = $handler->timestamp( 'urlalias-wildcard' );
 510          if ( $expiryTime > $timestamp )
 511              $retVal = true;
 512          return $retVal;
 513      }
 514  
 515      /*!
 516       Expires the wildcard cache. This causes the wildcard cache to be
 517       regenerated on the next page load.
 518      */
 519      function expireWildcards()
 520      {
 521          include_once ( 'lib/ezutils/classes/ezexpiryhandler.php' );
 522          $handler =& eZExpiryHandler::instance();
 523          $handler->setTimestamp( 'urlalias-wildcard', mktime() );
 524          $handler->store();
 525      }
 526  
 527      /*!
 528       \static
 529       Transforms the URI if there exists an alias for it.
 530       \return \c true is if successful, \c false otherwise
 531       \return The eZURLAlias object of the new url is returned if the translation was found, but the resource has moved.
 532      */
 533      function &translateByWildcard( &$uri, $reverse = false )
 534      {
 535          if ( get_class( $uri ) == "ezuri" )
 536          {
 537              $uriString = $uri->elements();
 538          }
 539          else
 540          {
 541              $uriString = $uri;
 542          }
 543          $uriString = eZURLAlias::cleanURL( $uriString );
 544  
 545          $info = eZURLAlias::cacheInfo();
 546          $hasCache = false;
 547          $isExpired = true;
 548          $return = false;
 549  
 550          // VS-DBFILE
 551  
 552          require_once ( 'kernel/classes/ezclusterfilehandler.php' );
 553          $cacheFile = eZClusterFileHandler::instance( $info['path'] );
 554  
 555          if ( $cacheFile->exists() )
 556          {
 557              $timestamp = $cacheFile->mtime();
 558              $isExpired = eZURLAlias::isWildcardExpired( $timestamp );
 559              $hasCache = true;
 560          }
 561          if ( $isExpired )
 562          {
 563              eZURLAlias::createWildcardMatches();
 564              $hasCache = true;
 565          }
 566          if ( $hasCache )
 567          {
 568              // VS-DBFILE
 569  
 570              $cacheFile->fetch();
 571              include_once( $info['path'] );
 572              $cacheFile->deleteLocal();
 573  
 574              $hasCache = false;
 575              if ( function_exists( EZURLALIAS_CACHE_FUNCTION ) )
 576              {
 577                  $hasCache = true;
 578                  $function = EZURLALIAS_CACHE_FUNCTION;
 579                  $hasTranslated = false;
 580                  $url = false;
 581                  $ini =& eZINI::instance();
 582                  $maxIterationCount = $ini->variable( 'URLTranslator', 'MaximumWildcardIterations' );
 583                  $iteration = 0;
 584                  while ( $function( $uriString, $urlAlias ) )
 585                  {
 586                      $hasTranslated = true;
 587                      $url = eZURLAlias::fetchBySourceURL( $uriString, true, true, false );
 588                      if ( $url )
 589                          break;
 590                      ++$iteration;
 591                      if ( $iteration >= $maxIterationCount )
 592                          break;
 593                  }
 594                  if ( $hasTranslated )
 595                  {
 596                      if ( $urlAlias['is_wildcard'] == EZ_URLALIAS_WILDCARD_TYPE_FORWARD )
 597                      {
 598                          if ( !$url )
 599                              $url = eZURLAlias::fetchBySourceURL( $uriString, true, true, false );
 600                          if ( $url and $url->attribute( 'forward_to_id' ) != 0 )
 601                          {
 602                              // This will set the forwarded alias as the URL to redirect to.
 603                              $return = eZURLAlias::fetch( $url->attribute( 'forward_to_id' ) );
 604                              $uriString = 'error/301';
 605                          }
 606                          else if ( $url )
 607                          {
 608                              // This will set the current alias as the URL to redirect to.
 609                              $return =& $url;
 610                              $uriString = 'error/301';
 611                          }
 612                      }
 613                      else if ( $urlAlias['is_wildcard'] == EZ_URLALIAS_WILDCARD_TYPE_DIRECT )
 614                      {
 615                          if ( !$url )
 616                              $url = eZURLAlias::fetchBySourceURL( $uriString, true, true, false );
 617                          if ( $url and $url->attribute( 'forward_to_id' ) != 0 )
 618                          {
 619                              // This will set the forwarded alias as the URL to use as system URL.
 620                              $url = eZURLAlias::fetch( $url->attribute( 'forward_to_id' ) );
 621                              $uriString = $url->attribute( 'destination_url' );
 622                              $return = true;
 623                          }
 624                          else if ( $url )
 625                          {
 626                              // This will set the current alias as the URL to use as system URL.
 627                              $uriString = $url->attribute( 'destination_url' );
 628                              $return = true;
 629                          }
 630                      }
 631                  }
 632              }
 633          }
 634          if ( !$hasCache )
 635          {
 636              $return = false;
 637              return $return;
 638          }
 639  
 640          if ( get_class( $uri ) == "ezuri" )
 641          {
 642              $uri->setURIString( $uriString, false );
 643          }
 644          else
 645          {
 646              $uri = $uriString;
 647          }
 648          return $return;
 649      }
 650  
 651      /*!
 652       \static
 653        Counts the non-internal URL alias
 654      */
 655      function &totalCount( )
 656      {
 657          $db =& eZDB::instance();
 658          $query = "SELECT count(id) AS count
 659   FROM ezurlalias
 660   WHERE is_internal = 0";
 661          $res = $db->arrayQuery( $query );
 662          return $res[0]['count'];
 663      }
 664  
 665      /*!
 666       \static
 667       Converts the path \a $urlElement into a new alias url which only conists of characters
 668       in the range a-z, numbers and _.
 669       All other characters are converted to _.
 670       \return the converted element
 671  
 672       \example
 673       'My car' => 'my_car'
 674       'What is this?' => 'what_is_this'
 675       'This & that' => 'this_that'
 676       'myfile.tpl' => 'myfile_tpl',
 677       'øæå' => 'oeaeaa'
 678       \endexample
 679      */
 680      function convertToAlias( $urlElement, $defaultValue = false )
 681      {
 682          include_once ( 'lib/ezi18n/classes/ezchartransform.php' );
 683          $trans =& eZCharTransform::instance();
 684  
 685          $urlElement = $trans->transformByGroup( $urlElement, 'urlalias' );
 686          if ( strlen( $urlElement ) == 0 )
 687          {
 688              if ( $defaultValue === false )
 689                  $urlElement = '_1';
 690              else
 691              {
 692                  $urlElement = $defaultValue;
 693                  $urlElement = $trans->transformByGroup( $urlElement, 'urlalias' );
 694              }
 695          }
 696          return $urlElement;
 697      }
 698  
 699      /*!
 700       \static
 701       Converts the path \a $pathURL into a new alias path with limited characters.
 702       For more information on the conversion see convertToAlias().
 703       \note each element in the path (separated by / (slash) ) is converted separately.
 704       \return the converted path
 705      */
 706      function convertPathToAlias( $pathURL )
 707      {
 708          $result = array();
 709  
 710          $elements = explode( '/', $pathURL );
 711  
 712          foreach ( $elements as $element )
 713          {
 714              $element = eZURLAlias::convertToAlias( $element );
 715              $result[] = $element;
 716          }
 717  
 718          return implode( '/', $result );
 719      }
 720  
 721      /*!
 722       \static
 723       Transforms the URI if there exists an alias for it.
 724       \return \c true is if successful, \c false otherwise
 725       \return The eZURLAlias object of the new url is returned if the translation was found, but the resource has moved.
 726      */
 727      function translate( &$uri, $reverse = false )
 728      {
 729          if ( get_class( $uri ) == "ezuri" )
 730          {
 731              $uriString = $uri->elements();
 732          }
 733          else
 734          {
 735              $uriString = $uri;
 736          }
 737          $uriString = eZURLAlias::cleanURL( $uriString );
 738          $internalURIString = $uriString;
 739  
 740          if ( isset( $GLOBALS['eZURLAliasTranslate'][$uriString] ) )
 741          {
 742              $uri = $GLOBALS['eZURLAliasTranslate'][$uriString]['uri'];
 743              return $GLOBALS['eZURLAliasTranslate'][$uriString]['return'];
 744          }
 745  
 746          $originalURIString = $uriString;
 747  
 748          $ini =& eZIni::instance();
 749          if ( $ini->hasVariable( 'SiteAccessSettings', 'PathPrefix' ) &&
 750               $ini->variable( 'SiteAccessSettings', 'PathPrefix' ) != '' )
 751          {
 752              $prefix = $ini->variable( 'SiteAccessSettings', 'PathPrefix' );
 753              // Only prepend the path prefix if it's not already the first element of the url.
 754              if ( !preg_match( "#^$prefix(/.*)?$#", $uriString )  )
 755              {
 756                  $exclude = $ini->hasVariable( 'SiteAccessSettings', 'PathPrefixExclude' )
 757                             ? $ini->variable( 'SiteAccessSettings', 'PathPrefixExclude' )
 758                             : false;
 759                  $breakInternalURI = false;
 760                  foreach ( $exclude as $item )
 761                  {
 762                      if ( preg_match( "#^$item(/.*)?$#", $uriString )  )
 763                      {
 764                          $breakInternalURI = true;
 765                          break;
 766                      }
 767                  }
 768                  // We should check if this urlString is internal
 769                  // If yes we should not use PathPrefix
 770                  $urlAliasObject = eZURLAlias::fetchBySourceURL( $uriString, false, false );
 771                  if ( $urlAliasObject )
 772                      $breakInternalURI = true;
 773  
 774                  if ( !$breakInternalURI )
 775                      $internalURIString = eZUrlAlias::cleanURL( eZUrlAlias::cleanURL( $prefix ) . '/' . $uriString );
 776              }
 777          }
 778  
 779          $db =& eZDB::instance();
 780          if ( $reverse )
 781          {
 782              $query = "SELECT source_url as destination_url, forward_to_id
 783  FROM ezurlalias
 784  WHERE destination_url = '" . $db->escapeString( $internalURIString ) . "' AND
 785        forward_to_id = 0 AND
 786        is_wildcard = 0
 787  ORDER BY forward_to_id ASC";
 788          }
 789          else
 790          {
 791              $query = "SELECT destination_url, forward_to_id
 792  FROM ezurlalias
 793  WHERE source_md5 = '" . md5( $internalURIString ) . "' AND
 794        is_wildcard = 0
 795  ORDER BY forward_to_id ASC, is_internal ASC";
 796          }
 797  
 798          $return = false;
 799          $urlAliasArray = $db->arrayQuery( $query, array( 'limit' => 1 ) );
 800          if ( count( $urlAliasArray ) > 0 )
 801          {
 802              $uriString = $urlAliasArray[0]['destination_url'];
 803              if ( $uriString == '' )
 804                  $uriString = '/';
 805  
 806              if ( $urlAliasArray[0]['forward_to_id'] == -1 )
 807              {
 808                  $uriString = 'error/301';
 809  
 810                  $return = $urlAliasArray[0]['destination_url'];
 811              }
 812              else if ( $urlAliasArray[0]['forward_to_id'] != 0 )
 813              {
 814                  $uriString = 'error/301';
 815  
 816                  $return = eZURLAlias::fetch( $urlAliasArray[0]['forward_to_id'] );
 817              }
 818              else
 819              {
 820                  $return = true;
 821              }
 822          }
 823  
 824          if ( get_class( $uri ) == "ezuri" )
 825          {
 826              $uri->setURIString( $uriString, false );
 827          }
 828          else
 829          {
 830              $uri = $uriString;
 831          }
 832  
 833          $GLOBALS['eZURLAliasTranslate'][$originalURIString] = array( 'return' => $return,
 834                                                                       'uri' => $uri );
 835  
 836          return $return;
 837      }
 838  
 839      /*!
 840       \static
 841       Makes sure the URL \a $url does not contain leading and trailing slashes (/).
 842       \return the clean URL
 843      */
 844      function cleanURL( $url )
 845      {
 846          return trim( $url, '/ ' );
 847      }
 848  }
 849  
 850  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7