1<?php 2/** 3 * DokuWiki Plugin farmer (Helper Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Michael Große <grosse@cosmocode.de> 7 * @author Andreas Gohr <gohr@cosmocode.de> 8 */ 9 10// must be run within Dokuwiki 11if(!defined('DOKU_INC')) die(); 12 13class helper_plugin_farmer extends DokuWiki_Plugin { 14 15 private $allPlugins = array(); 16 17 /** 18 * Returns the name of the current animal if any, false otherwise 19 * 20 * @return string|false 21 */ 22 public function getAnimal() { 23 if(!isset($GLOBALS['FARMCORE'])) return false; 24 return $GLOBALS['FARMCORE']->getAnimal(); 25 } 26 27 /** 28 * Get the farm config 29 * 30 * @return array 31 */ 32 public function getConfig() { 33 if(!isset($GLOBALS['FARMCORE'])) return array(); 34 return $GLOBALS['FARMCORE']->getConfig(); 35 } 36 37 /** 38 * Was the current animal requested by host? 39 * 40 * @return bool 41 */ 42 public function isHostbased() { 43 if(!isset($GLOBALS['FARMCORE'])) return false; 44 return $GLOBALS['FARMCORE']->isHostbased(); 45 } 46 47 /** 48 * Was an animal requested that could not be found? 49 * 50 * @return bool 51 */ 52 public function wasNotfound() { 53 if(!isset($GLOBALS['FARMCORE'])) return false; 54 return $GLOBALS['FARMCORE']->wasNotfound(); 55 } 56 57 /** 58 * Guess the URL for an animal 59 * 60 * @param $animal 61 * @return string 62 */ 63 public function getAnimalURL($animal) { 64 $config = $this->getConfig(); 65 66 if(strpos($animal, '.') !== false) { 67 return 'http://' . $animal; 68 } elseif($config['base']['basedomain']) { 69 return 'http://' . $animal . '.' . $config['base']['basedomain']; 70 } else { 71 return DOKU_URL . '!' . $animal . '/'; 72 } 73 } 74 75 /** 76 * List of all animals, i.e. directories within DOKU_FARMDIR without the template. 77 * 78 * @return array 79 */ 80 public function getAllAnimals() { 81 $animals = array(); 82 $list = glob(DOKU_FARMDIR . '/*/conf/', GLOB_ONLYDIR); 83 foreach($list as $path) { 84 $animal = basename(dirname($path)); 85 if($animal == '_animal') continue; // old template 86 $animals[] = $animal; 87 } 88 sort($animals); 89 return $animals; 90 } 91 92 /** 93 * checks wether $path is in under $container 94 * 95 * @param string $path 96 * @param string $container 97 * @return bool 98 */ 99 public function isInPath($path, $container) { 100 return (strpos(fullpath($path), fullpath($container)) === 0); 101 } 102 103 /** 104 * Check if the farm is correctly configured for this farmer plugin 105 * 106 * @return bool 107 */ 108 public function checkFarmSetup() { 109 return defined('DOKU_FARMDIR') && isset($GLOBALS['FARMCORE']); 110 } 111 112 /** 113 * @param string $animalname 114 * 115 * @return bool 116 */ 117 public function validateAnimalName($animalname) { 118 return preg_match("/^[a-z0-9]+([\\.\\-][a-z0-9]+)*$/i", $animalname) === 1; 119 } 120 121 /** 122 * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki. 123 * 124 * @todo: needs tests 125 * 126 * @author Aidan Lister <aidan@php.net> 127 * @author Michael Große <grosse@cosmocode.de> 128 * @author Andreas Gohr <gohr@cosmocode.de> 129 * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ 130 * 131 * @param string $source Source path 132 * @param string $destination Destination path 133 * @param string $exclude Regular expression to exclude files or directories (complete with delimiters) 134 * @return bool Returns TRUE on success, FALSE on failure 135 */ 136 function io_copyDir($source, $destination, $exclude = '') { 137 if($exclude && preg_match($exclude, $source)) { 138 return true; 139 } 140 141 if(is_link($source)) { 142 io_lock($destination); 143 $result = symlink(readlink($source), $destination); 144 io_unlock($destination); 145 return $result; 146 } 147 148 if(is_file($source)) { 149 io_lock($destination); 150 $result = copy($source, $destination); 151 io_unlock($destination); 152 return $result; 153 } 154 155 if(!is_dir($destination)) { 156 io_mkdir_p($destination); 157 } 158 159 $dir = @dir($source); 160 if($dir === false) return false; 161 while(false !== ($entry = $dir->read())) { 162 if($entry == '.' || $entry == '..') { 163 continue; 164 } 165 166 // recurse into directories 167 $this->io_copyDir("$source/$entry", "$destination/$entry", $exclude); 168 } 169 170 $dir->close(); 171 return true; 172 } 173 174 /** 175 * get a list of all Plugins installed in the farmer wiki, regardless whether they are active or not. 176 * 177 * @return array 178 */ 179 public function getAllPlugins() { 180 $dir = dir(DOKU_PLUGIN); 181 $plugins = array(); 182 while(false !== ($entry = $dir->read())) { 183 if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') { 184 continue; 185 } 186 if(!is_dir(DOKU_PLUGIN . "/$entry")) { 187 continue; 188 } 189 $plugins[] = $entry; 190 } 191 sort($plugins); 192 return $plugins; 193 } 194 195 /** 196 * Actiate a specific plugin in a specific animal 197 * 198 * @param string $plugin Name of the plugin to be activated 199 * @param string $animal Directory of the animal within DOKU_FARMDIR 200 */ 201 public function activatePlugin($plugin, $animal) { 202 if(isset($this->allPlugins[$animal])) { 203 $plugins = $this->allPlugins[$animal]; 204 } else { 205 include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 206 } 207 if(isset($plugins[$plugin]) && $plugins[$plugin] === 0) { 208 unset($plugins[$plugin]); 209 $this->writePluginConf($plugins, $animal); 210 } 211 $this->allPlugins[$animal] = $plugins; 212 } 213 214 /** 215 * @param string $plugin Name of the plugin to be deactivated 216 * @param string $animal Directory of the animal within DOKU_FARMDIR 217 */ 218 public function deactivatePlugin($plugin, $animal) { 219 if(isset($this->allPlugins[$animal])) { 220 $plugins = $this->allPlugins[$animal]; 221 } else { 222 include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 223 } 224 if(!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) { 225 $plugins[$plugin] = 0; 226 $this->writePluginConf($plugins, $animal); 227 } 228 $this->allPlugins[$animal] = $plugins; 229 } 230 231 /** 232 * Write the list of (deactivated) plugins as plugin configuration of an animal to file 233 * 234 * @param array $plugins associative array with the key being the plugin name and the value 0 or 1 235 * @param string $animal Directory of the animal within DOKU_FARMDIR 236 */ 237 public function writePluginConf($plugins, $animal) { 238 $pluginConf = '<?php' . "\n"; 239 foreach($plugins as $plugin => $status) { 240 $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n"; 241 } 242 io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf); 243 touch(DOKU_FARMDIR . $animal . '/conf/local.php'); 244 } 245} 246