StyleUtils.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace dokuwiki;
  3. /**
  4. * Class StyleUtils
  5. *
  6. * Reads and applies the template's style.ini settings
  7. */
  8. class StyleUtils
  9. {
  10. /** @var string current template */
  11. protected $tpl;
  12. /** @var bool reinitialize styles config */
  13. protected $reinit;
  14. /** @var bool $preview preview mode */
  15. protected $preview;
  16. /** @var array default replacements to be merged with custom style configs */
  17. protected $defaultReplacements = array(
  18. '__text__' => "#000",
  19. '__background__' => "#fff",
  20. '__text_alt__' => "#999",
  21. '__background_alt__' => "#eee",
  22. '__text_neu__' => "#666",
  23. '__background_neu__' => "#ddd",
  24. '__border__' => "#ccc",
  25. '__highlight__' => "#ff9",
  26. '__link__' => "#00f",
  27. );
  28. /**
  29. * StyleUtils constructor.
  30. * @param string $tpl template name: if not passed as argument, the default value from $conf will be used
  31. * @param bool $preview
  32. * @param bool $reinit whether static style conf should be reinitialized
  33. */
  34. public function __construct($tpl = '', $preview = false, $reinit = false)
  35. {
  36. if (!$tpl) {
  37. global $conf;
  38. $tpl = $conf['template'];
  39. }
  40. $this->tpl = $tpl;
  41. $this->reinit = $reinit;
  42. $this->preview = $preview;
  43. }
  44. /**
  45. * Load style ini contents
  46. *
  47. * Loads and merges style.ini files from template and config and prepares
  48. * the stylesheet modes
  49. *
  50. * @author Andreas Gohr <andi@splitbrain.org>
  51. * @author Anna Dabrowska <info@cosmocode.de>
  52. *
  53. * @return array with keys 'stylesheets' and 'replacements'
  54. */
  55. public function cssStyleini()
  56. {
  57. static $combined = [];
  58. if (!empty($combined) && !$this->reinit) {
  59. return $combined;
  60. }
  61. global $conf;
  62. global $config_cascade;
  63. $stylesheets = array(); // mode, file => base
  64. // guaranteed placeholder => value
  65. $replacements = $this->defaultReplacements;
  66. // merge all styles from config cascade
  67. if (!is_array($config_cascade['styleini'])) {
  68. trigger_error('Missing config cascade for styleini', E_USER_WARNING);
  69. }
  70. // allow replacement overwrites in preview mode
  71. if ($this->preview) {
  72. $config_cascade['styleini']['local'][] = $conf['cachedir'] . '/preview.ini';
  73. }
  74. $combined['stylesheets'] = [];
  75. $combined['replacements'] = [];
  76. foreach (array('default', 'local', 'protected') as $config_group) {
  77. if (empty($config_cascade['styleini'][$config_group])) continue;
  78. // set proper server dirs
  79. $webbase = $this->getWebbase($config_group);
  80. foreach ($config_cascade['styleini'][$config_group] as $inifile) {
  81. // replace the placeholder with the name of the current template
  82. $inifile = str_replace('%TEMPLATE%', $this->tpl, $inifile);
  83. $incbase = dirname($inifile) . '/';
  84. if (file_exists($inifile)) {
  85. $config = parse_ini_file($inifile, true);
  86. if (isset($config['stylesheets']) && is_array($config['stylesheets'])) {
  87. foreach ($config['stylesheets'] as $inifile => $mode) {
  88. // validate and include style files
  89. $stylesheets = array_merge(
  90. $stylesheets,
  91. $this->getValidatedStyles($stylesheets, $inifile, $mode, $incbase, $webbase)
  92. );
  93. $combined['stylesheets'] = array_merge($combined['stylesheets'], $stylesheets);
  94. }
  95. }
  96. if (isset($config['replacements']) && is_array($config['replacements'])) {
  97. $replacements = array_replace(
  98. $replacements,
  99. $this->cssFixreplacementurls($config['replacements'], $webbase)
  100. );
  101. $combined['replacements'] = array_merge($combined['replacements'], $replacements);
  102. }
  103. }
  104. }
  105. }
  106. return $combined;
  107. }
  108. /**
  109. * Checks if configured style files exist and, if necessary, adjusts file extensions in config
  110. *
  111. * @param array $stylesheets
  112. * @param string $file
  113. * @param string $mode
  114. * @param string $incbase
  115. * @param string $webbase
  116. * @return mixed
  117. */
  118. protected function getValidatedStyles($stylesheets, $file, $mode, $incbase, $webbase)
  119. {
  120. global $conf;
  121. if (!file_exists($incbase . $file)) {
  122. list($extension, $basename) = array_map('strrev', sexplode('.', strrev($file), 2, ''));
  123. $newExtension = $extension === 'css' ? 'less' : 'css';
  124. if (file_exists($incbase . $basename . '.' . $newExtension)) {
  125. $stylesheets[$mode][$incbase . $basename . '.' . $newExtension] = $webbase;
  126. if ($conf['allowdebug']) {
  127. msg("Stylesheet $file not found, using $basename.$newExtension instead. " .
  128. "Please contact developer of \"$this->tpl\" template.", 2);
  129. }
  130. } elseif ($conf['allowdebug']) {
  131. msg("Stylesheet $file not found, please contact the developer of \"$this->tpl\" template.", 2);
  132. }
  133. }
  134. $stylesheets[$mode][fullpath($incbase . $file)] = $webbase;
  135. return $stylesheets;
  136. }
  137. /**
  138. * Returns the web base path for the given level/group in config cascade.
  139. * Style resources are relative to the template directory for the main (default) styles
  140. * but relative to DOKU_BASE for everything else"
  141. *
  142. * @param string $config_group
  143. * @return string
  144. */
  145. protected function getWebbase($config_group)
  146. {
  147. if ($config_group === 'default') {
  148. return tpl_basedir($this->tpl);
  149. } else {
  150. return DOKU_BASE;
  151. }
  152. }
  153. /**
  154. * Amend paths used in replacement relative urls, refer FS#2879
  155. *
  156. * @author Chris Smith <chris@jalakai.co.uk>
  157. *
  158. * @param array $replacements with key-value pairs
  159. * @param string $location
  160. * @return array
  161. */
  162. protected function cssFixreplacementurls($replacements, $location)
  163. {
  164. foreach ($replacements as $key => $value) {
  165. $replacements[$key] = preg_replace(
  166. '#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#',
  167. '\\1' . $location,
  168. $value
  169. );
  170. }
  171. return $replacements;
  172. }
  173. }