From 4130b1d880b10d010031c774d48c186aa130d2d1 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Tue, 30 May 2017 20:31:05 +0200 Subject: [PATCH] initial checkin --- lam/lib/modules/courierMailAccount.inc | 601 +++++++++++++++++++++++++ 1 file changed, 601 insertions(+) create mode 100644 lam/lib/modules/courierMailAccount.inc diff --git a/lam/lib/modules/courierMailAccount.inc b/lam/lib/modules/courierMailAccount.inc new file mode 100644 index 00000000..eabfc5ad --- /dev/null +++ b/lam/lib/modules/courierMailAccount.inc @@ -0,0 +1,601 @@ +autoAddObjectClasses = false; + } + + /** + * Returns true if this module can manage accounts of the current type, otherwise false. + * + * @return boolean true if module fits + */ + public function can_manage() { + return in_array($this->get_scope(), array( + 'user' + )); + } + + /** + * {@inheritDoc} + */ + public function get_metaData() { + $return = array(); + // icon + $return['icon'] = 'courierMail.png'; + // alias name + $return["alias"] = _("Courier"); + // module dependencies + $return['dependencies'] = array( + 'depends' => array( + 'inetOrgPerson' + ), + 'conflicts' => array() + ); + // managed object classes + $return['objectClasses'] = array( + 'courierMailAccount' + ); + // managed attributes + $return['attributes'] = array( + 'mailbox', 'mailhost', 'sharedgroup', 'disableimap', + 'disablepop3', 'disableshared', 'disablewebmail', 'quota' + ); + // help entries + $return['help'] = array( + 'disableimap' => array( + "Headline" => _("Disable IMAP access"), + "attr" => 'disableimap', + "Text" => _("Setting this will disallow the user from logging in using IMAP.") + ), + 'disablepop3' => array( + "Headline" => _("Disable POP3 access"), + "attr" => 'disablepop3', + "Text" => _("Setting this will disallow the user from logging in using POP3.") + ), + 'disablewebmail' => array( + "Headline" => _("Disable webmail access"), + "attr" => 'disablewebmail', + "Text" => _("Setting this will disallow the user from logging in using SQwebmail.") + ), + 'disableshared' => array( + "Headline" => _("Disable shared folder access"), + "attr" => 'disableshared', + "Text" => _("Setting this will make shared folders invisible to this user.") + ), + 'mailbox' => array( + "Headline" => _("User mailbox"), + "attr" => 'mailbox', + "Text" => _("Path to the user's mailbox. Usually relative to the user's home folder. In a mixed setup with users that are both local unix users and virtual Courier mail users it can be beneficial to configure this to be the Courier mail home directory.") + ), + 'mailhost' => array( + "Headline" => _("User mail host"), + "attr" => 'mailhost', + "Text" => _("Name of the host that stores the user's email. Must be a FQDN.") + ), + 'quota' => array( + "Headline" => _("User mail quota"), + "attr" => 'quota', + "Text" => _("The amont of storage available to the user.") + ), + 'autoAdd' => array( + "Headline" => _("Automatically add this extension"), + "Text" => _("Automatically add this extension when creating new users.") + ), + 'unixUserType' => array( + "Headline" => _("How are local Unix users handled"), + "Text" => _("This choice depends on how you wish to run your system. If you choose 'local' then it will be assumed that Courier mail is set up to use the homedirectory attribute as the root path for maildirs and the mailbox attribute for the name of the maildir folder if different from './MAILDIR'. If you choose 'virtual' then it will be assumed that Courier mail is set up to ignore the 'homedirectory' attribute and instead use the 'mailbox' attribute as the root path for maildirs, and the maildir directory is 'MAILDIR' below this directory.") + ), + 'virtMailRoot' => array( + "Headline" => _("The root path for virtual mail users"), + "Text" => _("This path will be used as the mail root for virtual users.") + ), + 'homeDirectory' => array( + "Headline" => _("User home directory"), + "attr" => 'homeDirectory', + "Text" => _("The system home directory of the user. In a virtual user setup this is typically the root for all users.") + ), + 'clearPassword' => array( + "Headline" => _("User's password in clear text"), + "attr" => 'clearPassword', + "Text" => _("This attribute contains the user's password in clear text. It is not recommended to use.") + ), + 'description' => array( + "Headline" => _("Description of the user"), + "attr" => 'description', + "Text" => _("A description of the user.") + ), + 'gecos' => array( + "Headline" => _("GECOS field"), + "attr" => 'gecos', + "Text" => _("") + ), + 'gidNumber' => array( + "Headline" => _("Group number"), + "attr" => 'gidNumber', + "Text" => _("The number of the operating system group this user belongs to.") + ), + 'loginShell' => array( + "Headline" => _("User unix login shell"), + "attr" => 'loginShell', + "Text" => _("The shell used by this user") + ), + 'uid' => array( + "Headline" => _("User id"), + "attr" => 'uid', + "Text" => _("The id of the user in the operating system.") + ), + 'uidNumber' => array( + "Headline" => _("User id number"), + "attr" => 'uidnumber', + "Text" => _("The operating system number for this user.") + ), + 'userPassword' => array( + "Headline" => _("User's encrypted password"), + "attr" => 'userPassword', + "Text" => _("The encrypted password for this user.") + ) + ); + // profile options + $profileContainer = new htmlTable(); + $profileContainer->addElement(new htmlTableExtendedInputCheckbox('courierMailAccount_addExt', false, _('Automatically add this extension'), 'autoAdd'), true); + $return['profile_options'] = $profileContainer; + // PDF fields + $return['PDF_fields'] = array( + 'mailbox' => _('User mailbox'), + 'mailhost' => _('User mail host'), + 'quota' => _('User mail quota'), + 'disableimap' => _('Disable IMAP access'), + 'disablepop3' => _('Disable POP3 access'), + 'disablewebmail' => _('Disable webmail access'), + 'disableshared' => _('Disable shared folder access') + ); + return $return; + } + + /** + * This function fills the error message array with messages + */ + public function load_Messages() { + $this->messages['mailbox'][] = array('ERROR', _('The mailbox folder is not valid.')); // third array value is set dynamically + $this->messages['mailhost'][] = array('ERROR', _('The Mailhost is not valid')); // third array value is set dynamically + $this->messages['quota'][] = array('ERROR', _('The quota must be entered as a floating point number.')); // third array value is set dynamically + $this->messages['quota'][] = array('ERROR', _('The quota must be entered as one of four formats: 1000, 1000S, 1000k, 1000kB.')); // third array value is set dynamically + $this->messages['virtMailRoot'][] = array('ERROR', _('The virtual mail root must be a file path')); // third array value is set dynamically + $this->messages['homeDirectory'][] = array('ERROR', _('The home directory must be a file path')); // third array value is set dynamically + } + + /** + * {@inheritDoc} + * @see baseModule::getManagedAttributes() + */ + public function getManagedAttributes($typeId) { + $attrs = parent::getManagedAttributes($typeId); + $typeManager = new TypeManager(); + $modules = $typeManager->getConfiguredType($typeId)->getModules(); + if ($this->isUnixDisabled($modules)) { + $attrs[] = 'homeDirectory'; + } + return $attrs; + } + + /** + * {@inheritDoc} + */ + public function display_html_attributes() { + $return = new htmlTable(); + if ($this->isExtensionEnabled()) { + if ($this->isUnixDisabled($this->getAccountContainer()->get_type()->getModules())) { + // home directory + $this->addSimpleInputTextField($return, 'homeDirectory', _('Home directory'), true); + } + + // show current mailBox + $this->addSimpleInputTextField($return, 'mailbox', _('Mailbox folder')); + // mail host input + $this->addSimpleInputTextField($return, 'mailhost', _('Mailbox host')); + // check boxes for account access control + $disableimap = isset($this->attributes['disableimap'][0]) && ($this->attributes['disableimap'][0] != 0); + $return->addElement(new htmlTableExtendedInputCheckbox('disableimap', $disableimap, _('Disable IMAP use'), 'disableimap'), true); + $disablepop3 = isset($this->attributes['disablepop3'][0]) && ($this->attributes['disablepop3'][0] != 0); + $return->addElement(new htmlTableExtendedInputCheckbox('disablepop3', $disablepop3, _('Disable POP3 use'), 'disablepop3'), true); + $disablewebmail = isset($this->attributes['disablewebmail'][0]) && ($this->attributes['disablewebmail'][0] != 0); + $return->addElement(new htmlTableExtendedInputCheckbox('disablewebmail', $disablewebmail, _('Disable Webmail use'), 'disablewebmail'), true); + $disableshared = isset($this->attributes['disableshared'][0]) && ($this->attributes['disableshared'][0] != 0); + $return->addElement(new htmlTableExtendedInputCheckbox('disableshared', $disableshared, _('Disable Shared Folder use'), 'disableshared'), true); + // quota with selection for unit of measure + $unitIndex = 2; + $value = 0; + if (isset($this->attributes['quota'][0]) && (strlen($this->attributes['quota'][0]) > 0)) { + $unitIndex = floor(log(substr($this->attributes['quota'][0], 0, -1), 1000)); + $value = round((float) (substr($this->attributes['quota'][0], 0, -1)) / pow(1000, $unitIndex), 2); + } + $return->addElement(new htmlOutputText(_('Mail quota'))); + $tempTable = new htmlTable(); + $boxInput = new htmlInputField('quotaValue', $value); + $boxInput->setFieldSize(5); + $boxInput->setFieldMaxLength(5); + $tempTable->addElement($boxInput); + $units = array( + 'B', 'kB', 'MB', 'GB', + 'TB', 'PB', 'EB' + ); + $selected = array( + $units[$unitIndex] + ); + $tempTable->addElement(new htmlSelect('quotaUnit', $units, $selected)); + $return->addElement($tempTable); + $return->addElement(new htmlHelpLink('quota'), true); + $return->addElement(new htmlSpacer(null, '20px'), true); + $remButton = new htmlButton('remObjectClass', _('Remove Courier mail extension')); + $remButton->colspan = 3; + $return->addElement($remButton); + } + else { + $return->addElement(new htmlButton('addObjectClass', _('Add Courier mail extension'))); + } + return $return; + } + + /** + * {@inheritdoc} + */ + public function process_attributes() { + // add extension + if (isset($_POST['addObjectClass'])) { + $this->attributes['objectClass'][] = 'courierMailAccount'; + return array(); + } + // remove extension + elseif (isset($_POST['remObjectClass'])) { + $this->attributes['objectClass'] = array_delete(array( + 'courierMailAccount' + ), $this->attributes['objectClass']); + $attrNames = $this->getManagedAttributes($this->getAccountContainer()->get_type()->getId()); + foreach ($attrNames as $attrName) { + if (isset($this->attributes[$attrName])) { + unset($this->attributes[$attrName]); + } + } + return array(); + } + + // skip processing if extension is not active + + if (!$this->isExtensionEnabled()) { + return array(); + } + + $errors = array(); + + // check new mailbox + $this->attributes['mailbox'] = array(); + if (isset($_POST['mailbox']) && ($_POST['mailbox'] != "")) { + $attrs = $this->getAccountContainer()->getAccountModule('inetOrgPerson')->getAttributes(); + $mailbox = str_replace('$email', $attrs['mail'][0], $_POST['mailbox']); + + // check for valid path + if (!get_preg($mailbox, 'filePath')) { + $message = $this->messages['mailbox'][0]; + $message[] = $mailbox; + $errors[] = $message; + } + $this->attributes['mailbox'][] = $mailbox; + } + + $this->attributes['quota'] = array(); + $units = array( + 'B', 'kB', 'MB', + 'GB', 'TB', 'PB', 'EB' + ); + + // check new quota + + if (isset($_POST['quotaValue'])) { + if (is_numeric($_POST['quotaValue']) && in_array($_POST['quotaUnit'], $units)) { + if ($_POST['quotaValue'] > 0) { + $power = array_search($_POST['quotaUnit'], $units); + $this->attributes['quota'][] = $_POST['quotaValue'] * pow(1000, $power) . "S"; + } + else { + unset($this->attributes['quota']); + } + } + else { + $message = $this->messages['courier'][1]; + $message[] = $_POST['quotaValue']; + $errors[] = $message; + } + } + else { + unset($this->attributes['quota']); + } + + $this->attributes['mailhost'] = array(); + if (isset($_POST['mailhost']) && ($_POST['mailhost'] != "")) { + // Must be a valid host name + if (!get_preg($this->attributes['mailhost'][0] = $_POST['mailhost'], 'hostname')) { + $message = $this->messages['mailhost'][0]; + $message[] = $_POST['mailhost']; + $errors[] = $message; + } + $this->attributes['mailhost'][] = $_POST['mailhost']; + } + else { + unset($this->attributes['mailhost']); + } + + $this->attributes['disableimap'][0] = 0; + if (isset($_POST['disableimap'])) { + $this->attributes['disableimap'][0] = 1; + } + + $this->attributes['disablepop3'][0] = 0; + if (isset($_POST['disablepop3'])) { + $this->attributes['disablepop3'][0] = 1; + } + + $this->attributes['disablewebmail'][0] = 0; + if (isset($_POST['disablewebmail'])) { + $this->attributes['disablewebmail'][0] = 1; + } + + $this->attributes['disableshared'][0] = 0; + if (isset($_POST['disableshared'])) { + $this->attributes['disableshared'][0] = 1; + } + + $this->attributes['disableshared'][0] = 0; + if (isset($_POST['disableshared'])) { + $this->attributes['disableshared'][0] = 1; + } + + if ($this->isUnixDisabled($this->getAccountContainer()->get_type()->getModules())) { + + // check new home directory + $this->attributes['homeDirectory'] = array(); + if (isset($_POST['homeDirectory']) && ($_POST['homeDirectory'] != "")) { + $attrs = $this->getAccountContainer()->getAccountModule('inetOrgPerson')->getAttributes(); + $mailbox = str_replace('$email', $attrs['mail'][0], $_POST['homeDirectory']); + // check for valid path + if (!get_preg($mailbox, 'filePath')) { + $message = $this->messages['homeDirectory'][0]; + $message[] = $mailbox; + $errors[] = $message; + } + $this->attributes['homeDirectory'][] = $mailbox; + } + + } + + return $errors; + } + + /** + * {@inheritdoc} + */ + public function module_ready() { + $attrs = $this->getAccountContainer()->getAccountModule('inetOrgPerson')->getAttributes(); + return isset($attrs['mail'][0]); + } + + /** + * Returns if the Courier extension is enabled. + * + * @return boolean Courier extension is active + */ + private function isExtensionEnabled() { + return in_array('courierMailAccount', $this->attributes['objectClass']); + } + + /** + * {@inheritDoc} + * @see baseModule::get_pdfFields() + */ + public function get_pdfFields($typeId) { + $fields = parent::get_pdfFields($typeId); + $typeManager = new TypeManager(); + $modules = $typeManager->getConfiguredType($typeId)->getModules(); + if ($this->isUnixDisabled($modules)) { + $fields['homeDirectory'] = _('Home directory'); + } + return $fields; + } + + /** + * {@inheritdoc} + */ + public function get_pdfEntries() { + $return = array(); + $this->addSimplePDFField($return, 'mailbox', _('User mailbox')); + $this->addSimplePDFField($return, 'mailhost', _('User mail host')); + $this->addSimplePDFField($return, 'quota', _('User mail quota')); + $this->addSimplePDFField($return, 'disableimap', _('IMAP access disabled')); + $this->addSimplePDFField($return, 'disablepop3', _('POP3 access disabled')); + $this->addSimplePDFField($return, 'disablewebmail', _('Webmail access disabled')); + $this->addSimplePDFField($return, 'disableshared', _('Shared folder access disabled')); + $this->addSimplePDFField($return, 'homeDirectory', _('Home directory')); + return $return; + } + + /** + * {@inheritdoc} + */ + public function build_uploadAccounts($rawAccounts, $ids, &$partialAccounts, $selectedModules) { + $messages = array(); + $unitsLong = array( + 'B', 'kB', 'MB', 'GB', + 'TB', 'PB', 'EB' + ); + $unitsShort = array( + '', 'k', 'M', 'G', + 'T', 'P', 'E' + ); + for ($i = 0; $i < sizeof($rawAccounts); $i++) { + + // add object class + + if (!in_array("courierMailAccount", $partialAccounts[$i]['objectClass'])) $partialAccounts[$i]['objectClass'][] = "courierMailAccount"; + + // add mailbox + + if ($rawAccounts[$i][$ids['courierMailAccount_mailbox']] != "") { + if (!get_preg($rawAccounts[$i][$ids['courierMailAccount_mailbox']], 'filePath')) { + $message = $this->messages['mailbox'][0]; + $message[] = $_POST['mailbox']; + $errors[] = $message; + } + else + $partialAccounts[$i]['mailbox'][] = $rawAccounts[$i][$ids['courierMailAccount_mailbox']]; + } + + // add mailhost + + if ($rawAccounts[$i][$ids['courierMailAccount_mailhost']] != "") { + if (!get_preg($rawAccounts[$i][$ids['courierMailAccount_mailhost']], 'hostname')) { + $message = $this->messages['mailhost'][0]; + $message[] = $_POST['mailhost']; + $errors[] = $message; + } + else + $partialAccounts[$i]['mailhost'][] = $rawAccounts[$i][$ids['courierMailAccount_mailhost']]; + } + + // add mail quota. This works if the quota is one of four formats: 1000, 1000S, 1000k, 1000kB + + if ($rawAccounts[$i][$ids['courierMailAccount_quota']] != "") { + + // 1st format: 1000 + + if (is_numeric($rawAccounts[$i][$ids['courierMailAccount_quota']])) + $partialAccounts[$i]['quota'][] = $rawAccounts[$i][$ids['courierMailAccount_quota']] . "S"; + + // Is it one of 2nd or 3rd formats: 1000S, 1000k + + elseif (is_numeric(mb_substr($rawAccounts[$i][$ids['courierMailAccount_quota']], 0, -1))) { + + // 2nd format: 1000S + + if (mb_substr($rawAccounts[$i][$ids['courierMailAccount_quota']], -1) == "S") + $partialAccounts[$i]['quota'][] = $rawAccounts[$i][$ids['courierMailAccount_quota']] . "S"; + + // 3rd format: 1000k + + elseif (in_array(mb_substr($rawAccounts[$i][$ids['courierMailAccount_quota']], -1), $unitsShort)) { + $power = array_search(mb_substr($rawAccounts[$i][$ids['courierMailAccount_quota']], -1), $unitsShort); + $partialAccounts[$i]['quota'][] = mb_substr($rawAccounts[$i][$ids['courierMailAccount_quota']], 0, -1) * pow(1000, $power) . "S"; + } + } + + // 4th format: 1000kB + + elseif (is_numeric(mb_substr($rawAccounts[$i][$ids['courierMailAccount_quota']], 0, -2)) && in_array(mb_substr($rawAccounts[$i][$ids['courierMailAccount_quota']], -2), $unitsLong)) { + $power = array_search(mb_substr($rawAccounts[$i][$ids['courierMailAccount_quota']], -1), $unitsShort); + $partialAccounts[$i]['quota'][] = mb_substr($rawAccounts[$i][$ids['courierMailAccount_quota']], 0, -1) * pow(1000, $power) . "S"; + } + else { + $errMsg = $this->messages['quota'][1]; + array_push($errMsg, array( + $i + )); + $messages[] = $errMsg; + } + } + + if ($rawAccounts[$i][$ids['courierMailAccount_disableimap']] != "") { + if (($rawAccounts[$i][$ids['courierMailAccount_disableimap']]) == "true" || ($rawAccounts[$i][$ids['courierMailAccount_disableimap']] == "1")) $partialAccounts[$i]['disableimap'][] = 1; + } + + if ($rawAccounts[$i][$ids['courierMailAccount_disablepop3']] != "") { + if (($rawAccounts[$i][$ids['courierMailAccount_disablepop3']]) == "true" || ($rawAccounts[$i][$ids['courierMailAccount_disablepop3']] == "1")) $partialAccounts[$i]['disablepop3'][] = 1; + } + + if ($rawAccounts[$i][$ids['courierMailAccount_disablewebmail']] != "") { + if (($rawAccounts[$i][$ids['courierMailAccount_disablewebmail']]) == "true" || ($rawAccounts[$i][$ids['courierMailAccount_disablewebmail']] == "1")) $partialAccounts[$i]['disablewebmail'][] = 1; + } + + if ($rawAccounts[$i][$ids['courierMailAccount_disableshared']] != "") { + if (($rawAccounts[$i][$ids['courierMailAccount_disableshared']]) == "true" || ($rawAccounts[$i][$ids['courierMailAccount_disableshared']] == "1")) $partialAccounts[$i]['disableshared'][] = 1; + } + } + + return $messages; + } + + /** + * {@inheritdoc} + */ + public function load_profile($profile) { + // profile mappings in meta data + parent::load_profile($profile); + // add extension + if (isset($profile['courierMailAccount_addExt'][0]) && ($profile['courierMailAccount_addExt'][0] == "true")) { + if (!in_array('courierMailAccount', $this->attributes['objectClass'])) { + $this->attributes['objectClass'][] = 'courierMailAccount'; + } + } + } + + /** + * Returns if this account is virtual only or also a unix account + * + * @param string[] modules + * @return boolean only virtual account + */ + private function isUnixDisabled($modules) { + if (in_array('posixAccount', $modules)) { + return false; + } + else { + return true; + } + } + +} + +?> \ No newline at end of file