1bc461538SMichael Große<?php 2bc461538SMichael Große/** 3bc461538SMichael Große * DokuWiki Plugin farmer (Helper Component) 4bc461538SMichael Große * 5bc461538SMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6bc461538SMichael Große * @author Michael Große <grosse@cosmocode.de> 7bc461538SMichael Große */ 8bc461538SMichael Große 9bc461538SMichael Große// must be run within Dokuwiki 10bc461538SMichael Großeif(!defined('DOKU_INC')) die(); 11bc461538SMichael Große 12bc461538SMichael Großeclass helper_plugin_farmer extends DokuWiki_Plugin { 13bc461538SMichael Große 14fcbe16a4SMichael Große private $allPlugins = array(); 15fcbe16a4SMichael Große 16bc461538SMichael Große /** 17bc461538SMichael Große * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki. 18bc461538SMichael Große * 19bc461538SMichael Große * @todo: needs tests 20bc461538SMichael Große * 21bc461538SMichael Große * @author Aidan Lister <aidan@php.net> 22bc461538SMichael Große * @author Michael Große <grosse@cosmocode.de> 23bc461538SMichael Große * @version 1.0.1 24bc461538SMichael Große * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ 25bc461538SMichael Große * 26bc461538SMichael Große * @param string $source Source path 27bc461538SMichael Große * @param string $destination Destination path 28bc461538SMichael Große * 29bc461538SMichael Große * @return bool Returns TRUE on success, FALSE on failure 30bc461538SMichael Große */ 31bc461538SMichael Große function io_copyDir($source, $destination) { 32bc461538SMichael Große if (is_link($source)) { 33bc461538SMichael Große io_lock($destination); 34bc461538SMichael Große $result=symlink(readlink($source), $destination); 35bc461538SMichael Große io_unlock($destination); 36bc461538SMichael Große return $result; 37bc461538SMichael Große } 38bc461538SMichael Große 39bc461538SMichael Große if (is_file($source)) { 40bc461538SMichael Große io_lock($destination); 41bc461538SMichael Große $result=copy($source, $destination); 42bc461538SMichael Große io_unlock($destination); 43bc461538SMichael Große return $result; 44bc461538SMichael Große } 45bc461538SMichael Große 46bc461538SMichael Große if (!is_dir($destination)) { 47bc461538SMichael Große io_mkdir_p($destination); 48bc461538SMichael Große } 49bc461538SMichael Große 50bc461538SMichael Große $dir = dir($source); 51bc461538SMichael Große while (false !== ($entry = $dir->read())) { 52bc461538SMichael Große if ($entry == '.' || $entry == '..') { 53bc461538SMichael Große continue; 54bc461538SMichael Große } 55bc461538SMichael Große 56bc461538SMichael Große // recurse into directories 57bc461538SMichael Große $this->io_copyDir("$source/$entry", "$destination/$entry"); 58bc461538SMichael Große } 59bc461538SMichael Große 60bc461538SMichael Große $dir->close(); 61bc461538SMichael Große return true; 62bc461538SMichael Große } 63bc461538SMichael Große 640b96e6d7SMichael Große 650b96e6d7SMichael Große 660b96e6d7SMichael Große public function getAllPlugins() { 670b96e6d7SMichael Große $dir = dir(DOKU_PLUGIN); 680b96e6d7SMichael Große $plugins = array(); 690b96e6d7SMichael Große while (false !== ($entry = $dir->read())) { 703949d8a1SMichael Große if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') { 710b96e6d7SMichael Große continue; 720b96e6d7SMichael Große } 730b96e6d7SMichael Große if (!is_dir(DOKU_PLUGIN ."/$entry")) { 740b96e6d7SMichael Große continue; 750b96e6d7SMichael Große } 760b96e6d7SMichael Große $plugins[] = $entry; 770b96e6d7SMichael Große } 786ec1ad8fSMichael Große sort($plugins); 790b96e6d7SMichael Große return $plugins; 800b96e6d7SMichael Große } 810b96e6d7SMichael Große 820b96e6d7SMichael Große public function getAllAnimals() { 830b96e6d7SMichael Große $animals = array(); 840b96e6d7SMichael Große 850b96e6d7SMichael Große $dir = dir(DOKU_FARMDIR); 860b96e6d7SMichael Große while (false !== ($entry = $dir->read())) { 87a0fc814bSMichael Große if ($entry == '.' || $entry == '..' || $entry == '_animal' || $entry == '.htaccess') { 88a0fc814bSMichael Große continue; 89a0fc814bSMichael Große } 90a0fc814bSMichael Große if (!is_dir(DOKU_FARMDIR . $entry)) { 910b96e6d7SMichael Große continue; 920b96e6d7SMichael Große } 930b96e6d7SMichael Große $animals[] = $entry; 940b96e6d7SMichael Große } 950b96e6d7SMichael Große $dir->close(); 960b96e6d7SMichael Große return $animals; 970b96e6d7SMichael Große } 980b96e6d7SMichael Große 99fcbe16a4SMichael Große public function activatePlugin($plugin, $animal) { 100fcbe16a4SMichael Große if (isset($this->allPlugins[$animal])) { 101fcbe16a4SMichael Große $plugins = $this->allPlugins[$animal]; 102fcbe16a4SMichael Große } else { 103fcbe16a4SMichael Große include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 104fcbe16a4SMichael Große } 105fcbe16a4SMichael Große if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) { 106fcbe16a4SMichael Große unset($plugins[$plugin]); 107fcbe16a4SMichael Große $this->writePluginConf($plugins, $animal); 108fcbe16a4SMichael Große } 109fcbe16a4SMichael Große $this->allPlugins[$animal] = $plugins; 110fcbe16a4SMichael Große } 111fcbe16a4SMichael Große 112a0fc814bSMichael Große /** 113a0fc814bSMichael Große * @param $plugin {string} Name of the plugin to deactivate 114a0fc814bSMichael Große * @param $animal {string} directory of the animal within DOKU_FARMDIR 115a0fc814bSMichael Große */ 116fcbe16a4SMichael Große public function deactivatePlugin($plugin, $animal) { 117fcbe16a4SMichael Große if (isset($this->allPlugins[$animal])) { 118fcbe16a4SMichael Große $plugins = $this->allPlugins[$animal]; 119fcbe16a4SMichael Große } else { 120fcbe16a4SMichael Große include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php'); 121fcbe16a4SMichael Große } 122fcbe16a4SMichael Große if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) { 123fcbe16a4SMichael Große $plugins[$plugin] = 0; 124fcbe16a4SMichael Große $this->writePluginConf($plugins, $animal); 125fcbe16a4SMichael Große } 126fcbe16a4SMichael Große $this->allPlugins[$animal] = $plugins; 127fcbe16a4SMichael Große } 128fcbe16a4SMichael Große 129fcbe16a4SMichael Große public function writePluginConf($plugins, $animal) { 130fcbe16a4SMichael Große dbglog($plugins); 131fcbe16a4SMichael Große $pluginConf = '<?php' . "\n"; 132fcbe16a4SMichael Große foreach ($plugins as $plugin => $status) { 133fcbe16a4SMichael Große $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n"; 134fcbe16a4SMichael Große } 135fcbe16a4SMichael Große io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf); 136fcbe16a4SMichael Große touch(DOKU_FARMDIR . $animal . '/conf/local.php'); 137fcbe16a4SMichael Große } 138fcbe16a4SMichael Große 139a12b96c0SMichael Große public function addErrorsToForm(\dokuwiki\Form\Form &$form, $errorArray) { 140*628ea26fSMichael Große foreach ($errorArray as $elementName => $errorMessage) { 141*628ea26fSMichael Große $offset = 0; 142*628ea26fSMichael Große msg($errorMessage, -1); 143*628ea26fSMichael Große while ($form->findPositionByAttribute('name',$elementName, $offset)) { 144*628ea26fSMichael Große $offset = $form->findPositionByAttribute('name',$elementName, $offset); 145*628ea26fSMichael Große $form->getElementAt($offset)->addClass('error'); 146*628ea26fSMichael Große ++$offset; 147a12b96c0SMichael Große } 148a12b96c0SMichael Große } 149a12b96c0SMichael Große } 150a12b96c0SMichael Große 1514d120480SMichael Große public function reloadAdminPage($page = null) { 1524d120480SMichael Große global $ID; 1534d120480SMichael Große $get = $_GET; 1544d120480SMichael Große if(isset($get['id'])) unset($get['id']); 1554d120480SMichael Große if ($page !== null ) { 1564d120480SMichael Große $get['page'] = $page; 1574d120480SMichael Große } 1584d120480SMichael Große $self = wl($ID, $get, false, '&'); 1594d120480SMichael Große send_redirect($self); 1604d120480SMichael Große } 1614d120480SMichael Große 16279435a6fSMichael Große public function downloadTemplate($animalpath) { 16379435a6fSMichael Große file_put_contents($animalpath . '/_animal.zip',fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip','r')); 164c9fd7b89SMichael Große $zip = new splitbrain\PHPArchive\Zip(); 16579435a6fSMichael Große $zip->open($animalpath.'/_animal.zip'); 166c9fd7b89SMichael Große $zip->extract($animalpath); 16779435a6fSMichael Große $zip->close(); 16879435a6fSMichael Große unlink($animalpath.'/_animal.zip'); 16979435a6fSMichael Große } 17079435a6fSMichael Große 171efa7af45SMichael Große /** 172efa7af45SMichael Große * recursive function to test wether a (non-existing) path points into an existint path 173efa7af45SMichael Große * 174efa7af45SMichael Große * @param $path string 175efa7af45SMichael Große * 176efa7af45SMichael Große * @param $container string has to exist 177efa7af45SMichael Große * 178efa7af45SMichael Große * @throws BadMethodCallException 179efa7af45SMichael Große * 180efa7af45SMichael Große * @return bool 181efa7af45SMichael Große */ 182efa7af45SMichael Große public function isInPath ($path, $container) { 183efa7af45SMichael Große if (!file_exists($container)) { 184efa7af45SMichael Große throw new BadMethodCallException('The Container has to exist and be accessable by realpath().'); 185efa7af45SMichael Große } 186efa7af45SMichael Große if (realpath($path) === false) { 187efa7af45SMichael Große return $this->isInPath(dirname($path), $container); 188efa7af45SMichael Große } 189efa7af45SMichael Große if (strpos(realpath($path), realpath($container)) !== false) { 190efa7af45SMichael Große return true; 191efa7af45SMichael Große } else { 192efa7af45SMichael Große return false; 193efa7af45SMichael Große } 194efa7af45SMichael Große } 195efa7af45SMichael Große 196d9ec4524SMichael Große /** 197d9ec4524SMichael Große * @return bool 198d9ec4524SMichael Große */ 199d9ec4524SMichael Große public function checkFarmSetup () { 200d9ec4524SMichael Große if(defined('DOKU_FARMDIR') && defined('DOKU_FARMTYPE')) { 201d9ec4524SMichael Große if (DOKU_FARMTYPE == 'subdomain') { 202d9ec4524SMichael Große return true; 203d9ec4524SMichael Große } elseif (DOKU_FARMTYPE == 'htaccess') { 204d9ec4524SMichael Große return defined('DOKU_FARMRELDIR'); 205d9ec4524SMichael Große } else { 206d9ec4524SMichael Große return false; 207d9ec4524SMichael Große } 208d9ec4524SMichael Große } 209d9ec4524SMichael Große return false; 210d9ec4524SMichael Große } 211d9ec4524SMichael Große 212bc461538SMichael Große} 213