quota: allow k/m/g/t/K/M/G/T

This commit is contained in:
Roland Gruber 2017-10-21 21:37:40 +02:00
parent b3a5e0603e
commit c5a0be924c
4 changed files with 62 additions and 26 deletions

View File

@ -1,6 +1,7 @@
December 2017 December 2017
- PHP 5.6 and Internet Explorer 11 or later required - PHP 5.6 and Internet Explorer 11 or later required
- Account status also shows expired accounts - Account status also shows expired accounts
- Quota: support k/m/g/t/K/M/G/T to specify values in e.g. kB
19.09.2017 6.1 19.09.2017 6.1

View File

@ -583,6 +583,9 @@ function get_preg($argument, $regexp) {
case 'objectClass': case 'objectClass':
$pregexpr = '/^[[:alnum:]_-]+$/'; $pregexpr = '/^[[:alnum:]_-]+$/';
break; break;
case 'quotaNumber':
$pregexpr = '/^[[:digit:]]+[KMGTkmgt]?$/';
break;
} }
if ($pregexpr!='') if ($pregexpr!='')
if (preg_match($pregexpr, $argument)) { if (preg_match($pregexpr, $argument)) {

View File

@ -280,8 +280,11 @@ class quota extends baseModule {
$i=0; $i=0;
$quotastring = ""; $quotastring = "";
for ($i = 0; $i < sizeof($this->quota[$server]); $i++) { for ($i = 0; $i < sizeof($this->quota[$server]); $i++) {
$quotastring = $quotastring . $this->quota[$server][$i][0] . ',' . $this->quota[$server][$i][2] . ',' . $this->quota[$server][$i][3] $quotastring .= $this->quota[$server][$i][0];
. ',' . $this->quota[$server][$i][6] . ',' . $this->quota[$server][$i][7] . ':'; $quotastring .= ',' . $this->getQuotaNumber($this->quota[$server][$i][2]);
$quotastring .= ',' . $this->getQuotaNumber($this->quota[$server][$i][3]);
$quotastring .= ',' . $this->getQuotaNumber($this->quota[$server][$i][6]);
$quotastring .= ',' . $this->getQuotaNumber($this->quota[$server][$i][7]) . ':';
} }
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($server);
@ -291,6 +294,43 @@ class quota extends baseModule {
return $messages; return $messages;
} }
/**
* Returns the quota value as plain number.
*
* @param string $quotaInput quota value (might contain e.g. "K" for KB)
* @return int quota number
*/
private function getQuotaNumber($quotaInput) {
if (empty($quotaInput) || (strlen($quotaInput) < 2)) {
return $quotaInput;
}
if (substr($quotaInput, -1, 1) === 'K') {
return 1024 * substr($quotaInput, 0, -1);
}
if (substr($quotaInput, -1, 1) === 'M') {
return 1024 * 1024 * substr($quotaInput, 0, -1);
}
if (substr($quotaInput, -1, 1) === 'G') {
return 1024 * 1024 * 1024 * substr($quotaInput, 0, -1);
}
if (substr($quotaInput, -1, 1) === 'T') {
return 1024 * 1024 * 1024 * 1024 * substr($quotaInput, 0, -1);
}
if (substr($quotaInput, -1, 1) === 'k') {
return 1000 * substr($quotaInput, 0, -1);
}
if (substr($quotaInput, -1, 1) === 'm') {
return 1000 * 1000 * substr($quotaInput, 0, -1);
}
if (substr($quotaInput, -1, 1) === 'g') {
return 1000 * 1000 * 1000 * substr($quotaInput, 0, -1);
}
if (substr($quotaInput, -1, 1) === 't') {
return 1000 * 1000 * 1000 * 1000 * substr($quotaInput, 0, -1);
}
return $quotaInput;
}
/** /**
* Allows the module to run commands before the LDAP entry is deleted. * Allows the module to run commands before the LDAP entry is deleted.
* *
@ -363,13 +403,13 @@ class quota extends baseModule {
$this->quota[$server][$i][6] = $_POST[$i . '_6_' . $id]; $this->quota[$server][$i][6] = $_POST[$i . '_6_' . $id];
$this->quota[$server][$i][7] = $_POST[$i . '_7_' . $id]; $this->quota[$server][$i][7] = $_POST[$i . '_7_' . $id];
// Check if values are OK and set automatic values. if not error-variable will be set // Check if values are OK and set automatic values. if not error-variable will be set
if (!get_preg($this->quota[$server][$i][2], 'digit')) if (!get_preg($this->quota[$server][$i][2], 'quotaNumber'))
$errors[] = $this->messages['softblock'][0]; $errors[] = $this->messages['softblock'][0];
if (!get_preg($this->quota[$server][$i][3], 'digit')) if (!get_preg($this->quota[$server][$i][3], 'quotaNumber'))
$errors[] = $this->messages['hardblock'][0]; $errors[] = $this->messages['hardblock'][0];
if (!get_preg($this->quota[$server][$i][6], 'digit')) if (!get_preg($this->quota[$server][$i][6], 'quotaNumber'))
$errors[] = $this->messages['softinode'][0]; $errors[] = $this->messages['softinode'][0];
if (!get_preg($this->quota[$server][$i][7], 'digit')) if (!get_preg($this->quota[$server][$i][7], 'quotaNumber'))
$errors[] = $this->messages['hardinode'][0]; $errors[] = $this->messages['hardinode'][0];
if (intval($this->quota[$server][$i][2]) > intval($this->quota[$server][$i][3])) if (intval($this->quota[$server][$i][2]) > intval($this->quota[$server][$i][3]))
$errors[] = $this->messages['block_cmp'][0]; $errors[] = $this->messages['block_cmp'][0];
@ -437,24 +477,20 @@ class quota extends baseModule {
$sbLimitInput = new htmlInputField($i . '_2_' . $id, $this->quota[$server][$i][2]); $sbLimitInput = new htmlInputField($i . '_2_' . $id, $this->quota[$server][$i][2]);
$sbLimitInput->setFieldSize(12); $sbLimitInput->setFieldSize(12);
$sbLimitInput->setFieldMaxLength(20); $sbLimitInput->setFieldMaxLength(20);
$sbLimitInput->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$return->addElement($sbLimitInput); $return->addElement($sbLimitInput);
$hbLimit = new htmlInputField($i . '_3_' . $id, $this->quota[$server][$i][3]); $hbLimit = new htmlInputField($i . '_3_' . $id, $this->quota[$server][$i][3]);
$hbLimit->setFieldSize(12); $hbLimit->setFieldSize(12);
$hbLimit->setFieldMaxLength(20); $hbLimit->setFieldMaxLength(20);
$hbLimit->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$return->addElement($hbLimit); $return->addElement($hbLimit);
$return->addElement(new htmlOutputText($this->quota[$server][$i][4])); $return->addElement(new htmlOutputText($this->quota[$server][$i][4]));
$return->addElement(new htmlOutputText($this->quota[$server][$i][5])); $return->addElement(new htmlOutputText($this->quota[$server][$i][5]));
$siLimit = new htmlInputField($i . '_6_' . $id, $this->quota[$server][$i][6]); $siLimit = new htmlInputField($i . '_6_' . $id, $this->quota[$server][$i][6]);
$siLimit->setFieldMaxLength(20); $siLimit->setFieldMaxLength(20);
$siLimit->setFieldSize(12); $siLimit->setFieldSize(12);
$siLimit->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$return->addElement($siLimit); $return->addElement($siLimit);
$hiLimit = new htmlInputField($i . '_7_' . $id, $this->quota[$server][$i][7]); $hiLimit = new htmlInputField($i . '_7_' . $id, $this->quota[$server][$i][7]);
$hiLimit->setFieldMaxLength(20); $hiLimit->setFieldMaxLength(20);
$hiLimit->setFieldSize(12); $hiLimit->setFieldSize(12);
$hiLimit->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$return->addElement($hiLimit); $return->addElement($hiLimit);
$return->addElement(new htmlOutputText($this->quota[$server][$i][8])); $return->addElement(new htmlOutputText($this->quota[$server][$i][8]));
$return->addNewLine(); $return->addNewLine();
@ -749,25 +785,25 @@ class quota extends baseModule {
$errors[] = $errMsg; $errors[] = $errMsg;
continue; continue;
} }
if (!get_preg($parts[0], 'digit')) { if (!get_preg($parts[0], 'quotaNumber')) {
$errMsg = $this->messages['softblock'][1]; $errMsg = $this->messages['softblock'][1];
array_push($errMsg, array($i, 'quota_' . $temp['quotas'][$m])); array_push($errMsg, array($i, 'quota_' . $temp['quotas'][$m]));
$errors[] = $errMsg; $errors[] = $errMsg;
continue; continue;
} }
if (!get_preg($parts[1], 'digit')) { if (!get_preg($parts[1], 'quotaNumber')) {
$errMsg = $this->messages['hardblock'][1]; $errMsg = $this->messages['hardblock'][1];
array_push($errMsg, array($i, 'quota_' . $temp['quotas'][$m])); array_push($errMsg, array($i, 'quota_' . $temp['quotas'][$m]));
$errors[] = $errMsg; $errors[] = $errMsg;
continue; continue;
} }
if (!get_preg($parts[2], 'digit')) { if (!get_preg($parts[2], 'quotaNumber')) {
$errMsg = $this->messages['softinode'][1]; $errMsg = $this->messages['softinode'][1];
array_push($errMsg, array($i, 'quota_' . $temp['quotas'][$m])); array_push($errMsg, array($i, 'quota_' . $temp['quotas'][$m]));
$errors[] = $errMsg; $errors[] = $errMsg;
continue; continue;
} }
if (!get_preg($parts[3], 'digit')) { if (!get_preg($parts[3], 'quotaNumber')) {
$errMsg = $this->messages['hardinode'][1]; $errMsg = $this->messages['hardinode'][1];
array_push($errMsg, array($i, 'quota_' . $temp['quotas'][$m])); array_push($errMsg, array($i, 'quota_' . $temp['quotas'][$m]));
$errors[] = $errMsg; $errors[] = $errMsg;
@ -785,6 +821,10 @@ class quota extends baseModule {
$errors[] = $errMsg; $errors[] = $errMsg;
continue; continue;
} }
$parts[0] = $this->getQuotaNumber($parts[0]);
$parts[1] = $this->getQuotaNumber($parts[1]);
$parts[2] = $this->getQuotaNumber($parts[2]);
$parts[3] = $this->getQuotaNumber($parts[3]);
// save quota settings // save quota settings
$temp['accounts'][$name][$temp['quotas'][$m]] = $parts; $temp['accounts'][$name][$temp['quotas'][$m]] = $parts;
} }

View File

@ -140,19 +140,15 @@ class systemQuotas extends baseModule {
$container->addElement(new htmlInputField('path_' . $i, $parts[0], 20)); $container->addElement(new htmlInputField('path_' . $i, $parts[0], 20));
$container->addElement($spacer); $container->addElement($spacer);
$softBlockInput = new htmlInputField('softBlock_' . $i, $parts[1], 10); $softBlockInput = new htmlInputField('softBlock_' . $i, $parts[1], 10);
$softBlockInput->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$container->addElement($softBlockInput); $container->addElement($softBlockInput);
$container->addElement($spacer); $container->addElement($spacer);
$hardBlockInput = new htmlInputField('hardBlock_' . $i, $parts[2], 10); $hardBlockInput = new htmlInputField('hardBlock_' . $i, $parts[2], 10);
$hardBlockInput->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$container->addElement($hardBlockInput); $container->addElement($hardBlockInput);
$container->addElement($spacer); $container->addElement($spacer);
$softInodeInput = new htmlInputField('softInode_' . $i, $parts[3], 10); $softInodeInput = new htmlInputField('softInode_' . $i, $parts[3], 10);
$softInodeInput->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$container->addElement($softInodeInput); $container->addElement($softInodeInput);
$container->addElement($spacer); $container->addElement($spacer);
$hardInodeInput = new htmlInputField('hardInode_' . $i, $parts[4], 10); $hardInodeInput = new htmlInputField('hardInode_' . $i, $parts[4], 10);
$hardInodeInput->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$container->addElement($hardInodeInput); $container->addElement($hardInodeInput);
$container->addElement(new htmlButton('del_' . $i, 'del.png', true), true); $container->addElement(new htmlButton('del_' . $i, 'del.png', true), true);
} }
@ -161,19 +157,15 @@ class systemQuotas extends baseModule {
$container->addElement(new htmlInputField('path', null, 20)); $container->addElement(new htmlInputField('path', null, 20));
$container->addElement($spacer); $container->addElement($spacer);
$newSoftBlockInput = new htmlInputField('softBlock', 0, 10); $newSoftBlockInput = new htmlInputField('softBlock', 0, 10);
$newSoftBlockInput->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$container->addElement($newSoftBlockInput); $container->addElement($newSoftBlockInput);
$container->addElement($spacer); $container->addElement($spacer);
$newHardBlockInput = new htmlInputField('hardBlock', 0, 10); $newHardBlockInput = new htmlInputField('hardBlock', 0, 10);
$newHardBlockInput->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$container->addElement($newHardBlockInput); $container->addElement($newHardBlockInput);
$container->addElement($spacer); $container->addElement($spacer);
$newSoftInodeInput = new htmlInputField('softInode', 0, 10); $newSoftInodeInput = new htmlInputField('softInode', 0, 10);
$newSoftInodeInput->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$container->addElement($newSoftInodeInput); $container->addElement($newSoftInodeInput);
$container->addElement($spacer); $container->addElement($spacer);
$newHardInodeInput = new htmlInputField('hardInode', 0, 10); $newHardInodeInput = new htmlInputField('hardInode', 0, 10);
$newHardInodeInput->setValidationRule(htmlElement::VALIDATE_NUMERIC);
$container->addElement($newHardInodeInput); $container->addElement($newHardInodeInput);
$container->addElement(new htmlButton('add', 'add.png', true)); $container->addElement(new htmlButton('add', 'add.png', true));
return $container; return $container;
@ -253,7 +245,7 @@ class systemQuotas extends baseModule {
$return[] = $error; $return[] = $error;
} }
} }
if (!get_preg($softBlock, 'digit')) { if (!get_preg($softBlock, 'quotaNumber')) {
if ($uploadIndex == null) { if ($uploadIndex == null) {
$return[] = $this->messages['softblock'][0]; $return[] = $this->messages['softblock'][0];
} }
@ -263,7 +255,7 @@ class systemQuotas extends baseModule {
$return[] = $error; $return[] = $error;
} }
} }
if (!get_preg($hardBlock, 'digit')) { if (!get_preg($hardBlock, 'quotaNumber')) {
if ($uploadIndex == null) { if ($uploadIndex == null) {
$return[] = $this->messages['hardblock'][0]; $return[] = $this->messages['hardblock'][0];
} }
@ -273,7 +265,7 @@ class systemQuotas extends baseModule {
$return[] = $error; $return[] = $error;
} }
} }
if (!get_preg($softInode, 'digit')) { if (!get_preg($softInode, 'quotaNumber')) {
if ($uploadIndex == null) { if ($uploadIndex == null) {
$return[] = $this->messages['softinode'][0]; $return[] = $this->messages['softinode'][0];
} }
@ -283,7 +275,7 @@ class systemQuotas extends baseModule {
$return[] = $error; $return[] = $error;
} }
} }
if (!get_preg($hardInode, 'digit')) { if (!get_preg($hardInode, 'quotaNumber')) {
if ($uploadIndex == null) { if ($uploadIndex == null) {
$return[] = $this->messages['hardinode'][0]; $return[] = $this->messages['hardinode'][0];
} }