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