From b541460231671a971d1e2bd67fdd6b96c9637dbd Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Wed, 13 Dec 2017 10:59:38 +0100 Subject: [PATCH] error handling --- lam/lib/modules/quota.inc | 138 ++++++++++++++++++++------------------ lam/lib/remote.inc | 12 ++-- 2 files changed, 80 insertions(+), 70 deletions(-) diff --git a/lam/lib/modules/quota.inc b/lam/lib/modules/quota.inc index 8b9cdc17..576c1bf5 100644 --- a/lam/lib/modules/quota.inc +++ b/lam/lib/modules/quota.inc @@ -165,7 +165,6 @@ class quota extends baseModule { /** * Initializes the quota values. - * */ function initQuotas() { if (isset($this->quota)) return; @@ -519,73 +518,79 @@ class quota extends baseModule { $optionsAvailable = false; // get list of lamdaemon servers $lamdaemonServers = explode(";", $_SESSION['config']->get_scriptServers()); - for ($s = 0; $s < sizeof($lamdaemonServers); $s++) { - $temp = explode(":", $lamdaemonServers[$s]); - $server = $temp[0]; - $id = $this->replaceSpecialChars($server); - $description = $temp[0]; - if (isset($temp[1])) { - $description = $temp[1] . ' (' . $temp[0] . ')'; - } - // Get quotas - $remote = new \LAM\REMOTE\Remote(); - $remote->connect($server); - $quotas = $remote->execute(implode(quota::$SPLIT_DELIMITER, array("+", "quota", "get", $this->get_scope()))); - $remote->disconnect(); - if (empty($quotas)) { - continue; - } - $dirs = explode(":", $quotas); - array_pop($dirs); // remove empty element at the end - for ($i = 0; $i < sizeof($dirs); $i++) { - if (strpos($dirs[$i], quota::$QUOTA_PREFIX) !== 0) { - unset($dirs[$i]); - $dirs = array_values($dirs); - $i--; + try { + for ($s = 0; $s < sizeof($lamdaemonServers); $s++) { + $temp = explode(":", $lamdaemonServers[$s]); + $server = $temp[0]; + $id = $this->replaceSpecialChars($server); + $description = $temp[0]; + if (isset($temp[1])) { + $description = $temp[1] . ' (' . $temp[0] . ')'; + } + // Get quotas + $remote = new \LAM\REMOTE\Remote(); + $remote->connect($server); + $quotas = $remote->execute(implode(quota::$SPLIT_DELIMITER, array("+", "quota", "get", $this->get_scope()))); + $remote->disconnect(); + if (empty($quotas)) { continue; } - $dirs[$i] = substr($dirs[$i], strlen(quota::$QUOTA_PREFIX)); - $dirs[$i] = explode(",", $dirs[$i]); - $dirs[$i] = $dirs[$i][0]; - } - $dirs = array_values($dirs); - if (sizeof($dirs) < 1) continue; // stop if no quota directories were found - $optionsAvailable = true; - $return->addElement(new htmlSubTitle($description), true); - $return->addElement(new htmlOutputText(' ' . _('Mountpoint') . ' ', false)); - $return->addElement(new htmlOutputText(' ' . _('Soft block limit') . ' ', false)); - $return->addElement(new htmlOutputText(' ' . _('Hard block limit') . ' ', false)); - $return->addElement(new htmlOutputText(' ' . _('Soft inode limit') . ' ', false)); - $return->addElement(new htmlOutputText(' ' . _('Hard inode limit') . ' ', false)); - $return->addNewLine(); - $return->addElement(new htmlHelpLink('Mountpoint')); - $return->addElement(new htmlHelpLink('SoftBlockLimit')); - $return->addElement(new htmlHelpLink('HardBlockLimit')); - $return->addElement(new htmlHelpLink('SoftInodeLimit')); - $return->addElement(new htmlHelpLink('HardInodeLimit')); - $return->addNewLine(); - - for ($i = 0; $i < sizeof($dirs); $i++) { - $return->addElement(new htmlOutputText($dirs[$i])); - $sbLimit = new htmlInputField("quota_softblock_" . $id . "_" . $dirs[$i]); - $sbLimit->setFieldSize(12); - $sbLimit->setFieldMaxLength(20); - $return->addElement($sbLimit); - $hbLimit = new htmlInputField("quota_hardblock_" . $id . "_" . $dirs[$i]); - $hbLimit->setFieldSize(12); - $hbLimit->setFieldMaxLength(20); - $return->addElement($hbLimit); - $siLimit = new htmlInputField("quota_softinode_" . $id . "_" . $dirs[$i]); - $siLimit->setFieldSize(12); - $siLimit->setFieldMaxLength(20); - $return->addElement($siLimit); - $hiLimit = new htmlInputField("quota_hardinode_" . $id . "_" . $dirs[$i]); - $hiLimit->setFieldSize(12); - $hiLimit->setFieldMaxLength(20); - $return->addElement($hiLimit); + $dirs = explode(":", $quotas); + array_pop($dirs); // remove empty element at the end + for ($i = 0; $i < sizeof($dirs); $i++) { + if (strpos($dirs[$i], quota::$QUOTA_PREFIX) !== 0) { + unset($dirs[$i]); + $dirs = array_values($dirs); + $i--; + continue; + } + $dirs[$i] = substr($dirs[$i], strlen(quota::$QUOTA_PREFIX)); + $dirs[$i] = explode(",", $dirs[$i]); + $dirs[$i] = $dirs[$i][0]; + } + $dirs = array_values($dirs); + if (sizeof($dirs) < 1) continue; // stop if no quota directories were found + $optionsAvailable = true; + $return->addElement(new htmlSubTitle($description), true); + $return->addElement(new htmlOutputText(' ' . _('Mountpoint') . ' ', false)); + $return->addElement(new htmlOutputText(' ' . _('Soft block limit') . ' ', false)); + $return->addElement(new htmlOutputText(' ' . _('Hard block limit') . ' ', false)); + $return->addElement(new htmlOutputText(' ' . _('Soft inode limit') . ' ', false)); + $return->addElement(new htmlOutputText(' ' . _('Hard inode limit') . ' ', false)); $return->addNewLine(); + $return->addElement(new htmlHelpLink('Mountpoint')); + $return->addElement(new htmlHelpLink('SoftBlockLimit')); + $return->addElement(new htmlHelpLink('HardBlockLimit')); + $return->addElement(new htmlHelpLink('SoftInodeLimit')); + $return->addElement(new htmlHelpLink('HardInodeLimit')); + $return->addNewLine(); + + for ($i = 0; $i < sizeof($dirs); $i++) { + $return->addElement(new htmlOutputText($dirs[$i])); + $sbLimit = new htmlInputField("quota_softblock_" . $id . "_" . $dirs[$i]); + $sbLimit->setFieldSize(12); + $sbLimit->setFieldMaxLength(20); + $return->addElement($sbLimit); + $hbLimit = new htmlInputField("quota_hardblock_" . $id . "_" . $dirs[$i]); + $hbLimit->setFieldSize(12); + $hbLimit->setFieldMaxLength(20); + $return->addElement($hbLimit); + $siLimit = new htmlInputField("quota_softinode_" . $id . "_" . $dirs[$i]); + $siLimit->setFieldSize(12); + $siLimit->setFieldMaxLength(20); + $return->addElement($siLimit); + $hiLimit = new htmlInputField("quota_hardinode_" . $id . "_" . $dirs[$i]); + $hiLimit->setFieldSize(12); + $hiLimit->setFieldMaxLength(20); + $return->addElement($hiLimit); + $return->addNewLine(); + } } } + catch (LAMException $e) { + $return->addElement(new htmlStatusMessage('WARN', $e->getTitle())); + return $return; + } if (!$optionsAvailable) { return null; } @@ -640,7 +645,12 @@ class quota extends baseModule { * @param array $profile hash array with profile values (identifier => value) */ function load_profile($profile) { - $this->initQuotas(); + try { + $this->initQuotas(); + } + catch (LAMException $e) { + logNewMessage(LOG_ERR, $e->getTitle(), $e->getMessage()); + } if (!isset($this->quota) || !is_array($this->quota)) return; $servers = array_keys($this->quota); for ($s = 0; $s < sizeof($servers); $s++) { diff --git a/lam/lib/remote.inc b/lam/lib/remote.inc index e705233a..4f491f43 100644 --- a/lam/lib/remote.inc +++ b/lam/lib/remote.inc @@ -1,6 +1,6 @@ loginSSH($handle); $this->server = $handle; @@ -111,7 +111,7 @@ class Remote { $username = $entry[0]['uid'][0]; } if (empty($username)) { - throw new Exception(sprintf(_("Your LAM admin user (%s) must be a valid Unix account to work with lamdaemon!"), getAbstractDN($credentials[0]))); + throw new LAMException(sprintf(_("Your LAM admin user (%s) must be a valid Unix account to work with lamdaemon!"), getAbstractDN($credentials[0]))); } } $password = $credentials[1]; @@ -119,7 +119,7 @@ class Remote { if (!empty($keyPath)) { // use key authentication if (!file_exists($keyPath) || !is_readable($keyPath)) { - throw new Exception(sprintf(_("Unable to read %s."), htmlspecialchars($keyPath))); + throw new LAMException(sprintf(_("Unable to read %s."), htmlspecialchars($keyPath))); } $key = file_get_contents($keyPath); $rsa = new RSA(); @@ -128,13 +128,13 @@ class Remote { $rsa->setPassword($keyPassword); } if (!$rsa->loadKey($key)) { - throw new Exception(sprintf(_("Unable to load key %s."), htmlspecialchars($keyPath))); + throw new LAMException(sprintf(_("Unable to load key %s."), htmlspecialchars($keyPath))); } $password = $rsa; } $login = @$handle->login($username, $password); if (!$login) { - throw new Exception(_("Unable to login to remote server!")); + throw new LAMException(_("Unable to login to remote server!")); } }