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 * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki. 18 * 19 * @todo: needs tests 20 * 21 * @author Aidan Lister <aidan@php.net> 22 * @author Michael Große <grosse@cosmocode.de> 23 * @version 1.0.1 24 * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ 25 * 26 * @param string $source Source path 27 * @param string $destination Destination path 28 * 29 * @return bool Returns TRUE on success, FALSE on failure 30 */ 31 function io_copyDir($source, $destination) { 32 if (is_link($source)) { 33 io_lock($destination); 34 $result=symlink(readlink($source), $destination); 35 io_unlock($destination); 36 return $result; 37 } 38 39 if (is_file($source)) { 40 io_lock($destination); 41 $result=copy($source, $destination); 42 io_unlock($destination); 43 return $result; 44 } 45 46 if (!is_dir($destination)) { 47 io_mkdir_p($destination); 48 } 49 50 $dir = dir($source); 51 while (false !== ($entry = $dir->read())) { 52 if ($entry == '.' || $entry == '..') { 53 continue; 54 } 55 56 // recurse into directories 57 $this->io_copyDir("$source/$entry", "$destination/$entry"); 58 } 59 60 $dir->close(); 61 return true; 62 } 63 64 65 66 public function getAllPlugins() { 67 $dir = dir(DOKU_PLUGIN); 68 $plugins = array(); 69 while (false !== ($entry = $dir->read())) { 70 if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') { 71 continue; 72 } 73 if (!is_dir(DOKU_PLUGIN ."/$entry")) { 74 continue; 75 } 76 $plugins[] = $entry; 77 } 78 sort($plugins); 79 return $plugins; 80 } 81 82 public function getAllAnimals() { 83 $animals = array(); 84 85 $dir = dir(DOKU_FARMDIR); 86 while (false !== ($entry = $dir->read())) { 87 if ($entry == '.' || $entry == '..' || $entry == '_animal' || $entry == '.htaccess') { 88 continue; 89 } 90 if (!is_dir(DOKU_FARMDIR . $entry)) { 91 continue; 92 } 93 $animals[] = $entry; 94 } 95 $dir->close(); 96 return $animals; 97 } 98 99 public function activatePlugin($plugin, $animal) { 100 if (isset($this->allPlugins[$animal])) { 101 $plugins = $this->allPlugins[$animal]; 102 } else { 103 include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 104 } 105 if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) { 106 unset($plugins[$plugin]); 107 $this->writePluginConf($plugins, $animal); 108 } 109 $this->allPlugins[$animal] = $plugins; 110 } 111 112 /** 113 * @param $plugin {string} Name of the plugin to deactivate 114 * @param $animal {string} directory of the animal within DOKU_FARMDIR 115 */ 116 public function deactivatePlugin($plugin, $animal) { 117 if (isset($this->allPlugins[$animal])) { 118 $plugins = $this->allPlugins[$animal]; 119 } else { 120 include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 121 } 122 if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) { 123 $plugins[$plugin] = 0; 124 $this->writePluginConf($plugins, $animal); 125 } 126 $this->allPlugins[$animal] = $plugins; 127 } 128 129 public function writePluginConf($plugins, $animal) { 130 dbglog($plugins); 131 $pluginConf = '<?php' . "\n"; 132 foreach ($plugins as $plugin => $status) { 133 $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n"; 134 } 135 io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf); 136 touch(DOKU_FARMDIR . $animal . '/conf/local.php'); 137 } 138 139 public function addErrorsToForm(\dokuwiki\Form\Form &$form, $errorArray) { 140 foreach ($errorArray as $elementName => $errorMessage) { 141 $offset = 0; 142 msg($errorMessage, -1); 143 while ($form->findPositionByAttribute('name',$elementName, $offset)) { 144 $offset = $form->findPositionByAttribute('name',$elementName, $offset); 145 $form->getElementAt($offset)->addClass('error'); 146 ++$offset; 147 } 148 } 149 } 150 151 public function reloadAdminPage($page = null) { 152 global $ID; 153 $get = $_GET; 154 if(isset($get['id'])) unset($get['id']); 155 if ($page !== null ) { 156 $get['page'] = $page; 157 } 158 $self = wl($ID, $get, false, '&'); 159 send_redirect($self); 160 } 161 162 public function downloadTemplate($animalpath) { 163 file_put_contents($animalpath . '/_animal.zip',fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip','r')); 164 $zip = new splitbrain\PHPArchive\Zip(); 165 $zip->open($animalpath.'/_animal.zip'); 166 $zip->extract($animalpath); 167 $zip->close(); 168 unlink($animalpath.'/_animal.zip'); 169 } 170 171 /** 172 * recursive function to test wether a (non-existing) path points into an existint path 173 * 174 * @param $path string 175 * 176 * @param $container string has to exist 177 * 178 * @throws BadMethodCallException 179 * 180 * @return bool 181 */ 182 public function isInPath ($path, $container) { 183 if (!file_exists($container)) { 184 throw new BadMethodCallException('The Container has to exist and be accessable by realpath().'); 185 } 186 if (realpath($path) === false) { 187 return $this->isInPath(dirname($path), $container); 188 } 189 if (strpos(realpath($path), realpath($container)) !== false) { 190 return true; 191 } else { 192 return false; 193 } 194 } 195 196 /** 197 * @return bool 198 */ 199 public function checkFarmSetup () { 200 if(defined('DOKU_FARMDIR') && defined('DOKU_FARMTYPE')) { 201 if (DOKU_FARMTYPE == 'subdomain') { 202 return true; 203 } elseif (DOKU_FARMTYPE == 'htaccess') { 204 return defined('DOKU_FARMRELDIR'); 205 } else { 206 return false; 207 } 208 } 209 return false; 210 } 211 212} 213