xref: /dokuwiki/lib/exe/css.php (revision 1c2d1019f48fa6b2dbc154f7534341ad0869a5d7)
1<?php
2/**
3 * DokuWiki StyleSheet creator
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
11require_once(DOKU_INC.'inc/init.php');
12require_once(DOKU_INC.'inc/pageutils.php');
13require_once(DOKU_INC.'inc/io.php');
14require_once(DOKU_INC.'inc/confutils.php');
15
16// Main (don't run when UNIT test)
17if(!defined('SIMPLE_TEST')){
18    header('Content-Type: text/css; charset=utf-8');
19    css_out();
20}
21
22
23// ---------------------- functions ------------------------------
24
25/**
26 * Output all needed Styles
27 *
28 * @author Andreas Gohr <andi@splitbrain.org>
29 */
30function css_out(){
31    global $conf;
32    global $lang;
33    $print = (bool) $_REQUEST['print'];   //print mode?
34
35    // The generated script depends on some dynamic options
36    $cache = getCacheName('styles'.$print,'.css');
37
38    // Array of needed files and their web locations, the latter ones
39    // are needed to fix relative paths in the stylesheets
40    $files   = array();
41    if($print){
42        $files[DOKU_TPLINC.'print.css'] = DOKU_TPL;
43        // load plugin styles
44        $files = array_merge($files, css_pluginstyles('print'));
45        $files[DOKU_CONF.'userprint.css'] = '';
46    }else{
47        $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/';
48        //fixme spellchecker style
49        $files[DOKU_TPLINC.'layout.css'] = DOKU_TPL;
50        $files[DOKU_TPLINC.'design.css'] = DOKU_TPL;
51        if($lang['direction'] == 'rtl'){
52            $files[DOKU_TPLINC.'rtl.css'] = DOKU_TPL;
53        }
54        // load plugin styles
55        $files = array_merge($files, css_pluginstyles('screen'));
56        $files[DOKU_CONF.'userstyle.css'] = '';
57    }
58
59    // check cache age
60    if(css_cacheok($cache,array_keys($files))){
61        readfile($cache);
62        return;
63    }
64
65    // start output buffering and build the stylesheet
66    ob_start();
67
68    // print the default classes for Interwikilinks
69    css_interwiki();
70
71    // load files
72    foreach($files as $file => $location){
73        print css_loadfile($file, $location);
74    }
75
76    // end output buffering and get contents
77    $css = ob_get_contents();
78    ob_end_clean();
79
80    // compress whitespace and comments
81    if($conf['compress']){
82        $css = css_compress($css);
83    }
84
85    // save cache file
86    io_saveFile($cache,$css);
87
88    // finally send output
89    print $css;
90}
91
92/**
93 * Checks if a CSS Cache file still is valid
94 *
95 * @author Andreas Gohr <andi@splitbrain.org>
96 */
97function css_cacheok($cache,$files){
98    $ctime = @filemtime($cache);
99    if(!$ctime) return false; //There is no cache
100
101    // some additional files to check
102    $files[] = DOKU_CONF.'dokuwiki.conf';
103    $files[] = DOKU_CONF.'local.conf';
104    $files[] = __FILE__;
105
106    // now walk the files
107    foreach($files as $file){
108        if(@filemtime($file) > $ctime){
109            return false;
110        }
111    }
112    return true;
113}
114
115/**
116 * Prints classes for interwikilinks
117 *
118 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
119 * $name is the identifier given in the config. All Interwiki links get
120 * an default style with a default icon. If a special icon is available
121 * for an interwiki URL it is set in it's own class. Both classes can be
122 * overwritten in the template or userstyles.
123 *
124 * @author Andreas Gohr <andi@splitbrain.org>
125 */
126function css_interwiki(){
127
128    // default style
129    echo 'a.interwiki {';
130    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
131    echo ' padding-left: 16px;';
132    echo '}';
133
134    // additional styles when icon available
135    $iwlinks = getInterwiki();
136    foreach(array_keys($iwlinks) as $iw){
137        if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
138            echo "a.iw_$iw {";
139            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
140            echo '}';
141        }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
142            echo "a.iw_$iw {";
143            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
144            echo '}';
145        }
146    }
147
148}
149
150/**
151 * Loads a given file and fixes relative URLs with the
152 * given location prefix
153 */
154function css_loadfile($file,$location=''){
155    if(!@file_exists($file)) return '';
156    $css = io_readFile($file);
157    if(!$location) return $css;
158
159    $css = preg_replace('!(url\( *)([^/])!','\\1'.$location.'\\2',$css);
160    return $css;
161}
162
163/**
164 * Returns a list of possible Plugin Styles (no existance check here)
165 *
166 * @author Andreas Gohr <andi@splitbrain.org>
167 */
168function css_pluginstyles($mode='screen'){
169    $list = array();
170    $plugins = plugin_list();
171    foreach ($plugins as $p){
172        if($mode == 'print'){
173            $list[DOKU_PLUGIN."$p/print.css"]  = DOKU_BASE."lib/plugins/$p/";
174        }else{
175            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
176            $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
177        }
178    }
179    return $list;
180}
181
182/**
183 * Very simple CSS optimizer
184 *
185 * @author Andreas Gohr <andi@splitbrain.org>
186 */
187function css_compress($css){
188    // strip whitespaces
189    $css = preg_replace('![\r\n\t ]+!',' ',$css);
190    $css = preg_replace('/ ?([:;,{}]) ?/','\\1',$css);
191
192    // strip comments (ungreedy)
193    // We keep very small comments to maintain typical browser hacks
194    $css = preg_replace('!(/\*)(.{4,})(\*/)!U','',$css);
195
196    // shorten colors
197    $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);
198
199    return $css;
200}
201
202
203//Setup VIM: ex: et ts=4 enc=utf-8 :
204?>
205