1<?php
2
3/**
4 * This overwrites DOKU_CONF. Each animal gets its own configuration and data directory.
5 * This can be used together with preload.php. See preload.php.dist for an example setup.
6 * For more information see http://www.dokuwiki.org/farms.
7 *
8 * The farm directory (constant DOKU_FARMDIR) can be any directory and needs to be set.
9 * Animals are direct subdirectories of the farm directory.
10 * There are two different approaches:
11 *  * An .htaccess based setup can use any animal directory name:
12 *    http://example.org/<path_to_farm>/subdir/ will need the subdirectory '$farm/subdir/'.
13 *  * A virtual host based setup needs animal directory names which have to reflect
14 *    the domain name: If an animal resides in http://www.example.org:8080/mysite/test/,
15 *    directories that will match range from '$farm/8080.www.example.org.mysite.test/'
16 *    to a simple '$farm/domain/'.
17 *
18 * @author Anika Henke <anika@selfthinker.org>
19 * @author Michael Klier <chi@chimeric.de>
20 * @author Christopher Smith <chris@jalakai.co.uk>
21 * @author virtual host part of farm_confpath() based on conf_path() from Drupal.org's /includes/bootstrap.inc
22 *   (see https://github.com/drupal/drupal/blob/7.x/includes/bootstrap.inc#L537)
23 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
24 */
25
26// DOKU_FARMDIR needs to be set in preload.php, the fallback is the same as DOKU_INC would be (if it was set already)
27if (!defined('DOKU_FARMDIR')) define('DOKU_FARMDIR', fullpath(__DIR__ . '/../') . '/');
28if (!defined('DOKU_CONF')) define('DOKU_CONF', farm_confpath(DOKU_FARMDIR));
29if (!defined('DOKU_FARM')) define('DOKU_FARM', false);
30
31/**
32 * Find the appropriate configuration directory.
33 *
34 * If the .htaccess based setup is used, the configuration directory can be
35 * any subdirectory of the farm directory.
36 *
37 * Otherwise try finding a matching configuration directory by stripping the
38 * website's hostname from left to right and pathname from right to left. The
39 * first configuration file found will be used; the remaining will ignored.
40 * If no configuration file is found, return the default confdir './conf'.
41 *
42 * @param string $farm
43 *
44 * @return string
45 */
46function farm_confpath($farm)
47{
48
49    // htaccess based or cli
50    // cli usage example: animal=your_animal bin/indexer.php
51    if (isset($_GET['animal']) || ('cli' == PHP_SAPI && isset($_SERVER['animal']))) {
52        $mode = isset($_GET['animal']) ? 'htaccess' : 'cli';
53        $animal = $mode == 'htaccess' ? $_GET['animal'] : $_SERVER['animal'];
54        if (isset($_GET['animal'])) {
55            // now unset the parameter to not leak into new queries
56            // code by @splitbrain from farmer plugin
57            unset($_GET['animal']);
58            $params = [];
59            parse_str($_SERVER['QUERY_STRING'], $params);
60            if (isset($params['animal'])) unset($params['animal']);
61            $_SERVER['QUERY_STRING'] = http_build_query($params);
62        }
63        // check that $animal is a string and just a directory name and not a path
64        if (!is_string($animal) || strpbrk($animal, '\\/') !== false)
65            nice_die('Sorry! Invalid animal name!');
66        if (!is_dir($farm . '/' . $animal))
67            nice_die("Sorry! This Wiki doesn't exist!");
68        if (!defined('DOKU_FARM')) define('DOKU_FARM', $mode);
69        return $farm . '/' . $animal . '/conf/';
70    }
71
72    // virtual host based
73    $uri = explode('/', $_SERVER['SCRIPT_NAME'] ?: $_SERVER['SCRIPT_FILENAME']);
74    $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
75    for ($i = count($uri) - 1; $i > 0; $i--) {
76        for ($j = count($server); $j > 0; $j--) {
77            $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
78            if (is_dir("$farm/$dir/conf/")) {
79                if (!defined('DOKU_FARM')) define('DOKU_FARM', 'virtual');
80                return "$farm/$dir/conf/";
81            }
82        }
83    }
84
85    // default conf directory in farm
86    if (is_dir("$farm/default/conf/")) {
87        if (!defined('DOKU_FARM')) define('DOKU_FARM', 'default');
88        return "$farm/default/conf/";
89    }
90    // farmer
91    return DOKU_INC . 'conf/';
92}
93
94/* Use default config files and local animal config files */
95$config_cascade = [
96    'main' => [
97        'default' => [DOKU_INC . 'conf/dokuwiki.php'],
98        'local' => [DOKU_CONF . 'local.php'],
99        'protected' => [DOKU_CONF . 'local.protected.php']
100    ],
101    'acronyms' => [
102        'default' => [DOKU_INC . 'conf/acronyms.conf'],
103        'local' => [DOKU_CONF . 'acronyms.local.conf']
104    ],
105    'entities' => [
106        'default' => [DOKU_INC . 'conf/entities.conf'],
107        'local' => [DOKU_CONF . 'entities.local.conf']
108    ],
109    'interwiki' => [
110        'default' => [DOKU_INC . 'conf/interwiki.conf'],
111        'local' => [DOKU_CONF . 'interwiki.local.conf']
112    ],
113    'license' => [
114        'default' => [DOKU_INC . 'conf/license.php'],
115        'local' => [DOKU_CONF . 'license.local.php']
116    ],
117    'mediameta' => [
118        'default' => [DOKU_INC . 'conf/mediameta.php'],
119        'local' => [DOKU_CONF . 'mediameta.local.php']
120    ],
121    'mime' => [
122        'default' => [DOKU_INC . 'conf/mime.conf'],
123        'local' => [DOKU_CONF . 'mime.local.conf']
124    ],
125    'scheme' => [
126        'default' => [DOKU_INC . 'conf/scheme.conf'],
127        'local' => [DOKU_CONF . 'scheme.local.conf']
128    ],
129    'smileys' => [
130        'default' => [DOKU_INC . 'conf/smileys.conf'],
131        'local' => [DOKU_CONF . 'smileys.local.conf']
132    ],
133    'wordblock' => [
134        'default' => [DOKU_INC . 'conf/wordblock.conf'],
135        'local' => [DOKU_CONF . 'wordblock.local.conf']
136    ],
137    'acl' => [
138        'default' => DOKU_CONF . 'acl.auth.php'
139    ],
140    'plainauth.users' => [
141        'default' => DOKU_CONF . 'users.auth.php'
142    ],
143    'plugins' => [
144        // needed since Angua
145        'default' => [DOKU_INC . 'conf/plugins.php'],
146        'local' => [DOKU_CONF . 'plugins.local.php'],
147        'protected' => [DOKU_INC . 'conf/plugins.required.php', DOKU_CONF . 'plugins.protected.php'],
148    ],
149    'userstyle' => [
150        'screen' => [DOKU_CONF . 'userstyle.css', DOKU_CONF . 'userstyle.less'],
151        'print' => [DOKU_CONF . 'userprint.css', DOKU_CONF . 'userprint.less'],
152        'feed' => [DOKU_CONF . 'userfeed.css', DOKU_CONF . 'userfeed.less'],
153        'all' => [DOKU_CONF . 'userall.css', DOKU_CONF . 'userall.less']
154    ],
155    'userscript' => [
156        'default' => [DOKU_CONF . 'userscript.js']
157    ]
158];
159