|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Subudhinath Smarty Rookie
Joined: 24 Jul 2004 Posts: 11 Location: Finland
|
Posted: Wed Jul 28, 2004 1:12 pm Post subject: postfilter rel2abs 1.0 released: Makes all URI:s absolute |
|
|
This postfilter converts all relative URI:s to absolute ones so that the linking to images, css-files, js-files and other webpages will work.
The point of this is that many template-designers may be people who aren't interested in technical details and therefore just want to design their webpages in i.e. Dreamweaver as they're used to. In this way their templates will work just as they are no matter where the php-script using them is located.
More information on the history of this script can be found under: Feature Requests: Relative to Absolute URLs during compilation:
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=3083
Please test it, improve it, comment on it...
Example of things that could be improved:
- it'd be good if it would figure out the absolute URI of the template file by itself so that it wouldn't need the input to $GLOBALS['tpl_absuri']
- to improve performance it'd be good if the ereg-stuff could be replaced by preg, I just don't know how to use preg
[php:1:d57b4bbaa9]<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: postfilter.rel2abs.php
* Type: postfilter
* Name: rel2abs
* Purpose: Converts URI:s from relative to absolute
* -------------------------------------------------------------
*/
/*
Smarty postfilter rel2abs 1.0 - released July 28, 2004
The postfilter rel2abs detects relative URI:s in a template-file
and uses make_abs (see below) to make them absolute and then replaces them.
The absolute URI of the template has to be put into the variable
$GLOBALS['tpl_absuri'](please leave out http://domain)
(i.e. /templates/something/something.tpl
and not http://domain/templates/something/something.tpl)
If anybody figures out a way to not having to input $GLOBALS['tpl_absuri']
please let me know. Other comments, suggestions and modifications are welcome.
It was written by Simon Rönnqvist, the most recent version can
be found at http://ownmedia.net/products/
make_abs takes a relative URI (i.e. ../../css/stylesheet.css)
asd and the absolute URI (i.e. /templates/something/something.tpl)
of the document in which the relative URI was found
and outputs an absolute one (i.e. /css/stylesheet.css)
make_abs was written by Andreas Friedrich http://www.x-author.de/
and found at http://www.webmasterworld.com/forum88/334.htm
*/
function make_abs($rel_uri, $base, $REMOVE_LEADING_DOTS = true) {
preg_match("'^([^:]+://[^/]+)/'", $base, $m);
$base_start = $m[1];
if (preg_match("'^/'", $rel_uri)) {
return $base_start . $rel_uri;
}
$base = preg_replace("{[^/]+$}", '', $base);
$base .= $rel_uri;
$base = preg_replace("{^[^:]+://[^/]+}", '', $base);
$base_array = explode('/', $base);
if (count($base_array) and!strlen($base_array[0]))
array_shift($base_array);
$i = 1;
while ($i < count($base_array)) {
if ($base_array[$i - 1] == ".") {
array_splice($base_array, $i - 1, 1);
if ($i > 1) $i--;
} elseif ($base_array[$i] == ".." and $base_array[$i - 1]!= "..") {
array_splice($base_array, $i - 1, 2);
if ($i > 1) {
$i--;
if ($i == count($base_array)) array_push($base_array, "");
}
} else {
$i++;
}
}
if (count($base_array) and $base_array[-1] == ".")
$base_array[-1] = "";
/* How do we treat the case where there are still some leading ../
segments left? According to RFC2396 we are free to handle that
any way we want. The default is to remove them.
#
"If the resulting buffer string still begins with one or more
complete path segments of "..", then the reference is considered
to be in error. Implementations may handle this error by
retaining these components in the resolved path (i.e., treating
them as part of the final URI), by removing them from the
resolved path (i.e., discarding relative levels above the root),
or by avoiding traversal of the reference."
#
http://www.faqs.org/rfcs/rfc2396.html 5.2.6.g
*/
if ($REMOVE_LEADING_DOTS) {
while (count($base_array) and preg_match("/^\.\.?$/", $base_array[0])) {
array_shift($base_array);
}
}
return($base_start . '/' . implode("/", $base_array));
}
function smarty_postfilter_rel2abs($compiled, &$smarty) {
//Extracts strings containing href or src="something"
//that "something" can't begin with / or contain a :
//because that would indicate that its already a relative URI
while (eregi("(href|src|action)=\"(([^/])[[:alnum:]/+=%&_.~?-]*)\"", $compiled, $regs)) {
$input_uri = $regs[2];
//Inputs the extracted string into the function make_abs
$output_uri = make_abs($input_uri, $GLOBALS['tpl_absuri']);
//Replaces the relative URI with the absolute one
$compiled = ereg_replace("((href|src|action)=\")$input_uri(\")", "\\1$output_uri\\3", $compiled);
//Repeats over again until no relative URI:s are detected
}
if (!isset($GLOBALS['tpl_absuri'])) {
$compiled = "ERROR: Since the variable \$GLOBALS['tpl_absuri'] defining the template's absolute URI is not set the Smarty plug-in postfilter.rel2abs.php treats it like it'd be in the site root.\n
Please define \$GLOBALS['tpl_absuri'] and then make sure this template is recompiled." . $compiled;
}
return($compiled);
}
?>
[/php:1:d57b4bbaa9]
Last edited by Subudhinath on Thu Jul 29, 2004 1:03 am; edited 1 time in total |
|
Back to top |
|
Subudhinath Smarty Rookie
Joined: 24 Jul 2004 Posts: 11 Location: Finland
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|