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