[ Index ]
 

Code source de DokuWiki 2006-11-06

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/exe/ -> css.php (source)

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


Généré le : Tue Apr 3 20:47:31 2007 par Balluche grâce à PHPXref 0.7