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 * @author Andreas Gohr <gohr@cosmocode.de> 8 */ 9 10// must be run within Dokuwiki 11if(!defined('DOKU_INC')) die(); 12 13/** 14 * Setup the farm by creating preload.php etc 15 */ 16class admin_plugin_farmer_setup extends DokuWiki_Admin_Plugin { 17 18 /** @var helper_plugin_farmer $helper */ 19 private $helper; 20 21 /** 22 * @return bool admin only! 23 */ 24 public function forAdminOnly() { 25 return true; 26 } 27 28 /** 29 * Should carry out any processing required by the plugin. 30 */ 31 public function handle() { 32 global $INPUT; 33 global $ID; 34 35 if(!$INPUT->bool('farmdir')) return; 36 if(!checkSecurityToken()) return; 37 38 $this->helper = plugin_load('helper', 'farmer'); 39 40 $farmdir = trim($INPUT->str('farmdir', '')); 41 if($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir; 42 $farmdir = fullpath($farmdir); 43 44 $errors = array(); 45 if($farmdir === '') { 46 $errors[] = $this->getLang('farmdir_missing'); 47 } elseif($this->helper->isInPath($farmdir, DOKU_INC) !== false) { 48 $errors[] = sprintf($this->getLang('farmdir_in_dokuwiki'), hsc($farmdir), hsc(DOKU_INC)); 49 } elseif(!io_mkdir_p($farmdir)) { 50 $errors[] = sprintf($this->getLang('farmdir_uncreatable'), hsc($farmdir)); 51 } elseif(!is_writeable($farmdir)) { 52 $errors[] = sprintf($this->getLang('farmdir_unwritable'), hsc($farmdir)); 53 } elseif(count(scandir($farmdir)) > 2) { 54 $errors[] = sprintf($this->getLang('farmdir_notEmpty'), hsc($farmdir)); 55 } 56 57 if($errors) { 58 foreach($errors as $error) { 59 msg($error, -1); 60 } 61 return; 62 } 63 64 // create the files 65 $ok = $this->createPreloadPHP(); 66 if($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess(); 67 if($ok) $ok &= $this->createFarmIni($farmdir); 68 69 if($ok) { 70 msg($this->getLang('preload creation success'), 1); 71 $link = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&'); 72 send_redirect($link); 73 } else { 74 msg($this->getLang('preload creation error'), -1); 75 } 76 } 77 78 /** 79 * Render HTML output, e.g. helpful text and a form 80 */ 81 public function html() { 82 // Is preload.php already enabled? 83 if(file_exists(DOKU_INC . 'inc/preload.php')) { 84 msg($this->getLang('overwrite_preload'), -1); 85 } 86 87 $form = new \dokuwiki\Form\Form(); 88 $form->addClass('plugin_farmer'); 89 $form->addFieldsetOpen($this->getLang('preloadPHPForm')); 90 $form->addTextInput('farmdir', $this->getLang('farm dir')); 91 $form->addCheckbox('htaccess', $this->getLang('htaccess setup'))->attr('checked', 'checked'); 92 $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit'); 93 $form->addFieldsetClose(); 94 echo $form->toHTML(); 95 96 } 97 98 /** 99 * Creates the preload that loads our farm controller 100 * @return bool true if saving was successful 101 */ 102 protected function createPreloadPHP() { 103 $content = "<?php\n"; 104 $content .= "# farm setup by farmer plugin\n"; 105 $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n"; 106 $content .= " include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n"; 107 $content .= "}\n"; 108 return io_saveFile(DOKU_INC . 'inc/preload.php', $content); 109 } 110 111 /** 112 * Prepends the needed config to the main .htaccess for htaccess type setups 113 * 114 * @return bool true if saving was successful 115 */ 116 protected function createHtaccess() { 117 // load existing or template 118 if(file_exists(DOKU_INC . '.htaccess')) { 119 $old = io_readFile(DOKU_INC . '.htaccess'); 120 } elseif(file_exists(DOKU_INC . '.htaccess.dist')) { 121 $old = io_readFile(DOKU_INC . '.htaccess.dist'); 122 } else { 123 $old = ''; 124 } 125 126 $content = "# Options added for farm setup by farmer plugin:\n"; 127 $content .= "RewriteEngine On\n"; 128 $content .= 'RewriteRule ^!([^/]+)/(.*) $2?animal=$1 [QSA,DPI]' . "\n"; 129 $content .= 'RewriteRule ^!([^/]+)$ ?animal=$1 [QSA,DPI]' . "\n"; 130 $content .= 'Options +FollowSymLinks' . "\n"; 131 $content .= '# end of farm configuration' . "\n\n"; 132 $content .= $old; 133 return io_saveFile(DOKU_INC . '.htaccess', $content); 134 } 135 136 /** 137 * Creates the initial configuration 138 * 139 * @param $animalpath 140 * @return bool true if saving was successful 141 */ 142 protected function createFarmIni($animalpath) { 143 $content = "; farm config created by the farmer plugin\n\n"; 144 $content .= "[base]\n"; 145 $content .= "farmdir = $animalpath\n"; 146 $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n"; 147 return io_saveFile(DOKU_INC . 'conf/farm.ini', $content); 148 } 149} 150 151// vim:ts=4:sw=4:et: 152