1*632c5618SAndreas Gohr<?php 2*632c5618SAndreas Gohr/** 3*632c5618SAndreas Gohr * DokuWiki Plugin farmer (Admin Component) 4*632c5618SAndreas Gohr * 5*632c5618SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6*632c5618SAndreas Gohr * @author Michael Große <grosse@cosmocode.de> 7*632c5618SAndreas Gohr */ 8*632c5618SAndreas Gohr 9*632c5618SAndreas Gohr// must be run within Dokuwiki 10*632c5618SAndreas Gohrif(!defined('DOKU_INC')) die(); 11*632c5618SAndreas Gohr 12*632c5618SAndreas Gohrclass admin_plugin_farmer_setup extends DokuWiki_Admin_Plugin { 13*632c5618SAndreas Gohr 14*632c5618SAndreas Gohr /** @var helper_plugin_farmer $helper */ 15*632c5618SAndreas Gohr private $helper; 16*632c5618SAndreas Gohr 17*632c5618SAndreas Gohr /** 18*632c5618SAndreas Gohr * @return bool admin only! 19*632c5618SAndreas Gohr */ 20*632c5618SAndreas Gohr public function forAdminOnly() { 21*632c5618SAndreas Gohr return true; 22*632c5618SAndreas Gohr } 23*632c5618SAndreas Gohr 24*632c5618SAndreas Gohr /** 25*632c5618SAndreas Gohr * Should carry out any processing required by the plugin. 26*632c5618SAndreas Gohr */ 27*632c5618SAndreas Gohr public function handle() { 28*632c5618SAndreas Gohr global $INPUT; 29*632c5618SAndreas Gohr global $ID; 30*632c5618SAndreas Gohr 31*632c5618SAndreas Gohr if(!$INPUT->bool('farmdir')) return; 32*632c5618SAndreas Gohr if(!checkSecurityToken()) return; 33*632c5618SAndreas Gohr 34*632c5618SAndreas Gohr $this->helper = plugin_load('helper', 'farmer'); 35*632c5618SAndreas Gohr 36*632c5618SAndreas Gohr $farmdir = trim($INPUT->str('farmdir', '')); 37*632c5618SAndreas Gohr if($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir; 38*632c5618SAndreas Gohr $farmdir = fullpath($farmdir); 39*632c5618SAndreas Gohr 40*632c5618SAndreas Gohr $errors = array(); 41*632c5618SAndreas Gohr if($farmdir === '') { 42*632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_missing'); 43*632c5618SAndreas Gohr } elseif($this->helper->isInPath($farmdir, DOKU_INC) !== false) { 44*632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_in_dokuwiki'); 45*632c5618SAndreas Gohr } elseif(!io_mkdir_p($farmdir)) { 46*632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_uncreatable'); 47*632c5618SAndreas Gohr } elseif(!is_writeable($farmdir)) { 48*632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_unwritable'); 49*632c5618SAndreas Gohr } elseif(count(scandir($farmdir)) > 2) { 50*632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_notEmpty'); 51*632c5618SAndreas Gohr } 52*632c5618SAndreas Gohr 53*632c5618SAndreas Gohr if($INPUT->str('serversetup', '', true) === '') { 54*632c5618SAndreas Gohr $errors[] = $this->getLang('serversetup_missing'); 55*632c5618SAndreas Gohr } 56*632c5618SAndreas Gohr 57*632c5618SAndreas Gohr if($errors) { 58*632c5618SAndreas Gohr foreach($errors as $error) { 59*632c5618SAndreas Gohr msg($error, -1); 60*632c5618SAndreas Gohr } 61*632c5618SAndreas Gohr return; 62*632c5618SAndreas Gohr } 63*632c5618SAndreas Gohr 64*632c5618SAndreas Gohr if($this->createPreloadPHP($farmdir . "/", $INPUT->str('serversetup'))) { 65*632c5618SAndreas Gohr msg($this->getLang('preload creation success'), 1); 66*632c5618SAndreas Gohr $link = wl($ID, array('do' => 'admin', 'page' => 'farmer'), true, '&'); 67*632c5618SAndreas Gohr send_redirect($link); 68*632c5618SAndreas Gohr } else { 69*632c5618SAndreas Gohr msg($this->getLang('preload creation error'), -1); 70*632c5618SAndreas Gohr } 71*632c5618SAndreas Gohr } 72*632c5618SAndreas Gohr 73*632c5618SAndreas Gohr /** 74*632c5618SAndreas Gohr * Render HTML output, e.g. helpful text and a form 75*632c5618SAndreas Gohr */ 76*632c5618SAndreas Gohr public function html() { 77*632c5618SAndreas Gohr // Is preload.php already enabled? 78*632c5618SAndreas Gohr if(file_exists(DOKU_INC . 'inc/preload.php')) { 79*632c5618SAndreas Gohr msg($this->getLang('overwrite_preload'), -1); 80*632c5618SAndreas Gohr } 81*632c5618SAndreas Gohr 82*632c5618SAndreas Gohr $form = new \dokuwiki\Form\Form(); 83*632c5618SAndreas Gohr $form->addClass('plugin_farmer'); 84*632c5618SAndreas Gohr $form->addFieldsetOpen($this->getLang('preloadPHPForm')); 85*632c5618SAndreas Gohr $form->addTextInput('farmdir', $this->getLang('farm dir'))->addClass('block edit'); 86*632c5618SAndreas Gohr 87*632c5618SAndreas Gohr $form->addRadioButton('serversetup', $this->getLang('subdomain setup'))->val('subdomain')->attr('type', 'radio')->addClass('block edit')->id('subdomain__setup'); 88*632c5618SAndreas Gohr $form->addRadioButton('serversetup', $this->getLang('htaccess setup'))->val('htaccess')->attr('type', 'radio')->addClass('block edit')->attr('checked', true)->id('htaccess__setup'); 89*632c5618SAndreas Gohr 90*632c5618SAndreas Gohr $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit'); 91*632c5618SAndreas Gohr $form->addFieldsetClose(); 92*632c5618SAndreas Gohr echo $form->toHTML(); 93*632c5618SAndreas Gohr 94*632c5618SAndreas Gohr echo sprintf($this->locale_xhtml('preload'), dirname(DOKU_REL) . '/farm/'); 95*632c5618SAndreas Gohr 96*632c5618SAndreas Gohr } 97*632c5618SAndreas Gohr 98*632c5618SAndreas Gohr /** 99*632c5618SAndreas Gohr * @param string $animalpath path to where the animals are stored 100*632c5618SAndreas Gohr * @param bool $htaccess Should the .htaccess be adjusted? 101*632c5618SAndreas Gohr * @return bool 102*632c5618SAndreas Gohr */ 103*632c5618SAndreas Gohr protected function createPreloadPHP($animalpath, $htaccess) { 104*632c5618SAndreas Gohr if($htaccess && !$this->createHtaccess()) { 105*632c5618SAndreas Gohr return false; 106*632c5618SAndreas Gohr } 107*632c5618SAndreas Gohr 108*632c5618SAndreas Gohr $content = "<?php\n"; 109*632c5618SAndreas Gohr $content .= "# farm setup by farmer plugin\n"; 110*632c5618SAndreas Gohr $content .= "if(!defined('DOKU_FARMDIR')) define('DOKU_FARMDIR', '$animalpath');\n"; 111*632c5618SAndreas Gohr $content .= "include(fullpath(dirname(__FILE__)).'/../lib/plugins/farmer/farm.php');\n"; 112*632c5618SAndreas Gohr return io_saveFile(DOKU_INC . 'inc/preload.php', $content); 113*632c5618SAndreas Gohr } 114*632c5618SAndreas Gohr 115*632c5618SAndreas Gohr /** 116*632c5618SAndreas Gohr * Appends the needed config to the main .htaccess for htaccess type setups 117*632c5618SAndreas Gohr * 118*632c5618SAndreas Gohr * @return bool true if saving was successful 119*632c5618SAndreas Gohr */ 120*632c5618SAndreas Gohr protected function createHtaccess() { 121*632c5618SAndreas Gohr $content = "\n\n# Options added for farm setup by Farmer Plugin:\n"; 122*632c5618SAndreas Gohr $content .= "RewriteEngine On\n"; 123*632c5618SAndreas Gohr $content .= 'RewriteRule ^/?!([^/]+)/(.*) ' . DOKU_REL . '$2?animal=$1 [QSA]' . "\n"; 124*632c5618SAndreas Gohr $content .= 'RewriteRule ^/?!([^/]+)$ ' . DOKU_REL . '?animal=$1 [QSA]' . "\n"; 125*632c5618SAndreas Gohr $content .= 'Options +FollowSymLinks'."\n"; 126*632c5618SAndreas Gohr return io_saveFile(DOKU_INC . '.htaccess', $content, true); 127*632c5618SAndreas Gohr } 128*632c5618SAndreas Gohr 129*632c5618SAndreas Gohr} 130*632c5618SAndreas Gohr 131*632c5618SAndreas Gohr// vim:ts=4:sw=4:et: 132