|    1:  | <?php | 
|    2:  |  | 
|    3:  |  | 
|    4:  |  | 
|    5:  |  | 
|    6:  |  | 
|    7:  |  | 
|    8:  | namespace Aurora\Modules\Mail; | 
|    9:  |  | 
|   10:  | use Aurora\Api; | 
|   11:  | use Aurora\Modules\Core\Models\Tenant; | 
|   12:  | use Aurora\Modules\Core\Models\User; | 
|   13:  | use Aurora\Modules\Mail\Classes\Message; | 
|   14:  | use Aurora\Modules\Mail\Enums\SearchInFoldersType; | 
|   15:  | use Aurora\Modules\Mail\Models\Identity; | 
|   16:  | use Aurora\Modules\Mail\Models\TrustedSender; | 
|   17:  | use Aurora\System\Exceptions\InvalidArgumentException; | 
|   18:  | use PHPMailer\DKIMValidator\Validator as DKIMValidator; | 
|   19:  | use PHPMailer\DKIMValidator\DKIMException; | 
|   20:  |  | 
|   21:  |  | 
|   22:  |  | 
|   23:  |  | 
|   24:  |  | 
|   25:  |  | 
|   26:  |  | 
|   27:  |  | 
|   28:  | class Module extends \Aurora\System\Module\AbstractModule | 
|   29:  | { | 
|   30:  |      | 
|   31:  |  | 
|   32:  |  | 
|   33:  |     protected $oMailManager = null; | 
|   34:  |  | 
|   35:  |      | 
|   36:  |  | 
|   37:  |  | 
|   38:  |     protected $oAccountsManager = null; | 
|   39:  |  | 
|   40:  |      | 
|   41:  |  | 
|   42:  |  | 
|   43:  |     protected $oServersManager = null; | 
|   44:  |  | 
|   45:  |      | 
|   46:  |  | 
|   47:  |  | 
|   48:  |     protected $oIdentitiesManager = null; | 
|   49:  |  | 
|   50:  |      | 
|   51:  |  | 
|   52:  |  | 
|   53:  |     protected $oSieveManager = null; | 
|   54:  |  | 
|   55:  |      | 
|   56:  |  | 
|   57:  |  | 
|   58:  |     protected $oFilecacheManager = null; | 
|   59:  |  | 
|   60:  |      | 
|   61:  |  | 
|   62:  |  | 
|   63:  |  | 
|   64:  |     public static function getInstance() | 
|   65:  |     { | 
|   66:  |         return \Aurora\System\Api::GetModule(self::GetName()); | 
|   67:  |     } | 
|   68:  |  | 
|   69:  |      | 
|   70:  |  | 
|   71:  |  | 
|   72:  |  | 
|   73:  |  | 
|   74:  |     public function init() | 
|   75:  |     { | 
|   76:  |         $this->aErrors = [ | 
|   77:  |             Enums\ErrorCodes::CannotConnectToMailServer				=> $this->i18N('ERROR_CONNECT_TO_MAIL_SERVER'), | 
|   78:  |             Enums\ErrorCodes::CannotLoginCredentialsIncorrect		=> $this->i18N('ERROR_CREDENTIALS_INCORRECT'), | 
|   79:  |             Enums\ErrorCodes::FolderAlreadyExists					=> $this->i18N('ERROR_FOLDER_EXISTS'), | 
|   80:  |             Enums\ErrorCodes::FolderNameContainsDelimiter			=> $this->i18N('ERROR_FOLDER_NAME_CONTAINS_DELIMITER'), | 
|   81:  |             Enums\ErrorCodes::CannotRenameNonExistenFolder			=> $this->i18N('ERROR_RENAME_NONEXISTEN_FOLDER'), | 
|   82:  |             Enums\ErrorCodes::CannotGetMessage						=> $this->i18N('ERROR_GET_MESSAGE'), | 
|   83:  |             Enums\ErrorCodes::CannotMoveMessage						=> $this->i18N('ERROR_MOVE_MESSAGE'), | 
|   84:  |             Enums\ErrorCodes::CannotSendMessageInvalidRecipients	=> $this->i18N('ERROR_SEND_MESSAGE_INVALID_RECIPIENTS'), | 
|   85:  |             Enums\ErrorCodes::CannotSendMessageToRecipients			=> $this->i18N('ERROR_SEND_MESSAGE_TO_RECIPIENTS'), | 
|   86:  |             Enums\ErrorCodes::CannotSendMessageToExternalRecipients	=> $this->i18N('ERROR_SEND_MESSAGE_TO_EXTERNAL_RECIPIENTS'), | 
|   87:  |             Enums\ErrorCodes::CannotSaveMessageToSentItems			=> $this->i18N('ERROR_SEND_MESSAGE_NOT_SAVED'), | 
|   88:  |             Enums\ErrorCodes::CannotUploadMessage					=> $this->i18N('ERROR_UPLOAD_MESSAGE'), | 
|   89:  |             Enums\ErrorCodes::CannotUploadMessageFileNotEml			=> $this->i18N('ERROR_UPLOAD_MESSAGE_FILE_NOT_EML'), | 
|   90:  |             Enums\ErrorCodes::DomainIsNotAllowedForLoggingIn		=> $this->i18N('DOMAIN_IS_NOT_ALLOWED_FOR_LOGGING_IN'), | 
|   91:  |             Enums\ErrorCodes::TenantQuotaExceeded					=> $this->i18N('ERROR_TENANT_QUOTA_EXCEEDED'), | 
|   92:  |         ]; | 
|   93:  |  | 
|   94:  |         $this->AddEntries( | 
|   95:  |             array( | 
|   96:  |                 'message-newtab' => 'EntryMessageNewtab', | 
|   97:  |                 'mail-attachment' => 'EntryDownloadAttachment', | 
|   98:  |                 'mail-attachments-cookieless' => 'EntryDownloadAttachmentCookieless' | 
|   99:  |             ) | 
|  100:  |         ); | 
|  101:  |  | 
|  102:  |         $this->subscribeEvent('Login', array($this, 'onLogin')); | 
|  103:  |         $this->subscribeEvent('Core::DeleteUser::before', array($this, 'onBeforeDeleteUser')); | 
|  104:  |         $this->subscribeEvent('Core::GetAccounts', array($this, 'onGetAccounts')); | 
|  105:  |         $this->subscribeEvent('Autodiscover::GetAutodiscover::after', array($this, 'onAfterGetAutodiscover')); | 
|  106:  |         $this->subscribeEvent('Core::DeleteTenant::after', array($this, 'onAfterDeleteTenant')); | 
|  107:  |         $this->subscribeEvent('Core::DeleteUser::after', array($this, 'onAfterDeleteUser')); | 
|  108:  |         $this->subscribeEvent('Core::GetDigestHash', array($this, 'onGetDigestHash')); | 
|  109:  |         $this->subscribeEvent('Core::GetAccountUsedToAuthorize', array($this, 'onGetAccountUsedToAuthorize')); | 
|  110:  |         $this->subscribeEvent('System::RunEntry::before', array($this, 'onBeforeRunEntry')); | 
|  111:  |         $this->subscribeEvent('System::CastExtendedProp', array($this, 'onCastExtendedProp')); | 
|  112:  |  | 
|  113:  |         \MailSo\Config::$PreferStartTlsIfAutoDetect = !!$this->getConfig('PreferStarttls', true); | 
|  114:  |     } | 
|  115:  |  | 
|  116:  |      | 
|  117:  |  | 
|  118:  |  | 
|  119:  |     public static function Decorator() | 
|  120:  |     { | 
|  121:  |         return parent::Decorator(); | 
|  122:  |     } | 
|  123:  |  | 
|  124:  |      | 
|  125:  |  | 
|  126:  |  | 
|  127:  |  | 
|  128:  |     public function getAccountsManager() | 
|  129:  |     { | 
|  130:  |         if ($this->oAccountsManager === null) { | 
|  131:  |             $this->oAccountsManager = new Managers\Accounts\Manager($this); | 
|  132:  |         } | 
|  133:  |  | 
|  134:  |         return $this->oAccountsManager; | 
|  135:  |     } | 
|  136:  |  | 
|  137:  |     public function setAccountsManager($oManager) | 
|  138:  |     { | 
|  139:  |         $this->oAccountsManager = $oManager; | 
|  140:  |     } | 
|  141:  |  | 
|  142:  |      | 
|  143:  |  | 
|  144:  |  | 
|  145:  |  | 
|  146:  |     public function getServersManager() | 
|  147:  |     { | 
|  148:  |         if ($this->oServersManager === null) { | 
|  149:  |             $this->oServersManager = new Managers\Servers\Manager($this); | 
|  150:  |         } | 
|  151:  |  | 
|  152:  |         return $this->oServersManager; | 
|  153:  |     } | 
|  154:  |  | 
|  155:  |      | 
|  156:  |  | 
|  157:  |  | 
|  158:  |  | 
|  159:  |     public function getIdentitiesManager() | 
|  160:  |     { | 
|  161:  |         if ($this->oIdentitiesManager === null) { | 
|  162:  |             $this->oIdentitiesManager = new Managers\Identities\Manager($this); | 
|  163:  |         } | 
|  164:  |  | 
|  165:  |         return $this->oIdentitiesManager; | 
|  166:  |     } | 
|  167:  |  | 
|  168:  |      | 
|  169:  |  | 
|  170:  |  | 
|  171:  |  | 
|  172:  |     public function getMailManager() | 
|  173:  |     { | 
|  174:  |         if ($this->oMailManager === null) { | 
|  175:  |             $this->oMailManager = new Managers\Main\Manager($this); | 
|  176:  |         } | 
|  177:  |  | 
|  178:  |         return $this->oMailManager; | 
|  179:  |     } | 
|  180:  |  | 
|  181:  |      | 
|  182:  |  | 
|  183:  |  | 
|  184:  |  | 
|  185:  |     public function getSieveManager() | 
|  186:  |     { | 
|  187:  |         if ($this->oSieveManager === null) { | 
|  188:  |             $this->oSieveManager = new Managers\Sieve\Manager($this); | 
|  189:  |         } | 
|  190:  |  | 
|  191:  |         return $this->oSieveManager; | 
|  192:  |     } | 
|  193:  |  | 
|  194:  |      | 
|  195:  |  | 
|  196:  |  | 
|  197:  |  | 
|  198:  |     public function getFilecacheManager() | 
|  199:  |     { | 
|  200:  |         if ($this->oFilecacheManager === null) { | 
|  201:  |             $this->oFilecacheManager = new \Aurora\System\Managers\Filecache(); | 
|  202:  |         } | 
|  203:  |  | 
|  204:  |         return $this->oFilecacheManager; | 
|  205:  |     } | 
|  206:  |  | 
|  207:  |      | 
|  208:  |  | 
|  209:  |  | 
|  210:  |  | 
|  211:  |  | 
|  212:  |  | 
|  213:  |     public static function checkAccess($oAccount = null, $iUserId = null) | 
|  214:  |     { | 
|  215:  |         if (\Aurora\System\Api::accessCheckIsSkipped()) { | 
|  216:  |              | 
|  217:  |             return; | 
|  218:  |         } | 
|  219:  |  | 
|  220:  |         $bAccessDenied = true; | 
|  221:  |         $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); | 
|  222:  |         $iUserRole = $oAuthenticatedUser instanceof \Aurora\Modules\Core\Models\User ? $oAuthenticatedUser->Role : \Aurora\System\Enums\UserRole::Anonymous; | 
|  223:  |         switch ($iUserRole) { | 
|  224:  |             case (\Aurora\System\Enums\UserRole::SuperAdmin): | 
|  225:  |                  | 
|  226:  |                 $bAccessDenied = false; | 
|  227:  |                 break; | 
|  228:  |             case (\Aurora\System\Enums\UserRole::TenantAdmin): | 
|  229:  |                  | 
|  230:  |                 $oUser = null; | 
|  231:  |                 if ($oAccount !== null) { | 
|  232:  |                     $oUser = \Aurora\Modules\Core\Module::getInstance()->GetUser($oAccount->IdUser); | 
|  233:  |                 } | 
|  234:  |                 if ($iUserId !== null) { | 
|  235:  |                     $oUser = \Aurora\Modules\Core\Module::getInstance()->GetUser($iUserId); | 
|  236:  |                 } | 
|  237:  |                 if ($oUser instanceof \Aurora\Modules\Core\Models\User) { | 
|  238:  |                     if ($oAuthenticatedUser->IdTenant === $oUser->IdTenant) { | 
|  239:  |                         $bAccessDenied = false; | 
|  240:  |                     } | 
|  241:  |                 } | 
|  242:  |                 break; | 
|  243:  |             case (\Aurora\System\Enums\UserRole::NormalUser): | 
|  244:  |                  | 
|  245:  |                 if ($iUserId !== null) { | 
|  246:  |                     if ($iUserId === $oAuthenticatedUser->Id) { | 
|  247:  |                         $bAccessDenied = false; | 
|  248:  |                     } | 
|  249:  |                 } | 
|  250:  |  | 
|  251:  |                  | 
|  252:  |                 if ($oAccount !== null) { | 
|  253:  |                     if ($oAccount instanceof Models\MailAccount && $oAccount->IdUser === $oAuthenticatedUser->Id) { | 
|  254:  |                         $bAccessDenied = false; | 
|  255:  |                     } | 
|  256:  |                 } | 
|  257:  |                 break; | 
|  258:  |             case (\Aurora\System\Enums\UserRole::Customer): | 
|  259:  |             case (\Aurora\System\Enums\UserRole::Anonymous): | 
|  260:  |                  | 
|  261:  |                 break; | 
|  262:  |         } | 
|  263:  |         if ($bAccessDenied) { | 
|  264:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied); | 
|  265:  |         } | 
|  266:  |     } | 
|  267:  |  | 
|  268:  |      | 
|  269:  |      | 
|  270:  |  | 
|  271:  |  | 
|  272:  |  | 
|  273:  |  | 
|  274:  |      | 
|  275:  |  | 
|  276:  |  | 
|  277:  |  | 
|  278:  |  | 
|  279:  |  | 
|  280:  |  | 
|  281:  |  | 
|  282:  |  | 
|  283:  |  | 
|  284:  |  | 
|  285:  |  | 
|  286:  |  | 
|  287:  |  | 
|  288:  |  | 
|  289:  |  | 
|  290:  |  | 
|  291:  |  | 
|  292:  |  | 
|  293:  |  | 
|  294:  |  | 
|  295:  |  | 
|  296:  |  | 
|  297:  |  | 
|  298:  |  | 
|  299:  |  | 
|  300:  |  | 
|  301:  |  | 
|  302:  |  | 
|  303:  |  | 
|  304:  |  | 
|  305:  |  | 
|  306:  |  | 
|  307:  |  | 
|  308:  |  | 
|  309:  |  | 
|  310:  |  | 
|  311:  |  | 
|  312:  |  | 
|  313:  |  | 
|  314:  |  | 
|  315:  |  | 
|  316:  |  | 
|  317:  |  | 
|  318:  |  | 
|  319:  |  | 
|  320:  |  | 
|  321:  |  | 
|  322:  |  | 
|  323:  |  | 
|  324:  |  | 
|  325:  |  | 
|  326:  |  | 
|  327:  |  | 
|  328:  |      | 
|  329:  |  | 
|  330:  |  | 
|  331:  |  | 
|  332:  |     public function GetSettings() | 
|  333:  |     { | 
|  334:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous); | 
|  335:  |  | 
|  336:  |         $aSettings = array( | 
|  337:  |             'Accounts' => array(), | 
|  338:  |             'AllowAddAccounts' => $this->getConfig('AllowAddAccounts', false), | 
|  339:  |             'AllowAutosaveInDrafts' => (bool)$this->getConfig('AllowAutosaveInDrafts', false), | 
|  340:  |             'AllowChangeMailQuotaOnMailServer' => $this->getConfig('AllowChangeMailQuotaOnMailServer', false), | 
|  341:  |             'AllowDefaultAccountForUser' => $this->getConfig('AllowDefaultAccountForUser', false), | 
|  342:  |             'AllowIdentities' => $this->getConfig('AllowIdentities', false), | 
|  343:  |             'OnlyUserEmailsInIdentities' => $this->getConfig('OnlyUserEmailsInIdentities', false), | 
|  344:  |             'AllowInsertImage' => $this->getConfig('AllowInsertImage', false), | 
|  345:  |             'AutoSaveIntervalSeconds' => $this->getConfig('AutoSaveIntervalSeconds', 60), | 
|  346:  |             'AllowTemplateFolders' => $this->getConfig('AllowTemplateFolders', false), | 
|  347:  |             'AllowInsertTemplateOnCompose' => $this->getConfig('AllowInsertTemplateOnCompose', false), | 
|  348:  |             'MaxTemplatesCountOnCompose' => $this->getConfig('MaxTemplatesCountOnCompose', 100), | 
|  349:  |             'AllowAlwaysRefreshFolders' => $this->getConfig('AllowAlwaysRefreshFolders', false), | 
|  350:  |             'AutocreateMailAccountOnNewUserFirstLogin' => $this->getConfig('AutocreateMailAccountOnNewUserFirstLogin', false), | 
|  351:  |             'IgnoreImapSubscription' => $this->getConfig('IgnoreImapSubscription', false), | 
|  352:  |             'ImageUploadSizeLimit' => $this->getConfig('ImageUploadSizeLimit', 0), | 
|  353:  |             'AllowUnifiedInbox' => $this->getConfig('AllowUnifiedInbox', false), | 
|  354:  |             'SmtpAuthType' => (new \Aurora\Modules\Mail\Enums\SmtpAuthType())->getMap(), | 
|  355:  |             'MessagesSortBy' => $this->getConfig('MessagesSortBy', []) | 
|  356:  |         ); | 
|  357:  |  | 
|  358:  |         $oUser = \Aurora\System\Api::getAuthenticatedUser(); | 
|  359:  |         if ($oUser && $oUser->isNormalOrTenant()) { | 
|  360:  |             $aAcc = $this->GetAccounts($oUser->Id); | 
|  361:  |             $aResponseAcc = []; | 
|  362:  |             foreach ($aAcc as $oAccount) { | 
|  363:  |                 $aResponseAcc[] = $oAccount->toResponseArray(); | 
|  364:  |             } | 
|  365:  |             $aSettings['Accounts'] = $aResponseAcc; | 
|  366:  |  | 
|  367:  |             if (isset($oUser->{self::GetName().'::AllowAutosaveInDrafts'})) { | 
|  368:  |                 $aSettings['AllowAutosaveInDrafts'] = $oUser->{self::GetName().'::AllowAutosaveInDrafts'}; | 
|  369:  |             } | 
|  370:  |         } | 
|  371:  |  | 
|  372:  |         return $aSettings; | 
|  373:  |     } | 
|  374:  |  | 
|  375:  |      | 
|  376:  |  | 
|  377:  |  | 
|  378:  |  | 
|  379:  |  | 
|  380:  |  | 
|  381:  |  | 
|  382:  |  | 
|  383:  |  | 
|  384:  |  | 
|  385:  |  | 
|  386:  |  | 
|  387:  |  | 
|  388:  |  | 
|  389:  |  | 
|  390:  |  | 
|  391:  |  | 
|  392:  |  | 
|  393:  |  | 
|  394:  |  | 
|  395:  |  | 
|  396:  |  | 
|  397:  |  | 
|  398:  |  | 
|  399:  |  | 
|  400:  |  | 
|  401:  |  | 
|  402:  |  | 
|  403:  |  | 
|  404:  |  | 
|  405:  |  | 
|  406:  |  | 
|  407:  |  | 
|  408:  |  | 
|  409:  |  | 
|  410:  |  | 
|  411:  |  | 
|  412:  |  | 
|  413:  |  | 
|  414:  |  | 
|  415:  |  | 
|  416:  |  | 
|  417:  |  | 
|  418:  |  | 
|  419:  |  | 
|  420:  |  | 
|  421:  |  | 
|  422:  |  | 
|  423:  |  | 
|  424:  |      | 
|  425:  |  | 
|  426:  |  | 
|  427:  |  | 
|  428:  |  | 
|  429:  |  | 
|  430:  |  | 
|  431:  |     public function UpdateSettings($AutocreateMailAccountOnNewUserFirstLogin = null, $AllowAddAccounts = null, $AllowAutosaveInDrafts = null) | 
|  432:  |     { | 
|  433:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
|  434:  |  | 
|  435:  |         $oUser = \Aurora\System\Api::getAuthenticatedUser(); | 
|  436:  |         if ($oUser) { | 
|  437:  |             if ($oUser->isNormalOrTenant()) { | 
|  438:  |                 if ($AllowAutosaveInDrafts !== null) { | 
|  439:  |                     $oUser->setExtendedProp(self::GetName().'::AllowAutosaveInDrafts', $AllowAutosaveInDrafts); | 
|  440:  |                 } | 
|  441:  |                 $oCoreDecorator = \Aurora\Modules\Core\Module::Decorator(); | 
|  442:  |                 return $oCoreDecorator->UpdateUserObject($oUser); | 
|  443:  |             } | 
|  444:  |             if ($oUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin) { | 
|  445:  |                 if ($AutocreateMailAccountOnNewUserFirstLogin !== null) { | 
|  446:  |                     $this->setConfig('AutocreateMailAccountOnNewUserFirstLogin', $AutocreateMailAccountOnNewUserFirstLogin); | 
|  447:  |                 } | 
|  448:  |                 if ($AllowAddAccounts !== null) { | 
|  449:  |                     $this->setConfig('AllowAddAccounts', $AllowAddAccounts); | 
|  450:  |                 } | 
|  451:  |                 return $this->saveModuleConfig(); | 
|  452:  |             } | 
|  453:  |         } | 
|  454:  |  | 
|  455:  |         return false; | 
|  456:  |     } | 
|  457:  |  | 
|  458:  |     public function GetEntitySpaceLimits($Type, $UserId = null, $TenantId = null) | 
|  459:  |     { | 
|  460:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin); | 
|  461:  |         $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); | 
|  462:  |  | 
|  463:  |         if ($Type === 'User' && is_int($UserId) && $UserId > 0) { | 
|  464:  |             $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserUnchecked($UserId); | 
|  465:  |             if ($oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin || | 
|  466:  |                     $oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::TenantAdmin && $oAuthenticatedUser->IdTenant === $oUser->IdTenant) { | 
|  467:  |                 $oTenant = \Aurora\System\Api::getTenantById($oUser->IdTenant); | 
|  468:  |                 return [ | 
|  469:  |                     'UserSpaceLimitMb' => $oUser->{self::GetName() . '::UserSpaceLimitMb'}, | 
|  470:  |                     'AllowChangeUserSpaceLimit' => $oTenant->{self::GetName() . '::AllowChangeUserSpaceLimit'}, | 
|  471:  |                 ]; | 
|  472:  |             } | 
|  473:  |         } | 
|  474:  |  | 
|  475:  |         if ($Type === 'Tenant' && is_int($TenantId) && $TenantId > 0) { | 
|  476:  |             $oTenant = \Aurora\System\Api::getTenantById($TenantId); | 
|  477:  |             if ($oTenant instanceof \Aurora\Modules\Core\Models\Tenant && $oAuthenticatedUser instanceof \Aurora\Modules\Core\Models\User && | 
|  478:  |                     ($oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin || | 
|  479:  |                     $oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::TenantAdmin && $oAuthenticatedUser->IdTenant === $TenantId)) { | 
|  480:  |                 return [ | 
|  481:  |                     'TenantSpaceLimitMb' => $oTenant->{self::GetName() . '::TenantSpaceLimitMb'}, | 
|  482:  |                     'UserSpaceLimitMb' => $oTenant->{self::GetName() . '::UserSpaceLimitMb'}, | 
|  483:  |                     'AllowChangeUserSpaceLimit' => $oTenant->{self::GetName() . '::AllowChangeUserSpaceLimit'}, | 
|  484:  |                     'AllocatedSpaceMb' => $oTenant->{self::GetName() . '::AllocatedSpaceMb'}, | 
|  485:  |                 ]; | 
|  486:  |             } | 
|  487:  |         } | 
|  488:  |  | 
|  489:  |         return []; | 
|  490:  |     } | 
|  491:  |  | 
|  492:  |     protected function updateAllocatedTenantSpace($iTenantId, $iNewUserQuota, $iPrevUserQuota) | 
|  493:  |     { | 
|  494:  |         $oTenant = \Aurora\System\Api::getTenantById($iTenantId); | 
|  495:  |         $iAllocatedSpaceMb = $oTenant->{self::GetName() . '::AllocatedSpaceMb'}; | 
|  496:  |         $iAllocatedSpaceMb += $iNewUserQuota - $iPrevUserQuota; | 
|  497:  |         if ($iAllocatedSpaceMb < 0) { | 
|  498:  |             $iAllocatedSpaceMb = 0; | 
|  499:  |         } | 
|  500:  |         $oTenant->setExtendedProp(self::GetName() . '::AllocatedSpaceMb', $iAllocatedSpaceMb); | 
|  501:  |         \Aurora\Modules\Core\Module::Decorator()->getTenantsManager()->updateTenant($oTenant); | 
|  502:  |     } | 
|  503:  |  | 
|  504:  |     public function UpdateEntitySpaceLimits($Type, $UserId, $TenantId, $TenantSpaceLimitMb, $UserSpaceLimitMb = null) | 
|  505:  |     { | 
|  506:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin); | 
|  507:  |         $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); | 
|  508:  |  | 
|  509:  |         if ($oAuthenticatedUser instanceof \Aurora\Modules\Core\Models\User && $Type === 'User' && is_int($UserId) && $UserId > 0) { | 
|  510:  |             $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserUnchecked($UserId); | 
|  511:  |             if ($oUser instanceof \Aurora\Modules\Core\Models\User | 
|  512:  |                     && ($oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin | 
|  513:  |                     || $oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::TenantAdmin && $oAuthenticatedUser->IdTenant === $oUser->IdTenant)) { | 
|  514:  |                 $aPrevUserQuota = self::Decorator()->GetEntitySpaceLimits('User', $UserId, $oUser->IdTenant); | 
|  515:  |                 $aTenantQuota = self::Decorator()->GetEntitySpaceLimits('Tenant', $UserId, $oUser->IdTenant); | 
|  516:  |                 $mResult = false; | 
|  517:  |                 if (is_array($aTenantQuota) && isset($aTenantQuota['AllocatedSpaceMb']) && is_array($aPrevUserQuota) &&  isset($aPrevUserQuota['UserSpaceLimitMb'])) { | 
|  518:  |                     $iNewAllocatedSpaceMb = $aTenantQuota['AllocatedSpaceMb'] - $aPrevUserQuota['UserSpaceLimitMb'] + $UserSpaceLimitMb; | 
|  519:  |                     if ($aTenantQuota['TenantSpaceLimitMb'] > 0 && $aTenantQuota['TenantSpaceLimitMb'] < $iNewAllocatedSpaceMb) { | 
|  520:  |                         throw new \Aurora\Modules\Mail\Exceptions\Exception(Enums\ErrorCodes::TenantQuotaExceeded); | 
|  521:  |                     } | 
|  522:  |                     $aArgs = [ | 
|  523:  |                         'TenantId' => $TenantId, | 
|  524:  |                         'Email' => $oUser->PublicId, | 
|  525:  |                         'QuotaMb' => $UserSpaceLimitMb | 
|  526:  |                     ]; | 
|  527:  |                     $this->broadcastEvent( | 
|  528:  |                         'UpdateQuota', | 
|  529:  |                         $aArgs, | 
|  530:  |                         $mResult | 
|  531:  |                     ); | 
|  532:  |                 } | 
|  533:  |                 if ($mResult !== false) { | 
|  534:  |                     $this->updateAllocatedTenantSpace($TenantId, $UserSpaceLimitMb, $aPrevUserQuota['UserSpaceLimitMb']); | 
|  535:  |                     $oUser->setExtendedProp(self::GetName() . '::UserSpaceLimitMb', $UserSpaceLimitMb); | 
|  536:  |                     $oUser->save(); | 
|  537:  |                 } | 
|  538:  |                 return $mResult; | 
|  539:  |             } | 
|  540:  |         } | 
|  541:  |  | 
|  542:  |         if ($Type === 'Tenant' && is_int($TenantId) && $TenantId > 0) { | 
|  543:  |             $oTenant = \Aurora\Modules\Core\Module::Decorator()->GetTenantUnchecked($TenantId); | 
|  544:  |             if ($oTenant && ($oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin || | 
|  545:  |                     $oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::TenantAdmin && $oAuthenticatedUser->IdTenant === $TenantId)) { | 
|  546:  |                 $oTenant->setExtendedProp(self::GetName() . '::TenantSpaceLimitMb', $TenantSpaceLimitMb); | 
|  547:  |                 if (is_int($UserSpaceLimitMb)) { | 
|  548:  |                     $oTenant->setExtendedProp(self::GetName() . '::UserSpaceLimitMb', $UserSpaceLimitMb); | 
|  549:  |                 } | 
|  550:  |                 return $oTenant->save(); | 
|  551:  |             } | 
|  552:  |         } | 
|  553:  |  | 
|  554:  |         return false; | 
|  555:  |     } | 
|  556:  |  | 
|  557:  |      | 
|  558:  |  | 
|  559:  |  | 
|  560:  |  | 
|  561:  |  | 
|  562:  |  | 
|  563:  |  | 
|  564:  |  | 
|  565:  |  | 
|  566:  |  | 
|  567:  |  | 
|  568:  |  | 
|  569:  |  | 
|  570:  |  | 
|  571:  |  | 
|  572:  |  | 
|  573:  |  | 
|  574:  |  | 
|  575:  |  | 
|  576:  |  | 
|  577:  |  | 
|  578:  |  | 
|  579:  |  | 
|  580:  |  | 
|  581:  |  | 
|  582:  |  | 
|  583:  |  | 
|  584:  |  | 
|  585:  |  | 
|  586:  |  | 
|  587:  |  | 
|  588:  |  | 
|  589:  |  | 
|  590:  |  | 
|  591:  |  | 
|  592:  |  | 
|  593:  |  | 
|  594:  |  | 
|  595:  |  | 
|  596:  |  | 
|  597:  |  | 
|  598:  |  | 
|  599:  |  | 
|  600:  |  | 
|  601:  |  | 
|  602:  |  | 
|  603:  |  | 
|  604:  |  | 
|  605:  |  | 
|  606:  |  | 
|  607:  |  | 
|  608:  |  | 
|  609:  |      | 
|  610:  |  | 
|  611:  |  | 
|  612:  |  | 
|  613:  |  | 
|  614:  |     public function GetAccounts($UserId) | 
|  615:  |     { | 
|  616:  |         $mResult = false; | 
|  617:  |  | 
|  618:  |         $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); | 
|  619:  |         if ($oAuthenticatedUser && $oAuthenticatedUser->Id === $UserId) { | 
|  620:  |              | 
|  621:  |             \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
|  622:  |         } else { | 
|  623:  |              | 
|  624:  |              | 
|  625:  |              | 
|  626:  |             \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); | 
|  627:  |         } | 
|  628:  |  | 
|  629:  |         $aAccounts = $this->getAccountsManager()->getUserAccounts($UserId); | 
|  630:  |  | 
|  631:  |         $mResult = []; | 
|  632:  |         foreach ($aAccounts as $oAccount) { | 
|  633:  |             if ($oAuthenticatedUser && $oAccount->IncomingLogin === $oAuthenticatedUser->PublicId) { | 
|  634:  |                 array_unshift($mResult, $oAccount); | 
|  635:  |             } else { | 
|  636:  |                 $mResult[] = $oAccount; | 
|  637:  |             } | 
|  638:  |         } | 
|  639:  |  | 
|  640:  |  | 
|  641:  |         return $mResult; | 
|  642:  |     } | 
|  643:  |  | 
|  644:  |      | 
|  645:  |  | 
|  646:  |  | 
|  647:  |  | 
|  648:  |  | 
|  649:  |  | 
|  650:  |  | 
|  651:  |  | 
|  652:  |  | 
|  653:  |  | 
|  654:  |  | 
|  655:  |  | 
|  656:  |  | 
|  657:  |  | 
|  658:  |  | 
|  659:  |  | 
|  660:  |  | 
|  661:  |  | 
|  662:  |  | 
|  663:  |  | 
|  664:  |  | 
|  665:  |  | 
|  666:  |  | 
|  667:  |  | 
|  668:  |  | 
|  669:  |  | 
|  670:  |  | 
|  671:  |  | 
|  672:  |  | 
|  673:  |  | 
|  674:  |  | 
|  675:  |  | 
|  676:  |  | 
|  677:  |  | 
|  678:  |  | 
|  679:  |  | 
|  680:  |  | 
|  681:  |  | 
|  682:  |  | 
|  683:  |  | 
|  684:  |  | 
|  685:  |  | 
|  686:  |  | 
|  687:  |  | 
|  688:  |  | 
|  689:  |  | 
|  690:  |  | 
|  691:  |  | 
|  692:  |  | 
|  693:  |  | 
|  694:  |  | 
|  695:  |  | 
|  696:  |  | 
|  697:  |  | 
|  698:  |  | 
|  699:  |  | 
|  700:  |  | 
|  701:  |  | 
|  702:  |  | 
|  703:  |  | 
|  704:  |  | 
|  705:  |  | 
|  706:  |  | 
|  707:  |  | 
|  708:  |      | 
|  709:  |  | 
|  710:  |  | 
|  711:  |  | 
|  712:  |  | 
|  713:  |     public function GetAccount($AccountId) | 
|  714:  |     { | 
|  715:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
|  716:  |  | 
|  717:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountId); | 
|  718:  |  | 
|  719:  |         self::checkAccess($oAccount); | 
|  720:  |  | 
|  721:  |         return $oAccount; | 
|  722:  |     } | 
|  723:  |  | 
|  724:  |     public function GetAccountByEmail($Email, $UserId = 0) | 
|  725:  |     { | 
|  726:  |         $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); | 
|  727:  |         $oUser = $UserId !== 0 ? \Aurora\Modules\Core\Module::Decorator()->GetUserUnchecked($UserId) : null; | 
|  728:  |  | 
|  729:  |          | 
|  730:  |         if ($oAuthenticatedUser instanceof \Aurora\Modules\Core\Models\User && $oUser instanceof \Aurora\Modules\Core\Models\User) { | 
|  731:  |             if ($oUser->Id === $oAuthenticatedUser->Id) { | 
|  732:  |                 \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
|  733:  |             } elseif ($oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::TenantAdmin && $oUser->IdTenant === $oAuthenticatedUser->IdTenant) { | 
|  734:  |                 \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin); | 
|  735:  |             } | 
|  736:  |         } else { | 
|  737:  |             \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); | 
|  738:  |         } | 
|  739:  |  | 
|  740:  |         $mResult = false; | 
|  741:  |         $oAccount = $this->getAccountsManager()->getAccountByEmail($Email, $UserId); | 
|  742:  |         if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount && $UserId === $oAccount->IdUser) { | 
|  743:  |             $mResult = $oAccount; | 
|  744:  |         } | 
|  745:  |  | 
|  746:  |         return $mResult; | 
|  747:  |     } | 
|  748:  |  | 
|  749:  |      | 
|  750:  |  | 
|  751:  |  | 
|  752:  |  | 
|  753:  |  | 
|  754:  |  | 
|  755:  |  | 
|  756:  |  | 
|  757:  |  | 
|  758:  |  | 
|  759:  |  | 
|  760:  |  | 
|  761:  |  | 
|  762:  |  | 
|  763:  |  | 
|  764:  |  | 
|  765:  |  | 
|  766:  |  | 
|  767:  |  | 
|  768:  |  | 
|  769:  |  | 
|  770:  |  | 
|  771:  |  | 
|  772:  |  | 
|  773:  |  | 
|  774:  |  | 
|  775:  |  | 
|  776:  |  | 
|  777:  |  | 
|  778:  |  | 
|  779:  |  | 
|  780:  |  | 
|  781:  |  | 
|  782:  |  | 
|  783:  |  | 
|  784:  |  | 
|  785:  |  | 
|  786:  |  | 
|  787:  |  | 
|  788:  |  | 
|  789:  |  | 
|  790:  |  | 
|  791:  |  | 
|  792:  |  | 
|  793:  |  | 
|  794:  |  | 
|  795:  |  | 
|  796:  |  | 
|  797:  |  | 
|  798:  |  | 
|  799:  |  | 
|  800:  |  | 
|  801:  |  | 
|  802:  |  | 
|  803:  |  | 
|  804:  |  | 
|  805:  |  | 
|  806:  |  | 
|  807:  |  | 
|  808:  |  | 
|  809:  |  | 
|  810:  |  | 
|  811:  |  | 
|  812:  |  | 
|  813:  |  | 
|  814:  |  | 
|  815:  |  | 
|  816:  |  | 
|  817:  |      | 
|  818:  |  | 
|  819:  |  | 
|  820:  |  | 
|  821:  |  | 
|  822:  |  | 
|  823:  |  | 
|  824:  |  | 
|  825:  |  | 
|  826:  |  | 
|  827:  |     public function CreateAccount( | 
|  828:  |         $UserId = 0, | 
|  829:  |         $FriendlyName = '', | 
|  830:  |         $Email = '', | 
|  831:  |         $IncomingLogin = '', | 
|  832:  |         $IncomingPassword = '', | 
|  833:  |         $Server = null, | 
|  834:  |         $XAuth = null | 
|  835:  |     ) { | 
|  836:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
|  837:  |  | 
|  838:  |         self::checkAccess(null, $UserId); | 
|  839:  |  | 
|  840:  |         $oAccount = $this->GetAccountByEmail($Email, $UserId); | 
|  841:  |         $oServer = false; | 
|  842:  |  | 
|  843:  |         if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) { | 
|  844:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccountExists); | 
|  845:  |         } else { | 
|  846:  |             if ($Email) { | 
|  847:  |                 $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($Email); | 
|  848:  |                 $bCustomServerCreated = false; | 
|  849:  |                 $iServerId = $Server['ServerId']; | 
|  850:  |                 if ($Server !== null && $iServerId === 0) { | 
|  851:  |                     $oNewServer = new \Aurora\Modules\Mail\Models\Server(); | 
|  852:  |                     $oNewServer->Name = $Server['IncomingServer']; | 
|  853:  |                     $oNewServer->IncomingServer = $Server['IncomingServer']; | 
|  854:  |                     $oNewServer->IncomingPort = $Server['IncomingPort']; | 
|  855:  |                     $oNewServer->IncomingUseSsl = $Server['IncomingUseSsl']; | 
|  856:  |                     $oNewServer->OutgoingServer = $Server['OutgoingServer']; | 
|  857:  |                     $oNewServer->OutgoingPort = $Server['OutgoingPort']; | 
|  858:  |                     $oNewServer->OutgoingUseSsl = $Server['OutgoingUseSsl']; | 
|  859:  |                     $oNewServer->SmtpAuthType = $Server['SmtpAuthType']; | 
|  860:  |                     $oNewServer->Domains = $sDomain; | 
|  861:  |                     $oNewServer->EnableThreading = $Server['EnableThreading']; | 
|  862:  |                     $iServerId = $this->getServersManager()->createServer($oNewServer); | 
|  863:  |                     $bCustomServerCreated = true; | 
|  864:  |                 } | 
|  865:  |  | 
|  866:  |                 if ($Server === null) { | 
|  867:  |                     $aServerResult = self::Decorator()->GetMailServerByDomain($sDomain, true); | 
|  868:  |                     if (isset($aServerResult['Server'])) { | 
|  869:  |                         $oServer = $aServerResult['Server']; | 
|  870:  |                         $iServerId = $oServer->Id; | 
|  871:  |                     } | 
|  872:  |                 } | 
|  873:  |  | 
|  874:  |                 $oAccount = new \Aurora\Modules\Mail\Models\MailAccount(); | 
|  875:  |  | 
|  876:  |                 $oAccount->IdUser = $UserId; | 
|  877:  |                 $oAccount->FriendlyName = $FriendlyName; | 
|  878:  |                 $oAccount->Email = $Email; | 
|  879:  |                 $oAccount->IncomingLogin = $IncomingLogin; | 
|  880:  |                 $oAccount->setPassword($IncomingPassword); | 
|  881:  |                 if ($iServerId > 0) { | 
|  882:  |                     $oServer = $this->getServersManager()->getServer($iServerId); | 
|  883:  |                 } | 
|  884:  |  | 
|  885:  |                 if ($oServer) { | 
|  886:  |                     $oAccount->Server()->associate($oServer); | 
|  887:  |                     $oAccount->UseThreading = $oServer->EnableThreading; | 
|  888:  |                 } | 
|  889:  |  | 
|  890:  |                 $oAccount->XOAuth = $XAuth; | 
|  891:  |  | 
|  892:  |                 $bAccoutResult = false; | 
|  893:  |                 $oResException = null; | 
|  894:  |                 $bDoImapLoginOnAccountCreate = $this->getConfig('DoImapLoginOnAccountCreate', true) && is_null($XAuth); | 
|  895:  |                 if ($bDoImapLoginOnAccountCreate) { | 
|  896:  |                     $oResException = $this->getMailManager()->validateAccountConnection($oAccount, false); | 
|  897:  |                 } | 
|  898:  |  | 
|  899:  |                 $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserUnchecked($UserId); | 
|  900:  |                 $aQuota = []; | 
|  901:  |                 $iQuota = 0; | 
|  902:  |  | 
|  903:  |                 if ($oResException === null && $oUser instanceof \Aurora\Modules\Core\Models\User && $oUser->PublicId === $oAccount->Email) { | 
|  904:  |                     $aTenantQuota = self::Decorator()->GetEntitySpaceLimits('Tenant', $UserId, $oUser->IdTenant); | 
|  905:  |                     if (is_array($aTenantQuota) && isset($aTenantQuota['AllocatedSpaceMb']) && isset($aTenantQuota['TenantSpaceLimitMb'])) { | 
|  906:  |                         if ($bDoImapLoginOnAccountCreate) { | 
|  907:  |                             $aQuota = $this->getMailManager()->getQuota($oAccount); | 
|  908:  |                         } else { | 
|  909:  |                             $aQuota[1] = 0; | 
|  910:  |                         } | 
|  911:  |                         $iQuota = (is_array($aQuota) && isset($aQuota[1])) ? $aQuota[1] / 1024 : 0; | 
|  912:  |                         $iNewAllocatedSpaceMb = $aTenantQuota['AllocatedSpaceMb'] + $iQuota; | 
|  913:  |                         if ($aTenantQuota['TenantSpaceLimitMb'] > 0 && $aTenantQuota['TenantSpaceLimitMb'] < $iNewAllocatedSpaceMb) { | 
|  914:  |                             $oResException = new \Aurora\Modules\Mail\Exceptions\Exception(Enums\ErrorCodes::TenantQuotaExceeded, null, $this->i18N('ERROR_TENANT_QUOTA_EXCEEDED')); | 
|  915:  |                         } | 
|  916:  |                     } | 
|  917:  |                 } | 
|  918:  |  | 
|  919:  |                 if ($oResException === null) { | 
|  920:  |                     if ($oUser instanceof \Aurora\Modules\Core\Models\User && $oUser->PublicId === $Email && | 
|  921:  |                         !$this->getAccountsManager()->useToAuthorizeAccountExists($Email)) { | 
|  922:  |                         $oAccount->UseToAuthorize = true; | 
|  923:  |                     } | 
|  924:  |                     $bAccoutResult = $this->getAccountsManager()->createAccount($oAccount); | 
|  925:  |                 } | 
|  926:  |  | 
|  927:  |                 if ($bAccoutResult) { | 
|  928:  |                     if ($oAccount->Email === $oUser->PublicId && $bDoImapLoginOnAccountCreate) { | 
|  929:  |                         if (empty($aQuota)) { | 
|  930:  |                             $aQuota = $this->getMailManager()->getQuota($oAccount); | 
|  931:  |                             $iQuota = (is_array($aQuota) && isset($aQuota[1])) ? $aQuota[1] / 1024 : 0; | 
|  932:  |                         } | 
|  933:  |                         $this->updateAllocatedTenantSpace($oUser->IdTenant, $iQuota, 0); | 
|  934:  |                         $oUser->setExtendedProp(self::GetName() . '::UserSpaceLimitMb', $iQuota); | 
|  935:  |                         $oUser->save(); | 
|  936:  |                     } | 
|  937:  |                     if ($oServer && $oServer->EnableSieve && $this->getConfig('EnableAllowBlockLists', false)) { | 
|  938:  |                         try { | 
|  939:  |                             $this->getSieveManager()->setAllowBlockLists($oAccount, [], []); | 
|  940:  |                         } catch (\Exception $oEx) { | 
|  941:  |                             Api::LogException($oEx); | 
|  942:  |                         } | 
|  943:  |                     } | 
|  944:  |                     return $oAccount; | 
|  945:  |                 } elseif ($bCustomServerCreated) { | 
|  946:  |                     $this->getServersManager()->deleteServer($iServerId); | 
|  947:  |                 } | 
|  948:  |  | 
|  949:  |                 if ($oResException !== null) { | 
|  950:  |                     throw $oResException; | 
|  951:  |                 } | 
|  952:  |             } | 
|  953:  |         } | 
|  954:  |         return false; | 
|  955:  |     } | 
|  956:  |  | 
|  957:  |      | 
|  958:  |  | 
|  959:  |  | 
|  960:  |  | 
|  961:  |  | 
|  962:  |  | 
|  963:  |  | 
|  964:  |  | 
|  965:  |  | 
|  966:  |  | 
|  967:  |  | 
|  968:  |  | 
|  969:  |  | 
|  970:  |  | 
|  971:  |  | 
|  972:  |  | 
|  973:  |  | 
|  974:  |  | 
|  975:  |  | 
|  976:  |  | 
|  977:  |  | 
|  978:  |  | 
|  979:  |  | 
|  980:  |  | 
|  981:  |  | 
|  982:  |  | 
|  983:  |  | 
|  984:  |  | 
|  985:  |  | 
|  986:  |  | 
|  987:  |  | 
|  988:  |  | 
|  989:  |  | 
|  990:  |  | 
|  991:  |  | 
|  992:  |  | 
|  993:  |  | 
|  994:  |  | 
|  995:  |  | 
|  996:  |  | 
|  997:  |  | 
|  998:  |  | 
|  999:  |  | 
| 1000:  |  | 
| 1001:  |  | 
| 1002:  |  | 
| 1003:  |  | 
| 1004:  |  | 
| 1005:  |  | 
| 1006:  |  | 
| 1007:  |  | 
| 1008:  |  | 
| 1009:  |  | 
| 1010:  |  | 
| 1011:  |  | 
| 1012:  |  | 
| 1013:  |      | 
| 1014:  |  | 
| 1015:  |  | 
| 1016:  |  | 
| 1017:  |  | 
| 1018:  |  | 
| 1019:  |  | 
| 1020:  |  | 
| 1021:  |  | 
| 1022:  |  | 
| 1023:  |  | 
| 1024:  |  | 
| 1025:  |  | 
| 1026:  |  | 
| 1027:  |     public function UpdateAccount( | 
| 1028:  |         $AccountID, | 
| 1029:  |         $UseToAuthorize = null, | 
| 1030:  |         $Email = null, | 
| 1031:  |         $FriendlyName = null, | 
| 1032:  |         $IncomingLogin = null, | 
| 1033:  |         $IncomingPassword = null, | 
| 1034:  |         $Server = null, | 
| 1035:  |         $UseThreading = null, | 
| 1036:  |         $SaveRepliesToCurrFolder = null | 
| 1037:  |     ) { | 
| 1038:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 1039:  |  | 
| 1040:  |         if ($AccountID > 0) { | 
| 1041:  |             $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 1042:  |  | 
| 1043:  |             self::checkAccess($oAccount); | 
| 1044:  |  | 
| 1045:  |             if ($oAccount) { | 
| 1046:  |                 if (!empty($Email)) { | 
| 1047:  |                     $oAccount->Email = $Email; | 
| 1048:  |                 } | 
| 1049:  |                 if ($UseToAuthorize === false || $UseToAuthorize === true && !$this->getAccountsManager()->useToAuthorizeAccountExists($oAccount->Email, $oAccount->Id)) { | 
| 1050:  |                     $oAccount->UseToAuthorize = $UseToAuthorize; | 
| 1051:  |                 } | 
| 1052:  |                 if ($FriendlyName !== null) { | 
| 1053:  |                     $oAccount->FriendlyName = $FriendlyName; | 
| 1054:  |                 } | 
| 1055:  |                 if (!empty($IncomingLogin)) { | 
| 1056:  |                     $oAccount->IncomingLogin = $IncomingLogin; | 
| 1057:  |                 } | 
| 1058:  |                 if (!empty($IncomingPassword)) { | 
| 1059:  |                     $oAccount->setPassword($IncomingPassword); | 
| 1060:  |                 } | 
| 1061:  |                 if ($Server !== null) { | 
| 1062:  |                     if ($Server['ServerId'] === 0) { | 
| 1063:  |                         $sDomains = explode('@', $oAccount->Email)[1]; | 
| 1064:  |                         $oNewServer = new \Aurora\Modules\Mail\Models\Server(); | 
| 1065:  |                         $oNewServer->Name = $Server['IncomingServer']; | 
| 1066:  |                         $oNewServer->IncomingServer = $Server['IncomingServer']; | 
| 1067:  |                         $oNewServer->IncomingPort = $Server['IncomingPort']; | 
| 1068:  |                         $oNewServer->IncomingUseSsl = $Server['IncomingUseSsl']; | 
| 1069:  |                         $oNewServer->OutgoingServer = $Server['OutgoingServer']; | 
| 1070:  |                         $oNewServer->OutgoingPort = $Server['OutgoingPort']; | 
| 1071:  |                         $oNewServer->OutgoingUseSsl = $Server['OutgoingUseSsl']; | 
| 1072:  |                         $oNewServer->SmtpAuthType = $Server['SmtpAuthType']; | 
| 1073:  |                         $oNewServer->Domains = $sDomains; | 
| 1074:  |                         $oNewServer->EnableThreading = $Server['EnableThreading']; | 
| 1075:  |                         $iNewServerId = $this->getServersManager()->createServer($oNewServer); | 
| 1076:  |                         $oAccount->Server = $iNewServerId; | 
| 1077:  |                     } elseif ($oAccount->ServerId === $Server['ServerId']) { | 
| 1078:  |                         $oAccServer = $oAccount->getServer(); | 
| 1079:  |                         if ($oAccServer && $oAccServer->OwnerType === \Aurora\Modules\Mail\Enums\ServerOwnerType::Account) { | 
| 1080:  |                             $oAccServer->Name = $Server['IncomingServer']; | 
| 1081:  |                             $oAccServer->IncomingServer = $Server['IncomingServer']; | 
| 1082:  |                             $oAccServer->IncomingPort = $Server['IncomingPort']; | 
| 1083:  |                             $oAccServer->IncomingUseSsl = $Server['IncomingUseSsl']; | 
| 1084:  |                             $oAccServer->OutgoingServer = $Server['OutgoingServer']; | 
| 1085:  |                             $oAccServer->OutgoingPort = $Server['OutgoingPort']; | 
| 1086:  |                             $oAccServer->OutgoingUseSsl = $Server['OutgoingUseSsl']; | 
| 1087:  |                             $oAccServer->SmtpAuthType = $Server['SmtpAuthType']; | 
| 1088:  |  | 
| 1089:  |                             $this->getServersManager()->updateServer($oAccServer); | 
| 1090:  |                         } | 
| 1091:  |                     } else { | 
| 1092:  |                         $oAccServer = $oAccount->getServer(); | 
| 1093:  |                         if ($oAccServer && $oAccServer->OwnerType === \Aurora\Modules\Mail\Enums\ServerOwnerType::Account) { | 
| 1094:  |                             $this->getServersManager()->deleteServer($oAccServer->Id); | 
| 1095:  |                         } | 
| 1096:  |                         $oAccount->Server = $Server['ServerId']; | 
| 1097:  |                     } | 
| 1098:  |                 } | 
| 1099:  |  | 
| 1100:  |                 if ($UseThreading !== null) { | 
| 1101:  |                     $oAccount->UseThreading = $UseThreading; | 
| 1102:  |                 } | 
| 1103:  |  | 
| 1104:  |                 if ($SaveRepliesToCurrFolder !== null) { | 
| 1105:  |                     $oAccount->SaveRepliesToCurrFolder = $SaveRepliesToCurrFolder; | 
| 1106:  |                 } | 
| 1107:  |  | 
| 1108:  |                 if ($this->getAccountsManager()->updateAccount($oAccount)) { | 
| 1109:  |                     return $oAccount; | 
| 1110:  |                 } | 
| 1111:  |             } | 
| 1112:  |         } else { | 
| 1113:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 1114:  |         } | 
| 1115:  |  | 
| 1116:  |         return false; | 
| 1117:  |     } | 
| 1118:  |  | 
| 1119:  |     public function UpdateAccountUnifiedMailbox( | 
| 1120:  |         $AccountID, | 
| 1121:  |         $IncludeInUnifiedMailbox, | 
| 1122:  |         $ShowUnifiedMailboxLabel, | 
| 1123:  |         $UnifiedMailboxLabelText, | 
| 1124:  |         $UnifiedMailboxLabelColor | 
| 1125:  |     ) { | 
| 1126:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 1127:  |         if (!$this->getConfig('AllowUnifiedInbox', false)) { | 
| 1128:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied); | 
| 1129:  |         } | 
| 1130:  |         if (!is_int($AccountID) || $AccountID <= 0 || !is_bool($IncludeInUnifiedMailbox) || !is_bool($ShowUnifiedMailboxLabel) || | 
| 1131:  |             !is_string($UnifiedMailboxLabelText) || !is_string($UnifiedMailboxLabelColor)) { | 
| 1132:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 1133:  |         } | 
| 1134:  |  | 
| 1135:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 1136:  |  | 
| 1137:  |         self::checkAccess($oAccount); | 
| 1138:  |  | 
| 1139:  |         if ($oAccount instanceof Models\MailAccount) { | 
| 1140:  |             $oAccount->IncludeInUnifiedMailbox = $IncludeInUnifiedMailbox; | 
| 1141:  |             $oAccount->ShowUnifiedMailboxLabel = $ShowUnifiedMailboxLabel; | 
| 1142:  |             $oAccount->UnifiedMailboxLabelText = $UnifiedMailboxLabelText; | 
| 1143:  |             $oAccount->UnifiedMailboxLabelColor = $UnifiedMailboxLabelColor; | 
| 1144:  |  | 
| 1145:  |             if ($this->getAccountsManager()->updateAccount($oAccount)) { | 
| 1146:  |                 return $oAccount; | 
| 1147:  |             } | 
| 1148:  |         } | 
| 1149:  |  | 
| 1150:  |         return false; | 
| 1151:  |     } | 
| 1152:  |  | 
| 1153:  |      | 
| 1154:  |  | 
| 1155:  |  | 
| 1156:  |  | 
| 1157:  |  | 
| 1158:  |  | 
| 1159:  |  | 
| 1160:  |  | 
| 1161:  |  | 
| 1162:  |  | 
| 1163:  |  | 
| 1164:  |  | 
| 1165:  |  | 
| 1166:  |  | 
| 1167:  |  | 
| 1168:  |  | 
| 1169:  |  | 
| 1170:  |  | 
| 1171:  |  | 
| 1172:  |  | 
| 1173:  |  | 
| 1174:  |  | 
| 1175:  |  | 
| 1176:  |  | 
| 1177:  |  | 
| 1178:  |  | 
| 1179:  |  | 
| 1180:  |  | 
| 1181:  |  | 
| 1182:  |  | 
| 1183:  |  | 
| 1184:  |  | 
| 1185:  |  | 
| 1186:  |  | 
| 1187:  |  | 
| 1188:  |  | 
| 1189:  |  | 
| 1190:  |  | 
| 1191:  |  | 
| 1192:  |  | 
| 1193:  |  | 
| 1194:  |  | 
| 1195:  |  | 
| 1196:  |  | 
| 1197:  |  | 
| 1198:  |  | 
| 1199:  |  | 
| 1200:  |      | 
| 1201:  |  | 
| 1202:  |  | 
| 1203:  |  | 
| 1204:  |  | 
| 1205:  |  | 
| 1206:  |     public function DeleteAccount($AccountID) | 
| 1207:  |     { | 
| 1208:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 1209:  |  | 
| 1210:  |         $bResult = false; | 
| 1211:  |  | 
| 1212:  |         if ($AccountID > 0) { | 
| 1213:  |             $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 1214:  |  | 
| 1215:  |             self::checkAccess($oAccount); | 
| 1216:  |  | 
| 1217:  |             if ($oAccount) { | 
| 1218:  |                 $this->getIdentitiesManager()->deleteAccountIdentities($oAccount->Id); | 
| 1219:  |                 $this->getMailManager()->deleteSystemFolderNames($oAccount->Id); | 
| 1220:  |  | 
| 1221:  |                 $oServer = $oAccount->getServer(); | 
| 1222:  |  | 
| 1223:  |                 $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserUnchecked($oAccount->IdUser); | 
| 1224:  |                 if ($oUser instanceof \Aurora\Modules\Core\Models\User && $oAccount->Email === $oUser->PublicId) { | 
| 1225:  |                     $iQuota = $oUser->{self::GetName() . '::UserSpaceLimitMb'}; | 
| 1226:  |                     $this->updateAllocatedTenantSpace($oUser->IdTenant, 0, $iQuota); | 
| 1227:  |                 } | 
| 1228:  |  | 
| 1229:  |                 $aArgs = [ | 
| 1230:  |                     'Account' => $oAccount, | 
| 1231:  |                     'User' => $oUser | 
| 1232:  |                 ]; | 
| 1233:  |                 $this->broadcastEvent( | 
| 1234:  |                     'BeforeDeleteAccount', | 
| 1235:  |                     $aArgs | 
| 1236:  |                 ); | 
| 1237:  |                 $bResult = $this->getAccountsManager()->deleteAccount($oAccount); | 
| 1238:  |  | 
| 1239:  |                 if ($bResult && $oServer && $oServer->OwnerType === \Aurora\Modules\Mail\Enums\ServerOwnerType::Account) { | 
| 1240:  |                     $this->getServersManager()->deleteServer($oServer->Id); | 
| 1241:  |                 } | 
| 1242:  |             } | 
| 1243:  |  | 
| 1244:  |             return $bResult; | 
| 1245:  |         } else { | 
| 1246:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 1247:  |         } | 
| 1248:  |     } | 
| 1249:  |  | 
| 1250:  |      | 
| 1251:  |  | 
| 1252:  |  | 
| 1253:  |  | 
| 1254:  |  | 
| 1255:  |  | 
| 1256:  |  | 
| 1257:  |  | 
| 1258:  |  | 
| 1259:  |  | 
| 1260:  |  | 
| 1261:  |  | 
| 1262:  |  | 
| 1263:  |  | 
| 1264:  |  | 
| 1265:  |  | 
| 1266:  |  | 
| 1267:  |  | 
| 1268:  |  | 
| 1269:  |  | 
| 1270:  |  | 
| 1271:  |  | 
| 1272:  |  | 
| 1273:  |  | 
| 1274:  |  | 
| 1275:  |  | 
| 1276:  |  | 
| 1277:  |  | 
| 1278:  |  | 
| 1279:  |  | 
| 1280:  |  | 
| 1281:  |  | 
| 1282:  |  | 
| 1283:  |  | 
| 1284:  |  | 
| 1285:  |  | 
| 1286:  |  | 
| 1287:  |  | 
| 1288:  |  | 
| 1289:  |  | 
| 1290:  |  | 
| 1291:  |  | 
| 1292:  |  | 
| 1293:  |  | 
| 1294:  |  | 
| 1295:  |  | 
| 1296:  |  | 
| 1297:  |  | 
| 1298:  |  | 
| 1299:  |  | 
| 1300:  |  | 
| 1301:  |  | 
| 1302:  |      | 
| 1303:  |  | 
| 1304:  |  | 
| 1305:  |  | 
| 1306:  |  | 
| 1307:  |  | 
| 1308:  |  | 
| 1309:  |  | 
| 1310:  |     public function GetServers($TenantId = 0, $Offset = 0, $Limit = 0, $Search = '') | 
| 1311:  |     { | 
| 1312:  |         $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); | 
| 1313:  |         if ($oAuthenticatedUser instanceof \Aurora\Modules\Core\Models\User && $oAuthenticatedUser->isNormalOrTenant()) { | 
| 1314:  |             if ($TenantId === 0) { | 
| 1315:  |                 $TenantId = $oAuthenticatedUser->IdTenant; | 
| 1316:  |             } elseif ($TenantId !== $oAuthenticatedUser->IdTenant) { | 
| 1317:  |                 throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied); | 
| 1318:  |             } | 
| 1319:  |         } else { | 
| 1320:  |             \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); | 
| 1321:  |         } | 
| 1322:  |  | 
| 1323:  |         return [ | 
| 1324:  |             'Items' => $this->getServersManager()->getServerList($TenantId, $Offset, $Limit, $Search)->toArray(), | 
| 1325:  |             'Count' => $this->getServersManager()->getServersCount($TenantId, $Search), | 
| 1326:  |         ]; | 
| 1327:  |     } | 
| 1328:  |  | 
| 1329:  |      | 
| 1330:  |  | 
| 1331:  |  | 
| 1332:  |  | 
| 1333:  |  | 
| 1334:  |  | 
| 1335:  |  | 
| 1336:  |  | 
| 1337:  |  | 
| 1338:  |  | 
| 1339:  |  | 
| 1340:  |  | 
| 1341:  |  | 
| 1342:  |  | 
| 1343:  |  | 
| 1344:  |  | 
| 1345:  |  | 
| 1346:  |  | 
| 1347:  |  | 
| 1348:  |  | 
| 1349:  |  | 
| 1350:  |  | 
| 1351:  |  | 
| 1352:  |  | 
| 1353:  |  | 
| 1354:  |  | 
| 1355:  |  | 
| 1356:  |  | 
| 1357:  |  | 
| 1358:  |  | 
| 1359:  |  | 
| 1360:  |  | 
| 1361:  |  | 
| 1362:  |  | 
| 1363:  |  | 
| 1364:  |  | 
| 1365:  |  | 
| 1366:  |  | 
| 1367:  |  | 
| 1368:  |  | 
| 1369:  |  | 
| 1370:  |  | 
| 1371:  |  | 
| 1372:  |  | 
| 1373:  |  | 
| 1374:  |  | 
| 1375:  |  | 
| 1376:  |  | 
| 1377:  |  | 
| 1378:  |  | 
| 1379:  |  | 
| 1380:  |  | 
| 1381:  |  | 
| 1382:  |  | 
| 1383:  |  | 
| 1384:  |  | 
| 1385:  |  | 
| 1386:  |  | 
| 1387:  |  | 
| 1388:  |  | 
| 1389:  |  | 
| 1390:  |  | 
| 1391:  |  | 
| 1392:  |  | 
| 1393:  |  | 
| 1394:  |      | 
| 1395:  |  | 
| 1396:  |  | 
| 1397:  |  | 
| 1398:  |  | 
| 1399:  |     public function GetServer($ServerId) | 
| 1400:  |     { | 
| 1401:  |         $oServer = $this->getServersManager()->getServer($ServerId); | 
| 1402:  |         $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); | 
| 1403:  |         if ($oAuthenticatedUser instanceof \Aurora\Modules\Core\Models\User && $oAuthenticatedUser->isNormalOrTenant()) { | 
| 1404:  |             if ($oServer->TenantId !== $oAuthenticatedUser->IdTenant) { | 
| 1405:  |                 throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied); | 
| 1406:  |             } | 
| 1407:  |         } else { | 
| 1408:  |             \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); | 
| 1409:  |         } | 
| 1410:  |  | 
| 1411:  |         return $oServer; | 
| 1412:  |     } | 
| 1413:  |  | 
| 1414:  |      | 
| 1415:  |  | 
| 1416:  |  | 
| 1417:  |  | 
| 1418:  |  | 
| 1419:  |  | 
| 1420:  |  | 
| 1421:  |  | 
| 1422:  |  | 
| 1423:  |  | 
| 1424:  |  | 
| 1425:  |  | 
| 1426:  |  | 
| 1427:  |  | 
| 1428:  |  | 
| 1429:  |  | 
| 1430:  |  | 
| 1431:  |  | 
| 1432:  |  | 
| 1433:  |  | 
| 1434:  |  | 
| 1435:  |  | 
| 1436:  |  | 
| 1437:  |  | 
| 1438:  |  | 
| 1439:  |  | 
| 1440:  |  | 
| 1441:  |  | 
| 1442:  |  | 
| 1443:  |  | 
| 1444:  |  | 
| 1445:  |  | 
| 1446:  |  | 
| 1447:  |  | 
| 1448:  |  | 
| 1449:  |  | 
| 1450:  |  | 
| 1451:  |  | 
| 1452:  |  | 
| 1453:  |  | 
| 1454:  |  | 
| 1455:  |  | 
| 1456:  |  | 
| 1457:  |  | 
| 1458:  |  | 
| 1459:  |  | 
| 1460:  |  | 
| 1461:  |  | 
| 1462:  |  | 
| 1463:  |  | 
| 1464:  |  | 
| 1465:  |  | 
| 1466:  |  | 
| 1467:  |  | 
| 1468:  |  | 
| 1469:  |  | 
| 1470:  |  | 
| 1471:  |  | 
| 1472:  |  | 
| 1473:  |  | 
| 1474:  |      | 
| 1475:  |  | 
| 1476:  |  | 
| 1477:  |  | 
| 1478:  |  | 
| 1479:  |  | 
| 1480:  |  | 
| 1481:  |  | 
| 1482:  |  | 
| 1483:  |  | 
| 1484:  |  | 
| 1485:  |  | 
| 1486:  |  | 
| 1487:  |  | 
| 1488:  |  | 
| 1489:  |  | 
| 1490:  |  | 
| 1491:  |  | 
| 1492:  |  | 
| 1493:  |  | 
| 1494:  |  | 
| 1495:  |  | 
| 1496:  |  | 
| 1497:  |  | 
| 1498:  |  | 
| 1499:  |  | 
| 1500:  |  | 
| 1501:  |  | 
| 1502:  |  | 
| 1503:  |  | 
| 1504:  |  | 
| 1505:  |  | 
| 1506:  |  | 
| 1507:  |  | 
| 1508:  |  | 
| 1509:  |  | 
| 1510:  |  | 
| 1511:  |     public function CreateServer( | 
| 1512:  |         $Name, | 
| 1513:  |         $IncomingServer, | 
| 1514:  |         $IncomingPort, | 
| 1515:  |         $IncomingUseSsl, | 
| 1516:  |         $OutgoingServer, | 
| 1517:  |         $OutgoingPort, | 
| 1518:  |         $OutgoingUseSsl, | 
| 1519:  |         $SmtpAuthType, | 
| 1520:  |         $Domains, | 
| 1521:  |         $EnableThreading = true, | 
| 1522:  |         $EnableSieve = false, | 
| 1523:  |         $SievePort = 4190, | 
| 1524:  |         $SmtpLogin = '', | 
| 1525:  |         $SmtpPassword = '', | 
| 1526:  |         $UseFullEmailAddressAsLogin = true, | 
| 1527:  |         $TenantId = 0, | 
| 1528:  |         $SetExternalAccessServers = false, | 
| 1529:  |         $ExternalAccessImapServer = '', | 
| 1530:  |         $ExternalAccessImapPort = 143, | 
| 1531:  |         $ExternalAccessImapAlterPort = 0, | 
| 1532:  |         $ExternalAccessImapUseSsl = false, | 
| 1533:  |         $ExternalAccessPop3Server = '', | 
| 1534:  |         $ExternalAccessPop3Port = 143, | 
| 1535:  |         $ExternalAccessPop3AlterPort = 0, | 
| 1536:  |         $ExternalAccessPop3UseSsl = false, | 
| 1537:  |         $ExternalAccessSmtpServer = '', | 
| 1538:  |         $ExternalAccessSmtpPort = 25, | 
| 1539:  |         $ExternalAccessSmtpAlterPort = 0, | 
| 1540:  |         $ExternalAccessImapSmtpUseSsl = false, | 
| 1541:  |         $OAuthEnable = false, | 
| 1542:  |         $OAuthName = '', | 
| 1543:  |         $OAuthType = '', | 
| 1544:  |         $OAuthIconUrl = '' | 
| 1545:  |     ) { | 
| 1546:  |         $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); | 
| 1547:  |         if ($oAuthenticatedUser instanceof \Aurora\Modules\Core\Models\User && $oAuthenticatedUser->Role === \Aurora\Modules\Mail\Enums\ServerOwnerType::Tenant) { | 
| 1548:  |             if ($TenantId !== $oAuthenticatedUser->IdTenant) { | 
| 1549:  |                 throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied); | 
| 1550:  |             } | 
| 1551:  |         } else { | 
| 1552:  |             \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); | 
| 1553:  |         } | 
| 1554:  |  | 
| 1555:  |         $sOwnerType = ($TenantId === 0) ? \Aurora\Modules\Mail\Enums\ServerOwnerType::SuperAdmin : \Aurora\Modules\Mail\Enums\ServerOwnerType::Tenant; | 
| 1556:  |  | 
| 1557:  |         $oServer = new \Aurora\Modules\Mail\Models\Server(); | 
| 1558:  |         $oServer->OwnerType = $sOwnerType; | 
| 1559:  |         $oServer->TenantId = $TenantId; | 
| 1560:  |         $oServer->Name = $Name; | 
| 1561:  |         $oServer->IncomingServer = $IncomingServer; | 
| 1562:  |         $oServer->IncomingPort = $IncomingPort; | 
| 1563:  |         $oServer->IncomingUseSsl = $IncomingUseSsl; | 
| 1564:  |         $oServer->OutgoingServer = $OutgoingServer; | 
| 1565:  |         $oServer->OutgoingPort = $OutgoingPort; | 
| 1566:  |         $oServer->OutgoingUseSsl = $OutgoingUseSsl; | 
| 1567:  |         $oServer->SmtpAuthType = $SmtpAuthType; | 
| 1568:  |         $oServer->SmtpLogin = $SmtpLogin; | 
| 1569:  |         $oServer->SmtpPassword = $SmtpPassword; | 
| 1570:  |         $oServer->Domains = $this->getServersManager()->trimDomains($Domains); | 
| 1571:  |         $oServer->EnableThreading = $EnableThreading; | 
| 1572:  |         $oServer->EnableSieve = $EnableSieve; | 
| 1573:  |         $oServer->SievePort = $SievePort; | 
| 1574:  |         $oServer->UseFullEmailAddressAsLogin = $UseFullEmailAddressAsLogin; | 
| 1575:  |         $oServer->SetExternalAccessServers = $SetExternalAccessServers; | 
| 1576:  |         if ($oServer->SetExternalAccessServers) { | 
| 1577:  |             $oServer->ExternalAccessImapServer = $ExternalAccessImapServer; | 
| 1578:  |             $oServer->ExternalAccessImapPort = $ExternalAccessImapPort; | 
| 1579:  |             $oServer->ExternalAccessImapAlterPort = $ExternalAccessImapAlterPort; | 
| 1580:  |             $oServer->ExternalAccessImapUseSsl = $ExternalAccessImapUseSsl; | 
| 1581:  |  | 
| 1582:  |             $oServer->ExternalAccessPop3Server = $ExternalAccessPop3Server; | 
| 1583:  |             $oServer->ExternalAccessPop3Port = $ExternalAccessPop3Port; | 
| 1584:  |             $oServer->ExternalAccessPop3AlterPort = $ExternalAccessPop3AlterPort; | 
| 1585:  |             $oServer->ExternalAccessPop3UseSsl = $ExternalAccessPop3UseSsl; | 
| 1586:  |  | 
| 1587:  |             $oServer->ExternalAccessSmtpServer = $ExternalAccessSmtpServer; | 
| 1588:  |             $oServer->ExternalAccessSmtpPort = $ExternalAccessSmtpPort; | 
| 1589:  |             $oServer->ExternalAccessSmtpAlterPort = $ExternalAccessSmtpAlterPort; | 
| 1590:  |             $oServer->ExternalAccessSmtpUseSsl = $ExternalAccessImapSmtpUseSsl; | 
| 1591:  |         } | 
| 1592:  |         $oServer->OAuthEnable = $OAuthEnable; | 
| 1593:  |         if ($oServer->OAuthEnable) { | 
| 1594:  |             $oServer->OAuthName = $OAuthName; | 
| 1595:  |             $oServer->OAuthType = $OAuthType; | 
| 1596:  |             $oServer->OAuthIconUrl = $OAuthIconUrl; | 
| 1597:  |         } | 
| 1598:  |  | 
| 1599:  |         return $this->getServersManager()->createServer($oServer); | 
| 1600:  |     } | 
| 1601:  |  | 
| 1602:  |      | 
| 1603:  |  | 
| 1604:  |  | 
| 1605:  |  | 
| 1606:  |  | 
| 1607:  |  | 
| 1608:  |  | 
| 1609:  |  | 
| 1610:  |  | 
| 1611:  |  | 
| 1612:  |  | 
| 1613:  |  | 
| 1614:  |  | 
| 1615:  |  | 
| 1616:  |  | 
| 1617:  |  | 
| 1618:  |  | 
| 1619:  |  | 
| 1620:  |  | 
| 1621:  |  | 
| 1622:  |  | 
| 1623:  |  | 
| 1624:  |  | 
| 1625:  |  | 
| 1626:  |  | 
| 1627:  |  | 
| 1628:  |  | 
| 1629:  |  | 
| 1630:  |  | 
| 1631:  |  | 
| 1632:  |  | 
| 1633:  |  | 
| 1634:  |  | 
| 1635:  |  | 
| 1636:  |  | 
| 1637:  |  | 
| 1638:  |  | 
| 1639:  |  | 
| 1640:  |  | 
| 1641:  |  | 
| 1642:  |  | 
| 1643:  |  | 
| 1644:  |  | 
| 1645:  |  | 
| 1646:  |  | 
| 1647:  |  | 
| 1648:  |  | 
| 1649:  |  | 
| 1650:  |  | 
| 1651:  |  | 
| 1652:  |  | 
| 1653:  |  | 
| 1654:  |  | 
| 1655:  |  | 
| 1656:  |  | 
| 1657:  |  | 
| 1658:  |  | 
| 1659:  |  | 
| 1660:  |  | 
| 1661:  |  | 
| 1662:  |  | 
| 1663:  |      | 
| 1664:  |  | 
| 1665:  |  | 
| 1666:  |  | 
| 1667:  |  | 
| 1668:  |  | 
| 1669:  |  | 
| 1670:  |  | 
| 1671:  |  | 
| 1672:  |  | 
| 1673:  |  | 
| 1674:  |  | 
| 1675:  |  | 
| 1676:  |  | 
| 1677:  |  | 
| 1678:  |  | 
| 1679:  |  | 
| 1680:  |  | 
| 1681:  |  | 
| 1682:  |  | 
| 1683:  |  | 
| 1684:  |  | 
| 1685:  |  | 
| 1686:  |  | 
| 1687:  |  | 
| 1688:  |  | 
| 1689:  |  | 
| 1690:  |  | 
| 1691:  |  | 
| 1692:  |  | 
| 1693:  |  | 
| 1694:  |  | 
| 1695:  |  | 
| 1696:  |  | 
| 1697:  |  | 
| 1698:  |  | 
| 1699:  |  | 
| 1700:  |  | 
| 1701:  |     public function UpdateServer( | 
| 1702:  |         $ServerId, | 
| 1703:  |         $Name, | 
| 1704:  |         $IncomingServer, | 
| 1705:  |         $IncomingPort, | 
| 1706:  |         $IncomingUseSsl, | 
| 1707:  |         $OutgoingServer, | 
| 1708:  |         $OutgoingPort, | 
| 1709:  |         $OutgoingUseSsl, | 
| 1710:  |         $SmtpAuthType, | 
| 1711:  |         $Domains, | 
| 1712:  |         $EnableThreading, | 
| 1713:  |         $EnableSieve, | 
| 1714:  |         $SievePort, | 
| 1715:  |         $SmtpLogin = '', | 
| 1716:  |         $SmtpPassword = '', | 
| 1717:  |         $UseFullEmailAddressAsLogin = true, | 
| 1718:  |         $TenantId = 0, | 
| 1719:  |         $SetExternalAccessServers = false, | 
| 1720:  |         $ExternalAccessImapServer = '', | 
| 1721:  |         $ExternalAccessImapPort = 143, | 
| 1722:  |         $ExternalAccessImapAlterPort = 0, | 
| 1723:  |         $ExternalAccessImapUseSsl = false, | 
| 1724:  |         $ExternalAccessPop3Server = '', | 
| 1725:  |         $ExternalAccessPop3Port = 143, | 
| 1726:  |         $ExternalAccessPop3AlterPort = 0, | 
| 1727:  |         $ExternalAccessPop3UseSsl = false, | 
| 1728:  |         $ExternalAccessSmtpServer = '', | 
| 1729:  |         $ExternalAccessSmtpPort = 25, | 
| 1730:  |         $ExternalAccessSmtpAlterPort = 0, | 
| 1731:  |         $ExternalAccessSmtpUseSsl = false, | 
| 1732:  |         $OAuthEnable = false, | 
| 1733:  |         $OAuthName = '', | 
| 1734:  |         $OAuthType = '', | 
| 1735:  |         $OAuthIconUrl = '' | 
| 1736:  |     ) { | 
| 1737:  |         $bResult = false; | 
| 1738:  |  | 
| 1739:  |         $oServer = $this->getServersManager()->getServer($ServerId); | 
| 1740:  |  | 
| 1741:  |         if ($oServer->OwnerType === \Aurora\Modules\Mail\Enums\ServerOwnerType::SuperAdmin) { | 
| 1742:  |             \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); | 
| 1743:  |         } else { | 
| 1744:  |             \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin); | 
| 1745:  |         } | 
| 1746:  |  | 
| 1747:  |         if ($oServer && ($oServer->OwnerType === \Aurora\Modules\Mail\Enums\ServerOwnerType::SuperAdmin || | 
| 1748:  |                 $oServer->OwnerType === \Aurora\Modules\Mail\Enums\ServerOwnerType::Tenant && $oServer->TenantId === $TenantId)) { | 
| 1749:  |             $oServer->Name = $Name; | 
| 1750:  |             $oServer->IncomingServer = $IncomingServer; | 
| 1751:  |             $oServer->IncomingPort = $IncomingPort; | 
| 1752:  |             $oServer->IncomingUseSsl = $IncomingUseSsl; | 
| 1753:  |             $oServer->OutgoingServer = $OutgoingServer; | 
| 1754:  |             $oServer->OutgoingPort = $OutgoingPort; | 
| 1755:  |             $oServer->OutgoingUseSsl = $OutgoingUseSsl; | 
| 1756:  |             $oServer->SmtpAuthType = $SmtpAuthType; | 
| 1757:  |             $oServer->SmtpLogin = $SmtpLogin; | 
| 1758:  |              | 
| 1759:  |              | 
| 1760:  |             if ($SmtpPassword !== '*****') { | 
| 1761:  |                 $oServer->SmtpPassword = $SmtpPassword; | 
| 1762:  |             } | 
| 1763:  |             $oServer->Domains = $this->getServersManager()->trimDomains($Domains); | 
| 1764:  |             $oServer->EnableThreading = $EnableThreading; | 
| 1765:  |             $oServer->EnableSieve = $EnableSieve; | 
| 1766:  |             $oServer->SievePort = $SievePort; | 
| 1767:  |             $oServer->UseFullEmailAddressAsLogin = $UseFullEmailAddressAsLogin; | 
| 1768:  |             $oServer->SetExternalAccessServers = $SetExternalAccessServers; | 
| 1769:  |             if ($oServer->SetExternalAccessServers) { | 
| 1770:  |                 $oServer->ExternalAccessImapServer = $ExternalAccessImapServer; | 
| 1771:  |                 $oServer->ExternalAccessImapPort = $ExternalAccessImapPort; | 
| 1772:  |                 $oServer->ExternalAccessImapAlterPort = $ExternalAccessImapAlterPort; | 
| 1773:  |                 $oServer->ExternalAccessImapUseSsl = $ExternalAccessImapUseSsl; | 
| 1774:  |  | 
| 1775:  |                 $oServer->ExternalAccessPop3Server = $ExternalAccessPop3Server; | 
| 1776:  |                 $oServer->ExternalAccessPop3Port = $ExternalAccessPop3Port; | 
| 1777:  |                 $oServer->ExternalAccessPop3AlterPort = $ExternalAccessPop3AlterPort; | 
| 1778:  |                 $oServer->ExternalAccessPop3UseSsl = $ExternalAccessPop3UseSsl; | 
| 1779:  |  | 
| 1780:  |                 $oServer->ExternalAccessSmtpServer = $ExternalAccessSmtpServer; | 
| 1781:  |                 $oServer->ExternalAccessSmtpPort = $ExternalAccessSmtpPort; | 
| 1782:  |                 $oServer->ExternalAccessSmtpAlterPort = $ExternalAccessSmtpAlterPort; | 
| 1783:  |                 $oServer->ExternalAccessImapUseSsl = $ExternalAccessImapUseSsl; | 
| 1784:  |                 $oServer->ExternalAccessSmtpUseSsl = $ExternalAccessSmtpUseSsl; | 
| 1785:  |             } | 
| 1786:  |             $oServer->OAuthEnable = $OAuthEnable; | 
| 1787:  |             if ($oServer->OAuthEnable) { | 
| 1788:  |                 $oServer->OAuthName = $OAuthName; | 
| 1789:  |                 $oServer->OAuthType = $OAuthType; | 
| 1790:  |                 $oServer->OAuthIconUrl = $OAuthIconUrl; | 
| 1791:  |             } | 
| 1792:  |  | 
| 1793:  |             $bResult = $this->getServersManager()->updateServer($oServer); | 
| 1794:  |         } else { | 
| 1795:  |             $bResult = false; | 
| 1796:  |         } | 
| 1797:  |  | 
| 1798:  |         return $bResult; | 
| 1799:  |     } | 
| 1800:  |  | 
| 1801:  |      | 
| 1802:  |  | 
| 1803:  |  | 
| 1804:  |  | 
| 1805:  |  | 
| 1806:  |  | 
| 1807:  |  | 
| 1808:  |  | 
| 1809:  |  | 
| 1810:  |  | 
| 1811:  |  | 
| 1812:  |  | 
| 1813:  |  | 
| 1814:  |  | 
| 1815:  |  | 
| 1816:  |  | 
| 1817:  |  | 
| 1818:  |  | 
| 1819:  |  | 
| 1820:  |  | 
| 1821:  |  | 
| 1822:  |  | 
| 1823:  |  | 
| 1824:  |  | 
| 1825:  |  | 
| 1826:  |  | 
| 1827:  |  | 
| 1828:  |  | 
| 1829:  |  | 
| 1830:  |  | 
| 1831:  |  | 
| 1832:  |  | 
| 1833:  |  | 
| 1834:  |  | 
| 1835:  |  | 
| 1836:  |  | 
| 1837:  |  | 
| 1838:  |  | 
| 1839:  |  | 
| 1840:  |  | 
| 1841:  |  | 
| 1842:  |  | 
| 1843:  |  | 
| 1844:  |  | 
| 1845:  |  | 
| 1846:  |  | 
| 1847:  |  | 
| 1848:  |  | 
| 1849:  |      | 
| 1850:  |  | 
| 1851:  |  | 
| 1852:  |  | 
| 1853:  |  | 
| 1854:  |  | 
| 1855:  |     public function DeleteServer($ServerId, $TenantId = 0) | 
| 1856:  |     { | 
| 1857:  |         $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); | 
| 1858:  |         if ($oAuthenticatedUser instanceof \Aurora\Modules\Core\Models\User && $oAuthenticatedUser->Role === \Aurora\Modules\Mail\Enums\ServerOwnerType::Tenant) { | 
| 1859:  |             if ($TenantId !== $oAuthenticatedUser->IdTenant) { | 
| 1860:  |                 throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied); | 
| 1861:  |             } | 
| 1862:  |         } else { | 
| 1863:  |             \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); | 
| 1864:  |         } | 
| 1865:  |  | 
| 1866:  |         $mPrimaryAccounts = $this->getAccountsManager()->getAccounts(['ServerId' => $ServerId, 'UseToAuthorize' => true]); | 
| 1867:  |         if ($mPrimaryAccounts) { | 
| 1868:  |             foreach ($mPrimaryAccounts as $oAccount) { | 
| 1869:  |                 \Aurora\Modules\Core\Module::Decorator()->DeleteUser($oAccount->IdUser); | 
| 1870:  |             } | 
| 1871:  |         } | 
| 1872:  |  | 
| 1873:  |         $mSecondaryAccounts = $this->getAccountsManager()->getAccounts(['ServerId' => $ServerId, 'UseToAuthorize' => false]); | 
| 1874:  |         if ($mSecondaryAccounts) { | 
| 1875:  |             foreach ($mSecondaryAccounts as $oAccount) { | 
| 1876:  |                 self::Decorator()->DeleteAccount($oAccount->Id); | 
| 1877:  |             } | 
| 1878:  |         } | 
| 1879:  |  | 
| 1880:  |         return $this->getServersManager()->deleteServer($ServerId, $TenantId); | 
| 1881:  |     } | 
| 1882:  |  | 
| 1883:  |      | 
| 1884:  |  | 
| 1885:  |  | 
| 1886:  |  | 
| 1887:  |  | 
| 1888:  |  | 
| 1889:  |  | 
| 1890:  |  | 
| 1891:  |  | 
| 1892:  |  | 
| 1893:  |  | 
| 1894:  |  | 
| 1895:  |  | 
| 1896:  |  | 
| 1897:  |  | 
| 1898:  |  | 
| 1899:  |  | 
| 1900:  |  | 
| 1901:  |  | 
| 1902:  |  | 
| 1903:  |  | 
| 1904:  |  | 
| 1905:  |  | 
| 1906:  |  | 
| 1907:  |  | 
| 1908:  |  | 
| 1909:  |  | 
| 1910:  |  | 
| 1911:  |  | 
| 1912:  |  | 
| 1913:  |  | 
| 1914:  |  | 
| 1915:  |  | 
| 1916:  |  | 
| 1917:  |  | 
| 1918:  |  | 
| 1919:  |  | 
| 1920:  |  | 
| 1921:  |  | 
| 1922:  |  | 
| 1923:  |  | 
| 1924:  |  | 
| 1925:  |  | 
| 1926:  |  | 
| 1927:  |  | 
| 1928:  |  | 
| 1929:  |  | 
| 1930:  |  | 
| 1931:  |  | 
| 1932:  |  | 
| 1933:  |  | 
| 1934:  |  | 
| 1935:  |  | 
| 1936:  |  | 
| 1937:  |  | 
| 1938:  |  | 
| 1939:  |  | 
| 1940:  |  | 
| 1941:  |  | 
| 1942:  |  | 
| 1943:  |  | 
| 1944:  |  | 
| 1945:  |  | 
| 1946:  |  | 
| 1947:  |  | 
| 1948:  |  | 
| 1949:  |  | 
| 1950:  |  | 
| 1951:  |  | 
| 1952:  |  | 
| 1953:  |  | 
| 1954:  |  | 
| 1955:  |      | 
| 1956:  |  | 
| 1957:  |  | 
| 1958:  |  | 
| 1959:  |  | 
| 1960:  |     public function GetFolders($AccountID) | 
| 1961:  |     { | 
| 1962:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 1963:  |  | 
| 1964:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 1965:  |  | 
| 1966:  |         self::checkAccess($oAccount); | 
| 1967:  |  | 
| 1968:  |         $oFolderCollection = $this->getMailManager()->getFolders($oAccount); | 
| 1969:  |         return array( | 
| 1970:  |             'Folders' => $oFolderCollection, | 
| 1971:  |             'Namespace' => $oFolderCollection->GetNamespace() | 
| 1972:  |         ); | 
| 1973:  |     } | 
| 1974:  |  | 
| 1975:  |     public function getSortInfo($SortBy, $SortOrder) | 
| 1976:  |     { | 
| 1977:  |         $aMessagesSortBy = $this->getConfig('MessagesSortBy', false); | 
| 1978:  |  | 
| 1979:  |         if ($SortOrder === null && $aMessagesSortBy !== false && isset($aMessagesSortBy['Allow']) && (bool) $aMessagesSortBy['Allow'] === true) { | 
| 1980:  |             $oSortOrder = new \Aurora\System\Enums\SortOrder(); | 
| 1981:  |             $aSortOrderMap = $oSortOrder->getMap(); | 
| 1982:  |             $SortOrder = isset($aMessagesSortBy['SortOrder']) ? $aSortOrderMap[\strtoupper($aMessagesSortBy['SortOrder'])] : ''; | 
| 1983:  |         } | 
| 1984:  |  | 
| 1985:  |         if ($SortBy === null) { | 
| 1986:  |             if ($aMessagesSortBy !== false && isset($aMessagesSortBy['Allow']) && (bool) $aMessagesSortBy['Allow'] === true) { | 
| 1987:  |                 $SortBy = isset($aMessagesSortBy['DefaultSortBy']) ? $aMessagesSortBy['DefaultSortBy'] : ''; | 
| 1988:  |             } | 
| 1989:  |         } else { | 
| 1990:  |             if ($aMessagesSortBy === false || isset($aMessagesSortBy['Allow']) && (bool) $aMessagesSortBy['Allow'] === false) { | 
| 1991:  |                 $SortBy = ''; | 
| 1992:  |             } | 
| 1993:  |         } | 
| 1994:  |  | 
| 1995:  |         if (empty($SortBy)) { | 
| 1996:  |             $SortOrder = \Aurora\System\Enums\SortOrder::ASC; | 
| 1997:  |         } | 
| 1998:  |  | 
| 1999:  |         return [$SortBy, $SortOrder]; | 
| 2000:  |     } | 
| 2001:  |  | 
| 2002:  |      | 
| 2003:  |  | 
| 2004:  |  | 
| 2005:  |  | 
| 2006:  |  | 
| 2007:  |  | 
| 2008:  |  | 
| 2009:  |  | 
| 2010:  |  | 
| 2011:  |  | 
| 2012:  |  | 
| 2013:  |  | 
| 2014:  |  | 
| 2015:  |  | 
| 2016:  |  | 
| 2017:  |  | 
| 2018:  |  | 
| 2019:  |  | 
| 2020:  |  | 
| 2021:  |  | 
| 2022:  |  | 
| 2023:  |  | 
| 2024:  |  | 
| 2025:  |  | 
| 2026:  |  | 
| 2027:  |  | 
| 2028:  |  | 
| 2029:  |  | 
| 2030:  |  | 
| 2031:  |  | 
| 2032:  |  | 
| 2033:  |  | 
| 2034:  |  | 
| 2035:  |  | 
| 2036:  |  | 
| 2037:  |  | 
| 2038:  |  | 
| 2039:  |  | 
| 2040:  |  | 
| 2041:  |  | 
| 2042:  |  | 
| 2043:  |  | 
| 2044:  |  | 
| 2045:  |  | 
| 2046:  |  | 
| 2047:  |  | 
| 2048:  |  | 
| 2049:  |  | 
| 2050:  |  | 
| 2051:  |  | 
| 2052:  |  | 
| 2053:  |  | 
| 2054:  |  | 
| 2055:  |  | 
| 2056:  |  | 
| 2057:  |  | 
| 2058:  |  | 
| 2059:  |  | 
| 2060:  |  | 
| 2061:  |  | 
| 2062:  |  | 
| 2063:  |  | 
| 2064:  |  | 
| 2065:  |  | 
| 2066:  |  | 
| 2067:  |  | 
| 2068:  |  | 
| 2069:  |  | 
| 2070:  |  | 
| 2071:  |  | 
| 2072:  |  | 
| 2073:  |  | 
| 2074:  |  | 
| 2075:  |  | 
| 2076:  |  | 
| 2077:  |  | 
| 2078:  |  | 
| 2079:  |  | 
| 2080:  |  | 
| 2081:  |  | 
| 2082:  |  | 
| 2083:  |  | 
| 2084:  |  | 
| 2085:  |  | 
| 2086:  |  | 
| 2087:  |  | 
| 2088:  |  | 
| 2089:  |  | 
| 2090:  |  | 
| 2091:  |  | 
| 2092:  |  | 
| 2093:  |  | 
| 2094:  |  | 
| 2095:  |  | 
| 2096:  |  | 
| 2097:  |  | 
| 2098:  |  | 
| 2099:  |  | 
| 2100:  |  | 
| 2101:  |  | 
| 2102:  |  | 
| 2103:  |  | 
| 2104:  |  | 
| 2105:  |  | 
| 2106:  |  | 
| 2107:  |  | 
| 2108:  |  | 
| 2109:  |  | 
| 2110:  |  | 
| 2111:  |  | 
| 2112:  |  | 
| 2113:  |  | 
| 2114:  |      | 
| 2115:  |  | 
| 2116:  |  | 
| 2117:  |  | 
| 2118:  |  | 
| 2119:  |  | 
| 2120:  |  | 
| 2121:  |  | 
| 2122:  |  | 
| 2123:  |  | 
| 2124:  |  | 
| 2125:  |  | 
| 2126:  |  | 
| 2127:  |     public function GetMessages($AccountID, $Folder, $Offset = 0, $Limit = 20, $Search = '', $Filters = '', $UseThreading = false, $InboxUidnext = '', $SortBy = null, $SortOrder = null) | 
| 2128:  |     { | 
| 2129:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 2130:  |  | 
| 2131:  |         $sSearch = \trim((string) $Search); | 
| 2132:  |  | 
| 2133:  |         $aFilters = array(); | 
| 2134:  |         $sFilters = \strtolower(\trim((string) $Filters)); | 
| 2135:  |         if (0 < \strlen($sFilters)) { | 
| 2136:  |             $aFilters = \array_filter(\explode(',', $sFilters), function ($sValue) { | 
| 2137:  |                 return '' !== trim($sValue); | 
| 2138:  |             }); | 
| 2139:  |         } | 
| 2140:  |  | 
| 2141:  |         $iOffset = (int) $Offset; | 
| 2142:  |         $iLimit = (int) $Limit; | 
| 2143:  |  | 
| 2144:  |         if (0 === \strlen(trim($Folder)) || 0 > $iOffset || 0 >= $iLimit || 200 < $iLimit) { | 
| 2145:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 2146:  |         } | 
| 2147:  |  | 
| 2148:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 2149:  |  | 
| 2150:  |         self::checkAccess($oAccount); | 
| 2151:  |  | 
| 2152:  |         $aSortInfo = $this->getSortInfo($SortBy, $SortOrder); | 
| 2153:  |  | 
| 2154:  |         $sSortBy = \strtoupper($aSortInfo[0]); | 
| 2155:  |         $sSortOrder = $aSortInfo[1] === \Aurora\System\Enums\SortOrder::DESC ? 'REVERSE' : ''; | 
| 2156:  |  | 
| 2157:  |         return  $this->getMailManager()->getMessageList( | 
| 2158:  |             $oAccount, | 
| 2159:  |             $Folder, | 
| 2160:  |             $iOffset, | 
| 2161:  |             $iLimit, | 
| 2162:  |             $sSearch, | 
| 2163:  |             $UseThreading, | 
| 2164:  |             $aFilters, | 
| 2165:  |             $InboxUidnext, | 
| 2166:  |             $sSortBy, | 
| 2167:  |             $sSortOrder | 
| 2168:  |         ); | 
| 2169:  |     } | 
| 2170:  |  | 
| 2171:  |     protected function getFoldersForSearch($oAccount, $Folder, $Search, &$sSearch) | 
| 2172:  |     { | 
| 2173:  |         $iSearchInFoldersType = SearchInFoldersType::Cur; | 
| 2174:  |         if (!empty(trim($Search))) { | 
| 2175:  |             $aSearch = explode(' ', $Search); | 
| 2176:  |             if (is_array($aSearch) && count($aSearch) > 0) { | 
| 2177:  |                 $iKey = array_search('folders:sub', $aSearch); | 
| 2178:  |                 if ($iKey !== false) { | 
| 2179:  |                     $iSearchInFoldersType = SearchInFoldersType::Sub; | 
| 2180:  |                     unset($aSearch[$iKey]); | 
| 2181:  |                 } else { | 
| 2182:  |                     $iKey = array_search('folders:all', $aSearch); | 
| 2183:  |                     if ($iKey !== false) { | 
| 2184:  |                         $iSearchInFoldersType = SearchInFoldersType::All; | 
| 2185:  |                         unset($aSearch[$iKey]); | 
| 2186:  |                     } | 
| 2187:  |                 } | 
| 2188:  |                 $sSearch = implode(' ', $aSearch); | 
| 2189:  |             } | 
| 2190:  |         } | 
| 2191:  |  | 
| 2192:  |         $aFolders = []; | 
| 2193:  |         $bCreateUnExistenSystemFolders = $Folder === ''; | 
| 2194:  |         if ($iSearchInFoldersType === SearchInFoldersType::Cur) { | 
| 2195:  |             $oFoldersColl = $this->getMailManager()->getFolders($oAccount, $bCreateUnExistenSystemFolders); | 
| 2196:  |             $oFolder = $oFoldersColl->getFolder($Folder); | 
| 2197:  |             $aFolders = [$oFolder]; | 
| 2198:  |         } else { | 
| 2199:  |             if ($iSearchInFoldersType === SearchInFoldersType::All) { | 
| 2200:  |                 $Folder = ''; | 
| 2201:  |             } | 
| 2202:  |             $oFoldersColl = $this->getMailManager()->getFolders($oAccount, $bCreateUnExistenSystemFolders, $Folder); | 
| 2203:  |  | 
| 2204:  |             $oFoldersColl->foreachWithSubFolders(function ($oFolder) use (&$aFolders) { | 
| 2205:  |                 if ($oFolder->isSubscribed() && $oFolder->isSelectable()) { | 
| 2206:  |                     if ($oFolder->getFolderXListType() !== \Aurora\Modules\Mail\Enums\FolderType::All) { | 
| 2207:  |                         $aFolders[] = $oFolder; | 
| 2208:  |                     } | 
| 2209:  |                 } | 
| 2210:  |             }); | 
| 2211:  |         } | 
| 2212:  |  | 
| 2213:  |         return $aFolders; | 
| 2214:  |     } | 
| 2215:  |  | 
| 2216:  |     public function GetMessagesByFolders($AccountID, $Folder = '', $Offset = 0, $Limit = 20, $Search = '', $Filters = '', $UseThreading = false, $InboxUidnext = '', $SortBy = null, $SortOrder = null) | 
| 2217:  |     { | 
| 2218:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 2219:  |  | 
| 2220:  |         $sSearch = \trim((string) $Search); | 
| 2221:  |  | 
| 2222:  |         $aFilters = []; | 
| 2223:  |         $sFilters = \strtolower(\trim((string) $Filters)); | 
| 2224:  |         if (0 < \strlen($sFilters)) { | 
| 2225:  |             $aFilters = \array_filter(\explode(',', $sFilters), function ($sValue) { | 
| 2226:  |                 return '' !== trim($sValue); | 
| 2227:  |             }); | 
| 2228:  |         } | 
| 2229:  |  | 
| 2230:  |         $iOffset = (int) $Offset; | 
| 2231:  |         $iLimit = (int) $Limit; | 
| 2232:  |  | 
| 2233:  |         if (0 > $iOffset || 0 >= $iLimit || 200 < $iLimit) { | 
| 2234:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 2235:  |         } | 
| 2236:  |  | 
| 2237:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 2238:  |  | 
| 2239:  |         self::checkAccess($oAccount); | 
| 2240:  |  | 
| 2241:  |         $aSortInfo = $this->getSortInfo($SortBy, $SortOrder); | 
| 2242:  |  | 
| 2243:  |         $sSortBy = \strtoupper($aSortInfo[0]); | 
| 2244:  |         $sSortOrder = $aSortInfo[1] === \Aurora\System\Enums\SortOrder::DESC ? 'REVERSE' : ''; | 
| 2245:  |  | 
| 2246:  |         $oMessageCollectionResult = \Aurora\Modules\Mail\Classes\MessageCollection::createInstance(); | 
| 2247:  |         $oMessageCollectionResult->FolderName = $Folder; | 
| 2248:  |         $oMessageCollectionResult->Limit = $iLimit; | 
| 2249:  |         $oMessageCollectionResult->Offset = $iOffset; | 
| 2250:  |         $oMessageCollectionResult->Search = $Search; | 
| 2251:  |         $oMessageCollectionResult->Filters = implode(',', $aFilters); | 
| 2252:  |  | 
| 2253:  |         $aFolderUids = []; | 
| 2254:  |         $aUids = []; | 
| 2255:  |         $iMessagesCount = 0; | 
| 2256:  |         $iMessagesResultCount = 0; | 
| 2257:  |         $iMessagesUnseenCount = 0; | 
| 2258:  |         $aFoldersHash = []; | 
| 2259:  |  | 
| 2260:  |         $sSortBy = 'ARRIVAL'; | 
| 2261:  |         $sSortOrder = $SortOrder === \Aurora\System\Enums\SortOrder::DESC ? 'REVERSE' : ''; | 
| 2262:  |  | 
| 2263:  |         $aFolders = $this->getFoldersForSearch($oAccount, $Folder, $Search, $sSearch); | 
| 2264:  |         foreach ($aFolders as $oFolder) { | 
| 2265:  |             $sFolder = $oFolder->getRawFullName(); | 
| 2266:  |             $aUnifiedInfo = $this->getMailManager()->getUnifiedMailboxMessagesInfo($oAccount, $sFolder, $sSearch, $aFilters, $UseThreading, $iOffset + $iLimit, $sSortBy, $sSortOrder); | 
| 2267:  |             if (is_array($aUnifiedInfo['Uids']) && count($aUnifiedInfo['Uids']) > 0) { | 
| 2268:  |                 foreach ($aUnifiedInfo['Uids'] as $iKey => $aUid) { | 
| 2269:  |                     $aUnifiedInfo['Uids'][$iKey]['folder'] = $sFolder; | 
| 2270:  |                 } | 
| 2271:  |                 $aUids = array_merge( | 
| 2272:  |                     $aUids, | 
| 2273:  |                     $aUnifiedInfo['Uids'] | 
| 2274:  |                 ); | 
| 2275:  |             } | 
| 2276:  |             $iMessagesCount += $aUnifiedInfo['Count']; | 
| 2277:  |             $iMessagesResultCount += $aUnifiedInfo['ResultCount']; | 
| 2278:  |             $iMessagesUnseenCount += $aUnifiedInfo['UnreadCount']; | 
| 2279:  |             $aFoldersHash[] = $sFolder . ':' . $aUnifiedInfo['FolderHash']; | 
| 2280:  |         } | 
| 2281:  |  | 
| 2282:  |          | 
| 2283:  |         usort($aUids, function ($a, $b) use ($SortOrder) { | 
| 2284:  |             if ($SortOrder === \Aurora\System\Enums\SortOrder::DESC) { | 
| 2285:  |                 return (strtotime($a['internaldate']) < strtotime($b['internaldate'])) ? 1 : -1; | 
| 2286:  |             } else { | 
| 2287:  |                 return (strtotime($a['internaldate']) > strtotime($b['internaldate'])) ? 1 : -1; | 
| 2288:  |             } | 
| 2289:  |         }); | 
| 2290:  |         if (count($aUids) >= 0) { | 
| 2291:  |             $aUids = array_slice($aUids, $iOffset, $iLimit); | 
| 2292:  |         } | 
| 2293:  |  | 
| 2294:  |         $aAllMessages = []; | 
| 2295:  |         $aNextUids = []; | 
| 2296:  |         $aFoldersHash = []; | 
| 2297:  |  | 
| 2298:  |         $aInboxUidsNext = []; | 
| 2299:  |         if (!empty($InboxUidnext)) { | 
| 2300:  |             $aInboxUids = \explode('.', $InboxUidnext); | 
| 2301:  |             foreach ($aInboxUids as $aUid) { | 
| 2302:  |                 $aUidsNext = \explode(':', $aUid); | 
| 2303:  |                 if (count($aUidsNext) === 2) { | 
| 2304:  |                     $aInboxUidsNext[$aUidsNext[0]] = $aUidsNext[1]; | 
| 2305:  |                 } | 
| 2306:  |             } | 
| 2307:  |         } | 
| 2308:  |  | 
| 2309:  |         foreach ($aUids as $aUid) { | 
| 2310:  |             $aFolderUids[$aUid['folder']][] = $aUid['uid']; | 
| 2311:  |         } | 
| 2312:  |         foreach ($aFolderUids as $sFolder => $aFldUids) { | 
| 2313:  |             $sFolder = (string) $sFolder; | 
| 2314:  |             $sInboxUidnext = isset($aInboxUidsNext[$sFolder]) ? $aInboxUidsNext[$sFolder] : ''; | 
| 2315:  |  | 
| 2316:  |             $oMessageCollection = $this->getMailManager()->getMessageListByUids( | 
| 2317:  |                 $oAccount, | 
| 2318:  |                 $sFolder, | 
| 2319:  |                 $aFldUids, | 
| 2320:  |                 $sInboxUidnext | 
| 2321:  |             ); | 
| 2322:  |  | 
| 2323:  |             if ($UseThreading) { | 
| 2324:  |                 $oMessageCollection->ForeachList(function ( $oMessage) use ($aUids, $sFolder) { | 
| 2325:  |                     $iUid = $oMessage->getUid(); | 
| 2326:  |                     $aUidInfo = current(array_filter($aUids, function ($aUid) use ($sFolder, $iUid) { | 
| 2327:  |                         return $aUid['folder'] === $sFolder && $aUid['uid'] == $iUid; | 
| 2328:  |                     })); | 
| 2329:  |                     if (isset($aUidInfo['threads']) && is_array($aUidInfo['threads'])) { | 
| 2330:  |                         $oMessage->setThreads($aUidInfo['threads']); | 
| 2331:  |                     } | 
| 2332:  |                 }); | 
| 2333:  |             } | 
| 2334:  |  | 
| 2335:  |             foreach ($oMessageCollection->New as $aNew) { | 
| 2336:  |                 $aNew['Folder'] = $sFolder; | 
| 2337:  |                 $oMessageCollectionResult->New[] = $aNew; | 
| 2338:  |             } | 
| 2339:  |  | 
| 2340:  |             $aNextUids[] = $sFolder . ':' . $oMessageCollection->UidNext; | 
| 2341:  |             $aMessages = $oMessageCollection->GetAsArray(); | 
| 2342:  |             foreach ($aMessages as $oMessage) { | 
| 2343:  |                  | 
| 2344:  |                  | 
| 2345:  |                 $oMessage->setUnifiedUid($oAccount->Id . ':' . $sFolder . ':' . $oMessage->getUid()); | 
| 2346:  |             } | 
| 2347:  |             $aAllMessages = array_merge($aAllMessages, $aMessages); | 
| 2348:  |         } | 
| 2349:  |  | 
| 2350:  |          | 
| 2351:  |         usort($aAllMessages, function ($a, $b) use ($SortOrder) { | 
| 2352:  |             if ($SortOrder === \Aurora\System\Enums\SortOrder::DESC) { | 
| 2353:  |                 return ($a->getReceivedOrDateTimeStamp() < $b->getReceivedOrDateTimeStamp()) ? 1 : -1; | 
| 2354:  |             } else { | 
| 2355:  |                 return ($a->getReceivedOrDateTimeStamp() > $b->getReceivedOrDateTimeStamp()) ? 1 : -1; | 
| 2356:  |             } | 
| 2357:  |         }); | 
| 2358:  |  | 
| 2359:  |         $oMessageCollectionResult->Uids = array_map(function ($oMessage) { | 
| 2360:  |             return $oMessage->getUnifiedUid(); | 
| 2361:  |         }, $aAllMessages); | 
| 2362:  |  | 
| 2363:  |         $oMessageCollectionResult->MessageCount = $iMessagesCount; | 
| 2364:  |         $oMessageCollectionResult->MessageResultCount = $iMessagesResultCount; | 
| 2365:  |         $oMessageCollectionResult->MessageUnseenCount = $iMessagesUnseenCount; | 
| 2366:  |         $oMessageCollectionResult->UidNext = implode('.', $aNextUids); | 
| 2367:  |         $oMessageCollectionResult->FolderHash = implode('.', $aFoldersHash); | 
| 2368:  |         $oMessageCollectionResult->AddArray($aAllMessages); | 
| 2369:  |  | 
| 2370:  |         return $oMessageCollectionResult; | 
| 2371:  |     } | 
| 2372:  |  | 
| 2373:  |     public function GetUnifiedMailboxMessages($UserId, $Folder = 'INBOX', $Offset = 0, $Limit = 20, $Search = '', $Filters = '', $UseThreading = false, $InboxUidnext = '', $SortOrder = \Aurora\System\Enums\SortOrder::DESC) | 
| 2374:  |     { | 
| 2375:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 2376:  |         self::checkAccess(null, $UserId); | 
| 2377:  |         if (!$this->getConfig('AllowUnifiedInbox', false)) { | 
| 2378:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied); | 
| 2379:  |         } | 
| 2380:  |  | 
| 2381:  |         $aFilters = array(); | 
| 2382:  |         $sFilters = \strtolower(\trim((string) $Filters)); | 
| 2383:  |         if (0 < \strlen($sFilters)) { | 
| 2384:  |             $aFilters = \array_filter(\explode(',', $sFilters), function ($sValue) { | 
| 2385:  |                 return '' !== trim($sValue); | 
| 2386:  |             }); | 
| 2387:  |         } | 
| 2388:  |  | 
| 2389:  |         $oMessageCollectionResult = \Aurora\Modules\Mail\Classes\MessageCollection::createInstance(); | 
| 2390:  |         $oMessageCollectionResult->FolderName = $Folder; | 
| 2391:  |         $oMessageCollectionResult->Limit = $Limit; | 
| 2392:  |         $oMessageCollectionResult->Offset = $Offset; | 
| 2393:  |         $oMessageCollectionResult->Search = $Search; | 
| 2394:  |         $oMessageCollectionResult->Filters = implode(',', $aFilters); | 
| 2395:  |  | 
| 2396:  |         $aAccounts = $this->getAccountsManager()->getUserAccounts($UserId); | 
| 2397:  |         $aAccountsCache = []; | 
| 2398:  |         $aUids = []; | 
| 2399:  |         $aAccountUids = []; | 
| 2400:  |         $iMessagesCount = 0; | 
| 2401:  |         $iMessagesResultCount = 0; | 
| 2402:  |         $iMessagesUnseenCount = 0; | 
| 2403:  |         $aFoldersHash = []; | 
| 2404:  |  | 
| 2405:  |         $sSortBy = 'ARRIVAL'; | 
| 2406:  |         $sSortOrder = $SortOrder === \Aurora\System\Enums\SortOrder::DESC ? 'REVERSE' : ''; | 
| 2407:  |  | 
| 2408:  |         foreach ($aAccounts as $oAccount) { | 
| 2409:  |             if ($oAccount->IncludeInUnifiedMailbox) { | 
| 2410:  |                 $aAccountsCache[$oAccount->Id]['Account'] = $oAccount; | 
| 2411:  |                 $aAccountUids[$oAccount->Id] = []; | 
| 2412:  |                 $sSearch = $Search; | 
| 2413:  |  | 
| 2414:  |                 $aFolders = $this->getFoldersForSearch($oAccount, $Folder, $Search, $sSearch); | 
| 2415:  |  | 
| 2416:  |                 $aFolderFullNamesRaw = array_map(function ($oFolder) { | 
| 2417:  |                     return $oFolder->getRawFullName(); | 
| 2418:  |                 }, $aFolders); | 
| 2419:  |  | 
| 2420:  |                 $aFoldersInfo = $this->getMailManager()->getFolderListInformation($oAccount, $aFolderFullNamesRaw, true); | 
| 2421:  |                 foreach ($aFolderFullNamesRaw as $sFolder) { | 
| 2422:  |                     $aUnifiedInfo = $this->getMailManager()->getUnifiedMailboxMessagesInfo( | 
| 2423:  |                         $oAccount, | 
| 2424:  |                         $sFolder, | 
| 2425:  |                         $sSearch, | 
| 2426:  |                         $aFilters, | 
| 2427:  |                         $UseThreading, | 
| 2428:  |                         $Offset + $Limit, | 
| 2429:  |                         $sSortBy, | 
| 2430:  |                         $sSortOrder | 
| 2431:  |                     ); | 
| 2432:  |                     if (is_array($aUnifiedInfo['Uids']) && count($aUnifiedInfo['Uids']) > 0) { | 
| 2433:  |                         foreach ($aUnifiedInfo['Uids'] as $iKey => $aUid) { | 
| 2434:  |                             $aUnifiedInfo['Uids'][$iKey]['folder'] = $sFolder; | 
| 2435:  |                         } | 
| 2436:  |                         $aUids = array_merge( | 
| 2437:  |                             $aUids, | 
| 2438:  |                             $aUnifiedInfo['Uids'] | 
| 2439:  |                         ); | 
| 2440:  |                     } | 
| 2441:  |  | 
| 2442:  |                     $iMessagesCount += (int) $aFoldersInfo[$sFolder][0]; | 
| 2443:  |                     $iMessagesResultCount += $aUnifiedInfo['ResultCount']; | 
| 2444:  |                     $iMessagesUnseenCount += $aFoldersInfo[$sFolder][1]; | 
| 2445:  |                     $aFoldersHash[] = $aFoldersInfo[$sFolder][3]; | 
| 2446:  |                 } | 
| 2447:  |             } | 
| 2448:  |         } | 
| 2449:  |  | 
| 2450:  |          | 
| 2451:  |         usort($aUids, function ($a, $b) use ($SortOrder) { | 
| 2452:  |             if ($SortOrder === \Aurora\System\Enums\SortOrder::DESC) { | 
| 2453:  |                 return (strtotime($a['internaldate']) < strtotime($b['internaldate'])) ? 1 : -1; | 
| 2454:  |             } else { | 
| 2455:  |                 return (strtotime($a['internaldate']) > strtotime($b['internaldate'])) ? 1 : -1; | 
| 2456:  |             } | 
| 2457:  |         }); | 
| 2458:  |         if (count($aUids) >= 0) { | 
| 2459:  |             $aUids = array_slice($aUids, $Offset, $Limit); | 
| 2460:  |         } | 
| 2461:  |  | 
| 2462:  |         $aAllMessages = []; | 
| 2463:  |         $aNextUids = []; | 
| 2464:  |  | 
| 2465:  |         $aInboxUidsNext = []; | 
| 2466:  |         if (!empty($InboxUidnext)) { | 
| 2467:  |             $aInboxUids = \explode('.', $InboxUidnext); | 
| 2468:  |             foreach ($aInboxUids as $aUid) { | 
| 2469:  |                 $aUidsNext = \explode(':', $aUid); | 
| 2470:  |                 if (count($aUidsNext) === 3) { | 
| 2471:  |                     $aInboxUidsNext[$aUidsNext[0]][$aUidsNext[1]] = $aUidsNext[2]; | 
| 2472:  |                 } | 
| 2473:  |             } | 
| 2474:  |         } | 
| 2475:  |  | 
| 2476:  |         foreach ($aUids as $aUid) { | 
| 2477:  |             $aAccountUids[$aUid['accountid']][$aUid['folder']][] = $aUid['uid']; | 
| 2478:  |         } | 
| 2479:  |         foreach ($aAccountUids as $iAccountId => $aFolders) { | 
| 2480:  |             $oAccount = $aAccountsCache[$iAccountId]['Account']; | 
| 2481:  |             foreach ($aFolders as $sFolder => $aFolderUids) { | 
| 2482:  |                 $sInboxUidnext = isset($aInboxUidsNext[$iAccountId][$sFolder]) ? $aInboxUidsNext[$iAccountId][$sFolder] : ''; | 
| 2483:  |                 $oMessageCollection = $this->getMailManager()->getMessageListByUids( | 
| 2484:  |                     $oAccount, | 
| 2485:  |                     $sFolder, | 
| 2486:  |                     $aFolderUids, | 
| 2487:  |                     $sInboxUidnext | 
| 2488:  |                 ); | 
| 2489:  |  | 
| 2490:  |                 if ($UseThreading) { | 
| 2491:  |                     $oMessageCollection->ForeachList(function ( $oMessage) use ($aUids, $iAccountId, $sFolder) { | 
| 2492:  |                         $iUid = $oMessage->getUid(); | 
| 2493:  |                         $aUidInfo = current(array_filter($aUids, function ($aUid) use ($iAccountId, $iUid, $sFolder) { | 
| 2494:  |                             return $aUid['folder'] === $sFolder && $aUid['accountid'] === $iAccountId && $aUid['uid'] == $iUid; | 
| 2495:  |                         })); | 
| 2496:  |                         if (isset($aUidInfo['threads']) && is_array($aUidInfo['threads'])) { | 
| 2497:  |                             $oMessage->setThreads($aUidInfo['threads']); | 
| 2498:  |                         } | 
| 2499:  |                     }); | 
| 2500:  |                 } | 
| 2501:  |                 $sPrefix = $oAccount->Id . ':' . $sFolder . ':'; | 
| 2502:  |  | 
| 2503:  |                 foreach ($oMessageCollection->New as $aNew) { | 
| 2504:  |                     $aNew['AccountId'] = $oAccount->Id; | 
| 2505:  |                     $aNew['Folder'] = $sFolder; | 
| 2506:  |  | 
| 2507:  |                     $oMessageCollectionResult->New[] = $aNew; | 
| 2508:  |                 } | 
| 2509:  |  | 
| 2510:  |                 $aNextUids[] = $sPrefix . $oMessageCollection->UidNext; | 
| 2511:  |                 $aMessages = $oMessageCollection->GetAsArray(); | 
| 2512:  |                 foreach ($aMessages as $oMessage) { | 
| 2513:  |                      | 
| 2514:  |                      | 
| 2515:  |                     $oMessage->setUnifiedUid($sPrefix . $oMessage->getUid()); | 
| 2516:  |                 } | 
| 2517:  |                 $aAllMessages = array_merge($aAllMessages, $aMessages); | 
| 2518:  |             } | 
| 2519:  |         } | 
| 2520:  |  | 
| 2521:  |          | 
| 2522:  |         usort($aAllMessages, function ($a, $b) use ($SortOrder) { | 
| 2523:  |             if ($SortOrder === \Aurora\System\Enums\SortOrder::DESC) { | 
| 2524:  |                 return ($a->getReceivedOrDateTimeStamp() < $b->getReceivedOrDateTimeStamp()) ? 1 : -1; | 
| 2525:  |             } else { | 
| 2526:  |                 return ($a->getReceivedOrDateTimeStamp() > $b->getReceivedOrDateTimeStamp()) ? 1 : -1; | 
| 2527:  |             } | 
| 2528:  |         }); | 
| 2529:  |  | 
| 2530:  |         $oMessageCollectionResult->Uids = array_map(function ($oMessage) { | 
| 2531:  |             return $oMessage->getUnifiedUid(); | 
| 2532:  |         }, $aAllMessages); | 
| 2533:  |  | 
| 2534:  |         $oMessageCollectionResult->MessageCount = $iMessagesCount; | 
| 2535:  |         $oMessageCollectionResult->MessageResultCount = $iMessagesResultCount; | 
| 2536:  |         $oMessageCollectionResult->MessageUnseenCount = $iMessagesUnseenCount; | 
| 2537:  |         $oMessageCollectionResult->UidNext = implode('.', $aNextUids); | 
| 2538:  |         $oMessageCollectionResult->FolderHash = implode('.', $aFoldersHash); | 
| 2539:  |         $oMessageCollectionResult->AddArray($aAllMessages); | 
| 2540:  |  | 
| 2541:  |         return $oMessageCollectionResult; | 
| 2542:  |     } | 
| 2543:  |  | 
| 2544:  |     public function GetMessagesInfo($AccountID, $Folder, $Search = null, $UseThreading = false, $SortBy = null, $SortOrder = null) | 
| 2545:  |     { | 
| 2546:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 2547:  |  | 
| 2548:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 2549:  |  | 
| 2550:  |         self::checkAccess($oAccount); | 
| 2551:  |  | 
| 2552:  |         $aSortInfo = $this->getSortInfo($SortBy, $SortOrder); | 
| 2553:  |  | 
| 2554:  |         $sSortBy = \strtoupper($aSortInfo[0]); | 
| 2555:  |         $sSortOrder = $aSortInfo[1] === \Aurora\System\Enums\SortOrder::DESC ? 'REVERSE' : ''; | 
| 2556:  |  | 
| 2557:  |         return $this->getMailManager()->GetMessagesInfo( | 
| 2558:  |             $oAccount, | 
| 2559:  |             $Folder, | 
| 2560:  |             $Search, | 
| 2561:  |             $UseThreading, | 
| 2562:  |             $sSortBy, | 
| 2563:  |             $sSortOrder | 
| 2564:  |         ); | 
| 2565:  |     } | 
| 2566:  |  | 
| 2567:  |     public function GetUnifiedRelevantFoldersInformation($AccountsData) | 
| 2568:  |     { | 
| 2569:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 2570:  |         if (!$this->getConfig('AllowUnifiedInbox', false)) { | 
| 2571:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied); | 
| 2572:  |         } | 
| 2573:  |  | 
| 2574:  |         if (!\is_array($AccountsData) || 0 === \count($AccountsData)) { | 
| 2575:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 2576:  |         } | 
| 2577:  |  | 
| 2578:  |         $aResult = []; | 
| 2579:  |         $iUnifiedCount = 0; | 
| 2580:  |         $iUnifiedUnseenCount = 0; | 
| 2581:  |         $aUnifiedUidNext = []; | 
| 2582:  |         $aUnifiedFolderHash = []; | 
| 2583:  |         foreach ($AccountsData as $aAccountData) { | 
| 2584:  |             $iAccountId = $aAccountData['AccountID']; | 
| 2585:  |             $oAccount = $this->getAccountsManager()->getAccountById($iAccountId); | 
| 2586:  |             if ($oAccount instanceof Models\MailAccount) { | 
| 2587:  |                 self::checkAccess($oAccount); | 
| 2588:  |                 $aCounts = self::Decorator()->GetRelevantFoldersInformation($iAccountId, $aAccountData['Folders'], $aAccountData['UseListStatusIfPossible']); | 
| 2589:  |                 $aCounts['AccountId'] = $iAccountId; | 
| 2590:  |                 $aResult[] = $aCounts; | 
| 2591:  |                 if (isset($aCounts['Counts']['INBOX']) && $oAccount->IncludeInUnifiedMailbox) { | 
| 2592:  |                     $iUnifiedCount += $aCounts['Counts']['INBOX'][0]; | 
| 2593:  |                     $iUnifiedUnseenCount += $aCounts['Counts']['INBOX'][1]; | 
| 2594:  |                     $aUnifiedUidNext[] = $iAccountId . ':' . $aCounts['Counts']['INBOX'][2]; | 
| 2595:  |                     $aUnifiedFolderHash[] = $iAccountId . ':' . $aCounts['Counts']['INBOX'][3]; | 
| 2596:  |                 } | 
| 2597:  |             } | 
| 2598:  |         } | 
| 2599:  |  | 
| 2600:  |         return [ | 
| 2601:  |             'Accounts' => $aResult, | 
| 2602:  |             'Unified' => [$iUnifiedCount, $iUnifiedUnseenCount, implode('.', $aUnifiedUidNext), implode('.', $aUnifiedFolderHash)] | 
| 2603:  |         ]; | 
| 2604:  |     } | 
| 2605:  |  | 
| 2606:  |      | 
| 2607:  |  | 
| 2608:  |  | 
| 2609:  |  | 
| 2610:  |  | 
| 2611:  |  | 
| 2612:  |  | 
| 2613:  |  | 
| 2614:  |  | 
| 2615:  |  | 
| 2616:  |  | 
| 2617:  |  | 
| 2618:  |  | 
| 2619:  |  | 
| 2620:  |  | 
| 2621:  |  | 
| 2622:  |  | 
| 2623:  |  | 
| 2624:  |  | 
| 2625:  |  | 
| 2626:  |  | 
| 2627:  |  | 
| 2628:  |  | 
| 2629:  |  | 
| 2630:  |  | 
| 2631:  |  | 
| 2632:  |  | 
| 2633:  |  | 
| 2634:  |  | 
| 2635:  |  | 
| 2636:  |  | 
| 2637:  |  | 
| 2638:  |  | 
| 2639:  |  | 
| 2640:  |  | 
| 2641:  |  | 
| 2642:  |  | 
| 2643:  |  | 
| 2644:  |  | 
| 2645:  |  | 
| 2646:  |  | 
| 2647:  |  | 
| 2648:  |  | 
| 2649:  |  | 
| 2650:  |  | 
| 2651:  |  | 
| 2652:  |  | 
| 2653:  |  | 
| 2654:  |  | 
| 2655:  |  | 
| 2656:  |  | 
| 2657:  |      | 
| 2658:  |  | 
| 2659:  |  | 
| 2660:  |  | 
| 2661:  |  | 
| 2662:  |  | 
| 2663:  |  | 
| 2664:  |  | 
| 2665:  |  | 
| 2666:  |     public function GetRelevantFoldersInformation($AccountID, $Folders, $UseListStatusIfPossible) | 
| 2667:  |     { | 
| 2668:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 2669:  |  | 
| 2670:  |         if (!\is_array($Folders) || 0 === \count($Folders)) { | 
| 2671:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 2672:  |         } | 
| 2673:  |  | 
| 2674:  |         $aResult = array(); | 
| 2675:  |  | 
| 2676:  |         try { | 
| 2677:  |             $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 2678:  |  | 
| 2679:  |             self::checkAccess($oAccount); | 
| 2680:  |  | 
| 2681:  |             $aResult = $this->getMailManager()->getFolderListInformation($oAccount, $Folders, $UseListStatusIfPossible); | 
| 2682:  |         } catch (\MailSo\Net\Exceptions\ConnectionException $oException) { | 
| 2683:  |             throw $oException; | 
| 2684:  |         } catch (\MailSo\Imap\Exceptions\LoginException $oException) { | 
| 2685:  |             throw $oException; | 
| 2686:  |         } catch (\Aurora\Modules\Mail\Exceptions\Exception $oException) { | 
| 2687:  |             throw $oException; | 
| 2688:  |         } catch (\Exception $oException) { | 
| 2689:  |             \Aurora\System\Api::Log((string) $oException); | 
| 2690:  |         } | 
| 2691:  |  | 
| 2692:  |         return array( | 
| 2693:  |             'Counts' => $aResult, | 
| 2694:  |         ); | 
| 2695:  |     } | 
| 2696:  |  | 
| 2697:  |      | 
| 2698:  |  | 
| 2699:  |  | 
| 2700:  |  | 
| 2701:  |  | 
| 2702:  |  | 
| 2703:  |  | 
| 2704:  |  | 
| 2705:  |  | 
| 2706:  |  | 
| 2707:  |  | 
| 2708:  |  | 
| 2709:  |  | 
| 2710:  |  | 
| 2711:  |  | 
| 2712:  |  | 
| 2713:  |  | 
| 2714:  |  | 
| 2715:  |  | 
| 2716:  |  | 
| 2717:  |  | 
| 2718:  |  | 
| 2719:  |  | 
| 2720:  |  | 
| 2721:  |  | 
| 2722:  |  | 
| 2723:  |  | 
| 2724:  |  | 
| 2725:  |  | 
| 2726:  |  | 
| 2727:  |  | 
| 2728:  |  | 
| 2729:  |  | 
| 2730:  |  | 
| 2731:  |  | 
| 2732:  |  | 
| 2733:  |  | 
| 2734:  |  | 
| 2735:  |  | 
| 2736:  |  | 
| 2737:  |  | 
| 2738:  |  | 
| 2739:  |  | 
| 2740:  |  | 
| 2741:  |  | 
| 2742:  |  | 
| 2743:  |  | 
| 2744:  |      | 
| 2745:  |  | 
| 2746:  |  | 
| 2747:  |  | 
| 2748:  |  | 
| 2749:  |     public function GetQuota($AccountID) | 
| 2750:  |     { | 
| 2751:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 2752:  |  | 
| 2753:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 2754:  |  | 
| 2755:  |         self::checkAccess($oAccount); | 
| 2756:  |  | 
| 2757:  |         $aQuota = $this->getMailManager()->getQuota($oAccount); | 
| 2758:  |         $iQuota = (is_array($aQuota) && isset($aQuota[1])) ? $aQuota[1] / 1024 : 0; | 
| 2759:  |         $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserUnchecked($oAccount->IdUser); | 
| 2760:  |         $iUserSpaceLimitMb = ($oUser instanceof \Aurora\Modules\Core\Models\User) ? $oUser->{self::GetName() . '::UserSpaceLimitMb'} : 0; | 
| 2761:  |         if ($iQuota !== $iUserSpaceLimitMb) { | 
| 2762:  |             $this->updateAllocatedTenantSpace($oUser->IdTenant, $iQuota, $iUserSpaceLimitMb); | 
| 2763:  |             $oUser->setExtendedProp(self::GetName() . '::UserSpaceLimitMb', $iQuota); | 
| 2764:  |             $oUser->save(); | 
| 2765:  |         } | 
| 2766:  |  | 
| 2767:  |         return $aQuota;  | 
| 2768:  |     } | 
| 2769:  |  | 
| 2770:  |      | 
| 2771:  |  | 
| 2772:  |  | 
| 2773:  |  | 
| 2774:  |  | 
| 2775:  |  | 
| 2776:  |  | 
| 2777:  |  | 
| 2778:  |  | 
| 2779:  |  | 
| 2780:  |  | 
| 2781:  |  | 
| 2782:  |  | 
| 2783:  |  | 
| 2784:  |  | 
| 2785:  |  | 
| 2786:  |  | 
| 2787:  |  | 
| 2788:  |  | 
| 2789:  |  | 
| 2790:  |  | 
| 2791:  |  | 
| 2792:  |  | 
| 2793:  |  | 
| 2794:  |  | 
| 2795:  |  | 
| 2796:  |  | 
| 2797:  |  | 
| 2798:  |  | 
| 2799:  |  | 
| 2800:  |  | 
| 2801:  |  | 
| 2802:  |  | 
| 2803:  |  | 
| 2804:  |  | 
| 2805:  |  | 
| 2806:  |  | 
| 2807:  |  | 
| 2808:  |  | 
| 2809:  |  | 
| 2810:  |  | 
| 2811:  |  | 
| 2812:  |  | 
| 2813:  |  | 
| 2814:  |  | 
| 2815:  |  | 
| 2816:  |  | 
| 2817:  |  | 
| 2818:  |  | 
| 2819:  |  | 
| 2820:  |  | 
| 2821:  |  | 
| 2822:  |  | 
| 2823:  |  | 
| 2824:  |  | 
| 2825:  |  | 
| 2826:  |  | 
| 2827:  |  | 
| 2828:  |  | 
| 2829:  |  | 
| 2830:  |  | 
| 2831:  |  | 
| 2832:  |  | 
| 2833:  |  | 
| 2834:  |  | 
| 2835:  |  | 
| 2836:  |  | 
| 2837:  |  | 
| 2838:  |  | 
| 2839:  |  | 
| 2840:  |  | 
| 2841:  |  | 
| 2842:  |  | 
| 2843:  |  | 
| 2844:  |  | 
| 2845:  |  | 
| 2846:  |  | 
| 2847:  |  | 
| 2848:  |  | 
| 2849:  |  | 
| 2850:  |  | 
| 2851:  |  | 
| 2852:  |  | 
| 2853:  |  | 
| 2854:  |  | 
| 2855:  |  | 
| 2856:  |  | 
| 2857:  |  | 
| 2858:  |  | 
| 2859:  |  | 
| 2860:  |  | 
| 2861:  |  | 
| 2862:  |  | 
| 2863:  |  | 
| 2864:  |  | 
| 2865:  |  | 
| 2866:  |  | 
| 2867:  |  | 
| 2868:  |  | 
| 2869:  |  | 
| 2870:  |  | 
| 2871:  |  | 
| 2872:  |  | 
| 2873:  |  | 
| 2874:  |      | 
| 2875:  |  | 
| 2876:  |  | 
| 2877:  |  | 
| 2878:  |  | 
| 2879:  |  | 
| 2880:  |  | 
| 2881:  |  | 
| 2882:  |     public function GetMessagesBodies($AccountID, $Folder, $Uids, $MessageBodyTruncationThreshold = null) | 
| 2883:  |     { | 
| 2884:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 2885:  |  | 
| 2886:  |         if (0 === \strlen(\trim($Folder)) || !\is_array($Uids) || 0 === \count($Uids)) { | 
| 2887:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 2888:  |         } | 
| 2889:  |  | 
| 2890:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 2891:  |         $oImapClient =& $this->getMailManager()->_getImapClient($oAccount); | 
| 2892:  |         $oImapClient->FolderExamine($Folder); | 
| 2893:  |  | 
| 2894:  |         $aBodystructuresFetchResponse = $oImapClient->Fetch(array( | 
| 2895:  |             \MailSo\Imap\Enumerations\FetchType::BODYSTRUCTURE), \implode(',', $Uids), true); | 
| 2896:  |         $aBodystructures = []; | 
| 2897:  |         foreach ($aBodystructuresFetchResponse as $oBodystructureFetchResponse) { | 
| 2898:  |             $aBodystructures[(int) $oBodystructureFetchResponse->GetFetchValue('UID')] = $oBodystructureFetchResponse; | 
| 2899:  |         } | 
| 2900:  |         unset($aBodystructuresFetchResponse); | 
| 2901:  |  | 
| 2902:  |          | 
| 2903:  |  | 
| 2904:  |         $aList = array(); | 
| 2905:  |         foreach ($Uids as $iUid) { | 
| 2906:  |             if (\is_numeric($iUid)) { | 
| 2907:  |                 $oBody = isset($aBodystructures[$iUid]) ? $aBodystructures[$iUid] : null; | 
| 2908:  |                 $oMessage = $this->Decorator()->GetMessage($AccountID, $Folder, (string) $iUid, '', $MessageBodyTruncationThreshold, $oBody); | 
| 2909:  |                 if ($oMessage instanceof \Aurora\Modules\Mail\Classes\Message) { | 
| 2910:  |                     $aList[] = $oMessage; | 
| 2911:  |                 } | 
| 2912:  |  | 
| 2913:  |                 unset($oMessage); | 
| 2914:  |             } | 
| 2915:  |         } | 
| 2916:  |  | 
| 2917:  |         return $aList; | 
| 2918:  |     } | 
| 2919:  |  | 
| 2920:  |      | 
| 2921:  |  | 
| 2922:  |  | 
| 2923:  |  | 
| 2924:  |  | 
| 2925:  |  | 
| 2926:  |  | 
| 2927:  |  | 
| 2928:  |  | 
| 2929:  |  | 
| 2930:  |  | 
| 2931:  |  | 
| 2932:  |  | 
| 2933:  |  | 
| 2934:  |  | 
| 2935:  |  | 
| 2936:  |  | 
| 2937:  |  | 
| 2938:  |  | 
| 2939:  |  | 
| 2940:  |  | 
| 2941:  |  | 
| 2942:  |  | 
| 2943:  |  | 
| 2944:  |  | 
| 2945:  |  | 
| 2946:  |  | 
| 2947:  |  | 
| 2948:  |  | 
| 2949:  |  | 
| 2950:  |  | 
| 2951:  |  | 
| 2952:  |  | 
| 2953:  |  | 
| 2954:  |  | 
| 2955:  |  | 
| 2956:  |  | 
| 2957:  |  | 
| 2958:  |  | 
| 2959:  |  | 
| 2960:  |  | 
| 2961:  |  | 
| 2962:  |  | 
| 2963:  |  | 
| 2964:  |  | 
| 2965:  |  | 
| 2966:  |  | 
| 2967:  |  | 
| 2968:  |  | 
| 2969:  |  | 
| 2970:  |  | 
| 2971:  |  | 
| 2972:  |  | 
| 2973:  |  | 
| 2974:  |  | 
| 2975:  |  | 
| 2976:  |  | 
| 2977:  |  | 
| 2978:  |  | 
| 2979:  |  | 
| 2980:  |  | 
| 2981:  |  | 
| 2982:  |  | 
| 2983:  |  | 
| 2984:  |  | 
| 2985:  |  | 
| 2986:  |  | 
| 2987:  |  | 
| 2988:  |  | 
| 2989:  |  | 
| 2990:  |  | 
| 2991:  |  | 
| 2992:  |  | 
| 2993:  |  | 
| 2994:  |  | 
| 2995:  |  | 
| 2996:  |  | 
| 2997:  |  | 
| 2998:  |  | 
| 2999:  |  | 
| 3000:  |  | 
| 3001:  |  | 
| 3002:  |  | 
| 3003:  |  | 
| 3004:  |  | 
| 3005:  |  | 
| 3006:  |  | 
| 3007:  |  | 
| 3008:  |  | 
| 3009:  |  | 
| 3010:  |  | 
| 3011:  |  | 
| 3012:  |  | 
| 3013:  |  | 
| 3014:  |  | 
| 3015:  |  | 
| 3016:  |  | 
| 3017:  |  | 
| 3018:  |  | 
| 3019:  |  | 
| 3020:  |  | 
| 3021:  |      | 
| 3022:  |  | 
| 3023:  |  | 
| 3024:  |  | 
| 3025:  |  | 
| 3026:  |  | 
| 3027:  |  | 
| 3028:  |  | 
| 3029:  |  | 
| 3030:  |  | 
| 3031:  |     public function GetMessage($AccountID, $Folder, $Uid, $Rfc822MimeIndex = '', $MessageBodyTruncationThreshold = null, $oBody = null) | 
| 3032:  |     { | 
| 3033:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 3034:  |  | 
| 3035:  |         $iUid = 0 < \strlen($Uid) && \is_numeric($Uid) ? (int) $Uid : 0; | 
| 3036:  |  | 
| 3037:  |         if (0 === \strlen(\trim($Folder)) || 0 >= $iUid) { | 
| 3038:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 3039:  |         } | 
| 3040:  |  | 
| 3041:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3042:  |  | 
| 3043:  |         self::checkAccess($oAccount); | 
| 3044:  |  | 
| 3045:  |         if (0 === \strlen($Folder) || !\is_numeric($iUid) || 0 >= (int) $iUid) { | 
| 3046:  |             throw new InvalidArgumentException(); | 
| 3047:  |         } | 
| 3048:  |  | 
| 3049:  |         $oImapClient =& $this->getMailManager()->_getImapClient($oAccount); | 
| 3050:  |  | 
| 3051:  |         $oImapClient->FolderExamine($Folder); | 
| 3052:  |  | 
| 3053:  |         $oMessage = false; | 
| 3054:  |  | 
| 3055:  |         $aTextMimeIndexes = array(); | 
| 3056:  |  | 
| 3057:  |         if (!isset($oBody)) { | 
| 3058:  |             $aFetchResponse = $oImapClient->Fetch(array( | 
| 3059:  |                 \MailSo\Imap\Enumerations\FetchType::BODYSTRUCTURE), $iUid, true); | 
| 3060:  |             $oBodyStructure = (0 < \count($aFetchResponse)) ? $aFetchResponse[0]->GetFetchBodyStructure($Rfc822MimeIndex) : null; | 
| 3061:  |         } else { | 
| 3062:  |             $oBodyStructure = $oBody->GetFetchBodyStructure($Rfc822MimeIndex); | 
| 3063:  |         } | 
| 3064:  |  | 
| 3065:  |         $aCustomParts = array(); | 
| 3066:  |         if ($oBodyStructure) { | 
| 3067:  |             $aTextParts = $oBodyStructure->SearchHtmlOrPlainParts(); | 
| 3068:  |             if (\is_array($aTextParts) && 0 < \count($aTextParts)) { | 
| 3069:  |                 foreach ($aTextParts as $oPart) { | 
| 3070:  |                     $aTextMimeIndexes[] = array($oPart->PartID(), $oPart->Size()); | 
| 3071:  |                 } | 
| 3072:  |             } | 
| 3073:  |  | 
| 3074:  |             $aParts = $oBodyStructure->GetAllParts(); | 
| 3075:  |  | 
| 3076:  |             $this->broadcastEvent( | 
| 3077:  |                 'GetBodyStructureParts', | 
| 3078:  |                 $aParts, | 
| 3079:  |                 $aCustomParts | 
| 3080:  |             ); | 
| 3081:  |         } | 
| 3082:  |  | 
| 3083:  |         $bTruncated = false; | 
| 3084:  |         $aFetchItems = array( | 
| 3085:  |             \MailSo\Imap\Enumerations\FetchType::INDEX, | 
| 3086:  |             \MailSo\Imap\Enumerations\FetchType::UID, | 
| 3087:  |             \MailSo\Imap\Enumerations\FetchType::RFC822_SIZE, | 
| 3088:  |             \MailSo\Imap\Enumerations\FetchType::INTERNALDATE, | 
| 3089:  |             \MailSo\Imap\Enumerations\FetchType::FLAGS, | 
| 3090:  |             0 < strlen($Rfc822MimeIndex) | 
| 3091:  |                 ? \MailSo\Imap\Enumerations\FetchType::BODY_PEEK.'['.$Rfc822MimeIndex.'.HEADER]' | 
| 3092:  |                 : \MailSo\Imap\Enumerations\FetchType::BODY_HEADER_PEEK | 
| 3093:  |         ); | 
| 3094:  |  | 
| 3095:  |         if (0 < \count($aTextMimeIndexes)) { | 
| 3096:  |             if (0 < \strlen($Rfc822MimeIndex) && \is_numeric($Rfc822MimeIndex)) { | 
| 3097:  |                 $sLine = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK.'['.$aTextMimeIndexes[0][0].'.1]'; | 
| 3098:  |                 if (\is_numeric($MessageBodyTruncationThreshold) && 0 < $MessageBodyTruncationThreshold && $MessageBodyTruncationThreshold < $aTextMimeIndexes[0][1]) { | 
| 3099:  |                     $sLine .= '<0.'.((int) $MessageBodyTruncationThreshold).'>'; | 
| 3100:  |                     $bTruncated = true; | 
| 3101:  |                 } | 
| 3102:  |  | 
| 3103:  |                 $aFetchItems[] = $sLine; | 
| 3104:  |             } else { | 
| 3105:  |                 foreach ($aTextMimeIndexes as $aTextMimeIndex) { | 
| 3106:  |                     $sLine = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK.'['.$aTextMimeIndex[0].']'; | 
| 3107:  |                     if (\is_numeric($MessageBodyTruncationThreshold) && 0 < $MessageBodyTruncationThreshold && $MessageBodyTruncationThreshold < $aTextMimeIndex[1]) { | 
| 3108:  |                         $sLine .= '<0.'.((int) $MessageBodyTruncationThreshold).'>'; | 
| 3109:  |                         $bTruncated = true; | 
| 3110:  |                     } | 
| 3111:  |  | 
| 3112:  |                     $aFetchItems[] = $sLine; | 
| 3113:  |                 } | 
| 3114:  |             } | 
| 3115:  |         } | 
| 3116:  |  | 
| 3117:  |         foreach ($aCustomParts as $oCustomPart) { | 
| 3118:  |             $aFetchItems[] = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK.'['.$oCustomPart->PartID().']'; | 
| 3119:  |         } | 
| 3120:  |  | 
| 3121:  |         if (!$oBodyStructure) { | 
| 3122:  |             $aFetchItems[] = \MailSo\Imap\Enumerations\FetchType::BODYSTRUCTURE; | 
| 3123:  |         } | 
| 3124:  |  | 
| 3125:  |         $aFetchResponse = $oImapClient->Fetch($aFetchItems, $iUid, true); | 
| 3126:  |         if (0 < \count($aFetchResponse)) { | 
| 3127:  |             $oMessage = \Aurora\Modules\Mail\Classes\Message::createInstance($Folder, $aFetchResponse[0], $oBodyStructure, $Rfc822MimeIndex, $bTruncated); | 
| 3128:  |         } | 
| 3129:  |  | 
| 3130:  |         if ($oMessage) { | 
| 3131:  |             $oMessage->setAccountId($oAccount->Id); | 
| 3132:  |             $sFromEmail = ''; | 
| 3133:  |             $oFromCollection = $oMessage->getFrom(); | 
| 3134:  |             if ($oFromCollection && 0 < $oFromCollection->Count()) { | 
| 3135:  |                 $oFrom =& $oFromCollection->GetByIndex(0); | 
| 3136:  |                 if ($oFrom) { | 
| 3137:  |                     $sFromEmail = trim($oFrom->GetEmail()); | 
| 3138:  |                 } | 
| 3139:  |             } | 
| 3140:  |  | 
| 3141:  |             if (0 < \strlen($sFromEmail)) { | 
| 3142:  |                 $bAlwaysShowImagesInMessage = !!$this->getConfig('AlwaysShowImagesInMessage', false); | 
| 3143:  |                 $oMessage->setSafety($bAlwaysShowImagesInMessage ? true : | 
| 3144:  |                         $this->getMailManager()->isSafetySender($oAccount->IdUser, $sFromEmail)); | 
| 3145:  |             } | 
| 3146:  |  | 
| 3147:  |             $aData = array(); | 
| 3148:  |             foreach ($aCustomParts as $oCustomPart) { | 
| 3149:  |                 $sData = $aFetchResponse[0]->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::BODY.'['.$oCustomPart->PartID().']'); | 
| 3150:  |                 if (!empty($sData)) { | 
| 3151:  |                     $sData = \MailSo\Base\Utils::DecodeEncodingValue($sData, $oCustomPart->MailEncodingName()); | 
| 3152:  |                     $sData = \MailSo\Base\Utils::ConvertEncoding( | 
| 3153:  |                         $sData, | 
| 3154:  |                         \MailSo\Base\Utils::NormalizeCharset($oCustomPart->Charset(), true), | 
| 3155:  |                         \MailSo\Base\Enumerations\Charset::UTF_8 | 
| 3156:  |                     ); | 
| 3157:  |                 } | 
| 3158:  |                 $aData[] = array( | 
| 3159:  |                     'Data' => $sData, | 
| 3160:  |                     'Part' => $oCustomPart | 
| 3161:  |                 ); | 
| 3162:  |             } | 
| 3163:  |  | 
| 3164:  |             $this->broadcastEvent('ExtendMessageData', $aData, $oMessage); | 
| 3165:  |         } | 
| 3166:  |  | 
| 3167:  |         if (!($oMessage instanceof \Aurora\Modules\Mail\Classes\Message)) { | 
| 3168:  |             throw new \Aurora\Modules\Mail\Exceptions\Exception(Enums\ErrorCodes::CannotGetMessage); | 
| 3169:  |         } | 
| 3170:  |  | 
| 3171:  |         return $oMessage; | 
| 3172:  |     } | 
| 3173:  |  | 
| 3174:  |      | 
| 3175:  |  | 
| 3176:  |  | 
| 3177:  |  | 
| 3178:  |  | 
| 3179:  |  | 
| 3180:  |  | 
| 3181:  |  | 
| 3182:  |     public function Unsubscribe($AccountID, $Folder, $Uid) | 
| 3183:  |     { | 
| 3184:  |         $mResult = false; | 
| 3185:  |  | 
| 3186:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3187:  |  | 
| 3188:  |         self::checkAccess($oAccount); | 
| 3189:  |  | 
| 3190:  |         $validate = false; | 
| 3191:  |         $this->getMailManager()->directMessageToStream( | 
| 3192:  |             $oAccount, | 
| 3193:  |             function ($rResource) use (&$validate) { | 
| 3194:  |                 if (\is_resource($rResource)) { | 
| 3195:  |                     $rawMessage = stream_get_contents($rResource); | 
| 3196:  |  | 
| 3197:  |                     try { | 
| 3198:  |                         $dkimValidator = new DKIMValidator($rawMessage); | 
| 3199:  |                         $validateResult = @$dkimValidator->validate(); | 
| 3200:  |  | 
| 3201:  |                         $validateCount = count($validateResult); | 
| 3202:  |                         $validateSuccessCount = 0; | 
| 3203:  |                         if ($validateCount > 0) { | 
| 3204:  |                             foreach ($validateResult as $val) { | 
| 3205:  |                                 if ($val[0]['status'] === 'SUCCESS') { | 
| 3206:  |                                     $validateSuccessCount++; | 
| 3207:  |                                 } | 
| 3208:  |                             } | 
| 3209:  |                         } | 
| 3210:  |  | 
| 3211:  |                         $validate = $validateCount === $validateSuccessCount; | 
| 3212:  |                     } catch (DKIMException $e) { | 
| 3213:  |                         $validate = false; | 
| 3214:  |                     } | 
| 3215:  |                 } | 
| 3216:  |             }, | 
| 3217:  |             $Folder, | 
| 3218:  |             $Uid | 
| 3219:  |         ); | 
| 3220:  |  | 
| 3221:  |         if ($validate) { | 
| 3222:  |             $oMessage = self::Decorator()->GetMessage($AccountID, $Folder, $Uid); | 
| 3223:  |             if ($oMessage instanceof Message) { | 
| 3224:  |                 $aParsedHeaders = $oMessage->parseUnsubscribeHeaders(); | 
| 3225:  |  | 
| 3226:  |                 if ($aParsedHeaders['OneClick']) { | 
| 3227:  |                     if (!empty($aParsedHeaders['Url'])) { | 
| 3228:  |                         $iCode = 0; | 
| 3229:  |                         $this->oHttp->SendPostRequest( | 
| 3230:  |                             $aParsedHeaders['Url'], | 
| 3231:  |                             ['List-Unsubscribe' => 'One-Click'], | 
| 3232:  |                             '', | 
| 3233:  |                             $iCode, | 
| 3234:  |                             \Aurora\Api::SystemLogger() | 
| 3235:  |                         ); | 
| 3236:  |                         $mResult = ($iCode == 200); | 
| 3237:  |                     } elseif (!empty($aParsedHeaders['Email'])) { | 
| 3238:  |                         $sEmailCheck = str_ireplace('mailto:', '', $aParsedHeaders['Email']); | 
| 3239:  |                         $aEmailData = explode('?', $sEmailCheck); | 
| 3240:  |                         $mResult = self::Decorator()->SendMessage( | 
| 3241:  |                             $AccountID, | 
| 3242:  |                             null, | 
| 3243:  |                             null, | 
| 3244:  |                             0, | 
| 3245:  |                             [], | 
| 3246:  |                             "", | 
| 3247:  |                             $aEmailData[0], | 
| 3248:  |                             "", | 
| 3249:  |                             "", | 
| 3250:  |                             [], | 
| 3251:  |                             'Unsubscribe' | 
| 3252:  |                         ); | 
| 3253:  |                     } | 
| 3254:  |                 } | 
| 3255:  |             } | 
| 3256:  |         } | 
| 3257:  |  | 
| 3258:  |         return $mResult; | 
| 3259:  |     } | 
| 3260:  |  | 
| 3261:  |     public function GetMessageByMessageID($AccountID, $Folder, $UidFrom, $MessageID) | 
| 3262:  |     { | 
| 3263:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3264:  |  | 
| 3265:  |         self::checkAccess($oAccount); | 
| 3266:  |         $mResult = false; | 
| 3267:  |         $iUID = $this->getMailManager()->getMessageUIDByMessageID($oAccount, $Folder, $UidFrom, $MessageID); | 
| 3268:  |         if ($iUID !== false) { | 
| 3269:  |             $aMessages = $this->GetMessagesBodies($AccountID, $Folder, [$iUID]); | 
| 3270:  |             if (is_array($aMessages) && count($aMessages) > 0) { | 
| 3271:  |                 $mResult = $aMessages[0]; | 
| 3272:  |             } | 
| 3273:  |         } | 
| 3274:  |  | 
| 3275:  |         return $mResult; | 
| 3276:  |     } | 
| 3277:  |  | 
| 3278:  |      | 
| 3279:  |  | 
| 3280:  |  | 
| 3281:  |  | 
| 3282:  |  | 
| 3283:  |  | 
| 3284:  |  | 
| 3285:  |  | 
| 3286:  |  | 
| 3287:  |  | 
| 3288:  |  | 
| 3289:  |  | 
| 3290:  |  | 
| 3291:  |  | 
| 3292:  |  | 
| 3293:  |  | 
| 3294:  |  | 
| 3295:  |  | 
| 3296:  |  | 
| 3297:  |  | 
| 3298:  |  | 
| 3299:  |  | 
| 3300:  |  | 
| 3301:  |  | 
| 3302:  |  | 
| 3303:  |  | 
| 3304:  |  | 
| 3305:  |  | 
| 3306:  |  | 
| 3307:  |  | 
| 3308:  |  | 
| 3309:  |  | 
| 3310:  |  | 
| 3311:  |  | 
| 3312:  |  | 
| 3313:  |  | 
| 3314:  |  | 
| 3315:  |  | 
| 3316:  |  | 
| 3317:  |  | 
| 3318:  |  | 
| 3319:  |  | 
| 3320:  |  | 
| 3321:  |  | 
| 3322:  |  | 
| 3323:  |  | 
| 3324:  |  | 
| 3325:  |  | 
| 3326:  |  | 
| 3327:  |  | 
| 3328:  |      | 
| 3329:  |  | 
| 3330:  |  | 
| 3331:  |  | 
| 3332:  |  | 
| 3333:  |  | 
| 3334:  |  | 
| 3335:  |  | 
| 3336:  |     public function SetMessagesSeen($AccountID, $Folder, $Uids, $SetAction) | 
| 3337:  |     { | 
| 3338:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 3339:  |  | 
| 3340:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3341:  |  | 
| 3342:  |         self::checkAccess($oAccount); | 
| 3343:  |  | 
| 3344:  |         return $this->setMessageFlag($AccountID, $Folder, $Uids, $SetAction, \MailSo\Imap\Enumerations\MessageFlag::SEEN); | 
| 3345:  |     } | 
| 3346:  |  | 
| 3347:  |      | 
| 3348:  |  | 
| 3349:  |  | 
| 3350:  |  | 
| 3351:  |  | 
| 3352:  |  | 
| 3353:  |  | 
| 3354:  |  | 
| 3355:  |  | 
| 3356:  |  | 
| 3357:  |  | 
| 3358:  |  | 
| 3359:  |  | 
| 3360:  |  | 
| 3361:  |  | 
| 3362:  |  | 
| 3363:  |  | 
| 3364:  |  | 
| 3365:  |  | 
| 3366:  |  | 
| 3367:  |  | 
| 3368:  |  | 
| 3369:  |  | 
| 3370:  |  | 
| 3371:  |  | 
| 3372:  |  | 
| 3373:  |  | 
| 3374:  |  | 
| 3375:  |  | 
| 3376:  |  | 
| 3377:  |  | 
| 3378:  |  | 
| 3379:  |  | 
| 3380:  |  | 
| 3381:  |  | 
| 3382:  |  | 
| 3383:  |  | 
| 3384:  |  | 
| 3385:  |  | 
| 3386:  |  | 
| 3387:  |  | 
| 3388:  |  | 
| 3389:  |  | 
| 3390:  |  | 
| 3391:  |  | 
| 3392:  |  | 
| 3393:  |  | 
| 3394:  |  | 
| 3395:  |  | 
| 3396:  |  | 
| 3397:  |      | 
| 3398:  |  | 
| 3399:  |  | 
| 3400:  |  | 
| 3401:  |  | 
| 3402:  |  | 
| 3403:  |  | 
| 3404:  |  | 
| 3405:  |     public function SetMessageFlagged($AccountID, $Folder, $Uids, $SetAction) | 
| 3406:  |     { | 
| 3407:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 3408:  |  | 
| 3409:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3410:  |  | 
| 3411:  |         self::checkAccess($oAccount); | 
| 3412:  |  | 
| 3413:  |         return $this->setMessageFlag($AccountID, $Folder, $Uids, $SetAction, \MailSo\Imap\Enumerations\MessageFlag::FLAGGED); | 
| 3414:  |     } | 
| 3415:  |  | 
| 3416:  |      | 
| 3417:  |  | 
| 3418:  |  | 
| 3419:  |  | 
| 3420:  |  | 
| 3421:  |  | 
| 3422:  |  | 
| 3423:  |  | 
| 3424:  |  | 
| 3425:  |  | 
| 3426:  |  | 
| 3427:  |  | 
| 3428:  |  | 
| 3429:  |  | 
| 3430:  |  | 
| 3431:  |  | 
| 3432:  |  | 
| 3433:  |  | 
| 3434:  |  | 
| 3435:  |  | 
| 3436:  |  | 
| 3437:  |  | 
| 3438:  |  | 
| 3439:  |  | 
| 3440:  |  | 
| 3441:  |  | 
| 3442:  |  | 
| 3443:  |  | 
| 3444:  |  | 
| 3445:  |  | 
| 3446:  |  | 
| 3447:  |  | 
| 3448:  |  | 
| 3449:  |  | 
| 3450:  |  | 
| 3451:  |  | 
| 3452:  |  | 
| 3453:  |  | 
| 3454:  |  | 
| 3455:  |  | 
| 3456:  |  | 
| 3457:  |  | 
| 3458:  |  | 
| 3459:  |  | 
| 3460:  |  | 
| 3461:  |  | 
| 3462:  |  | 
| 3463:  |  | 
| 3464:  |      | 
| 3465:  |  | 
| 3466:  |  | 
| 3467:  |  | 
| 3468:  |  | 
| 3469:  |  | 
| 3470:  |  | 
| 3471:  |     public function SetAllMessagesSeen($AccountID, $Folder) | 
| 3472:  |     { | 
| 3473:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 3474:  |  | 
| 3475:  |         if (0 === \strlen(\trim($Folder))) { | 
| 3476:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 3477:  |         } | 
| 3478:  |  | 
| 3479:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3480:  |  | 
| 3481:  |         self::checkAccess($oAccount); | 
| 3482:  |  | 
| 3483:  |         return $this->getMailManager()->setMessageFlag( | 
| 3484:  |             $oAccount, | 
| 3485:  |             $Folder, | 
| 3486:  |             array(), | 
| 3487:  |             \MailSo\Imap\Enumerations\MessageFlag::SEEN, | 
| 3488:  |             \Aurora\Modules\Mail\Enums\MessageStoreAction::Add, | 
| 3489:  |             true | 
| 3490:  |         ); | 
| 3491:  |     } | 
| 3492:  |  | 
| 3493:  |      | 
| 3494:  |  | 
| 3495:  |  | 
| 3496:  |  | 
| 3497:  |  | 
| 3498:  |  | 
| 3499:  |  | 
| 3500:  |  | 
| 3501:  |  | 
| 3502:  |  | 
| 3503:  |  | 
| 3504:  |  | 
| 3505:  |  | 
| 3506:  |  | 
| 3507:  |  | 
| 3508:  |  | 
| 3509:  |  | 
| 3510:  |  | 
| 3511:  |  | 
| 3512:  |  | 
| 3513:  |  | 
| 3514:  |  | 
| 3515:  |  | 
| 3516:  |  | 
| 3517:  |  | 
| 3518:  |  | 
| 3519:  |  | 
| 3520:  |  | 
| 3521:  |  | 
| 3522:  |  | 
| 3523:  |  | 
| 3524:  |  | 
| 3525:  |  | 
| 3526:  |  | 
| 3527:  |  | 
| 3528:  |  | 
| 3529:  |  | 
| 3530:  |  | 
| 3531:  |  | 
| 3532:  |  | 
| 3533:  |  | 
| 3534:  |  | 
| 3535:  |  | 
| 3536:  |  | 
| 3537:  |  | 
| 3538:  |  | 
| 3539:  |  | 
| 3540:  |  | 
| 3541:  |  | 
| 3542:  |      | 
| 3543:  |  | 
| 3544:  |  | 
| 3545:  |  | 
| 3546:  |  | 
| 3547:  |  | 
| 3548:  |  | 
| 3549:  |  | 
| 3550:  |  | 
| 3551:  |     public function CopyMessages($AccountID, $Folder, $ToFolder, $Uids) | 
| 3552:  |     { | 
| 3553:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 3554:  |  | 
| 3555:  |         $aUids = \Aurora\System\Utils::ExplodeIntUids((string) $Uids); | 
| 3556:  |  | 
| 3557:  |         if (0 === \strlen(\trim($Folder)) || 0 === \strlen(\trim($ToFolder)) || !\is_array($aUids) || 0 === \count($aUids)) { | 
| 3558:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 3559:  |         } | 
| 3560:  |  | 
| 3561:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3562:  |  | 
| 3563:  |         self::checkAccess($oAccount); | 
| 3564:  |  | 
| 3565:  |         try { | 
| 3566:  |             $this->getMailManager()->copyMessage($oAccount, $Folder, $ToFolder, $aUids); | 
| 3567:  |         } catch (\MailSo\Imap\Exceptions\NegativeResponseException $oException) { | 
| 3568:  |             $oResponse =  $oException->GetLastResponse(); | 
| 3569:  |             throw new \Aurora\Modules\Mail\Exceptions\Exception( | 
| 3570:  |                 Enums\ErrorCodes::CannotMoveMessageQuota, | 
| 3571:  |                 $oException, | 
| 3572:  |                 $oResponse instanceof \MailSo\Imap\Response ? $oResponse->Tag.' '.$oResponse->StatusOrIndex.' '.$oResponse->HumanReadable : '' | 
| 3573:  |             ); | 
| 3574:  |         } catch (\Exception $oException) { | 
| 3575:  |             throw new \Aurora\Modules\Mail\Exceptions\Exception( | 
| 3576:  |                 Enums\ErrorCodes::CannotMoveMessage, | 
| 3577:  |                 $oException, | 
| 3578:  |                 $oException->getMessage() | 
| 3579:  |             ); | 
| 3580:  |         } | 
| 3581:  |  | 
| 3582:  |         return true; | 
| 3583:  |     } | 
| 3584:  |  | 
| 3585:  |      | 
| 3586:  |  | 
| 3587:  |  | 
| 3588:  |  | 
| 3589:  |  | 
| 3590:  |  | 
| 3591:  |  | 
| 3592:  |  | 
| 3593:  |  | 
| 3594:  |  | 
| 3595:  |  | 
| 3596:  |  | 
| 3597:  |  | 
| 3598:  |  | 
| 3599:  |  | 
| 3600:  |  | 
| 3601:  |  | 
| 3602:  |  | 
| 3603:  |  | 
| 3604:  |  | 
| 3605:  |  | 
| 3606:  |  | 
| 3607:  |  | 
| 3608:  |  | 
| 3609:  |  | 
| 3610:  |  | 
| 3611:  |  | 
| 3612:  |  | 
| 3613:  |  | 
| 3614:  |  | 
| 3615:  |  | 
| 3616:  |  | 
| 3617:  |  | 
| 3618:  |  | 
| 3619:  |  | 
| 3620:  |  | 
| 3621:  |  | 
| 3622:  |  | 
| 3623:  |  | 
| 3624:  |  | 
| 3625:  |  | 
| 3626:  |  | 
| 3627:  |  | 
| 3628:  |  | 
| 3629:  |  | 
| 3630:  |  | 
| 3631:  |  | 
| 3632:  |  | 
| 3633:  |  | 
| 3634:  |      | 
| 3635:  |  | 
| 3636:  |  | 
| 3637:  |  | 
| 3638:  |  | 
| 3639:  |  | 
| 3640:  |  | 
| 3641:  |  | 
| 3642:  |  | 
| 3643:  |     public function MoveMessages($AccountID, $Folder, $ToFolder, $Uids) | 
| 3644:  |     { | 
| 3645:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 3646:  |  | 
| 3647:  |         $aUids = \Aurora\System\Utils::ExplodeIntUids((string) $Uids); | 
| 3648:  |  | 
| 3649:  |         if (0 === \strlen(\trim($Folder)) || 0 === \strlen(\trim($ToFolder)) || !\is_array($aUids) || 0 === \count($aUids)) { | 
| 3650:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 3651:  |         } | 
| 3652:  |  | 
| 3653:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3654:  |  | 
| 3655:  |         self::checkAccess($oAccount); | 
| 3656:  |  | 
| 3657:  |         try { | 
| 3658:  |             $this->getMailManager()->moveMessage($oAccount, $Folder, $ToFolder, $aUids); | 
| 3659:  |         } catch (\MailSo\Imap\Exceptions\NegativeResponseException $oException) { | 
| 3660:  |             $oResponse =  $oException->GetLastResponse(); | 
| 3661:  |             throw new \Aurora\Modules\Mail\Exceptions\Exception( | 
| 3662:  |                 Enums\ErrorCodes::CannotMoveMessageQuota, | 
| 3663:  |                 $oException, | 
| 3664:  |                 $oResponse instanceof \MailSo\Imap\Response ? $oResponse->Tag.' '.$oResponse->StatusOrIndex.' '.$oResponse->HumanReadable : '' | 
| 3665:  |             ); | 
| 3666:  |         } catch (\Exception $oException) { | 
| 3667:  |             throw new \Aurora\Modules\Mail\Exceptions\Exception( | 
| 3668:  |                 Enums\ErrorCodes::CannotMoveMessage, | 
| 3669:  |                 $oException, | 
| 3670:  |                 $oException->getMessage() | 
| 3671:  |             ); | 
| 3672:  |         } | 
| 3673:  |  | 
| 3674:  |         return true; | 
| 3675:  |     } | 
| 3676:  |  | 
| 3677:  |      | 
| 3678:  |  | 
| 3679:  |  | 
| 3680:  |  | 
| 3681:  |  | 
| 3682:  |  | 
| 3683:  |  | 
| 3684:  |  | 
| 3685:  |  | 
| 3686:  |  | 
| 3687:  |  | 
| 3688:  |  | 
| 3689:  |  | 
| 3690:  |  | 
| 3691:  |  | 
| 3692:  |  | 
| 3693:  |  | 
| 3694:  |  | 
| 3695:  |  | 
| 3696:  |  | 
| 3697:  |  | 
| 3698:  |  | 
| 3699:  |  | 
| 3700:  |  | 
| 3701:  |  | 
| 3702:  |  | 
| 3703:  |  | 
| 3704:  |  | 
| 3705:  |  | 
| 3706:  |  | 
| 3707:  |  | 
| 3708:  |  | 
| 3709:  |  | 
| 3710:  |  | 
| 3711:  |  | 
| 3712:  |  | 
| 3713:  |  | 
| 3714:  |  | 
| 3715:  |  | 
| 3716:  |  | 
| 3717:  |  | 
| 3718:  |  | 
| 3719:  |  | 
| 3720:  |  | 
| 3721:  |  | 
| 3722:  |  | 
| 3723:  |  | 
| 3724:  |  | 
| 3725:  |  | 
| 3726:  |      | 
| 3727:  |  | 
| 3728:  |  | 
| 3729:  |  | 
| 3730:  |  | 
| 3731:  |  | 
| 3732:  |  | 
| 3733:  |  | 
| 3734:  |     public function DeleteMessages($AccountID, $Folder, $Uids) | 
| 3735:  |     { | 
| 3736:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 3737:  |  | 
| 3738:  |         $aUids = \Aurora\System\Utils::ExplodeIntUids((string) $Uids); | 
| 3739:  |  | 
| 3740:  |         if (0 === \strlen(\trim($Folder)) || !\is_array($aUids) || 0 === \count($aUids)) { | 
| 3741:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 3742:  |         } | 
| 3743:  |  | 
| 3744:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3745:  |  | 
| 3746:  |         self::checkAccess($oAccount); | 
| 3747:  |  | 
| 3748:  |         $this->getMailManager()->deleteMessage($oAccount, $Folder, $aUids); | 
| 3749:  |  | 
| 3750:  |         return true; | 
| 3751:  |     } | 
| 3752:  |  | 
| 3753:  |      | 
| 3754:  |  | 
| 3755:  |  | 
| 3756:  |  | 
| 3757:  |  | 
| 3758:  |  | 
| 3759:  |  | 
| 3760:  |  | 
| 3761:  |  | 
| 3762:  |  | 
| 3763:  |  | 
| 3764:  |  | 
| 3765:  |  | 
| 3766:  |  | 
| 3767:  |  | 
| 3768:  |  | 
| 3769:  |  | 
| 3770:  |  | 
| 3771:  |  | 
| 3772:  |  | 
| 3773:  |  | 
| 3774:  |  | 
| 3775:  |  | 
| 3776:  |  | 
| 3777:  |  | 
| 3778:  |  | 
| 3779:  |  | 
| 3780:  |  | 
| 3781:  |  | 
| 3782:  |  | 
| 3783:  |  | 
| 3784:  |  | 
| 3785:  |  | 
| 3786:  |  | 
| 3787:  |  | 
| 3788:  |  | 
| 3789:  |  | 
| 3790:  |  | 
| 3791:  |  | 
| 3792:  |  | 
| 3793:  |  | 
| 3794:  |  | 
| 3795:  |  | 
| 3796:  |  | 
| 3797:  |  | 
| 3798:  |  | 
| 3799:  |  | 
| 3800:  |  | 
| 3801:  |  | 
| 3802:  |  | 
| 3803:  |  | 
| 3804:  |      | 
| 3805:  |  | 
| 3806:  |  | 
| 3807:  |  | 
| 3808:  |  | 
| 3809:  |  | 
| 3810:  |  | 
| 3811:  |  | 
| 3812:  |  | 
| 3813:  |     public function CreateFolder($AccountID, $FolderNameInUtf8, $FolderParentFullNameRaw, $Delimiter) | 
| 3814:  |     { | 
| 3815:  |         $bResult = true; | 
| 3816:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 3817:  |  | 
| 3818:  |         if (0 === \strlen($FolderNameInUtf8) || 1 !== \strlen($Delimiter)) { | 
| 3819:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 3820:  |         } | 
| 3821:  |  | 
| 3822:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3823:  |  | 
| 3824:  |         self::checkAccess($oAccount); | 
| 3825:  |  | 
| 3826:  |         try { | 
| 3827:  |             $this->getMailManager()->createFolder($oAccount, $FolderNameInUtf8, $Delimiter, $FolderParentFullNameRaw); | 
| 3828:  |         } catch (\MailSo\Mail\Exceptions\AlreadyExistsFolder $oException) { | 
| 3829:  |             throw new \Aurora\Modules\Mail\Exceptions\Exception( | 
| 3830:  |                 Enums\ErrorCodes::FolderAlreadyExists, | 
| 3831:  |                 $oException, | 
| 3832:  |                 $oException->getMessage() | 
| 3833:  |             ); | 
| 3834:  |         } | 
| 3835:  |  | 
| 3836:  |         $aFoldersOrderList = $this->getMailManager()->getFoldersOrder($oAccount); | 
| 3837:  |         if (\is_array($aFoldersOrderList) && 0 < \count($aFoldersOrderList)) { | 
| 3838:  |             $aFoldersOrderListNew = $aFoldersOrderList; | 
| 3839:  |  | 
| 3840:  |             $sFolderNameInUtf7Imap = \MailSo\Base\Utils::ConvertEncoding( | 
| 3841:  |                 $FolderNameInUtf8, | 
| 3842:  |                 \MailSo\Base\Enumerations\Charset::UTF_8, | 
| 3843:  |                 \MailSo\Base\Enumerations\Charset::UTF_7_IMAP | 
| 3844:  |             ); | 
| 3845:  |  | 
| 3846:  |             $sFolderFullNameRaw = (0 < \strlen($FolderParentFullNameRaw) ? $FolderParentFullNameRaw.$Delimiter : ''). | 
| 3847:  |                 $sFolderNameInUtf7Imap; | 
| 3848:  |  | 
| 3849:  |             $sFolderFullNameUtf8 = \MailSo\Base\Utils::ConvertEncoding( | 
| 3850:  |                 $sFolderFullNameRaw, | 
| 3851:  |                 \MailSo\Base\Enumerations\Charset::UTF_7_IMAP, | 
| 3852:  |                 \MailSo\Base\Enumerations\Charset::UTF_8 | 
| 3853:  |             ); | 
| 3854:  |  | 
| 3855:  |             $aFoldersOrderListNew[] = $sFolderFullNameRaw; | 
| 3856:  |  | 
| 3857:  |             $aFoldersOrderListUtf8 = \array_map(function ($sValue) { | 
| 3858:  |                 return \MailSo\Base\Utils::ConvertEncoding( | 
| 3859:  |                     $sValue, | 
| 3860:  |                     \MailSo\Base\Enumerations\Charset::UTF_7_IMAP, | 
| 3861:  |                     \MailSo\Base\Enumerations\Charset::UTF_8 | 
| 3862:  |                 ); | 
| 3863:  |             }, $aFoldersOrderListNew); | 
| 3864:  |  | 
| 3865:  |             \usort($aFoldersOrderListUtf8, 'strnatcasecmp'); | 
| 3866:  |  | 
| 3867:  |             $iKey = \array_search($sFolderFullNameUtf8, $aFoldersOrderListUtf8, true); | 
| 3868:  |             if (\is_int($iKey) && 0 < $iKey && isset($aFoldersOrderListUtf8[$iKey - 1])) { | 
| 3869:  |                 $sUpperName = $aFoldersOrderListUtf8[$iKey - 1]; | 
| 3870:  |  | 
| 3871:  |                 $iUpperKey = \array_search(\MailSo\Base\Utils::ConvertEncoding( | 
| 3872:  |                     $sUpperName, | 
| 3873:  |                     \MailSo\Base\Enumerations\Charset::UTF_8, | 
| 3874:  |                     \MailSo\Base\Enumerations\Charset::UTF_7_IMAP | 
| 3875:  |                 ), $aFoldersOrderList, true); | 
| 3876:  |  | 
| 3877:  |                 if (\is_int($iUpperKey) && isset($aFoldersOrderList[$iUpperKey])) { | 
| 3878:  |                     \Aurora\System\Api::Log('insert order index:'.$iUpperKey); | 
| 3879:  |                     \array_splice($aFoldersOrderList, $iUpperKey + 1, 0, $sFolderFullNameRaw); | 
| 3880:  |                     $this->getMailManager()->updateFoldersOrder($oAccount, $aFoldersOrderList); | 
| 3881:  |                 } | 
| 3882:  |             } | 
| 3883:  |         } | 
| 3884:  |  | 
| 3885:  |         return $bResult; | 
| 3886:  |     } | 
| 3887:  |  | 
| 3888:  |      | 
| 3889:  |  | 
| 3890:  |  | 
| 3891:  |  | 
| 3892:  |  | 
| 3893:  |  | 
| 3894:  |  | 
| 3895:  |  | 
| 3896:  |  | 
| 3897:  |  | 
| 3898:  |  | 
| 3899:  |  | 
| 3900:  |  | 
| 3901:  |  | 
| 3902:  |  | 
| 3903:  |  | 
| 3904:  |  | 
| 3905:  |  | 
| 3906:  |  | 
| 3907:  |  | 
| 3908:  |  | 
| 3909:  |  | 
| 3910:  |  | 
| 3911:  |  | 
| 3912:  |  | 
| 3913:  |  | 
| 3914:  |  | 
| 3915:  |  | 
| 3916:  |  | 
| 3917:  |  | 
| 3918:  |  | 
| 3919:  |  | 
| 3920:  |  | 
| 3921:  |  | 
| 3922:  |  | 
| 3923:  |  | 
| 3924:  |  | 
| 3925:  |  | 
| 3926:  |  | 
| 3927:  |  | 
| 3928:  |  | 
| 3929:  |  | 
| 3930:  |  | 
| 3931:  |  | 
| 3932:  |  | 
| 3933:  |  | 
| 3934:  |  | 
| 3935:  |  | 
| 3936:  |  | 
| 3937:  |  | 
| 3938:  |  | 
| 3939:  |  | 
| 3940:  |      | 
| 3941:  |  | 
| 3942:  |  | 
| 3943:  |  | 
| 3944:  |  | 
| 3945:  |  | 
| 3946:  |  | 
| 3947:  |  | 
| 3948:  |     public function RenameFolder($AccountID, $PrevFolderFullNameRaw, $NewFolderNameInUtf8, $ChangeParent, $NewParentFolder) | 
| 3949:  |     { | 
| 3950:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 3951:  |  | 
| 3952:  |         if (0 === \strlen($PrevFolderFullNameRaw) || 0 === \strlen($NewFolderNameInUtf8)) { | 
| 3953:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 3954:  |         } | 
| 3955:  |  | 
| 3956:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 3957:  |  | 
| 3958:  |         self::checkAccess($oAccount); | 
| 3959:  |  | 
| 3960:  |         $mResult = $this->getMailManager()->renameFolder($oAccount, $PrevFolderFullNameRaw, $NewFolderNameInUtf8, $ChangeParent, $NewParentFolder); | 
| 3961:  |  | 
| 3962:  |         return (0 < \strlen($mResult) ? array( | 
| 3963:  |             'FullName' => $mResult, | 
| 3964:  |             'FullNameHash' => \md5($mResult) | 
| 3965:  |         ) : false); | 
| 3966:  |     } | 
| 3967:  |  | 
| 3968:  |      | 
| 3969:  |  | 
| 3970:  |  | 
| 3971:  |  | 
| 3972:  |  | 
| 3973:  |  | 
| 3974:  |  | 
| 3975:  |  | 
| 3976:  |  | 
| 3977:  |  | 
| 3978:  |  | 
| 3979:  |  | 
| 3980:  |  | 
| 3981:  |  | 
| 3982:  |  | 
| 3983:  |  | 
| 3984:  |  | 
| 3985:  |  | 
| 3986:  |  | 
| 3987:  |  | 
| 3988:  |  | 
| 3989:  |  | 
| 3990:  |  | 
| 3991:  |  | 
| 3992:  |  | 
| 3993:  |  | 
| 3994:  |  | 
| 3995:  |  | 
| 3996:  |  | 
| 3997:  |  | 
| 3998:  |  | 
| 3999:  |  | 
| 4000:  |  | 
| 4001:  |  | 
| 4002:  |  | 
| 4003:  |  | 
| 4004:  |  | 
| 4005:  |  | 
| 4006:  |  | 
| 4007:  |  | 
| 4008:  |  | 
| 4009:  |  | 
| 4010:  |  | 
| 4011:  |  | 
| 4012:  |  | 
| 4013:  |  | 
| 4014:  |  | 
| 4015:  |  | 
| 4016:  |      | 
| 4017:  |  | 
| 4018:  |  | 
| 4019:  |  | 
| 4020:  |  | 
| 4021:  |  | 
| 4022:  |  | 
| 4023:  |     public function DeleteFolder($AccountID, $Folder) | 
| 4024:  |     { | 
| 4025:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4026:  |  | 
| 4027:  |         if (0 === \strlen(\trim($Folder))) { | 
| 4028:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 4029:  |         } | 
| 4030:  |  | 
| 4031:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4032:  |  | 
| 4033:  |         self::checkAccess($oAccount); | 
| 4034:  |  | 
| 4035:  |         $this->getMailManager()->deleteFolder($oAccount, $Folder); | 
| 4036:  |  | 
| 4037:  |         return true; | 
| 4038:  |     } | 
| 4039:  |  | 
| 4040:  |      | 
| 4041:  |  | 
| 4042:  |  | 
| 4043:  |  | 
| 4044:  |  | 
| 4045:  |  | 
| 4046:  |  | 
| 4047:  |  | 
| 4048:  |  | 
| 4049:  |  | 
| 4050:  |  | 
| 4051:  |  | 
| 4052:  |  | 
| 4053:  |  | 
| 4054:  |  | 
| 4055:  |  | 
| 4056:  |  | 
| 4057:  |  | 
| 4058:  |  | 
| 4059:  |  | 
| 4060:  |  | 
| 4061:  |  | 
| 4062:  |  | 
| 4063:  |  | 
| 4064:  |  | 
| 4065:  |  | 
| 4066:  |  | 
| 4067:  |  | 
| 4068:  |  | 
| 4069:  |  | 
| 4070:  |  | 
| 4071:  |  | 
| 4072:  |  | 
| 4073:  |  | 
| 4074:  |  | 
| 4075:  |  | 
| 4076:  |  | 
| 4077:  |  | 
| 4078:  |  | 
| 4079:  |  | 
| 4080:  |  | 
| 4081:  |  | 
| 4082:  |  | 
| 4083:  |  | 
| 4084:  |  | 
| 4085:  |  | 
| 4086:  |  | 
| 4087:  |  | 
| 4088:  |  | 
| 4089:  |      | 
| 4090:  |  | 
| 4091:  |  | 
| 4092:  |  | 
| 4093:  |  | 
| 4094:  |  | 
| 4095:  |  | 
| 4096:  |  | 
| 4097:  |     public function SubscribeFolder($AccountID, $Folder, $SetAction) | 
| 4098:  |     { | 
| 4099:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4100:  |  | 
| 4101:  |         if ($this->getConfig('IgnoreImapSubscription', false)) { | 
| 4102:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied); | 
| 4103:  |         } | 
| 4104:  |  | 
| 4105:  |         if (0 === \strlen(\trim($Folder))) { | 
| 4106:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 4107:  |         } | 
| 4108:  |  | 
| 4109:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4110:  |  | 
| 4111:  |         self::checkAccess($oAccount); | 
| 4112:  |  | 
| 4113:  |         $this->getMailManager()->subscribeFolder($oAccount, $Folder, $SetAction); | 
| 4114:  |  | 
| 4115:  |         return true; | 
| 4116:  |     } | 
| 4117:  |  | 
| 4118:  |      | 
| 4119:  |  | 
| 4120:  |  | 
| 4121:  |  | 
| 4122:  |  | 
| 4123:  |  | 
| 4124:  |  | 
| 4125:  |  | 
| 4126:  |  | 
| 4127:  |  | 
| 4128:  |  | 
| 4129:  |  | 
| 4130:  |  | 
| 4131:  |  | 
| 4132:  |  | 
| 4133:  |  | 
| 4134:  |  | 
| 4135:  |  | 
| 4136:  |  | 
| 4137:  |  | 
| 4138:  |  | 
| 4139:  |  | 
| 4140:  |  | 
| 4141:  |  | 
| 4142:  |  | 
| 4143:  |  | 
| 4144:  |  | 
| 4145:  |  | 
| 4146:  |  | 
| 4147:  |  | 
| 4148:  |  | 
| 4149:  |  | 
| 4150:  |  | 
| 4151:  |  | 
| 4152:  |  | 
| 4153:  |  | 
| 4154:  |  | 
| 4155:  |  | 
| 4156:  |  | 
| 4157:  |  | 
| 4158:  |  | 
| 4159:  |  | 
| 4160:  |  | 
| 4161:  |  | 
| 4162:  |  | 
| 4163:  |  | 
| 4164:  |  | 
| 4165:  |  | 
| 4166:  |      | 
| 4167:  |  | 
| 4168:  |  | 
| 4169:  |  | 
| 4170:  |  | 
| 4171:  |  | 
| 4172:  |  | 
| 4173:  |     public function UpdateFoldersOrder($AccountID, $FolderList) | 
| 4174:  |     { | 
| 4175:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4176:  |  | 
| 4177:  |         if (!\is_array($FolderList)) { | 
| 4178:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 4179:  |         } | 
| 4180:  |  | 
| 4181:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4182:  |  | 
| 4183:  |         self::checkAccess($oAccount); | 
| 4184:  |  | 
| 4185:  |         return $this->getMailManager()->updateFoldersOrder($oAccount, $FolderList); | 
| 4186:  |     } | 
| 4187:  |  | 
| 4188:  |      | 
| 4189:  |  | 
| 4190:  |  | 
| 4191:  |  | 
| 4192:  |  | 
| 4193:  |  | 
| 4194:  |  | 
| 4195:  |  | 
| 4196:  |  | 
| 4197:  |  | 
| 4198:  |  | 
| 4199:  |  | 
| 4200:  |  | 
| 4201:  |  | 
| 4202:  |  | 
| 4203:  |  | 
| 4204:  |  | 
| 4205:  |  | 
| 4206:  |  | 
| 4207:  |  | 
| 4208:  |  | 
| 4209:  |  | 
| 4210:  |  | 
| 4211:  |  | 
| 4212:  |  | 
| 4213:  |  | 
| 4214:  |  | 
| 4215:  |  | 
| 4216:  |  | 
| 4217:  |  | 
| 4218:  |  | 
| 4219:  |  | 
| 4220:  |  | 
| 4221:  |  | 
| 4222:  |  | 
| 4223:  |  | 
| 4224:  |  | 
| 4225:  |  | 
| 4226:  |  | 
| 4227:  |  | 
| 4228:  |  | 
| 4229:  |  | 
| 4230:  |  | 
| 4231:  |  | 
| 4232:  |  | 
| 4233:  |  | 
| 4234:  |  | 
| 4235:  |      | 
| 4236:  |  | 
| 4237:  |  | 
| 4238:  |  | 
| 4239:  |  | 
| 4240:  |  | 
| 4241:  |  | 
| 4242:  |     public function ClearFolder($AccountID, $Folder) | 
| 4243:  |     { | 
| 4244:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4245:  |  | 
| 4246:  |         if (0 === \strlen(\trim($Folder))) { | 
| 4247:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 4248:  |         } | 
| 4249:  |  | 
| 4250:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4251:  |  | 
| 4252:  |         self::checkAccess($oAccount); | 
| 4253:  |  | 
| 4254:  |         $this->getMailManager()->clearFolder($oAccount, $Folder); | 
| 4255:  |  | 
| 4256:  |         return true; | 
| 4257:  |     } | 
| 4258:  |  | 
| 4259:  |      | 
| 4260:  |  | 
| 4261:  |  | 
| 4262:  |  | 
| 4263:  |  | 
| 4264:  |  | 
| 4265:  |  | 
| 4266:  |  | 
| 4267:  |  | 
| 4268:  |  | 
| 4269:  |  | 
| 4270:  |  | 
| 4271:  |  | 
| 4272:  |  | 
| 4273:  |  | 
| 4274:  |  | 
| 4275:  |  | 
| 4276:  |  | 
| 4277:  |  | 
| 4278:  |  | 
| 4279:  |  | 
| 4280:  |  | 
| 4281:  |  | 
| 4282:  |  | 
| 4283:  |  | 
| 4284:  |  | 
| 4285:  |  | 
| 4286:  |  | 
| 4287:  |  | 
| 4288:  |  | 
| 4289:  |  | 
| 4290:  |  | 
| 4291:  |  | 
| 4292:  |  | 
| 4293:  |  | 
| 4294:  |  | 
| 4295:  |  | 
| 4296:  |  | 
| 4297:  |  | 
| 4298:  |  | 
| 4299:  |  | 
| 4300:  |  | 
| 4301:  |  | 
| 4302:  |  | 
| 4303:  |  | 
| 4304:  |  | 
| 4305:  |  | 
| 4306:  |  | 
| 4307:  |  | 
| 4308:  |  | 
| 4309:  |  | 
| 4310:  |  | 
| 4311:  |  | 
| 4312:  |  | 
| 4313:  |  | 
| 4314:  |  | 
| 4315:  |  | 
| 4316:  |  | 
| 4317:  |  | 
| 4318:  |  | 
| 4319:  |  | 
| 4320:  |  | 
| 4321:  |  | 
| 4322:  |  | 
| 4323:  |  | 
| 4324:  |  | 
| 4325:  |  | 
| 4326:  |  | 
| 4327:  |  | 
| 4328:  |  | 
| 4329:  |  | 
| 4330:  |  | 
| 4331:  |  | 
| 4332:  |  | 
| 4333:  |  | 
| 4334:  |  | 
| 4335:  |  | 
| 4336:  |  | 
| 4337:  |  | 
| 4338:  |  | 
| 4339:  |  | 
| 4340:  |  | 
| 4341:  |  | 
| 4342:  |  | 
| 4343:  |  | 
| 4344:  |  | 
| 4345:  |  | 
| 4346:  |  | 
| 4347:  |  | 
| 4348:  |  | 
| 4349:  |  | 
| 4350:  |  | 
| 4351:  |  | 
| 4352:  |  | 
| 4353:  |  | 
| 4354:  |  | 
| 4355:  |  | 
| 4356:  |  | 
| 4357:  |  | 
| 4358:  |  | 
| 4359:  |  | 
| 4360:  |  | 
| 4361:  |  | 
| 4362:  |  | 
| 4363:  |  | 
| 4364:  |  | 
| 4365:  |  | 
| 4366:  |      | 
| 4367:  |  | 
| 4368:  |  | 
| 4369:  |  | 
| 4370:  |  | 
| 4371:  |  | 
| 4372:  |  | 
| 4373:  |  | 
| 4374:  |     public function GetMessagesByUids($AccountID, $Folder, $Uids) | 
| 4375:  |     { | 
| 4376:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4377:  |  | 
| 4378:  |         if (0 === \strlen(trim($Folder)) || !\is_array($Uids)) { | 
| 4379:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 4380:  |         } | 
| 4381:  |  | 
| 4382:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4383:  |  | 
| 4384:  |         self::checkAccess($oAccount); | 
| 4385:  |  | 
| 4386:  |         return $this->getMailManager()->getMessageListByUids($oAccount, $Folder, $Uids); | 
| 4387:  |     } | 
| 4388:  |  | 
| 4389:  |      | 
| 4390:  |  | 
| 4391:  |  | 
| 4392:  |  | 
| 4393:  |  | 
| 4394:  |  | 
| 4395:  |  | 
| 4396:  |  | 
| 4397:  |  | 
| 4398:  |  | 
| 4399:  |  | 
| 4400:  |  | 
| 4401:  |  | 
| 4402:  |  | 
| 4403:  |  | 
| 4404:  |  | 
| 4405:  |  | 
| 4406:  |  | 
| 4407:  |  | 
| 4408:  |  | 
| 4409:  |  | 
| 4410:  |  | 
| 4411:  |  | 
| 4412:  |  | 
| 4413:  |  | 
| 4414:  |  | 
| 4415:  |  | 
| 4416:  |  | 
| 4417:  |  | 
| 4418:  |  | 
| 4419:  |  | 
| 4420:  |  | 
| 4421:  |  | 
| 4422:  |  | 
| 4423:  |  | 
| 4424:  |  | 
| 4425:  |  | 
| 4426:  |  | 
| 4427:  |  | 
| 4428:  |  | 
| 4429:  |  | 
| 4430:  |  | 
| 4431:  |  | 
| 4432:  |  | 
| 4433:  |  | 
| 4434:  |  | 
| 4435:  |  | 
| 4436:  |  | 
| 4437:  |      | 
| 4438:  |  | 
| 4439:  |  | 
| 4440:  |  | 
| 4441:  |  | 
| 4442:  |  | 
| 4443:  |  | 
| 4444:  |  | 
| 4445:  |     public function GetMessagesFlags($AccountID, $Folder, $Uids) | 
| 4446:  |     { | 
| 4447:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4448:  |  | 
| 4449:  |         if (0 === \strlen(\trim($Folder)) || !\is_array($Uids)) { | 
| 4450:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 4451:  |         } | 
| 4452:  |  | 
| 4453:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4454:  |  | 
| 4455:  |         self::checkAccess($oAccount); | 
| 4456:  |  | 
| 4457:  |         return $this->getMailManager()->getMessagesFlags($oAccount, $Folder, $Uids); | 
| 4458:  |     } | 
| 4459:  |  | 
| 4460:  |      | 
| 4461:  |  | 
| 4462:  |  | 
| 4463:  |  | 
| 4464:  |  | 
| 4465:  |  | 
| 4466:  |  | 
| 4467:  |  | 
| 4468:  |  | 
| 4469:  |  | 
| 4470:  |  | 
| 4471:  |  | 
| 4472:  |  | 
| 4473:  |  | 
| 4474:  |  | 
| 4475:  |  | 
| 4476:  |  | 
| 4477:  |  | 
| 4478:  |  | 
| 4479:  |  | 
| 4480:  |  | 
| 4481:  |  | 
| 4482:  |  | 
| 4483:  |  | 
| 4484:  |  | 
| 4485:  |  | 
| 4486:  |  | 
| 4487:  |  | 
| 4488:  |  | 
| 4489:  |  | 
| 4490:  |  | 
| 4491:  |  | 
| 4492:  |  | 
| 4493:  |  | 
| 4494:  |  | 
| 4495:  |  | 
| 4496:  |  | 
| 4497:  |  | 
| 4498:  |  | 
| 4499:  |  | 
| 4500:  |  | 
| 4501:  |  | 
| 4502:  |  | 
| 4503:  |  | 
| 4504:  |  | 
| 4505:  |  | 
| 4506:  |  | 
| 4507:  |  | 
| 4508:  |  | 
| 4509:  |  | 
| 4510:  |  | 
| 4511:  |  | 
| 4512:  |  | 
| 4513:  |  | 
| 4514:  |  | 
| 4515:  |  | 
| 4516:  |  | 
| 4517:  |  | 
| 4518:  |  | 
| 4519:  |  | 
| 4520:  |  | 
| 4521:  |  | 
| 4522:  |  | 
| 4523:  |  | 
| 4524:  |  | 
| 4525:  |  | 
| 4526:  |  | 
| 4527:  |  | 
| 4528:  |      | 
| 4529:  |  | 
| 4530:  |  | 
| 4531:  |  | 
| 4532:  |  | 
| 4533:  |  | 
| 4534:  |  | 
| 4535:  |  | 
| 4536:  |  | 
| 4537:  |  | 
| 4538:  |  | 
| 4539:  |  | 
| 4540:  |  | 
| 4541:  |  | 
| 4542:  |  | 
| 4543:  |  | 
| 4544:  |  | 
| 4545:  |  | 
| 4546:  |  | 
| 4547:  |  | 
| 4548:  |  | 
| 4549:  |  | 
| 4550:  |  | 
| 4551:  |  | 
| 4552:  |     public function SaveMessage( | 
| 4553:  |         $AccountID, | 
| 4554:  |         $Fetcher = null, | 
| 4555:  |         $Alias = null, | 
| 4556:  |         $IdentityID = 0, | 
| 4557:  |         $DraftInfo = [], | 
| 4558:  |         $DraftUid = "", | 
| 4559:  |         $To = "", | 
| 4560:  |         $Cc = "", | 
| 4561:  |         $Bcc = "", | 
| 4562:  |         $Subject = "", | 
| 4563:  |         $Text = "", | 
| 4564:  |         $IsHtml = false, | 
| 4565:  |         $Importance = \MailSo\Mime\Enumerations\MessagePriority::NORMAL, | 
| 4566:  |         $SendReadingConfirmation = false, | 
| 4567:  |         $Attachments = array(), | 
| 4568:  |         $InReplyTo = "", | 
| 4569:  |         $References = "", | 
| 4570:  |         $Sensitivity = \MailSo\Mime\Enumerations\Sensitivity::NOTHING, | 
| 4571:  |         $DraftFolder = "" | 
| 4572:  |     ) { | 
| 4573:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4574:  |  | 
| 4575:  |         $mResult = false; | 
| 4576:  |  | 
| 4577:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4578:  |  | 
| 4579:  |         self::checkAccess($oAccount); | 
| 4580:  |  | 
| 4581:  |         if (0 === \strlen($DraftFolder)) { | 
| 4582:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 4583:  |         } | 
| 4584:  |  | 
| 4585:  |         $oIdentity = $IdentityID !== 0 ? $this->getIdentitiesManager()->getIdentity($IdentityID) : null; | 
| 4586:  |  | 
| 4587:  |         $oMessage = self::Decorator()->BuildMessage( | 
| 4588:  |             $oAccount, | 
| 4589:  |             $To, | 
| 4590:  |             $Cc, | 
| 4591:  |             $Bcc, | 
| 4592:  |             $Subject, | 
| 4593:  |             $IsHtml, | 
| 4594:  |             $Text, | 
| 4595:  |             $Attachments, | 
| 4596:  |             $DraftInfo, | 
| 4597:  |             $InReplyTo, | 
| 4598:  |             $References, | 
| 4599:  |             $Importance, | 
| 4600:  |             $Sensitivity, | 
| 4601:  |             $SendReadingConfirmation, | 
| 4602:  |             $Fetcher, | 
| 4603:  |             $Alias, | 
| 4604:  |             true, | 
| 4605:  |             $oIdentity | 
| 4606:  |         ); | 
| 4607:  |         if ($oMessage) { | 
| 4608:  |             try { | 
| 4609:  |                 $mResult = $this->getMailManager()->saveMessage($oAccount, $oMessage, $DraftFolder, $DraftUid); | 
| 4610:  |             } catch (\Aurora\System\Exceptions\ManagerException $oException) { | 
| 4611:  |                 throw new \Aurora\Modules\Mail\Exceptions\Exception(Enums\ErrorCodes::CannotSaveMessage, $oException, $oException->getMessage()); | 
| 4612:  |             } | 
| 4613:  |         } | 
| 4614:  |  | 
| 4615:  |         return $mResult; | 
| 4616:  |     } | 
| 4617:  |  | 
| 4618:  |      | 
| 4619:  |  | 
| 4620:  |  | 
| 4621:  |  | 
| 4622:  |  | 
| 4623:  |  | 
| 4624:  |  | 
| 4625:  |  | 
| 4626:  |  | 
| 4627:  |  | 
| 4628:  |  | 
| 4629:  |  | 
| 4630:  |  | 
| 4631:  |  | 
| 4632:  |  | 
| 4633:  |  | 
| 4634:  |  | 
| 4635:  |  | 
| 4636:  |  | 
| 4637:  |  | 
| 4638:  |  | 
| 4639:  |  | 
| 4640:  |  | 
| 4641:  |  | 
| 4642:  |  | 
| 4643:  |  | 
| 4644:  |  | 
| 4645:  |  | 
| 4646:  |  | 
| 4647:  |  | 
| 4648:  |  | 
| 4649:  |  | 
| 4650:  |  | 
| 4651:  |  | 
| 4652:  |  | 
| 4653:  |  | 
| 4654:  |  | 
| 4655:  |  | 
| 4656:  |  | 
| 4657:  |  | 
| 4658:  |  | 
| 4659:  |  | 
| 4660:  |  | 
| 4661:  |  | 
| 4662:  |  | 
| 4663:  |  | 
| 4664:  |  | 
| 4665:  |  | 
| 4666:  |  | 
| 4667:  |  | 
| 4668:  |  | 
| 4669:  |  | 
| 4670:  |  | 
| 4671:  |  | 
| 4672:  |  | 
| 4673:  |  | 
| 4674:  |  | 
| 4675:  |  | 
| 4676:  |  | 
| 4677:  |  | 
| 4678:  |  | 
| 4679:  |  | 
| 4680:  |  | 
| 4681:  |  | 
| 4682:  |  | 
| 4683:  |  | 
| 4684:  |  | 
| 4685:  |  | 
| 4686:  |  | 
| 4687:  |  | 
| 4688:  |  | 
| 4689:  |      | 
| 4690:  |  | 
| 4691:  |  | 
| 4692:  |  | 
| 4693:  |  | 
| 4694:  |  | 
| 4695:  |  | 
| 4696:  |  | 
| 4697:  |  | 
| 4698:  |  | 
| 4699:  |  | 
| 4700:  |  | 
| 4701:  |  | 
| 4702:  |  | 
| 4703:  |  | 
| 4704:  |  | 
| 4705:  |  | 
| 4706:  |  | 
| 4707:  |  | 
| 4708:  |  | 
| 4709:  |  | 
| 4710:  |  | 
| 4711:  |  | 
| 4712:  |  | 
| 4713:  |  | 
| 4714:  |  | 
| 4715:  |  | 
| 4716:  |  | 
| 4717:  |  | 
| 4718:  |     public function SendMessage( | 
| 4719:  |         $AccountID, | 
| 4720:  |         $Fetcher = null, | 
| 4721:  |         $Alias = null, | 
| 4722:  |         $IdentityID = 0, | 
| 4723:  |         $DraftInfo = [], | 
| 4724:  |         $DraftUid = "", | 
| 4725:  |         $To = "", | 
| 4726:  |         $Cc = "", | 
| 4727:  |         $Bcc = "", | 
| 4728:  |         $Recipients = array(), | 
| 4729:  |         $Subject = "", | 
| 4730:  |         $Text = "", | 
| 4731:  |         $IsHtml = false, | 
| 4732:  |         $Importance = \MailSo\Mime\Enumerations\MessagePriority::NORMAL, | 
| 4733:  |         $SendReadingConfirmation = false, | 
| 4734:  |         $Attachments = array(), | 
| 4735:  |         $InReplyTo = "", | 
| 4736:  |         $References = "", | 
| 4737:  |         $Sensitivity = \MailSo\Mime\Enumerations\Sensitivity::NOTHING, | 
| 4738:  |         $SentFolder = "", | 
| 4739:  |         $DraftFolder = "", | 
| 4740:  |         $ConfirmFolder = "", | 
| 4741:  |         $ConfirmUid = "", | 
| 4742:  |         $CustomHeaders = [] | 
| 4743:  |     ) { | 
| 4744:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4745:  |  | 
| 4746:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4747:  |  | 
| 4748:  |         self::checkAccess($oAccount); | 
| 4749:  |  | 
| 4750:  |         $oIdentity = $IdentityID !== 0 ? $this->getIdentitiesManager()->getIdentity($IdentityID) : null; | 
| 4751:  |  | 
| 4752:  |         $oMessage = self::Decorator()->BuildMessage( | 
| 4753:  |             $oAccount, | 
| 4754:  |             $To, | 
| 4755:  |             $Cc, | 
| 4756:  |             $Bcc, | 
| 4757:  |             $Subject, | 
| 4758:  |             $IsHtml, | 
| 4759:  |             $Text, | 
| 4760:  |             $Attachments, | 
| 4761:  |             $DraftInfo, | 
| 4762:  |             $InReplyTo, | 
| 4763:  |             $References, | 
| 4764:  |             $Importance, | 
| 4765:  |             $Sensitivity, | 
| 4766:  |             $SendReadingConfirmation, | 
| 4767:  |             $Fetcher, | 
| 4768:  |             $Alias, | 
| 4769:  |             false, | 
| 4770:  |             $oIdentity, | 
| 4771:  |             $CustomHeaders | 
| 4772:  |         ); | 
| 4773:  |  | 
| 4774:  |         if ($oMessage) { | 
| 4775:  |             $mResult = $this->getMailManager()->sendMessage($oAccount, $oMessage, $Fetcher, $oIdentity, $SentFolder, $DraftFolder, $DraftUid, $Recipients); | 
| 4776:  |  | 
| 4777:  |             if ($mResult) { | 
| 4778:  |                 $aCollection = $oMessage->GetRcpt(); | 
| 4779:  |  | 
| 4780:  |                 $aEmails = array(); | 
| 4781:  |                 $aCollection->ForeachList(function ($oEmail) use (&$aEmails) { | 
| 4782:  |                     $aEmails[strtolower($oEmail->GetEmail())] = trim($oEmail->GetDisplayName()); | 
| 4783:  |                 }); | 
| 4784:  |  | 
| 4785:  |                 if (\is_array($aEmails)) { | 
| 4786:  |                     $aArgs = ['IdUser' => $oAccount->IdUser, 'Emails' => $aEmails]; | 
| 4787:  |                     $this->broadcastEvent('AfterUseEmails', $aArgs); | 
| 4788:  |                 } | 
| 4789:  |             } | 
| 4790:  |  | 
| 4791:  |             if (\is_array($DraftInfo) && 3 === \count($DraftInfo)) { | 
| 4792:  |                 $sDraftInfoType = $DraftInfo[0]; | 
| 4793:  |                 $sDraftInfoUid = $DraftInfo[1]; | 
| 4794:  |                 $sDraftInfoFolder = $DraftInfo[2]; | 
| 4795:  |  | 
| 4796:  |                 try { | 
| 4797:  |                     switch (\strtolower($sDraftInfoType)) { | 
| 4798:  |                         case 'reply': | 
| 4799:  |                         case 'reply-all': | 
| 4800:  |                             $this->getMailManager()->setMessageFlag( | 
| 4801:  |                                 $oAccount, | 
| 4802:  |                                 $sDraftInfoFolder, | 
| 4803:  |                                 array($sDraftInfoUid), | 
| 4804:  |                                 \MailSo\Imap\Enumerations\MessageFlag::ANSWERED, | 
| 4805:  |                                 \Aurora\Modules\Mail\Enums\MessageStoreAction::Add | 
| 4806:  |                             ); | 
| 4807:  |                             break; | 
| 4808:  |                         case 'forward': | 
| 4809:  |                             $this->getMailManager()->setMessageFlag( | 
| 4810:  |                                 $oAccount, | 
| 4811:  |                                 $sDraftInfoFolder, | 
| 4812:  |                                 array($sDraftInfoUid), | 
| 4813:  |                                 '$Forwarded', | 
| 4814:  |                                 \Aurora\Modules\Mail\Enums\MessageStoreAction::Add | 
| 4815:  |                             ); | 
| 4816:  |                             break; | 
| 4817:  |                     } | 
| 4818:  |                 } catch (\Exception $oException) { | 
| 4819:  |                 } | 
| 4820:  |             } | 
| 4821:  |  | 
| 4822:  |             if (0 < \strlen($ConfirmFolder) && 0 < \strlen($ConfirmUid)) { | 
| 4823:  |                 try { | 
| 4824:  |                     $mResult = $this->getMailManager()->setMessageFlag( | 
| 4825:  |                         $oAccount, | 
| 4826:  |                         $ConfirmFolder, | 
| 4827:  |                         array($ConfirmUid), | 
| 4828:  |                         '$ReadConfirm', | 
| 4829:  |                         \Aurora\Modules\Mail\Enums\MessageStoreAction::Add, | 
| 4830:  |                         false, | 
| 4831:  |                         true | 
| 4832:  |                     ); | 
| 4833:  |                 } catch (\Exception $oException) { | 
| 4834:  |                 } | 
| 4835:  |             } | 
| 4836:  |         } | 
| 4837:  |  | 
| 4838:  |         \Aurora\System\Api::LogEvent('message-send: ' . $oAccount->Email, self::GetName()); | 
| 4839:  |         return $mResult; | 
| 4840:  |     } | 
| 4841:  |  | 
| 4842:  |      | 
| 4843:  |  | 
| 4844:  |  | 
| 4845:  |  | 
| 4846:  |  | 
| 4847:  |  | 
| 4848:  |  | 
| 4849:  |  | 
| 4850:  |  | 
| 4851:  |  | 
| 4852:  |  | 
| 4853:  |  | 
| 4854:  |  | 
| 4855:  |  | 
| 4856:  |  | 
| 4857:  |  | 
| 4858:  |  | 
| 4859:  |  | 
| 4860:  |  | 
| 4861:  |  | 
| 4862:  |  | 
| 4863:  |  | 
| 4864:  |  | 
| 4865:  |  | 
| 4866:  |  | 
| 4867:  |  | 
| 4868:  |  | 
| 4869:  |  | 
| 4870:  |  | 
| 4871:  |  | 
| 4872:  |  | 
| 4873:  |  | 
| 4874:  |  | 
| 4875:  |  | 
| 4876:  |  | 
| 4877:  |  | 
| 4878:  |  | 
| 4879:  |  | 
| 4880:  |  | 
| 4881:  |  | 
| 4882:  |  | 
| 4883:  |  | 
| 4884:  |  | 
| 4885:  |  | 
| 4886:  |  | 
| 4887:  |  | 
| 4888:  |  | 
| 4889:  |  | 
| 4890:  |  | 
| 4891:  |  | 
| 4892:  |  | 
| 4893:  |      | 
| 4894:  |  | 
| 4895:  |  | 
| 4896:  |  | 
| 4897:  |  | 
| 4898:  |  | 
| 4899:  |  | 
| 4900:  |  | 
| 4901:  |  | 
| 4902:  |     public function SetupSystemFolders($AccountID, $Sent, $Drafts, $Trash, $Spam) | 
| 4903:  |     { | 
| 4904:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4905:  |  | 
| 4906:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4907:  |  | 
| 4908:  |         self::checkAccess($oAccount); | 
| 4909:  |  | 
| 4910:  |         $aSystemNames = [ | 
| 4911:  |             \Aurora\Modules\Mail\Enums\FolderType::Sent => \trim($Sent), | 
| 4912:  |             \Aurora\Modules\Mail\Enums\FolderType::Drafts => \trim($Drafts), | 
| 4913:  |             \Aurora\Modules\Mail\Enums\FolderType::Trash => \trim($Trash), | 
| 4914:  |             \Aurora\Modules\Mail\Enums\FolderType::Spam => \trim($Spam) | 
| 4915:  |         ]; | 
| 4916:  |  | 
| 4917:  |         return $this->getMailManager()->updateSystemFolderNames($oAccount, $aSystemNames); | 
| 4918:  |     } | 
| 4919:  |  | 
| 4920:  |      | 
| 4921:  |  | 
| 4922:  |  | 
| 4923:  |  | 
| 4924:  |  | 
| 4925:  |  | 
| 4926:  |     public function SetAlwaysRefreshFolder($AccountID, $FolderFullName, $AlwaysRefresh) | 
| 4927:  |     { | 
| 4928:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4929:  |  | 
| 4930:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4931:  |  | 
| 4932:  |         self::checkAccess($oAccount); | 
| 4933:  |  | 
| 4934:  |         return $this->getMailManager()->setAlwaysRefreshFolder($oAccount, $FolderFullName, $AlwaysRefresh); | 
| 4935:  |     } | 
| 4936:  |  | 
| 4937:  |      | 
| 4938:  |  | 
| 4939:  |  | 
| 4940:  |  | 
| 4941:  |  | 
| 4942:  |  | 
| 4943:  |  | 
| 4944:  |     public function SetTemplateFolderType($AccountID, $FolderFullName, $SetTemplate) | 
| 4945:  |     { | 
| 4946:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 4947:  |  | 
| 4948:  |         if ($this->getConfig('AllowTemplateFolders', false)) { | 
| 4949:  |             $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 4950:  |  | 
| 4951:  |             self::checkAccess($oAccount); | 
| 4952:  |  | 
| 4953:  |             return $this->getMailManager()->setSystemFolder($oAccount, $FolderFullName, \Aurora\Modules\Mail\Enums\FolderType::Template, $SetTemplate); | 
| 4954:  |         } | 
| 4955:  |  | 
| 4956:  |         return false; | 
| 4957:  |     } | 
| 4958:  |  | 
| 4959:  |      | 
| 4960:  |  | 
| 4961:  |  | 
| 4962:  |  | 
| 4963:  |  | 
| 4964:  |  | 
| 4965:  |  | 
| 4966:  |  | 
| 4967:  |  | 
| 4968:  |  | 
| 4969:  |  | 
| 4970:  |  | 
| 4971:  |  | 
| 4972:  |  | 
| 4973:  |  | 
| 4974:  |  | 
| 4975:  |  | 
| 4976:  |  | 
| 4977:  |  | 
| 4978:  |  | 
| 4979:  |  | 
| 4980:  |  | 
| 4981:  |  | 
| 4982:  |  | 
| 4983:  |  | 
| 4984:  |  | 
| 4985:  |  | 
| 4986:  |  | 
| 4987:  |  | 
| 4988:  |  | 
| 4989:  |  | 
| 4990:  |  | 
| 4991:  |  | 
| 4992:  |  | 
| 4993:  |  | 
| 4994:  |  | 
| 4995:  |  | 
| 4996:  |  | 
| 4997:  |  | 
| 4998:  |  | 
| 4999:  |  | 
| 5000:  |  | 
| 5001:  |  | 
| 5002:  |  | 
| 5003:  |  | 
| 5004:  |  | 
| 5005:  |  | 
| 5006:  |  | 
| 5007:  |      | 
| 5008:  |  | 
| 5009:  |  | 
| 5010:  |  | 
| 5011:  |  | 
| 5012:  |  | 
| 5013:  |  | 
| 5014:  |     public function SetEmailSafety($AccountID, $Email) | 
| 5015:  |     { | 
| 5016:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 5017:  |  | 
| 5018:  |         if (0 === \strlen(\trim($Email))) { | 
| 5019:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 5020:  |         } | 
| 5021:  |  | 
| 5022:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 5023:  |  | 
| 5024:  |         self::checkAccess($oAccount); | 
| 5025:  |  | 
| 5026:  |         $this->getMailManager()->setSafetySender($oAccount->IdUser, $Email); | 
| 5027:  |  | 
| 5028:  |         return true; | 
| 5029:  |     } | 
| 5030:  |  | 
| 5031:  |      | 
| 5032:  |  | 
| 5033:  |  | 
| 5034:  |  | 
| 5035:  |  | 
| 5036:  |  | 
| 5037:  |  | 
| 5038:  |  | 
| 5039:  |  | 
| 5040:  |  | 
| 5041:  |  | 
| 5042:  |  | 
| 5043:  |  | 
| 5044:  |  | 
| 5045:  |  | 
| 5046:  |  | 
| 5047:  |  | 
| 5048:  |  | 
| 5049:  |  | 
| 5050:  |  | 
| 5051:  |  | 
| 5052:  |  | 
| 5053:  |  | 
| 5054:  |  | 
| 5055:  |  | 
| 5056:  |  | 
| 5057:  |  | 
| 5058:  |  | 
| 5059:  |  | 
| 5060:  |  | 
| 5061:  |  | 
| 5062:  |  | 
| 5063:  |  | 
| 5064:  |  | 
| 5065:  |  | 
| 5066:  |  | 
| 5067:  |  | 
| 5068:  |  | 
| 5069:  |  | 
| 5070:  |  | 
| 5071:  |  | 
| 5072:  |  | 
| 5073:  |  | 
| 5074:  |  | 
| 5075:  |  | 
| 5076:  |  | 
| 5077:  |  | 
| 5078:  |  | 
| 5079:  |  | 
| 5080:  |  | 
| 5081:  |      | 
| 5082:  |  | 
| 5083:  |  | 
| 5084:  |  | 
| 5085:  |  | 
| 5086:  |  | 
| 5087:  |  | 
| 5088:  |  | 
| 5089:  |     public function CreateIdentity($UserId, $AccountID, $FriendlyName, $Email) | 
| 5090:  |     { | 
| 5091:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 5092:  |  | 
| 5093:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 5094:  |  | 
| 5095:  |         self::checkAccess($oAccount); | 
| 5096:  |  | 
| 5097:  |         return $this->getIdentitiesManager()->createIdentity($UserId, $AccountID, $FriendlyName, $Email); | 
| 5098:  |     } | 
| 5099:  |  | 
| 5100:  |      | 
| 5101:  |  | 
| 5102:  |  | 
| 5103:  |  | 
| 5104:  |  | 
| 5105:  |  | 
| 5106:  |  | 
| 5107:  |  | 
| 5108:  |  | 
| 5109:  |  | 
| 5110:  |  | 
| 5111:  |  | 
| 5112:  |  | 
| 5113:  |  | 
| 5114:  |  | 
| 5115:  |  | 
| 5116:  |  | 
| 5117:  |  | 
| 5118:  |  | 
| 5119:  |  | 
| 5120:  |  | 
| 5121:  |  | 
| 5122:  |  | 
| 5123:  |  | 
| 5124:  |  | 
| 5125:  |  | 
| 5126:  |  | 
| 5127:  |  | 
| 5128:  |  | 
| 5129:  |  | 
| 5130:  |  | 
| 5131:  |  | 
| 5132:  |  | 
| 5133:  |  | 
| 5134:  |  | 
| 5135:  |  | 
| 5136:  |  | 
| 5137:  |  | 
| 5138:  |  | 
| 5139:  |  | 
| 5140:  |  | 
| 5141:  |  | 
| 5142:  |  | 
| 5143:  |  | 
| 5144:  |  | 
| 5145:  |  | 
| 5146:  |  | 
| 5147:  |  | 
| 5148:  |  | 
| 5149:  |  | 
| 5150:  |  | 
| 5151:  |  | 
| 5152:  |  | 
| 5153:  |  | 
| 5154:  |      | 
| 5155:  |  | 
| 5156:  |  | 
| 5157:  |  | 
| 5158:  |  | 
| 5159:  |  | 
| 5160:  |  | 
| 5161:  |  | 
| 5162:  |  | 
| 5163:  |  | 
| 5164:  |  | 
| 5165:  |     public function UpdateIdentity($UserId, $AccountID, $EntityId, $FriendlyName, $Email, $Default = false, $AccountPart = false) | 
| 5166:  |     { | 
| 5167:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 5168:  |  | 
| 5169:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 5170:  |  | 
| 5171:  |         self::checkAccess($oAccount); | 
| 5172:  |  | 
| 5173:  |         if ($Default) { | 
| 5174:  |             $this->getIdentitiesManager()->resetDefaultIdentity($UserId, $AccountID); | 
| 5175:  |         } | 
| 5176:  |  | 
| 5177:  |         if ($AccountPart) { | 
| 5178:  |             return $this->UpdateAccount($AccountID, null, $Email, $FriendlyName); | 
| 5179:  |         } else { | 
| 5180:  |             return $this->getIdentitiesManager()->updateIdentity($EntityId, $FriendlyName, $Email, $Default); | 
| 5181:  |         } | 
| 5182:  |     } | 
| 5183:  |  | 
| 5184:  |      | 
| 5185:  |  | 
| 5186:  |  | 
| 5187:  |  | 
| 5188:  |  | 
| 5189:  |  | 
| 5190:  |  | 
| 5191:  |  | 
| 5192:  |  | 
| 5193:  |  | 
| 5194:  |  | 
| 5195:  |  | 
| 5196:  |  | 
| 5197:  |  | 
| 5198:  |  | 
| 5199:  |  | 
| 5200:  |  | 
| 5201:  |  | 
| 5202:  |  | 
| 5203:  |  | 
| 5204:  |  | 
| 5205:  |  | 
| 5206:  |  | 
| 5207:  |  | 
| 5208:  |  | 
| 5209:  |  | 
| 5210:  |  | 
| 5211:  |  | 
| 5212:  |  | 
| 5213:  |  | 
| 5214:  |  | 
| 5215:  |  | 
| 5216:  |  | 
| 5217:  |  | 
| 5218:  |  | 
| 5219:  |  | 
| 5220:  |  | 
| 5221:  |  | 
| 5222:  |  | 
| 5223:  |  | 
| 5224:  |  | 
| 5225:  |  | 
| 5226:  |  | 
| 5227:  |  | 
| 5228:  |  | 
| 5229:  |  | 
| 5230:  |  | 
| 5231:  |      | 
| 5232:  |  | 
| 5233:  |  | 
| 5234:  |  | 
| 5235:  |  | 
| 5236:  |     public function DeleteIdentity($AccountID, $EntityId) | 
| 5237:  |     { | 
| 5238:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 5239:  |  | 
| 5240:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 5241:  |  | 
| 5242:  |         self::checkAccess($oAccount); | 
| 5243:  |  | 
| 5244:  |         return $this->getIdentitiesManager()->deleteIdentity($EntityId); | 
| 5245:  |     } | 
| 5246:  |  | 
| 5247:  |      | 
| 5248:  |  | 
| 5249:  |  | 
| 5250:  |  | 
| 5251:  |  | 
| 5252:  |  | 
| 5253:  |  | 
| 5254:  |  | 
| 5255:  |  | 
| 5256:  |  | 
| 5257:  |  | 
| 5258:  |  | 
| 5259:  |  | 
| 5260:  |  | 
| 5261:  |  | 
| 5262:  |  | 
| 5263:  |  | 
| 5264:  |  | 
| 5265:  |  | 
| 5266:  |  | 
| 5267:  |  | 
| 5268:  |  | 
| 5269:  |  | 
| 5270:  |  | 
| 5271:  |  | 
| 5272:  |  | 
| 5273:  |  | 
| 5274:  |  | 
| 5275:  |  | 
| 5276:  |  | 
| 5277:  |  | 
| 5278:  |  | 
| 5279:  |  | 
| 5280:  |  | 
| 5281:  |  | 
| 5282:  |  | 
| 5283:  |  | 
| 5284:  |  | 
| 5285:  |  | 
| 5286:  |  | 
| 5287:  |  | 
| 5288:  |  | 
| 5289:  |  | 
| 5290:  |  | 
| 5291:  |  | 
| 5292:  |  | 
| 5293:  |  | 
| 5294:  |  | 
| 5295:  |  | 
| 5296:  |  | 
| 5297:  |  | 
| 5298:  |  | 
| 5299:  |  | 
| 5300:  |  | 
| 5301:  |  | 
| 5302:  |  | 
| 5303:  |  | 
| 5304:  |  | 
| 5305:  |      | 
| 5306:  |  | 
| 5307:  |  | 
| 5308:  |  | 
| 5309:  |  | 
| 5310:  |     public function GetIdentities($UserId) | 
| 5311:  |     { | 
| 5312:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 5313:  |  | 
| 5314:  |         self::checkAccess(null, $UserId); | 
| 5315:  |  | 
| 5316:  |         return $this->getIdentitiesManager()->getIdentities($UserId)->all(); | 
| 5317:  |     } | 
| 5318:  |  | 
| 5319:  |      | 
| 5320:  |  | 
| 5321:  |  | 
| 5322:  |  | 
| 5323:  |  | 
| 5324:  |  | 
| 5325:  |  | 
| 5326:  |  | 
| 5327:  |  | 
| 5328:  |  | 
| 5329:  |  | 
| 5330:  |  | 
| 5331:  |  | 
| 5332:  |  | 
| 5333:  |  | 
| 5334:  |  | 
| 5335:  |  | 
| 5336:  |  | 
| 5337:  |  | 
| 5338:  |  | 
| 5339:  |  | 
| 5340:  |  | 
| 5341:  |  | 
| 5342:  |  | 
| 5343:  |  | 
| 5344:  |  | 
| 5345:  |  | 
| 5346:  |  | 
| 5347:  |  | 
| 5348:  |  | 
| 5349:  |  | 
| 5350:  |  | 
| 5351:  |  | 
| 5352:  |  | 
| 5353:  |  | 
| 5354:  |  | 
| 5355:  |  | 
| 5356:  |  | 
| 5357:  |  | 
| 5358:  |  | 
| 5359:  |  | 
| 5360:  |  | 
| 5361:  |  | 
| 5362:  |  | 
| 5363:  |  | 
| 5364:  |  | 
| 5365:  |  | 
| 5366:  |  | 
| 5367:  |  | 
| 5368:  |  | 
| 5369:  |      | 
| 5370:  |  | 
| 5371:  |  | 
| 5372:  |  | 
| 5373:  |  | 
| 5374:  |  | 
| 5375:  |  | 
| 5376:  |  | 
| 5377:  |  | 
| 5378:  |     public function UpdateSignature($AccountID, $UseSignature = null, $Signature = null, $IdentityId = null) | 
| 5379:  |     { | 
| 5380:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 5381:  |  | 
| 5382:  |         if ($AccountID > 0) { | 
| 5383:  |             $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 5384:  |  | 
| 5385:  |             self::checkAccess($oAccount); | 
| 5386:  |  | 
| 5387:  |             if ($this->getConfig('AllowIdentities', false) && $IdentityId !== null) { | 
| 5388:  |                 return $this->getIdentitiesManager()->updateIdentitySignature($IdentityId, $UseSignature, $Signature); | 
| 5389:  |             } else { | 
| 5390:  |                 if ($oAccount) { | 
| 5391:  |                     if ($UseSignature !== null) { | 
| 5392:  |                         $oAccount->UseSignature = $UseSignature; | 
| 5393:  |                     } | 
| 5394:  |                     if ($Signature !== null) { | 
| 5395:  |                         $oAccount->Signature = $Signature; | 
| 5396:  |                     } | 
| 5397:  |  | 
| 5398:  |                     return $this->getAccountsManager()->updateAccount($oAccount); | 
| 5399:  |                 } | 
| 5400:  |             } | 
| 5401:  |         } else { | 
| 5402:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 5403:  |         } | 
| 5404:  |  | 
| 5405:  |         return false; | 
| 5406:  |     } | 
| 5407:  |  | 
| 5408:  |      | 
| 5409:  |  | 
| 5410:  |  | 
| 5411:  |  | 
| 5412:  |  | 
| 5413:  |  | 
| 5414:  |  | 
| 5415:  |  | 
| 5416:  |  | 
| 5417:  |  | 
| 5418:  |  | 
| 5419:  |  | 
| 5420:  |  | 
| 5421:  |  | 
| 5422:  |  | 
| 5423:  |  | 
| 5424:  |  | 
| 5425:  |  | 
| 5426:  |  | 
| 5427:  |  | 
| 5428:  |  | 
| 5429:  |  | 
| 5430:  |  | 
| 5431:  |  | 
| 5432:  |  | 
| 5433:  |  | 
| 5434:  |  | 
| 5435:  |  | 
| 5436:  |  | 
| 5437:  |  | 
| 5438:  |  | 
| 5439:  |  | 
| 5440:  |  | 
| 5441:  |  | 
| 5442:  |  | 
| 5443:  |  | 
| 5444:  |  | 
| 5445:  |  | 
| 5446:  |  | 
| 5447:  |  | 
| 5448:  |  | 
| 5449:  |  | 
| 5450:  |  | 
| 5451:  |  | 
| 5452:  |  | 
| 5453:  |  | 
| 5454:  |  | 
| 5455:  |  | 
| 5456:  |  | 
| 5457:  |  | 
| 5458:  |  | 
| 5459:  |      | 
| 5460:  |  | 
| 5461:  |  | 
| 5462:  |  | 
| 5463:  |  | 
| 5464:  |  | 
| 5465:  |  | 
| 5466:  |     public function UploadAttachment($UserId, $AccountID, $UploadData) | 
| 5467:  |     { | 
| 5468:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 5469:  |  | 
| 5470:  |         $sUUID = \Aurora\System\Api::getUserUUIDById($UserId); | 
| 5471:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 5472:  |  | 
| 5473:  |         self::checkAccess($oAccount); | 
| 5474:  |  | 
| 5475:  |         $sError = ''; | 
| 5476:  |         $aResponse = array(); | 
| 5477:  |  | 
| 5478:  |         if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) { | 
| 5479:  |             if (\is_array($UploadData)) { | 
| 5480:  |                 $sSavedName = 'upload-post-'.\md5($UploadData['name'].$UploadData['tmp_name']); | 
| 5481:  |                 $rData = false; | 
| 5482:  |                 if (\is_resource($UploadData['tmp_name'])) { | 
| 5483:  |                     $rData = $UploadData['tmp_name']; | 
| 5484:  |                 } else { | 
| 5485:  |                     if ($this->getFilecacheManager()->moveUploadedFile($sUUID, $sSavedName, $UploadData['tmp_name'])) { | 
| 5486:  |                         $rData = $this->getFilecacheManager()->getFile($sUUID, $sSavedName); | 
| 5487:  |                     } | 
| 5488:  |                 } | 
| 5489:  |                 if ($rData) { | 
| 5490:  |                     $sUploadName = $UploadData['name']; | 
| 5491:  |                     $iSize = $UploadData['size']; | 
| 5492:  |                     $aResponse['Attachment'] = \Aurora\System\Utils::GetClientFileResponse( | 
| 5493:  |                         null, | 
| 5494:  |                         $UserId, | 
| 5495:  |                         $sUploadName, | 
| 5496:  |                         $sSavedName, | 
| 5497:  |                         $iSize | 
| 5498:  |                     ); | 
| 5499:  |                 } else { | 
| 5500:  |                     $sError = 'unknown'; | 
| 5501:  |                 } | 
| 5502:  |             } else { | 
| 5503:  |                 $sError = 'unknown'; | 
| 5504:  |             } | 
| 5505:  |         } else { | 
| 5506:  |             $sError = 'auth'; | 
| 5507:  |         } | 
| 5508:  |  | 
| 5509:  |         if (0 < strlen($sError)) { | 
| 5510:  |             $aResponse['Error'] = $sError; | 
| 5511:  |         } | 
| 5512:  |  | 
| 5513:  |         return $aResponse; | 
| 5514:  |     } | 
| 5515:  |  | 
| 5516:  |      | 
| 5517:  |  | 
| 5518:  |  | 
| 5519:  |  | 
| 5520:  |  | 
| 5521:  |  | 
| 5522:  |  | 
| 5523:  |  | 
| 5524:  |  | 
| 5525:  |  | 
| 5526:  |  | 
| 5527:  |  | 
| 5528:  |  | 
| 5529:  |  | 
| 5530:  |  | 
| 5531:  |  | 
| 5532:  |  | 
| 5533:  |  | 
| 5534:  |  | 
| 5535:  |  | 
| 5536:  |  | 
| 5537:  |  | 
| 5538:  |  | 
| 5539:  |  | 
| 5540:  |  | 
| 5541:  |  | 
| 5542:  |  | 
| 5543:  |  | 
| 5544:  |  | 
| 5545:  |  | 
| 5546:  |  | 
| 5547:  |  | 
| 5548:  |  | 
| 5549:  |  | 
| 5550:  |  | 
| 5551:  |  | 
| 5552:  |  | 
| 5553:  |  | 
| 5554:  |  | 
| 5555:  |  | 
| 5556:  |  | 
| 5557:  |  | 
| 5558:  |  | 
| 5559:  |  | 
| 5560:  |  | 
| 5561:  |  | 
| 5562:  |  | 
| 5563:  |  | 
| 5564:  |      | 
| 5565:  |  | 
| 5566:  |  | 
| 5567:  |  | 
| 5568:  |  | 
| 5569:  |  | 
| 5570:  |  | 
| 5571:  |     public function SaveAttachmentsAsTempFiles($AccountID, $Attachments = array()) | 
| 5572:  |     { | 
| 5573:  |         $mResult = false; | 
| 5574:  |         $self = $this; | 
| 5575:  |  | 
| 5576:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 5577:  |  | 
| 5578:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 5579:  |  | 
| 5580:  |         self::checkAccess($oAccount); | 
| 5581:  |  | 
| 5582:  |         if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) { | 
| 5583:  |             $sUUID = \Aurora\System\Api::getUserUUIDById($oAccount->IdUser); | 
| 5584:  |             try { | 
| 5585:  |                 if (is_array($Attachments) && 0 < count($Attachments)) { | 
| 5586:  |                     $mResult = array(); | 
| 5587:  |                     foreach ($Attachments as $sAttachment) { | 
| 5588:  |                         $aValues = \Aurora\System\Api::DecodeKeyValues($sAttachment); | 
| 5589:  |                         if (is_array($aValues)) { | 
| 5590:  |                             $sFolder = isset($aValues['Folder']) ? $aValues['Folder'] : ''; | 
| 5591:  |                             $iUid = (int) isset($aValues['Uid']) ? $aValues['Uid'] : 0; | 
| 5592:  |                             $sMimeIndex = (string) isset($aValues['MimeIndex']) ? $aValues['MimeIndex'] : ''; | 
| 5593:  |  | 
| 5594:  |                             $sTempName = md5($sAttachment); | 
| 5595:  |                             if (!$this->getFilecacheManager()->isFileExists($sUUID, $sTempName)) { | 
| 5596:  |                                 $this->getMailManager()->directMessageToStream( | 
| 5597:  |                                     $oAccount, | 
| 5598:  |                                     function ($rResource, $sContentType, $sFileName, $sMimeIndex = '') use ($sUUID, &$mResult, $sTempName, $sAttachment, $self) { | 
| 5599:  |                                         if (is_resource($rResource)) { | 
| 5600:  |                                             $sContentType = (empty($sFileName)) ? 'text/plain' : \MailSo\Base\Utils::MimeContentType($sFileName); | 
| 5601:  |                                             $sFileName = \Aurora\System\Utils::clearFileName($sFileName, $sContentType, $sMimeIndex); | 
| 5602:  |  | 
| 5603:  |                                             if ($self->getFilecacheManager()->putFile($sUUID, $sTempName, $rResource)) { | 
| 5604:  |                                                 $mResult[$sTempName] = $sAttachment; | 
| 5605:  |                                             } | 
| 5606:  |                                         } | 
| 5607:  |                                     }, | 
| 5608:  |                                     $sFolder, | 
| 5609:  |                                     $iUid, | 
| 5610:  |                                     $sMimeIndex | 
| 5611:  |                                 ); | 
| 5612:  |                             } else { | 
| 5613:  |                                 $mResult[$sTempName] = $sAttachment; | 
| 5614:  |                             } | 
| 5615:  |                         } | 
| 5616:  |                     } | 
| 5617:  |                 } | 
| 5618:  |             } catch (\Exception $oException) { | 
| 5619:  |                 throw new \Aurora\Modules\Mail\Exceptions\Exception(Enums\ErrorCodes::CannotConnectToMailServer, $oException, $oException->getMessage()); | 
| 5620:  |             } | 
| 5621:  |         } | 
| 5622:  |  | 
| 5623:  |         return $mResult; | 
| 5624:  |     } | 
| 5625:  |  | 
| 5626:  |      | 
| 5627:  |  | 
| 5628:  |  | 
| 5629:  |  | 
| 5630:  |  | 
| 5631:  |  | 
| 5632:  |  | 
| 5633:  |  | 
| 5634:  |  | 
| 5635:  |  | 
| 5636:  |  | 
| 5637:  |  | 
| 5638:  |  | 
| 5639:  |  | 
| 5640:  |  | 
| 5641:  |  | 
| 5642:  |  | 
| 5643:  |  | 
| 5644:  |  | 
| 5645:  |  | 
| 5646:  |  | 
| 5647:  |  | 
| 5648:  |  | 
| 5649:  |  | 
| 5650:  |  | 
| 5651:  |  | 
| 5652:  |  | 
| 5653:  |  | 
| 5654:  |  | 
| 5655:  |  | 
| 5656:  |  | 
| 5657:  |  | 
| 5658:  |  | 
| 5659:  |  | 
| 5660:  |  | 
| 5661:  |  | 
| 5662:  |  | 
| 5663:  |  | 
| 5664:  |  | 
| 5665:  |  | 
| 5666:  |  | 
| 5667:  |  | 
| 5668:  |  | 
| 5669:  |  | 
| 5670:  |  | 
| 5671:  |  | 
| 5672:  |  | 
| 5673:  |  | 
| 5674:  |  | 
| 5675:  |  | 
| 5676:  |  | 
| 5677:  |  | 
| 5678:  |  | 
| 5679:  |  | 
| 5680:  |      | 
| 5681:  |  | 
| 5682:  |  | 
| 5683:  |  | 
| 5684:  |  | 
| 5685:  |  | 
| 5686:  |  | 
| 5687:  |  | 
| 5688:  |  | 
| 5689:  |     public function SaveMessageAsTempFile($AccountID, $MessageFolder, $MessageUid, $FileName) | 
| 5690:  |     { | 
| 5691:  |         $mResult = false; | 
| 5692:  |         $self = $this; | 
| 5693:  |  | 
| 5694:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 5695:  |  | 
| 5696:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 5697:  |  | 
| 5698:  |         self::checkAccess($oAccount); | 
| 5699:  |  | 
| 5700:  |         if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) { | 
| 5701:  |             $sUUID = \Aurora\System\Api::getUserUUIDById($oAccount->IdUser); | 
| 5702:  |             try { | 
| 5703:  |                 $sMimeType = 'message/rfc822'; | 
| 5704:  |                 $sTempName = md5($MessageFolder.$MessageUid); | 
| 5705:  |                 if (!$this->getFilecacheManager()->isFileExists($sUUID, $sTempName)) { | 
| 5706:  |                     $this->getMailManager()->directMessageToStream( | 
| 5707:  |                         $oAccount, | 
| 5708:  |                         function ($rResource, $sContentType, $sFileName) use ($sUUID, $sTempName, &$sMimeType, $self) { | 
| 5709:  |                             if (is_resource($rResource)) { | 
| 5710:  |                                 $sMimeType = $sContentType; | 
| 5711:  |                                 $sFileName = \Aurora\System\Utils::clearFileName($sFileName, $sMimeType, ''); | 
| 5712:  |                                 $self->getFilecacheManager()->putFile($sUUID, $sTempName, $rResource); | 
| 5713:  |                             } | 
| 5714:  |                         }, | 
| 5715:  |                         $MessageFolder, | 
| 5716:  |                         $MessageUid | 
| 5717:  |                     ); | 
| 5718:  |                 } | 
| 5719:  |  | 
| 5720:  |                 if ($this->getFilecacheManager()->isFileExists($sUUID, $sTempName)) { | 
| 5721:  |                     $iSize = $this->getFilecacheManager()->fileSize($sUUID, $sTempName); | 
| 5722:  |                     $mResult = \Aurora\System\Utils::GetClientFileResponse( | 
| 5723:  |                         null, | 
| 5724:  |                         $oAccount->IdUser, | 
| 5725:  |                         $FileName, | 
| 5726:  |                         $sTempName, | 
| 5727:  |                         $iSize | 
| 5728:  |                     ); | 
| 5729:  |                 } | 
| 5730:  |             } catch (\Exception $oException) { | 
| 5731:  |                 throw new \Aurora\Modules\Mail\Exceptions\Exception(Enums\ErrorCodes::CannotConnectToMailServer, $oException, $oException->getMessage()); | 
| 5732:  |             } | 
| 5733:  |         } | 
| 5734:  |  | 
| 5735:  |         return $mResult; | 
| 5736:  |     } | 
| 5737:  |  | 
| 5738:  |      | 
| 5739:  |  | 
| 5740:  |  | 
| 5741:  |  | 
| 5742:  |  | 
| 5743:  |  | 
| 5744:  |  | 
| 5745:  |  | 
| 5746:  |  | 
| 5747:  |  | 
| 5748:  |  | 
| 5749:  |  | 
| 5750:  |  | 
| 5751:  |  | 
| 5752:  |  | 
| 5753:  |  | 
| 5754:  |  | 
| 5755:  |  | 
| 5756:  |  | 
| 5757:  |  | 
| 5758:  |  | 
| 5759:  |  | 
| 5760:  |  | 
| 5761:  |  | 
| 5762:  |  | 
| 5763:  |  | 
| 5764:  |  | 
| 5765:  |  | 
| 5766:  |  | 
| 5767:  |  | 
| 5768:  |  | 
| 5769:  |  | 
| 5770:  |  | 
| 5771:  |  | 
| 5772:  |  | 
| 5773:  |  | 
| 5774:  |  | 
| 5775:  |  | 
| 5776:  |  | 
| 5777:  |  | 
| 5778:  |  | 
| 5779:  |  | 
| 5780:  |  | 
| 5781:  |  | 
| 5782:  |  | 
| 5783:  |  | 
| 5784:  |  | 
| 5785:  |  | 
| 5786:  |  | 
| 5787:  |      | 
| 5788:  |  | 
| 5789:  |  | 
| 5790:  |  | 
| 5791:  |  | 
| 5792:  |  | 
| 5793:  |  | 
| 5794:  |  | 
| 5795:  |     public function UploadMessage($AccountID, $Folder, $UploadData) | 
| 5796:  |     { | 
| 5797:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 5798:  |  | 
| 5799:  |         $bResult = false; | 
| 5800:  |  | 
| 5801:  |         $oAccount = $this->getAccountsManager()->getAccountById((int)$AccountID); | 
| 5802:  |  | 
| 5803:  |         self::checkAccess($oAccount); | 
| 5804:  |  | 
| 5805:  |         if ($oAccount) { | 
| 5806:  |             $sUUID = \Aurora\System\Api::getUserUUIDById($oAccount->IdUser); | 
| 5807:  |             if (is_array($UploadData)) { | 
| 5808:  |                 $sUploadName = $UploadData['name']; | 
| 5809:  |                 $bIsEmlExtension  = strtolower(pathinfo($sUploadName, PATHINFO_EXTENSION)) === 'eml'; | 
| 5810:  |  | 
| 5811:  |                 if ($bIsEmlExtension) { | 
| 5812:  |                     $sSavedName = 'upload-post-' . md5($UploadData['name'] . $UploadData['tmp_name']); | 
| 5813:  |                     if (is_resource($UploadData['tmp_name'])) { | 
| 5814:  |                         $this->getFilecacheManager()->putFile($sUUID, $sSavedName, $UploadData['tmp_name']); | 
| 5815:  |                     } else { | 
| 5816:  |                         $this->getFilecacheManager()->moveUploadedFile($sUUID, $sSavedName, $UploadData['tmp_name']); | 
| 5817:  |                     } | 
| 5818:  |                     if ($this->getFilecacheManager()->isFileExists($sUUID, $sSavedName)) { | 
| 5819:  |                         $sSavedFullName = $this->getFilecacheManager()->generateFullFilePath($sUUID, $sSavedName); | 
| 5820:  |                         try { | 
| 5821:  |                             $this->getMailManager()->appendMessageFromFile($oAccount, $sSavedFullName, $Folder); | 
| 5822:  |                             $bResult = true; | 
| 5823:  |                         } catch (\Exception $oException) { | 
| 5824:  |                             throw new \Aurora\Modules\Mail\Exceptions\Exception(Enums\ErrorCodes::CannotUploadMessage, $oException, $oException->getMessage()); | 
| 5825:  |                         } | 
| 5826:  |                     } else { | 
| 5827:  |                         throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::UnknownError); | 
| 5828:  |                     } | 
| 5829:  |                 } else { | 
| 5830:  |                     throw new \Aurora\Modules\Mail\Exceptions\Exception(Enums\ErrorCodes::CannotUploadMessageFileNotEml); | 
| 5831:  |                 } | 
| 5832:  |             } | 
| 5833:  |         } else { | 
| 5834:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 5835:  |         } | 
| 5836:  |  | 
| 5837:  |         return $bResult; | 
| 5838:  |     } | 
| 5839:  |  | 
| 5840:  |      | 
| 5841:  |  | 
| 5842:  |  | 
| 5843:  |  | 
| 5844:  |  | 
| 5845:  |  | 
| 5846:  |  | 
| 5847:  |  | 
| 5848:  |  | 
| 5849:  |  | 
| 5850:  |  | 
| 5851:  |  | 
| 5852:  |  | 
| 5853:  |  | 
| 5854:  |  | 
| 5855:  |  | 
| 5856:  |  | 
| 5857:  |  | 
| 5858:  |  | 
| 5859:  |  | 
| 5860:  |  | 
| 5861:  |  | 
| 5862:  |  | 
| 5863:  |  | 
| 5864:  |  | 
| 5865:  |  | 
| 5866:  |  | 
| 5867:  |  | 
| 5868:  |  | 
| 5869:  |  | 
| 5870:  |  | 
| 5871:  |  | 
| 5872:  |  | 
| 5873:  |  | 
| 5874:  |  | 
| 5875:  |  | 
| 5876:  |  | 
| 5877:  |  | 
| 5878:  |  | 
| 5879:  |  | 
| 5880:  |  | 
| 5881:  |  | 
| 5882:  |  | 
| 5883:  |  | 
| 5884:  |  | 
| 5885:  |  | 
| 5886:  |  | 
| 5887:  |  | 
| 5888:  |      | 
| 5889:  |  | 
| 5890:  |  | 
| 5891:  |  | 
| 5892:  |  | 
| 5893:  |  | 
| 5894:  |  | 
| 5895:  |  | 
| 5896:  |     public function ChangePassword($AccountId, $CurrentPassword, $NewPassword) | 
| 5897:  |     { | 
| 5898:  |         $mResult = false; | 
| 5899:  |  | 
| 5900:  |         if ($AccountId > 0) { | 
| 5901:  |             $oAccount = $this->getAccountsManager()->getAccountById($AccountId); | 
| 5902:  |  | 
| 5903:  |             self::checkAccess($oAccount); | 
| 5904:  |  | 
| 5905:  |             $oUser = \Aurora\System\Api::getAuthenticatedUser(); | 
| 5906:  |             if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount && | 
| 5907:  |                 $oUser instanceof \Aurora\Modules\Core\Models\User && | 
| 5908:  |                 (($oUser->isNormalOrTenant() && $oUser->Id === $oAccount->IdUser) || | 
| 5909:  |                 $oUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin) | 
| 5910:  |             ) { | 
| 5911:  |                 if ($oUser->Role !== \Aurora\System\Enums\UserRole::SuperAdmin && $oAccount->getPassword() !== $CurrentPassword) { | 
| 5912:  |                     \Aurora\System\Api::LogEvent('password-change-failed: ' . $oAccount->Email, self::GetName()); | 
| 5913:  |                     throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Exceptions\Errs::UserManager_AccountOldPasswordNotCorrect); | 
| 5914:  |                 } | 
| 5915:  |  | 
| 5916:  |                 $aArgs = [ | 
| 5917:  |                     'Account' => $oAccount, | 
| 5918:  |                     'CurrentPassword' => $CurrentPassword, | 
| 5919:  |                     'NewPassword' => $NewPassword | 
| 5920:  |                 ]; | 
| 5921:  |                 $aResponse = [ | 
| 5922:  |                     'AccountPasswordChanged' => false | 
| 5923:  |                 ]; | 
| 5924:  |  | 
| 5925:  |                 \Aurora\System\Api::GetModule('Core')->broadcastEvent( | 
| 5926:  |                     'Mail::ChangeAccountPassword', | 
| 5927:  |                     $aArgs, | 
| 5928:  |                     $aResponse | 
| 5929:  |                 ); | 
| 5930:  |  | 
| 5931:  |                 if ($aResponse['AccountPasswordChanged']) { | 
| 5932:  |                     $oAccount->setPassword($NewPassword); | 
| 5933:  |                     if ($this->getAccountsManager()->updateAccount($oAccount)) { | 
| 5934:  |                         $mResult = $oAccount; | 
| 5935:  |                         $mResult->RefreshToken = \Aurora\System\Api::UserSession()->UpdateTimestamp(\Aurora\System\Api::getAuthToken(), time()); | 
| 5936:  |                         \Aurora\System\Api::LogEvent('password-change-success: ' . $oAccount->Email, self::GetName()); | 
| 5937:  |                         \Aurora\System\Api::UserSession()->DeleteAllUserSessions($oUser->Id); | 
| 5938:  |                     } | 
| 5939:  |                 } | 
| 5940:  |             } | 
| 5941:  |         } | 
| 5942:  |         if (!$mResult) { | 
| 5943:  |             \Aurora\System\Api::LogEvent('password-change-failed: ' . ($oAccount ? $oAccount->Email : 'Account ID ') . $AccountId, self::GetName()); | 
| 5944:  |         } | 
| 5945:  |  | 
| 5946:  |         return $mResult; | 
| 5947:  |     } | 
| 5948:  |  | 
| 5949:  |      | 
| 5950:  |  | 
| 5951:  |  | 
| 5952:  |  | 
| 5953:  |  | 
| 5954:  |  | 
| 5955:  |  | 
| 5956:  |  | 
| 5957:  |  | 
| 5958:  |  | 
| 5959:  |  | 
| 5960:  |  | 
| 5961:  |  | 
| 5962:  |  | 
| 5963:  |  | 
| 5964:  |  | 
| 5965:  |  | 
| 5966:  |  | 
| 5967:  |  | 
| 5968:  |  | 
| 5969:  |  | 
| 5970:  |  | 
| 5971:  |  | 
| 5972:  |  | 
| 5973:  |  | 
| 5974:  |  | 
| 5975:  |  | 
| 5976:  |  | 
| 5977:  |  | 
| 5978:  |  | 
| 5979:  |  | 
| 5980:  |  | 
| 5981:  |  | 
| 5982:  |  | 
| 5983:  |  | 
| 5984:  |  | 
| 5985:  |  | 
| 5986:  |  | 
| 5987:  |  | 
| 5988:  |  | 
| 5989:  |  | 
| 5990:  |  | 
| 5991:  |  | 
| 5992:  |  | 
| 5993:  |  | 
| 5994:  |  | 
| 5995:  |  | 
| 5996:  |      | 
| 5997:  |  | 
| 5998:  |  | 
| 5999:  |  | 
| 6000:  |  | 
| 6001:  |     public function GetFilters($AccountID) | 
| 6002:  |     { | 
| 6003:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 6004:  |  | 
| 6005:  |         $mResult = false; | 
| 6006:  |  | 
| 6007:  |         $oAccount = $this->getAccountsManager()->getAccountById((int) $AccountID); | 
| 6008:  |  | 
| 6009:  |         self::checkAccess($oAccount); | 
| 6010:  |  | 
| 6011:  |         if ($oAccount) { | 
| 6012:  |             $mResult = $this->getSieveManager()->getSieveFilters($oAccount); | 
| 6013:  |         } | 
| 6014:  |  | 
| 6015:  |         return $mResult; | 
| 6016:  |     } | 
| 6017:  |  | 
| 6018:  |      | 
| 6019:  |  | 
| 6020:  |  | 
| 6021:  |  | 
| 6022:  |  | 
| 6023:  |  | 
| 6024:  |  | 
| 6025:  |  | 
| 6026:  |  | 
| 6027:  |  | 
| 6028:  |  | 
| 6029:  |  | 
| 6030:  |  | 
| 6031:  |  | 
| 6032:  |  | 
| 6033:  |  | 
| 6034:  |  | 
| 6035:  |  | 
| 6036:  |  | 
| 6037:  |  | 
| 6038:  |  | 
| 6039:  |  | 
| 6040:  |  | 
| 6041:  |  | 
| 6042:  |  | 
| 6043:  |  | 
| 6044:  |  | 
| 6045:  |  | 
| 6046:  |  | 
| 6047:  |  | 
| 6048:  |  | 
| 6049:  |  | 
| 6050:  |  | 
| 6051:  |  | 
| 6052:  |  | 
| 6053:  |  | 
| 6054:  |  | 
| 6055:  |  | 
| 6056:  |  | 
| 6057:  |  | 
| 6058:  |  | 
| 6059:  |  | 
| 6060:  |  | 
| 6061:  |  | 
| 6062:  |  | 
| 6063:  |  | 
| 6064:  |  | 
| 6065:  |  | 
| 6066:  |  | 
| 6067:  |      | 
| 6068:  |  | 
| 6069:  |  | 
| 6070:  |  | 
| 6071:  |  | 
| 6072:  |  | 
| 6073:  |     public function UpdateFilters($AccountID, $Filters) | 
| 6074:  |     { | 
| 6075:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 6076:  |  | 
| 6077:  |         $bResult = false; | 
| 6078:  |  | 
| 6079:  |         $oAccount = $this->getAccountsManager()->getAccountById((int) $AccountID); | 
| 6080:  |  | 
| 6081:  |         self::checkAccess($oAccount); | 
| 6082:  |  | 
| 6083:  |         if ($oAccount) { | 
| 6084:  |             $aFilters = array(); | 
| 6085:  |  | 
| 6086:  |             if (is_array($Filters)) { | 
| 6087:  |                 foreach ($Filters as $aFilterData) { | 
| 6088:  |                     $oFilter = $this->getSieveManager()->createFilterInstance($oAccount, $aFilterData); | 
| 6089:  |  | 
| 6090:  |                     $aArgs = [ | 
| 6091:  |                         'Account' => $oAccount, | 
| 6092:  |                         'Filter' => &$oFilter, | 
| 6093:  |                     ]; | 
| 6094:  |                     $this->broadcastEvent('CreateFilterInstance', $aArgs); | 
| 6095:  |  | 
| 6096:  |                     if ($oFilter) { | 
| 6097:  |                         $aFilters[] = $oFilter; | 
| 6098:  |                     } | 
| 6099:  |                 } | 
| 6100:  |             } | 
| 6101:  |  | 
| 6102:  |             $bResult = $this->getSieveManager()->updateSieveFilters($oAccount, $aFilters); | 
| 6103:  |         } | 
| 6104:  |  | 
| 6105:  |         return $bResult; | 
| 6106:  |     } | 
| 6107:  |  | 
| 6108:  |      | 
| 6109:  |  | 
| 6110:  |  | 
| 6111:  |  | 
| 6112:  |  | 
| 6113:  |  | 
| 6114:  |  | 
| 6115:  |  | 
| 6116:  |  | 
| 6117:  |  | 
| 6118:  |  | 
| 6119:  |  | 
| 6120:  |  | 
| 6121:  |  | 
| 6122:  |  | 
| 6123:  |  | 
| 6124:  |  | 
| 6125:  |  | 
| 6126:  |  | 
| 6127:  |  | 
| 6128:  |  | 
| 6129:  |  | 
| 6130:  |  | 
| 6131:  |  | 
| 6132:  |  | 
| 6133:  |  | 
| 6134:  |  | 
| 6135:  |  | 
| 6136:  |  | 
| 6137:  |  | 
| 6138:  |  | 
| 6139:  |  | 
| 6140:  |  | 
| 6141:  |  | 
| 6142:  |  | 
| 6143:  |  | 
| 6144:  |  | 
| 6145:  |  | 
| 6146:  |  | 
| 6147:  |  | 
| 6148:  |  | 
| 6149:  |  | 
| 6150:  |  | 
| 6151:  |  | 
| 6152:  |  | 
| 6153:  |  | 
| 6154:  |  | 
| 6155:  |  | 
| 6156:  |  | 
| 6157:  |      | 
| 6158:  |  | 
| 6159:  |  | 
| 6160:  |  | 
| 6161:  |  | 
| 6162:  |     public function GetForward($AccountID) | 
| 6163:  |     { | 
| 6164:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 6165:  |  | 
| 6166:  |         $mResult = false; | 
| 6167:  |  | 
| 6168:  |         $oAccount = $this->getAccountsManager()->getAccountById((int) $AccountID); | 
| 6169:  |  | 
| 6170:  |         self::checkAccess($oAccount); | 
| 6171:  |  | 
| 6172:  |         if ($oAccount) { | 
| 6173:  |             $mResult = $this->getSieveManager()->getForward($oAccount); | 
| 6174:  |         } | 
| 6175:  |  | 
| 6176:  |         return $mResult; | 
| 6177:  |     } | 
| 6178:  |  | 
| 6179:  |      | 
| 6180:  |  | 
| 6181:  |  | 
| 6182:  |  | 
| 6183:  |  | 
| 6184:  |  | 
| 6185:  |  | 
| 6186:  |  | 
| 6187:  |  | 
| 6188:  |  | 
| 6189:  |  | 
| 6190:  |  | 
| 6191:  |  | 
| 6192:  |  | 
| 6193:  |  | 
| 6194:  |  | 
| 6195:  |  | 
| 6196:  |  | 
| 6197:  |  | 
| 6198:  |  | 
| 6199:  |  | 
| 6200:  |  | 
| 6201:  |  | 
| 6202:  |  | 
| 6203:  |  | 
| 6204:  |  | 
| 6205:  |  | 
| 6206:  |  | 
| 6207:  |  | 
| 6208:  |  | 
| 6209:  |  | 
| 6210:  |  | 
| 6211:  |  | 
| 6212:  |  | 
| 6213:  |  | 
| 6214:  |  | 
| 6215:  |  | 
| 6216:  |  | 
| 6217:  |  | 
| 6218:  |  | 
| 6219:  |  | 
| 6220:  |  | 
| 6221:  |  | 
| 6222:  |  | 
| 6223:  |  | 
| 6224:  |  | 
| 6225:  |  | 
| 6226:  |  | 
| 6227:  |  | 
| 6228:  |      | 
| 6229:  |  | 
| 6230:  |  | 
| 6231:  |  | 
| 6232:  |  | 
| 6233:  |  | 
| 6234:  |  | 
| 6235:  |     public function UpdateForward($AccountID, $Enable = false, $Email = "") | 
| 6236:  |     { | 
| 6237:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 6238:  |  | 
| 6239:  |         $mResult = false; | 
| 6240:  |  | 
| 6241:  |         $oAccount = $this->getAccountsManager()->getAccountById((int) $AccountID); | 
| 6242:  |  | 
| 6243:  |         self::checkAccess($oAccount); | 
| 6244:  |  | 
| 6245:  |         if ($oAccount) { | 
| 6246:  |             $mResult = $this->getSieveManager()->setForward($oAccount, $Email, $Enable); | 
| 6247:  |         } | 
| 6248:  |  | 
| 6249:  |         return $mResult; | 
| 6250:  |     } | 
| 6251:  |  | 
| 6252:  |      | 
| 6253:  |  | 
| 6254:  |  | 
| 6255:  |  | 
| 6256:  |  | 
| 6257:  |  | 
| 6258:  |  | 
| 6259:  |  | 
| 6260:  |  | 
| 6261:  |  | 
| 6262:  |  | 
| 6263:  |  | 
| 6264:  |  | 
| 6265:  |  | 
| 6266:  |  | 
| 6267:  |  | 
| 6268:  |  | 
| 6269:  |  | 
| 6270:  |  | 
| 6271:  |  | 
| 6272:  |  | 
| 6273:  |  | 
| 6274:  |  | 
| 6275:  |  | 
| 6276:  |  | 
| 6277:  |  | 
| 6278:  |  | 
| 6279:  |  | 
| 6280:  |  | 
| 6281:  |  | 
| 6282:  |  | 
| 6283:  |  | 
| 6284:  |  | 
| 6285:  |  | 
| 6286:  |  | 
| 6287:  |  | 
| 6288:  |  | 
| 6289:  |  | 
| 6290:  |  | 
| 6291:  |  | 
| 6292:  |  | 
| 6293:  |  | 
| 6294:  |  | 
| 6295:  |  | 
| 6296:  |  | 
| 6297:  |  | 
| 6298:  |  | 
| 6299:  |  | 
| 6300:  |  | 
| 6301:  |  | 
| 6302:  |      | 
| 6303:  |  | 
| 6304:  |  | 
| 6305:  |  | 
| 6306:  |  | 
| 6307:  |     public function GetAutoresponder($AccountID) | 
| 6308:  |     { | 
| 6309:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 6310:  |  | 
| 6311:  |         $mResult = false; | 
| 6312:  |  | 
| 6313:  |         $oAccount = $this->getAccountsManager()->getAccountById((int) $AccountID); | 
| 6314:  |  | 
| 6315:  |         self::checkAccess($oAccount); | 
| 6316:  |  | 
| 6317:  |         if ($oAccount) { | 
| 6318:  |             $mResult = $this->getSieveManager()->getAutoresponder($oAccount); | 
| 6319:  |         } | 
| 6320:  |  | 
| 6321:  |         return $mResult; | 
| 6322:  |     } | 
| 6323:  |  | 
| 6324:  |      | 
| 6325:  |  | 
| 6326:  |  | 
| 6327:  |  | 
| 6328:  |  | 
| 6329:  |  | 
| 6330:  |  | 
| 6331:  |  | 
| 6332:  |  | 
| 6333:  |  | 
| 6334:  |  | 
| 6335:  |  | 
| 6336:  |  | 
| 6337:  |  | 
| 6338:  |  | 
| 6339:  |  | 
| 6340:  |  | 
| 6341:  |  | 
| 6342:  |  | 
| 6343:  |  | 
| 6344:  |  | 
| 6345:  |  | 
| 6346:  |  | 
| 6347:  |  | 
| 6348:  |  | 
| 6349:  |  | 
| 6350:  |  | 
| 6351:  |  | 
| 6352:  |  | 
| 6353:  |  | 
| 6354:  |  | 
| 6355:  |  | 
| 6356:  |  | 
| 6357:  |  | 
| 6358:  |  | 
| 6359:  |  | 
| 6360:  |  | 
| 6361:  |  | 
| 6362:  |  | 
| 6363:  |  | 
| 6364:  |  | 
| 6365:  |  | 
| 6366:  |  | 
| 6367:  |  | 
| 6368:  |  | 
| 6369:  |  | 
| 6370:  |  | 
| 6371:  |  | 
| 6372:  |  | 
| 6373:  |  | 
| 6374:  |      | 
| 6375:  |  | 
| 6376:  |  | 
| 6377:  |  | 
| 6378:  |  | 
| 6379:  |  | 
| 6380:  |  | 
| 6381:  |  | 
| 6382:  |     public function UpdateAutoresponder($AccountID, $Enable = false, $Subject = "", $Message = "") | 
| 6383:  |     { | 
| 6384:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 6385:  |  | 
| 6386:  |         $mResult = false; | 
| 6387:  |  | 
| 6388:  |         $oAccount = $this->getAccountsManager()->getAccountById((int) $AccountID); | 
| 6389:  |  | 
| 6390:  |         self::checkAccess($oAccount); | 
| 6391:  |  | 
| 6392:  |         if ($Enable && \trim($Subject) === "" && \trim($Message) === "") { | 
| 6393:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 6394:  |         } | 
| 6395:  |  | 
| 6396:  |         if ($oAccount) { | 
| 6397:  |             $mResult = $this->getSieveManager()->setAutoresponder($oAccount, $Subject, $Message, $Enable); | 
| 6398:  |         } | 
| 6399:  |  | 
| 6400:  |         return $mResult; | 
| 6401:  |     } | 
| 6402:  |  | 
| 6403:  |     public function SetAccountSpamSettings($AccountID, $SpamScore, $AllowList, $BlockList) | 
| 6404:  |     { | 
| 6405:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 6406:  |  | 
| 6407:  |         $mResult = false; | 
| 6408:  |  | 
| 6409:  |         $oAccount = $this->getAccountsManager()->getAccountById((int) $AccountID); | 
| 6410:  |  | 
| 6411:  |         self::checkAccess($oAccount); | 
| 6412:  |  | 
| 6413:  |         if ($oAccount) { | 
| 6414:  |             $mResult = $this->getSieveManager()->setAllowBlockLists($oAccount, $AllowList, $BlockList, $SpamScore); | 
| 6415:  |         } | 
| 6416:  |  | 
| 6417:  |         return $mResult; | 
| 6418:  |     } | 
| 6419:  |  | 
| 6420:  |     public function GetAccountSpamSettings($AccountID) | 
| 6421:  |     { | 
| 6422:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 6423:  |  | 
| 6424:  |         $mResult = false; | 
| 6425:  |  | 
| 6426:  |         $oAccount = $this->getAccountsManager()->getAccountById((int) $AccountID); | 
| 6427:  |  | 
| 6428:  |         self::checkAccess($oAccount); | 
| 6429:  |  | 
| 6430:  |         if ($oAccount) { | 
| 6431:  |             $mResult = $this->getSieveManager()->getAllowBlockLists($oAccount); | 
| 6432:  |         } | 
| 6433:  |  | 
| 6434:  |         return $mResult; | 
| 6435:  |     } | 
| 6436:  |  | 
| 6437:  |     public function AddEmailToAllowList($AccountID, $Email) | 
| 6438:  |     { | 
| 6439:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 6440:  |  | 
| 6441:  |         $mResult = false; | 
| 6442:  |  | 
| 6443:  |         $oAccount = $this->getAccountsManager()->getAccountById((int) $AccountID); | 
| 6444:  |  | 
| 6445:  |         self::checkAccess($oAccount); | 
| 6446:  |  | 
| 6447:  |         if ($oAccount) { | 
| 6448:  |             $mResult = $this->getSieveManager()->addRowToAllowList($oAccount, $Email); | 
| 6449:  |         } | 
| 6450:  |  | 
| 6451:  |         return $mResult; | 
| 6452:  |     } | 
| 6453:  |  | 
| 6454:  |     public function AddEmailToBlockList($AccountID, $Email) | 
| 6455:  |     { | 
| 6456:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 6457:  |  | 
| 6458:  |         $mResult = false; | 
| 6459:  |  | 
| 6460:  |         $oAccount = $this->getAccountsManager()->getAccountById((int) $AccountID); | 
| 6461:  |  | 
| 6462:  |         self::checkAccess($oAccount); | 
| 6463:  |  | 
| 6464:  |         if ($oAccount) { | 
| 6465:  |             $mResult = $this->getSieveManager()->addRowToBlockList($oAccount, $Email); | 
| 6466:  |         } | 
| 6467:  |  | 
| 6468:  |         return $mResult; | 
| 6469:  |     } | 
| 6470:  |      | 
| 6471:  |  | 
| 6472:  |      | 
| 6473:  |  | 
| 6474:  |      | 
| 6475:  |  | 
| 6476:  |  | 
| 6477:  |  | 
| 6478:  |  | 
| 6479:  |  | 
| 6480:  |  | 
| 6481:  |     public function onBeforeDeleteUser($aArgs, &$mResult) | 
| 6482:  |     { | 
| 6483:  |         $mResult = $this->getAccountsManager()->getUserAccounts($aArgs["UserId"]); | 
| 6484:  |  | 
| 6485:  |         foreach ($mResult as $oItem) { | 
| 6486:  |             self::Decorator()->DeleteAccount($oItem->Id); | 
| 6487:  |         } | 
| 6488:  |     } | 
| 6489:  |  | 
| 6490:  |     public function onAfterDeleteUser($aArgs, &$mResult) | 
| 6491:  |     { | 
| 6492:  |         if ($mResult) { | 
| 6493:  |             Identity::where('IdUser', $aArgs["UserId"])->delete(); | 
| 6494:  |             TrustedSender::where('IdUser', $aArgs["UserId"])->delete(); | 
| 6495:  |         } | 
| 6496:  |     } | 
| 6497:  |  | 
| 6498:  |      | 
| 6499:  |  | 
| 6500:  |  | 
| 6501:  |  | 
| 6502:  |  | 
| 6503:  |  | 
| 6504:  |     public function GetMailServerByDomain($Domain, $AllowWildcardDomain = false) | 
| 6505:  |     { | 
| 6506:  |         $aResult = []; | 
| 6507:  |         $bFoundWithWildcard = false; | 
| 6508:  |  | 
| 6509:  |         $oServer = $this->getServersManager()->getServerByDomain($Domain); | 
| 6510:  |  | 
| 6511:  |         if (!($oServer instanceof \Aurora\Modules\Mail\Models\Server) && $AllowWildcardDomain) { | 
| 6512:  |             $oServer = $this->getServersManager()->getServerByDomain('*'); | 
| 6513:  |             $bFoundWithWildcard = true; | 
| 6514:  |         } | 
| 6515:  |  | 
| 6516:  |         if (!($oServer instanceof \Aurora\Modules\Mail\Models\Server) && $AllowWildcardDomain) { | 
| 6517:  |             $oServer = $this->getServersManager()->getServerByDomain(''); | 
| 6518:  |             $bFoundWithWildcard = true; | 
| 6519:  |         } | 
| 6520:  |  | 
| 6521:  |         if ($oServer instanceof \Aurora\Modules\Mail\Models\Server) { | 
| 6522:  |             $aResult = [ | 
| 6523:  |                 'Server'			=> $oServer, | 
| 6524:  |                 'FoundWithWildcard'	=> $bFoundWithWildcard | 
| 6525:  |             ]; | 
| 6526:  |         } | 
| 6527:  |  | 
| 6528:  |         return $aResult; | 
| 6529:  |     } | 
| 6530:  |  | 
| 6531:  |      | 
| 6532:  |  | 
| 6533:  |  | 
| 6534:  |  | 
| 6535:  |  | 
| 6536:  |  | 
| 6537:  |  | 
| 6538:  |  | 
| 6539:  |     public function onLogin($aArgs, &$mResult) | 
| 6540:  |     { | 
| 6541:  |         $bResult = false; | 
| 6542:  |         $iUserId = 0; | 
| 6543:  |  | 
| 6544:  |         $sEmail = $aArgs['Login']; | 
| 6545:  |         $sMailLogin = $aArgs['Login']; | 
| 6546:  |         $oAccount = $this->getAccountsManager()->getAccountUsedToAuthorize($sEmail); | 
| 6547:  |  | 
| 6548:  |         $bNewAccount = false; | 
| 6549:  |         $bAutocreateMailAccountOnNewUserFirstLogin = $this->getConfig('AutocreateMailAccountOnNewUserFirstLogin', false); | 
| 6550:  |         if (!$bAutocreateMailAccountOnNewUserFirstLogin && !$oAccount) { | 
| 6551:  |             $oUser = \Aurora\System\Api::GetModuleDecorator('Core')->GetUserByPublicId($sEmail); | 
| 6552:  |             if ($oUser instanceof \Aurora\Modules\Core\Models\User) { | 
| 6553:  |                 $bAutocreateMailAccountOnNewUserFirstLogin = true; | 
| 6554:  |             } | 
| 6555:  |         } | 
| 6556:  |  | 
| 6557:  |         $oServer = null; | 
| 6558:  |         if ($bAutocreateMailAccountOnNewUserFirstLogin && !$oAccount) { | 
| 6559:  |             $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); | 
| 6560:  |             if (!empty(trim($sDomain))) { | 
| 6561:  |                 $aGetMailServerResult = self::Decorator()->GetMailServerByDomain($sDomain, true); | 
| 6562:  |                 if (!empty($aGetMailServerResult) && isset($aGetMailServerResult['Server']) && $aGetMailServerResult['Server'] instanceof \Aurora\Modules\Mail\Models\Server) { | 
| 6563:  |                     $oServer = $aGetMailServerResult['Server']; | 
| 6564:  |                 } | 
| 6565:  |  | 
| 6566:  |                 $oTenant = \Aurora\System\Api::getTenantByWebDomain(); | 
| 6567:  |                 if ($oServer && (!$oTenant || $oServer->OwnerType === \Aurora\Modules\Mail\Enums\ServerOwnerType::SuperAdmin || $oServer->TenantId === $oTenant->Id)) { | 
| 6568:  |                     $sMailLogin = !$oServer->UseFullEmailAddressAsLogin && preg_match('/(.+)@.+$/', $sEmail, $matches) && $matches[1] ? $matches[1] : $sEmail; | 
| 6569:  |  | 
| 6570:  |                     $oAccount = new \Aurora\Modules\Mail\Models\MailAccount(); | 
| 6571:  |                     $oAccount->Email = $sEmail; | 
| 6572:  |                     $oAccount->IncomingLogin = $sMailLogin; | 
| 6573:  |                     $oAccount->setPassword($aArgs['Password']); | 
| 6574:  |                     $oAccount->ServerId = $oServer->Id; | 
| 6575:  |                     $bNewAccount = true; | 
| 6576:  |                 } else { | 
| 6577:  |                     throw new \Aurora\System\Exceptions\ApiException( | 
| 6578:  |                         Enums\ErrorCodes::DomainIsNotAllowedForLoggingIn, | 
| 6579:  |                         null, | 
| 6580:  |                         '', | 
| 6581:  |                         [], | 
| 6582:  |                         $this | 
| 6583:  |                     ); | 
| 6584:  |                 } | 
| 6585:  |             } | 
| 6586:  |         } | 
| 6587:  |  | 
| 6588:  |         if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) { | 
| 6589:  |             try { | 
| 6590:  |                 if ($bAutocreateMailAccountOnNewUserFirstLogin || !$bNewAccount) { | 
| 6591:  |                     $sOldPassword = $oAccount->getPassword(); | 
| 6592:  |                     $sNewPassword = $aArgs['Password']; | 
| 6593:  |  | 
| 6594:  |                     $oAccount->setPassword($sNewPassword); | 
| 6595:  |                     $mValidResult = $this->getMailManager()->validateAccountConnection($oAccount, false); | 
| 6596:  |                     $bResult = !($mValidResult instanceof \Exception); | 
| 6597:  |                     if ($bResult && $sNewPassword !== $sOldPassword) { | 
| 6598:  |                          | 
| 6599:  |                         $this->getAccountsManager()->updateAccount($oAccount); | 
| 6600:  |                     } | 
| 6601:  |                 } | 
| 6602:  |  | 
| 6603:  |                 if ($bResult && $bAutocreateMailAccountOnNewUserFirstLogin && $bNewAccount) { | 
| 6604:  |                     $oUser = null; | 
| 6605:  |                     $aSubArgs = array( | 
| 6606:  |                         'UserName' => $sEmail, | 
| 6607:  |                         'Email' => $sEmail, | 
| 6608:  |                         'UserId' => $iUserId, | 
| 6609:  |                         'TenantId' => $oServer->TenantId | 
| 6610:  |                     ); | 
| 6611:  |                     $this->broadcastEvent( | 
| 6612:  |                         'CreateAccount', | 
| 6613:  |                         $aSubArgs, | 
| 6614:  |                         $oUser | 
| 6615:  |                     ); | 
| 6616:  |  | 
| 6617:  |                     if ($oUser instanceof \Aurora\Modules\Core\Models\User) { | 
| 6618:  |                         $iUserId = $oUser->Id; | 
| 6619:  |                         $bPrevState = \Aurora\System\Api::skipCheckUserRole(true); | 
| 6620:  |                         $oAccount = self::Decorator()->CreateAccount( | 
| 6621:  |                             $iUserId, | 
| 6622:  |                             '', | 
| 6623:  |                             $sEmail, | 
| 6624:  |                             $sMailLogin, | 
| 6625:  |                             $aArgs['Password'], | 
| 6626:  |                             array('ServerId' => $oServer->Id) | 
| 6627:  |                         ); | 
| 6628:  |                         \Aurora\System\Api::skipCheckUserRole($bPrevState); | 
| 6629:  |                         if ($oAccount) { | 
| 6630:  |                             $oAccount->UseToAuthorize = true; | 
| 6631:  |                             $oAccount->UseThreading = $oServer->EnableThreading; | 
| 6632:  |                             $bResult = $this->getAccountsManager()->updateAccount($oAccount); | 
| 6633:  |                         } else { | 
| 6634:  |                             $bResult = false; | 
| 6635:  |                         } | 
| 6636:  |                     } | 
| 6637:  |                 } | 
| 6638:  |  | 
| 6639:  |                 if ($bResult) { | 
| 6640:  |                     $mResult = \Aurora\System\UserSession::getTokenData($oAccount, $aArgs['SignMe']); | 
| 6641:  |                 } | 
| 6642:  |             } catch (\Aurora\System\Exceptions\ApiException $oException) { | 
| 6643:  |                 throw $oException; | 
| 6644:  |             } catch (\Exception $oException) { | 
| 6645:  |             } | 
| 6646:  |         } | 
| 6647:  |  | 
| 6648:  |         return $bResult; | 
| 6649:  |     } | 
| 6650:  |  | 
| 6651:  |      | 
| 6652:  |  | 
| 6653:  |  | 
| 6654:  |  | 
| 6655:  |  | 
| 6656:  |     public function onGetAccounts($aArgs, &$aResult) | 
| 6657:  |     { | 
| 6658:  |         $aUserInfo = \Aurora\System\Api::getAuthenticatedUserInfo($aArgs['AuthToken']); | 
| 6659:  |         if (isset($aUserInfo['userId'])) { | 
| 6660:  |             $mResult = $this->GetAccounts($aUserInfo['userId']); | 
| 6661:  |             if (\is_array($mResult)) { | 
| 6662:  |                 foreach ($mResult as $oItem) { | 
| 6663:  |                     $aResult[] = [ | 
| 6664:  |                         'Type' => $oItem->getName(), | 
| 6665:  |                         'Module' => $oItem->getModule(), | 
| 6666:  |                         'Id' => $oItem->Id, | 
| 6667:  |                         'UUID' => $oItem->UUID, | 
| 6668:  |                         'Login' => $oItem->IncomingLogin | 
| 6669:  |                     ]; | 
| 6670:  |                 } | 
| 6671:  |             } | 
| 6672:  |         } | 
| 6673:  |     } | 
| 6674:  |  | 
| 6675:  |      | 
| 6676:  |  | 
| 6677:  |  | 
| 6678:  |  | 
| 6679:  |  | 
| 6680:  |  | 
| 6681:  |  | 
| 6682:  |  | 
| 6683:  |  | 
| 6684:  |  | 
| 6685:  |     private function setMessageFlag($AccountID, $sFolderFullNameRaw, $sUids, $bSetAction, $sFlagName) | 
| 6686:  |     { | 
| 6687:  |         $aUids = \Aurora\System\Utils::ExplodeIntUids((string) $sUids); | 
| 6688:  |  | 
| 6689:  |         if (0 === \strlen(\trim($sFolderFullNameRaw)) || !\is_array($aUids) || 0 === \count($aUids)) { | 
| 6690:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); | 
| 6691:  |         } | 
| 6692:  |  | 
| 6693:  |         $oAccount = $this->getAccountsManager()->getAccountById($AccountID); | 
| 6694:  |  | 
| 6695:  |         return $this->getMailManager()->setMessageFlag( | 
| 6696:  |             $oAccount, | 
| 6697:  |             $sFolderFullNameRaw, | 
| 6698:  |             $aUids, | 
| 6699:  |             $sFlagName, | 
| 6700:  |             $bSetAction ? \Aurora\Modules\Mail\Enums\MessageStoreAction::Add : \Aurora\Modules\Mail\Enums\MessageStoreAction::Remove | 
| 6701:  |         ); | 
| 6702:  |     } | 
| 6703:  |  | 
| 6704:  |      | 
| 6705:  |  | 
| 6706:  |  | 
| 6707:  |  | 
| 6708:  |  | 
| 6709:  |  | 
| 6710:  |  | 
| 6711:  |  | 
| 6712:  |     private function fixBase64EncodeOmitsPaddingBytes($sRaw) | 
| 6713:  |     { | 
| 6714:  |         $rStream = \fopen('php://memory', 'r+'); | 
| 6715:  |         \fwrite($rStream, '0'); | 
| 6716:  |         \rewind($rStream); | 
| 6717:  |         $rFilter = \stream_filter_append($rStream, 'convert.base64-encode'); | 
| 6718:  |  | 
| 6719:  |         if (0 === \strlen(\stream_get_contents($rStream))) { | 
| 6720:  |             $iFileSize = \strlen($sRaw); | 
| 6721:  |             $sRaw = \str_pad($sRaw, $iFileSize + ($iFileSize % 3)); | 
| 6722:  |         } | 
| 6723:  |  | 
| 6724:  |         return $sRaw; | 
| 6725:  |     } | 
| 6726:  |  | 
| 6727:  |      | 
| 6728:  |  | 
| 6729:  |  | 
| 6730:  |  | 
| 6731:  |  | 
| 6732:  |  | 
| 6733:  |  | 
| 6734:  |  | 
| 6735:  |  | 
| 6736:  |  | 
| 6737:  |  | 
| 6738:  |  | 
| 6739:  |  | 
| 6740:  |  | 
| 6741:  |  | 
| 6742:  |  | 
| 6743:  |  | 
| 6744:  |  | 
| 6745:  |  | 
| 6746:  |  | 
| 6747:  |  | 
| 6748:  |  | 
| 6749:  |  | 
| 6750:  |     public function BuildMessage( | 
| 6751:  |         $oAccount, | 
| 6752:  |         $sTo = '', | 
| 6753:  |         $sCc = '', | 
| 6754:  |         $sBcc = '', | 
| 6755:  |         $sSubject = '', | 
| 6756:  |         $bTextIsHtml = false, | 
| 6757:  |         $sText = '', | 
| 6758:  |         $aAttachments = null, | 
| 6759:  |         $aDraftInfo = null, | 
| 6760:  |         $sInReplyTo = '', | 
| 6761:  |         $sReferences = '', | 
| 6762:  |         $iImportance = '', | 
| 6763:  |         $iSensitivity = 0, | 
| 6764:  |         $bSendReadingConfirmation = false, | 
| 6765:  |         $oFetcher = null, | 
| 6766:  |         $oAlias = null, | 
| 6767:  |         $bWithDraftInfo = true, | 
| 6768:  |         $oIdentity = null, | 
| 6769:  |         $aCustomHeaders = [] | 
| 6770:  |     ) { | 
| 6771:  |         self::checkAccess($oAccount); | 
| 6772:  |  | 
| 6773:  |         $oMessage = \MailSo\Mime\Message::NewInstance(); | 
| 6774:  |         $oMessage->RegenerateMessageId(); | 
| 6775:  |  | 
| 6776:  |         $sUUID = \Aurora\System\Api::getUserUUIDById($oAccount->IdUser); | 
| 6777:  |  | 
| 6778:  |         $sXMailer = $this->getConfig('XMailerValue', ''); | 
| 6779:  |         if (0 < \strlen($sXMailer)) { | 
| 6780:  |             $oMessage->SetXMailer($sXMailer); | 
| 6781:  |         } | 
| 6782:  |  | 
| 6783:  |         $sXOriginatingIPHeaderName = $this->getConfig('XOriginatingIPHeaderName', ''); | 
| 6784:  |         if (!empty($sXOriginatingIPHeaderName)) { | 
| 6785:  |             $sIP = $this->oHttp->GetClientIp(); | 
| 6786:  |             $oMessage->SetCustomHeader( | 
| 6787:  |                 $sXOriginatingIPHeaderName, | 
| 6788:  |                 $this->oHttp->IsLocalhost($sIP) ? '127.0.0.1' : $sIP | 
| 6789:  |             ); | 
| 6790:  |         } | 
| 6791:  |  | 
| 6792:  |         if ($oIdentity) { | 
| 6793:  |             $oFrom = \MailSo\Mime\Email::NewInstance($oIdentity->Email, $oIdentity->FriendlyName); | 
| 6794:  |         } elseif ($oAlias) { | 
| 6795:  |             $oFrom = \MailSo\Mime\Email::NewInstance($oAlias->Email, $oAlias->FriendlyName); | 
| 6796:  |         } else { | 
| 6797:  |             $oFrom = $oFetcher | 
| 6798:  |                 ? \MailSo\Mime\Email::NewInstance($oFetcher->Email, $oFetcher->Name) | 
| 6799:  |                 : \MailSo\Mime\Email::NewInstance($oAccount->Email, $oAccount->FriendlyName); | 
| 6800:  |         } | 
| 6801:  |  | 
| 6802:  |         $oMessage | 
| 6803:  |             ->SetFrom($oFrom) | 
| 6804:  |             ->SetSubject($sSubject) | 
| 6805:  |         ; | 
| 6806:  |  | 
| 6807:  |         $oToEmails = \MailSo\Mime\EmailCollection::NewInstance($sTo); | 
| 6808:  |         if ($oToEmails && $oToEmails->Count()) { | 
| 6809:  |             $oMessage->SetTo($oToEmails); | 
| 6810:  |         } | 
| 6811:  |  | 
| 6812:  |         $oCcEmails = \MailSo\Mime\EmailCollection::NewInstance($sCc); | 
| 6813:  |         if ($oCcEmails && $oCcEmails->Count()) { | 
| 6814:  |             $oMessage->SetCc($oCcEmails); | 
| 6815:  |         } | 
| 6816:  |  | 
| 6817:  |         $oBccEmails = \MailSo\Mime\EmailCollection::NewInstance($sBcc); | 
| 6818:  |         if ($oBccEmails && $oBccEmails->Count()) { | 
| 6819:  |             $oMessage->SetBcc($oBccEmails); | 
| 6820:  |         } | 
| 6821:  |  | 
| 6822:  |         if ($bWithDraftInfo && \is_array($aDraftInfo) && !empty($aDraftInfo[0]) && !empty($aDraftInfo[1]) && !empty($aDraftInfo[2])) { | 
| 6823:  |             $oMessage->SetDraftInfo($aDraftInfo[0], $aDraftInfo[1], $aDraftInfo[2]); | 
| 6824:  |         } | 
| 6825:  |  | 
| 6826:  |         if (0 < \strlen($sInReplyTo)) { | 
| 6827:  |             $oMessage->SetInReplyTo($sInReplyTo); | 
| 6828:  |         } | 
| 6829:  |  | 
| 6830:  |         if (0 < \strlen($sReferences)) { | 
| 6831:  |             $oMessage->SetReferences($sReferences); | 
| 6832:  |         } | 
| 6833:  |  | 
| 6834:  |         if (\in_array($iImportance, array( | 
| 6835:  |             \MailSo\Mime\Enumerations\MessagePriority::HIGH, | 
| 6836:  |             \MailSo\Mime\Enumerations\MessagePriority::NORMAL, | 
| 6837:  |             \MailSo\Mime\Enumerations\MessagePriority::LOW | 
| 6838:  |         ))) { | 
| 6839:  |             $oMessage->SetPriority($iImportance); | 
| 6840:  |         } | 
| 6841:  |  | 
| 6842:  |         if (\in_array($iSensitivity, array( | 
| 6843:  |             \MailSo\Mime\Enumerations\Sensitivity::NOTHING, | 
| 6844:  |             \MailSo\Mime\Enumerations\Sensitivity::CONFIDENTIAL, | 
| 6845:  |             \MailSo\Mime\Enumerations\Sensitivity::PRIVATE_, | 
| 6846:  |             \MailSo\Mime\Enumerations\Sensitivity::PERSONAL, | 
| 6847:  |         ))) { | 
| 6848:  |             $oMessage->SetSensitivity((int) $iSensitivity); | 
| 6849:  |         } | 
| 6850:  |  | 
| 6851:  |         if (is_array($aCustomHeaders)) { | 
| 6852:  |             foreach ($aCustomHeaders as $sHeaderName => $sHeaderValue) { | 
| 6853:  |                 $oMessage->SetCustomHeader($sHeaderName, $sHeaderValue); | 
| 6854:  |             } | 
| 6855:  |         } | 
| 6856:  |  | 
| 6857:  |         if ($bSendReadingConfirmation) { | 
| 6858:  |             $oMessage->SetReadConfirmation($oFetcher ? $oFetcher->Email : $oAccount->Email); | 
| 6859:  |         } | 
| 6860:  |  | 
| 6861:  |         $aFoundCids = array(); | 
| 6862:  |  | 
| 6863:  |         if ($bTextIsHtml) { | 
| 6864:  |             $sTextConverted = \MailSo\Base\HtmlUtils::ConvertHtmlToPlain($sText); | 
| 6865:  |             $oMessage->AddText($sTextConverted, false); | 
| 6866:  |         } | 
| 6867:  |  | 
| 6868:  |         $mFoundDataURL = array(); | 
| 6869:  |         $aFoundedContentLocationUrls = array(); | 
| 6870:  |  | 
| 6871:  |         $sTextConverted = $bTextIsHtml ? | 
| 6872:  |             \MailSo\Base\HtmlUtils::BuildHtml($sText, $aFoundCids, $mFoundDataURL, $aFoundedContentLocationUrls) : $sText; | 
| 6873:  |  | 
| 6874:  |         $oMessage->AddText($sTextConverted, $bTextIsHtml); | 
| 6875:  |  | 
| 6876:  |         if (\is_array($aAttachments)) { | 
| 6877:  |             foreach ($aAttachments as $sTempName => $aData) { | 
| 6878:  |                 if (\is_array($aData) && isset($aData[0], $aData[1], $aData[2], $aData[3])) { | 
| 6879:  |                     $sFileName = (string) $aData[0]; | 
| 6880:  |                     $sCID = (string) $aData[1]; | 
| 6881:  |                     $bIsInline = '1' === (string) $aData[2]; | 
| 6882:  |                     $bIsLinked = '1' === (string) $aData[3]; | 
| 6883:  |                     $sContentLocation = isset($aData[4]) ? (string) $aData[4] : ''; | 
| 6884:  |  | 
| 6885:  |                     $rResource = $this->getFilecacheManager()->getFile($sUUID, $sTempName); | 
| 6886:  |                     if (\is_resource($rResource)) { | 
| 6887:  |                         $iFileSize = $this->getFilecacheManager()->fileSize($sUUID, $sTempName); | 
| 6888:  |  | 
| 6889:  |                         $sCID = \trim(\trim($sCID), '<>'); | 
| 6890:  |                         $bIsFounded = 0 < \strlen($sCID) ? \in_array($sCID, $aFoundCids) : false; | 
| 6891:  |  | 
| 6892:  |                         if (!$bIsLinked || $bIsFounded) { | 
| 6893:  |                             $oMessage->Attachments()->Add( | 
| 6894:  |                                 \MailSo\Mime\Attachment::NewInstance( | 
| 6895:  |                                     $rResource, | 
| 6896:  |                                     $sFileName, | 
| 6897:  |                                     $iFileSize, | 
| 6898:  |                                     $bIsInline, | 
| 6899:  |                                     $bIsLinked, | 
| 6900:  |                                     $bIsLinked ? '<'.$sCID.'>' : '', | 
| 6901:  |                                     array(), | 
| 6902:  |                                     $sContentLocation | 
| 6903:  |                                 ) | 
| 6904:  |                             ); | 
| 6905:  |                         } | 
| 6906:  |                     } else { | 
| 6907:  |                         \Aurora\System\Api::Log('Error: there is no temp file for attachment ' . $sFileName); | 
| 6908:  |                     } | 
| 6909:  |                 } | 
| 6910:  |             } | 
| 6911:  |         } | 
| 6912:  |  | 
| 6913:  |         if ($mFoundDataURL && \is_array($mFoundDataURL) && 0 < \count($mFoundDataURL)) { | 
| 6914:  |             foreach ($mFoundDataURL as $sCidHash => $sDataUrlString) { | 
| 6915:  |                 $aMatch = array(); | 
| 6916:  |                 $sCID = '<'.$sCidHash.'>'; | 
| 6917:  |                 if (\preg_match('/^data:(image\/[a-zA-Z0-9]+\+?[a-zA-Z0-9]+);base64,(.+)$/i', $sDataUrlString, $aMatch) && | 
| 6918:  |                     !empty($aMatch[1]) && !empty($aMatch[2])) { | 
| 6919:  |                     $sRaw = \MailSo\Base\Utils::Base64Decode($aMatch[2]); | 
| 6920:  |                     $iFileSize = \strlen($sRaw); | 
| 6921:  |                     if (0 < $iFileSize) { | 
| 6922:  |                         $sFileName = \preg_replace('/[^a-z0-9]+/i', '.', \MailSo\Base\Utils::NormalizeContentType($aMatch[1])); | 
| 6923:  |  | 
| 6924:  |                          | 
| 6925:  |                         $sRaw = $this->fixBase64EncodeOmitsPaddingBytes($sRaw); | 
| 6926:  |  | 
| 6927:  |                         $rResource = \MailSo\Base\ResourceRegistry::CreateMemoryResourceFromString($sRaw); | 
| 6928:  |  | 
| 6929:  |                         $sRaw = ''; | 
| 6930:  |                         unset($sRaw); | 
| 6931:  |                         unset($aMatch); | 
| 6932:  |  | 
| 6933:  |                         $oMessage->Attachments()->Add( | 
| 6934:  |                             \MailSo\Mime\Attachment::NewInstance($rResource, $sFileName, $iFileSize, true, true, $sCID) | 
| 6935:  |                         ); | 
| 6936:  |                     } | 
| 6937:  |                 } | 
| 6938:  |             } | 
| 6939:  |         } | 
| 6940:  |  | 
| 6941:  |         return $oMessage; | 
| 6942:  |     } | 
| 6943:  |  | 
| 6944:  |     public function onAfterDeleteTenant(&$aArgs, &$mResult) | 
| 6945:  |     { | 
| 6946:  |         $TenantId = $aArgs['TenantId']; | 
| 6947:  |         $aServers = self::Decorator()->GetServers($TenantId); | 
| 6948:  |         if (is_array($aServers) && $aServers['Count'] > 0) { | 
| 6949:  |             foreach ($aServers['Items'] as $oServer) { | 
| 6950:  |                 if ($oServer['TenantId'] === $TenantId) { | 
| 6951:  |                     self::Decorator()->DeleteServer($oServer['Id'], $TenantId); | 
| 6952:  |                 } | 
| 6953:  |             } | 
| 6954:  |         } | 
| 6955:  |     } | 
| 6956:  |  | 
| 6957:  |     public function onAfterGetAutodiscover(&$aArgs, &$mResult) | 
| 6958:  |     { | 
| 6959:  |         $sIncomingServer = \trim($this->getConfig('ExternalHostNameOfLocalImap')); | 
| 6960:  |         $sOutgoingServer = \trim($this->getConfig('ExternalHostNameOfLocalSmtp')); | 
| 6961:  |         $sEmail = $aArgs['Email']; | 
| 6962:  |  | 
| 6963:  |         if (0 < \strlen($sIncomingServer) && 0 < \strlen($sOutgoingServer)) { | 
| 6964:  |             $iIncomingPort = 143; | 
| 6965:  |             $iOutgoingPort = 25; | 
| 6966:  |  | 
| 6967:  |             $aMatch = array(); | 
| 6968:  |             if (\preg_match('/:([\d]+)$/', $sIncomingServer, $aMatch) && !empty($aMatch[1]) && \is_numeric($aMatch[1])) { | 
| 6969:  |                 $sIncomingServer = \preg_replace('/:[\d]+$/', $sIncomingServer, ''); | 
| 6970:  |                 $iIncomingPort = (int) $aMatch[1]; | 
| 6971:  |             } | 
| 6972:  |  | 
| 6973:  |             $aMatch = array(); | 
| 6974:  |             if (\preg_match('/:([\d]+)$/', $sOutgoingServer, $aMatch) && !empty($aMatch[1]) && \is_numeric($aMatch[1])) { | 
| 6975:  |                 $sOutgoingServer = \preg_replace('/:[\d]+$/', $sOutgoingServer, ''); | 
| 6976:  |                 $iOutgoingPort = (int) $aMatch[1]; | 
| 6977:  |             } | 
| 6978:  |  | 
| 6979:  |             $sResult = \implode("\n", array( | 
| 6980:  | '		<Account>', | 
| 6981:  | '			<AccountType>email</AccountType>', | 
| 6982:  | '			<Action>settings</Action>', | 
| 6983:  | '			<Protocol>', | 
| 6984:  | '				<Type>IMAP</Type>', | 
| 6985:  | '				<Server>'.$sIncomingServer.'</Server>', | 
| 6986:  | '				<LoginName>'.$sEmail.'</LoginName>', | 
| 6987:  | '				<Port>'.$iIncomingPort.'</Port>', | 
| 6988:  | '				<SSL>'.(993 === $iIncomingPort ? 'on' : 'off').'</SSL>', | 
| 6989:  | '				<SPA>off</SPA>', | 
| 6990:  | '				<AuthRequired>on</AuthRequired>', | 
| 6991:  | '			</Protocol>', | 
| 6992:  | '			<Protocol>', | 
| 6993:  | '				<Type>SMTP</Type>', | 
| 6994:  | '				<Server>'.$sOutgoingServer.'</Server>', | 
| 6995:  | '				<LoginName>'.$sEmail.'</LoginName>', | 
| 6996:  | '				<Port>'.$iOutgoingPort.'</Port>', | 
| 6997:  | '				<SSL>'.(465 === $iOutgoingPort ? 'on' : 'off').'</SSL>', | 
| 6998:  | '				<SPA>off</SPA>', | 
| 6999:  | '				<AuthRequired>on</AuthRequired>', | 
| 7000:  | '			</Protocol>', | 
| 7001:  | '		</Account>' | 
| 7002:  | )); | 
| 7003:  |             $mResult = $mResult . $sResult; | 
| 7004:  |         } | 
| 7005:  |     } | 
| 7006:  |  | 
| 7007:  |     public function EntryMessageNewtab() | 
| 7008:  |     { | 
| 7009:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 7010:  |  | 
| 7011:  |         $oApiIntegrator = \Aurora\System\Managers\Integrator::getInstance(); | 
| 7012:  |  | 
| 7013:  |         if ($oApiIntegrator) { | 
| 7014:  |             \Aurora\Modules\CoreWebclient\Module::Decorator()->SetHtmlOutputHeaders(); | 
| 7015:  |             $aConfig = array( | 
| 7016:  |                 'new_tab' => true, | 
| 7017:  |                 'modules_list' => $oApiIntegrator->GetModulesForEntry('MailWebclient') | 
| 7018:  |             ); | 
| 7019:  |  | 
| 7020:  |             $oCoreWebclientModule = \Aurora\System\Api::GetModule('CoreWebclient'); | 
| 7021:  |             if ($oCoreWebclientModule instanceof \Aurora\System\Module\AbstractModule) { | 
| 7022:  |                 $sResult = \file_get_contents($oCoreWebclientModule->GetPath().'/templates/Index.html'); | 
| 7023:  |                 if (\is_string($sResult)) { | 
| 7024:  |                     return strtr($sResult, array( | 
| 7025:  |                         '{{AppVersion}}' => AU_APP_VERSION, | 
| 7026:  |                         '{{IntegratorDir}}' => $oApiIntegrator->isRtl() ? 'rtl' : 'ltr', | 
| 7027:  |                         '{{IntegratorLinks}}' => $oApiIntegrator->buildHeadersLink(), | 
| 7028:  |                         '{{IntegratorBody}}' => $oApiIntegrator->buildBody($aConfig) | 
| 7029:  |                     )); | 
| 7030:  |                 } | 
| 7031:  |             } | 
| 7032:  |         } | 
| 7033:  |     } | 
| 7034:  |  | 
| 7035:  |     public function EntryDownloadAttachment() | 
| 7036:  |     { | 
| 7037:  |         $sHash = (string) \Aurora\System\Router::getItemByIndex(1, ''); | 
| 7038:  |         $aValues = \Aurora\System\Api::DecodeKeyValues($sHash); | 
| 7039:  |         $sAuthToken = isset($aValues['AuthToken']) ? $aValues['AuthToken'] : null; | 
| 7040:  |         if (isset($sAuthToken)) { | 
| 7041:  |             \Aurora\System\Api::setUserId( | 
| 7042:  |                 \Aurora\System\Api::getAuthenticatedUserId($sAuthToken) | 
| 7043:  |             ); | 
| 7044:  |             \Aurora\System\Api::setAuthToken($sAuthToken); | 
| 7045:  |         } | 
| 7046:  |  | 
| 7047:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 7048:  |  | 
| 7049:  |         if ($this->getConfig('CleanupOutputBeforeDownload', false)) { | 
| 7050:  |             @ob_clean();  | 
| 7051:  |         } | 
| 7052:  |  | 
| 7053:  |         $this->getRaw( | 
| 7054:  |             $sHash, | 
| 7055:  |             (string) \Aurora\System\Router::getItemByIndex(2, '') | 
| 7056:  |         ); | 
| 7057:  |     } | 
| 7058:  |  | 
| 7059:  |     public function onBeforeRunEntry(&$aArguments, &$aResult) | 
| 7060:  |     { | 
| 7061:  |         $sEntry = 'mail-attachment'; | 
| 7062:  |         if ($aArguments['EntryName'] === $sEntry) { | 
| 7063:  |             $sParam3 = \Aurora\System\Router::getItemByIndex(3, null); | 
| 7064:  |             if (isset($sParam3) && $sParam3 === 'get-expired-link') { | 
| 7065:  |                 $sHash = (string) \Aurora\System\Router::getItemByIndex(1, ''); | 
| 7066:  |                 $sAction = (string) \Aurora\System\Router::getItemByIndex(2, ''); | 
| 7067:  |                 $iTime = $this->getConfig('ExpiredLinkLifetimeMinutes ', 30); | 
| 7068:  |  | 
| 7069:  |                 $aValues = \Aurora\System\Api::DecodeKeyValues($sHash); | 
| 7070:  |  | 
| 7071:  |                 if (!isset($aValues['AuthToken'])) { | 
| 7072:  |                     $aValues['AuthToken'] = \Aurora\System\Api::UserSession()->Set( | 
| 7073:  |                         array( | 
| 7074:  |                             'token' => 'auth', | 
| 7075:  |                             'id' => \Aurora\System\Api::getAuthenticatedUserId(), | 
| 7076:  |                         ), | 
| 7077:  |                         time(), | 
| 7078:  |                         time() + 60 * $iTime | 
| 7079:  |                     ); | 
| 7080:  |  | 
| 7081:  |                     $sHash = \Aurora\System\Api::EncodeKeyValues($aValues); | 
| 7082:  |                 } | 
| 7083:  |  | 
| 7084:  |                 \header('Location: ' . \MailSo\Base\Http::SingletonInstance()->GetFullUrl() . '?' . $sEntry .'/' . $sHash . '/' . $sAction); | 
| 7085:  |             } | 
| 7086:  |         } | 
| 7087:  |     } | 
| 7088:  |  | 
| 7089:  |     public function EntryDownloadAttachmentCookieless() | 
| 7090:  |     { | 
| 7091:  |         $sAuthToken = $_GET['AuthToken']; | 
| 7092:  |  | 
| 7093:  |         $re = '/^[a-zA-Z0-9_-]+$/'; | 
| 7094:  |         $bSafeToken = preg_match($re, $sAuthToken); | 
| 7095:  |  | 
| 7096:  |         if ($sAuthToken !== '' && !!$bSafeToken) { | 
| 7097:  |             \Aurora\System\Api::authorise($sAuthToken); | 
| 7098:  |             $this->EntryDownloadAttachment(); | 
| 7099:  |         } | 
| 7100:  |     } | 
| 7101:  |  | 
| 7102:  |      | 
| 7103:  |  | 
| 7104:  |  | 
| 7105:  |  | 
| 7106:  |  | 
| 7107:  |     private function getRaw($sHash, $sAction = '') | 
| 7108:  |     { | 
| 7109:  |         $self = $this; | 
| 7110:  |         $bDownload = true; | 
| 7111:  |         $bThumbnail = false; | 
| 7112:  |  | 
| 7113:  |         switch ($sAction) { | 
| 7114:  |             case 'view': | 
| 7115:  |                 $bDownload = false; | 
| 7116:  |                 $bThumbnail = false; | 
| 7117:  |                 break; | 
| 7118:  |             case 'thumb': | 
| 7119:  |                 $bDownload = false; | 
| 7120:  |                 $bThumbnail = true; | 
| 7121:  |                 break; | 
| 7122:  |             default: | 
| 7123:  |                 $bDownload = true; | 
| 7124:  |                 $bThumbnail = false; | 
| 7125:  |                 break; | 
| 7126:  |         } | 
| 7127:  |  | 
| 7128:  |         $aValues = \Aurora\System\Api::DecodeKeyValues($sHash); | 
| 7129:  |  | 
| 7130:  |         $sFolder = ''; | 
| 7131:  |         $iUid = 0; | 
| 7132:  |         $sMimeIndex = ''; | 
| 7133:  |  | 
| 7134:  |         $oAccount = null; | 
| 7135:  |  | 
| 7136:  |         $iUserId = null; | 
| 7137:  |  | 
| 7138:  |         if (isset($aValues['AccountID'])) { | 
| 7139:  |             $oAccount = $this->getAccountsManager()->getAccountById((int) $aValues['AccountID']); | 
| 7140:  |  | 
| 7141:  |             self::checkAccess($oAccount); | 
| 7142:  |  | 
| 7143:  |             if (!$oAccount || \Aurora\System\Api::getAuthenticatedUserId() !== $oAccount->IdUser) { | 
| 7144:  |                 return false; | 
| 7145:  |             } | 
| 7146:  |             $iUserId = $oAccount->IdUser; | 
| 7147:  |         } | 
| 7148:  |  | 
| 7149:  |         $sFolder = isset($aValues['Folder']) ? $aValues['Folder'] : ''; | 
| 7150:  |         $iUid = (int) (isset($aValues['Uid']) ? $aValues['Uid'] : 0); | 
| 7151:  |         $sMimeIndex = (string) (isset($aValues['MimeIndex']) ? $aValues['MimeIndex'] : ''); | 
| 7152:  |         $sContentTypeIn = (string) (isset($aValues['MimeType']) ? $aValues['MimeType'] : ''); | 
| 7153:  |         $sFileNameIn = (string) (isset($aValues['FileName']) ? $aValues['FileName'] : ''); | 
| 7154:  |  | 
| 7155:  |          | 
| 7156:  |         if (!$bDownload && strtolower(pathinfo($sFileNameIn, PATHINFO_EXTENSION)) === 'svg') { | 
| 7157:  |             $this->oHttp->StatusHeader(403); | 
| 7158:  |             exit(); | 
| 7159:  |         } | 
| 7160:  |  | 
| 7161:  |         $bCache = true; | 
| 7162:  |         if ($bCache && 0 < \strlen($sFolder) && 0 < $iUid) { | 
| 7163:  |             \Aurora\System\Managers\Response::verifyCacheByKey($sHash); | 
| 7164:  |         } | 
| 7165:  |  | 
| 7166:  |         if ($bThumbnail) { | 
| 7167:  |             $sThumbnail = \Aurora\System\Managers\Thumb::GetResourceCache($iUserId, $sFileNameIn); | 
| 7168:  |             if ($sThumbnail) { | 
| 7169:  |                 $sContentType = \MailSo\Base\Utils::MimeContentType($sFileNameIn); | 
| 7170:  |                 \Aurora\System\Managers\Response::OutputHeaders($bDownload, $sContentType, $sFileNameIn); | 
| 7171:  |                 echo $sThumbnail; | 
| 7172:  |                 exit(); | 
| 7173:  |             } | 
| 7174:  |         } | 
| 7175:  |  | 
| 7176:  |         return $this->getMailManager()->directMessageToStream( | 
| 7177:  |             $oAccount, | 
| 7178:  |             function ($rResource, $sContentType, $sFileName, $sMimeIndex = '') use ($self, $iUserId, $sHash, $bCache, $sContentTypeIn, $sFileNameIn, $bThumbnail, $bDownload) { | 
| 7179:  |                 if (\is_resource($rResource)) { | 
| 7180:  |                     $sContentTypeOut = $sContentTypeIn; | 
| 7181:  |                     if (empty($sContentTypeOut)) { | 
| 7182:  |                         $sContentTypeOut = $sContentType; | 
| 7183:  |                         if (empty($sContentTypeOut)) { | 
| 7184:  |                             $sContentTypeOut = (empty($sFileName)) ? 'text/plain' : \MailSo\Base\Utils::MimeContentType($sFileName); | 
| 7185:  |                         } | 
| 7186:  |                     } | 
| 7187:  |  | 
| 7188:  |                     $sFileNameOut = $sFileNameIn; | 
| 7189:  |                     if (empty($sFileNameOut) || '.' === $sFileNameOut[0]) { | 
| 7190:  |                         $sFileNameOut = $sFileName; | 
| 7191:  |                     } | 
| 7192:  |  | 
| 7193:  |                     $sFileNameOut = \Aurora\System\Utils::clearFileName($sFileNameOut, $sContentType, $sMimeIndex); | 
| 7194:  |  | 
| 7195:  |                     if ($bCache) { | 
| 7196:  |                         \Aurora\System\Managers\Response::cacheByKey($sHash); | 
| 7197:  |                     } | 
| 7198:  |  | 
| 7199:  |                     \Aurora\System\Utils::OutputFileResource($iUserId, $sContentType, $sFileName, $rResource, $bThumbnail, $bDownload); | 
| 7200:  |                 } | 
| 7201:  |             }, | 
| 7202:  |             $sFolder, | 
| 7203:  |             $iUid, | 
| 7204:  |             $sMimeIndex | 
| 7205:  |         ); | 
| 7206:  |     } | 
| 7207:  |  | 
| 7208:  |     public function onGetDigestHash($aArgs, &$mResult) | 
| 7209:  |     { | 
| 7210:  |         $oAccount = $this->getAccountsManager()->getAccountUsedToAuthorize($aArgs['Login']); | 
| 7211:  |         if (is_a($oAccount, $aArgs['Type'])) { | 
| 7212:  |             $mResult = \md5($aArgs['Login'] . ':' . $aArgs['Realm'] . ':' . $oAccount->GetPassword()); | 
| 7213:  |             return true; | 
| 7214:  |         } | 
| 7215:  |     } | 
| 7216:  |  | 
| 7217:  |     public function onGetAccountUsedToAuthorize($aArgs, &$mResult) | 
| 7218:  |     { | 
| 7219:  |         $oAccount = $this->getAccountsManager()->getAccountUsedToAuthorize($aArgs['Login']); | 
| 7220:  |         if ($oAccount) { | 
| 7221:  |             $mResult = $oAccount; | 
| 7222:  |             return true; | 
| 7223:  |         } | 
| 7224:  |     } | 
| 7225:  |  | 
| 7226:  |     public function onCastExtendedProp($aArgs, &$mValue) | 
| 7227:  |     { | 
| 7228:  |         if ($aArgs['Model'] instanceof User) { | 
| 7229:  |             if ($aArgs['PropertyName'] === $this->GetName() . '::UserSpaceLimitMb') { | 
| 7230:  |                 $mValue = (int) $mValue; | 
| 7231:  |             } | 
| 7232:  |             if ($aArgs['PropertyName'] === $this->GetName() . '::AllowAutosaveInDrafts') { | 
| 7233:  |                 $mValue = (bool) $mValue; | 
| 7234:  |             } | 
| 7235:  |         } | 
| 7236:  |  | 
| 7237:  |         if ($aArgs['Model'] instanceof Tenant) { | 
| 7238:  |             if ($aArgs['PropertyName'] === $this->GetName() . '::TenantSpaceLimitMb' || | 
| 7239:  |                 $aArgs['PropertyName'] === $this->GetName() . '::UserSpaceLimitMb' || | 
| 7240:  |                 $aArgs['PropertyName'] === $this->GetName() . '::AllocatedSpaceMb' | 
| 7241:  |             ) { | 
| 7242:  |                 $mValue = (int) $mValue; | 
| 7243:  |             } | 
| 7244:  |             if ($aArgs['PropertyName'] === $this->GetName() . '::AllowChangeUserSpaceLimit') { | 
| 7245:  |                 $mValue = (bool) $mValue; | 
| 7246:  |             } | 
| 7247:  |         } | 
| 7248:  |     } | 
| 7249:  |  | 
| 7250:  |     public function GetServerDomains($ServerId, $TenantId) | 
| 7251:  |     { | 
| 7252:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 7253:  |         $aResult = []; | 
| 7254:  |  | 
| 7255:  |         $oServer = $this->getServersManager()->getServer($ServerId); | 
| 7256:  |         if ($oServer instanceof Models\Server) { | 
| 7257:  |             $aResult = explode("\n", $oServer->Domains); | 
| 7258:  |             $iWildcard = array_search('*', $aResult); | 
| 7259:  |             if ($iWildcard !== false) { | 
| 7260:  |                 unset($aResult[$iWildcard]); | 
| 7261:  |             } | 
| 7262:  |         } | 
| 7263:  |  | 
| 7264:  |         return $aResult; | 
| 7265:  |     } | 
| 7266:  |  | 
| 7267:  |     public function IsEmailAllowedForCreation($Email) | 
| 7268:  |     { | 
| 7269:  |          | 
| 7270:  |         $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); | 
| 7271:  |         if ( | 
| 7272:  |             $oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin | 
| 7273:  |             || $oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::TenantAdmin | 
| 7274:  |         ) { | 
| 7275:  |             $oUser = \Aurora\System\Api::GetModuleDecorator('Core')->GetUserByPublicId($Email); | 
| 7276:  |             if ($oUser instanceof \Aurora\Modules\Core\Models\User) { | 
| 7277:  |                 return false; | 
| 7278:  |             } | 
| 7279:  |             $aAccounts = $this->getAccountsManager()->getAccounts([ | 
| 7280:  |                 'Email' => $Email, | 
| 7281:  |                 'IsDisabled' => false | 
| 7282:  |             ]); | 
| 7283:  |             if (is_array($aAccounts) && count($aAccounts) > 0) { | 
| 7284:  |                 return false; | 
| 7285:  |             } | 
| 7286:  |  | 
| 7287:  |             return true; | 
| 7288:  |         } else { | 
| 7289:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied); | 
| 7290:  |         } | 
| 7291:  |     } | 
| 7292:  |  | 
| 7293:  |      | 
| 7294:  | } | 
| 7295:  |  |