<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */
/* to activate it 1) you need to copy the file in lib/smarty_tiki directory 2) you need to add $smarty->load_filter('output','sef'); in setup_smarty.php just after the other load_filter
 */
/**
 * Smarty sef outputfilter plugin
 *
 * File:     outputfilter.sef.php<br>
 * Type:     outputfilter<br>
 * Name:     sef<br>
 * Date:     Jan 25, 2003<br>
 * Purpose:  Converts one-argument dynamic URLs to Search-Engine-Friendly
 *           URLs. Needs to be combined with apache's mod_rewrite.
 *           Takes care not to rewrite remote URLs.
 * Install:  Drop into the plugin directory, call 
 *           <code>$smarty->load_filter('output','sef');</code>
 *           from application.
 * @author   David Young <davidy@funkypenguin.co.za>
 * @author   Inspired by article on Coofer Cat's Weblog
 * @author   http://www.coofercat.com/2003/00000010.html
 * @version  0.1 
 * @param string
 * @param Smarty
* @ modify par sylvieg to handle href="tiki-index.php" (with no /) and with complete pagename
 */

#__________________________________________
#                                          |
#   |~|_        -- Funky Penguin --        |
#   o-o        Open Source Solutions       |
#   /V\                                    |
#  // \\      Proudly South African! :)    |
# /(   )\  ..Work smarter, not harder..    |
#  ^-~-^     [www.funkypenguin.co.za]      |
###########################################|
//Function:    sefalize 
//Description: Intended for converting one-argument dynamic URLs to
//             Search-Engine-Friendly URLs. Unsuitable for scripts
//             which are passed more than one argument. 
//Arguments:   $url         - the regex pattern to search for 
//             $replacement - the regex pattern to replace
//             $string      - the string we are smarty-output-filtering

function sefalize($url,$replacement,$string) {
   
   return preg_replace("/href=('|\")$url\\1/i","href=$1$replacement$1",$string);
 }

###########################################|
//Function:    niceify 
//Description: a scaled-down version of "sefalize". Intended just for renaming
//             static scripts, not scripts which are passed variables
//Arguments:   $url         - the script to "nice-ify"
//             $replacement - the "nice" name to give the script
//             $string      - the string we are smarty-output-filtering

function niceify($url,$replacement,$string) {
   return preg_replace("/href=('|\")$url\\1/i","href=$1$replacement$1",$string);
 }
 
function smarty_outputfilter_sef($string)
{
global $feature_server_name;
if ($feature_server_name == '') return $string;

# Sort out relative links - else things will get REALLY messy
 $string=preg_replace('/(href|src|action)=(["|\'])([^\/][^:\s]+)(["|\'])/i','$1=$2$3$4',$string);
# because of the base href, the anchor must be renamed
$string=preg_replace('/href=(["|\'])#/', 'href=$1'.$_SERVER['REQUEST_URI'].'#', $string);

# SEF-alize the scripts that are worth SEF-alizing
$string=sefalize('tiki-view_blog.php\?blogId=([0-9]+)','blog/$2',$string);
$string = preg_replace("/href=(')tiki-index.php\?page=([^'.]*)(')/i","href=$1$2$3",$string);
$string = preg_replace("/href=(\")tiki-index.php\?page=([^\".]*)(\")/i","href=$1$2$3",$string);
#$string=sefalize('tiki-index.php\?page=([^\".]*)','$2',$string);
#$string=sefalize('tiki-index.php\?page=([^\"]*)','page/$2',$string);
$string=sefalize('tiki-read_article.php\?articleId=([0-9]+)','article/$2',$string);
$string=sefalize('tiki-list_file_gallery.php\?galleryId=([0-9]+)','file/$2',$string);
$string=sefalize('tiki-download_file.php\?fileId=([0-9]+)','download/$2',$string);
$string=sefalize('tiki-browse_categories.php\?parentId=([0-9]+)','category/$2',$string);
$string=sefalize('tiki-browse_gallery.php\?galleryId=([0-9]+)','gallery/$2',$string);
$string=sefalize('tiki-view_forum.php\?forumId=([0-9]+)','forum/$2',$string);
$string=sefalize('tiki-view_faq.php\?faqId=([0-9]+)','faq/$2',$string);
$string=sefalize('tiki-take_quiz.php\?quizId=([0-9]+)','quiz/$2',$string);
$string=sefalize('tiki-view_tracker.php\?trackerId=([0-9]+)','tracker/$2',$string);
$string=sefalize('tiki-take_survey.php\?surveyId=([0-9]+)','survey/$2',$string);


# generalize some script names 
$string=niceify('tiki-view_articles.php','articles',$string);
$string=niceify('tiki-list_blogs.php','blogs',$string);
$string=niceify('tiki-browse_categories.php','categories',$string);
$string=niceify('tiki-list_charts.php','charts',$string);
$string=niceify('tiki-chat.php','chat',$string);
$string=niceify('tiki-contact.php','contact',$string);
$string=niceify('tiki-directory_browse.php','directories',$string);
$string=niceify('tiki-eph.php','eph',$string);
$string=niceify('tiki-list_faqs.php','faqs',$string);
$string=niceify('tiki-file_galleries.php','files',$string);
$string=niceify('tiki-forums.php','forums',$string);
$string=niceify('tiki-galleries.php','images',$string);
$string=niceify('tiki-list_games.php','games',$string);
$string=niceify('tiki-my_tiki.php','my',$string);
$string=niceify('tiki-newsletters.php','newsletters',$string);
$string=niceify('tiki-list_quizzes.php','quizzes',$string);
$string=niceify('tiki-stats.php','stats',$string);
$string=niceify('tiki-list_surveys.php','surveys',$string);
$string=niceify('tiki-list_trackers.php','trackers',$string);
$string=niceify('tiki-irc_logs.php','irc',$string);
$string=niceify('tiki-mobile.php','mobile',$string);
# $string=niceify('tiki-mobile.php','wml',$string);
# $string=niceify('tiki-index.php','wiki',$string);
 
return $string; 
}

?>
