New Stylesheets Tag {pURL_stylesheet}
Posted: Fri Jul 27, 2007 5:08 pm
I made the tag as a drop in replacement for the existing {stylesheet} tag.
The idea of this tag is to be able to have Pretty URLs for stylesheets. I'm sure thats not at the top of most peoples list of required features, but User Agents do no always properly cache stylesheets that use the query string.
Using this tag you can set a far future expires header for your stylesheets, hopefully streamlining your visitors access to your site.
Anyway here is the tag, any ideas for improvement would be appreciated.
The idea of this tag is to be able to have Pretty URLs for stylesheets. I'm sure thats not at the top of most peoples list of required features, but User Agents do no always properly cache stylesheets that use the query string.
Using this tag you can set a far future expires header for your stylesheets, hopefully streamlining your visitors access to your site.
Anyway here is the tag, any ideas for improvement would be appreciated.
Code: Select all
<?php
#CMS - CMS Made Simple
#(c)2007 by Matthew Miller (junkmailtrapenator@gmail.com)
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
function smarty_cms_function_pURL_stylesheet($params, &$smarty)
{
global $gCms;
$config = &$gCms->config;
$pageinfo = &$gCms->variables['pageinfo'];
$db =& $gCms->GetDb();
$templateid = $pageinfo->template_id;
$stylesheets='';
$case=3;
if(isset($params['static']) && $params['static']=='true'){
$case=1;
}elseif(isset($config["assume_mod_rewrite"]) && $config["assume_mod_rewrite"] == 'true'){
$case=2;
}
$dbprefix = $config['db_prefix'];
if (!empty($params['name'])) {
$sql = "SELECT css_text, css_name, media_type, UNIX_TIMESTAMP(modified_date) AS mtime FROM {$dbprefix}css WHERE css_name = " . $db->qstr($params['name']);
} else {
$sql = "SELECT c.css_text, c.css_id, c.css_name, c.media_type, UNIX_TIMESTAMP(c.modified_date) AS mtime FROM {$dbprefix}css c, {$dbprefix}css_assoc ac WHERE ac.assoc_type='template' AND ac.assoc_to_id = $templateid AND ac.assoc_css_id = c.css_id ORDER BY ac.create_date";
}
$result = $db->Execute($sql);
$dirname = dirname(__FILE__) . '/../stylesheets/static/';
$dir = dir($dirname);
while ($result && $row = $result->FetchRow()) {
$filename = preg_replace('/[^0-9a-zA-ZäöüÄÖÜß\-\.\,]/', '_', $row['css_name']) .'-'.$row['mtime']. '.css';
if (!empty($params['name']) and !empty($params['media'])) {
$media = $row['media_type'] ? "$params[media]" : '';
} else {
$media = $row['media_type'] ? "$row[media_type]" : '';
}
switch($case){
case 1:
$stylesheets[$filename]['href']=$config['root_url']."/stylesheets/static/$filename";
if($media==''){
$stylesheets[$filename]['media']="";
}else{
$stylesheets[$filename]['media']="media='$media'";
}
break;
case 2:
if(!(isset($stylesheets[$media])&&$row['mtime']<=$stylesheets[$media]['mtime'])){
$stylesheets[$media]['href']=$config['root_url'].'/stylesheets/'.$templateid.'-'.$row['mtime'].'/';
if($media!=""){
$stylesheets[$media]['href'].=$media.'/';
}
$stylesheets[$media]['mtime']=$row['mtime'];
if($media==''){
$stylesheets[$media]['media']="";
}else{
$stylesheets[$media]['media']="media='$media'";
}
}
break;
case 3:
$stylesheets[$media]['href']=$config['root_url'].'/stylesheet.php?templateid='.$templateid;
if ($media != ''){
$html .= '&mediatype='.urlencode($media);
}
if($media==''){
$stylesheets[$media]['media']="";
}else{
$stylesheets[$media]['media']="media='$media'";
}
break;
}
if (!file_exists("$dirname/$filename")&&$case==1) {
$fp = fopen("$dirname/$filename", 'w');
fwrite($fp, $row['css_text']);
fclose($fp);
}
}
$html='';
foreach($stylesheets as $link){
$html.="<link rel='stylesheet' type='text/css' ".$link['media']." href='".$link['href']."' />\n";
}
if (!(isset($config["use_smarty_php_tags"]) && $config["use_smarty_php_tags"] == true))
{
$html = ereg_replace("\{\/?php\}", "", $html);
}
return $html;
}
function smarty_cms_help_function_pURL_stylesheet() {
?>
<h3>What does this do?</h3>
<p>Gets stylesheet information from the system. By default, it grabs all of the stylesheets attached to the current template.</p>
<h3>What else does it do</h3>
<p>If you have $config['assume_mod_rewrite'] set to true in your config.php file then it will output PrettyURLS™ with the last modified timestamp as part of the URL. The addtion of the timestamp allows you to add far future expiers headers to your stylesheets without sacrificing the ability to quickly update your stylesheets.</p>
<h4>To use PrettyURLS™ you must add rules similar to the ones below to your rewrite rules.</h4>
<p><code>
RewriteRule ^stylesheets/([0-9]+)-([0-9]+)/([A-za-z0-9-]+)/?$ stylesheet.php?templateid=$1&mediatype=$3 [NC,L]<br/>
RewriteRule ^stylesheets/([0-9]+)-([0-9]+)/$ stylesheet.php?templateid=$1 [NC,L]<br/>
RewriteRule ^stylesheets/([A-za-z][A-za-z0-9-]+)-([0-9]+)/?$ stylesheet.php?name=$1 [NC,L]<br/>
</code></p>
<p>Note: Stylesheet names can't begin with a number.</p>
<h4>To add expires headers (in Apache) do something like the following in your config (.htaccess).</h4>
<p><code><pre>
###############################
##Cache static content
###############################
# enable expirations
ExpiresActive On
# expire css stylesheets after 1 year
ExpiresByType text/css A31556926</pre></code></p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your template/page's head section like: <code>{pURL_stylesheet}</code></p>
<h3>What parameters does it take?</h3>
<ul>
<li><em>(optional)</em>name - Instead of getting all stylesheets for the given page, it will only get one spefically named one, whether it's attached to the current template or not.</li>
<li><em>(optional)</em>media - If name is defined, this allows you set a different media type for that stylesheet.</li>
<li><em>(optional)</em>static - If set to "true" this publishes the stylesheets to static files.</li>
</ul>
<?php
}
function smarty_cms_about_function_pURL_stylesheet() {
?>
<p>Author: Matthew Miller<junkmailtrapenator@gmail.com></p>
<p>Version: 1.0</p>
<p>
Change History:<br/>
None
</p>
<?php
}
?>