xref: /dokuwiki/inc/StyleUtils.php (revision 432cf0d1b5413bbcf958fc48ac30f9e8eb56d667)
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){
45*432cf0d1SMichael Große                if (!file_exists($incbase . $file)) {
46*432cf0d1SMichael Große                    list($extension, $basename) = array_map('strrev', explode('.', strrev($file), 2));
47*432cf0d1SMichael Große                    $newExtension = $extension === 'css' ? 'less' : 'css';
48*432cf0d1SMichael Große                    if (file_exists($incbase . $basename . '.' . $newExtension)) {
49*432cf0d1SMichael Große                        $stylesheets[$mode][$incbase . $basename . '.' . $newExtension] = $webbase;
50*432cf0d1SMichael Große                        if ($conf['allowdebug']) {
51*432cf0d1SMichael Große                            msg("Stylesheet $file not found, using $basename.$newExtension instead. Please contact developer of \"{$conf['template']}\" template.", 2);
52*432cf0d1SMichael Große                        }
53*432cf0d1SMichael Große                        continue;
54*432cf0d1SMichael Große                    }
55*432cf0d1SMichael Große                }
56fb1f9089SMichael Große                $stylesheets[$mode][$incbase . $file] = $webbase;
57fb1f9089SMichael Große            }
58fb1f9089SMichael Große
59fb1f9089SMichael Große            // replacements
60fb1f9089SMichael Große            if(is_array($data['replacements'])){
61fb1f9089SMichael Große                $replacements = array_merge($replacements, $this->cssFixreplacementurls($data['replacements'],$webbase));
62fb1f9089SMichael Große            }
63fb1f9089SMichael Große        }
64fb1f9089SMichael Große
65fb1f9089SMichael Große        // load configs's style.ini
66fb1f9089SMichael Große        $webbase = DOKU_BASE;
67fb1f9089SMichael Große        $ini = DOKU_CONF."tpl/$tpl/style.ini";
68fb1f9089SMichael Große        $incbase = dirname($ini).'/';
69fb1f9089SMichael Große        if(file_exists($ini)){
70fb1f9089SMichael Große            $data = parse_ini_file($ini, true);
71fb1f9089SMichael Große
72fb1f9089SMichael Große            // stylesheets
73fb1f9089SMichael Große            if(isset($data['stylesheets']) && is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){
74fb1f9089SMichael Große                $stylesheets[$mode][$incbase.$file] = $webbase;
75fb1f9089SMichael Große            }
76fb1f9089SMichael Große
77fb1f9089SMichael Große            // replacements
78fb1f9089SMichael Große            if(isset($data['replacements']) && is_array($data['replacements'])){
79fb1f9089SMichael Große                $replacements = array_merge($replacements, $this->cssFixreplacementurls($data['replacements'],$webbase));
80fb1f9089SMichael Große            }
81fb1f9089SMichael Große        }
82fb1f9089SMichael Große
83fb1f9089SMichael Große        // allow replacement overwrites in preview mode
84fb1f9089SMichael Große        if($preview) {
85fb1f9089SMichael Große            $webbase = DOKU_BASE;
86fb1f9089SMichael Große            $ini     = $conf['cachedir'].'/preview.ini';
87fb1f9089SMichael Große            if(file_exists($ini)) {
88fb1f9089SMichael Große                $data = parse_ini_file($ini, true);
89fb1f9089SMichael Große                // replacements
90fb1f9089SMichael Große                if(is_array($data['replacements'])) {
91fb1f9089SMichael Große                    $replacements = array_merge($replacements, $this->cssFixreplacementurls($data['replacements'], $webbase));
92fb1f9089SMichael Große                }
93fb1f9089SMichael Große            }
94fb1f9089SMichael Große        }
95fb1f9089SMichael Große
96fb1f9089SMichael Große        return array(
97fb1f9089SMichael Große            'stylesheets' => $stylesheets,
98fb1f9089SMichael Große            'replacements' => $replacements
99fb1f9089SMichael Große        );
100fb1f9089SMichael Große    }
101fb1f9089SMichael Große
102fb1f9089SMichael Große
103fb1f9089SMichael Große    /**
104fb1f9089SMichael Große     * Amend paths used in replacement relative urls, refer FS#2879
105fb1f9089SMichael Große     *
106fb1f9089SMichael Große     * @author Chris Smith <chris@jalakai.co.uk>
107fb1f9089SMichael Große     *
108fb1f9089SMichael Große     * @param array $replacements with key-value pairs
109fb1f9089SMichael Große     * @param string $location
110fb1f9089SMichael Große     * @return array
111fb1f9089SMichael Große     */
112fb1f9089SMichael Große    protected function cssFixreplacementurls($replacements, $location) {
113fb1f9089SMichael Große        foreach($replacements as $key => $value) {
114fb1f9089SMichael Große            $replacements[$key] = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$value);
115fb1f9089SMichael Große        }
116fb1f9089SMichael Große        return $replacements;
117fb1f9089SMichael Große    }
118fb1f9089SMichael Große}
119