<?php
/**
 * This overwrites the DOKU_CONF. Each animal gets its own configuration and data directory.
 */

function displayAndExit($r) {
		$l = 'en';
		if($r != 'unknown') {
			$farmconf = array();
			require_once(DOKU_FARM_PLUGIN.'animal.class.php');
			$a = new dokuwiki_farm_animal(DOKU_FARM_ANIMAL, null);
			$conf = $a->getWikiConf();
			if(isset($conf['editable']['lang']) && !empty($conf['editable']['lang'])) $l = $conf['editable']['lang'];
			if(isset($conf['protected']['lang']) && !empty($conf['protected']['lang'])) $l = $conf['protected']['lang'];
		}else{
			$conf = array();
			include dirname(__FILE__).'/../conf/local.php';
			if(isset($conf['lang']) && !empty($conf['lang'])) $l = $conf['lang'];
		}
		$lang = array();
		@include DOKU_FARM_PLUGIN.'lang/en/lang.php';
		if($l != 'en') @include DOKU_FARM_PLUGIN.'lang/'.$l.'/lang.php';
		@include DOKU_FARM_PLUGIN.'customlang/en/lang.php';
		if($l != 'en') @include DOKU_FARM_PLUGIN.'customlang/'.$l.'/lang.php';
		
		$title = str_replace('%s', DOKU_FARM_ANIMAL, $lang['message_'.$r.'_title']);
		$msg = str_replace('%s', DOKU_FARM_ANIMAL, $lang['message_'.$r.'_desc']);
		
		echo '<html>
	<head>
		<title>'.$title.'</title>
	</head>
	<body>
		<h3>'.$title.'</h3>
		<p>'.$msg.'</p>
	</body>
</html>';
		exit();
}

$cf = fullpath(dirname(__FILE__).'/../').'/lib/plugins/farm/config.php';
$vhf = fullpath(dirname(__FILE__).'/../').'/lib/plugins/farm/virtual_hosts.php';

if(@file_exists($cf)) {
    $farmconf = array();
    include $cf;
	
	$barn = $farmconf['farmfsroot'].$farmconf['barn'];
	$plugin = $farmconf['farmfsroot'].$farmconf['farmer'].'lib/plugins/farm/';
	define('DOKU_FARM_PLUGIN', $plugin);
	
	$animal = null;
	if(isset($_REQUEST['virtual_animal']) && @file_exists($vhf)) {
		$host = $_SERVER['HTTP_HOST'];
		foreach(explode("\n", @file_get_contents($vhf)) as $l) {
			if(preg_match('`^\s*([^\s]+)\s+([^\s#]+)`', $l, $m)) {
				if($m[1] == $host) {
					$animal = $m[2];
					define('DOKU_BASE', '/');
					define('DOKU_REL', '/');
					$p = isset($_SERVER['HTTPS']) ? ((!preg_match('`off`i', $_SERVER['HTTPS']) && $_SERVER['HTTPS']) ? 'https' : 'http') : 'http';
					define('DOKU_URL', $p.'://'.$m[1].'/');
					break;
				}
			}
		}
	}
	
	if(!$animal) {
		$animal = isset($_REQUEST['animal']) && !empty($_REQUEST['animal']) ? $_REQUEST['animal'] : null;
		if(!preg_match('`^[a-z0-9\_\.\-]+$`i', $animal)) $animal = null;
	}
	
	if($animal) {
		define('DOKU_FARM_ANIMAL', $animal);
		if(!@is_dir($barn.$animal)) displayAndExit('unknown');
		
		$status = null;
		$locked = null;
		foreach(explode("\n", @file_get_contents($barn.$animal.'/animal.meta')) as $l) {
			if(preg_match('`^\s*([^\s]+)\s+:\s+([^\s]+)`', $l, $m)) {
				if($m[1] == 'status') $status = $m[2];
				if($m[1] == 'locked') $locked = ($m[2] != null && $m[2] != '0');
			}
		}
		if(!$status) $status = 'open';
		
		if($status != 'open') displayAndExit($status);
		
		$isfarmer = false;
		define('DOKU_CONF', $barn.$animal.'/conf/');
		$wr = $farmconf['farmwebroot'];
		if(preg_match('`^([^:]+://)?([^/]+)?/?(.*)$`i', $wr, $m)) $wr = $m[3];
		$exp = !$farmconf['userewrite'] ? $farmconf['farmer'] : (!$farmconf['farmrewrite'] ? $farmconf['barn'] : '').$animal.'/';
		define('DOKU_ANIMAL_BASEDIR', $wr);
		if(!defined('DOKU_BASE')) define('DOKU_BASE', '/'.$wr.$exp);
		if(!defined('DOKU_REL')) define('DOKU_REL', DOKU_BASE);
		if(!defined('DOKU_URL')) define('DOKU_URL', '/'.$farmconf['farmwebroot'].$exp);
	}else{
		// no animal name passed, we are the farmer
		$isfarmer = true;
		define('DOKU_CONF', $farmconf['farmfsroot'].$farmconf['farmer'].'conf/');
	}
	define('DOKU_IS_FARMER', $isfarmer);
}

$config_cascade = array(
    'main' => array(
        'default'   => array(DOKU_INC.'conf/dokuwiki.php'),
        'local'     => array(DOKU_CONF.'local.php'),
        'protected' => array(DOKU_CONF.'local.protected.php'),
    ),
    'acronyms'  => array(
        'default'   => array(DOKU_INC.'conf/acronyms.conf'),
        'local'     => array(DOKU_CONF.'acronyms.local.conf'),
    ),
    'entities'  => array(
        'default'   => array(DOKU_INC.'conf/entities.conf'),
        'local'     => array(DOKU_CONF.'entities.local.conf'),
    ),
    'interwiki' => array(
        'default'   => array(DOKU_INC.'conf/interwiki.conf'),
        'local'     => array(DOKU_CONF.'interwiki.local.conf'),
    ),
    'license' => array(
        'default'   => array(DOKU_INC.'conf/license.php'),
        'local'     => array(DOKU_CONF.'license.local.php'),
    ),
    'mediameta' => array(
        'default'   => array(DOKU_INC.'conf/mediameta.php'),
        'local'     => array(DOKU_CONF.'mediameta.local.php'),
    ),
    'mime'      => array(
        'default'   => array(DOKU_INC.'conf/mime.conf'),
        'local'     => array(DOKU_CONF.'mime.local.conf'),
    ),
    'scheme'    => array(
        'default'   => array(DOKU_INC.'conf/scheme.conf'),
        'local'     => array(DOKU_CONF.'scheme.local.conf'),
    ),
    'smileys'   => array(
        'default'   => array(DOKU_INC.'conf/smileys.conf'),
        'local'     => array(DOKU_CONF.'smileys.local.conf'),
    ),
    'wordblock' => array(
        'default'   => array(DOKU_INC.'conf/wordblock.conf'),
        'local'     => array(DOKU_CONF.'wordblock.local.conf'),
    )
);

?>