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> 7632c5618SAndreas Gohr */ 8632c5618SAndreas Gohr 9632c5618SAndreas Gohr// must be run within Dokuwiki 10632c5618SAndreas Gohrif(!defined('DOKU_INC')) die(); 11632c5618SAndreas Gohr 12632c5618SAndreas Gohrclass admin_plugin_farmer_setup extends DokuWiki_Admin_Plugin { 13632c5618SAndreas Gohr 14632c5618SAndreas Gohr /** @var helper_plugin_farmer $helper */ 15632c5618SAndreas Gohr private $helper; 16632c5618SAndreas Gohr 17632c5618SAndreas Gohr /** 18632c5618SAndreas Gohr * @return bool admin only! 19632c5618SAndreas Gohr */ 20632c5618SAndreas Gohr public function forAdminOnly() { 21632c5618SAndreas Gohr return true; 22632c5618SAndreas Gohr } 23632c5618SAndreas Gohr 24632c5618SAndreas Gohr /** 25632c5618SAndreas Gohr * Should carry out any processing required by the plugin. 26632c5618SAndreas Gohr */ 27632c5618SAndreas Gohr public function handle() { 28632c5618SAndreas Gohr global $INPUT; 29632c5618SAndreas Gohr global $ID; 30632c5618SAndreas Gohr 31632c5618SAndreas Gohr if(!$INPUT->bool('farmdir')) return; 32632c5618SAndreas Gohr if(!checkSecurityToken()) return; 33632c5618SAndreas Gohr 34632c5618SAndreas Gohr $this->helper = plugin_load('helper', 'farmer'); 35632c5618SAndreas Gohr 36632c5618SAndreas Gohr $farmdir = trim($INPUT->str('farmdir', '')); 37632c5618SAndreas Gohr if($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir; 38632c5618SAndreas Gohr $farmdir = fullpath($farmdir); 39632c5618SAndreas Gohr 40632c5618SAndreas Gohr $errors = array(); 41632c5618SAndreas Gohr if($farmdir === '') { 42632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_missing'); 43632c5618SAndreas Gohr } elseif($this->helper->isInPath($farmdir, DOKU_INC) !== false) { 44632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_in_dokuwiki'); 45632c5618SAndreas Gohr } elseif(!io_mkdir_p($farmdir)) { 46632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_uncreatable'); 47632c5618SAndreas Gohr } elseif(!is_writeable($farmdir)) { 48632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_unwritable'); 49632c5618SAndreas Gohr } elseif(count(scandir($farmdir)) > 2) { 50632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_notEmpty'); 51632c5618SAndreas Gohr } 52632c5618SAndreas Gohr 53632c5618SAndreas Gohr if($INPUT->str('serversetup', '', true) === '') { 54632c5618SAndreas Gohr $errors[] = $this->getLang('serversetup_missing'); 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 64*a646d519SAndreas Gohr // create the files 65*a646d519SAndreas Gohr $ok = $this->createPreloadPHP($farmdir); 66*a646d519SAndreas Gohr if($ok && $INPUT->str('serversetup') == 'htaccess') $ok &= $this->createHtaccess(); 67*a646d519SAndreas Gohr $ok &= $this->createFarmIni($farmdir); 68*a646d519SAndreas Gohr 69*a646d519SAndreas Gohr if($ok) { 70632c5618SAndreas Gohr msg($this->getLang('preload creation success'), 1); 71632c5618SAndreas Gohr $link = wl($ID, array('do' => 'admin', 'page' => 'farmer'), 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')); 90632c5618SAndreas Gohr $form->addTextInput('farmdir', $this->getLang('farm dir'))->addClass('block edit'); 91632c5618SAndreas Gohr 92632c5618SAndreas Gohr $form->addRadioButton('serversetup', $this->getLang('subdomain setup'))->val('subdomain')->attr('type', 'radio')->addClass('block edit')->id('subdomain__setup'); 93632c5618SAndreas Gohr $form->addRadioButton('serversetup', $this->getLang('htaccess setup'))->val('htaccess')->attr('type', 'radio')->addClass('block edit')->attr('checked', true)->id('htaccess__setup'); 94632c5618SAndreas Gohr 95632c5618SAndreas Gohr $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit'); 96632c5618SAndreas Gohr $form->addFieldsetClose(); 97632c5618SAndreas Gohr echo $form->toHTML(); 98632c5618SAndreas Gohr 99632c5618SAndreas Gohr echo sprintf($this->locale_xhtml('preload'), dirname(DOKU_REL) . '/farm/'); 100632c5618SAndreas Gohr 101632c5618SAndreas Gohr } 102632c5618SAndreas Gohr 103632c5618SAndreas Gohr /** 104*a646d519SAndreas Gohr * Creates the preload that loads our farm controller 105*a646d519SAndreas Gohr * @return bool true if saving was successful 106632c5618SAndreas Gohr */ 107*a646d519SAndreas Gohr protected function createPreloadPHP() { 108632c5618SAndreas Gohr $content = "<?php\n"; 109632c5618SAndreas Gohr $content .= "# farm setup by farmer plugin\n"; 110*a646d519SAndreas Gohr $content .= "include(fullpath(dirname(__FILE__)).'/../lib/plugins/farmer/DokuWikiFarmCore.php');\n"; 111632c5618SAndreas Gohr return io_saveFile(DOKU_INC . 'inc/preload.php', $content); 112632c5618SAndreas Gohr } 113632c5618SAndreas Gohr 114632c5618SAndreas Gohr /** 115632c5618SAndreas Gohr * Appends the needed config to the main .htaccess for htaccess type setups 116632c5618SAndreas Gohr * 117632c5618SAndreas Gohr * @return bool true if saving was successful 118632c5618SAndreas Gohr */ 119632c5618SAndreas Gohr protected function createHtaccess() { 120*a646d519SAndreas Gohr $content = "\n\n# Options added for farm setup by farmer plugin:\n"; 121632c5618SAndreas Gohr $content .= "RewriteEngine On\n"; 122632c5618SAndreas Gohr $content .= 'RewriteRule ^/?!([^/]+)/(.*) ' . DOKU_REL . '$2?animal=$1 [QSA]' . "\n"; 123632c5618SAndreas Gohr $content .= 'RewriteRule ^/?!([^/]+)$ ' . DOKU_REL . '?animal=$1 [QSA]' . "\n"; 124632c5618SAndreas Gohr $content .= 'Options +FollowSymLinks'."\n"; 125632c5618SAndreas Gohr return io_saveFile(DOKU_INC . '.htaccess', $content, true); 126632c5618SAndreas Gohr } 127632c5618SAndreas Gohr 128*a646d519SAndreas Gohr /** 129*a646d519SAndreas Gohr * Creates the initial configuration 130*a646d519SAndreas Gohr * 131*a646d519SAndreas Gohr * @param $animalpath 132*a646d519SAndreas Gohr * @return bool true if saving was successful 133*a646d519SAndreas Gohr */ 134*a646d519SAndreas Gohr protected function createFarmIni($animalpath) { 135*a646d519SAndreas Gohr $content = "; farm config created by the farmer plugin\n\n"; 136*a646d519SAndreas Gohr $content .= "[base]\n"; 137*a646d519SAndreas Gohr $content .= "farmdir = $animalpath\n"; 138*a646d519SAndreas Gohr $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n"; 139*a646d519SAndreas Gohr return io_saveFile(DOKU_INC . 'conf/farm.ini', $content); 140*a646d519SAndreas Gohr } 141632c5618SAndreas Gohr} 142632c5618SAndreas Gohr 143632c5618SAndreas Gohr// vim:ts=4:sw=4:et: 144