PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/elgg/mod/dokuwiki/lib/dokuwiki/lib/exe/css.php

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