diff options
Diffstat (limited to 'platform/www/lib/plugins/farmer/admin')
-rw-r--r-- | platform/www/lib/plugins/farmer/admin/config.php | 126 | ||||
-rw-r--r-- | platform/www/lib/plugins/farmer/admin/delete.php | 95 | ||||
-rw-r--r-- | platform/www/lib/plugins/farmer/admin/info.php | 116 | ||||
-rw-r--r-- | platform/www/lib/plugins/farmer/admin/new.php | 338 | ||||
-rw-r--r-- | platform/www/lib/plugins/farmer/admin/plugins.php | 104 | ||||
-rw-r--r-- | platform/www/lib/plugins/farmer/admin/setup.php | 151 |
6 files changed, 930 insertions, 0 deletions
diff --git a/platform/www/lib/plugins/farmer/admin/config.php b/platform/www/lib/plugins/farmer/admin/config.php new file mode 100644 index 0000000..7d79970 --- /dev/null +++ b/platform/www/lib/plugins/farmer/admin/config.php @@ -0,0 +1,126 @@ +<?php +/** + * DokuWiki Plugin farmer (Admin Component) + * + * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html + * @author Michael Große <grosse@cosmocode.de> + * @author Andreas Gohr <gohr@cosmocode.de> + */ +use dokuwiki\Form\Form; + +// must be run within Dokuwiki +if(!defined('DOKU_INC')) die(); + +/** + * Configuration Interface for farm.ini + */ +class admin_plugin_farmer_config extends DokuWiki_Admin_Plugin { + + /** @var helper_plugin_farmer */ + protected $helper; + + /** + * @return bool admin only! + */ + public function forAdminOnly() { + return false; + } + + /** + * admin_plugin_farmer_config constructor. + */ + public function __construct() { + $this->helper = plugin_load('helper', 'farmer'); + } + + /** + * Should carry out any processing required by the plugin. + */ + public function handle() { + global $INPUT; + global $ID; + if(!$INPUT->has('farmconf')) return; + if(!checkSecurityToken()) return; + + $farmconf = $this->helper->getConfig(); + $farmdir = $farmconf['base']['farmdir']; + $farmconf = array_merge($farmconf, $INPUT->arr('farmconf')); + $farmconf['base']['farmdir'] = $farmdir; + + $farmconf['base']['basedomain'] = trim(trim($farmconf['base']['basedomain'], '.')); + + $ini = DOKU_INC . 'conf/farm.ini'; + $data = "; Farm config created by the farmer plugin\n"; + $data .= $this->createIni($farmconf); + io_saveFile($ini, $data); + + $self = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&'); + send_redirect($self); + } + + /** + * Render HTML output, e.g. helpful text and a form + */ + public function html() { + $farmconf = $this->helper->getConfig(); + + $form = new Form(array('method' => 'post')); + + $form->addFieldsetOpen($this->getLang('base')); + $form->addHTML('<label><span>' . $this->getLang('farm dir') . '</span>' . DOKU_FARMDIR); + $form->addTextInput('farmconf[base][farmhost]', $this->getLang('farm host'))->val($farmconf['base']['farmhost']); + $form->addTextInput('farmconf[base][basedomain]', $this->getLang('base domain'))->val($farmconf['base']['basedomain']); + $form->addFieldsetClose(); + + $form->addFieldsetOpen($this->getLang('conf_inherit')); + foreach($farmconf['inherit'] as $key => $val) { + $form->setHiddenField("farmconf[inherit][$key]", 0); + $chk = $form->addCheckbox("farmconf[inherit][$key]", $this->getLang('conf_inherit_' . $key))->useInput(false); + if($val) $chk->attr('checked', 'checked'); + } + $form->addFieldsetClose(); + + $options = array( + 'farmer' => $this->getLang('conf_notfound_farmer'), + '404' => $this->getLang('conf_notfound_404'), + 'list' => $this->getLang('conf_notfound_list'), + 'redirect' => $this->getLang('conf_notfound_redirect') + ); + + $form->addFieldsetOpen($this->getLang('conf_notfound')); + $form->addDropdown('farmconf[notfound][show]', $options, $this->getLang('conf_notfound'))->val($farmconf['notfound']['show']); + $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))->val($farmconf['notfound']['url']); + $form->addFieldsetClose(); + + $form->addButton('save', $this->getLang('save')); + echo $form->toHTML(); + } + + /** + * Simple function to create an ini file + * + * Does no escaping, but should suffice for our use case + * + * @link http://stackoverflow.com/a/5695202/172068 + * @param array $data The data to transform + * @return string + */ + public function createIni($data) { + $res = array(); + foreach($data as $key => $val) { + if(is_array($val)) { + $res[] = ''; + $res[] = "[$key]"; + foreach($val as $skey => $sval) { + $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"'); + } + } else { + $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"'); + } + } + $res[] = ''; + return join("\n", $res); + } +} + +// vim:ts=4:sw=4:et: diff --git a/platform/www/lib/plugins/farmer/admin/delete.php b/platform/www/lib/plugins/farmer/admin/delete.php new file mode 100644 index 0000000..d8b3450 --- /dev/null +++ b/platform/www/lib/plugins/farmer/admin/delete.php @@ -0,0 +1,95 @@ +<?php +/** + * DokuWiki Plugin farmer (Admin Component) + * + * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html + * @author Michael Große <grosse@cosmocode.de> + * @author Andreas Gohr <gohr@cosmocode.de> + */ + +// must be run within Dokuwiki +use dokuwiki\Form\Form; + +if(!defined('DOKU_INC')) die(); + +/** + * Information about the farm and the current instance + */ +class admin_plugin_farmer_delete extends DokuWiki_Admin_Plugin { + + /** @var helper_plugin_farmer */ + protected $helper; + + /** + * admin_plugin_farmer_info constructor. + */ + public function __construct() { + $this->helper = plugin_load('helper', 'farmer'); + } + + /** + * @return bool admin only! + */ + public function forAdminOnly() { + return true; + } + + /** + * Should carry out any processing required by the plugin. + */ + public function handle() { + global $INPUT; + global $ID; + if(!$INPUT->has('delete')) return; + + if($INPUT->filter('trim')->str('delanimal') === '') { + msg($this->getLang('delete_noanimal'), -1); + return; + } + + if($INPUT->str('delanimal') != $INPUT->str('confirm')) { + msg($this->getLang('delete_mismatch'), -1); + return; + } + + $animaldir = DOKU_FARMDIR . $INPUT->str('delanimal'); + + if(!$this->helper->isInPath($animaldir, DOKU_FARMDIR) || !is_dir($animaldir)) { + msg($this->getLang('delete_invalid'), -1); + return; + } + + // let's delete it + $ok = io_rmdir($animaldir, true); + if($ok) { + msg($this->getLang('delete_success'), 1); + } else { + msg($this->getLang('delete_fail'), -1); + } + + $link = wl($ID, array('do'=>'admin', 'page'=>'farmer', 'sub' => 'delete'), true, '&'); + send_redirect($link); + } + + /** + * Render HTML output, e.g. helpful text and a form + */ + public function html() { + + $form = new Form(); + $form->addFieldsetOpen($this->getLang('delete_animal')); + + $animals = $this->helper->getAllAnimals(); + array_unshift($animals, ''); + $form->addDropdown('delanimal', $animals)->addClass('farmer_chosen_animals'); + $form->addTextInput('confirm', $this->getLang('delete_confirm')); + $form->addButton('delete', $this->getLang('delete')); + $form->addFieldsetClose(); + echo $form->toHTML(); + + } + + +} + +// vim:ts=4:sw=4:et: diff --git a/platform/www/lib/plugins/farmer/admin/info.php b/platform/www/lib/plugins/farmer/admin/info.php new file mode 100644 index 0000000..3bf2938 --- /dev/null +++ b/platform/www/lib/plugins/farmer/admin/info.php @@ -0,0 +1,116 @@ +<?php +/** + * DokuWiki Plugin farmer (Admin Component) + * + * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html + * @author Michael Große <grosse@cosmocode.de> + * @author Andreas Gohr <gohr@cosmocode.de> + */ + +// must be run within Dokuwiki +if(!defined('DOKU_INC')) die(); + +/** + * Information about the farm and the current instance + */ +class admin_plugin_farmer_info extends DokuWiki_Admin_Plugin { + + /** @var helper_plugin_farmer */ + protected $helper; + + /** + * admin_plugin_farmer_info constructor. + */ + public function __construct() { + $this->helper = plugin_load('helper', 'farmer'); + } + + /** + * @return bool admin only! + */ + public function forAdminOnly() { + return false; + } + + /** + * Should carry out any processing required by the plugin. + */ + public function handle() { + } + + /** + * Render HTML output, e.g. helpful text and a form + */ + public function html() { + global $conf; + global $INPUT; + + $animal = $this->helper->getAnimal(); + $config = $this->helper->getConfig(); + + echo '<table class="inline">'; + + $this->line('thisis', $animal ? $this->getLang('thisis.animal') : $this->getLang('thisis.farmer')); + if($animal) { + $this->line('animal', $animal); + } + $this->line('confdir', fullpath(DOKU_CONF) . '/'); + $this->line('savedir', fullpath($conf['savedir']) . '/'); + $this->line('baseinstall', DOKU_INC); + $this->line('farm host', $config['base']['farmhost']); + $this->line('farm dir', DOKU_FARMDIR); + + $this->line('animals', $this->animals($INPUT->bool('list'))); + + foreach($config['inherit'] as $key => $value) { + $this->line('conf_inherit_' . $key, $this->getLang($value ? 'conf_inherit_yes' : 'conf_inherit_no')); + } + + $this->line('plugins', join(', ', $this->helper->getAllPlugins(false))); + + echo '</table>'; + } + + /** + * List or count the animals + * + * @param bool $list + * @return string + */ + protected function animals($list) { + global $ID; + + $animals = $this->helper->getAllAnimals(); + $html = ''; + if(!$list) { + $html = count($animals); + $self = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'info', 'list' => 1)); + $html .= ' [<a href="' . $self . '">' . $this->getLang('conf_notfound_list') . '</a>]'; + return $html; + } + + $html .= '<ol>'; + foreach($animals as $animal) { + $link = $this->helper->getAnimalURL($animal); + $html .= '<li><div class="li"><a href="' . $link . '">' . $animal . '</a></div></li>'; + } + $html .= '</ol>'; + return $html; + } + + /** + * Output a table line + * + * @param string $langkey + * @param string $value + */ + protected function line($langkey, $value) { + echo '<tr>'; + echo '<th>' . $this->getLang($langkey) . '</th>'; + echo '<td>' . $value . '</td>'; + echo '</tr>'; + } + +} + +// vim:ts=4:sw=4:et: diff --git a/platform/www/lib/plugins/farmer/admin/new.php b/platform/www/lib/plugins/farmer/admin/new.php new file mode 100644 index 0000000..8cbf94f --- /dev/null +++ b/platform/www/lib/plugins/farmer/admin/new.php @@ -0,0 +1,338 @@ +<?php +/** + * DokuWiki Plugin farmer (Admin Component) + * + * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html + * @author Michael Große <grosse@cosmocode.de> + */ + +// must be run within Dokuwiki +if(!defined('DOKU_INC')) die(); + +class admin_plugin_farmer_new extends DokuWiki_Admin_Plugin { + + /** @var helper_plugin_farmer $helper */ + protected $helper; + + /** + * @return bool true if only access for superuser, false is for superusers and moderators + */ + public function forAdminOnly() { + return true; + } + + /** + * admin_plugin_farmer_new constructor. + */ + public function __construct() { + $this->helper = plugin_load('helper', 'farmer'); + } + + /** + * Should carry out any processing required by the plugin. + */ + public function handle() { + global $INPUT; + global $ID; + if(!$INPUT->has('farmer__submit')) return; + + $data = $this->validateAnimalData(); + if(!$data) return; + if($this->createNewAnimal($data['name'], $data['admin'], $data['pass'], $data['template'], $data['aclpolicy'], $data['allowreg'])) { + $url = $this->helper->getAnimalURL($data['name']); + $link = '<a href="' . $url . '">' . hsc($data['name']) . '</a>'; + + msg(sprintf($this->getLang('animal creation success'), $link), 1); + $link = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'new'), true, '&'); + send_redirect($link); + } + } + + /** + * Render HTML output, e.g. helpful text and a form + */ + public function html() { + global $lang; + $farmconfig = $this->helper->getConfig(); + + $form = new \dokuwiki\Form\Form(); + $form->addClass('plugin_farmer')->id('farmer__create_animal_form'); + + $form->addFieldsetOpen($this->getLang('animal configuration')); + $form->addTextInput('animalname', $this->getLang('animal')); + $form->addFieldsetClose(); + + $animals = $this->helper->getAllAnimals(); + array_unshift($animals, ''); + $form->addFieldsetOpen($this->getLang('animal template')); + $form->addDropdown('animaltemplate', $animals)->addClass('farmer_chosen_animals'); + $form->addFieldsetClose(); + + $form->addFieldsetOpen($lang['i_policy'])->attr('id', 'aclPolicyFieldset'); + $policyOptions = array('open' => $lang['i_pol0'],'public' => $lang['i_pol1'], 'closed' => $lang['i_pol2']); + $form->addDropdown('aclpolicy', $policyOptions)->addClass('acl_chosen'); + if ($farmconfig['inherit']['main']) { + $form->addRadioButton('allowreg',$this->getLang('inherit user registration'))->val('inherit')->attr('checked', 'checked'); + $form->addRadioButton('allowreg',$this->getLang('enable user registration'))->val('allow'); + $form->addRadioButton('allowreg',$this->getLang('disable user registration'))->val('disable'); + } else { + $form->addCheckbox('allowreg', $lang['i_allowreg'])->attr('checked', 'checked'); + } + + $form->addFieldsetClose(); + + $form->addFieldsetOpen($this->getLang('animal administrator')); + $btn = $form->addRadioButton('adminsetup', $this->getLang('noUsers'))->val('noUsers'); + if($farmconfig['inherit']['users']) { + $btn->attr('checked', 'checked'); // default when inherit available + } else { + // no user copying when inheriting + $form->addRadioButton('adminsetup', $this->getLang('importUsers'))->val('importUsers'); + $form->addRadioButton('adminsetup', $this->getLang('currentAdmin'))->val('currentAdmin'); + } + $btn = $form->addRadioButton('adminsetup', $this->getLang('newAdmin'))->val('newAdmin'); + if(!$farmconfig['inherit']['users']) { + $btn->attr('checked', 'checked'); // default when inherit not available + } + $form->addPasswordInput('adminPassword', $this->getLang('admin password')); + $form->addFieldsetClose(); + + $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit')->val('newAnimal'); + echo $form->toHTML(); + } + + /** + * Validate the data for a new animal + * + * @return array|bool false on errors, clean data otherwise + */ + protected function validateAnimalData() { + global $INPUT; + + $animalname = $INPUT->filter('trim')->str('animalname'); + $adminsetup = $INPUT->str('adminsetup'); + $adminpass = $INPUT->filter('trim')->str('adminPassword'); + $template = $INPUT->filter('trim')->str('animaltemplate'); + $aclpolicy = $INPUT->filter('trim')->str('aclpolicy'); + $allowreg = $INPUT->str('allowreg'); + + $errors = array(); + + if($animalname === '') { + $errors[] = $this->getLang('animalname_missing'); + } elseif(!$this->helper->validateAnimalName($animalname)) { + $errors[] = $this->getLang('animalname_invalid'); + } + + if($adminsetup === 'newAdmin' && $adminpass === '') { + $errors[] = $this->getLang('adminPassword_empty'); + } + + if($animalname !== '' && file_exists(DOKU_FARMDIR . '/' . $animalname)) { + $errors[] = $this->getLang('animalname_preexisting'); + } + + if (!is_dir(DOKU_FARMDIR . $template) && !in_array($aclpolicy,array('open', 'public', 'closed'))) { + $errors[] = $this->getLang('aclpolicy missing/bad'); + } + + if($errors) { + foreach($errors as $error) { + msg($error, -1); + } + return false; + } + + if(!is_dir(DOKU_FARMDIR . $template)) { + $template = ''; + } + if ($template != '') { + $aclpolicy = ''; + } + + return array( + 'name' => $animalname, + 'admin' => $adminsetup, + 'pass' => $adminpass, + 'template' => $template, + 'aclpolicy' => $aclpolicy, + 'allowreg' => $allowreg + ); + } + + /** + * Create a new animal + * + * @param string $name name/title of the animal, will be the directory name for htaccess setup + * @param string $adminSetup newAdmin, currentAdmin or importUsers + * @param string $adminPassword required if $adminSetup is newAdmin + * @param string $template name of animal to copy + * @param $aclpolicy + * @param $userreg + * @return bool true if successful + * @throws Exception + */ + protected function createNewAnimal($name, $adminSetup, $adminPassword, $template, $aclpolicy, $userreg) { + $animaldir = DOKU_FARMDIR . $name; + + // copy basic template + $ok = $this->helper->io_copyDir(__DIR__ . '/../_animal', $animaldir); + if(!$ok) { + msg($this->getLang('animal creation error'), -1); + return false; + } + + // copy animal template + if($template != '') { + foreach(array('conf', 'data/pages', 'data/media', 'data/meta', 'data/media_meta', 'index') as $dir) { + $templatedir = DOKU_FARMDIR . $template . '/' . $dir; + if(!is_dir($templatedir)) continue; + // do not copy changelogs in meta + if(substr($dir, -4) == 'meta') { + $exclude = '/\.changes$/'; + } else { + $exclude = ''; + } + if(!$this->helper->io_copyDir($templatedir, $animaldir . '/' . $dir, $exclude)) { + msg(sprintf($this->getLang('animal template copy error'), $dir), -1); + // we go on anyway + } + } + } + + // append title to local config + $ok &= io_saveFile($animaldir . '/conf/local.php', "\n" . '$conf[\'title\'] = \'' . $name . '\';' . "\n", true); + + // create a random logo and favicon + if(!class_exists('\splitbrain\RingIcon\RingIcon', false)) { + require(__DIR__ . '/../3rdparty/RingIcon.php'); + } + if(!class_exists('\chrisbliss18\phpico\PHPIco', false)) { + require(__DIR__ . '/../3rdparty/PHPIco.php'); + } + try { + if(function_exists('imagecreatetruecolor')) { + $logo = $animaldir . '/data/media/wiki/logo.png'; + if(!file_exists($logo)) { + $ringicon = new \splitbrain\RingIcon\RingIcon(64); + $ringicon->createImage($animaldir, $logo); + } + + $icon = $animaldir . '/data/media/wiki/favicon.ico'; + if(!file_exists($icon)) { + $icongen = new \chrisbliss18\phpico\PHPIco($logo); + $icongen->save_ico($icon); + } + } + } catch(\Exception $ignore) { + // something went wrong, but we don't care. this is a nice to have feature only + } + + // create admin user + if($adminSetup === 'newAdmin') { + $users = "# <?php exit()?>\n" . $this->makeAdminLine($adminPassword) . "\n"; + } elseif($adminSetup === 'currentAdmin') { + $users = "# <?php exit()?>\n" . $this->getAdminLine() . "\n"; + } elseif($adminSetup === 'noUsers') { + if(file_exists($animaldir . '/conf/users.auth.php')) { + // a user file exists already, probably from animal template - don't overwrite + $users = ''; + } else { + // create empty user file + $users = "# <?php exit()?>\n"; + } + } else { + $users = io_readFile(DOKU_CONF . 'users.auth.php'); + } + if($users) { + $ok &= io_saveFile($animaldir . '/conf/users.auth.php', $users); + } + + if ($aclpolicy != '') { + $aclfile = file($animaldir . '/conf/acl.auth.php'); + $aclfile = array_map('trim', $aclfile); + array_pop($aclfile); + switch ($aclpolicy) { + case 'open': + $aclfile[] = "* @ALL 8"; + break; + case 'public': + $aclfile[] = "* @ALL 1"; + $aclfile[] = "* @user 8"; + break; + case 'closed': + $aclfile[] = "* @ALL 0"; + $aclfile[] = "* @user 8"; + break; + default: + throw new Exception('Undefined aclpolicy given'); + } + $ok &= io_saveFile($animaldir . '/conf/acl.auth.php', join("\n", $aclfile)."\n"); + + global $conf; + switch ($userreg) { + case 'allow': + $disableactions = join(',', array_diff(explode(',', $conf['disableactions']), array('register'))); + $ok &= io_saveFile($animaldir . '/conf/local.php', "\n" . '$conf[\'disableactions\'] = \''.$disableactions.'\';' . "\n", true); + break; + case 'disable': + $disableactions = join(',', array_merge(explode(',', $conf['disableactions']), array('register'))); + $ok &= io_saveFile($animaldir . '/conf/local.php', "\n" . '$conf[\'disableactions\'] = \''.$disableactions.'\';' . "\n", true); + break; + case 'inherit': + case true: + // nothing needs to be done + break; + default: + $ok &= io_saveFile($animaldir . '/conf/local.php', "\n" . '$conf[\'disableactions\'] = \'register\';' . "\n", true); + } + } + + // deactivate plugins by default FIXME this should be nicer + $deactivatedPluginsList = explode(',', $this->getConf('deactivated plugins')); + $deactivatedPluginsList = array_map('trim', $deactivatedPluginsList); + $deactivatedPluginsList = array_unique($deactivatedPluginsList); + $deactivatedPluginsList = array_filter($deactivatedPluginsList); + foreach($deactivatedPluginsList as $plugin) { + $this->helper->setPluginState(trim($plugin), $name, 0); + } + + return $ok; + } + + /** + * Creates a new user line + * + * @param $password + * @return string + */ + protected function makeAdminLine($password) { + $pass = auth_cryptPassword($password); + $line = join( + ':', array( + 'admin', + $pass, + 'Administrator', + 'admin@example.org', + 'admin,user' + ) + ); + return $line; + } + + /** + * Copies the current user as new admin line + * + * @return string + */ + protected function getAdminLine() { + $currentAdmin = $_SERVER['REMOTE_USER']; + $masterUsers = file_get_contents(DOKU_CONF . 'users.auth.php'); + $masterUsers = ltrim(strstr($masterUsers, "\n" . $currentAdmin . ":")); + $newAdmin = substr($masterUsers, 0, strpos($masterUsers, "\n") + 1); + return $newAdmin; + } + +} + +// vim:ts=4:sw=4:et: diff --git a/platform/www/lib/plugins/farmer/admin/plugins.php b/platform/www/lib/plugins/farmer/admin/plugins.php new file mode 100644 index 0000000..74f0e60 --- /dev/null +++ b/platform/www/lib/plugins/farmer/admin/plugins.php @@ -0,0 +1,104 @@ +<?php +/** + * DokuWiki Plugin farmer (Admin Component) + * + * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html + * @author Michael Große <grosse@cosmocode.de> + * @author Andreas Gohr <gohr@cosmocode.de> + */ + +// must be run within Dokuwiki +if(!defined('DOKU_INC')) die(); + +/** + * Manage Animal Plugin settings + */ +class admin_plugin_farmer_plugins extends DokuWiki_Admin_Plugin { + + /** @var helper_plugin_farmer $helper */ + private $helper; + + public function __construct() { + $this->helper = plugin_load('helper', 'farmer'); + } + + /** + * handle user request + */ + public function handle() { + global $INPUT; + global $ID; + + $self = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'plugins'), true, '&'); + + if($INPUT->has('bulk_plugin') && $INPUT->has('state')) { + $animals = $this->helper->getAllAnimals(); + $plugin = $INPUT->str('bulk_plugin'); + foreach($animals as $animal) { + $this->helper->setPluginState($plugin, $animal, $INPUT->int('state')); + } + msg($this->getLang('plugindone'), 1); + send_redirect($self); + } + + if($INPUT->has('bulk_animal') && $INPUT->has('bulk_plugins')) { + $animal = $INPUT->str('bulk_animal'); + $activePlugins = $INPUT->arr('bulk_plugins'); + foreach($activePlugins as $plugin => $state) { + $this->helper->setPluginState($plugin, $animal, $state); + } + msg($this->getLang('plugindone'), 1); + send_redirect($self); + } + } + + /** + * output appropriate html + */ + public function html() { + + echo $this->locale_xhtml('plugins'); + $switchForm = new \dokuwiki\Form\Form(); + $switchForm->addClass('plugin_farmer'); + $switchForm->addFieldsetOpen($this->getLang('bulkSingleSwitcher')); + $switchForm->addRadioButton('bulkSingleSwitch', $this->getLang('bulkEdit'))->id('farmer__bulk')->attr('type', 'radio'); + $switchForm->addRadioButton('bulkSingleSwitch', $this->getLang('singleEdit'))->id('farmer__single')->attr('type', 'radio'); + $switchForm->addRadioButton('bulkSingleSwitch', $this->getLang('matrixEdit'))->id('farmer__matrix')->attr('type', 'radio'); + $switchForm->addFieldsetClose(); + echo $switchForm->toHTML(); + + /** @var helper_plugin_farmer $helper */ + $helper = plugin_load('helper', 'farmer'); + $plugins = $helper->getAllPlugins(); + array_unshift($plugins, ''); + + // All Animals at once + $bulkForm = new \dokuwiki\Form\Form(); + $bulkForm->id('farmer__pluginsforall'); + $bulkForm->addFieldsetOpen($this->getLang('bulkEditForm')); + $bulkForm->addDropdown('bulk_plugin', $plugins); + $bulkForm->addButton('state', $this->getLang('default'))->attr('value', '-1')->attr('type', 'submit')->attr('disabled', 'disabled'); + $bulkForm->addButton('state', $this->getLang('activate'))->attr('value', '1')->attr('type', 'submit')->attr('disabled', 'disabled'); + $bulkForm->addButton('state', $this->getLang('deactivate'))->attr('value', '0')->attr('type', 'submit')->attr('disabled', 'disabled'); + $bulkForm->addFieldsetClose(); + echo $bulkForm->toHTML(); + + $animals = $helper->getAllAnimals(); + array_unshift($animals, ''); + + // One Animal, all the plugins + $singleForm = new \dokuwiki\Form\Form(); + $singleForm->id('farmer__pluginsforone'); + $singleForm->addFieldsetOpen($this->getLang('singleEditForm')); + $singleForm->addDropdown('bulk_animal', $animals); + $singleForm->addTagOpen('div')->addClass('output'); + $singleForm->addTagClose('div'); + $singleForm->addButton('save', $this->getLang('save'))->attr('disabled', 'disabled'); + + echo $singleForm->toHTML(); + + + echo '<div id="farmer__pluginmatrix"></div>'; + } +} + diff --git a/platform/www/lib/plugins/farmer/admin/setup.php b/platform/www/lib/plugins/farmer/admin/setup.php new file mode 100644 index 0000000..f836ae5 --- /dev/null +++ b/platform/www/lib/plugins/farmer/admin/setup.php @@ -0,0 +1,151 @@ +<?php +/** + * DokuWiki Plugin farmer (Admin Component) + * + * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html + * @author Michael Große <grosse@cosmocode.de> + * @author Andreas Gohr <gohr@cosmocode.de> + */ + +// must be run within Dokuwiki +if(!defined('DOKU_INC')) die(); + +/** + * Setup the farm by creating preload.php etc + */ +class admin_plugin_farmer_setup extends DokuWiki_Admin_Plugin { + + /** @var helper_plugin_farmer $helper */ + private $helper; + + /** + * @return bool admin only! + */ + public function forAdminOnly() { + return true; + } + + /** + * Should carry out any processing required by the plugin. + */ + public function handle() { + global $INPUT; + global $ID; + + if(!$INPUT->bool('farmdir')) return; + if(!checkSecurityToken()) return; + + $this->helper = plugin_load('helper', 'farmer'); + + $farmdir = trim($INPUT->str('farmdir', '')); + if($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir; + $farmdir = fullpath($farmdir); + + $errors = array(); + if($farmdir === '') { + $errors[] = $this->getLang('farmdir_missing'); + } elseif($this->helper->isInPath($farmdir, DOKU_INC) !== false) { + $errors[] = sprintf($this->getLang('farmdir_in_dokuwiki'), hsc($farmdir), hsc(DOKU_INC)); + } elseif(!io_mkdir_p($farmdir)) { + $errors[] = sprintf($this->getLang('farmdir_uncreatable'), hsc($farmdir)); + } elseif(!is_writeable($farmdir)) { + $errors[] = sprintf($this->getLang('farmdir_unwritable'), hsc($farmdir)); + } elseif(count(scandir($farmdir)) > 2) { + $errors[] = sprintf($this->getLang('farmdir_notEmpty'), hsc($farmdir)); + } + + if($errors) { + foreach($errors as $error) { + msg($error, -1); + } + return; + } + + // create the files + $ok = $this->createPreloadPHP(); + if($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess(); + if($ok) $ok &= $this->createFarmIni($farmdir); + + if($ok) { + msg($this->getLang('preload creation success'), 1); + $link = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&'); + send_redirect($link); + } else { + msg($this->getLang('preload creation error'), -1); + } + } + + /** + * Render HTML output, e.g. helpful text and a form + */ + public function html() { + // Is preload.php already enabled? + if(file_exists(DOKU_INC . 'inc/preload.php')) { + msg($this->getLang('overwrite_preload'), -1); + } + + $form = new \dokuwiki\Form\Form(); + $form->addClass('plugin_farmer'); + $form->addFieldsetOpen($this->getLang('preloadPHPForm')); + $form->addTextInput('farmdir', $this->getLang('farm dir')); + $form->addCheckbox('htaccess', $this->getLang('htaccess setup'))->attr('checked', 'checked'); + $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit'); + $form->addFieldsetClose(); + echo $form->toHTML(); + + } + + /** + * Creates the preload that loads our farm controller + * @return bool true if saving was successful + */ + protected function createPreloadPHP() { + $content = "<?php\n"; + $content .= "# farm setup by farmer plugin\n"; + $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n"; + $content .= " include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n"; + $content .= "}\n"; + return io_saveFile(DOKU_INC . 'inc/preload.php', $content); + } + + /** + * Prepends the needed config to the main .htaccess for htaccess type setups + * + * @return bool true if saving was successful + */ + protected function createHtaccess() { + // load existing or template + if(file_exists(DOKU_INC . '.htaccess')) { + $old = io_readFile(DOKU_INC . '.htaccess'); + } elseif(file_exists(DOKU_INC . '.htaccess.dist')) { + $old = io_readFile(DOKU_INC . '.htaccess.dist'); + } else { + $old = ''; + } + + $content = "# Options added for farm setup by farmer plugin:\n"; + $content .= "RewriteEngine On\n"; + $content .= 'RewriteRule ^!([^/]+)/(.*) $2?animal=$1 [QSA,DPI]' . "\n"; + $content .= 'RewriteRule ^!([^/]+)$ ?animal=$1 [QSA,DPI]' . "\n"; + $content .= 'Options +FollowSymLinks' . "\n"; + $content .= '# end of farm configuration' . "\n\n"; + $content .= $old; + return io_saveFile(DOKU_INC . '.htaccess', $content); + } + + /** + * Creates the initial configuration + * + * @param $animalpath + * @return bool true if saving was successful + */ + protected function createFarmIni($animalpath) { + $content = "; farm config created by the farmer plugin\n\n"; + $content .= "[base]\n"; + $content .= "farmdir = $animalpath\n"; + $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n"; + return io_saveFile(DOKU_INC . 'conf/farm.ini', $content); + } +} + +// vim:ts=4:sw=4:et: |