Exemplo n.º 1
0
 private function executeDomainPage()
 {
     global $USER, $APPLICATION;
     $APPLICATION->setTitle(GetMessage('INTR_MAIL_DOMAIN_PAGE_TITLE'));
     if (!$USER->isAdmin() && !$USER->canDoOperation('bitrix24_config')) {
         $APPLICATION->AuthForm(GetMessage('ACCESS_DENIED'));
         return;
     }
     $errors = array();
     $status = false;
     $serviceId = null;
     $settings = array();
     $services = CIntranetMailSetupHelper::getMailServices();
     if ($domainService = CIntranetMailSetupHelper::getDomainService()) {
         $serviceId = $domainService['id'];
         $settings = array('type' => $domainService['type'], 'domain' => $domainService['server'], 'flags' => $domainService['flags'], 'token' => $domainService['token'], 'public' => $domainService['encryption'] == 'N' ? 'Y' : 'N');
     }
     if ($serviceId) {
         $status = self::checkDomainStatus($settings, $error);
         // не нужно при посте
         if ($error) {
             $errors[] = $error;
         }
     }
     if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['act'])) {
         $errors = array();
         if (!check_bitrix_sessid()) {
             $errors[] = GetMessage('INTR_MAIL_CSRF');
         }
         if (!empty($errors)) {
         } else {
             if ($_POST['act'] == 'save') {
                 if (empty($_REQUEST['type']) || !in_array($_REQUEST['type'], array('delegate', 'connect'))) {
                     $errors[] = GetMessage('INTR_MAIL_FORM_ERROR');
                 }
                 if (empty($errors)) {
                     if (!$serviceId && empty($_REQUEST['domain'])) {
                         $errors[] = GetMessage('INTR_MAIL_INP_DOMAIN_EMPTY');
                     }
                     if ($_REQUEST['type'] == 'connect' && empty($_REQUEST['token'])) {
                         $errors[] = GetMessage('INTR_MAIL_INP_TOKEN_EMPTY');
                     }
                     if (!$serviceId) {
                         $settings['type'] = $_REQUEST['type'] == 'connect' ? 'domain' : 'crdomain';
                         $settings['domain'] = $_REQUEST['domain'];
                     }
                     if ($settings['type'] == 'domain') {
                         $settings['token'] = $_REQUEST['token'];
                     }
                     $settings['public'] = isset($_REQUEST['public']) && $_REQUEST['public'] == 'Y' ? 'Y' : 'N';
                     if (empty($errors)) {
                         $status = self::checkDomainStatus($settings, $error);
                         if ($error) {
                             $errors[] = $error;
                         }
                         if (empty($errors)) {
                             if ($serviceId) {
                                 $result = \Bitrix\Mail\MailServicesTable::update($serviceId, array('TOKEN' => $settings['token'], 'ENCRYPTION' => $settings['public'] == 'Y' ? 'N' : 'Y'));
                             } else {
                                 $result = \Bitrix\Mail\MailServicesTable::add(array('SITE_ID' => SITE_ID, 'ACTIVE' => 'Y', 'SERVICE_TYPE' => $settings['type'], 'NAME' => $settings['domain'], 'SERVER' => $settings['domain'], 'ENCRYPTION' => $settings['public'] == 'Y' ? 'N' : 'Y', 'SORT' => $serviceId ? $services[$serviceId]['sort'] + 1 : 100, 'TOKEN' => $settings['token']));
                                 if ($result->isSuccess()) {
                                     $serviceId = $result->getId();
                                     if ($settings['type'] == 'domain') {
                                         if ($status['stage'] == 'added') {
                                             CAgent::addAgent('CIntranetUtils::notifyMailDomain("nomailbox", ' . $serviceId . ', ' . $USER->getId() . ');', 'intranet', 'N', 3600 * 24 * 7);
                                         } else {
                                             CAgent::addAgent('CIntranetUtils::checkMailDomain(' . $serviceId . ', ' . $USER->getId() . ');', 'intranet', 'N', 600);
                                             CAgent::addAgent('CIntranetUtils::notifyMailDomain("nocomplete", ' . $serviceId . ', ' . $USER->getId() . ');', 'intranet', 'N', 3600 * 24 * 3);
                                         }
                                         CMailDomain2::setDomainLogo($settings['token'], $settings['domain'], $_SERVER['DOCUMENT_ROOT'] . $this->getPath() . '/images/' . GetMessage('INTR_MAIL_DOMAIN_ICON'), $replace = false, $error);
                                         CMailDomain2::selLocale($settings['token'], $settings['domain'], LANGUAGE_ID, $error);
                                     }
                                 }
                             }
                             if ($result->isSuccess()) {
                                 if ($status['stage'] == 'added') {
                                     LocalRedirect($APPLICATION->GetCurPage() . '?page=manage');
                                 }
                             } else {
                                 $errors[] = GetMessage('INTR_MAIL_SAVE_ERROR');
                             }
                         }
                     }
                 }
             } else {
                 if ($serviceId && $_POST['act'] == 'remove') {
                     $result = \Bitrix\Mail\MailServicesTable::delete($serviceId);
                     if ($result->isSuccess()) {
                         LocalRedirect($APPLICATION->GetCurPage() . '?page=home');
                     } else {
                         LocalRedirect($APPLICATION->GetCurPage() . '?page=domain');
                     }
                 }
             }
         }
     }
     $this->arParams['SERVICES'] = $services;
     $this->arResult['SERVICE'] = $serviceId;
     $this->arResult['SETTINGS'] = $settings;
     $this->arResult['ERRORS'] = $errors;
     $this->arResult['STATUS'] = $status;
     $this->includeComponentTemplate('domain');
 }
