xref: /dokuwiki/inc/StyleUtils.php (revision 71a75f041e8e73a77d75db26da40a770a58dccbe)
1<?php
2
3namespace dokuwiki;
4
5class StyleUtils
6{
7    /**
8     * Load style ini contents
9     *
10     * Loads and merges style.ini files from template and config and prepares
11     * the stylesheet modes
12     *
13     * @author Andreas Gohr <andi@splitbrain.org>
14     *
15     * @param string $tpl the used template
16     * @param bool   $preview load preview replacements
17     * @return array with keys 'stylesheets' and 'replacements'
18     */
19    public function cssStyleini($tpl, $preview=false) {
20        global $conf;
21
22        $stylesheets = array(); // mode, file => base
23        // guaranteed placeholder => value
24        $replacements = array(
25            '__text__' => "#000",
26            '__background__' => "#fff",
27            '__text_alt__' => "#999",
28            '__background_alt__' => "#eee",
29            '__text_neu__' => "#666",
30            '__background_neu__' => "#ddd",
31            '__border__' => "#ccc",
32            '__highlight__' => "#ff9",
33            '__link__' => "#00f",
34        );
35
36        // load template's style.ini
37        $incbase = tpl_incdir($tpl);
38        $webbase = tpl_basedir($tpl);
39        $ini = $incbase.'style.ini';
40        if(file_exists($ini)){
41            $data = parse_ini_file($ini, true);
42
43            // stylesheets
44            if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){
45                if (!file_exists($incbase . $file)) {
46                    list($extension, $basename) = array_map('strrev', explode('.', strrev($file), 2));
47                    $newExtension = $extension === 'css' ? 'less' : 'css';
48                    if (file_exists($incbase . $basename . '.' . $newExtension)) {
49                        $stylesheets[$mode][$incbase . $basename . '.' . $newExtension] = $webbase;
50                        if ($conf['allowdebug']) {
51                            msg(
52                                "Stylesheet $file not found, using $basename.$newExtension instead.
53                                Please contact developer of \"{$conf['template']}\" template.",
54                                2
55                            );
56                        }
57                        continue;
58                    }
59                }
60                $stylesheets[$mode][$incbase . $file] = $webbase;
61            }
62
63            // replacements
64            if(is_array($data['replacements'])){
65                $replacements = array_merge(
66                    $replacements,
67                    $this->cssFixreplacementurls($data['replacements'], $webbase)
68                );
69            }
70        }
71
72        // load configs's style.ini
73        $webbase = DOKU_BASE;
74        $ini = DOKU_CONF."tpl/$tpl/style.ini";
75        $incbase = dirname($ini).'/';
76        if(file_exists($ini)){
77            $data = parse_ini_file($ini, true);
78
79            // stylesheets
80            if(isset($data['stylesheets']) && is_array($data['stylesheets'])) {
81                foreach($data['stylesheets'] as $file => $mode) {
82                    $stylesheets[$mode][$incbase . $file] = $webbase;
83                }
84            }
85
86            // replacements
87            if(isset($data['replacements']) && is_array($data['replacements'])){
88                $replacements = array_merge(
89                    $replacements,
90                    $this->cssFixreplacementurls($data['replacements'], $webbase)
91                );
92            }
93        }
94
95        // allow replacement overwrites in preview mode
96        if($preview) {
97            $webbase = DOKU_BASE;
98            $ini     = $conf['cachedir'].'/preview.ini';
99            if(file_exists($ini)) {
100                $data = parse_ini_file($ini, true);
101                // replacements
102                if(is_array($data['replacements'])) {
103                    $replacements = array_merge(
104                        $replacements,
105                        $this->cssFixreplacementurls($data['replacements'], $webbase)
106                    );
107                }
108            }
109        }
110
111        return array(
112            'stylesheets' => $stylesheets,
113            'replacements' => $replacements
114        );
115    }
116
117
118    /**
119     * Amend paths used in replacement relative urls, refer FS#2879
120     *
121     * @author Chris Smith <chris@jalakai.co.uk>
122     *
123     * @param array $replacements with key-value pairs
124     * @param string $location
125     * @return array
126     */
127    protected function cssFixreplacementurls($replacements, $location) {
128        foreach($replacements as $key => $value) {
129            $replacements[$key] = preg_replace(
130                '#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#',
131                '\\1' . $location,
132                $value
133            );
134        }
135        return $replacements;
136    }
137}
138