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 * List of all animals, i.e. directories within DOKU_FARMDIR without the template. 58 * 59 * @return array 60 */ 61 public function getAllAnimals() { 62 $animals = array(); 63 $list = glob(DOKU_FARMDIR . '/*/conf/', GLOB_ONLYDIR); 64 foreach($list as $path) { 65 $animal = basename(dirname($path)); 66 if($animal == '_animal') continue; // old template 67 $animals[] = $animal; 68 } 69 sort($animals); 70 return $animals; 71 } 72 73 /** 74 * checks wether $path is in under $container 75 * 76 * @param string $path 77 * @param string $container 78 * @return bool 79 */ 80 public function isInPath ($path, $container) { 81 return (strpos(fullpath($path), fullpath($container)) === 0); 82 } 83 84 /** 85 * Check if the farm is correctly configured for this farmer plugin 86 * 87 * @return bool 88 */ 89 public function checkFarmSetup () { 90 return defined('DOKU_FARMDIR') && isset($GLOBALS['FARMCORE']); 91 } 92 93 94 95 96 97 98 99 100 /** 101 * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki. 102 * 103 * @todo: needs tests 104 * 105 * @author Aidan Lister <aidan@php.net> 106 * @author Michael Große <grosse@cosmocode.de> 107 * @version 1.0.1 108 * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ 109 * 110 * @param string $source Source path 111 * @param string $destination Destination path 112 * 113 * @return bool Returns TRUE on success, FALSE on failure 114 */ 115 function io_copyDir($source, $destination) { 116 if (is_link($source)) { 117 io_lock($destination); 118 $result=symlink(readlink($source), $destination); 119 io_unlock($destination); 120 return $result; 121 } 122 123 if (is_file($source)) { 124 io_lock($destination); 125 $result=copy($source, $destination); 126 io_unlock($destination); 127 return $result; 128 } 129 130 if (!is_dir($destination)) { 131 io_mkdir_p($destination); 132 } 133 134 $dir = dir($source); 135 while (false !== ($entry = $dir->read())) { 136 if ($entry == '.' || $entry == '..') { 137 continue; 138 } 139 140 // recurse into directories 141 $this->io_copyDir("$source/$entry", "$destination/$entry"); 142 } 143 144 $dir->close(); 145 return true; 146 } 147 148 /** 149 * get a list of all Plugins installed in the farmer wiki, regardless whether they are active or not. 150 * 151 * @return array 152 */ 153 public function getAllPlugins() { 154 $dir = dir(DOKU_PLUGIN); 155 $plugins = array(); 156 while (false !== ($entry = $dir->read())) { 157 if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') { 158 continue; 159 } 160 if (!is_dir(DOKU_PLUGIN ."/$entry")) { 161 continue; 162 } 163 $plugins[] = $entry; 164 } 165 sort($plugins); 166 return $plugins; 167 } 168 169 170 /** 171 * Actiate a specific plugin in a specific animal 172 * 173 * @param string $plugin Name of the plugin to be activated 174 * @param string $animal Directory of the animal within DOKU_FARMDIR 175 */ 176 public function activatePlugin($plugin, $animal) { 177 if (isset($this->allPlugins[$animal])) { 178 $plugins = $this->allPlugins[$animal]; 179 } else { 180 include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 181 } 182 if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) { 183 unset($plugins[$plugin]); 184 $this->writePluginConf($plugins, $animal); 185 } 186 $this->allPlugins[$animal] = $plugins; 187 } 188 189 /** 190 * @param string $plugin Name of the plugin to be deactivated 191 * @param string $animal Directory of the animal within DOKU_FARMDIR 192 */ 193 public function deactivatePlugin($plugin, $animal) { 194 if (isset($this->allPlugins[$animal])) { 195 $plugins = $this->allPlugins[$animal]; 196 } else { 197 include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 198 } 199 if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) { 200 $plugins[$plugin] = 0; 201 $this->writePluginConf($plugins, $animal); 202 } 203 $this->allPlugins[$animal] = $plugins; 204 } 205 206 /** 207 * Write the list of (deactivated) plugins as plugin configuration of an animal to file 208 * 209 * @param array $plugins associative array with the key being the plugin name and the value 0 or 1 210 * @param string $animal Directory of the animal within DOKU_FARMDIR 211 */ 212 public function writePluginConf($plugins, $animal) { 213 $pluginConf = '<?php' . "\n"; 214 foreach ($plugins as $plugin => $status) { 215 $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n"; 216 } 217 io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf); 218 touch(DOKU_FARMDIR . $animal . '/conf/local.php'); 219 } 220 221 /** 222 * Show a message for all errors which occured during form validation 223 * 224 * @param \dokuwiki\Form\Form $form The form to which the errors should be added. 225 * @param array $errorArray An associative array with the key being the name of the element at fault 226 * and the value being the associated error message. 227 */ 228 public function addErrorsToForm(\dokuwiki\Form\Form &$form, $errorArray) { 229 foreach ($errorArray as $elementName => $errorMessage) { 230 $offset = 0; 231 msg($errorMessage, -1); 232 while ($form->findPositionByAttribute('name',$elementName, $offset)) { 233 $offset = $form->findPositionByAttribute('name',$elementName, $offset); 234 $form->getElementAt($offset)->addClass('error'); 235 ++$offset; 236 } 237 } 238 } 239 240 /** 241 * @param string|null $page load adminpage $page, reload the current page if $page is ommited or null 242 */ 243 public function reloadAdminPage($page = null) { 244 global $ID; 245 $get = $_GET; 246 if(isset($get['id'])) unset($get['id']); 247 if ($page !== null ) { 248 $get['page'] = $page; 249 } 250 $self = wl($ID, $get, false, '&'); 251 send_redirect($self); 252 } 253 254 /** 255 * Download and extract the animal template 256 * 257 * @param string $animalpath 258 * 259 * @throws \splitbrain\PHPArchive\ArchiveIOException 260 */ 261 public function downloadTemplate($animalpath) { 262 file_put_contents($animalpath . '/_animal.zip',fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip','r')); 263 $zip = new splitbrain\PHPArchive\Zip(); 264 $zip->open($animalpath.'/_animal.zip'); 265 $zip->extract($animalpath); 266 $zip->close(); 267 unlink($animalpath.'/_animal.zip'); 268 } 269 270 271 /** 272 * The subdomain must contain at least two dots 273 * 274 * @link http://stackoverflow.com/questions/17986371/regular-expression-to-validate-fqdn-in-c-sharp-and-javascript 275 * 276 * @param string $subdomain 277 * 278 * @return bool 279 */ 280 public function validateSubdomain ($subdomain) { 281 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; 282 } 283 284 /** 285 * @param string $animalname 286 * 287 * @return bool 288 */ 289 public function validateAnimalName ($animalname) { 290 return preg_match("/^[a-z0-9]+(-[a-z0-9]+)*$/i",$animalname) === 1; 291 } 292 293 /** 294 * @return string 295 */ 296 public function getUserLine($currentAdmin) { 297 $masterUsers = file_get_contents(DOKU_CONF . 'users.auth.php'); 298 $masterUsers = ltrim(strstr($masterUsers, "\n" . $currentAdmin . ":")); 299 $newAdmin = substr($masterUsers, 0, strpos($masterUsers, "\n") + 1); 300 return $newAdmin; 301 } 302 303} 304