| 1 |
<?php |
| 2 |
/* |
| 3 |
|
| 4 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 5 |
Copyright (C) 2010 - 2011 Pavel Pozdniak |
| 6 |
2010 - 2026 Roland Gruber |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software |
| 20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* Manages mailboxes on an IMAP server. |
| 25 |
* |
| 26 |
* @package modules |
| 27 |
* @author Pavel Pozdniak |
| 28 |
* @author Roland Gruber |
| 29 |
*/ |
| 30 |
|
| 31 |
use LAM\TYPES\ConfiguredType; |
| 32 |
use Webklex\PHPIMAP\Client; |
| 33 |
use Webklex\PHPIMAP\ClientManager; |
| 34 |
use Webklex\PHPIMAP\Connection\Protocols\ImapProtocol; |
| 35 |
|
| 36 |
/** |
| 37 |
* Manages mailboxes on an IMAP server. |
| 38 |
* |
| 39 |
* @package modules |
| 40 |
* @author Pavel Pozdniak |
| 41 |
* @author Roland Gruber |
| 42 |
*/ |
| 43 |
class imapAccess extends baseModule { |
| 44 |
|
| 45 |
private const QUOTA_USAGE = 'usage'; |
| 46 |
private const QUOTA_LIMIT = 'limit'; |
| 47 |
|
| 48 |
/** @var string|null $profileQuotaLimit quota limit from profile */ |
| 49 |
private ?string $profileQuotaLimit = null; |
| 50 |
|
| 51 |
/** user name */ |
| 52 |
private string $user = ''; |
| 53 |
/** email address */ |
| 54 |
private string $email = ''; |
| 55 |
|
| 56 |
/** |
| 57 |
* {@inheritDoc} |
| 58 |
*/ |
| 59 |
public function can_manage(): bool { |
| 60 |
return $this->get_scope() === 'user'; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* {@inheritDoc} |
| 65 |
*/ |
| 66 |
public function get_metaData(): array { |
| 67 |
$return = []; |
| 68 |
// alias name |
| 69 |
$return["alias"] = _("Mailbox"); |
| 70 |
// module dependencies |
| 71 |
$return['dependencies'] = ['depends' => [['inetOrgPerson', 'windowsUser']], 'conflicts' => []]; |
| 72 |
// managed object classes |
| 73 |
$return['objectClasses'] = []; |
| 74 |
// managed attributes |
| 75 |
$return['attributes'] = []; |
| 76 |
// icon |
| 77 |
$return['icon'] = 'mail.svg'; |
| 78 |
// help |
| 79 |
$return['help'] = [ |
| 80 |
'ImapServerAddress' => [ |
| 81 |
"Headline" => _("Server address"), |
| 82 |
"Text" => _("Address of IMAP server (e.g. mail.example.org).")], |
| 83 |
'ImapServerEncryptionProtocol' => [ |
| 84 |
"Headline" => _("Encryption protocol"), |
| 85 |
"Text" => _("Encryption protocol for connecting to IMAP server. LAM requires an encrypted connection.")], |
| 86 |
'ImapValidateServerCert' => [ |
| 87 |
"Headline" => _("Validate server certificate"), |
| 88 |
"Text" => _("This option allows you to disable the certificate check of your IMAP server certificate. Disabling the certificate check is not recommended.")], |
| 89 |
'ImapAdmin' => [ |
| 90 |
"Headline" => _("IMAP admin user"), |
| 91 |
"Text" => _("The login name of your IMAP user who has rights to create/delete mailboxes.") . ' ' . _('Use wildcards like $uid$ for LDAP attributes of the current LAM admin user.')], |
| 92 |
'ImapAdminPasswordSelect' => [ |
| 93 |
"Headline" => _("IMAP password input"), |
| 94 |
"Text" => _("Choose the way how to provide the IMAP admin password. You can use the same password as for the LAM login or LAM will ask you for a password when it is required.") |
| 95 |
. ' ' . _('Storing the password in your server profile is also possible but not recommended.') |
| 96 |
], |
| 97 |
'ImapAdminPassword_Sess' => [ |
| 98 |
"Headline" => _("Password of IMAP admin user"), |
| 99 |
"Text" => _("The password of your IMAP admin user. The login name for the IMAP admin user is stored in the LAM server profile.")], |
| 100 |
'ImapUserPrefix' => [ |
| 101 |
"Headline" => _("Prefix for mailboxes"), |
| 102 |
"Text" => _("Some IMAP servers store mailboxes with a prefix (e.g. \"user\" for Cyrus which results in \"user.username\").")], |
| 103 |
'ImapMailDomain' => [ |
| 104 |
"Headline" => _("Mail domains"), |
| 105 |
"Text" => _("Please enter a comma separated list of domain names (e.g. \"company.com,example.com\"). LAM will only manage mailboxes from these domains.")], |
| 106 |
'ImapUserNameAttr' => [ |
| 107 |
"Headline" => _("User name attribute"), |
| 108 |
"Text" => _("Please choose the attribute to get the IMAP user name. The default is \"mail\" but you can also use \"uid\" or \"userPrincipalName\".")], |
| 109 |
'MailAddress' => [ |
| 110 |
"Headline" => _("Mailbox"), |
| 111 |
"Text" => _("This mailbox will be created/deleted.")], |
| 112 |
'ImapUserQuotaLimit' => [ |
| 113 |
"Headline" => _("Quota"), |
| 114 |
"Text" => _("Please enter the quota limit of this mailbox in kilobytes.")], |
| 115 |
'pathSeparator' => [ |
| 116 |
"Headline" => _("Path separator"), |
| 117 |
"Text" => _("This is the separator for the mailbox path. Usually, this is \".\" but e.g. Cyrus with \"unixhierarchysep\" will require \"/\".")], |
| 118 |
'initialFolders' => [ |
| 119 |
"Headline" => _("Initial folders"), |
| 120 |
"Text" => _("Use this to provide a list of folders (e.g. Trash) to add for new accounts.")], |
| 121 |
'createMailbox' => [ |
| 122 |
"Headline" => _("Create mailbox"), |
| 123 |
"Text" => _('Set to "true" to create the mailbox.')], |
| 124 |
]; |
| 125 |
// configuration checks |
| 126 |
$return['config_checks']['all']['ImapAccess_ImapServerAddress'] = [ |
| 127 |
'type' => 'ext_preg', |
| 128 |
'regex' => 'DNSname', |
| 129 |
'required' => true, |
| 130 |
'required_message' => $this->messages['config'][0], |
| 131 |
'error_message' => $this->messages['config'][0]]; |
| 132 |
$return['config_checks']['all']['ImapAccess_ImapDomain'] = [ |
| 133 |
'type' => 'regex_i', |
| 134 |
'regex' => '[\\*a-z0-9\\._-]+(,[a-z0-9\\._-]+)*', |
| 135 |
'required' => true, |
| 136 |
'required_message' => $this->messages['config'][1], |
| 137 |
'error_message' => $this->messages['config'][1]]; |
| 138 |
// profile options |
| 139 |
$profileContainer = new htmlResponsiveRow(); |
| 140 |
$profileContainer->add(new htmlResponsiveInputField(_('Quota'), 'ImapAccess_QuotaLimit', null, 'ImapUserQuotaLimit')); |
| 141 |
$return['profile_options'] = $profileContainer; |
| 142 |
$return['profile_checks']['ImapAccess_QuotaLimit'] = [ |
| 143 |
'type' => 'ext_preg', |
| 144 |
'regex' => 'digit', |
| 145 |
'error_message' => $this->messages['managemailbox'][8]]; |
| 146 |
return $return; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* {@inheritDoc} |
| 151 |
*/ |
| 152 |
protected function load_Messages(): void { |
| 153 |
$this->messages['config'][0] = ['ERROR', _('Please enter a valid server name where the mailboxes reside.')]; |
| 154 |
$this->messages['config'][1] = ['ERROR', _('Please enter a correct list of valid mail domains.')]; |
| 155 |
$this->messages['config'][2] = ['ERROR', _('The IMAP admin password is empty.')]; |
| 156 |
$this->messages['managemailbox'][0] = ['ERROR', _('Unable to change ACL on IMAP server for mailbox deletion.')]; |
| 157 |
$this->messages['managemailbox'][1] = ['ERROR', _('Unable to delete mailbox from IMAP server.')]; |
| 158 |
$this->messages['managemailbox'][2] = ['ERROR', _('Unable to create mailbox on IMAP server.')]; |
| 159 |
$this->messages['managemailbox'][3] = ['ERROR', _('Unable to locate mailbox on IMAP.')]; |
| 160 |
$this->messages['managemailbox'][4] = ['ERROR', _('Your IMAP domains and email address domain do not match.')]; |
| 161 |
$this->messages['managemailbox'][5] = ['ERROR', _('Invalid password for IMAP admin or other problem occurred.')]; |
| 162 |
$this->messages['managemailbox'][6] = ['WARN', _('Your LAM login password was not accepted by the IMAP server.')]; |
| 163 |
$this->messages['managemailbox'][7] = ['ERROR', _('Cannot update quota.')]; |
| 164 |
$this->messages['managemailbox'][8] = ['ERROR', _('Wrong quota format. Quota must be numeric.')]; |
| 165 |
$this->messages['managemailbox'][9] = ['ERROR', _('Account %s:') . ' imapAccess_quota', _('Wrong quota format. Quota must be numeric.')]; |
| 166 |
$this->messages['managemailbox'][10] = ['ERROR', _('Cannot read quota.')]; |
| 167 |
$this->messages['createMailbox'][0] = ['ERROR', _('Account %s:') . ' imapAccess_createMailbox', _('This value can only be "true" or "false".')]; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Extracts user name and email address from inetOrgPerson/posixAccount/windowsUser modules. |
| 172 |
* |
| 173 |
* @param array<string, string[]|string> $attrs LDAP attributes (retrieved from other account modules if empty) |
| 174 |
* @return ?htmlStatusMessage message if any |
| 175 |
*/ |
| 176 |
private function extractUserAndEmail($attrs = null): ?htmlStatusMessage { |
| 177 |
$this->email = ''; |
| 178 |
if ($attrs === null) { |
| 179 |
if ($this->getAccountContainer()->getAccountModule('inetOrgPerson') != null) { |
| 180 |
$attrs = $this->getAccountContainer()->getAccountModule('inetOrgPerson')->getAttributes(); |
| 181 |
} |
| 182 |
else { |
| 183 |
$attrs = $this->getAccountContainer()->getAccountModule('windowsUser')->getAttributes(); |
| 184 |
} |
| 185 |
if ($this->getAccountContainer()->getAccountModule('posixAccount') != null) { |
| 186 |
$attrs = array_merge($attrs, $this->getAccountContainer()->getAccountModule('posixAccount')->getAttributes()); |
| 187 |
} |
| 188 |
} |
| 189 |
$this->email = empty($attrs['mail'][0]) ? '' : $attrs['mail'][0]; |
| 190 |
$this->user = ''; |
| 191 |
// extract user name from email address |
| 192 |
if (empty($this->moduleSettings['ImapAccess_UserNameAttribute'][0]) || $this->moduleSettings['ImapAccess_UserNameAttribute'][0] == 'mail') { |
| 193 |
$email_parts = explode('@', $this->email, 2); |
| 194 |
$this->user = array_shift($email_parts); |
| 195 |
} |
| 196 |
elseif ($this->moduleSettings['ImapAccess_UserNameAttribute'][0] == 'userPrincipalName') { |
| 197 |
if (!empty($attrs['userPrincipalName'][0])) { |
| 198 |
$parts = explode('@', $attrs['userPrincipalName'][0], 2); |
| 199 |
$this->user = array_shift($parts); |
| 200 |
} |
| 201 |
} |
| 202 |
// extract user name from Unix user name (might be in inetOrgPerson/windowUser or posixAccount module) |
| 203 |
else { |
| 204 |
$this->user = empty($attrs['uid'][0]) ? '' : $attrs['uid'][0]; |
| 205 |
} |
| 206 |
|
| 207 |
if (empty($this->email)) { |
| 208 |
$modName = ($this->getAccountContainer()->getAccountModule('inetOrgPerson') != null) ? 'inetOrgPerson' : 'windowsUser'; |
| 209 |
return new htmlStatusMessage('INFO', _("Please enter an email address on this page: %s"), '', [$this->getAccountContainer()->getAccountModule($modName)->get_alias()]); |
| 210 |
} |
| 211 |
return null; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* {@inheritDoc} |
| 216 |
*/ |
| 217 |
public function display_html_attributes(): htmlElement|array { |
| 218 |
$return = new htmlResponsiveRow(); |
| 219 |
if (!checkIfWriteAccessIsAllowed($this->get_scope())) { |
| 220 |
return $return; |
| 221 |
} |
| 222 |
$msg = $this->extractUserAndEmail(); |
| 223 |
if ($msg != null) { |
| 224 |
$return->add($msg); |
| 225 |
return $return; |
| 226 |
} |
| 227 |
$prefix = $this->getMailboxPrefix(); |
| 228 |
|
| 229 |
$email_domain = substr(strstr($this->email, '@'), 1); |
| 230 |
$adminPassword = $this->getAdminPassword(); // check for password for fall back mechanism |
| 231 |
if (!isset($_SESSION['imapAdmPass']) && !isset($adminPassword)) { |
| 232 |
return $this->display_html_password(); |
| 233 |
} |
| 234 |
|
| 235 |
$return->addLabel(new htmlOutputText(_('Email address'))); |
| 236 |
$return->addField(new htmlOutputText($this->email)); |
| 237 |
|
| 238 |
$adminUser = $this->getAdminUser(); |
| 239 |
$adminPassword = $this->getAdminPassword(); |
| 240 |
try { |
| 241 |
$client = $this->connect($adminUser, $adminPassword); |
| 242 |
} |
| 243 |
catch (LAMException $e) { |
| 244 |
return $this->display_html_password(new htmlStatusMessage('ERROR', $e->getTitle(), $e->getMessage())); |
| 245 |
} |
| 246 |
|
| 247 |
$return->addLabel(new htmlOutputText(_('Mailbox'))); |
| 248 |
$mailboxGroup = new htmlGroup(); |
| 249 |
$mailboxGroup->addElement(new htmlOutputText($prefix . $this->getSep() . $this->user)); |
| 250 |
$mailboxGroup->addElement(new htmlHelpLink('MailAddress')); |
| 251 |
$return->addField($mailboxGroup); |
| 252 |
$return->addVerticalSpacer('2rem'); |
| 253 |
|
| 254 |
if ($this->isMailboxExisting($client, $prefix . $this->getSep() . $this->user)) { |
| 255 |
$this->renderQuotasForMailbox($return, $client, $prefix . $this->getSep() . $this->user); |
| 256 |
$return->addVerticalSpacer('2rem'); |
| 257 |
$delButton = new htmlButton('deleteMailbox', _('Delete mailbox')); |
| 258 |
$delButton->setCSSClasses(['lam-danger']); |
| 259 |
$return->add($delButton, 12, 12, 12, 'text-center'); |
| 260 |
} |
| 261 |
else { |
| 262 |
$mailboxMessage = new htmlOutputText(_("Mailbox does not exist on IMAP server.")); |
| 263 |
$return->add($mailboxMessage, 12, 12, 12, 'text-center'); |
| 264 |
$return->addVerticalSpacer('2rem'); |
| 265 |
if ($this->isWrongDomain($email_domain)) { |
| 266 |
$return->add(new htmlStatusMessage('INFO', $this->messages['managemailbox'][4][1])); |
| 267 |
$return->addVerticalSpacer('1rem'); |
| 268 |
} |
| 269 |
else { |
| 270 |
$createButton = new htmlButton('createMailbox', _('Create mailbox')); |
| 271 |
$return->add($createButton, 12, 12, 12, 'text-center'); |
| 272 |
} |
| 273 |
} |
| 274 |
$this->doLogout($client); |
| 275 |
return $return; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Returns the HTML meta data for the password page. |
| 280 |
* |
| 281 |
* @param htmlStatusMessage|null $message status message |
| 282 |
* @return htmlResponsiveRow HTML meta data |
| 283 |
*/ |
| 284 |
public function display_html_password($message = null) { |
| 285 |
$return = new htmlResponsiveRow(); |
| 286 |
if ($message !== null) { |
| 287 |
$return->add($message); |
| 288 |
$return->addVerticalSpacer('1rem'); |
| 289 |
} |
| 290 |
$passwordInput = new htmlResponsiveInputField(_("Password of IMAP admin user"), 'ImapAdminPassword', '', 'ImapAdminPassword_Sess'); |
| 291 |
$passwordInput->setIsPassword(true); |
| 292 |
$passwordInput->setRequired(true); |
| 293 |
$passwordInput->setOnKeyPress('SubmitForm(\'enterPasswordButton\', event);'); |
| 294 |
$return->add($passwordInput); |
| 295 |
$return->addVerticalSpacer('2rem'); |
| 296 |
$return->add(new htmlButton('enterPasswordButton', _('Ok')), 12, 12, 12, 'text-center'); |
| 297 |
return $return; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Display the mailbox quota. |
| 302 |
* |
| 303 |
* @param htmlResponsiveRow $container structure that contained information to be displayed |
| 304 |
* @param Client $client IMAP client |
| 305 |
* @param String $username username to connect to IMAP server |
| 306 |
* @return htmlResponsiveRow table with added information about user quotas or controls to add quota |
| 307 |
*/ |
| 308 |
private function renderQuotasForMailbox($container, $client, $username) { |
| 309 |
if (($this->profileQuotaLimit !== null) && ($this->profileQuotaLimit !== '')) { |
| 310 |
try { |
| 311 |
$this->runSetQuotaCommand($client, $username, $this->profileQuotaLimit); |
| 312 |
} |
| 313 |
catch (LAMException $e) { |
| 314 |
logNewMessage(LOG_ERR, 'Unable to set quota for ' . $username . ': ' . $e->getMessage()); |
| 315 |
} |
| 316 |
$this->profileQuotaLimit = null; |
| 317 |
} |
| 318 |
try { |
| 319 |
$quota_values = $this->getQuota($client, $username); |
| 320 |
if (!empty($quota_values)) { |
| 321 |
if (isset($quota_values[self::QUOTA_USAGE])) { |
| 322 |
$quotaLimit = $quota_values[self::QUOTA_LIMIT]; |
| 323 |
$container->addLabel(new htmlOutputText(_("Current usage (kB)"))); |
| 324 |
$container->addField(new htmlOutputText($quota_values[self::QUOTA_USAGE])); |
| 325 |
$quotaLimitInput = new htmlResponsiveInputField(_("Quota limit (kB)"), 'ImapUserQuotaLimit', $quotaLimit, 'ImapUserQuotaLimit'); |
| 326 |
$container->add($quotaLimitInput); |
| 327 |
$container->addVerticalSpacer('0.5rem'); |
| 328 |
$container->add(new htmlButton('updateQuota', _('Update quota')), 12, 12, 12, 'text-center'); |
| 329 |
$container->addVerticalSpacer('1rem'); |
| 330 |
} |
| 331 |
} |
| 332 |
else { |
| 333 |
$quotaLimit = ""; |
| 334 |
$quotaLimitInput = new htmlResponsiveInputField(_("Quota limit (kB)"), 'ImapUserQuotaLimit', $quotaLimit, 'ImapUserQuotaLimit'); |
| 335 |
$container->add($quotaLimitInput); |
| 336 |
$container->addVerticalSpacer('0.5rem'); |
| 337 |
$container->add(new htmlButton('updateQuota', _('Update quota')), 12, 12, 12, 'text-center'); |
| 338 |
$container->addVerticalSpacer('1rem'); |
| 339 |
} |
| 340 |
} |
| 341 |
catch (Exception $e) { |
| 342 |
$container->add(new htmlStatusMessage('ERROR', $this->messages['managemailbox'][10][1], $e->getMessage())); |
| 343 |
} |
| 344 |
return $container; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* {@inheritDoc} |
| 349 |
*/ |
| 350 |
public function process_attributes(): array { |
| 351 |
$errors = []; |
| 352 |
if (!checkIfWriteAccessIsAllowed($this->get_scope())) { |
| 353 |
return $errors; |
| 354 |
} |
| 355 |
$prefix = $this->getMailboxPrefix(); |
| 356 |
|
| 357 |
$adminUser = $this->getAdminUser(); |
| 358 |
if (isset($_POST['ImapAdminPassword']) && isset($_POST['enterPasswordButton'])) { |
| 359 |
$errors = $this->doLogin(); |
| 360 |
} |
| 361 |
$adminPassword = $this->getAdminPassword(); |
| 362 |
|
| 363 |
try { |
| 364 |
$client = $this->connect($adminUser, $adminPassword); |
| 365 |
$this->extractUserAndEmail(); |
| 366 |
$email_domain = substr(strstr($this->email, '@'), 1); |
| 367 |
|
| 368 |
if (isset($_POST['deleteMailbox'])) { |
| 369 |
if ($this->isWrongDomain($email_domain)) { |
| 370 |
$errors[] = $this->messages['managemailbox'][4]; |
| 371 |
} |
| 372 |
else { |
| 373 |
$root = $prefix . $this->getSep() . $this->user; |
| 374 |
try { |
| 375 |
$this->deleteMailbox($client, $root); |
| 376 |
} |
| 377 |
catch (Exception $e) { |
| 378 |
$message = $this->messages['managemailbox'][1]; |
| 379 |
$message[] = $e->getMessage(); |
| 380 |
$errors[] = $message; |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
if (isset($_POST['createMailbox'])) { |
| 386 |
$createMessages = $this->createMailbox($client, $this->user, $email_domain); |
| 387 |
$errors = array_merge($errors, $createMessages); |
| 388 |
} |
| 389 |
if (isset($_POST['updateQuota'])) { |
| 390 |
$quota = $_POST['ImapUserQuotaLimit']; |
| 391 |
$quotaMessages = $this->setQuota($client, $this->user, $email_domain, $quota); |
| 392 |
$errors = array_merge($errors, $quotaMessages); |
| 393 |
} |
| 394 |
$this->doLogout($client); |
| 395 |
} |
| 396 |
catch (LAMException $e) { |
| 397 |
return [['ERROR', $e->getTitle(), $e->getMessage()]]; |
| 398 |
} |
| 399 |
// Return error-messages |
| 400 |
return $errors; |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Creates the mailbox for a user. |
| 405 |
* |
| 406 |
* @param Client $client IMAP client |
| 407 |
* @param string $userName user name |
| 408 |
* @param string $email_domain email domain |
| 409 |
* @return array<string|string[]>[] error messages |
| 410 |
*/ |
| 411 |
private function createMailbox($client, $userName, $email_domain): array { |
| 412 |
$errors = []; |
| 413 |
$prefix = $this->getMailboxPrefix(); |
| 414 |
if ($this->isWrongDomain($email_domain)) { |
| 415 |
$errors[] = $this->messages['managemailbox'][4]; |
| 416 |
} |
| 417 |
else { |
| 418 |
$root = $prefix . $this->getSep() . $userName; |
| 419 |
logNewMessage(LOG_DEBUG, 'Creating mailbox: ' . $root); |
| 420 |
try { |
| 421 |
$client->getConnection()->createFolder($root); |
| 422 |
logNewMessage(LOG_DEBUG, 'Mailbox created'); |
| 423 |
if (!$this->isMailboxExisting($client, $root)) { |
| 424 |
$errors[] = $this->messages['managemailbox'][3]; |
| 425 |
return $errors; |
| 426 |
} |
| 427 |
// create initial folders |
| 428 |
foreach ($this->getInitialFolders() as $folder) { |
| 429 |
$fullFolderName = $root . $this->getSep() . $folder; |
| 430 |
logNewMessage(LOG_DEBUG, 'Creating folder: ' . $fullFolderName); |
| 431 |
$client->getConnection()->createFolder($fullFolderName); |
| 432 |
logNewMessage(LOG_DEBUG, 'Folder created: ' . $fullFolderName); |
| 433 |
} |
| 434 |
} |
| 435 |
catch (Exception $e) { |
| 436 |
$message = $this->messages['managemailbox'][2]; |
| 437 |
$message[] = $e->getMessage(); |
| 438 |
$errors[] = $message; |
| 439 |
} |
| 440 |
} |
| 441 |
return $errors; |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Sets the mailbox quota for a user. |
| 446 |
* |
| 447 |
* @param Client $client IMAP client |
| 448 |
* @param string $userName user name |
| 449 |
* @param string $email_domain email domain |
| 450 |
* @param string $quota mailbox quota |
| 451 |
* @return array<string|string[]>[] error messages |
| 452 |
*/ |
| 453 |
private function setQuota($client, $userName, $email_domain, $quota) { |
| 454 |
$prefix = $this->getMailboxPrefix(); |
| 455 |
$errors = []; |
| 456 |
$root = $prefix . $this->getSep() . $userName; |
| 457 |
if ($this->isWrongDomain($email_domain)) { |
| 458 |
$errors[] = $this->messages['managemailbox'][4]; |
| 459 |
} |
| 460 |
elseif ($quota == '') { |
| 461 |
try { |
| 462 |
$this->runSetQuotaCommand($client, $root, '-1'); |
| 463 |
} |
| 464 |
catch (Exception $e) { |
| 465 |
$message = $this->messages['managemailbox'][7]; |
| 466 |
$message[] = $e->getMessage(); |
| 467 |
$errors[] = $message; |
| 468 |
} |
| 469 |
} |
| 470 |
elseif (get_preg($quota, 'digit')) { |
| 471 |
logNewMessage(LOG_DEBUG, 'Setting quota ' . $quota . ' for ' . $root); |
| 472 |
try { |
| 473 |
$this->runSetQuotaCommand($client, $root, $quota); |
| 474 |
} |
| 475 |
catch (Exception $e) { |
| 476 |
$message = $this->messages['managemailbox'][7]; |
| 477 |
$message[] = $e->getMessage(); |
| 478 |
$errors[] = $message; |
| 479 |
} |
| 480 |
} |
| 481 |
else { |
| 482 |
$errors[] = $this->messages['managemailbox'][8]; |
| 483 |
} |
| 484 |
return $errors; |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
* {@inheritDoc} |
| 489 |
*/ |
| 490 |
public function load_profile(array $profile): void { |
| 491 |
// profile mappings in meta data |
| 492 |
parent::load_profile($profile); |
| 493 |
if (isset($profile['ImapAccess_QuotaLimit'][0]) && $profile['ImapAccess_QuotaLimit'][0] != '') { |
| 494 |
$this->profileQuotaLimit = $profile['ImapAccess_QuotaLimit'][0]; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* {@inheritDoc} |
| 500 |
*/ |
| 501 |
public function get_configOptions(array $scopes, array $allScopes): htmlElement|array { |
| 502 |
// configuration settings |
| 503 |
$configContainer = new htmlResponsiveRow(); |
| 504 |
$configServer = new htmlResponsiveInputField(_('Server address'), 'ImapAccess_ImapServerAddress', '', 'ImapServerAddress'); |
| 505 |
$configServer->setRequired(true); |
| 506 |
$configContainer->add($configServer); |
| 507 |
$configContainer->add(new htmlResponsiveSelect('ImapAccess_ImapServerEncriptionProtocol', ['TLS', 'SSL'], ['TLS'], _("Encryption protocol"), 'ImapServerEncryptionProtocol')); |
| 508 |
$configCertValidate = new htmlResponsiveSelect('ImapAccess_ImapValidateServerCert', [_('Yes') => 'validate-cert', _('No') => 'novalidate-cert'], ['validate-cert'], _("Validate server certificate"), 'ImapValidateServerCert'); |
| 509 |
$configCertValidate->setHasDescriptiveElements(true); |
| 510 |
$configContainer->add($configCertValidate); |
| 511 |
$configUser = new htmlResponsiveInputField(_('IMAP admin user'), 'ImapAccess_ImapAdmin', '', 'ImapAdmin'); |
| 512 |
$configUser->setRequired(true); |
| 513 |
$configContainer->add($configUser); |
| 514 |
$pwdSelectOptions = [ |
| 515 |
_('LAM user password') => 'lam_user_pass', |
| 516 |
_('Ask') => 'ask_pass', |
| 517 |
_('Server profile') => 'config']; |
| 518 |
$configPasswordType = new htmlResponsiveSelect('ImapAccess_ImapAdminPasswordSelect', $pwdSelectOptions, ['ask_pass'], _("IMAP password input"), 'ImapAdminPasswordSelect'); |
| 519 |
$configPasswordType->setHasDescriptiveElements(true); |
| 520 |
$configPasswordType->setTableRowsToShow(['config' => ['ImapAccess_ImapAdminPassword']]); |
| 521 |
$configPasswordType->setTableRowsToHide(['lam_user_pass' => ['ImapAccess_ImapAdminPassword'], 'ask_pass' => ['ImapAccess_ImapAdminPassword']]); |
| 522 |
$configContainer->add($configPasswordType); |
| 523 |
$adminPwdInput = new htmlResponsiveInputField(_('Admin password'), 'ImapAccess_ImapAdminPassword', null, 'ImapAdminPasswordSelect'); |
| 524 |
$adminPwdInput->setIsPassword(true); |
| 525 |
$adminPwdInput->setObfuscate(true); |
| 526 |
$configContainer->add($adminPwdInput); |
| 527 |
$mailDomainsInput = new htmlResponsiveInputField(_('Mail domains'), 'ImapAccess_ImapDomain', '', 'ImapMailDomain'); |
| 528 |
$mailDomainsInput->setRequired(true); |
| 529 |
$configContainer->add($mailDomainsInput); |
| 530 |
$configContainer->add(new htmlResponsiveInputField(_('Prefix for mailboxes'), 'ImapAccess_ImapUserPrefix', '', 'ImapUserPrefix')); |
| 531 |
$configContainer->add(new htmlResponsiveInputTextarea('ImapAccess_initialFolders', '', 10, 3, _('Initial folders'), 'initialFolders')); |
| 532 |
$configUserName = new htmlResponsiveSelect('ImapAccess_UserNameAttribute', ['mail', 'uid', 'userPrincipalName'], ['mail'], _("User name attribute"), 'ImapUserNameAttr'); |
| 533 |
$configContainer->add($configUserName); |
| 534 |
$configPathSeparator = new htmlResponsiveSelect('ImapAccess_pathSeparator', ['.', '/'], ['.'], _("Path separator"), 'pathSeparator'); |
| 535 |
$configContainer->add($configPathSeparator); |
| 536 |
return $configContainer; |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* {@inheritDoc} |
| 541 |
*/ |
| 542 |
public function check_configOptions(array $typeIds, array &$options): array { |
| 543 |
$errors = parent::check_configOptions($typeIds, $options); |
| 544 |
if (($options['ImapAccess_ImapAdminPasswordSelect'][0] == 'config') |
| 545 |
&& empty($options['ImapAccess_ImapAdminPassword'][0])) { |
| 546 |
$errors[] = $this->messages['config'][2]; |
| 547 |
} |
| 548 |
return $errors; |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* Returns the user name of the IMAP admin. |
| 553 |
* |
| 554 |
* @return String admin user name |
| 555 |
*/ |
| 556 |
private function getAdminUser() { |
| 557 |
if (isset($_SESSION['imapAdmUser'])) { |
| 558 |
return $_SESSION['imapAdmUser']; |
| 559 |
} |
| 560 |
$user = $this->moduleSettings['ImapAccess_ImapAdmin'][0]; |
| 561 |
// check if username contains any wildcards that need to be replaced with LDAP attribute values |
| 562 |
$matches = []; |
| 563 |
$isMatching = preg_match_all('/\\$[a-z0-9_-]+\\$/i', $this->moduleSettings['ImapAccess_ImapAdmin'][0], $matches); |
| 564 |
if ($isMatching) { |
| 565 |
// find wildcards |
| 566 |
$attrNames = []; |
| 567 |
foreach ($matches as $match) { |
| 568 |
foreach ($match as $attr) { |
| 569 |
$attrNames[] = substr($attr, 1, -1); |
| 570 |
} |
| 571 |
} |
| 572 |
$attrNames = array_values(array_unique($attrNames)); |
| 573 |
$attrNames = array_change_key_case($attrNames); |
| 574 |
// read LAM login user data |
| 575 |
$dn = $_SESSION['ldap']->getUserName(); |
| 576 |
$sr = @ldap_read($_SESSION['ldap']->server(), $dn, '(objectclass=*)', $attrNames, 0, 0, 0, LDAP_DEREF_NEVER); |
| 577 |
if ($sr) { |
| 578 |
$info = @ldap_get_entries($_SESSION['ldap']->server(), $sr); |
| 579 |
if ($info) { |
| 580 |
cleanLDAPResult($info); |
| 581 |
$info = $info[0]; |
| 582 |
} |
| 583 |
} |
| 584 |
// replace wildcards |
| 585 |
foreach ($attrNames as $attr) { |
| 586 |
if (empty($info[$attr])) { |
| 587 |
continue; |
| 588 |
} |
| 589 |
$user = preg_replace('/\\$' . $attr . '\\$/i', $info[$attr][0], $user); |
| 590 |
} |
| 591 |
} |
| 592 |
logNewMessage(LOG_DEBUG, 'IMAP admin user: ' . $user); |
| 593 |
$_SESSION['imapAdmUser'] = $user; |
| 594 |
return $user; |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* Returns the admin password. |
| 599 |
* |
| 600 |
* @return string|null password |
| 601 |
*/ |
| 602 |
private function getAdminPassword() { |
| 603 |
//perform admin password |
| 604 |
$password = null; //default value is null, it can be changed during the work |
| 605 |
if (isset($_SESSION['imapAdmPass'])) { |
| 606 |
$password = lamDecrypt($_SESSION['imapAdmPass']); |
| 607 |
} |
| 608 |
elseif (isset($this->moduleSettings['ImapAccess_ImapAdminPasswordSelect'][0]) && ($this->moduleSettings['ImapAccess_ImapAdminPasswordSelect'][0] == "lam_user_pass")) { |
| 609 |
$password = $_SESSION['ldap']->getPassword(); |
| 610 |
} |
| 611 |
elseif (!empty($this->moduleSettings['ImapAccess_ImapAdminPasswordSelect'][0]) && ($this->moduleSettings['ImapAccess_ImapAdminPasswordSelect'][0] == "config") |
| 612 |
&& !empty($this->moduleSettings['ImapAccess_ImapAdminPassword'][0])) { |
| 613 |
$password = deobfuscateText($this->moduleSettings['ImapAccess_ImapAdminPassword'][0]); |
| 614 |
} |
| 615 |
return $password; |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
* Checks the password given by user and save it as session parameter. |
| 620 |
* |
| 621 |
* @return array<string|string[]>[] list of error messages |
| 622 |
*/ |
| 623 |
private function doLogin(): array { |
| 624 |
$errors = []; |
| 625 |
$adminUser = $this->getAdminUser(); |
| 626 |
if (isset($_POST['ImapAdminPassword']) && $_POST['ImapAdminPassword'] != "") { |
| 627 |
$adminPassword = $_POST['ImapAdminPassword']; |
| 628 |
try { |
| 629 |
$client = $this->connect($adminUser, $adminPassword); |
| 630 |
$_SESSION['imapAdmPass'] = lamEncrypt($_POST['ImapAdminPassword']); |
| 631 |
$this->doLogout($client); |
| 632 |
} |
| 633 |
catch (LAMException $e) { |
| 634 |
$error = $this->messages['managemailbox'][5]; |
| 635 |
$error[] = $e->getMessage(); |
| 636 |
$errors[] = $error; |
| 637 |
} |
| 638 |
} |
| 639 |
return $errors; |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
* Closes the IMAP connection. |
| 644 |
* |
| 645 |
* @param Client $client client |
| 646 |
*/ |
| 647 |
private function doLogout($client): void { |
| 648 |
$client->disconnect(); |
| 649 |
} |
| 650 |
|
| 651 |
/** |
| 652 |
* Connects to the IMAP server. |
| 653 |
* |
| 654 |
* @param string $user user name |
| 655 |
* @param string $password password |
| 656 |
* @return Client IMAP client |
| 657 |
* @throws LAMException error during connect |
| 658 |
*/ |
| 659 |
private function connect($user, $password): Client { |
| 660 |
include_once __DIR__ . '/../3rdParty/composer/autoload.php'; |
| 661 |
$encryptionType = $this->moduleSettings['ImapAccess_ImapServerEncriptionProtocol'][0]; |
| 662 |
if (strrpos($this->moduleSettings['ImapAccess_ImapServerAddress'][0], ":")) { |
| 663 |
$port = substr(strstr($this->moduleSettings['ImapAccess_ImapServerAddress'][0], ':'), 1); |
| 664 |
$addressParts = explode(':', $this->moduleSettings['ImapAccess_ImapServerAddress'][0], 2); |
| 665 |
$serverName = array_shift($addressParts); |
| 666 |
} |
| 667 |
else { |
| 668 |
$serverName = $this->moduleSettings['ImapAccess_ImapServerAddress'][0]; |
| 669 |
$port = ($encryptionType === "TLS") ? 143 : 993; |
| 670 |
} |
| 671 |
$validateCertificate = !isset($this->moduleSettings['ImapAccess_ImapValidateServerCert'][0]) || ($this->moduleSettings['ImapAccess_ImapValidateServerCert'][0] !== 'novalidate-cert'); |
| 672 |
try { |
| 673 |
$encryption = ($encryptionType === 'SSL') ? 'ssl' : 'starttls'; |
| 674 |
$cm = new ClientManager([]); |
| 675 |
$options = [ |
| 676 |
'username' => $user, |
| 677 |
'password' => $password, |
| 678 |
'protocol' => 'imap', |
| 679 |
'host' => $serverName, |
| 680 |
'port' => $port, |
| 681 |
'encryption' => $encryption, |
| 682 |
'validate_cert' => $validateCertificate |
| 683 |
]; |
| 684 |
$client = $cm->make($options); |
| 685 |
$client->connect(); |
| 686 |
return $client; |
| 687 |
} |
| 688 |
catch (Exception $e) { |
| 689 |
throw new LAMException(_('Unable to connect to IMAP server.'), $e->getMessage(), $e); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
* This function returns the prefix for mailboxes. |
| 695 |
* If no prefix was given during configuration then "user" will be used (default for Cyrus). |
| 696 |
* |
| 697 |
* @return string prefix |
| 698 |
*/ |
| 699 |
private function getMailboxPrefix() { |
| 700 |
if (!isset($this->moduleSettings['ImapAccess_ImapUserPrefix'][0]) || ($this->moduleSettings['ImapAccess_ImapUserPrefix'][0] == '')) { |
| 701 |
return "user"; |
| 702 |
} |
| 703 |
else { |
| 704 |
return $this->moduleSettings['ImapAccess_ImapUserPrefix'][0]; |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
/** |
| 709 |
* This function checks if the domain of the mailbox is not in the list of domains listed in the configuration. |
| 710 |
* If it is in the list then it returns false, otherwise returns true. If the list of domains is not set then it returns true. |
| 711 |
* |
| 712 |
* @param String $email_domain email domain |
| 713 |
* @return boolean true if domains match |
| 714 |
*/ |
| 715 |
private function isWrongDomain($email_domain) { |
| 716 |
if (isset($this->moduleSettings['ImapAccess_ImapDomain'][0])) { |
| 717 |
$domain_list_string = $this->moduleSettings['ImapAccess_ImapDomain'][0]; |
| 718 |
if (empty($domain_list_string) || ($domain_list_string == '*')) { |
| 719 |
return false; |
| 720 |
} |
| 721 |
$domains_array = explode(",", $domain_list_string); |
| 722 |
if (in_array($email_domain, $domains_array)) { |
| 723 |
return false; |
| 724 |
} |
| 725 |
} |
| 726 |
else { |
| 727 |
return false; |
| 728 |
} |
| 729 |
return true; |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* Returns the path separator. |
| 734 |
* |
| 735 |
* @return string separator char |
| 736 |
*/ |
| 737 |
private function getSep(): string { |
| 738 |
return $this->moduleSettings['ImapAccess_pathSeparator'][0] ?? '.'; |
| 739 |
} |
| 740 |
|
| 741 |
/** |
| 742 |
* Returns the list of initial folders to create for a new mailbox. |
| 743 |
* |
| 744 |
* @return string[] list of folders |
| 745 |
*/ |
| 746 |
private function getInitialFolders(): array { |
| 747 |
$list = []; |
| 748 |
if (!empty($this->moduleSettings['ImapAccess_initialFolders'])) { |
| 749 |
foreach ($this->moduleSettings['ImapAccess_initialFolders'] as $folder) { |
| 750 |
$folder = trim($folder); |
| 751 |
if (!empty($folder)) { |
| 752 |
$list[] = $folder; |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
return $list; |
| 757 |
} |
| 758 |
|
| 759 |
/** |
| 760 |
* {@inheritDoc} |
| 761 |
*/ |
| 762 |
public function get_uploadColumns(array $selectedModules, ConfiguredType $type): array { |
| 763 |
$pwd = $this->getAdminPassword(); |
| 764 |
if (empty($pwd)) { |
| 765 |
return []; |
| 766 |
} |
| 767 |
return [ |
| 768 |
[ |
| 769 |
'name' => 'imapAccess_createMailbox', |
| 770 |
'description' => _('Create mailbox'), |
| 771 |
'example' => 'false', |
| 772 |
'default' => 'false', |
| 773 |
'values' => 'true, false', |
| 774 |
'help' => 'createMailbox' |
| 775 |
], |
| 776 |
[ |
| 777 |
'name' => 'imapAccess_quota', |
| 778 |
'description' => _('Quota limit (kB)'), |
| 779 |
'example' => '1000000', |
| 780 |
'help' => 'ImapUserQuotaLimit' |
| 781 |
], |
| 782 |
]; |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* {@inheritDoc} |
| 787 |
*/ |
| 788 |
public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array { |
| 789 |
$errors = []; |
| 790 |
if (!isset($ids['imapAccess_createMailbox'])) { |
| 791 |
return $errors; |
| 792 |
} |
| 793 |
for ($i = 0; $i < count($rawAccounts); $i++) { |
| 794 |
if (isset($rawAccounts[$i][$ids['imapAccess_createMailbox']]) |
| 795 |
&& !in_array($rawAccounts[$i][$ids['imapAccess_createMailbox']], ['true', 'false'])) { |
| 796 |
$errMsg = $this->messages['createMailbox'][0]; |
| 797 |
$errMsg[] = [$i]; |
| 798 |
$errors[] = $errMsg; |
| 799 |
} |
| 800 |
if (isset($rawAccounts[$i][$ids['imapAccess_createMailbox']]) |
| 801 |
&& ($rawAccounts[$i][$ids['imapAccess_createMailbox']] === 'true') |
| 802 |
&& !empty($ids['imapAccess_quota']) |
| 803 |
&& isset($rawAccounts[$i][$ids['imapAccess_quota']]) |
| 804 |
&& !get_preg($rawAccounts[$i][$ids['imapAccess_quota']], 'digit')) { |
| 805 |
$errMsg = $this->messages['managemailbox'][9]; |
| 806 |
$errMsg[] = [$i]; |
| 807 |
$errors[] = $errMsg; |
| 808 |
} |
| 809 |
} |
| 810 |
return $errors; |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* {@inheritDoc} |
| 815 |
*/ |
| 816 |
public function doUploadPostActions(array &$data, array $ids, array $failed, array &$temp, array &$accounts, |
| 817 |
array $selectedModules, ConfiguredType $type): array { |
| 818 |
if (!checkIfWriteAccessIsAllowed($this->get_scope())) { |
| 819 |
die(); |
| 820 |
} |
| 821 |
// on first call generate list of IMAP operations |
| 822 |
if (!isset($temp['counter'])) { |
| 823 |
$temp['users'] = []; |
| 824 |
$temp['counter'] = 0; |
| 825 |
$errors = []; |
| 826 |
if (isset($ids['imapAccess_createMailbox'])) { |
| 827 |
foreach ($data as $i => $dataRow) { |
| 828 |
if (in_array($i, $failed)) { |
| 829 |
continue; // ignore failed accounts |
| 830 |
} |
| 831 |
if ($dataRow[$ids['imapAccess_createMailbox']] === 'true') { |
| 832 |
$limit = ''; |
| 833 |
if (isset($ids['imapAccess_quota']) |
| 834 |
&& isset($dataRow[$ids['imapAccess_quota']]) |
| 835 |
&& ($dataRow[$ids['imapAccess_quota']] !== '')) { |
| 836 |
$limit = $dataRow[$ids['imapAccess_quota']]; |
| 837 |
} |
| 838 |
$attributes = $accounts[$i]; |
| 839 |
foreach ($attributes as $name => $value) { |
| 840 |
if (!is_array($value)) { |
| 841 |
$attributes[$name] = [$value]; |
| 842 |
} |
| 843 |
} |
| 844 |
$extractError = $this->extractUserAndEmail($attributes); |
| 845 |
if ($extractError !== null) { |
| 846 |
$errors[] = $extractError; |
| 847 |
} |
| 848 |
$temp['users'][] = [ |
| 849 |
'uid' => $this->user, |
| 850 |
'limit' => $limit, |
| 851 |
'email' => substr(strstr($this->email, '@'), 1) |
| 852 |
]; |
| 853 |
} |
| 854 |
} |
| 855 |
} |
| 856 |
return [ |
| 857 |
'status' => 'inProgress', |
| 858 |
'progress' => 0, |
| 859 |
'errors' => $errors |
| 860 |
]; |
| 861 |
} |
| 862 |
// add mailbox |
| 863 |
elseif ($temp['counter'] < count($temp['users'])) { |
| 864 |
$errors = []; |
| 865 |
$data = $temp['users'][$temp['counter']]; |
| 866 |
$uid = $data['uid']; |
| 867 |
$limit = $data['limit']; |
| 868 |
$email_domain = $data['email']; |
| 869 |
ob_start(); |
| 870 |
$imapUser = $this->getAdminUser(); |
| 871 |
$imapPassword = $this->getAdminPassword(); |
| 872 |
try { |
| 873 |
$client = $this->connect($imapUser, $imapPassword); |
| 874 |
$prefix = $this->getMailboxPrefix(); |
| 875 |
if (!$this->isMailboxExisting($client, $prefix . $this->getSep() . $uid)) { |
| 876 |
$createErrors = $this->createMailbox($client, $uid, $email_domain); |
| 877 |
$errors = array_merge($errors, $createErrors); |
| 878 |
if (empty($createErrors)) { |
| 879 |
$quotaErrors = $this->setQuota($client, $uid, $email_domain, $limit); |
| 880 |
$errors = array_merge($errors, $quotaErrors); |
| 881 |
} |
| 882 |
} |
| 883 |
$this->doLogout($client); |
| 884 |
} |
| 885 |
catch (Exception $e) { |
| 886 |
$message = $this->messages['managemailbox'][5]; |
| 887 |
$message[] = $e->getMessage(); |
| 888 |
$errors[] = $message; |
| 889 |
} |
| 890 |
ob_end_clean(); |
| 891 |
$temp['counter']++; |
| 892 |
return [ |
| 893 |
'status' => 'inProgress', |
| 894 |
'progress' => ($temp['counter'] * 100) / count($temp['users']), |
| 895 |
'errors' => $errors |
| 896 |
]; |
| 897 |
} |
| 898 |
// all modifications are done |
| 899 |
else { |
| 900 |
return [ |
| 901 |
'status' => 'finished', |
| 902 |
'progress' => 100, |
| 903 |
'errors' => [] |
| 904 |
]; |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
/** |
| 909 |
* Returns if the mailbox exists. |
| 910 |
* |
| 911 |
* @param Client $client IMAP client |
| 912 |
* @param string $path mailbox path |
| 913 |
* @return bool is existing |
| 914 |
*/ |
| 915 |
private function isMailboxExisting($client, string $path): bool { |
| 916 |
$list = $client->getConnection()->folders($path); |
| 917 |
return is_array($list->data()) && (count($list->data()) >= 1); |
| 918 |
} |
| 919 |
|
| 920 |
/** |
| 921 |
* Returns the quota data for the user. |
| 922 |
* |
| 923 |
* @param Client $client IMAP client |
| 924 |
* @param string $user user name |
| 925 |
* @return array<string, string> array with keys QUOTA_USAGE and QUOTA_LIMIT |
| 926 |
*/ |
| 927 |
private function getQuota($client, string $user): array { |
| 928 |
$connection = $client->getConnection(); |
| 929 |
if (!($connection instanceof ImapProtocol)) { |
| 930 |
return []; |
| 931 |
} |
| 932 |
$user = $connection->escapeString($user); |
| 933 |
$quota_values = []; |
| 934 |
try { |
| 935 |
$quota = $connection->requestAndResponse('GETQUOTA', [$user]); |
| 936 |
$quota = $quota->data(); |
| 937 |
if (isset($quota[0][2][0]) && ($quota[0][2][0] === 'STORAGE')) { |
| 938 |
$quota_values = [ |
| 939 |
self::QUOTA_LIMIT => $quota[0][2][2], |
| 940 |
self::QUOTA_USAGE => $quota[0][2][1], |
| 941 |
]; |
| 942 |
} |
| 943 |
} |
| 944 |
catch (Exception $e) { |
| 945 |
logNewMessage(LOG_DEBUG, 'Unable to read quote of ' . $user . ': ' . $e->getMessage()); |
| 946 |
} |
| 947 |
return $quota_values; |
| 948 |
} |
| 949 |
|
| 950 |
/** |
| 951 |
* Sets the quota for a user. |
| 952 |
* |
| 953 |
* @param Client $client client |
| 954 |
* @param string $user user name |
| 955 |
* @param string $value quota value |
| 956 |
* @throws LAMException error setting quota |
| 957 |
*/ |
| 958 |
private function runSetQuotaCommand($client, string $user, string $value): void { |
| 959 |
$connection = $client->getConnection(); |
| 960 |
if (!($connection instanceof ImapProtocol)) { |
| 961 |
throw new LAMException(null); |
| 962 |
} |
| 963 |
$user = $connection->escapeString($user); |
| 964 |
$result = $connection->requestAndResponse('SETQUOTA', [$user, '(STORAGE ' . $value . ')']); |
| 965 |
$result = $result->data(); |
| 966 |
if (isset($result[0][0]) && ($result[0][0] === 'OK')) { |
| 967 |
return; |
| 968 |
} |
| 969 |
array_shift($result); |
| 970 |
$message = implode(' ', $result); |
| 971 |
throw new LAMException(null, $message); |
| 972 |
} |
| 973 |
|
| 974 |
/** |
| 975 |
* Deletes a mailbox on the server. |
| 976 |
* |
| 977 |
* @param Client $client client |
| 978 |
* @param string $user user |
| 979 |
* @throws LAMException error deleting mailbox |
| 980 |
*/ |
| 981 |
private function deleteMailbox($client, $user): void { |
| 982 |
$connection = $client->getConnection(); |
| 983 |
if (!($connection instanceof ImapProtocol)) { |
| 984 |
throw new LAMException(null); |
| 985 |
} |
| 986 |
$userEscaped = $connection->escapeString($user); |
| 987 |
$adminUser = $connection->escapeString($this->getAdminUser()); |
| 988 |
$result = $connection->requestAndResponse('SETACL', [$userEscaped, $adminUser, '+c']); |
| 989 |
$result = $result->data(); |
| 990 |
if (!isset($result[0][0]) || ($result[0][0] !== 'OK')) { |
| 991 |
throw new LAMException(null); |
| 992 |
} |
| 993 |
$result = $connection->deleteFolder($user); |
| 994 |
if (!$result->successful()) { |
| 995 |
throw new LAMException(null); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
} |