old.aliroSEF.php

Go to the documentation of this file.
00001 <?php
00010 class aliroSEF {
00011 
00012     private $prefix = '';
00013     private $custom_code = array();
00014     private $custom_name = array();
00015     private $custom_PHP = array();
00016     private $custom_short = array();
00017     private $sef_content_task = array();
00018     private $sef_name_chars = array();
00019     public $sef_translate_chars = array();
00020     public $content_data = null;
00021     public $content_items = array();
00022     public $content_sections = array();
00023     public $content_categories = array();
00024     public $SEF_SPACE;
00025 
00026     function __construct () {
00027         /*******************************************************************************
00028         **  The following are parameters for the ReMOSef Search Engine
00029         **  Optimisation component.  $this->SEF_SPACE should be set to the
00030         **  character that is to replace blanks in names that form the URL.
00031         **  You can vary this, although the only sensible choices seem to be
00032         **  underscore or hyphen (_ or -).
00033         **
00034         **  The arrays $this->custom_code and $this->custom_name must be kept exactly
00035         **  in step with each other.  $this->custom_code is a list of the components
00036         **  that are to be handled by ReMOSef.
00037         **
00038         **  The array $this->custom_name is the alternative name that will be used
00039         **  in the optimised URL to identify the component and can be whatever
00040         **  you please so long as it is unique and legal for a URL.  Apart from
00041         **  using the custom name for the component, ReMOSef will do no further
00042         **  translation of the URL than is done by the standard Mambo SEF - UNLESS
00043         **  there is a sef_ext.php file installed for that component.  The
00044         **  exception to this is ReMOSitory - the optimisation code for Remository
00045         **  is integrated in ReMOSef.  For other components, if a sef_ext.php is
00046         **  present, it will be invoked by ReMOSef.
00047         **
00048         **  The array $remosef_content_task is capable of translating the tasks
00049         **  understood by the Mambo content component.  Please DO NOT CHANGE what
00050         **  is to the left of the equals sign.  When putting different values on
00051         **  the right hand side, remember that they must all be different, and must
00052         **  also be different from any of the custom names used for components.
00053         **  Without that, Remosef cannot figure out what a SEF URL means and will
00054         **  give unpredictable results.
00055         *******************************************************************************/
00056         $this->SEF_SPACE = "_";                 // divide words with underscores
00057                                             // can be changed to a hyphen "-"
00058         $this->custom_code = array('com_frontpage','com_contact');
00059         $this->custom_name = array('Frontpage','Contact_Us');
00060         /*******************************************************************************
00061         **  The following are the parameters for the optional content specific
00062         **  URL optimisation.
00063         **
00064         **  The following two lines define the translations that SEF will perform on
00065         **  names of sections and categories when translating them for inclusion in a URL.
00066         **  Each item in $this->sef_name_chars is translated into the corresponding
00067         **  element of $this->sef_translate_chars.
00068         **
00069         **  NOTE it is important that space be the last translate character, since the
00070         **  characters are processed in the order in which they appear.  Since earlier
00071         **  translates may create new spaces, it is vital that the space translation is
00072         **  done last.
00073         **
00074         **  You can extend these arrays as you wish, although it is obviously important
00075         **  to make sure that the items of one match the items of the other exactly.
00076         *******************************************************************************/
00077         $this->sef_name_chars = array('–', '?', '&', '/', ' ');
00078         $this->sef_translate_chars = array('-', '', 'and', ' or ', $this->SEF_SPACE);
00079 
00080 /************** DO NOT MAKE CHANGES PAST HERE EXCEPT AT YOUR OWN RISK! ********************/
00081 
00082         if (mamboCore::is_set('mosConfig_sef_prefix')) $this->prefix = mamboCore::get('mosConfig_sef_prefix');
00083         else $this->prefix = 'mos';
00084         if ($this->prefix == 'content' OR $this->prefix == 'component') $this->prefix = 'mos';
00085         foreach ($this->custom_code as $code) {
00086             $codefile = "components/$code/sef_ext.php";
00087             if (file_exists($codefile)) {
00088                 include ($codefile);
00089                 $this->custom_PHP[] = true;
00090             }
00091             else $this->custom_PHP[] = false;
00092             $split = explode('_',$code);
00093             $this->custom_short[] = $split[1];
00094         }
00095 
00096         $this->sef_content_task['findkey'] = 'findkey';
00097         $this->sef_content_task['view'] = 'view';
00098         $this->sef_content_task['section'] = 'section';
00099         $this->sef_content_task['category'] = 'category';
00100         $this->sef_content_task['blogsection'] = 'blogsection';
00101         $this->sef_content_task['blogcategorymulti'] = 'blogcategorymulti';
00102         $this->sef_content_task['blogcategory'] = 'blogcategory';
00103         $this->sef_content_task['archivesection'] = 'archivesection';
00104         $this->sef_content_task['archivecategory'] = 'archivecategory';
00105         $this->sef_content_task['save'] = 'save';
00106         $this->sef_content_task['cancel'] = 'cancel';
00107         $this->sef_content_task['emailform'] = 'emailform';
00108         $this->sef_content_task['emailsend'] = 'emailsend';
00109         $this->sef_content_task['vote'] = 'vote';
00110         $this->sef_content_task['showblogsection'] = 'showblogsection';
00111 
00112     }
00113 
00114     static function getInstance () {
00115         static $instance;
00116         if (!is_object($instance)) {
00117             $handler = aliroExtensionHandler::getInstance();
00118             $sefplugin = $handler->getExtensionByName('com_sef');
00119             if ($sefplugin AND $sefplugin->class) eval ('$instance = '.$sefplugin->class.'::getInstance();');
00120             else $instance = new aliroSEF();
00121         }
00122         return $instance;
00123     }
00124 
00125     function sefRetrieval($register_globals){
00126 
00127         if (preg_match('/(\b)GLOBALS|_REQUEST|_SERVER|_ENV|_COOKIE|_GET|_POST|_FILES|_SESSION(\b)/i', $_SERVER['REQUEST_URI']) > 0) {
00128             die('Invalid Request');
00129         }
00130         if (mamboCore::get('mosConfig_sef')) {
00131             $subdir = mamboCore::get('subdirectory');
00132             $uri = substr($_SERVER['REQUEST_URI'], strlen($subdir));
00133             $url_array = explode('/', $uri);
00139             if ('' == $url_array[1]) return 0;
00140             $foundit = false;
00141             if ($url_array[1] == 'content') {
00142                 $foundit = true;
00143                 $_REQUEST['option'] = $_GET['option'] = $option = 'com_content';
00144 
00145                 // language hook for content
00146                 $lang = "";
00147                 $parms = array();
00148                 foreach($url_array as $key=>$value) {
00149                     if ( strcasecmp(substr($value,0,5),'lang,') == 0 ) {
00150                         $parts = explode(",", $value);
00151                         if (count($parts) > 1) {
00152                             $lang = $_REQUEST['lang'] = $_GET['lang'] = $parts[1];
00153                         }
00154                     }
00155                     elseif ( $value != '' AND $key > 1 ) $parms[] = $value;
00156                 }
00157 
00158                 // $option/$task/$sectionid/$id/$Itemid/$limit/$limitstart
00159                 $task = array_search($parms[0], $this->sef_content_task);
00160                 if ($task === false OR $task === null) return 1;
00161                 $_REQUEST['task'] = $_GET['task'] = $task;
00162                 $QUERY_STRING = "option=com_content&task=$task";
00163                 $num = count($parms);
00164                 for ($i = 1; $i <= $num-1; $i++) {
00165                     if (strcmp($parms[$i], (int)$parms[$i]) !== 0) return 1;
00166                 }
00167                 $i = 1;
00168                 if ($num == 6 OR $num == 4) {
00169                     $_REQUEST['sectionid'] = $_GET['sectionid'] = $sectionid = $parms[$i];
00170                     $QUERY_STRING .= "&sectionid=$sectionid";
00171                     $i++;
00172                 }
00173                 if ($num > 1) {
00174                     $_REQUEST['id'] = $_GET['id'] = $id = $parms[$i];
00175                     $QUERY_STRING .= "&id=$id";
00176                 }
00177                 if ($num > 2) {
00178                     $_REQUEST['Itemid'] = $_GET['Itemid'] = $Itemid = $parms[$i+1];
00179                     mamboCore::set('Itemid',$Itemid);
00180                     $QUERY_STRING .= "&Itemid=$Itemid";
00181                 }
00182                 if ($num > 4) {
00183                     $_REQUEST['limit'] = $_GET['limit'] = $limit = $parms[$i+2];
00184                     $_REQUEST['limitstart'] = $_GET['limitstart'] = $limitstart = $parms[$i+3];
00185                     $QUERY_STRING .= "&limit=$limit&limitstart=$limitstart";
00186                 }
00187 
00188                 if ($lang!="") {
00189                     $QUERY_STRING .= "&lang=$lang";
00190                 }
00191             }
00192 
00193             /*
00194             Components
00195             http://www.domain.com/component/$name,$value
00196             */
00197             elseif ($url_array[1] == 'component') {
00198                 $QUERY_STRING = $this->default_revert('component');
00199                 if ($QUERY_STRING) $foundit = true;
00200             }
00201             elseif ($url_array[1] == $this->prefix) {
00202                 $menuhandler = aliroMenuHandler::getInstance();
00203                 foreach ($this->custom_name as $i=>$compname) {
00204                     if ($url_array[2] == $compname) {
00205                         $origname = $this->custom_code[$i];
00206                         $_REQUEST['Itemid'] = $_GET['Itemid'] = $Itemid = $menuhandler->getIDLikeQuery("option=$origname");
00207                         mamboCore::set('Itemid', $Itemid);
00208                         if ($this->custom_PHP[$i]) {
00209                             $fixup = '$QUERY_STRING = "option='.$origname.'&Itemid=".$Itemid.sef_'.$this->custom_short[$i].'::revert($url_array,1);';
00210                             eval($fixup);
00211                         }
00212                         else $QUERY_STRING = "option=$origname&Itemid=$Itemid".$this->default_revert($compname);
00213                         $_REQUEST['option'] = $this->custom_code[$i];
00214                         $foundit = true;
00215                         break;
00216                     }
00217                 }
00218                 if (!$foundit) {
00219                     $content_sef = mamboCore::get('mosConfig_absolute_path').'/components/com_content/sef_ext.php';
00220                     if (file_exists($content_sef)) {
00221                         require_once($content_sef);
00222                         $QUERY_STRING = sef_content::revert($url_array,1);
00223                         if ($QUERY_STRING) $foundit = true;
00224                     }
00225                 }
00226             }
00227             if ($foundit) {
00228                 $_SERVER['QUERY_STRING'] = $QUERY_STRING;
00229                 $REQUEST_URI = '/index.php?'.$QUERY_STRING;
00230                 $_SERVER['REQUEST_URI'] = $REQUEST_URI;
00231                 return 0;
00232             }
00233             else return 1;
00234         }
00235         return 0;
00236     }
00237 
00238     function default_revert ($specialname) {
00239         $request = explode($specialname.'/', $_SERVER['REQUEST_URI']);
00240         if (isset($request[1])) $parmset = explode("/", $request[1]);
00241         else $parmset = array();
00242         $QUERY_STRING = '';
00243         $menuhandler = aliroMenuHandler::getInstance();
00244         foreach($parmset as $values) {
00245             $parts = explode(",", $values);
00246             if (count($parts) > 1) {
00247                 $_REQUEST[$parts[0]] = $_GET[$parts[0]] = $parts[1];
00248                 if ($parts[0] == 'option') {
00249                     $_REQUEST['Itemid'] = $_GET['Itemid'] = $Itemid = $menuhandler->getIDLikeQuery("option=$parts[1]");
00250                     mamboCore::set('Itemid', $Itemid);
00251                     $QUERY_STRING .= "$parts[0]=$parts[1]&Itemid=$Itemid";
00252                 }
00253                 $QUERY_STRING .= "&$parts[0]=$parts[1]";
00254             }
00255         }
00256         return $QUERY_STRING;
00257     }
00258 
00259     function sefRelToAbs( $string ) {
00260         global $iso_client_lang;
00261 
00262         $server = mamboCore::get('mosConfig_live_site');
00263         if ($string == 'index.php') return $server.'/';
00264 
00265         $passed_string = str_replace('&amp;', '&', $string);
00266         if (!mamboCore::get('mosConfig_sef') OR strtolower(substr($passed_string,0,9)) != 'index.php' OR eregi('^(([^:/?#]+):)',$passed_string)) {
00267             return preg_replace('/(&)([^#]|$)/','&amp;$2', $passed_string);
00268         }
00269 
00270         $string = substr($passed_string,10);
00271         if(mamboCore::get('mosConfig_mbf_content') AND strpos("lang=", strtolower($string)) === false) {
00272             $string .= "&lang=$iso_client_lang";
00273         }
00274         $option = $task = '';
00275         $oktasks = true;
00276         parse_str($string, $params);
00277         foreach ($params as $key=>$value) {
00278             $lowkey = strtolower($key);
00279             $lowvalue = strtolower($value);
00280             $unset = true;
00281             switch ($lowkey) {
00282                 case 'option':
00283                     $option = $lowvalue;
00284                     break;
00285                 case 'task':
00286                     $task = $value;
00287                     if ($lowvalue == 'new' OR $lowvalue == 'edit') $oktasks = false;
00288                     break;
00289                 default:
00290                     $check_params[$lowkey] = $key;
00291                     $unset = false;
00292             }
00293             if ($unset) unset($params[$key]);
00294         }
00295         // Process content items
00296         if (($option == 'com_content' OR $option == 'content') AND $oktasks) {
00297             /*
00298             Content
00299             index.php?option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart
00300             */
00301             if ($task) $task = $this->sef_content_task[$task];
00302             $content_sef = mamboCore::get('mosConfig_absolute_path').'/components/com_content/sef_ext.php';
00303             if (file_exists($content_sef)) {
00304                 require_once($content_sef);
00305                 return $server.'/'.$this->prefix.sef_content::create($task, $params);
00306             }
00307             $keys = array('sectionid', 'id', 'itemid', 'limit', 'limitstart', 'lang');
00308             $string = "/content/$task/";
00309             foreach ($keys as $key) {
00310                 if (isset($check_params[$key])) {
00311                     $pkey = $check_params[$key];
00312                     $string .= $params[$pkey].'/';
00313                 }
00314             }
00315             return $server.$string;
00316         }
00317         // Other types of URL than content do not use Itemid in the SEO version
00318         if (isset($check_params['itemid'])) {
00319             $pkey = $check_params['itemid'];
00320             unset($params[$pkey]);
00321             unset($check_params['itemid']);
00322         }
00323         // Process customised components
00324         $i = array_search($option,$this->custom_code);
00325         if ($i !== false AND $i !== null) {
00326             if ($this->custom_PHP[$i]) eval('$string = sef_'.$this->custom_short[$i].'::create($passed_string);');
00327             else $string = $this->componentDetails($params,$task);
00328             return $server.'/'.$this->prefix.'/'.$this->custom_name[$i].'/'.$string;
00329         }
00330         // Process ordinary components
00331         if (strpos($option,'com_')===0 AND $option != 'com_registration' AND $oktasks) {
00332             return "$server/component/option,$option/".$this->componentDetails($params,$task);
00333         }
00334         // Anything else is returned as received, except it is guaranteed that & will be &amp;
00335         return $server.'/'.str_replace( '&', '&amp;', $passed_string );
00336     }
00337 
00338     function componentDetails (&$params, $task) {
00339         $string = ($task ? "task,$task/" : '');
00340         foreach ($params as $key=>$param) $string .= "$key,$param/";
00341         return $string;
00342     }
00343 
00344 }
00345 
00346 ?>

Generated on Thu Apr 17 13:03:27 2008 for ALIRO by  doxygen 1.5.5