PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wiki/lib/exe/css.php

https://github.com/godber/PHXdata-Website
PHP | 333 lines | 198 code | 45 blank | 90 comment | 45 complexity | 2831f1a74c2054c5a8855c348c21507a MD5 | raw file
  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. if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
  9. if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
  10. if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here
  11. require_once(DOKU_INC.'inc/init.php');
  12. // Main (don't run when UNIT test)
  13. if(!defined('SIMPLE_TEST')){
  14. header('Content-Type: text/css; charset=utf-8');
  15. css_out();
  16. }
  17. // ---------------------- functions ------------------------------
  18. /**
  19. * Output all needed Styles
  20. *
  21. * @author Andreas Gohr <andi@splitbrain.org>
  22. */
  23. function css_out(){
  24. global $conf;
  25. global $lang;
  26. global $config_cascade;
  27. $style = '';
  28. if (isset($_REQUEST['s']) &&
  29. in_array($_REQUEST['s'], array('all', 'print', 'feed'))) {
  30. $style = $_REQUEST['s'];
  31. }
  32. $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t']));
  33. if($tpl){
  34. $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/';
  35. $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/';
  36. }else{
  37. $tplinc = DOKU_TPLINC;
  38. $tpldir = DOKU_TPL;
  39. }
  40. // The generated script depends on some dynamic options
  41. $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$style,'.css');
  42. // load template styles
  43. $tplstyles = array();
  44. if(@file_exists($tplinc.'style.ini')){
  45. $ini = parse_ini_file($tplinc.'style.ini',true);
  46. foreach($ini['stylesheets'] as $file => $mode){
  47. $tplstyles[$mode][$tplinc.$file] = $tpldir;
  48. }
  49. }
  50. // Array of needed files and their web locations, the latter ones
  51. // are needed to fix relative paths in the stylesheets
  52. $files = array();
  53. //if (isset($tplstyles['all'])) $files = array_merge($files, $tplstyles['all']);
  54. if(!empty($style)){
  55. $files[DOKU_INC.'lib/styles/'.$style.'.css'] = DOKU_BASE.'lib/styles/';
  56. // load plugin, template, user styles
  57. $files = array_merge($files, css_pluginstyles($style));
  58. if (isset($tplstyles[$style])) $files = array_merge($files, $tplstyles[$style]);
  59. if(isset($config_cascade['userstyle'][$style])){
  60. $files[$config_cascade['userstyle'][$style]] = DOKU_BASE;
  61. }
  62. }else{
  63. $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/';
  64. // load plugin, template, user styles
  65. $files = array_merge($files, css_pluginstyles('screen'));
  66. if (isset($tplstyles['screen'])) $files = array_merge($files, $tplstyles['screen']);
  67. if($lang['direction'] == 'rtl'){
  68. if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']);
  69. }
  70. if(isset($config_cascade['userstyle']['default'])){
  71. $files[$config_cascade['userstyle']['default']] = DOKU_BASE;
  72. }
  73. }
  74. // check cache age & handle conditional request
  75. header('Cache-Control: public, max-age=3600');
  76. header('Pragma: public');
  77. if(css_cacheok($cache,array_keys($files),$tplinc)){
  78. http_conditionalRequest(filemtime($cache));
  79. if($conf['allowdebug']) header("X-CacheUsed: $cache");
  80. // finally send output
  81. if ($conf['gzip_output'] && http_gzip_valid($cache)) {
  82. header('Vary: Accept-Encoding');
  83. header('Content-Encoding: gzip');
  84. readfile($cache.".gz");
  85. } else {
  86. if (!http_sendfile($cache)) readfile($cache);
  87. }
  88. return;
  89. } else {
  90. http_conditionalRequest(time());
  91. }
  92. // start output buffering and build the stylesheet
  93. ob_start();
  94. // print the default classes for interwiki links and file downloads
  95. css_interwiki();
  96. css_filetypes();
  97. // load files
  98. foreach($files as $file => $location){
  99. print css_loadfile($file, $location);
  100. }
  101. // end output buffering and get contents
  102. $css = ob_get_contents();
  103. ob_end_clean();
  104. // apply style replacements
  105. $css = css_applystyle($css,$tplinc);
  106. // compress whitespace and comments
  107. if($conf['compress']){
  108. $css = css_compress($css);
  109. }
  110. // save cache file
  111. io_saveFile($cache,$css);
  112. if(function_exists('gzopen')) io_saveFile("$cache.gz",$css);
  113. // finally send output
  114. if ($conf['gzip_output']) {
  115. header('Vary: Accept-Encoding');
  116. header('Content-Encoding: gzip');
  117. print gzencode($css,9,FORCE_GZIP);
  118. } else {
  119. print $css;
  120. }
  121. }
  122. /**
  123. * Checks if a CSS Cache file still is valid
  124. *
  125. * @author Andreas Gohr <andi@splitbrain.org>
  126. */
  127. function css_cacheok($cache,$files,$tplinc){
  128. global $config_cascade;
  129. if(isset($_REQUEST['purge'])) return false; //support purge request
  130. $ctime = @filemtime($cache);
  131. if(!$ctime) return false; //There is no cache
  132. // some additional files to check
  133. $files = array_merge($files, getConfigFiles('main'));
  134. $files[] = $tplinc.'style.ini';
  135. $files[] = __FILE__;
  136. // now walk the files
  137. foreach($files as $file){
  138. if(@filemtime($file) > $ctime){
  139. return false;
  140. }
  141. }
  142. return true;
  143. }
  144. /**
  145. * Does placeholder replacements in the style according to
  146. * the ones defined in a templates style.ini file
  147. *
  148. * @author Andreas Gohr <andi@splitbrain.org>
  149. */
  150. function css_applystyle($css,$tplinc){
  151. if(@file_exists($tplinc.'style.ini')){
  152. $ini = parse_ini_file($tplinc.'style.ini',true);
  153. $css = strtr($css,$ini['replacements']);
  154. }
  155. return $css;
  156. }
  157. /**
  158. * Prints classes for interwikilinks
  159. *
  160. * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
  161. * $name is the identifier given in the config. All Interwiki links get
  162. * an default style with a default icon. If a special icon is available
  163. * for an interwiki URL it is set in it's own class. Both classes can be
  164. * overwritten in the template or userstyles.
  165. *
  166. * @author Andreas Gohr <andi@splitbrain.org>
  167. */
  168. function css_interwiki(){
  169. // default style
  170. echo 'a.interwiki {';
  171. echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
  172. echo ' padding-left: 16px;';
  173. echo '}';
  174. // additional styles when icon available
  175. $iwlinks = getInterwiki();
  176. foreach(array_keys($iwlinks) as $iw){
  177. $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
  178. if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
  179. echo "a.iw_$class {";
  180. echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
  181. echo '}';
  182. }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
  183. echo "a.iw_$class {";
  184. echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
  185. echo '}';
  186. }
  187. }
  188. }
  189. /**
  190. * Prints classes for file download links
  191. *
  192. * @author Andreas Gohr <andi@splitbrain.org>
  193. */
  194. function css_filetypes(){
  195. // default style
  196. echo 'a.mediafile {';
  197. echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
  198. echo ' padding-left: 18px;';
  199. echo ' padding-bottom: 1px;';
  200. echo '}';
  201. // additional styles when icon available
  202. // scan directory for all icons
  203. $exts = array();
  204. if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
  205. while(false !== ($file = readdir($dh))){
  206. if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
  207. $ext = strtolower($match[1]);
  208. $type = '.'.strtolower($match[2]);
  209. if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
  210. $exts[$ext] = $type;
  211. }
  212. }
  213. }
  214. closedir($dh);
  215. }
  216. foreach($exts as $ext=>$type){
  217. $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
  218. echo "a.mf_$class {";
  219. echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
  220. echo '}';
  221. }
  222. }
  223. /**
  224. * Loads a given file and fixes relative URLs with the
  225. * given location prefix
  226. */
  227. function css_loadfile($file,$location=''){
  228. if(!@file_exists($file)) return '';
  229. $css = io_readFile($file);
  230. if(!$location) return $css;
  231. $css = preg_replace('#(url\([ \'"]*)((?!/|http://|https://| |\'|"))#','\\1'.$location.'\\3',$css);
  232. return $css;
  233. }
  234. /**
  235. * Returns a list of possible Plugin Styles (no existance check here)
  236. *
  237. * @author Andreas Gohr <andi@splitbrain.org>
  238. */
  239. function css_pluginstyles($mode='screen'){
  240. global $lang;
  241. $list = array();
  242. $plugins = plugin_list();
  243. foreach ($plugins as $p){
  244. if($mode == 'all'){
  245. $list[DOKU_PLUGIN."$p/all.css"] = DOKU_BASE."lib/plugins/$p/";
  246. }elseif($mode == 'print'){
  247. $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/";
  248. }elseif($mode == 'feed'){
  249. $list[DOKU_PLUGIN."$p/feed.css"] = DOKU_BASE."lib/plugins/$p/";
  250. }else{
  251. $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
  252. $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
  253. }
  254. if($lang['direction'] == 'rtl'){
  255. $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/";
  256. }
  257. }
  258. return $list;
  259. }
  260. /**
  261. * Very simple CSS optimizer
  262. *
  263. * @author Andreas Gohr <andi@splitbrain.org>
  264. */
  265. function css_compress($css){
  266. //strip comments through a callback
  267. $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
  268. //strip (incorrect but common) one line comments
  269. $css = preg_replace('/(?<!:)\/\/.*$/m','',$css);
  270. // strip whitespaces
  271. $css = preg_replace('![\r\n\t ]+!',' ',$css);
  272. $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css);
  273. // shorten colors
  274. $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);
  275. return $css;
  276. }
  277. /**
  278. * Callback for css_compress()
  279. *
  280. * Keeps short comments (< 5 chars) to maintain typical browser hacks
  281. *
  282. * @author Andreas Gohr <andi@splitbrain.org>
  283. */
  284. function css_comment_cb($matches){
  285. if(strlen($matches[2]) > 4) return '';
  286. return $matches[0];
  287. }
  288. //Setup VIM: ex: et ts=4 enc=utf-8 :