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