1632c5618SAndreas Gohr<?php 21da41c8bSAndreas Gohr 31da41c8bSAndreas Gohruse dokuwiki\Extension\AdminPlugin; 41da41c8bSAndreas Gohruse dokuwiki\Form\Form; 51da41c8bSAndreas Gohr 6632c5618SAndreas Gohr/** 7632c5618SAndreas Gohr * DokuWiki Plugin farmer (Admin Component) 8632c5618SAndreas Gohr * 91da41c8bSAndreas Gohr * Setup the farm by creating preload.php etc 101da41c8bSAndreas Gohr * 11632c5618SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 12632c5618SAndreas Gohr * @author Michael Große <grosse@cosmocode.de> 130a5d2da2SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 14632c5618SAndreas Gohr */ 151da41c8bSAndreas Gohrclass admin_plugin_farmer_setup extends AdminPlugin 161da41c8bSAndreas Gohr{ 17632c5618SAndreas Gohr /** @var helper_plugin_farmer $helper */ 18632c5618SAndreas Gohr private $helper; 19632c5618SAndreas Gohr 20*341a2d35SAndreas Gohr /** @inheritdoc */ 21*341a2d35SAndreas Gohr public function showInMenu() 221da41c8bSAndreas Gohr { 23*341a2d35SAndreas Gohr return false; 24632c5618SAndreas Gohr } 25632c5618SAndreas Gohr 26*341a2d35SAndreas Gohr /** @inheritdoc */ 271da41c8bSAndreas Gohr public function handle() 281da41c8bSAndreas Gohr { 29632c5618SAndreas Gohr global $INPUT; 30632c5618SAndreas Gohr global $ID; 31632c5618SAndreas Gohr 32632c5618SAndreas Gohr if (!$INPUT->bool('farmdir')) return; 33632c5618SAndreas Gohr if (!checkSecurityToken()) return; 34632c5618SAndreas Gohr 35632c5618SAndreas Gohr $this->helper = plugin_load('helper', 'farmer'); 36632c5618SAndreas Gohr 37632c5618SAndreas Gohr $farmdir = trim($INPUT->str('farmdir', '')); 38632c5618SAndreas Gohr if ($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir; 39632c5618SAndreas Gohr $farmdir = fullpath($farmdir); 40632c5618SAndreas Gohr 411da41c8bSAndreas Gohr $errors = []; 42632c5618SAndreas Gohr if ($farmdir === '') { 43632c5618SAndreas Gohr $errors[] = $this->getLang('farmdir_missing'); 44632c5618SAndreas Gohr } elseif ($this->helper->isInPath($farmdir, DOKU_INC) !== false) { 45f4bc36c6SAndreas Gohr $errors[] = sprintf($this->getLang('farmdir_in_dokuwiki'), hsc($farmdir), hsc(DOKU_INC)); 46632c5618SAndreas Gohr } elseif (!io_mkdir_p($farmdir)) { 47f4bc36c6SAndreas Gohr $errors[] = sprintf($this->getLang('farmdir_uncreatable'), hsc($farmdir)); 481da41c8bSAndreas Gohr } elseif (!is_writable($farmdir)) { 49f4bc36c6SAndreas Gohr $errors[] = sprintf($this->getLang('farmdir_unwritable'), hsc($farmdir)); 50632c5618SAndreas Gohr } elseif (count(scandir($farmdir)) > 2) { 51f4bc36c6SAndreas Gohr $errors[] = sprintf($this->getLang('farmdir_notEmpty'), hsc($farmdir)); 52632c5618SAndreas Gohr } 53632c5618SAndreas Gohr 54632c5618SAndreas Gohr if ($errors) { 55632c5618SAndreas Gohr foreach ($errors as $error) { 56632c5618SAndreas Gohr msg($error, -1); 57632c5618SAndreas Gohr } 58632c5618SAndreas Gohr return; 59632c5618SAndreas Gohr } 60632c5618SAndreas Gohr 61a646d519SAndreas Gohr // create the files 62c4c8e953SAndreas Gohr $ok = $this->createPreloadPHP(); 63c4c8e953SAndreas Gohr if ($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess(); 6423164e01SAndreas Gohr if ($ok) $ok &= $this->createFarmIni($farmdir); 65a646d519SAndreas Gohr 66a646d519SAndreas Gohr if ($ok) { 67632c5618SAndreas Gohr msg($this->getLang('preload creation success'), 1); 681da41c8bSAndreas Gohr $link = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => 'config'], true, '&'); 69632c5618SAndreas Gohr send_redirect($link); 70632c5618SAndreas Gohr } else { 71632c5618SAndreas Gohr msg($this->getLang('preload creation error'), -1); 72632c5618SAndreas Gohr } 73632c5618SAndreas Gohr } 74632c5618SAndreas Gohr 75*341a2d35SAndreas Gohr /** @inheritdoc */ 761da41c8bSAndreas Gohr public function html() 771da41c8bSAndreas Gohr { 78632c5618SAndreas Gohr // Is preload.php already enabled? 79632c5618SAndreas Gohr if (file_exists(DOKU_INC . 'inc/preload.php')) { 80632c5618SAndreas Gohr msg($this->getLang('overwrite_preload'), -1); 81632c5618SAndreas Gohr } 82632c5618SAndreas Gohr 831da41c8bSAndreas Gohr $form = new Form(); 84632c5618SAndreas Gohr $form->addClass('plugin_farmer'); 85632c5618SAndreas Gohr $form->addFieldsetOpen($this->getLang('preloadPHPForm')); 86c4c8e953SAndreas Gohr $form->addTextInput('farmdir', $this->getLang('farm dir')); 87c4c8e953SAndreas Gohr $form->addCheckbox('htaccess', $this->getLang('htaccess setup'))->attr('checked', 'checked'); 88632c5618SAndreas Gohr $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit'); 89632c5618SAndreas Gohr $form->addFieldsetClose(); 90632c5618SAndreas Gohr echo $form->toHTML(); 91632c5618SAndreas Gohr } 92632c5618SAndreas Gohr 93632c5618SAndreas Gohr /** 94a646d519SAndreas Gohr * Creates the preload that loads our farm controller 95a646d519SAndreas Gohr * @return bool true if saving was successful 96632c5618SAndreas Gohr */ 971da41c8bSAndreas Gohr protected function createPreloadPHP() 981da41c8bSAndreas Gohr { 99632c5618SAndreas Gohr $content = "<?php\n"; 100632c5618SAndreas Gohr $content .= "# farm setup by farmer plugin\n"; 101c4c8e953SAndreas Gohr $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n"; 102c4c8e953SAndreas Gohr $content .= " include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n"; 103c4c8e953SAndreas Gohr $content .= "}\n"; 104632c5618SAndreas Gohr return io_saveFile(DOKU_INC . 'inc/preload.php', $content); 105632c5618SAndreas Gohr } 106632c5618SAndreas Gohr 107632c5618SAndreas Gohr /** 1085881efbeSAndreas Gohr * Prepends the needed config to the main .htaccess for htaccess type setups 109632c5618SAndreas Gohr * 110632c5618SAndreas Gohr * @return bool true if saving was successful 111632c5618SAndreas Gohr */ 1121da41c8bSAndreas Gohr protected function createHtaccess() 1131da41c8bSAndreas Gohr { 1145881efbeSAndreas Gohr // load existing or template 1155881efbeSAndreas Gohr if (file_exists(DOKU_INC . '.htaccess')) { 1165881efbeSAndreas Gohr $old = io_readFile(DOKU_INC . '.htaccess'); 1175881efbeSAndreas Gohr } elseif (file_exists(DOKU_INC . '.htaccess.dist')) { 1185881efbeSAndreas Gohr $old = io_readFile(DOKU_INC . '.htaccess.dist'); 1195881efbeSAndreas Gohr } else { 1205881efbeSAndreas Gohr $old = ''; 1215881efbeSAndreas Gohr } 1225881efbeSAndreas Gohr 1235881efbeSAndreas Gohr $content = "# Options added for farm setup by farmer plugin:\n"; 124632c5618SAndreas Gohr $content .= "RewriteEngine On\n"; 1255881efbeSAndreas Gohr $content .= 'RewriteRule ^!([^/]+)/(.*) $2?animal=$1 [QSA,DPI]' . "\n"; 1265881efbeSAndreas Gohr $content .= 'RewriteRule ^!([^/]+)$ ?animal=$1 [QSA,DPI]' . "\n"; 127632c5618SAndreas Gohr $content .= 'Options +FollowSymLinks' . "\n"; 1285881efbeSAndreas Gohr $content .= '# end of farm configuration' . "\n\n"; 1295881efbeSAndreas Gohr $content .= $old; 1305881efbeSAndreas Gohr return io_saveFile(DOKU_INC . '.htaccess', $content); 131632c5618SAndreas Gohr } 132632c5618SAndreas Gohr 133a646d519SAndreas Gohr /** 134a646d519SAndreas Gohr * Creates the initial configuration 135a646d519SAndreas Gohr * 136a646d519SAndreas Gohr * @param $animalpath 137a646d519SAndreas Gohr * @return bool true if saving was successful 138a646d519SAndreas Gohr */ 1391da41c8bSAndreas Gohr protected function createFarmIni($animalpath) 1401da41c8bSAndreas Gohr { 141a646d519SAndreas Gohr $content = "; farm config created by the farmer plugin\n\n"; 142a646d519SAndreas Gohr $content .= "[base]\n"; 143a646d519SAndreas Gohr $content .= "farmdir = $animalpath\n"; 144a646d519SAndreas Gohr $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n"; 145a646d519SAndreas Gohr return io_saveFile(DOKU_INC . 'conf/farm.ini', $content); 146a646d519SAndreas Gohr } 147632c5618SAndreas Gohr} 148