xref: /plugin/farmer/DokuWikiFarmCore.php (revision 9c4112b1143dd4d110bd542b6b72fc606d1a1109)
1<?php
2
3/**
4 * Core Manager for the Farm functionality
5 *
6 * This class is initialized before any other DokuWiki code runs. Therefore it is
7 * completely selfcontained and does not use any of DokuWiki's utility functions.
8 *
9 * It's registered as a global $FARMCORE variable but you should not interact with
10 * it directly. Instead use the Farmer plugin's helper component.
11 *
12 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
13 * @author  Andreas Gohr <gohr@cosmocode.de>
14 */
15class DokuWikiFarmCore {
16    /**
17     * @var array The default config - changed by loadConfig
18     */
19    protected $config = array(
20        'base' => array(
21            'farmdir' => '',
22            'farmhost' => '',
23            'basedomain' => '',
24        ),
25        'notfound' => array(
26            'show' => 'farmer',
27            'url' => ''
28        ),
29        'inherit' => array(
30            'main' => 1,
31            'acronyms' => 1,
32            'entities' => 1,
33            'interwiki' => 1,
34            'license' => 1,
35            'mime' => 1,
36            'scheme' => 1,
37            'smileys' => 1,
38            'wordblock' => 1,
39            'users' => 0,
40            'plugins' => 0,
41            'userstyle' => 0,
42            'userscript' => 0,
43            'styleini' => 0
44        )
45    );
46
47    /** @var string|false The current animal, false for farmer */
48    protected $animal = false;
49    /** @var bool true if an animal was requested but was not found */
50    protected $notfound = false;
51    /** @var bool true if the current animal was requested by host */
52    protected $hostbased = false;
53
54    /**
55     * DokuWikiFarmCore constructor.
56     *
57     * This initializes the whole farm by loading the configuration and setting
58     * DOKU_CONF depending on the requested animal
59     */
60    public function __construct() {
61        $this->loadConfig();
62        if($this->config['base']['farmdir'] === '') return; // farm setup not complete
63        $this->config['base']['farmdir'] = rtrim($this->config['base']['farmdir'], '/').'/'; // trailing slash always
64        define('DOKU_FARMDIR', $this->config['base']['farmdir']);
65
66        // animal?
67        $this->detectAnimal();
68
69        // setup defines
70        define('DOKU_FARM_ANIMAL', $this->animal);
71        if($this->animal) {
72            define('DOKU_CONF', DOKU_FARMDIR . $this->animal . '/conf/');
73        } else {
74            define('DOKU_CONF', DOKU_INC . '/conf/');
75        }
76
77        $this->setupCascade();
78        $this->adjustCascade();
79    }
80
81    /**
82     * @return array the current farm configuration
83     */
84    public function getConfig() {
85        return $this->config;
86    }
87
88    /**
89     * @return false|string
90     */
91    public function getAnimal() {
92        return $this->animal;
93    }
94
95    /**
96     * @return boolean
97     */
98    public function isHostbased() {
99        return $this->hostbased;
100    }
101
102    /**
103     * @return boolean
104     */
105    public function wasNotfound() {
106        return $this->notfound;
107    }
108
109    /**
110     * @return string
111     */
112    public function getAnimalDataDir() {
113        return DOKU_FARMDIR . $this->getAnimal() . '/data/';
114    }
115
116    /**
117     * @return string
118     */
119    public function getAnimalBaseDir() {
120        if($this->isHostbased()) return '/';
121        return getBaseURL() . '!' . $this->getAnimal();
122    }
123
124    /**
125     * Detect the current animal
126     *
127     * Sets internal members $animal, $notfound and $hostbased
128     *
129     * This borrows form DokuWiki's inc/farm.php but does not support a default conf dir
130     */
131    protected function detectAnimal() {
132        $farmdir = $this->config['base']['farmdir'];
133        $farmhost = $this->config['base']['farmhost'];
134
135        // check if animal was set via parameter (rewrite or CLI)
136        $animal = '';
137        if(isset($_REQUEST['animal'])) $animal = $_REQUEST['animal'];
138        if('cli' == php_sapi_name() && isset($_SERVER['animal'])) $animal = $_SERVER['animal'];
139        if($animal) {
140            // check that $animal is a string and just a directory name and not a path
141            if(!is_string($animal) || strpbrk($animal, '\\/') !== false) {
142                $this->notfound = true;
143                return;
144            };
145            $animal = strtolower($animal);
146
147            // check if animal exists
148            if(is_dir("$farmdir/$animal/conf")) {
149                $this->animal = $animal;
150                return;
151            } else {
152                $this->notfound = true;
153                return;
154            }
155        }
156
157        // no host - no host based setup. if we're still here then it's the farmer
158        if(!isset($_SERVER['HTTP_HOST'])) return;
159
160        // is this the farmer?
161        if(strtolower($_SERVER['HTTP_HOST']) == $farmhost) {
162            return;
163        }
164
165        // still here? check for host based
166        $this->hostbased = true;
167        $possible = $this->getAnimalNamesForHost($_SERVER['HTTP_HOST']);
168        foreach($possible as $animal) {
169            if(is_dir("$farmdir/$animal/conf/")) {
170                $this->animal = $animal;
171                return;
172            }
173        }
174
175        // no hit
176        $this->notfound = true;
177        return;
178    }
179
180    /**
181     * Return a list of possible animal names for the given host
182     *
183     * @param string $host the HTTP_HOST header
184     * @return array
185     */
186    protected function getAnimalNamesForHost($host) {
187        $animals = array();
188        $parts = explode('.', implode('.', explode(':', rtrim($host, '.'))));
189        for($j = count($parts); $j > 0; $j--) {
190            // strip from the end
191            $animals[] = implode('.', array_slice($parts, 0, $j));
192            // strip from the end without host part
193            $animals[] = implode('.', array_slice($parts, 1, $j));
194        }
195        $animals = array_unique($animals);
196        $animals = array_filter($animals);
197        usort(
198            $animals,
199            // compare by length, then alphabet
200            function ($a, $b) {
201                $ret = strlen($b) - strlen($a);
202                if($ret != 0) return $ret;
203                return $a > $b;
204            }
205        );
206        return $animals;
207    }
208
209    /**
210     * This sets up the default farming config cascade
211     */
212    protected function setupCascade() {
213        global $config_cascade;
214        $config_cascade = array(
215            'main' => array(
216                'default' => array(DOKU_INC . 'conf/dokuwiki.php',),
217                'local' => array(DOKU_CONF . 'local.php',),
218                'protected' => array(DOKU_CONF . 'local.protected.php',),
219            ),
220            'acronyms' => array(
221                'default' => array(DOKU_INC . 'conf/acronyms.conf',),
222                'local' => array(DOKU_CONF . 'acronyms.local.conf',),
223            ),
224            'entities' => array(
225                'default' => array(DOKU_INC . 'conf/entities.conf',),
226                'local' => array(DOKU_CONF . 'entities.local.conf',),
227            ),
228            'interwiki' => array(
229                'default' => array(DOKU_INC . 'conf/interwiki.conf',),
230                'local' => array(DOKU_CONF . 'interwiki.local.conf',),
231            ),
232            'license' => array(
233                'default' => array(DOKU_INC . 'conf/license.php',),
234                'local' => array(DOKU_CONF . 'license.local.php',),
235            ),
236            'manifest' => array(
237                'default' => array(DOKU_INC . 'conf/manifest.json',),
238                'local' => array(DOKU_CONF . 'manifest.local.json',),
239            ),
240            'mediameta' => array(
241                'default' => array(DOKU_INC . 'conf/mediameta.php',),
242                'local' => array(DOKU_CONF . 'mediameta.local.php',),
243            ),
244            'mime' => array(
245                'default' => array(DOKU_INC . 'conf/mime.conf',),
246                'local' => array(DOKU_CONF . 'mime.local.conf',),
247            ),
248            'scheme' => array(
249                'default' => array(DOKU_INC . 'conf/scheme.conf',),
250                'local' => array(DOKU_CONF . 'scheme.local.conf',),
251            ),
252            'smileys' => array(
253                'default' => array(DOKU_INC . 'conf/smileys.conf',),
254                'local' => array(DOKU_CONF . 'smileys.local.conf',),
255            ),
256            'wordblock' => array(
257                'default' => array(DOKU_INC . 'conf/wordblock.conf',),
258                'local' => array(DOKU_CONF . 'wordblock.local.conf',),
259            ),
260            'acl' => array(
261                'default' => DOKU_CONF . 'acl.auth.php',
262            ),
263            'plainauth.users' => array(
264                'default' => DOKU_CONF . 'users.auth.php',
265            ),
266            'plugins' => array(
267                'default' => array(DOKU_INC . 'conf/plugins.php',),
268                'local' => array(DOKU_CONF . 'plugins.local.php',),
269                'protected' => array(
270                    DOKU_INC . 'conf/plugins.required.php',
271                    DOKU_CONF . 'plugins.protected.php',
272                ),
273            ),
274            'userstyle' => array(
275                'screen' => array(DOKU_CONF . 'userstyle.css', DOKU_CONF . 'userstyle.less',),
276                'print' => array(DOKU_CONF . 'userprint.css', DOKU_CONF . 'userprint.less',),
277                'feed' => array(DOKU_CONF . 'userfeed.css', DOKU_CONF . 'userfeed.less',),
278                'all' => array(DOKU_CONF . 'userall.css', DOKU_CONF . 'userall.less',),
279            ),
280            'userscript' => array(
281                'default' => array(DOKU_CONF . 'userscript.js',),
282            ),
283            'styleini' => array(
284                'default'   => array(DOKU_INC . 'lib/tpl/%TEMPLATE%/' . 'style.ini'),
285                'local'     => array(DOKU_CONF . 'tpl/%TEMPLATE%/' . 'style.ini')
286            ),
287        );
288    }
289
290    /**
291     * This adds additional files to the config cascade based on the inheritence settings
292     *
293     * These are only added for animals, not the farmer
294     */
295    protected function adjustCascade() {
296        // nothing to do when on the farmer:
297        if(!$this->animal) return;
298
299        global $config_cascade;
300        foreach($this->config['inherit'] as $key => $val) {
301            if(!$val) continue;
302
303            // prepare what is to append or prepend
304            $append = array();
305            $prepend = array();
306            if($key == 'main') {
307                $append = array(
308                    'default' => array(DOKU_INC . 'conf/local.php'),
309                    'protected' => array(DOKU_INC . 'lib/plugins/farmer/includes/config.php')
310                );
311            } elseif($key == 'license') {
312                $append = array('default' => array(DOKU_INC . 'conf/' . $key . '.local.php'));
313            } elseif($key == 'userscript') {
314                $prepend = array('default' => array(DOKU_INC . 'conf/userscript.js'));
315            } elseif($key == 'userstyle') {
316                $prepend = array(
317                    'screen' => array(DOKU_INC . 'conf/userstyle.css', DOKU_INC . 'conf/userstyle.less',),
318                    'print' => array(DOKU_INC . 'conf/userprint.css', DOKU_INC . 'conf/userprint.less',),
319                    'feed' => array(DOKU_INC . 'conf/userfeed.css', DOKU_INC . 'conf/userfeed.less',),
320                    'all' => array(DOKU_INC . 'conf/userall.css', DOKU_INC . 'conf/userall.less',),
321                );
322            } elseif ($key == 'styleini') {
323                $append = array(
324                    'local' => array(
325                        DOKU_INC . 'conf/tpl/%TEMPLATE%/style.ini'
326                    )
327                );
328            } elseif($key == 'users') {
329                $config_cascade['plainauth.users']['protected'] = DOKU_INC . 'conf/users.auth.php';
330            } elseif($key == 'plugins') {
331                $append = array('default' => array(DOKU_INC . 'conf/plugins.local.php'));
332            } else {
333                $append = array('default' => array(DOKU_INC . 'conf/' . $key . '.local.conf'));
334            }
335
336            // add to cascade
337            foreach($prepend as $section => $data) {
338                $config_cascade[$key][$section] = array_merge($data, $config_cascade[$key][$section]);
339            }
340            foreach($append as $section => $data) {
341                $config_cascade[$key][$section] = array_merge($config_cascade[$key][$section], $data);
342            }
343        }
344
345        // add plugin overrides
346        $config_cascade['plugins']['protected'][] = DOKU_INC . 'lib/plugins/farmer/includes/plugins.php';
347    }
348
349    /**
350     * Loads the farm config
351     */
352    protected function loadConfig() {
353        $ini = DOKU_INC . 'conf/farm.ini';
354        if(!file_exists($ini)) return;
355        $config = parse_ini_file($ini, true);
356        foreach(array_keys($this->config) as $section) {
357            if(isset($config[$section])) {
358                $this->config[$section] = array_merge(
359                    $this->config[$section],
360                    $config[$section]
361                );
362            }
363        }
364
365        $this->config['base']['farmdir'] = trim($this->config['base']['farmdir']);
366        $this->config['base']['farmhost'] = strtolower(trim($this->config['base']['farmhost']));
367    }
368
369}
370
371// initialize it globally
372if(!defined('DOKU_UNITTEST')) {
373    global $FARMCORE;
374    $FARMCORE = new DokuWikiFarmCore();
375}
376