$field_error) { if(empty($source[$field_name])) { $errors[] = $field_error; } } return $errors; } function array_add(&$array, $value) { /* Why use a separate function to add an item to an array if the $name[] construct exists? * We only want to add an element to an array if the element isn't in the array yet, hence * the need for a custom function. * NOTE: This function works in-place. */ if(in_array($value, $array) === false) { $array[] = $value; } } function validate_hostname($hostname) { if(preg_match("/^[a-z\d](-*[a-z\d])*(\.[a-z\d](-*[a-z\d])*)*$/", $hostname)) { return true; } else { return false; } } function first_unused_ctid() { /* [OpenVZ only] This function finds the first valid unused CTID and returns it. */ global $database; $id_list = array(); $highest = 101; /* Collect all known CTIDs and keep track of the highest CTID. */ $result = $database->CachedQuery("SELECT `InternalId` FROM containers WHERE `VirtualizationType` = 1", array(), 0); foreach($result->data as $row) { $id = filter_var($row['InternalId'] ,FILTER_VALIDATE_INT); if($id !== false) { $id_list[] = $id; if($id > $highest) { $highest = $id; } } } /* Generate a list of all possible CTIDs between 101 and the highest CTID, and find * all possible CTIDs that do not exist in the known CTID list. We use array_merge * because otherwise the array indexes may not start from 0. */ $all_ids = range(101, $highest, 1); $missing = array_merge(array_diff($all_ids, $id_list)); if(count($missing) > 0) { /* Return the first unused CTID. */ return $missing[0]; } else { /* All CTIDs up to the highest CTID have been used. We'll just return the CTID * that is one above the highest known CTID. */ return $highest + 1; } } function format_size($input, $multiplier = 1024, $group = false, $decimal_places = 0, $return_array = false) { if($input > pow($multiplier, 8)) { $unit = "Y"; $number = $input / pow($multiplier, 8); } elseif($input > pow($multiplier, 7)) { $unit = "Z"; $number = $input / pow($multiplier, 7); } elseif($input > pow($multiplier, 6)) { $unit = "E"; $number = $input / pow($multiplier, 6); } elseif($input > pow($multiplier, 5)) { $unit = "P"; $number = $input / pow($multiplier, 5); } elseif($input > pow($multiplier, 4)) { $unit = "T"; $number = $input / pow($multiplier, 4); } elseif($input > pow($multiplier, 3)) { $unit = "G"; $number = $input / pow($multiplier, 3); } elseif($input > pow($multiplier, 2)) { $unit = "M"; $number = $input / pow($multiplier, 2); } elseif($input > $multiplier) { $unit = "K"; $number = $input / $multiplier; } else { $unit = ""; $number = $input; } if($group === true) { $number = number_format($number, $decimal_places); } else { $number = round($number, $decimal_places); } if($return_array == true) { return array($number, $unit); } else { return $number . $unit; } }