summaryrefslogblamecommitdiffstats
path: root/modules-available/vmstore/page.inc.php
blob: 6ef046699992538f3807b43e4dda7d14b6f024c8 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11










                                                         
                                                                













                                                                                         
                                                           







                                                                             




                                                              
                                                                                                                                     



                                                                              
                                                                                    



                                                                                                
                                                                                                



                                                                                                   
                                                                                                

                                                      
                                                            


                                                                                                    


         
<?php

class Page_VmStore extends Page
{
	private $mountTask = false;

	protected function doPreprocess()
	{
		User::load();

		if (!User::hasPermission('superadmin')) {
			Message::addError('main.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('mount', array(
				'task' => $this->mountTask['id']
			));
			return;
		}
		$vmstore = Property::getVmStoreConfig();
		if (isset($vmstore['storetype'])) {
			$vmstore['pre-' . $vmstore['storetype']] = 'checked';
		}
		Render::addTemplate('page-vmstore', $vmstore);
	}
	
	private function setStore()
	{
		foreach (array('storetype', 'nfsaddr', 'cifsaddr', 'cifsuser', 'cifspasswd', 'cifsuserro', 'cifspasswdro') as $key) {
			$vmstore[$key] = trim(Request::post($key, ''));
		}
		$storetype = $vmstore['storetype'];
		if (!in_array($storetype, array('internal', 'nfs', 'cifs'))) {
			Message::addError('main.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('main.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('main.value-invalid', 'nfsaddr', $vmstore['nfsaddr']);
			Util::redirect('?do=VmStore');
		}
		$this->mountTask = Trigger::mount($vmstore);
		if ($this->mountTask !== false) {
			TaskmanagerCallback::addCallback($this->mountTask, 'manualMount', $vmstore);
		}
	}

}