b2evolution PHP Cross Reference Blogging Systems

Source: /inc/slugs/model/_slug.funcs.php - 220 lines - 5383 bytes - Summary - Text - Print

Description: This file implements Slug handling functions.

   1  <?php
   2  /**
   3   * This file implements Slug handling functions.
   4   *
   5   * This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}.
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}.
   9  *
  10   * @license http://b2evolution.net/about/license.html GNU General Public License (GPL)
  11   *
  12   * {@internal Open Source relicensing agreement:
  13   * EVO FACTORY grants Francois PLANQUE the right to license
  14   * EVO FACTORY contributions to this file and the b2evolution project
  15   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  16   * }}
  17   *
  18   * @package evocore
  19   *
  20   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  21   * @author evfy-asimo: Attila Simo
  22   *
  23   * @version $ID: $
  24   */
  25  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  26  
  27  load_class( 'slugs/model/_slug.class.php', 'Slug' );
  28  
  29  /*
  30   * Generetae a tiny url (Slug)
  31   * The first character is always a lowercase letter
  32   * The second character is always a capital letter
  33   * The third caharacter is always a number
  34   * at most three letter (uppercase or lowercase) can be after each other -> every fourth character is a number
  35   * at most three number can be after each other -> every fourth character is a letter
  36   * 
  37   * @param string|NULL use this param from getnext_url function, when the last generated url already exists
  38   * @return string the newly generated tinyurl
  39   */
  40  function generate_tinyurl( $last_url = NULL )
  41  {
  42      global $Settings;
  43  
  44      /* Ascii code table for the used characters
  45      $zero = 48;
  46      $nine = 57;
  47      $lower_a = 97;
  48      $lower_z = 122;
  49      $upper_a = 65;
  50      $upper_z = 90;
  51      */
  52  
  53      // get the last used tinyurl
  54      if( ! $last_url && $Settings != null )
  55      { // last_url is not set
  56          $last_url = $Settings->get( 'tinyurl' );
  57      }
  58  
  59      if( ! $last_url )
  60      { // no tinyurl is set, used the default value for the first one
  61          $last_url = 'aA0';
  62          return $last_url;
  63      }
  64  
  65      $index = strlen($last_url);
  66      //find the last character what can be changed
  67      $newurl_ending = '';
  68      while( $index )
  69      {
  70          $char = substr( $last_url, $index - 1, 1 );
  71          $char_value = ord( $char );
  72          if( $index <= 3 )
  73          { // we have to change one of the first three character
  74              if( $char_value >= 65 )
  75              { // the character is a letter
  76                  if( $char_value >= 97 )
  77                  { // the character is a lowercase letter
  78                      if( $char_value < 122 )
  79                      { // there is still unused lowercase letter
  80                          $char_value++;
  81                          $newurl_ending = chr( $char_value ).$newurl_ending;
  82                          break;
  83                          // exit from the loop,the change was done
  84                      }
  85                      // there is no more variation , shift and try the next character
  86                      $newurl_ending = chr( 97 ).$newurl_ending;
  87                      $index--;
  88                      continue;
  89                  }
  90                  elseif( $char_value < 90 )
  91                  {
  92                      $char_value++;
  93                      $newurl_ending = chr( $char_value ).$newurl_ending;
  94                      break;
  95                  }
  96                  $newurl_ending = chr( 65 ).$newurl_ending;
  97                  $index--;
  98                  continue;
  99              }
 100              elseif( $char_value >= 48 )
 101              {
 102                  if( $char_value < 57 )
 103                  {
 104                      $char_value++;
 105                      $newurl_ending = chr( $char_value ).$newurl_ending;
 106                      break;
 107                  }
 108                  $newurl_ending = chr( 48 ).$newurl_ending;
 109                  $index--;
 110                  continue;
 111              }
 112              else
 113              {
 114                  debug_die( 'Wrong tinyurl in database!' );
 115              }
 116          }
 117          else
 118          { // we change a character after the third position
 119              if( $char_value >= 48 && $char_value <= 57 )
 120              {
 121                  if( $char_value != 57 )
 122                  {
 123                      $char_value++;
 124                  }
 125                  else
 126                  {
 127                      if( ( $index - 3) % 4 == 0 )
 128                      { // every fourth character must be a number
 129                          $newurl_ending = chr( 48 ).$newurl_ending;
 130                          $index--;
 131                          continue;
 132                      }
 133                      else
 134                      {
 135                          $char_value = 65;
 136                      }
 137                  }
 138                  $newurl_ending = chr( $char_value ).$newurl_ending;
 139                  break;
 140              }
 141              if( $char_value >= 65 && $char_value <= 90 )
 142              {
 143                  if( $char_value != 90 )
 144                  {
 145                      $char_value++;
 146                  }
 147                  else
 148                  {
 149                      $char_value = 97;
 150                  }
 151                  $newurl_ending = chr( $char_value ).$newurl_ending;
 152                  break;
 153              }
 154              if( $char_value >= 97 && $char_value < 122 )
 155              {
 156                  $char_value++;
 157                  $newurl_ending = chr( $char_value ).$newurl_ending;
 158                  break;
 159              }
 160              if( $char_value == 122 )
 161              { // no more variation we need to shift to another position
 162                  if( ( $index - 3 ) % 4 == 3 )
 163                  { // every fourth character must be a letter
 164                      $newurl_ending = chr( 65 ).$newurl_ending;
 165                  }
 166                  else
 167                  {
 168                      $newurl_ending = chr( 48 ).$newurl_ending;
 169                  }
 170                  $index--;
 171                  continue;
 172              }
 173              else
 174              {
 175                  debug_die( 'Wrong tinyurl in database!' );
 176              }
 177          }
 178      }
 179  
 180      if( $index )
 181      { // index is not null we change the last_url ending part
 182          $new_url = substr_replace( $last_url, $newurl_ending, $index - 1 );
 183          return $new_url;
 184      }
 185  
 186      // we need to add one more character, because we don't have any other unused variation
 187      $index = strlen($last_url);
 188      if( ( $index > 4 ) && ( ( $index - 2 ) % 4 == 3 ) )
 189      {
 190          $new_url = $newurl_ending.chr( 65 );
 191      }
 192      else
 193      {
 194          $new_url = $newurl_ending.chr( 48 );
 195      }
 196      return $new_url;
 197  }
 198  
 199  /*
 200   * Create a new unused tinyurl
 201   *  
 202   * @return string tinyurl
 203   */
 204  function getnext_tinyurl()
 205  {
 206      global $DB;
 207  
 208      $url = NULL;
 209      do
 210      { // create a new tinyurl, until it is unique
 211          $url = generate_tinyurl( $url );
 212          $query = 'SELECT slug_ID
 213                      FROM T_slug
 214                      WHERE slug_title = '.$DB->quote( $url );
 215      }
 216      while( $DB->get_var( $query ) != NULL );
 217      return $url;
 218  }
 219  
 220  ?>

title

Description

title

Description

title

Description

title

title

Body