tiki-sefurl.php 3.75 KB
Newer Older
1
<?php
changi67's avatar
changi67 committed
2 3
// (c) Copyright 2002-2009 by authors of the Tiki Wiki/CMS/Groupware Project
// 
4 5 6 7
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// Function created 2008-07-14 SEWilco (scot@wilcoxon.org)
// 2009-01-12 SEWilco (scot@wilcoxon.org) Modified for feature_sefurl_filter.
changi67's avatar
changi67 committed
8
if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
9 10 11
	header("location: index.php");
	exit;
}
12
define('PATTERN_TO_CLEAN_TEXT', '/[^0-9a-zA-Z_]/');
13 14
define('CLEAN_CHAR', '-');
define('TITLE_SEPARATOR', '-');
changi67's avatar
changi67 committed
15
function filter_out_sefurl($tpl_output, &$smarty, $type = null, $title = null) {
16 17 18 19
	global $sefurl_regex_out, $tikilib, $prefs;
	if ($prefs['feature_sefurl'] != 'y') {
		return $tpl_output;
	}
changi67's avatar
changi67 committed
20 21
	global $cachelib;
	include_once ('lib/cache/cachelib.php');
22 23 24 25 26 27 28 29 30 31 32 33
	if (!is_array($sefurl_regex_out)) {
		if (!$cachelib->isCached('sefurl_regex_out')) {
			$query = 'select * from `tiki_sefurl_regex_out` where `silent` != ? order by `order` asc';
			$result = $tikilib->query($query, array('y'));
			$sefurl_regex_out = array();
			if (!empty($result)) {
				while ($res = $result->fetchRow()) {
					if (empty($res['feature']) || $prefs[$res['feature']] == 'y') {
						$sefurl_regex_out[] = $res;
					}
				}
			}
changi67's avatar
changi67 committed
34
			$cachelib->cacheItem('sefurl_regex_out', serialize($sefurl_regex_out));
35 36 37 38
		} else {
			$sefurl_regex_out = unserialize($cachelib->getCached('sefurl_regex_out'));
		}
	}
39
	$title = '';
40
	if ($type == 'article' && $prefs['feature_sefurl_title_article'] == 'y') {
changi67's avatar
changi67 committed
41 42
		global $artlib;
		include_once ('lib/articles/artlib.php');
43
		if (preg_match('/articleId=([0-9]+)/', $tpl_output, $matches)) {
changi67's avatar
changi67 committed
44
			if (empty($title)) $title = $artlib->get_title($matches[1]);
sylvieg's avatar
sylvieg committed
45
			$title = preg_replace(PATTERN_TO_CLEAN_TEXT, CLEAN_CHAR, $tikilib->take_away_accent($title));
changi67's avatar
changi67 committed
46 47
			$title = preg_replace('/' . CLEAN_CHAR . CLEAN_CHAR . '+/', '-', $title);
			$title = preg_replace('/' . CLEAN_CHAR . '+$/', '', $title);
48 49 50
		}
	}
	if ($type == 'blog' && $prefs['feature_sefurl_title_blog'] == 'y') {
changi67's avatar
changi67 committed
51 52
		global $bloglib;
		include_once ('lib/blogs/bloglib.php');
53
		if (preg_match('/blogId=([0-9]+)/', $tpl_output, $matches)) {
changi67's avatar
changi67 committed
54
			if (empty($title)) $title = $bloglib->get_title($matches[1]);
sylvieg's avatar
sylvieg committed
55
			$title = preg_replace(PATTERN_TO_CLEAN_TEXT, CLEAN_CHAR, $tikilib->take_away_accent($title));
changi67's avatar
changi67 committed
56 57
			$title = preg_replace('/' . CLEAN_CHAR . CLEAN_CHAR . '+/', '-', $title);
			$title = preg_replace('/' . CLEAN_CHAR . '+$/', '', $title);
58 59
		}
	}
60
	if ($type == 'blogpost' && $prefs['feature_sefurl_title_blog'] == 'y') {
changi67's avatar
changi67 committed
61 62
		global $bloglib;
		include_once ('lib/blogs/bloglib.php');
63 64
		if (preg_match('/postId=([0-9]+)/', $tpl_output, $matches)) {
			if (empty($title)) {
changi67's avatar
changi67 committed
65
				if ($post_info = $bloglib->get_post($matches[1])) $title = $post_info['title'];
66 67
			}
			$title = preg_replace(PATTERN_TO_CLEAN_TEXT, CLEAN_CHAR, $tikilib->take_away_accent($title));
changi67's avatar
changi67 committed
68 69
			$title = preg_replace('/' . CLEAN_CHAR . CLEAN_CHAR . '+/', '-', $title);
			$title = preg_replace('/' . CLEAN_CHAR . '+$/', '', $title);
70 71
		}
	}
changi67's avatar
changi67 committed
72
	foreach($sefurl_regex_out as $regex) {
73
		if (empty($type) || $type == $regex['type']) {
74 75
			// if a question mark in pattern, deal with possible additional terms
			// The '?&' isn't pretty but seems to work.
76 77 78
			//if( strpos($regex['left'],'?') !== FALSE ) {
			//	$tpl_output = preg_replace( '/'.$regex['left'].'&/', $regex['right'].'?&', $tpl_output );
			//}
changi67's avatar
changi67 committed
79
			$tpl_output = preg_replace('/' . $regex['left'] . '/', $regex['right'], $tpl_output);
80 81
		}
	}
82
	if (!empty($title)) {
changi67's avatar
changi67 committed
83
		$tpl_output.= TITLE_SEPARATOR . $title;
84
	}
sylvieg's avatar
sylvieg committed
85
	if (is_array($prefs['feature_sefurl_paths'])) {
changi67's avatar
changi67 committed
86
		foreach($prefs['feature_sefurl_paths'] as $path) {
sylvieg's avatar
sylvieg committed
87
			if (isset($_REQUEST[$path])) {
changi67's avatar
changi67 committed
88
				$tpl_output = urlencode($_REQUEST[$path]) . "/$tpl_output";
sylvieg's avatar
sylvieg committed
89
			}
90 91 92
		}
	}
	return $tpl_output;
93
}