xref: /dokuwiki/inc/farm.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
113054fbfSAnika Henke<?php
2*d4f83172SAndreas Gohr
313054fbfSAnika Henke/**
413054fbfSAnika Henke * This overwrites DOKU_CONF. Each animal gets its own configuration and data directory.
513054fbfSAnika Henke * This can be used together with preload.php. See preload.php.dist for an example setup.
613054fbfSAnika Henke * For more information see http://www.dokuwiki.org/farms.
713054fbfSAnika Henke *
813054fbfSAnika Henke * The farm directory (constant DOKU_FARMDIR) can be any directory and needs to be set.
913054fbfSAnika Henke * Animals are direct subdirectories of the farm directory.
1013054fbfSAnika Henke * There are two different approaches:
1113054fbfSAnika Henke *  * An .htaccess based setup can use any animal directory name:
1213054fbfSAnika Henke *    http://example.org/<path_to_farm>/subdir/ will need the subdirectory '$farm/subdir/'.
1313054fbfSAnika Henke *  * A virtual host based setup needs animal directory names which have to reflect
1413054fbfSAnika Henke *    the domain name: If an animal resides in http://www.example.org:8080/mysite/test/,
1513054fbfSAnika Henke *    directories that will match range from '$farm/8080.www.example.org.mysite.test/'
1613054fbfSAnika Henke *    to a simple '$farm/domain/'.
1713054fbfSAnika Henke *
1813054fbfSAnika Henke * @author Anika Henke <anika@selfthinker.org>
1913054fbfSAnika Henke * @author Michael Klier <chi@chimeric.de>
2013054fbfSAnika Henke * @author Christopher Smith <chris@jalakai.co.uk>
2113054fbfSAnika Henke * @author virtual host part of farm_confpath() based on conf_path() from Drupal.org's /includes/bootstrap.inc
2213054fbfSAnika Henke *   (see https://github.com/drupal/drupal/blob/7.x/includes/bootstrap.inc#L537)
2313054fbfSAnika Henke * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
2413054fbfSAnika Henke */
2513054fbfSAnika Henke
2664159a61SAndreas 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)
2724870174SAndreas Gohrif (!defined('DOKU_FARMDIR')) define('DOKU_FARMDIR', fullpath(__DIR__ . '/../') . '/');
2813054fbfSAnika Henkeif (!defined('DOKU_CONF')) define('DOKU_CONF', farm_confpath(DOKU_FARMDIR));
2913054fbfSAnika Henkeif (!defined('DOKU_FARM')) define('DOKU_FARM', false);
3013054fbfSAnika Henke
3113054fbfSAnika Henke/**
3213054fbfSAnika Henke * Find the appropriate configuration directory.
3313054fbfSAnika Henke *
3413054fbfSAnika Henke * If the .htaccess based setup is used, the configuration directory can be
3513054fbfSAnika Henke * any subdirectory of the farm directory.
3613054fbfSAnika Henke *
3713054fbfSAnika Henke * Otherwise try finding a matching configuration directory by stripping the
3813054fbfSAnika Henke * website's hostname from left to right and pathname from right to left. The
3913054fbfSAnika Henke * first configuration file found will be used; the remaining will ignored.
4013054fbfSAnika Henke * If no configuration file is found, return the default confdir './conf'.
41f50a239bSTakamura *
42f50a239bSTakamura * @param string $farm
43f50a239bSTakamura *
44f50a239bSTakamura * @return string
4513054fbfSAnika Henke */
4624870174SAndreas Gohrfunction farm_confpath($farm)
4724870174SAndreas Gohr{
4813054fbfSAnika Henke
4913054fbfSAnika Henke    // htaccess based or cli
5013054fbfSAnika Henke    // cli usage example: animal=your_animal bin/indexer.php
5124870174SAndreas Gohr    if (isset($_GET['animal']) || ('cli' == PHP_SAPI && isset($_SERVER['animal']))) {
525170edc8SSchplurtz le Déboulonné        $mode = isset($_GET['animal']) ? 'htaccess' : 'cli';
535170edc8SSchplurtz le Déboulonné        $animal = $mode == 'htaccess' ? $_GET['animal'] : $_SERVER['animal'];
545170edc8SSchplurtz le Déboulonné        if (isset($_GET['animal'])) {
555170edc8SSchplurtz le Déboulonné            // now unset the parameter to not leak into new queries
565170edc8SSchplurtz le Déboulonné            // code by @splitbrain from farmer plugin
575170edc8SSchplurtz le Déboulonné            unset($_GET['animal']);
585170edc8SSchplurtz le Déboulonné            $params = [];
595170edc8SSchplurtz le Déboulonné            parse_str($_SERVER['QUERY_STRING'], $params);
605170edc8SSchplurtz le Déboulonné            if (isset($params['animal'])) unset($params['animal']);
615170edc8SSchplurtz le Déboulonné            $_SERVER['QUERY_STRING'] = http_build_query($params);
625170edc8SSchplurtz le Déboulonné        }
6313054fbfSAnika Henke        // check that $animal is a string and just a directory name and not a path
6413054fbfSAnika Henke        if (!is_string($animal) || strpbrk($animal, '\\/') !== false)
6513054fbfSAnika Henke            nice_die('Sorry! Invalid animal name!');
6613054fbfSAnika Henke        if (!is_dir($farm . '/' . $animal))
6713054fbfSAnika Henke            nice_die("Sorry! This Wiki doesn't exist!");
6813054fbfSAnika Henke        if (!defined('DOKU_FARM')) define('DOKU_FARM', $mode);
6913054fbfSAnika Henke        return $farm . '/' . $animal . '/conf/';
7013054fbfSAnika Henke    }
7113054fbfSAnika Henke
7213054fbfSAnika Henke    // virtual host based
7324870174SAndreas Gohr    $uri = explode('/', $_SERVER['SCRIPT_NAME'] ?: $_SERVER['SCRIPT_FILENAME']);
7413054fbfSAnika Henke    $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
7513054fbfSAnika Henke    for ($i = count($uri) - 1; $i > 0; $i--) {
7613054fbfSAnika Henke        for ($j = count($server); $j > 0; $j--) {
7713054fbfSAnika Henke            $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
7813054fbfSAnika Henke            if (is_dir("$farm/$dir/conf/")) {
7913054fbfSAnika Henke                if (!defined('DOKU_FARM')) define('DOKU_FARM', 'virtual');
8013054fbfSAnika Henke                return "$farm/$dir/conf/";
8113054fbfSAnika Henke            }
8213054fbfSAnika Henke        }
8313054fbfSAnika Henke    }
8413054fbfSAnika Henke
8513054fbfSAnika Henke    // default conf directory in farm
8613054fbfSAnika Henke    if (is_dir("$farm/default/conf/")) {
8713054fbfSAnika Henke        if (!defined('DOKU_FARM')) define('DOKU_FARM', 'default');
8813054fbfSAnika Henke        return "$farm/default/conf/";
8913054fbfSAnika Henke    }
9013054fbfSAnika Henke    // farmer
9113054fbfSAnika Henke    return DOKU_INC . 'conf/';
9213054fbfSAnika Henke}
9313054fbfSAnika Henke
9413054fbfSAnika Henke/* Use default config files and local animal config files */
9524870174SAndreas Gohr$config_cascade = [
9624870174SAndreas Gohr    'main' => [
9724870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/dokuwiki.php'],
9824870174SAndreas Gohr        'local' => [DOKU_CONF . 'local.php'],
9924870174SAndreas Gohr        'protected' => [DOKU_CONF . 'local.protected.php']
10024870174SAndreas Gohr    ],
10124870174SAndreas Gohr    'acronyms' => [
10224870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/acronyms.conf'],
10324870174SAndreas Gohr        'local' => [DOKU_CONF . 'acronyms.local.conf']
10424870174SAndreas Gohr    ],
10524870174SAndreas Gohr    'entities' => [
10624870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/entities.conf'],
10724870174SAndreas Gohr        'local' => [DOKU_CONF . 'entities.local.conf']
10824870174SAndreas Gohr    ],
10924870174SAndreas Gohr    'interwiki' => [
11024870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/interwiki.conf'],
11124870174SAndreas Gohr        'local' => [DOKU_CONF . 'interwiki.local.conf']
11224870174SAndreas Gohr    ],
11324870174SAndreas Gohr    'license' => [
11424870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/license.php'],
11524870174SAndreas Gohr        'local' => [DOKU_CONF . 'license.local.php']
11624870174SAndreas Gohr    ],
11724870174SAndreas Gohr    'mediameta' => [
11824870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/mediameta.php'],
11924870174SAndreas Gohr        'local' => [DOKU_CONF . 'mediameta.local.php']
12024870174SAndreas Gohr    ],
12124870174SAndreas Gohr    'mime' => [
12224870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/mime.conf'],
12324870174SAndreas Gohr        'local' => [DOKU_CONF . 'mime.local.conf']
12424870174SAndreas Gohr    ],
12524870174SAndreas Gohr    'scheme' => [
12624870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/scheme.conf'],
12724870174SAndreas Gohr        'local' => [DOKU_CONF . 'scheme.local.conf']
12824870174SAndreas Gohr    ],
12924870174SAndreas Gohr    'smileys' => [
13024870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/smileys.conf'],
13124870174SAndreas Gohr        'local' => [DOKU_CONF . 'smileys.local.conf']
13224870174SAndreas Gohr    ],
13324870174SAndreas Gohr    'wordblock' => [
13424870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/wordblock.conf'],
13524870174SAndreas Gohr        'local' => [DOKU_CONF . 'wordblock.local.conf']
13624870174SAndreas Gohr    ],
13724870174SAndreas Gohr    'acl' => [
13824870174SAndreas Gohr        'default' => DOKU_CONF . 'acl.auth.php'
13924870174SAndreas Gohr    ],
14024870174SAndreas Gohr    'plainauth.users' => [
14124870174SAndreas Gohr        'default' => DOKU_CONF . 'users.auth.php'
14224870174SAndreas Gohr    ],
14324870174SAndreas Gohr    'plugins' => [
14424870174SAndreas Gohr        // needed since Angua
14524870174SAndreas Gohr        'default' => [DOKU_INC . 'conf/plugins.php'],
14624870174SAndreas Gohr        'local' => [DOKU_CONF . 'plugins.local.php'],
14724870174SAndreas Gohr        'protected' => [DOKU_INC . 'conf/plugins.required.php', DOKU_CONF . 'plugins.protected.php'],
14824870174SAndreas Gohr    ],
14924870174SAndreas Gohr    'userstyle' => [
15024870174SAndreas Gohr        'screen' => [DOKU_CONF . 'userstyle.css', DOKU_CONF . 'userstyle.less'],
15124870174SAndreas Gohr        'print' => [DOKU_CONF . 'userprint.css', DOKU_CONF . 'userprint.less'],
15224870174SAndreas Gohr        'feed' => [DOKU_CONF . 'userfeed.css', DOKU_CONF . 'userfeed.less'],
15324870174SAndreas Gohr        'all' => [DOKU_CONF . 'userall.css', DOKU_CONF . 'userall.less']
15424870174SAndreas Gohr    ],
15524870174SAndreas Gohr    'userscript' => [
15624870174SAndreas Gohr        'default' => [DOKU_CONF . 'userscript.js']
15724870174SAndreas Gohr    ]
15824870174SAndreas Gohr];
159