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