* @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('
'); ptln(' infos'); ptln(' manager->opt['users']) ? 'class="current_action"' : '').' href="'.$this->wl(array('users' => true)).'" title="'.$this->manager->getLang('animal_users').'">users'); ptln(' manager->opt['editfile']) ? 'class="current_action"' : '').' href="'.$this->wl(array('editfile' => true)).'" title="'.$this->manager->getLang('animal_editfile').'">edit file'); ptln(' manager->opt['delete']) ? 'class="current_action"' : '').' href="'.$this->wl(array('delete' => true)).'" title="'.$this->manager->getLang('animal_delete').'">delete animal'); ptln(' visit'); ptln('
'); } } /** * Renders */ public function html() { $displaydefault = !isset($this->manager->opt['config']) && !isset($this->manager->opt['users']) && !isset($this->manager->opt['editfile']) && !isset($this->manager->opt['delete']); // Animal infos if($displaydefault && $this->animal) { ptln('
'); if($this->manager->conf['animalmaxsize'] > 0) { $s = $this->animal->getInfos()->getSize('all', false); $p = $s / $this->manager->conf['animalmaxsize']; if($p > 1) $p == 1; $g = min(255, 511 - round($p * 511)); $r = min(255, round(511 * $p)); ptln(' '.$this->manager->getLang('animal_info_size').' : '.round(100 * $p).'% ('.$this->manager->nicesize($s).' / '.$this->manager->nicesize($this->manager->conf['animalmaxsize']).')
'); } if($this->manager->conf['animalmaxsize'] <= 0) ptln(' '.$this->manager->getLang('animal_info_size').' : '.$this->animal->getInfos()->getSize('all', true).'
'); $status = $this->animal->getInfos()->getStatus(); if($status == '') $status = 'open'; ptln(' '.$this->manager->getLang('animal_info_status').' :
'); $lockstate = $this->animal->getInfos()->getLockState(); ptln(' '.$this->manager->getLang('animal_info_lockstate').' :
'); ptln(' '.$this->manager->getLang('animal_info_creation').' : '.$this->animal->getInfos()->getCreation(true).'
'); ptln('
'); foreach($this->animal->getInfos()->pagesInfo(true) as $k => $i) { ptln(' '.$this->manager->getLang('animal_info_pages'.$k).' : '.$i.'
'); } ptln('
'); foreach($this->animal->getInfos()->mediasInfo(true) as $k => $i) { ptln(' '.$this->manager->getLang('animal_info_medias'.$k).' : '.$i.'
'); } ptln('
'); ptln(' '.$this->manager->getLang('animal_info_cachesize').' : '.$this->animal->getInfos()->getSize('cache', true).'
'); ptln(' '.$this->manager->getLang('animal_info_indexsize').' : '.$this->animal->getInfos()->getSize('index', true).'
'); ptln('
'); } // Animal users if(isset($this->manager->opt['users']) && $this->animal) { ptln('
'); ptln(' '.$this->manager->getLang('animal_users')); ptln('
'); ptln('
'); ptln(' '.$this->manager->getLang('animal_users_warning')); ptln('
'); $sort = isset($this->manager->opt['sort']) ? $this->manager->opt['sort'] : 'name'; $grp = isset($this->manager->opt['grp']) ? $this->manager->opt['grp'] : null; $users = $this->animal->getUsers(); switch($sort) { case 'name' : uasort($users, create_function('$a,$b', 'return strcmp($a[\'name\'], $b[\'name\']);')); break; case 'namedesc' : uasort($users, create_function('$a,$b', 'return -1 * strcmp($a[\'name\'], $b[\'name\']);')); break; } if($grp) ptln($this->manager->getLang('animal_users_onlygrpactive').' : '.$grp.' [X]'); ptln(''); ptln(' '); ptln(' '); ptln(' '); ptln(' '); ptln(' '); ptln(' '); ptln(' '); foreach($this->animal->getUsers() as $l => $u) { $grps = array(); if($grp && !in_array($grp, $u['grps'])) continue; foreach($u['grps'] as $g) $grps[] = ''.$g.''; ptln(' '); ptln(' '); ptln(' '); ptln(' '); ptln(' '); ptln(' '); ptln(' '); } ptln('
'.$this->manager->getLang('animal_users_login').' sort'.$this->manager->getLang('animal_users_name').''.$this->manager->getLang('animal_users_mail').''.$this->manager->getLang('animal_users_grps').' 
'.$l.''.$u['name'].''.$u['mail'].''.implode(', ', $grps).'
'); $this->manager->formHead(array('farm_cmd' => 'animal', 'farm_opt[aid]' => $this->animal->getName(), 'farm_opt[users]' => true)); ptln('
'.$this->manager->getLang('animal_users_add').'
'); ptln(' '.$this->manager->getLang('animal_users_login').' :
'); ptln(' '.$this->manager->getLang('animal_users_pass').' :
'); ptln(' '.$this->manager->getLang('animal_users_name').' :
'); ptln(' '.$this->manager->getLang('animal_users_mail').' :
'); ptln(' '.$this->manager->getLang('animal_users_grps').' :
'); ptln(' '); ptln(''); } // Animal edit file if(isset($this->manager->opt['editfile']) && $this->animal) { ptln('
'); ptln(' '.$this->manager->getLang('animal_editfile')); ptln('
'); ptln('
'); ptln(' '.$this->manager->getLang('animal_change_warning')); ptln('
'); $file = isset($this->manager->opt['file']) ? base64_decode($this->manager->opt['file']) : null; $file = @file_exists($this->animal->getPath().'/'.$file) ? $file : null; $f64 = $file ? base64_encode($file) : null; ptln('
'); ptln(' '.$this->manager->getLang('animal_editfile_fs', $this->animal->getName())); foreach($this->animal->getFs() as $r64 => $i) { ptln('
'); if($i['type'] == 'file') ptln(''); ptln($i['name']); if($i['type'] == 'file') ptln(''); if($i['type'] == 'file') { ptln('
('.$this->animal->getInfos()->formatedprint($this->manager->getLang('file_info'), array('size' => $i['size'], 'date' => $i['mtime'], 'dformat' => $conf['dformat'])).')
'); } ptln('
'); } ptln('
'); $this->manager->formHead(array('farm_cmd' => 'animal', 'farm_opt[aid]' => $this->animal->getName(), 'farm_opt[editfile]' => true, 'farm_opt[newfile]' => true)); ptln($this->manager->getLang('animal_editfile_create').' : '); ptln(' '); ptln(''); if($file) { $this->manager->formHead(array('farm_cmd' => 'animal', 'farm_opt[aid]' => $this->animal->getName(), 'farm_opt[editfile]' => true, 'farm_opt[file]' => base64_encode($file))); ptln(' '.$this->manager->getLang('animal_editfile_file', $file).'
'); ptln('
'); ptln(' '.$this->manager->getLang('animal_editfile_saveempty').'
'); ptln(' '.$this->manager->getLang('animal_editfile_delete').'
'); ptln(' '); ptln(''); } } // New animal if(isset($this->manager->opt['new'])) { ptln('
'); ptln(' '.$this->manager->getLang('animal_new_title')); ptln('
'); $this->manager->formHead(array('farm_cmd' => 'animal', 'farm_opt[new]' => 1)); ptln(' '.$this->manager->getLang('animal_new_name').' : '.$this->manager->conf['farmwebroot'].'
'); ptln(' '.$this->manager->getLang('animal_new_template').' :
'); if($this->manager->conf['virtual']) ptln(' '.$this->manager->getLang('animal_new_host').' :
'); ptln(' '); ptln(''); } // Delete animal if(isset($this->manager->opt['delete'])) { ptln('
'); ptln(' '.$this->manager->getLang('animal_delete')); ptln('
'); $this->manager->formHead(array('farm_cmd' => 'animal', 'farm_opt[aid]' => $this->animal->getName(), 'farm_opt[delete]' => 1)); ptln(' '.$this->manager->getLang('animal_delete_sure', $this->animal->getName()).' '); ptln(''); } ptln(''); } } ?>