Exemple #1
0
function processRequestInput()
{
    global $user;
    $baseaccess = getContinuationVar('baseaccess', 0);
    $imagingaccess = getContinuationVar('imagingaccess', 0);
    $serveraccess = getContinuationVar('serveraccess', 0);
    $openend = getContinuationVar('openend', 0);
    $nousercheck = getContinuationVar('nousercheck', 0);
    $return['imaging'] = getContinuationVar('imaging', 0);
    $maxinitial = getContinuationVar('maxinitial', 0);
    $noimaging = getContinuationVar('noimaging', array());
    $return = array('err' => 0);
    # type
    $return['type'] = processInputVar('type', ARG_STRING);
    if (!preg_match('/^basic|imaging|server$/', $return['type'])) {
        $return['err'] = 1;
        $return['errmsg'] = i('Invalid data submitted');
        return $return;
    }
    if ($return['type'] == 'basic' && !$baseaccess || $return['type'] == 'imaging' && !$imagingaccess || $return['type'] == 'server' && !$serveraccess) {
        $return['err'] = 1;
        $return['errmsg'] = i('No access to submitted reservation type');
        return $return;
    }
    # ending
    $return['ending'] = processInputVar('ending', ARG_STRING);
    if (!preg_match('/^indefinite|endat|duration$/', $return['ending'])) {
        $return['err'] = 1;
        $return['errmsg'] = i('Invalid data submitted');
        return $return;
    }
    if ($return['ending'] == 'duration' && !$baseaccess || $return['ending'] == 'indefinite' && !$serveraccess || $return['ending'] == 'endat' && !$openend && !$serveraccess) {
        $return['err'] = 1;
        $return['errmsg'] = i('No access to submitted end type');
        return $return;
    }
    # imageid
    $return['imageid'] = processInputVar('imageid', ARG_NUMERIC);
    $resources = getUserResources(array("imageAdmin", "imageCheckOut"));
    $withnocheckout = $resources['image'];
    $images = removeNoCheckout($resources["image"]);
    $extraimages = getServerProfileImages($user['id']);
    if (!array_key_exists($return['imageid'], $images) && ($return['type'] != 'server' || !array_key_exists($return['imageid'], $extraimages)) && ($return['type'] != 'imaging' || !array_key_exists($return['imageid'], $withnocheckout)) || $return['type'] == 'imaging' && array_key_exists($return['imageid'], $noimaging)) {
        $return['err'] = 1;
        $return['errmsg'] = i('No access to submitted environment');
        return $return;
    }
    # nousercheck
    $return['nousercheck'] = processInputVar('nousercheck', ARG_NUMERIC);
    if (!$nousercheck || $return['nousercheck'] != 1) {
        $return['nousercheck'] = 0;
    }
    # revisionid
    $revids = processInputVar("revisionid", ARG_STRING);
    $revids = explode(':', $revids);
    $images = getImages(0, $return['imageid']);
    $return['revisionids'] = array();
    if (array_key_exists('subimages', $images[$return['imageid']])) {
        $subimages = $images[$return['imageid']]['subimages'];
        array_unshift($subimages, $return['imageid']);
        foreach ($subimages as $key => $imgid) {
            $revisions = getImageRevisions($imgid);
            if (!array_key_exists($key, $revids) || !is_numeric($revids[$key]) || !array_key_exists($revids[$key], $revisions)) {
                $revid = getProductionRevisionid($imgid);
            } else {
                $revid = $revids[$key];
            }
            if (!array_key_exists($imgid, $return['revisionids'])) {
                $return['revisionids'][$imgid] = array();
            }
            $return['revisionids'][$imgid][] = $revid;
        }
    } elseif ($revids[0] != '' && is_numeric($revids[0])) {
        $return['revisionids'][$return['imageid']][] = $revids[0];
    } else {
        $return['revisionids'][$return['imageid']][] = getProductionRevisionid($return['imageid']);
    }
    # duration
    if ($return['ending'] == 'duration') {
        $return['duration'] = processInputVar('duration', ARG_NUMERIC, 0);
        if ($return['duration'] > $maxinitial) {
            $return['duration'] = $maxinitial;
        }
    }
    # start/end
    $return['start'] = processInputVar('start', ARG_NUMERIC);
    $return['end'] = processInputVar('end', ARG_NUMERIC, 0);
    $now = time();
    if ($return['start'] == 0) {
        $start = $now;
    } else {
        $start = $return['start'];
    }
    if ($return['ending'] == 'endat') {
        $end = $return['end'];
    }
    if ($return['ending'] == 'indefinite') {
        $end = datetimeToUnix('2038-01-01 00:00:00');
    } elseif ($return['ending'] == 'duration') {
        $end = $start + $return['duration'] * 60;
    }
    if ($start < $now) {
        $return['err'] = 1;
        $return['errmsg'] = i('The submitted start time is in the past.');
        return $return;
    }
    if ($start + 900 > $end) {
        $return['err'] = 1;
        $return['errmsg'] = i('The end time must be at least 15 minutes later than the start time.');
        return $return;
    }
    $return['ipaddr'] = '';
    $return['macaddr'] = '';
    # server specific input
    if ($return['type'] == 'server') {
        # name
        $return['name'] = processInputVar('name', ARG_STRING);
        if (!preg_match('/^([-a-zA-Z0-9_\\. ]){0,255}$/', $return['name'])) {
            $return['err'] = 1;
            $return['errmsg'] = i('The reservation name can only contain letters, numbers, spaces, dashes(-), underscores(_), and periods(.) and can be up to 255 characters long');
            return $return;
        }
        # ipaddr
        $return['ipaddr'] = processInputVar('ipaddr', ARG_STRING);
        if ($return['ipaddr'] != '') {
            # validate fixed IP address
            if (!validateIPv4addr($return['ipaddr'])) {
                $return['err'] = 1;
                $return['errmsg'] = i('Invalid IP address. Must be w.x.y.z with each of w, x, y, and z being between 1 and 255 (inclusive)');
                return $return;
            }
            # validate netmask
            $return['netmask'] = processInputVar('netmask', ARG_STRING);
            $bnetmask = ip2long($return['netmask']);
            if (!preg_match('/^[1]+0[^1]+$/', sprintf('%032b', $bnetmask))) {
                $return['err'] = 1;
                $return['errmsg'] = i('Invalid netmask specified');
                return $return;
            }
            # validate router
            $return['router'] = processInputVar('router', ARG_STRING);
            if (!validateIPv4addr($return['router'])) {
                $return['err'] = 1;
                $return['errmsg'] = i('Invalid router address. Must be w.x.y.z with each of w, x, y, and z being between 1 and 255 (inclusive)');
                return $return;
            }
            $return['network'] = ip2long($return['ipaddr']) & $bnetmask;
            if ($return['network'] != (ip2long($return['router']) & $bnetmask)) {
                $return['err'] = 1;
                $return['errmsg'] = i('IP address and router are not on the same subnet based on the specified netmask.');
                return $return;
            }
            # validate dns server(s)
            $dns = processInputVar('dns', ARG_STRING);
            $tmp = explode(',', $dns);
            $cnt = 0;
            $return['dnsArr'] = array();
            foreach ($tmp as $dnsaddr) {
                if ($cnt && $dnsaddr == '') {
                    continue;
                }
                if ($cnt == 3) {
                    $return['err'] = 1;
                    $return['errmsg'] = i('Too many DNS servers specified - up to 3 are allowed.');
                    return $return;
                }
                if (!validateIPv4addr($dnsaddr)) {
                    $return['err'] = 1;
                    $return['errmsg'] = i('Invalid DNS server specified.');
                    return $return;
                }
                $return['dnsArr'][] = $dnsaddr;
                $cnt++;
            }
            # check that a management node can handle the network
            $mappedmns = getMnsFromImage($return['imageid']);
            $mnnets = checkAvailableNetworks($return['ipaddr']);
            $intersect = array_intersect($mappedmns, $mnnets);
            if (empty($intersect)) {
                $return['err'] = 1;
                $return['errmsg'] = i('There are no management nodes that can deploy the selected image with the specified IP address.');
                return $return;
            }
        }
        # macaddr
        $return['macaddr'] = processInputVar('macaddr', ARG_STRING);
        if ($return['macaddr'] != '' && !preg_match('/^(([A-Fa-f0-9]){2}:){5}([A-Fa-f0-9]){2}$/', $return['macaddr'])) {
            $return['err'] = 1;
            $return['errmsg'] = i('Invalid MAC address. Must be XX:XX:XX:XX:XX:XX with each pair of XX being from 00 to FF (inclusive)');
            return $return;
        }
        # profileid
        $return['profileid'] = processInputVar('profileid', ARG_NUMERIC, 0);
        $resources = getUserResources(array("serverCheckOut", "serverProfileAdmin"), array("available", "administer"));
        if (!array_key_exists($return['profileid'], $resources['serverprofile'])) {
            $return['profileid'] = 0;
        } elseif ($return['profileid'] != 0) {
            $tmp = getServerProfiles($return['profileid']);
            $tmp = $tmp[$return['profileid']];
            if ($tmp['imageid'] != $return['imageid'] && ($tmp['fixedIP'] != $return['ipaddr'] && $tmp['fixedMAC'] != $return['macaddr'] || $tmp['fixedIP'] == $return['ipaddr'] && $return['ipaddr'] == '' && $tmp['fixedMAC'] == $return['macaddr'] && $return['macaddr'] == '')) {
                $return['profileid'] = 0;
            }
        }
        # admingroupid
        $usergroups = getUserGroups();
        $return['admingroupid'] = processInputVar('admingroupid', ARG_NUMERIC);
        if ($return['admingroupid'] != 0 && !array_key_exists($return['admingroupid'], $usergroups)) {
            $return['err'] = 1;
            $return['errmsg'] = i('You do not have access to use the specified admin user group.');
            return $return;
        }
        # logingroupid
        $return['logingroupid'] = processInputVar('logingroupid', ARG_NUMERIC);
        if ($return['logingroupid'] != 0 && !array_key_exists($return['logingroupid'], $usergroups)) {
            $return['err'] = 1;
            $return['errmsg'] = i('You do not have access to use the specified access user group.');
            return $return;
        }
        # monitored
        $return['monitored'] = processInputVar('monitored', ARG_NUMERIC, 0);
        if ($return['monitored'] != 0 && $return['monitored'] != 1) {
            $return['monitored'] = 0;
        }
        # configs
        # TODO configs
        /*$tmp = getUserResources(array("configAdmin"));
        		$userconfigs = $tmp['config'];
        		$initconfigs = getMappedConfigs($return['imageid']);
        		if(array_key_exists('configdata', $_POST)) {
        			if(get_magic_quotes_gpc())
        				$_POST['configdata'] = stripslashes($_POST['configdata']);
        			$configdata = json_decode($_POST['configdata']);
        		}
        		if(array_key_exists('configdata', $_POST) &&
        			isset($configdata->configs))
        			$configs = $configdata->configs;
        		else
        			$configs = (object)array();
        		$return['configs'] = array();
        		foreach($initconfigs as $id => $config) {
        			if(isset($configs->{$id}) &&
        				isset($configs->{$id}->applied) &&
        			   $configs->{$config['id']}->applied != 'true' &&
        				$configs->{$config['id']}->applied != 'false')
        				unset($configs->{$config['id']});
        			if($config['optional'] &&
        			   (! isset($configs->{$id}) ||
        			   ! $configs->{$id}->applied))
        				continue;
        			$return['configs'][$id] = array('configid' => $config['configid'],
        			                                'configmapid' => $config['configmapid'],
        			                                'imageid' => $config['subimageid']);
        			if(isset($configs->{$id}))
        				unset($configs->{$id});
        		}
        		$rescfgmapids = array();
        		foreach($configs as $id => $config) {
        			if(! array_key_exists($config->configid, $userconfigs))
        				continue;
        			$return['configs'][$id] = array('configid' => $config->configid,
        			                                'configstageid' => $config->configstageid,
        			                                'imageid' => $config->imageid);
        			$tmp = explode('/', $id);
        			$rescfgmapids[$tmp[1]] = 1;
        		}
        
        		# configvars
        		$tmp = array_splice($initconfigs, 0);
        		$initconfigvars = getImageConfigVariables($tmp);
        		if(array_key_exists('configdata', $_POST) &&
        			isset($configdata->configvars))
        			$configvars = $configdata->configvars;
        		else
        			$configvars = (object)array();
        		#print "/*";
        		#printArray($initconfigvars);
        		#printArray($configvars);
        		#print "*" . "/";
        		$return['configvars'] = array();
        		foreach($initconfigvars as $id => $configvar) {
        			$tmp = explode('/', $id);
        			$cfgid = "{$tmp[0]}/{$tmp[1]}";
        			$varid = $tmp[2];
        			if($configvar['ask'] == 0 ||
        			   ! isset($configvars->{$id}) ||
        			   ! isset($configvars->{$id}->value)) {
        				$return['configvars'][$cfgid][$varid] =
        				         array('value' => $configvar['defaultvalue']);
        			}
        			else {
        				switch($configvar['datatype']) {
        					case 'bool':
        					case 'int':
        					case 'float':
        						$value = processInputData($configvars->{$id}->value, ARG_NUMERIC);
        						break;
        					default:
        						$value = processInputData($configvars->{$id}->value, ARG_STRING);
        						break;
        				}
        				$return['configvars'][$cfgid][$varid] = array('value' => $value);
        			}
        			if(isset($configvars->{$id}))
        				unset($configvars->{$id});
        		}*/
        /*print "/*";
        		printArray($rescfgmapids);
        		foreach($configvars as $id => $var) {
        			$cfgid = explode('/', $id);
        			print "cfgid: {$cfgid[1]}\n";
        			if(! array_key_exists($cfgid[1], $rescfgmapids))
        				continue;
        			// TODO validate based on var type
        			$value = processInputData($configvars->{$id}->value, ARG_STRING);
        			$return['configvars']["{$cfgid[0]}/{$cfgid[1]}"][$cfgid[2]] = array('value' => $value);
        		}
        		printArray($configvars);*/
        #print "*/";
    }
    return $return;
}
function AJremProfileFromGroup()
{
    $groupid = processInputVar('id', ARG_NUMERIC);
    $groups = getUserResources(array("serverProfileAdmin"), array("manageGroup"), 1);
    if (!array_key_exists($groupid, $groups['serverprofile'])) {
        $arr = array('profiles' => array(), 'addrem' => 0);
        sendJSON($arr);
        return;
    }
    $resources = getUserResources(array("serverProfileAdmin"), array("manageGroup"));
    $tmp = processInputVar('listids', ARG_STRING);
    $tmp = explode(',', $tmp);
    $profileids = array();
    foreach ($tmp as $id) {
        if (!is_numeric($id)) {
            continue;
        }
        if (!array_key_exists($id, $resources['serverprofile'])) {
            $arr = array('profiles' => array(), 'addrem' => 0, 'id' => $id, 'extra' => $resources['serverprofile']);
            sendJSON($arr);
            return;
        }
        $profileids[] = $id;
    }
    $allprofiles = getServerProfiles();
    foreach ($profileids as $id) {
        $query = "DELETE FROM resourcegroupmembers " . "WHERE resourceid = {$allprofiles[$id]['resourceid']} AND " . "resourcegroupid = {$groupid}";
        doQuery($query, 288);
    }
    $arr = array('profiles' => $profileids, 'addrem' => 0, 'removedaccess' => 0);
    $_SESSION['userresources'] = array();
    $_SESSION['usersessiondata'] = array();
    $resources = getUserResources(array("serverProfileAdmin"), array("manageGroup"));
    foreach ($profileids as $id) {
        if (!array_key_exists($id, $resources['serverprofile'])) {
            $arr['removedaccess'] = 1;
            $arr['remprofileids'][] = $id;
        }
    }
    sendJSON($arr);
}