xref: /dokuwiki/inc/farm.php (revision 24870174d2ee45460ba6bcfe5f5a0ae94715efd7)
113054fbfSAnika Henke<?php
213054fbfSAnika Henke/**
313054fbfSAnika Henke * This overwrites DOKU_CONF. Each animal gets its own configuration and data directory.
413054fbfSAnika Henke * This can be used together with preload.php. See preload.php.dist for an example setup.
513054fbfSAnika Henke * For more information see http://www.dokuwiki.org/farms.
613054fbfSAnika Henke *
713054fbfSAnika Henke * The farm directory (constant DOKU_FARMDIR) can be any directory and needs to be set.
813054fbfSAnika Henke * Animals are direct subdirectories of the farm directory.
913054fbfSAnika Henke * There are two different approaches:
1013054fbfSAnika Henke *  * An .htaccess based setup can use any animal directory name:
1113054fbfSAnika Henke *    http://example.org/<path_to_farm>/subdir/ will need the subdirectory '$farm/subdir/'.
1213054fbfSAnika Henke *  * A virtual host based setup needs animal directory names which have to reflect
1313054fbfSAnika Henke *    the domain name: If an animal resides in http://www.example.org:8080/mysite/test/,
1413054fbfSAnika Henke *    directories that will match range from '$farm/8080.www.example.org.mysite.test/'
1513054fbfSAnika Henke *    to a simple '$farm/domain/'.
1613054fbfSAnika Henke *
1713054fbfSAnika Henke * @author Anika Henke <anika@selfthinker.org>
1813054fbfSAnika Henke * @author Michael Klier <chi@chimeric.de>
1913054fbfSAnika Henke * @author Christopher Smith <chris@jalakai.co.uk>
2013054fbfSAnika Henke * @author virtual host part of farm_confpath() based on conf_path() from Drupal.org's /includes/bootstrap.inc
2113054fbfSAnika Henke *   (see https://github.com/drupal/drupal/blob/7.x/includes/bootstrap.inc#L537)
2213054fbfSAnika Henke * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
2313054fbfSAnika Henke */
2413054fbfSAnika Henke
2564159a61SAndreas Gohr// DOKU_FARMDIR needs to be set in preload.php, the fallback is the same as DOKU_INC would be (if it was set already)
26*24870174SAndreas Gohrif (!defined('DOKU_FARMDIR')) define('DOKU_FARMDIR', fullpath(__DIR__ . '/../') . '/');
2713054fbfSAnika Henkeif (!defined('DOKU_CONF')) define('DOKU_CONF', farm_confpath(DOKU_FARMDIR));
2813054fbfSAnika Henkeif (!defined('DOKU_FARM')) define('DOKU_FARM', false);
2913054fbfSAnika Henke
3013054fbfSAnika Henke/**
3113054fbfSAnika Henke * Find the appropriate configuration directory.
3213054fbfSAnika Henke *
3313054fbfSAnika Henke * If the .htaccess based setup is used, the configuration directory can be
3413054fbfSAnika Henke * any subdirectory of the farm directory.
3513054fbfSAnika Henke *
3613054fbfSAnika Henke * Otherwise try finding a matching configuration directory by stripping the
3713054fbfSAnika Henke * website's hostname from left to right and pathname from right to left. The
3813054fbfSAnika Henke * first configuration file found will be used; the remaining will ignored.
3913054fbfSAnika Henke * If no configuration file is found, return the default confdir './conf'.
40f50a239bSTakamura *
41f50a239bSTakamura * @param string $farm
42f50a239bSTakamura *
43f50a239bSTakamura * @return string
4413054fbfSAnika Henke */
45*24870174SAndreas Gohrfunction farm_confpath($farm)
46*24870174SAndreas Gohr{
4713054fbfSAnika Henke
4813054fbfSAnika Henke    // htaccess based or cli
4913054fbfSAnika Henke    // cli usage example: animal=your_animal bin/indexer.php
50*24870174SAndreas Gohr    if (isset($_GET['animal']) || ('cli' == PHP_SAPI && isset($_SERVER['animal']))) {
515170edc8SSchplurtz le Déboulonné        $mode = isset($_GET['animal']) ? 'htaccess' : 'cli';
525170edc8SSchplurtz le Déboulonné        $animal = $mode == 'htaccess' ? $_GET['animal'] : $_SERVER['animal'];
535170edc8SSchplurtz le Déboulonné        if (isset($_GET['animal'])) {
545170edc8SSchplurtz le Déboulonné            // now unset the parameter to not leak into new queries
555170edc8SSchplurtz le Déboulonné            // code by @splitbrain from farmer plugin
565170edc8SSchplurtz le Déboulonné            unset($_GET['animal']);
575170edc8SSchplurtz le Déboulonné            $params = [];
585170edc8SSchplurtz le Déboulonné            parse_str($_SERVER['QUERY_STRING'], $params);
595170edc8SSchplurtz le Déboulonné            if (isset($params['animal'])) unset($params['animal']);
605170edc8SSchplurtz le Déboulonné            $_SERVER['QUERY_STRING'] = http_build_query($params);
615170edc8SSchplurtz le Déboulonné        }
6213054fbfSAnika Henke        // check that $animal is a string and just a directory name and not a path
6313054fbfSAnika Henke        if (!is_string($animal) || strpbrk($animal, '\\/') !== false)
6413054fbfSAnika Henke            nice_die('Sorry! Invalid animal name!');
6513054fbfSAnika Henke        if (!is_dir($farm . '/' . $animal))
6613054fbfSAnika Henke            nice_die("Sorry! This Wiki doesn't exist!");
6713054fbfSAnika Henke        if (!defined('DOKU_FARM')) define('DOKU_FARM', $mode);
6813054fbfSAnika Henke        return $farm . '/' . $animal . '/conf/';
6913054fbfSAnika Henke    }
7013054fbfSAnika Henke
7113054fbfSAnika Henke    // virtual host based
72*24870174SAndreas Gohr    $uri = explode('/', $_SERVER['SCRIPT_NAME'] ?: $_SERVER['SCRIPT_FILENAME']);
7313054fbfSAnika Henke    $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
7413054fbfSAnika Henke    for ($i = count($uri) - 1; $i > 0; $i--) {
7513054fbfSAnika Henke        for ($j = count($server); $j > 0; $j--) {
7613054fbfSAnika Henke            $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
7713054fbfSAnika Henke            if (is_dir("$farm/$dir/conf/")) {
7813054fbfSAnika Henke                if (!defined('DOKU_FARM')) define('DOKU_FARM', 'virtual');
7913054fbfSAnika Henke                return "$farm/$dir/conf/";
8013054fbfSAnika Henke            }
8113054fbfSAnika Henke        }
8213054fbfSAnika Henke    }
8313054fbfSAnika Henke
8413054fbfSAnika Henke    // default conf directory in farm
8513054fbfSAnika Henke    if (is_dir("$farm/default/conf/")) {
8613054fbfSAnika Henke        if (!defined('DOKU_FARM')) define('DOKU_FARM', 'default');
8713054fbfSAnika Henke        return "$farm/default/conf/";
8813054fbfSAnika Henke    }
8913054fbfSAnika Henke    // farmer
9013054fbfSAnika Henke    return DOKU_INC . 'conf/';
9113054fbfSAnika Henke}
9213054fbfSAnika Henke
9313054fbfSAnika Henke/* Use default config files and local animal config files */
94*24870174SAndreas Gohr$config_cascade = [
95*24870174SAndreas Gohr    'main' => [
96*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/dokuwiki.php'],
97*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'local.php'],
98*24870174SAndreas Gohr        'protected' => [DOKU_CONF . 'local.protected.php']
99*24870174SAndreas Gohr    ],
100*24870174SAndreas Gohr    'acronyms' => [
101*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/acronyms.conf'],
102*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'acronyms.local.conf']
103*24870174SAndreas Gohr    ],
104*24870174SAndreas Gohr    'entities' => [
105*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/entities.conf'],
106*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'entities.local.conf']
107*24870174SAndreas Gohr    ],
108*24870174SAndreas Gohr    'interwiki' => [
109*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/interwiki.conf'],
110*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'interwiki.local.conf']
111*24870174SAndreas Gohr    ],
112*24870174SAndreas Gohr    'license' => [
113*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/license.php'],
114*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'license.local.php']
115*24870174SAndreas Gohr    ],
116*24870174SAndreas Gohr    'mediameta' => [
117*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/mediameta.php'],
118*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'mediameta.local.php']
119*24870174SAndreas Gohr    ],
120*24870174SAndreas Gohr    'mime' => [
121*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/mime.conf'],
122*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'mime.local.conf']
123*24870174SAndreas Gohr    ],
124*24870174SAndreas Gohr    'scheme' => [
125*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/scheme.conf'],
126*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'scheme.local.conf']
127*24870174SAndreas Gohr    ],
128*24870174SAndreas Gohr    'smileys' => [
129*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/smileys.conf'],
130*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'smileys.local.conf']
131*24870174SAndreas Gohr    ],
132*24870174SAndreas Gohr    'wordblock' => [
133*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/wordblock.conf'],
134*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'wordblock.local.conf']
135*24870174SAndreas Gohr    ],
136*24870174SAndreas Gohr    'acl' => [
137*24870174SAndreas Gohr        'default' => DOKU_CONF . 'acl.auth.php'
138*24870174SAndreas Gohr    ],
139*24870174SAndreas Gohr    'plainauth.users' => [
140*24870174SAndreas Gohr        'default' => DOKU_CONF . 'users.auth.php'
141*24870174SAndreas Gohr    ],
142*24870174SAndreas Gohr    'plugins' => [
143*24870174SAndreas Gohr        // needed since Angua
144*24870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/plugins.php'],
145*24870174SAndreas Gohr        'local' => [DOKU_CONF . 'plugins.local.php'],
146*24870174SAndreas Gohr        'protected' => [DOKU_INC . 'conf/plugins.required.php', DOKU_CONF . 'plugins.protected.php'],
147*24870174SAndreas Gohr    ],
148*24870174SAndreas Gohr    'userstyle' => [
149*24870174SAndreas Gohr        'screen' => [DOKU_CONF . 'userstyle.css', DOKU_CONF . 'userstyle.less'],
150*24870174SAndreas Gohr        'print' => [DOKU_CONF . 'userprint.css', DOKU_CONF . 'userprint.less'],
151*24870174SAndreas Gohr        'feed' => [DOKU_CONF . 'userfeed.css', DOKU_CONF . 'userfeed.less'],
152*24870174SAndreas Gohr        'all' => [DOKU_CONF . 'userall.css', DOKU_CONF . 'userall.less']
153*24870174SAndreas Gohr    ],
154*24870174SAndreas Gohr    'userscript' => [
155*24870174SAndreas Gohr        'default' => [DOKU_CONF . 'userscript.js']
156*24870174SAndreas Gohr    ]
157*24870174SAndreas Gohr];
158