[ Index ]

PHP Cross Reference of Mambo 4.6.5

[ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/includes/ -> sef.php (source)

   1  <?php
   2  /**
   3  * @package Mambo
   4  * @author Mambo Foundation Inc see README.php
   5  * @copyright (C) 2000 - 2009 Mambo Foundation Inc.
   6  * See COPYRIGHT.php for copyright notices and details.
   7  * @license GNU/GPL Version 2, see LICENSE.php
   8  *
   9  * Redistributions of files must retain the above copyright notice.
  10  *
  11  * Mambo is free software; you can redistribute it and/or
  12  * modify it under the terms of the GNU General Public License
  13  * as published by the Free Software Foundation; version 2 of the License.
  14  */
  15  
  16  class mosSEF {
  17      
  18      var $prefix = '';
  19      var $custom_code = array();
  20      var $custom_name = array();
  21      var $custom_PHP = array();
  22      var $custom_short = array();
  23      var $sef_content_task = array();
  24      var $sef_name_chars = array();
  25      var $sef_translate_chars = array();
  26      var $sef_name_string = '';
  27      var $sef_translate_string = '';
  28      var $content_data = null;
  29      var $content_items = array();
  30      var $content_sections = array();
  31      var $content_categories = array();
  32      var $SEF_SPACE;
  33      
  34      function mosSEF () {
  35          /*******************************************************************************
  36          **  The following are parameters for the ReMOSef Search Engine
  37          **  Optimisation component.  $this->SEF_SPACE should be set to the
  38          **  character that is to replace blanks in names that form the URL.
  39          **  You can vary this, although the only sensible choices seem to be
  40          **  underscore or hyphen (_ or -).
  41          **
  42          **  The arrays $this->custom_code and $this->custom_name must be kept exactly
  43          **  in step with each other.  $this->custom_code is a list of the components
  44          **  that are to be handled by ReMOSef.
  45          **
  46          **  The array $this->custom_name is the alternative name that will be used
  47          **  in the optimised URL to identify the component and can be whatever
  48          **  you please so long as it is unique and legal for a URL.  Apart from
  49          **  using the custom name for the component, ReMOSef will do no further
  50          **  translation of the URL than is done by the standard Mambo SEF - UNLESS
  51          **  there is a sef_ext.php file installed for that component.  The
  52          **  exception to this is ReMOSitory - the optimisation code for Remository
  53          **  is integrated in ReMOSef.  For other components, if a sef_ext.php is
  54          **  present, it will be invoked by ReMOSef.
  55          **
  56          **  The array $remosef_content_task is capable of translating the tasks
  57          **  understood by the Mambo content component.  Please DO NOT CHANGE what
  58          **  is to the left of the equals sign.  When putting different values on
  59          **  the right hand side, remember that they must all be different, and must
  60          **  also be different from any of the custom names used for components.
  61          **  Without that, Remosef cannot figure out what a SEF URL means and will
  62          **  give unpredictable results.
  63          **
  64          **  Note that the names in custom_name must not be allowed to clash with
  65          **  the names used as tasks by the content component, in their
  66          **  translated form - see below.
  67          *******************************************************************************/
  68          $this->SEF_SPACE = "_";                 // divide words with underscores
  69                                              // can be changed to a hyphen "-"
  70          $this->custom_code = array('com_frontpage','com_contact');
  71          $this->custom_name = array('Frontpage','Contact_Us');
  72          /*******************************************************************************
  73          **  The following are the parameters for the optional content specific
  74          **  URL optimisation.  They are not used within the standard SEF processing
  75          **  unless you add in a sef_ext.php that is integrated with the Mambo SEF.
  76          **
  77          **  The following two lines define the translations that SEF will perform on
  78          **  names of sections and categories when translating them for inclusion in a URL.
  79          **  Each item in $this->sef_name_chars is translated into the corresponding
  80          **  element of $this->sef_translate_chars.
  81          **
  82          **  NOTE it is important that space be the last translate character, since the
  83          **  characters are processed in the order in which they appear.  Since earlier
  84          **  translates may create new spaces, it is vital that the space translation is
  85          **  done last.
  86          **
  87          **  You can extend these arrays as you wish, although it is obviously important
  88          **  to make sure that the items of one match the items of the other exactly.
  89          *******************************************************************************/
  90          $this->sef_name_chars = array('?', '&', '/', ' ');
  91          $this->sef_translate_chars = array('', 'and', ' or ', $this->SEF_SPACE);
  92          $this->sef_name_string = '�\'';
  93          $this->sef_translate_string = '--';
  94  
  95  /************** DO NOT MAKE CHANGES IN THE FOLLOWING LINES EXCEPT AT YOUR OWN RISK! ********************/
  96  
  97          if (mamboCore::is_set('mosConfig_sef_prefix')) $this->prefix = mamboCore::get('mosConfig_sef_prefix');
  98          else $this->prefix = 'mos';
  99          if ($this->prefix == 'content' OR $this->prefix == 'component') $this->prefix = 'mos';
 100          foreach ($this->custom_code as $code) {
 101              $codefile = "components/$code/sef_ext.php";
 102              if (file_exists($codefile)) {
 103                  include ($codefile);
 104                  $this->custom_PHP[] = true;
 105              }
 106              else $this->custom_PHP[] = false;
 107              $split = explode('_',$code);
 108              $this->custom_short[] = $split[1];
 109          }
 110  
 111          $this->sef_content_task['findkey'] = 'findkey';
 112          $this->sef_content_task['view'] = 'view';
 113          $this->sef_content_task['section'] = 'section';
 114          $this->sef_content_task['category'] = 'category';
 115          $this->sef_content_task['blogsection'] = 'blogsection';
 116          $this->sef_content_task['blogcategorymulti'] = 'blogcategorymulti';
 117          $this->sef_content_task['blogcategory'] = 'blogcategory';
 118          $this->sef_content_task['archivesection'] = 'archivesection';
 119          $this->sef_content_task['archivecategory'] = 'archivecategory';
 120          $this->sef_content_task['save'] = 'save';
 121          $this->sef_content_task['cancel'] = 'cancel';
 122          $this->sef_content_task['emailform'] = 'emailform';
 123          $this->sef_content_task['emailsend'] = 'emailsend';
 124          $this->sef_content_task['vote'] = 'vote';
 125          $this->sef_content_task['showblogsection'] = 'showblogsection';
 126  
 127      }
 128      
 129      function &getInstance () {
 130          static $instance;
 131          if (!is_object($instance)) $instance = new mosSEF();
 132          return $instance;
 133      }
 134      
 135      function nameForURL ($string) {
 136          $accented = "���������������������������������������������������������������������".$this->sef_name_string;
 137          $nonaccent = "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy".$this->sef_translate_string;
 138          $string = strtr($string, $accented, $nonaccent);
 139          foreach ($this->sef_name_chars as $i=>$char) $string = str_replace($char, $this->sef_translate_chars[$i], $string);
 140          $string = urlencode($string);
 141          return $string;
 142      }
 143          
 144  
 145      function sefRetrieval($register_globals){
 146          
 147          if (preg_match('/(\b)[GLOBALS|_REQUEST|_SERVER|_ENV|_COOKIE|_GET|_POST|_FILES|_SESSION](\b)/i', $_SERVER['REQUEST_URI']) > 0) {
 148              die('Invalid Request');
 149          }
 150          if (mamboCore::get('mosConfig_sef')) {
 151              $subdir = mamboCore::get('subdirectory');
 152              $uri = substr($_SERVER['REQUEST_URI'], strlen($subdir));
 153              $url_array = explode('/', $uri);
 154              /**
 155              * Content
 156              * /$option/$task/$sectionid/$id/$Itemid/$limit/$limitstart
 157              */
 158              if (!isset($url_array[2]) AND strpos($uri, '?')) return 0;
 159              elseif ($url_array[2] == 'syndstyle') {
 160                  array_splice($url_array, 2, 1);
 161                  $_REQUEST['syndstyle'] = $_GET['syndstyle'] = $syndstyle = 'yes';
 162                  $QUERY_STRING = 'syndstyle=yes&';
 163              }
 164              else $QUERY_STRING = '';
 165  
 166              // language hook for content
 167              $lang = "";
 168              $parms = array();
 169              foreach($url_array as $key=>$value) {
 170                  if ( strcasecmp(substr($value,0,5),'lang,') == 0 ) {
 171                      $parts = explode(",", $value);
 172                      if (count($parts) > 1) {
 173                          $lang = $_REQUEST['lang'] = $_GET['lang'] = $parts[1];
 174                      }
 175                  }
 176                  elseif ( $value != '' AND $key > 1 ) $parms[] = $value;
 177              }
 178              
 179              $foundit = false;
 180              if ($url_array[1] == 'content') {
 181                  $num = count($parms);
 182                  $foundit = true;
 183                  $_REQUEST['option'] = $_GET['option'] = $option = 'com_content';
 184                  if (!is_numeric($parms[$num-1])) {
 185                      $_REQUEST['lang'] = $_GET['lang'] = $lang = array_pop($parms);
 186                      $num--;
 187                  }
 188                  // $option/$task/$sectionid/$id/$Itemid/$limit/$limitstart/
 189                  $task = array_search($parms[0], $this->sef_content_task);
 190                  if ($task === false OR $task === null) return 1;
 191                  $_REQUEST['task'] = $_GET['task'] = $task;
 192                  $QUERY_STRING .= "option=com_content&task=$task";
 193  
 194                  if ($task == 'archivecategory') {
 195                      $_REQUEST['year'] = $_GET['year'] = $year = intval(@$parms[1]);
 196                      $_REQUEST['month'] = $_GET['month'] = $month = intval(@$parms[2]);
 197                      $_REQUEST['module'] = $_GET['module'] = $module = intval(@$parms[3]);
 198                      $QUERY_STRING .= "&year=$year&month=$month&module=$module";
 199                  }
 200                  else {
 201                      for ($i = 1; $i <= $num-1; $i++) {
 202                          if (strcmp($parms[$i], (int)$parms[$i]) !== 0) return 1;
 203                      }
 204                                                  
 205                      $i = 1;
 206                      if ($num == 6 OR $num == 4) {
 207                          $_REQUEST['sectionid'] = $_GET['sectionid'] = $sectionid = $parms[$i];
 208                          $QUERY_STRING .= "&sectionid=$sectionid";
 209                          $i++;
 210                      }
 211                      if ($num > 1) {
 212                          $_REQUEST['id'] = $_GET['id'] = $id = $parms[$i];
 213                          $QUERY_STRING .= "&id=$id";
 214                      }
 215                      if ($num > 2) {
 216                          $_REQUEST['Itemid'] = $_GET['Itemid'] = $Itemid = $parms[$i+1];
 217                          mamboCore::set('Itemid',$Itemid);
 218                          $QUERY_STRING .= "&Itemid=$Itemid";
 219                      }
 220                      if ($num > 4) {
 221                          $_REQUEST['limit'] = $_GET['limit'] = $limit = $parms[$i+2];
 222                          $_REQUEST['limitstart'] = $_GET['limitstart'] = $limitstart = $parms[$i+3];
 223                          $QUERY_STRING .= "&limit=$limit&limitstart=$limitstart";
 224                          $i+=2;
 225                      }
 226                  }
 227                  if ($lang!="") {
 228                      $QUERY_STRING .= "&lang=$lang";
 229                  }
 230              }
 231                  
 232              /*
 233              Components
 234              http://www.domain.com/component/$name,$value
 235              */
 236              elseif ($url_array[1] == 'component') {
 237                  $QUERY_STRING .= $this->default_revert('component');
 238                  if ($QUERY_STRING) $foundit = true;
 239              }
 240              elseif ($url_array[1] == $this->prefix) {
 241                  $menuhandler =& mosMenuHandler::getInstance();
 242                  foreach ($this->custom_name as $i=>$compname) {
 243                      if ($url_array[2] == $compname) {
 244                          $origname = $this->custom_code[$i];
 245                          $_REQUEST['Itemid'] = $_GET['Itemid'] = $Itemid = $menuhandler->getIDLikeLink("index.php?option=$origname");
 246                          mamboCore::set('Itemid', $Itemid);
 247                          if ($this->custom_PHP[$i]) {
 248                              $fixup = '$QUERY_STRING .= "option='.$origname.'&Itemid=".$Itemid.sef_'.$this->custom_short[$i].'::revert($url_array,1);';
 249                              eval($fixup);
 250                          }
 251                          else $QUERY_STRING .= "option=$origname&Itemid=$Itemid".$this->default_revert($compname);
 252                          $_REQUEST['option'] = $this->custom_code[$i];
 253                          $foundit = true;
 254                          break;
 255                      }
 256                  }
 257                  if (!$foundit) {
 258                      $content_sef = mamboCore::get('mosConfig_absolute_path').'/components/com_content/sef_ext.php';
 259                      if (file_exists($content_sef)) {
 260                          require_once($content_sef);
 261                          $crevert = sef_content::revert($url_array,1);
 262                          if ($crevert) {
 263                              $foundit = true;
 264                              $QUERY_STRING .= $crevert;
 265                          }
 266                      }
 267                  }
 268              }
 269              if ($foundit) {
 270                  $_SERVER['QUERY_STRING'] = $QUERY_STRING;
 271                  $REQUEST_URI = '/index.php?'.$QUERY_STRING;
 272                  $_SERVER['REQUEST_URI'] = $REQUEST_URI;
 273                  return 0;
 274              }
 275              else return 1;
 276          }
 277          return 0;
 278      }
 279      
 280      function default_revert ($specialname) {
 281          $request = explode($specialname.'/', $_SERVER['REQUEST_URI']);
 282          if (isset($request[1])) $parmset = explode("/", $request[1]);
 283          else $parmset = array();
 284          $QUERY_STRING = '';
 285          $menuhandler =& mosMenuHandler::getInstance();
 286          foreach($parmset as $values) {
 287              $parts = explode(",", $values);
 288              if (count($parts) > 1) {
 289                  $_REQUEST[$parts[0]] = $_GET[$parts[0]] = $parts[1];
 290                  if ($parts[0] == 'option') {
 291                      $_REQUEST['Itemid'] = $_GET['Itemid'] = $Itemid = $menuhandler->getIDLikeLink("index.php?option=$parts[1]");
 292                      mamboCore::set('Itemid', $Itemid);
 293                      $QUERY_STRING .= "$parts[0]=$parts[1]&Itemid=$Itemid";
 294                  }
 295                  $QUERY_STRING .= "&$parts[0]=$parts[1]";
 296              }
 297          }
 298          return $QUERY_STRING;
 299      }
 300  
 301      function sefRelToAbs( $string ) {
 302          global $mosConfig_nok_content,$iso_client_lang;
 303  
 304          $server = mamboCore::get('mosConfig_live_site');
 305          if ($string == 'index.php') return $server.'/';
 306          if (strtolower(substr($string,0,9)) != 'index.php' OR eregi('^(([^:/?#]+):)',$string)) return $string;
 307          $anchor = '';
 308          if (preg_match('/#((.)+)$/',$string,$parts)){
 309              $anchor = $parts[1];
 310              $string = substr($string, 0, (strlen($anchor) + 1)*-1);
 311          }
 312          if (mosGetParam($_GET, 'syndstyle', '') == 'yes') $string .= '&syndstyle=yes';
 313          if($mosConfig_nok_content AND (strpos( strtolower($string),"lang=") === false)) {
 314              $string .= "&lang=$iso_client_lang";
 315          }
 316          $passed_string = str_replace('&amp;', '&', $string);
 317          if (!mamboCore::get('mosConfig_sef')) return $this->plusAnchor($server.'/'.str_replace( '&', '&amp;', $passed_string ),$anchor);
 318          $string = substr($passed_string,10);
 319          $option = $task = '';
 320          $oktasks = true;
 321          $syndmark = '';
 322          parse_str($string, $params);
 323          foreach ($params as $key=>$value) {
 324              $lowkey = strtolower($key);
 325              $lowvalue = strtolower($value);
 326              $unset = true;
 327              switch ($lowkey) {
 328                  case 'option':
 329                      $option = $lowvalue;
 330                      break;
 331                  case 'task':
 332                      $task = $value;
 333                      if ($lowvalue == 'new' OR $lowvalue == 'edit') $oktasks = false;
 334                      break;
 335                  case 'syndstyle':
 336                      $syndmark = '/syndstyle';
 337                      break;
 338                  default:
 339                      $check_params[$lowkey] = $key;
 340                      $unset = false;
 341              }
 342              if ($unset) unset($params[$key]);
 343          }
 344          // Process content items
 345          if (($option == 'com_content' OR $option == 'content') AND $oktasks) {
 346              /*
 347              Content
 348              index.php?option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart
 349              */
 350              $task = $this->sef_content_task[$task];
 351              $content_sef = mamboCore::get('mosConfig_absolute_path').'/components/com_content/sef_ext.php';
 352              if (file_exists($content_sef)) {
 353                  require_once($content_sef);
 354                  return $this->plusAnchor($server.'/'.$this->prefix.$syndmark.sef_content::create($task, $params),$anchor);
 355              }
 356              $keys = array('sectionid', 'id', 'itemid', 'limit', 'limitstart', 'year', 'month', 'module', 'lang');
 357              $string = '/content'.$syndmark.'/'.$task.'/';
 358              foreach ($keys as $key) {
 359                  if (isset($check_params[$key])) {
 360                      $pkey = $check_params[$key];
 361                      $string .= $params[$pkey].'/';
 362                  }
 363              }
 364              return $this->plusAnchor($server.$string,$anchor);
 365          }
 366          // Other types of URL than content do not use Itemid in the SEO version
 367          if (isset($check_params['itemid']) && isset($check_params['id'])) {
 368              $pkey = $check_params['itemid'];
 369              unset($params[$pkey]);
 370              unset($check_params['itemid']);
 371          }
 372          // Process customised components
 373          $i = array_search($option,$this->custom_code);
 374          if ($i !== false AND $i !== null) {
 375              if ($this->custom_PHP[$i]) eval('$string = sef_'.$this->custom_short[$i].'::create($passed_string);');
 376              else $string = $this->componentDetails($params,$task);
 377              return $this->plusAnchor($server.'/'.$this->prefix.$syndmark.'/'.$this->custom_name[$i].'/'.$string,$anchor);
 378          }
 379          // Process ordinary components
 380          if (strpos($option,'com_')===0 AND $option != 'com_registration' AND $oktasks) {
 381              return $this->plusAnchor("$server/component$syndmark/option,$option/".$this->componentDetails($params,$task),$anchor);
 382          }
 383          // Anything else is returned as received, except it is guaranteed that & will be &amp;
 384          return $this->plusAnchor($server.'/'.str_replace( '&', '&amp;', $passed_string ),$anchor);
 385      }
 386  
 387      function componentDetails (&$params, $task) {
 388          $string = ($task ? "task,$task/" : '');
 389          foreach ($params as $key=>$param) $string .= "$key,$param/";
 390          return $string;
 391      }
 392  
 393      function plusAnchor($sefString, $anchor){
 394          if ( substr($sefString,-1) == '/' ){
 395              $sefString = substr($sefString,0,-1);
 396              if ($anchor!=''){
 397                  $sefString.="#{$anchor}";
 398              }else{
 399                  $sefString.='/';
 400              }
 401          }
 402          return $sefString;
 403      }
 404  
 405  }
 406  
 407  function sefRelToAbs ($string) {
 408      $sef =& mosSEF::getInstance();
 409      return $sef->sefRelToAbs($string);
 410  }
 411  
 412  if (mamboCore::get('mosConfig_sef') AND $indextype == 3) {
 413      $sef =& mosSEF::getInstance();
 414      $urlerror = $sef->sefRetrieval(mamboCore::get('mosConfig_register_globals'));
 415      $indextype = 1;
 416  }
 417  ?>