1632c5618SAndreas Gohr<?php 2632c5618SAndreas Gohr/** 3632c5618SAndreas Gohr * DokuWiki Plugin farmer (Admin Component) 4632c5618SAndreas Gohr * 5632c5618SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6632c5618SAndreas Gohr * @author Michael Große <grosse@cosmocode.de> 70a5d2da2SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 8632c5618SAndreas Gohr */ 9632c5618SAndreas Gohr 10632c5618SAndreas Gohr// must be run within Dokuwiki 11632c5618SAndreas Gohrif(!defined('DOKU_INC')) die(); 12632c5618SAndreas Gohr 130a5d2da2SAndreas Gohr/** 140a5d2da2SAndreas Gohr * Setup the farm by creating preload.php etc 150a5d2da2SAndreas Gohr */ 16632c5618SAndreas Gohrclass admin_plugin_farmer_setup extends DokuWiki_Admin_Plugin { 17632c5618SAndreas Gohr 18632c5618SAndreas Gohr /** @var helper_plugin_farmer $helper */ 19632c5618SAndreas Gohr private $helper; 20632c5618SAndreas Gohr 21632c5618SAndreas Gohr /** 22632c5618SAndreas Gohr * @return bool admin only! 23632c5618SAndreas Gohr */ 24632c5618SAndreas Gohr public function forAdminOnly() { 25632c5618SAndreas Gohr return true; 26632c5618SAndreas Gohr } 27632c5618SAndreas Gohr 28632c5618SAndreas Gohr /** 29632c5618SAndreas Gohr * Should carry out any processing required by the plugin. 30632c5618SAndreas Gohr */ 31632c5618SAndreas Gohr public function handle() { 32632c5618SAndreas Gohr global $INPUT; 33632c5618SAndreas Gohr global $ID; 34632c5618SAndreas Gohr 35632c5618SAndreas Gohr if(!$INPUT->bool('farmdir')) return; 36632c5618SAndreas Gohr if(!checkSecurityToken()) return; 37632c5618SAndreas Gohr 38632c5618SAndreas Gohr $this->helper = plugin_load('helper', 'farmer'); 39632c5618SAndreas Gohr 40632c5618SAndreas Gohr $farmdir = trim($INPUT->str('farmdir', '')); 41632c5618SAndreas Gohr if($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir; 42632c5618SAndreas Gohr $farmdir = fullpath($farmdir); 43632c5618SAndreas Gohr 44632c5618SAndreas Gohr $errors = array(); 45632c5618SAndreas Gohr if($farmdir === '') { 46632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_missing'); 47632c5618SAndreas Gohr } elseif($this->helper->isInPath($farmdir, DOKU_INC) !== false) { 48632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_in_dokuwiki'); 49632c5618SAndreas Gohr } elseif(!io_mkdir_p($farmdir)) { 50632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_uncreatable'); 51632c5618SAndreas Gohr } elseif(!is_writeable($farmdir)) { 52632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_unwritable'); 53632c5618SAndreas Gohr } elseif(count(scandir($farmdir)) > 2) { 54632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_notEmpty'); 55632c5618SAndreas Gohr } 56632c5618SAndreas Gohr 57632c5618SAndreas Gohr if($errors) { 58632c5618SAndreas Gohr foreach($errors as $error) { 59632c5618SAndreas Gohr msg($error, -1); 60632c5618SAndreas Gohr } 61632c5618SAndreas Gohr return; 62632c5618SAndreas Gohr } 63632c5618SAndreas Gohr 64a646d519SAndreas Gohr // create the files 65c4c8e953SAndreas Gohr $ok = $this->createPreloadPHP(); 66c4c8e953SAndreas Gohr if($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess(); 67*23164e01SAndreas Gohr if($ok) $ok &= $this->createFarmIni($farmdir); 68a646d519SAndreas Gohr 69a646d519SAndreas Gohr if($ok) { 70632c5618SAndreas Gohr msg($this->getLang('preload creation success'), 1); 71c4c8e953SAndreas Gohr $link = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&'); 72632c5618SAndreas Gohr send_redirect($link); 73632c5618SAndreas Gohr } else { 74632c5618SAndreas Gohr msg($this->getLang('preload creation error'), -1); 75632c5618SAndreas Gohr } 76632c5618SAndreas Gohr } 77632c5618SAndreas Gohr 78632c5618SAndreas Gohr /** 79632c5618SAndreas Gohr * Render HTML output, e.g. helpful text and a form 80632c5618SAndreas Gohr */ 81632c5618SAndreas Gohr public function html() { 82632c5618SAndreas Gohr // Is preload.php already enabled? 83632c5618SAndreas Gohr if(file_exists(DOKU_INC . 'inc/preload.php')) { 84632c5618SAndreas Gohr msg($this->getLang('overwrite_preload'), -1); 85632c5618SAndreas Gohr } 86632c5618SAndreas Gohr 87632c5618SAndreas Gohr $form = new \dokuwiki\Form\Form(); 88632c5618SAndreas Gohr $form->addClass('plugin_farmer'); 89632c5618SAndreas Gohr $form->addFieldsetOpen($this->getLang('preloadPHPForm')); 90c4c8e953SAndreas Gohr $form->addTextInput('farmdir', $this->getLang('farm dir')); 91c4c8e953SAndreas Gohr $form->addCheckbox('htaccess', $this->getLang('htaccess setup'))->attr('checked', 'checked'); 92632c5618SAndreas Gohr $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit'); 93632c5618SAndreas Gohr $form->addFieldsetClose(); 94632c5618SAndreas Gohr echo $form->toHTML(); 95632c5618SAndreas Gohr 96632c5618SAndreas Gohr } 97632c5618SAndreas Gohr 98632c5618SAndreas Gohr /** 99a646d519SAndreas Gohr * Creates the preload that loads our farm controller 100a646d519SAndreas Gohr * @return bool true if saving was successful 101632c5618SAndreas Gohr */ 102a646d519SAndreas Gohr protected function createPreloadPHP() { 103632c5618SAndreas Gohr $content = "<?php\n"; 104632c5618SAndreas Gohr $content .= "# farm setup by farmer plugin\n"; 105c4c8e953SAndreas Gohr $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n"; 106c4c8e953SAndreas Gohr $content .= " include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n"; 107c4c8e953SAndreas Gohr $content .= "}\n"; 108632c5618SAndreas Gohr return io_saveFile(DOKU_INC . 'inc/preload.php', $content); 109632c5618SAndreas Gohr } 110632c5618SAndreas Gohr 111632c5618SAndreas Gohr /** 112632c5618SAndreas Gohr * Appends the needed config to the main .htaccess for htaccess type setups 113632c5618SAndreas Gohr * 114632c5618SAndreas Gohr * @return bool true if saving was successful 115632c5618SAndreas Gohr */ 116632c5618SAndreas Gohr protected function createHtaccess() { 117a646d519SAndreas Gohr $content = "\n\n# Options added for farm setup by farmer plugin:\n"; 118632c5618SAndreas Gohr $content .= "RewriteEngine On\n"; 119632c5618SAndreas Gohr $content .= 'RewriteRule ^/?!([^/]+)/(.*) ' . DOKU_REL . '$2?animal=$1 [QSA]' . "\n"; 120632c5618SAndreas Gohr $content .= 'RewriteRule ^/?!([^/]+)$ ' . DOKU_REL . '?animal=$1 [QSA]' . "\n"; 121632c5618SAndreas Gohr $content .= 'Options +FollowSymLinks' . "\n"; 122632c5618SAndreas Gohr return io_saveFile(DOKU_INC . '.htaccess', $content, true); 123632c5618SAndreas Gohr } 124632c5618SAndreas Gohr 125a646d519SAndreas Gohr /** 126a646d519SAndreas Gohr * Creates the initial configuration 127a646d519SAndreas Gohr * 128a646d519SAndreas Gohr * @param $animalpath 129a646d519SAndreas Gohr * @return bool true if saving was successful 130a646d519SAndreas Gohr */ 131a646d519SAndreas Gohr protected function createFarmIni($animalpath) { 132a646d519SAndreas Gohr $content = "; farm config created by the farmer plugin\n\n"; 133a646d519SAndreas Gohr $content .= "[base]\n"; 134a646d519SAndreas Gohr $content .= "farmdir = $animalpath\n"; 135a646d519SAndreas Gohr $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n"; 136a646d519SAndreas Gohr return io_saveFile(DOKU_INC . 'conf/farm.ini', $content); 137a646d519SAndreas Gohr } 138632c5618SAndreas Gohr} 139632c5618SAndreas Gohr 140632c5618SAndreas Gohr// vim:ts=4:sw=4:et: 141