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') : ''); }
public static function createMailbox($exists, $userId, $serviceId, $domain, $login, $password, &$error) { $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 = self::getMailServices(); if (empty($services[$serviceId]) || !in_array($services[$serviceId]['type'], array('controller', 'domain', 'crdomain'))) { $error = GetMessage('INTR_MAIL_FORM_ERROR'); } } if ($error === false) { $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 && !$exists) { 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 && intval($userId)) { if ($exists) { 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'); } } } if ($error === false) { $mailbox = self::getUserMailbox($userId); if (!empty($mailbox)) { $res = CMailbox::delete($mailbox['ID']); } $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'); } } } if ($error === false) { return $login . '@' . $domain; } }