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