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 * @version 1.0.1 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 * 134 * @return bool Returns TRUE on success, FALSE on failure 135 */ 136 function io_copyDir($source, $destination) { 137 if(is_link($source)) { 138 io_lock($destination); 139 $result = symlink(readlink($source), $destination); 140 io_unlock($destination); 141 return $result; 142 } 143 144 if(is_file($source)) { 145 io_lock($destination); 146 $result = copy($source, $destination); 147 io_unlock($destination); 148 return $result; 149 } 150 151 if(!is_dir($destination)) { 152 io_mkdir_p($destination); 153 } 154 155 $dir = @dir($source); 156 if($dir === false) return false; 157 while(false !== ($entry = $dir->read())) { 158 if($entry == '.' || $entry == '..') { 159 continue; 160 } 161 162 // recurse into directories 163 $this->io_copyDir("$source/$entry", "$destination/$entry"); 164 } 165 166 $dir->close(); 167 return true; 168 } 169 170 /** 171 * get a list of all Plugins installed in the farmer wiki, regardless whether they are active or not. 172 * 173 * @return array 174 */ 175 public function getAllPlugins() { 176 $dir = dir(DOKU_PLUGIN); 177 $plugins = array(); 178 while(false !== ($entry = $dir->read())) { 179 if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') { 180 continue; 181 } 182 if(!is_dir(DOKU_PLUGIN . "/$entry")) { 183 continue; 184 } 185 $plugins[] = $entry; 186 } 187 sort($plugins); 188 return $plugins; 189 } 190 191 /** 192 * Actiate a specific plugin in a specific animal 193 * 194 * @param string $plugin Name of the plugin to be activated 195 * @param string $animal Directory of the animal within DOKU_FARMDIR 196 */ 197 public function activatePlugin($plugin, $animal) { 198 if(isset($this->allPlugins[$animal])) { 199 $plugins = $this->allPlugins[$animal]; 200 } else { 201 include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 202 } 203 if(isset($plugins[$plugin]) && $plugins[$plugin] === 0) { 204 unset($plugins[$plugin]); 205 $this->writePluginConf($plugins, $animal); 206 } 207 $this->allPlugins[$animal] = $plugins; 208 } 209 210 /** 211 * @param string $plugin Name of the plugin to be deactivated 212 * @param string $animal Directory of the animal within DOKU_FARMDIR 213 */ 214 public function deactivatePlugin($plugin, $animal) { 215 if(isset($this->allPlugins[$animal])) { 216 $plugins = $this->allPlugins[$animal]; 217 } else { 218 include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 219 } 220 if(!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) { 221 $plugins[$plugin] = 0; 222 $this->writePluginConf($plugins, $animal); 223 } 224 $this->allPlugins[$animal] = $plugins; 225 } 226 227 /** 228 * Write the list of (deactivated) plugins as plugin configuration of an animal to file 229 * 230 * @param array $plugins associative array with the key being the plugin name and the value 0 or 1 231 * @param string $animal Directory of the animal within DOKU_FARMDIR 232 */ 233 public function writePluginConf($plugins, $animal) { 234 $pluginConf = '<?php' . "\n"; 235 foreach($plugins as $plugin => $status) { 236 $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n"; 237 } 238 io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf); 239 touch(DOKU_FARMDIR . $animal . '/conf/local.php'); 240 } 241} 242