Source for file sef.php

Documentation is available at sef.php

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

Documentation generated on Mon, 05 May 2008 16:22:50 +0400 by phpDocumentor 1.4.0