first commit. dokuwiki.
[h2N7SspZmY.git] / lib / exe / css.php
blobcb689d0158dd6728ace8f593736869faa6f85576
1 <?php
2 /**
3 * DokuWiki StyleSheet creator
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
9 if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
10 if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
11 if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here
12 require_once(DOKU_INC.'inc/init.php');
13 require_once(DOKU_INC.'inc/pageutils.php');
14 require_once(DOKU_INC.'inc/httputils.php');
15 require_once(DOKU_INC.'inc/io.php');
16 require_once(DOKU_INC.'inc/confutils.php');
18 // Main (don't run when UNIT test)
19 if(!defined('SIMPLE_TEST')){
20 header('Content-Type: text/css; charset=utf-8');
21 css_out();
25 // ---------------------- functions ------------------------------
27 /**
28 * Output all needed Styles
30 * @author Andreas Gohr <andi@splitbrain.org>
32 function css_out(){
33 global $conf;
34 global $lang;
35 $style = '';
36 if (isset($_REQUEST['s']) &&
37 in_array($_REQUEST['s'], array('all', 'print', 'feed'))) {
38 $style = $_REQUEST['s'];
41 $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t']));
42 if($tpl){
43 $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/';
44 $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/';
45 }else{
46 $tplinc = DOKU_TPLINC;
47 $tpldir = DOKU_TPL;
50 // The generated script depends on some dynamic options
51 $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$style,'.css');
53 // load template styles
54 $tplstyles = array();
55 if(@file_exists($tplinc.'style.ini')){
56 $ini = parse_ini_file($tplinc.'style.ini',true);
57 foreach($ini['stylesheets'] as $file => $mode){
58 $tplstyles[$mode][$tplinc.$file] = $tpldir;
62 // Array of needed files and their web locations, the latter ones
63 // are needed to fix relative paths in the stylesheets
64 $files = array();
65 //if (isset($tplstyles['all'])) $files = array_merge($files, $tplstyles['all']);
66 if(!empty($style)){
67 $files[DOKU_INC.'lib/styles/'.$style.'.css'] = DOKU_BASE.'lib/styles/';
68 // load plugin, template, user styles
69 $files = array_merge($files, css_pluginstyles($style));
70 if (isset($tplstyles[$style])) $files = array_merge($files, $tplstyles[$style]);
71 $files[DOKU_CONF.'user'.$style.'.css'] = DOKU_BASE;
72 }else{
73 $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/';
74 // load plugin, template, user styles
75 $files = array_merge($files, css_pluginstyles('screen'));
76 if (isset($tplstyles['screen'])) $files = array_merge($files, $tplstyles['screen']);
77 if($lang['direction'] == 'rtl'){
78 if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']);
80 $files[DOKU_CONF.'userstyle.css'] = DOKU_BASE;
83 // check cache age & handle conditional request
84 header('Cache-Control: public, max-age=3600');
85 header('Pragma: public');
86 if(css_cacheok($cache,array_keys($files),$tplinc)){
87 http_conditionalRequest(filemtime($cache));
88 if($conf['allowdebug']) header("X-CacheUsed: $cache");
90 // finally send output
91 if ($conf['gzip_output'] && http_gzip_valid($cache)) {
92 header('Vary: Accept-Encoding');
93 header('Content-Encoding: gzip');
94 readfile($cache.".gz");
95 } else {
96 if (!http_sendfile($cache)) readfile($cache);
99 return;
100 } else {
101 http_conditionalRequest(time());
104 // start output buffering and build the stylesheet
105 ob_start();
107 // print the default classes for interwiki links and file downloads
108 css_interwiki();
109 css_filetypes();
111 // load files
112 foreach($files as $file => $location){
113 print css_loadfile($file, $location);
116 // end output buffering and get contents
117 $css = ob_get_contents();
118 ob_end_clean();
120 // apply style replacements
121 $css = css_applystyle($css,$tplinc);
123 // compress whitespace and comments
124 if($conf['compress']){
125 $css = css_compress($css);
128 // save cache file
129 io_saveFile($cache,$css);
130 if(function_exists('gzopen')) io_saveFile("$cache.gz",$css);
132 // finally send output
133 if ($conf['gzip_output']) {
134 header('Vary: Accept-Encoding');
135 header('Content-Encoding: gzip');
136 print gzencode($css,9,FORCE_GZIP);
137 } else {
138 print $css;
143 * Checks if a CSS Cache file still is valid
145 * @author Andreas Gohr <andi@splitbrain.org>
147 function css_cacheok($cache,$files,$tplinc){
148 global $config_cascade;
150 if(isset($_REQUEST['purge'])) return false; //support purge request
152 $ctime = @filemtime($cache);
153 if(!$ctime) return false; //There is no cache
155 // some additional files to check
156 $files = array_merge($files, getConfigFiles('main'));
157 $files[] = $tplinc.'style.ini';
158 $files[] = __FILE__;
160 // now walk the files
161 foreach($files as $file){
162 if(@filemtime($file) > $ctime){
163 return false;
166 return true;
170 * Does placeholder replacements in the style according to
171 * the ones defined in a templates style.ini file
173 * @author Andreas Gohr <andi@splitbrain.org>
175 function css_applystyle($css,$tplinc){
176 if(@file_exists($tplinc.'style.ini')){
177 $ini = parse_ini_file($tplinc.'style.ini',true);
178 $css = strtr($css,$ini['replacements']);
180 return $css;
184 * Prints classes for interwikilinks
186 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
187 * $name is the identifier given in the config. All Interwiki links get
188 * an default style with a default icon. If a special icon is available
189 * for an interwiki URL it is set in it's own class. Both classes can be
190 * overwritten in the template or userstyles.
192 * @author Andreas Gohr <andi@splitbrain.org>
194 function css_interwiki(){
196 // default style
197 echo 'a.interwiki {';
198 echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
199 echo ' padding-left: 16px;';
200 echo '}';
202 // additional styles when icon available
203 $iwlinks = getInterwiki();
204 foreach(array_keys($iwlinks) as $iw){
205 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
206 if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
207 echo "a.iw_$class {";
208 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
209 echo '}';
210 }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
211 echo "a.iw_$class {";
212 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
213 echo '}';
219 * Prints classes for file download links
221 * @author Andreas Gohr <andi@splitbrain.org>
223 function css_filetypes(){
225 // default style
226 echo 'a.mediafile {';
227 echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
228 echo ' padding-left: 18px;';
229 echo ' padding-bottom: 1px;';
230 echo '}';
232 // additional styles when icon available
233 // scan directory for all icons
234 $exts = array();
235 if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
236 while(false !== ($file = readdir($dh))){
237 if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
238 $ext = strtolower($match[1]);
239 $type = '.'.strtolower($match[2]);
240 if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
241 $exts[$ext] = $type;
245 closedir($dh);
247 foreach($exts as $ext=>$type){
248 $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
249 echo "a.mf_$class {";
250 echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
251 echo '}';
256 * Loads a given file and fixes relative URLs with the
257 * given location prefix
259 function css_loadfile($file,$location=''){
260 if(!@file_exists($file)) return '';
261 $css = io_readFile($file);
262 if(!$location) return $css;
264 $css = preg_replace('#(url\([ \'"]*)((?!/|http://|https://| |\'|"))#','\\1'.$location.'\\3',$css);
265 return $css;
270 * Returns a list of possible Plugin Styles (no existance check here)
272 * @author Andreas Gohr <andi@splitbrain.org>
274 function css_pluginstyles($mode='screen'){
275 global $lang;
276 $list = array();
277 $plugins = plugin_list();
278 foreach ($plugins as $p){
279 if($mode == 'all'){
280 $list[DOKU_PLUGIN."$p/all.css"] = DOKU_BASE."lib/plugins/$p/";
281 }elseif($mode == 'print'){
282 $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/";
283 }elseif($mode == 'feed'){
284 $list[DOKU_PLUGIN."$p/feed.css"] = DOKU_BASE."lib/plugins/$p/";
285 }else{
286 $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
287 $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
289 if($lang['direction'] == 'rtl'){
290 $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/";
293 return $list;
297 * Very simple CSS optimizer
299 * @author Andreas Gohr <andi@splitbrain.org>
301 function css_compress($css){
302 //strip comments through a callback
303 $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
305 //strip (incorrect but common) one line comments
306 $css = preg_replace('/(?<!:)\/\/.*$/m','',$css);
308 // strip whitespaces
309 $css = preg_replace('![\r\n\t ]+!',' ',$css);
310 $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css);
312 // shorten colors
313 $css = preg_replace("/#([0-9a-fA-F]{1})\\1([0-9a-fA-F]{1})\\2([0-9a-fA-F]{1})\\3/", "#\\1\\2\\3",$css);
315 return $css;
319 * Callback for css_compress()
321 * Keeps short comments (< 5 chars) to maintain typical browser hacks
323 * @author Andreas Gohr <andi@splitbrain.org>
325 function css_comment_cb($matches){
326 if(strlen($matches[2]) > 4) return '';
327 return $matches[0];
330 //Setup VIM: ex: et ts=4 enc=utf-8 :