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