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') { 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 for ($position = 0; $position < $form->elementCount(); ++$position) { 141 if ($form->getElementAt($position) instanceof dokuwiki\Form\TagCloseElement) { 142 continue; 143 } 144 if ($form->getElementAt($position)->attr('name') == '') continue; 145 $elementName = $form->getElementAt($position)->attr('name'); 146 if (!isset($errorArray[$elementName])) continue; 147 $form->getElementAt($position)->addClass('error'); 148 $form->addTagOpen('div',$position+1)->addClass('error'); 149 $form->addHTML($errorArray[$elementName],$position+2); 150 $form->addTagClose('div',$position+3); 151 } 152 } 153 154 public function reloadAdminPage($page = null) { 155 global $ID; 156 $get = $_GET; 157 if(isset($get['id'])) unset($get['id']); 158 if ($page !== null ) { 159 $get['page'] = $page; 160 } 161 $self = wl($ID, $get, false, '&'); 162 send_redirect($self); 163 } 164 165 public function downloadTemplate($animalpath) { 166 file_put_contents($animalpath . '/_animal.zip',fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip','r')); 167 $zip = new splitbrain\PHPArchive\Zip(); 168 $zip->open($animalpath.'/_animal.zip'); 169 $zip->extract($animalpath); 170 $zip->close(); 171 unlink($animalpath.'/_animal.zip'); 172 } 173 174 /** 175 * recursive function to test wether a (non-existing) path points into an existint path 176 * 177 * @param $path string 178 * 179 * @param $container string has to exist 180 * 181 * @throws BadMethodCallException 182 * 183 * @return bool 184 */ 185 public function isInPath ($path, $container) { 186 if (!file_exists($container)) { 187 throw new BadMethodCallException('The Container has to exist and be accessable by realpath().'); 188 } 189 if (realpath($path) === false) { 190 return $this->isInPath(dirname($path), $container); 191 } 192 if (strpos(realpath($path), realpath($container)) !== false) { 193 return true; 194 } else { 195 return false; 196 } 197 } 198 199} 200