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 */ 8 9// must be run within Dokuwiki 10if(!defined('DOKU_INC')) die(); 11 12class helper_plugin_farmer extends DokuWiki_Plugin { 13 14 private $allPlugins = array(); 15 16 /** 17 * Returns the name of the current animal if any, false otherwise 18 * 19 * @return string|false 20 */ 21 public function getAnimal() { 22 if(!isset($GLOBALS['FARMCORE'])) return false; 23 return $GLOBALS['FARMCORE']->getAnimal(); 24 } 25 26 /** 27 * Get the farm config 28 * 29 * @return array 30 */ 31 public function getConfig() { 32 if(!isset($GLOBALS['FARMCORE'])) return array(); 33 return $GLOBALS['FARMCORE']->getConfig(); 34 } 35 36 /** 37 * Was the current animal requested by host? 38 * 39 * @return bool 40 */ 41 public function isHostbased() { 42 if(!isset($GLOBALS['FARMCORE'])) return false; 43 return $GLOBALS['FARMCORE']->isHostbased(); 44 } 45 46 /** 47 * Was an animal requested that could not be found? 48 * 49 * @return bool 50 */ 51 public function wasNotfound() { 52 if(!isset($GLOBALS['FARMCORE'])) return false; 53 return $GLOBALS['FARMCORE']->wasNotfound(); 54 } 55 56 /** 57 * Guess the URL for an animal 58 * 59 * @param $animal 60 * @return string 61 */ 62 public function getAnimalURL($animal) { 63 $config = $this->getConfig(); 64 65 if(strpos($animal, '.') !== false) { 66 return 'http://'.$animal; 67 } elseif($config['base']['basedomain']) { 68 return 'http://'.$animal.'.'.$config['base']['basedomain']; 69 } else { 70 return DOKU_URL.'!'.$animal; 71 } 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 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 /** 197 * Actiate a specific plugin in a specific animal 198 * 199 * @param string $plugin Name of the plugin to be activated 200 * @param string $animal Directory of the animal within DOKU_FARMDIR 201 */ 202 public function activatePlugin($plugin, $animal) { 203 if (isset($this->allPlugins[$animal])) { 204 $plugins = $this->allPlugins[$animal]; 205 } else { 206 include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 207 } 208 if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) { 209 unset($plugins[$plugin]); 210 $this->writePluginConf($plugins, $animal); 211 } 212 $this->allPlugins[$animal] = $plugins; 213 } 214 215 /** 216 * @param string $plugin Name of the plugin to be deactivated 217 * @param string $animal Directory of the animal within DOKU_FARMDIR 218 */ 219 public function deactivatePlugin($plugin, $animal) { 220 if (isset($this->allPlugins[$animal])) { 221 $plugins = $this->allPlugins[$animal]; 222 } else { 223 include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 224 } 225 if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) { 226 $plugins[$plugin] = 0; 227 $this->writePluginConf($plugins, $animal); 228 } 229 $this->allPlugins[$animal] = $plugins; 230 } 231 232 /** 233 * Write the list of (deactivated) plugins as plugin configuration of an animal to file 234 * 235 * @param array $plugins associative array with the key being the plugin name and the value 0 or 1 236 * @param string $animal Directory of the animal within DOKU_FARMDIR 237 */ 238 public function writePluginConf($plugins, $animal) { 239 $pluginConf = '<?php' . "\n"; 240 foreach ($plugins as $plugin => $status) { 241 $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n"; 242 } 243 io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf); 244 touch(DOKU_FARMDIR . $animal . '/conf/local.php'); 245 } 246 247 /** 248 * Show a message for all errors which occured during form validation 249 * 250 * @param \dokuwiki\Form\Form $form The form to which the errors should be added. 251 * @param array $errorArray An associative array with the key being the name of the element at fault 252 * and the value being the associated error message. 253 */ 254 public function addErrorsToForm(\dokuwiki\Form\Form &$form, $errorArray) { 255 foreach ($errorArray as $elementName => $errorMessage) { 256 $offset = 0; 257 msg($errorMessage, -1); 258 while ($form->findPositionByAttribute('name',$elementName, $offset)) { 259 $offset = $form->findPositionByAttribute('name',$elementName, $offset); 260 $form->getElementAt($offset)->addClass('error'); 261 ++$offset; 262 } 263 } 264 } 265 266 /** 267 * @param string|null $page load adminpage $page, reload the current page if $page is ommited or null 268 */ 269 public function reloadAdminPage($page = null) { 270 global $ID; 271 $get = $_GET; 272 if(isset($get['id'])) unset($get['id']); 273 if ($page !== null ) { 274 $get['page'] = $page; 275 } 276 $self = wl($ID, $get, false, '&'); 277 send_redirect($self); 278 } 279 280 /** 281 * Download and extract the animal template 282 * 283 * @param string $animalpath 284 * 285 * @throws \splitbrain\PHPArchive\ArchiveIOException 286 */ 287 public function downloadTemplate($animalpath) { 288 file_put_contents($animalpath . '/_animal.zip',fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip','r')); 289 $zip = new splitbrain\PHPArchive\Zip(); 290 $zip->open($animalpath.'/_animal.zip'); 291 $zip->extract($animalpath); 292 $zip->close(); 293 unlink($animalpath.'/_animal.zip'); 294 } 295 296 297 /** 298 * The subdomain must contain at least two dots 299 * 300 * @link http://stackoverflow.com/questions/17986371/regular-expression-to-validate-fqdn-in-c-sharp-and-javascript 301 * 302 * @param string $subdomain 303 * 304 * @return bool 305 */ 306 public function validateSubdomain ($subdomain) { 307 return preg_match("/^(?=.{1,254}$)((?=[a-z0-9-]{1,63}\.)(xn--+)?[a-z0-9]+(-[a-z0-9]+)*\.){2,}[a-z]{2,63}$/i",$subdomain) === 1; 308 } 309 310 311 312 313 314} 315