1*fb1f9089SMichael Große<?php 2*fb1f9089SMichael Große 3*fb1f9089SMichael Großenamespace dokuwiki; 4*fb1f9089SMichael Große 5*fb1f9089SMichael Großeclass StyleUtils 6*fb1f9089SMichael Große{ 7*fb1f9089SMichael Große /** 8*fb1f9089SMichael Große * Load style ini contents 9*fb1f9089SMichael Große * 10*fb1f9089SMichael Große * Loads and merges style.ini files from template and config and prepares 11*fb1f9089SMichael Große * the stylesheet modes 12*fb1f9089SMichael Große * 13*fb1f9089SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 14*fb1f9089SMichael Große * 15*fb1f9089SMichael Große * @param string $tpl the used template 16*fb1f9089SMichael Große * @param bool $preview load preview replacements 17*fb1f9089SMichael Große * @return array with keys 'stylesheets' and 'replacements' 18*fb1f9089SMichael Große */ 19*fb1f9089SMichael Große public function cssStyleini($tpl, $preview=false) { 20*fb1f9089SMichael Große global $conf; 21*fb1f9089SMichael Große 22*fb1f9089SMichael Große $stylesheets = array(); // mode, file => base 23*fb1f9089SMichael Große // guaranteed placeholder => value 24*fb1f9089SMichael Große $replacements = array( 25*fb1f9089SMichael Große '__text__' => "#000", 26*fb1f9089SMichael Große '__background__' => "#fff", 27*fb1f9089SMichael Große '__text_alt__' => "#999", 28*fb1f9089SMichael Große '__background_alt__' => "#eee", 29*fb1f9089SMichael Große '__text_neu__' => "#666", 30*fb1f9089SMichael Große '__background_neu__' => "#ddd", 31*fb1f9089SMichael Große '__border__' => "#ccc", 32*fb1f9089SMichael Große '__highlight__' => "#ff9", 33*fb1f9089SMichael Große '__link__' => "#00f", 34*fb1f9089SMichael Große ); 35*fb1f9089SMichael Große 36*fb1f9089SMichael Große // load template's style.ini 37*fb1f9089SMichael Große $incbase = tpl_incdir($tpl); 38*fb1f9089SMichael Große $webbase = tpl_basedir($tpl); 39*fb1f9089SMichael Große $ini = $incbase.'style.ini'; 40*fb1f9089SMichael Große if(file_exists($ini)){ 41*fb1f9089SMichael Große $data = parse_ini_file($ini, true); 42*fb1f9089SMichael Große 43*fb1f9089SMichael Große // stylesheets 44*fb1f9089SMichael Große if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ 45*fb1f9089SMichael Große $stylesheets[$mode][$incbase.$file] = $webbase; 46*fb1f9089SMichael Große } 47*fb1f9089SMichael Große 48*fb1f9089SMichael Große // replacements 49*fb1f9089SMichael Große if(is_array($data['replacements'])){ 50*fb1f9089SMichael Große $replacements = array_merge($replacements, $this->cssFixreplacementurls($data['replacements'],$webbase)); 51*fb1f9089SMichael Große } 52*fb1f9089SMichael Große } 53*fb1f9089SMichael Große 54*fb1f9089SMichael Große // load configs's style.ini 55*fb1f9089SMichael Große $webbase = DOKU_BASE; 56*fb1f9089SMichael Große $ini = DOKU_CONF."tpl/$tpl/style.ini"; 57*fb1f9089SMichael Große $incbase = dirname($ini).'/'; 58*fb1f9089SMichael Große if(file_exists($ini)){ 59*fb1f9089SMichael Große $data = parse_ini_file($ini, true); 60*fb1f9089SMichael Große 61*fb1f9089SMichael Große // stylesheets 62*fb1f9089SMichael Große if(isset($data['stylesheets']) && is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ 63*fb1f9089SMichael Große $stylesheets[$mode][$incbase.$file] = $webbase; 64*fb1f9089SMichael Große } 65*fb1f9089SMichael Große 66*fb1f9089SMichael Große // replacements 67*fb1f9089SMichael Große if(isset($data['replacements']) && is_array($data['replacements'])){ 68*fb1f9089SMichael Große $replacements = array_merge($replacements, $this->cssFixreplacementurls($data['replacements'],$webbase)); 69*fb1f9089SMichael Große } 70*fb1f9089SMichael Große } 71*fb1f9089SMichael Große 72*fb1f9089SMichael Große // allow replacement overwrites in preview mode 73*fb1f9089SMichael Große if($preview) { 74*fb1f9089SMichael Große $webbase = DOKU_BASE; 75*fb1f9089SMichael Große $ini = $conf['cachedir'].'/preview.ini'; 76*fb1f9089SMichael Große if(file_exists($ini)) { 77*fb1f9089SMichael Große $data = parse_ini_file($ini, true); 78*fb1f9089SMichael Große // replacements 79*fb1f9089SMichael Große if(is_array($data['replacements'])) { 80*fb1f9089SMichael Große $replacements = array_merge($replacements, $this->cssFixreplacementurls($data['replacements'], $webbase)); 81*fb1f9089SMichael Große } 82*fb1f9089SMichael Große } 83*fb1f9089SMichael Große } 84*fb1f9089SMichael Große 85*fb1f9089SMichael Große return array( 86*fb1f9089SMichael Große 'stylesheets' => $stylesheets, 87*fb1f9089SMichael Große 'replacements' => $replacements 88*fb1f9089SMichael Große ); 89*fb1f9089SMichael Große } 90*fb1f9089SMichael Große 91*fb1f9089SMichael Große 92*fb1f9089SMichael Große /** 93*fb1f9089SMichael Große * Amend paths used in replacement relative urls, refer FS#2879 94*fb1f9089SMichael Große * 95*fb1f9089SMichael Große * @author Chris Smith <chris@jalakai.co.uk> 96*fb1f9089SMichael Große * 97*fb1f9089SMichael Große * @param array $replacements with key-value pairs 98*fb1f9089SMichael Große * @param string $location 99*fb1f9089SMichael Große * @return array 100*fb1f9089SMichael Große */ 101*fb1f9089SMichael Große protected function cssFixreplacementurls($replacements, $location) { 102*fb1f9089SMichael Große foreach($replacements as $key => $value) { 103*fb1f9089SMichael Große $replacements[$key] = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$value); 104*fb1f9089SMichael Große } 105*fb1f9089SMichael Große return $replacements; 106*fb1f9089SMichael Große } 107*fb1f9089SMichael Große} 108