1<?php 2 3/** 4 * Core Manager for the Farm functionality 5 * 6 * This class is initialized before any other DokuWiki code runs. Therefore it is 7 * completely selfcontained and does not use any of DokuWiki's utility functions. 8 * 9 * It's registered as a global $FARMCORE variable but you should not interact with 10 * it directly. Instead use the Farmer plugin's helper component. 11 * 12 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 13 * @author Andreas Gohr <gohr@cosmocode.de> 14 */ 15class DokuWikiFarmCore { 16 /** 17 * @var array The default config - changed by loadConfig 18 */ 19 protected $config = array( 20 'base' => array( 21 'farmdir' => '', 22 'farmhost' => '', 23 'basedomain' => '', 24 ), 25 'notfound' => array( 26 'show' => 'farmer', 27 'url' => '' 28 ), 29 'inherit' => array( 30 'main' => 1, 31 'acronyms' => 1, 32 'entities' => 1, 33 'interwiki' => 1, 34 'license' => 1, 35 'mime' => 1, 36 'scheme' => 1, 37 'smileys' => 1, 38 'wordblock' => 1, 39 'users' => 0, 40 'plugins' => 0, 41 'userstyle' => 0, 42 'userscript' => 0, 43 'styleini' => 1 44 ) 45 ); 46 47 /** @var string|false The current animal, false for farmer */ 48 protected $animal = false; 49 /** @var bool true if an animal was requested but was not found */ 50 protected $notfound = false; 51 /** @var bool true if the current animal was requested by host */ 52 protected $hostbased = false; 53 54 /** 55 * DokuWikiFarmCore constructor. 56 * 57 * This initializes the whole farm by loading the configuration and setting 58 * DOKU_CONF depending on the requested animal 59 */ 60 public function __construct() { 61 $this->loadConfig(); 62 if($this->config['base']['farmdir'] === '') return; // farm setup not complete 63 $this->config['base']['farmdir'] = rtrim($this->config['base']['farmdir'], '/').'/'; // trailing slash always 64 define('DOKU_FARMDIR', $this->config['base']['farmdir']); 65 66 // animal? 67 $this->detectAnimal(); 68 69 // setup defines 70 define('DOKU_FARM_ANIMAL', $this->animal); 71 if($this->animal) { 72 define('DOKU_CONF', DOKU_FARMDIR . $this->animal . '/conf/'); 73 } else { 74 define('DOKU_CONF', DOKU_INC . '/conf/'); 75 } 76 77 $this->setupCascade(); 78 $this->adjustCascade(); 79 } 80 81 /** 82 * @return array the current farm configuration 83 */ 84 public function getConfig() { 85 return $this->config; 86 } 87 88 /** 89 * @return false|string 90 */ 91 public function getAnimal() { 92 return $this->animal; 93 } 94 95 /** 96 * @return boolean 97 */ 98 public function isHostbased() { 99 return $this->hostbased; 100 } 101 102 /** 103 * @return boolean 104 */ 105 public function wasNotfound() { 106 return $this->notfound; 107 } 108 109 /** 110 * @return string 111 */ 112 public function getAnimalDataDir() { 113 return DOKU_FARMDIR . $this->getAnimal() . '/data/'; 114 } 115 116 /** 117 * @return string 118 */ 119 public function getAnimalBaseDir() { 120 if($this->isHostbased()) return ''; 121 return getBaseURL() . '!' . $this->getAnimal(); 122 } 123 124 /** 125 * Detect the current animal 126 * 127 * Sets internal members $animal, $notfound and $hostbased 128 * 129 * This borrows form DokuWiki's inc/farm.php but does not support a default conf dir 130 */ 131 protected function detectAnimal() { 132 $farmdir = $this->config['base']['farmdir']; 133 $farmhost = $this->config['base']['farmhost']; 134 135 // check if animal was set via parameter (rewrite or CLI) 136 $animal = ''; 137 if(isset($_REQUEST['animal'])) $animal = $_REQUEST['animal']; 138 if('cli' == php_sapi_name() && isset($_SERVER['animal'])) $animal = $_SERVER['animal']; 139 if($animal) { 140 // check that $animal is a string and just a directory name and not a path 141 if(!is_string($animal) || strpbrk($animal, '\\/') !== false) { 142 $this->notfound = true; 143 return; 144 }; 145 $animal = strtolower($animal); 146 147 // check if animal exists 148 if(is_dir("$farmdir/$animal/conf")) { 149 $this->animal = $animal; 150 return; 151 } else { 152 $this->notfound = true; 153 return; 154 } 155 } 156 157 // no host - no host based setup. if we're still here then it's the farmer 158 if(!isset($_SERVER['HTTP_HOST'])) return; 159 160 // is this the farmer? 161 if(strtolower($_SERVER['HTTP_HOST']) == $farmhost) { 162 return; 163 } 164 165 // still here? check for host based 166 $this->hostbased = true; 167 $possible = $this->getAnimalNamesForHost($_SERVER['HTTP_HOST']); 168 foreach($possible as $animal) { 169 if(is_dir("$farmdir/$animal/conf/")) { 170 $this->animal = $animal; 171 return; 172 } 173 } 174 175 // no hit 176 $this->notfound = true; 177 return; 178 } 179 180 /** 181 * Return a list of possible animal names for the given host 182 * 183 * @param string $host the HTTP_HOST header 184 * @return array 185 */ 186 protected function getAnimalNamesForHost($host) { 187 $animals = array(); 188 $parts = explode('.', implode('.', explode(':', rtrim($host, '.')))); 189 for($j = count($parts); $j > 0; $j--) { 190 // strip from the end 191 $animals[] = implode('.', array_slice($parts, 0, $j)); 192 // strip from the end without host part 193 $animals[] = implode('.', array_slice($parts, 1, $j)); 194 } 195 $animals = array_unique($animals); 196 $animals = array_filter($animals); 197 usort( 198 $animals, 199 // compare by length, then alphabet 200 function ($a, $b) { 201 $ret = strlen($b) - strlen($a); 202 if($ret != 0) return $ret; 203 return $a > $b; 204 } 205 ); 206 return $animals; 207 } 208 209 /** 210 * This sets up the default farming config cascade 211 */ 212 protected function setupCascade() { 213 global $config_cascade; 214 $config_cascade = array( 215 'main' => array( 216 'default' => array(DOKU_INC . 'conf/dokuwiki.php',), 217 'local' => array(DOKU_CONF . 'local.php',), 218 'protected' => array(DOKU_CONF . 'local.protected.php',), 219 ), 220 'acronyms' => array( 221 'default' => array(DOKU_INC . 'conf/acronyms.conf',), 222 'local' => array(DOKU_CONF . 'acronyms.local.conf',), 223 ), 224 'entities' => array( 225 'default' => array(DOKU_INC . 'conf/entities.conf',), 226 'local' => array(DOKU_CONF . 'entities.local.conf',), 227 ), 228 'interwiki' => array( 229 'default' => array(DOKU_INC . 'conf/interwiki.conf',), 230 'local' => array(DOKU_CONF . 'interwiki.local.conf',), 231 ), 232 'license' => array( 233 'default' => array(DOKU_INC . 'conf/license.php',), 234 'local' => array(DOKU_CONF . 'license.local.php',), 235 ), 236 'mediameta' => array( 237 'default' => array(DOKU_INC . 'conf/mediameta.php',), 238 'local' => array(DOKU_CONF . 'mediameta.local.php',), 239 ), 240 'mime' => array( 241 'default' => array(DOKU_INC . 'conf/mime.conf',), 242 'local' => array(DOKU_CONF . 'mime.local.conf',), 243 ), 244 'scheme' => array( 245 'default' => array(DOKU_INC . 'conf/scheme.conf',), 246 'local' => array(DOKU_CONF . 'scheme.local.conf',), 247 ), 248 'smileys' => array( 249 'default' => array(DOKU_INC . 'conf/smileys.conf',), 250 'local' => array(DOKU_CONF . 'smileys.local.conf',), 251 ), 252 'wordblock' => array( 253 'default' => array(DOKU_INC . 'conf/wordblock.conf',), 254 'local' => array(DOKU_CONF . 'wordblock.local.conf',), 255 ), 256 'acl' => array( 257 'default' => DOKU_CONF . 'acl.auth.php', 258 ), 259 'plainauth.users' => array( 260 'default' => DOKU_CONF . 'users.auth.php', 261 ), 262 'plugins' => array( 263 'default' => array(DOKU_INC . 'conf/plugins.php',), 264 'local' => array(DOKU_CONF . 'plugins.local.php',), 265 'protected' => array( 266 DOKU_INC . 'conf/plugins.required.php', 267 DOKU_CONF . 'plugins.protected.php', 268 ), 269 ), 270 'userstyle' => array( 271 'screen' => array(DOKU_CONF . 'userstyle.css', DOKU_CONF . 'userstyle.less',), 272 'print' => array(DOKU_CONF . 'userprint.css', DOKU_CONF . 'userprint.less',), 273 'feed' => array(DOKU_CONF . 'userfeed.css', DOKU_CONF . 'userfeed.less',), 274 'all' => array(DOKU_CONF . 'userall.css', DOKU_CONF . 'userall.less',), 275 ), 276 'userscript' => array( 277 'default' => array(DOKU_CONF . 'userscript.js',), 278 ), 279 'styleini' => array( 280 'default' => array(DOKU_INC . 'lib/tpl/%TEMPLATE%/' . 'style.ini'), 281 'local' => array(DOKU_CONF . 'tpl/%TEMPLATE%/' . 'style.ini') 282 ), 283 ); 284 } 285 286 /** 287 * This adds additional files to the config cascade based on the inheritence settings 288 * 289 * These are only added for animals, not the farmer 290 */ 291 protected function adjustCascade() { 292 // nothing to do when on the farmer: 293 if(!$this->animal) return; 294 295 global $config_cascade; 296 foreach($this->config['inherit'] as $key => $val) { 297 if(!$val) continue; 298 299 // prepare what is to append or prepend 300 $append = array(); 301 $prepend = array(); 302 if($key == 'main') { 303 $append = array( 304 'default' => array(DOKU_INC . 'conf/local.php'), 305 'protected' => array(DOKU_INC . 'lib/plugins/farmer/includes/config.php') 306 ); 307 } elseif($key == 'license') { 308 $append = array('default' => array(DOKU_INC . 'conf/' . $key . '.local.php')); 309 } elseif($key == 'userscript') { 310 $prepend = array('default' => array(DOKU_INC . 'conf/userscript.js')); 311 } elseif($key == 'userstyle') { 312 $prepend = array( 313 'screen' => array(DOKU_INC . 'conf/userstyle.css', DOKU_INC . 'conf/userstyle.less',), 314 'print' => array(DOKU_INC . 'conf/userprint.css', DOKU_INC . 'conf/userprint.less',), 315 'feed' => array(DOKU_INC . 'conf/userfeed.css', DOKU_INC . 'conf/userfeed.less',), 316 'all' => array(DOKU_INC . 'conf/userall.css', DOKU_INC . 'conf/userall.less',), 317 ); 318 } elseif ($key == 'styleini') { 319 $append = array( 320 'local' => array( 321 DOKU_INC . 'conf/tpl/TPL_PLACEHOLDER/style.ini' 322 ) 323 ); 324 } elseif($key == 'users') { 325 $config_cascade['plainauth.users']['protected'] = DOKU_INC . 'conf/users.auth.php'; 326 } elseif($key == 'plugins') { 327 $append = array('default' => array(DOKU_INC . 'conf/plugins.local.php')); 328 } else { 329 $append = array('default' => array(DOKU_INC . 'conf/' . $key . '.local.conf')); 330 } 331 332 // add to cascade 333 foreach($prepend as $section => $data) { 334 $config_cascade[$key][$section] = array_merge($data, $config_cascade[$key][$section]); 335 } 336 foreach($append as $section => $data) { 337 $config_cascade[$key][$section] = array_merge($config_cascade[$key][$section], $data); 338 } 339 } 340 341 // add plugin overrides 342 $config_cascade['plugins']['protected'][] = DOKU_INC . 'lib/plugins/farmer/includes/plugins.php'; 343 } 344 345 /** 346 * Loads the farm config 347 */ 348 protected function loadConfig() { 349 $ini = DOKU_INC . 'conf/farm.ini'; 350 if(!file_exists($ini)) return; 351 $config = parse_ini_file($ini, true); 352 foreach(array_keys($this->config) as $section) { 353 if(isset($config[$section])) { 354 $this->config[$section] = array_merge( 355 $this->config[$section], 356 $config[$section] 357 ); 358 } 359 } 360 361 $this->config['base']['farmdir'] = trim($this->config['base']['farmdir']); 362 $this->config['base']['farmhost'] = strtolower(trim($this->config['base']['farmhost'])); 363 } 364 365} 366 367// initialize it globally 368if(!defined('DOKU_UNITTEST')) { 369 global $FARMCORE; 370 $FARMCORE = new DokuWikiFarmCore(); 371} 372