diff options
Diffstat (limited to 'modules/vmstore.inc.php')
-rw-r--r-- | modules/vmstore.inc.php | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/modules/vmstore.inc.php b/modules/vmstore.inc.php new file mode 100644 index 00000000..ab06b1af --- /dev/null +++ b/modules/vmstore.inc.php @@ -0,0 +1,72 @@ +<?php + +class Page_VmStore extends Page +{ + private $mountTask = false; + + protected function doPreprocess() + { + User::load(); + + if (!User::hasPermission('superadmin')) { + Message::addError('no-permission'); + Util::redirect('?do=Main'); + } + + $action = Request::post('action'); + + if ($action === 'setstore') { + $this->setStore(); + } + } + + protected function doRender() + { + $action = Request::post('action'); + if ($action === 'setstore' && !Taskmanager::isFailed($this->mountTask)) { + Render::addTemplate('vmstore/mount', array( + 'task' => $this->mountTask['id'] + )); + return; + } + $vmstore = Property::getVmStoreConfig(); + if (isset($vmstore['storetype'])) { + $vmstore['pre-' . $vmstore['storetype']] = 'checked'; + } + $vmstore['token'] = Session::get('token'); + Render::addTemplate('page-vmstore', $vmstore); + } + + private function setStore() + { + foreach (array('storetype', 'nfsaddr', 'cifsaddr', 'cifsuser', 'cifspasswd') as $key) { + $vmstore[$key] = trim(Request::post($key, '')); + } + $storetype = $vmstore['storetype']; + if (!in_array($storetype, array('internal', 'nfs', 'cifs'))) { + Message::addError('value-invalid', 'type', $storetype); + Util::redirect('?do=VmStore'); + } + // Validate syntax of nfs/cifs + if ($storetype === 'nfs' && !preg_match('#^\S+:\S+$#is', $vmstore['nfsaddr'])) { + Message::addError('value-invalid', 'nfsaddr', $vmstore['nfsaddr']); + Util::redirect('?do=VmStore'); + } + $vmstore['cifsaddr'] = str_replace('\\', '/', $vmstore['cifsaddr']); + if ($storetype === 'cifs' && !preg_match('#^//\S+/.+$#is', $vmstore['cifsaddr'])) { + Message::addError('value-invalid', 'nfsaddr', $vmstore['nfsaddr']); + Util::redirect('?do=VmStore'); + } + if ($storetype === 'nfs') $addr = $vmstore['nfsaddr']; + if ($storetype === 'cifs') $addr = $vmstore['nfsaddr']; + if ($storetype === 'internal') $addr = 'none'; + $this->mountTask = Taskmanager::submit('MountVmStore', array( + 'address' => $addr, + 'type' => 'images', + 'username' => $vmstore['cifsuser'], + 'password' => $vmstore['cifspasswd'] + )); + Property::setVmStoreConfig($vmstore); + } + +}
\ No newline at end of file |