* @desc Process and render animal management related requests */ // must be run under farm plugin context within Dokuwiki or SOAP context if(!defined('DOKU_FARMPLUGINLOADED') && !defined('DOKU_FARMPLUGINSOAPACTION')) die(); if(!defined('DOKU_FARM_PLUGIN')) define('DOKU_FARM_PLUGIN', defined('DOKU_FARMPLUGINLOADED') ? DOKU_INC.'lib/plugins/farm/' : './'); require_once(DOKU_FARM_PLUGIN.'animal.class.php'); class dokuwiki_farm_animalmanager { var $manager = null; private $animal = null; /** * @param $manager object that must handle error(), success(), nicesize(), getLang() ... calls */ function __construct($manager) { $this->manager = & $manager; } /** * Builds and return a link inside animal manager * @param $opts array of farm options * @return url string */ function wl($opts = array()) { $a = array_merge(array('aid' => $this->animal->getName()), $opts); return $this->manager->wl('animal', $a); } /** * Process requests */ public function process() { // Animal creation request if(isset($this->manager->opt['new'])) { if(isset($this->manager->opt['save'])) { if(!checkSecurityToken()) { $this->manager->error('system_errors', 'system_badtoken_failure'); return; // any changes done by post } if(isset($_POST['animal_name']) && !empty($_POST['animal_name'])) { $tpl = (isset($_POST['animal_template']) && !empty($_POST['animal_template'])) ? $_POST['animal_template'] : null; $host = (isset($_POST['animal_host']) && !empty($_POST['animal_host'])) ? $_POST['animal_host'] : null; if($n = dokuwiki_farm_animal::createNew($_POST['animal_name'], $tpl, $host, $this)) { $this->manager->success('animal_new_create_success'); unset($this->manager->opt['save']); unset($this->manager->opt['new']); $this->manager->opt['aid'] = $n; } }else{ $this->manager->error('animal_new_errors', 'animal_new_noname_failure'); return; } }else return; } // Animal selected check if(!isset($this->manager->opt['aid'])) { $this->manager->error('animal_errors', 'animal_noid_failure'); return; } if(!dokuwiki_farm_animal::exists($this->manager->opt['aid'])) { $this->manager->error('animal_errors', array('code' => 'animal_unknownanimal_failure', 'data' => array($this->manager->opt['aid']))); return; } $this->animal = new dokuwiki_farm_animal($this->manager->opt['aid'], $this->manager); // Options check if( !isset($this->manager->opt['status']) && !isset($this->manager->opt['lockstate']) && !(isset($this->manager->opt['users']) && (isset($this->manager->opt['save']) || isset($this->manager->opt['deleteuser']))) && !(isset($this->manager->opt['editfile']) && isset($this->manager->opt['save']) && isset($this->manager->opt['file']) && !empty($this->manager->opt['file'])) && !(isset($this->manager->opt['editfile']) && isset($this->manager->opt['create']) && isset($this->manager->opt['newfile']) && !empty($this->manager->opt['file'])) && !(isset($this->manager->opt['delete']) && (isset($this->manager->opt['confirm']) || isset($this->manager->opt['cancel']))) ) return; // Security check if(!checkSecurityToken()) { $this->manager->error('system_errors', 'system_badtoken_failure'); return; // any changes done by post } // Status change request if(isset($this->manager->opt['status'])) { $s = $this->manager->opt['status']; if(in_array($s, array('open', 'maintenance', 'closed', 'abuse'))) { if(!$this->animal->setMetadata('status', $s)) { $this->manager->error('animal_status_errors', array('code' => 'animal_status_update_failure', 'data' => array($s))); }else $this->manager->success('animal_status_update_success'); } } if(isset($this->manager->opt['lockstate'])) { $s = $this->manager->opt['lockstate']; if(in_array($s, array('', 'edits', 'admin', 'all'))) { if(!$this->animal->setMetadata('lockstate', $s)) { $this->manager->error('animal_status_errors', array('code' => 'animal_status_update_failure', 'data' => array($s))); }else $this->manager->success('animal_status_update_success'); } } // User create request if(isset($this->manager->opt['users']) && isset($this->manager->opt['save'])) { if(!isset($_POST['new_animal_user_login']) || !isset($_POST['new_animal_user_pass']) || !isset($_POST['new_animal_user_name']) || !isset($_POST['new_animal_user_mail']) || !isset($_POST['new_animal_user_grps'])) { $this->manager->error('animal_users_errors', 'postparametermissing_failure'); }elseif(empty($_POST['new_animal_user_login'])) { $this->manager->error('animal_users_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('animal_users_login')))); }elseif(empty($_POST['new_animal_user_pass'])) { $this->manager->error('animal_users_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('animal_users_pass')))); }elseif(empty($_POST['new_animal_user_mail'])) { $this->manager->error('animal_users_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('animal_users_mail')))); }else{ if(empty($_POST['new_animal_user_name'])) $_POST['new_animal_user_name'] = $_POST['new_animal_user_login']; $_POST['new_animal_user_login'] = preg_replace('/.*:/','',$_POST['new_animal_user_login']); $_POST['new_animal_user_login'] = cleanID($_POST['new_animal_user_login']); $_POST['new_animal_user_name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['new_animal_user_name'])); $_POST['new_animal_user_mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['new_animal_user_mail'])); if(empty($_POST['new_animal_user_login'])) { $this->manager->error('animal_users_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('animal_users_login')))); }elseif(empty($_POST['new_animal_user_pass'])) { $this->manager->error('animal_users_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('animal_users_pass')))); }elseif(empty($_POST['new_animal_user_name'])) { $this->manager->error('animal_users_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('animal_users_name')))); }elseif(empty($_POST['new_animal_user_mail']) || !mail_isvalid($_POST['new_animal_user_mail'])) { $this->manager->error('animal_users_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('animal_users_mail')))); }elseif(!preg_match('`^\s*[a-z0-9_-]+(\s*,\s*[a-z0-9_-]+)*\s*$`i', $_POST['new_animal_user_grps'])) { $this->manager->error('animal_users_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('animal_users_grps')))); }else{ if($this->animal->addUser($_POST['new_animal_user_login'], $_POST['new_animal_user_pass'], $_POST['new_animal_user_name'], $_POST['new_animal_user_mail'], array_map('trim', explode(',', $_POST['new_animal_user_grps'])))) { $this->manager->success('animal_users_add_success'); }else $this->manager->error('animal_users_errors', 'animal_users_add_create_failure'); } } } // User delete request if(isset($this->manager->opt['users']) && isset($this->manager->opt['deleteuser'])) { if($this->animal->deleteUser(base64_decode($this->manager->opt['deleteuser']))) { $this->manager->success('animal_users_delete_success'); }else $this->manager->error('animal_users_errors', 'animal_users_delete_failure'); } // File edition request if(isset($this->manager->opt['editfile']) && isset($this->manager->opt['save']) && isset($this->manager->opt['file']) && !empty($this->manager->opt['file'])) { if(isset($this->manager->opt['editfile_delete'])) { if($this->animal->setFileContent(base64_decode($this->manager->opt['file']), '', true)) { $this->manager->success('animal_editfile_delete_success'); $this->manager->opt['file'] = null; }else $this->manager->error('animal_editfile_errors', 'animal_editfile_delete_failure'); }elseif(isset($_POST['animal_file']) && (isset($this->manager->opt['editfile_saveempty']) || !empty($_POST['animal_file']))) { if($this->animal->setFileContent(base64_decode($this->manager->opt['file']), isset($this->manager->opt['editfile_saveempty']) ? '' : $_POST['animal_file'])) { $this->manager->success('animal_editfile_save_success'); }else $this->manager->error('animal_editfile_errors', 'animal_editfile_save_failure'); }else $this->manager->error('animal_editfile_errors', 'animal_editfile_save_failure'); } if(isset($this->manager->opt['editfile']) && isset($this->manager->opt['create']) && isset($this->manager->opt['newfile']) && !empty($this->manager->opt['file'])) { if($this->animal->setFileContent($this->manager->opt['file'], '')) { $this->manager->success('animal_editfile_save_success'); $this->manager->opt['file'] = base64_encode($this->manager->opt['file']); }else $this->manager->error('animal_editfile_errors', 'animal_editfile_save_failure'); } // Animal delete request if(isset($this->manager->opt['delete']) && isset($this->manager->opt['cancel'])) { if(isset($this->manager->opt['confirm'])) unset($this->manager->opt['confirm']); unset($this->manager->opt['delete']); } if(isset($this->manager->opt['delete']) && isset($this->manager->opt['confirm'])) { if($this->animal->getName() == $this->manager->conf['animaltemplate']) { $this->manager->error('animal_delete_errors', array('code' => 'animal_delete_tpl_failure', 'data' => $this->manager->conf['animaltemplate'])); }elseif($this->animal->delete()) { $this->manager->success('animal_delete_success'); $this->animal = null; unset($this->manager->opt['delete']); unset($this->manager->opt['confirm']); unset($this->manager->opt['aid']); }else $this->manager->error('animal_delete_errors', array('code' => 'animal_delete_failure', 'data' => $this->animal->getName())); } } /** * Renders header outputed before error / success section */ public function htmlheader() { $displaydefault = !isset($this->manager->opt['config']) && !isset($this->manager->opt['users']) && !isset($this->manager->opt['editfile']) && !isset($this->manager->opt['delete']); // Pannel menu if($this->animal) { ptln('
'.$this->manager->getLang('animal_users_login').' | '); ptln(''.$this->manager->getLang('animal_users_name').' | '); ptln(''.$this->manager->getLang('animal_users_mail').' | '); ptln(''.$this->manager->getLang('animal_users_grps').' | '); ptln(''); ptln(' |
---|---|---|---|---|
'.$l.' | '); ptln(''.$u['name'].' | '); ptln(''.$u['mail'].' | '); ptln(''.implode(', ', $grps).' | '); ptln(''); ptln(' |