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