Exemplo n.º 2
0
 private static function executeManageCreateMailbox(&$error)
 {
     $domainUsers = array('vacant' => array(), 'occupied' => array());
     $error = false;
     if (!check_bitrix_sessid()) {
         $error = GetMessage('INTR_MAIL_CSRF');
     }
     if ($error === false) {
         if (!isset($_REQUEST['create'])) {
             $error = GetMessage('INTR_MAIL_FORM_ERROR');
         } else {
             $exists = $_REQUEST['create'] == 0;
             $userId = $_REQUEST['USER_ID'];
             if ($exists) {
                 $serviceId = $_REQUEST['sservice'];
                 $domain = $_REQUEST['sdomain'];
                 $login = $_REQUEST['suser'];
             } else {
                 $serviceId = $_REQUEST['cservice'];
                 $domain = $_REQUEST['cdomain'];
                 $login = $_REQUEST['cuser'];
                 $password = $_REQUEST['password'];
                 $password2 = $_REQUEST['password2'];
             }
         }
     }
     if ($error === false) {
         if (intval($userId)) {
             $dbUser = CUser::getList($by = 'ID', $order = 'ASC', array('ID_EQUAL_EXACT' => intval($userId)), array('FIELDS' => 'ID'));
             if (!$dbUser->fetch()) {
                 $error = GetMessage('INTR_MAIL_FORM_ERROR');
             }
         }
     }
     if ($error === false) {
         $services = CIntranetMailSetupHelper::getMailServices();
         if (empty($services[$serviceId]) || !in_array($services[$serviceId]['type'], array('controller', 'domain', 'crdomain'))) {
             $error = GetMessage('INTR_MAIL_FORM_ERROR');
         } else {
             $service = $services[$serviceId];
             if ($service['type'] == 'controller') {
                 $crDomains = CControllerClient::ExecuteEvent('OnMailControllerGetDomains', array());
                 $arDomains = empty($crDomains['result']) ? array() : $crDomains['result'];
                 if (!is_array($arDomains) || !in_array($domain, $arDomains)) {
                     $error = CMail::getErrorMessage(CMail::ERR_API_OP_DENIED);
                 }
             } else {
                 if ($service['type'] == 'crdomain') {
                     $crDomains = CControllerClient::ExecuteEvent('OnMailControllerGetMemberDomains', array());
                     $arDomains = empty($crDomains['result']) ? array() : $crDomains['result'];
                     if (!is_array($arDomains) || !in_array($domain, $arDomains)) {
                         $error = CMail::getErrorMessage(CMail::ERR_API_OP_DENIED);
                     }
                 } else {
                     if ($service['type'] == 'domain') {
                         if ($service['server'] != $domain) {
                             $error = GetMessage('INTR_MAIL_FORM_ERROR');
                         }
                     }
                 }
             }
         }
     }
     if ($error === false) {
         if (!$exists && $password != $password2) {
             $error = GetMessage('INTR_MAIL_INP_PASSWORD2_BAD');
         }
     }
     if ($error === false) {
         if ($service['type'] == 'controller') {
             $crCheckName = CControllerClient::ExecuteEvent('OnMailControllerCheckName', array('DOMAIN' => $domain, 'NAME' => $login));
             if (isset($crCheckName['result'])) {
                 $isExistsNow = (bool) $crCheckName['result'];
             } else {
                 $error = empty($crCheckName['error']) ? GetMessage('INTR_MAIL_CONTROLLER_INVALID') : CMail::getErrorMessage($crCheckName['error']);
             }
         } else {
             if ($service['type'] == 'crdomain') {
                 $crCheckName = CControllerClient::ExecuteEvent('OnMailControllerCheckMemberName', array('DOMAIN' => $domain, 'NAME' => $login));
                 if (isset($crCheckName['result'])) {
                     $isExistsNow = (bool) $crCheckName['result'];
                 } else {
                     $error = empty($crCheckName['error']) ? GetMessage('INTR_MAIL_CONTROLLER_INVALID') : CMail::getErrorMessage($crCheckName['error']);
                 }
             } else {
                 if ($service['type'] == 'domain') {
                     $isExistsNow = CMailDomain2::isUserExists($service['token'], $domain, $login, $error);
                     if (is_null($isExistsNow)) {
                         $error = CMail::getErrorMessage($error);
                     }
                 }
             }
         }
         if ($error === false) {
             if ($exists) {
                 if ($isExistsNow == false) {
                     $error = CMail::getErrorMessage(CMail::ERR_API_USER_NOTFOUND);
                 }
                 if ($error === false) {
                     if ($service['type'] == 'controller') {
                         $crCheckMailbox = CControllerClient::ExecuteEvent('OnMailControllerCheckMailbox', array('DOMAIN' => $domain, 'NAME' => $login));
                         if (!isset($crCheckMailbox['result'])) {
                             $error = empty($crCheckMailbox['error']) ? GetMessage('INTR_MAIL_CONTROLLER_INVALID') : CMail::getErrorMessage($crCheckMailbox['error']);
                         }
                     }
                 }
                 if ($error === false) {
                     $dbMailbox = CMailbox::getList(array('TIMESTAMP_X' => 'ASC'), array('ACTIVE' => 'Y', '!USER_ID' => intval($userId), '=LOGIN' => $login . '@' . $domain));
                     if (($mailbox = $dbMailbox->fetch()) && $mailbox['USER_ID']) {
                         $error = GetMessage('INTR_MAIL_MAILBOX_OCCUPIED');
                     }
                 }
             } else {
                 if ($isExistsNow == true) {
                     $error = CMail::getErrorMessage(CMail::ERR_API_NAME_OCCUPIED);
                 }
                 if ($error === false) {
                     if (!preg_match('/^[a-z0-9_]+(\\.?[a-z0-9_-]*[a-z0-9_]+)*?$/i', $login)) {
                         $error = CMail::getErrorMessage(CMail::ERR_API_BAD_NAME);
                     }
                 }
                 if ($error === false) {
                     if ($service['type'] == 'controller') {
                         $crResponse = CControllerClient::ExecuteEvent('OnMailControllerAddUser', array('DOMAIN' => $domain, 'NAME' => $login, 'PASSWORD' => $password));
                         if (!isset($crResponse['result'])) {
                             $error = empty($crResponse['error']) ? GetMessage('INTR_MAIL_CONTROLLER_INVALID') : CMail::getErrorMessage($crResponse['error']);
                         }
                     } else {
                         if ($service['type'] == 'crdomain') {
                             $crResponse = CControllerClient::ExecuteEvent('OnMailControllerAddMemberUser', array('DOMAIN' => $domain, 'NAME' => $login, 'PASSWORD' => $password));
                             if (!isset($crResponse['result'])) {
                                 $error = empty($crResponse['error']) ? GetMessage('INTR_MAIL_CONTROLLER_INVALID') : CMail::getErrorMessage($crResponse['error']);
                             }
                         } else {
                             if ($service['type'] == 'domain') {
                                 $result = CMailDomain2::addUser($service['token'], $domain, $login, $password, $error);
                                 if (is_null($result)) {
                                     $error = CMail::getErrorMessage($error);
                                 }
                             }
                         }
                     }
                     if ($error === false) {
                         if (empty($domainUsers['vacant'][$service['id']])) {
                             $domainUsers['vacant'][$service['id']] = array();
                         }
                         if (empty($domainUsers['vacant'][$service['id']][$domain])) {
                             $domainUsers['vacant'][$service['id']][$domain] = array();
                         }
                         $domainUsers['vacant'][$service['id']][$domain][] = $login;
                     }
                 }
             }
             if ($error === false && $userId) {
                 $mailbox = CIntranetMailSetupHelper::getUserMailbox($userId);
                 if (!empty($mailbox)) {
                     $res = CMailbox::delete($mailbox['ID']);
                     if (in_array($mailbox['SERVER_TYPE'], array('domain', 'controller', 'crdomain')) && $res) {
                         list($login_tmp, $domain_tmp) = explode('@', $mailbox['LOGIN'], 2);
                         if (empty($domainUsers['vacant'][$mailbox['SERVICE_ID']])) {
                             $domainUsers['vacant'][$mailbox['SERVICE_ID']] = array();
                         }
                         if (empty($domainUsers['vacant'][$mailbox['SERVICE_ID']][$domain_tmp])) {
                             $domainUsers['vacant'][$mailbox['SERVICE_ID']][$domain_tmp] = array();
                         }
                         $domainUsers['vacant'][$mailbox['SERVICE_ID']][$domain_tmp][] = $login_tmp;
                     }
                 }
                 $arFields = array('LID' => SITE_ID, 'ACTIVE' => 'Y', 'SERVICE_ID' => $serviceId, 'NAME' => $service['name'], 'LOGIN' => $login . '@' . $domain, 'SERVER_TYPE' => $service['type'], 'USER_ID' => intval($userId));
                 $res = CMailbox::add($arFields);
                 if (!$res) {
                     $error = GetMessage('INTR_MAIL_SAVE_ERROR');
                 } else {
                     if (!empty($domainUsers['vacant'][$serviceId][$domain])) {
                         if ($key = array_search($login, $domainUsers['vacant'][$serviceId][$domain])) {
                             array_splice($domainUsers['vacant'][$serviceId][$domain], $key, 1);
                         }
                     }
                     if (empty($domainUsers['occupied'][$serviceId])) {
                         $domainUsers['occupied'][$serviceId] = array();
                     }
                     if (empty($domainUsers['occupied'][$serviceId][$domain])) {
                         $domainUsers['occupied'][$serviceId][$domain] = array();
                     }
                     $domainUsers['occupied'][$serviceId][$domain][] = $login;
                 }
             }
         }
     }
     if ($error === false) {
         $email = $login . '@' . $domain;
         $create = '<a href="#" onclick="mb.create(' . intval($userId) . '); return false; ">' . GetMessage('INTR_MAIL_MANAGE_CHANGE') . '</a>';
         $create .= '<br><a href="#" onclick="mb.changePassword(' . intval($userId) . '); return false; ">' . GetMessage('INTR_MAIL_MANAGE_PASSWORD') . '</a>';
         $delete = '<a href="#" onclick="mb.remove(' . intval($userId) . '); return false; ">' . GetMessage('INTR_MAIL_MANAGE_DELETE') . '</a>';
     }
     return array('users' => $domainUsers, 'email' => isset($email) ? $email : '', 'create' => isset($create) ? CharsetConverter::ConvertCharset($create, SITE_CHARSET, 'UTF-8') : '', 'delete' => isset($delete) ? CharsetConverter::ConvertCharset($delete, SITE_CHARSET, 'UTF-8') : '');
 }