1: <?php
2:
3: /**
4: * This code is licensed under AGPLv3 license or Afterlogic Software License
5: * if commercial version of the product was purchased.
6: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
7: */
8:
9: namespace Aurora\Modules\MailSignupDirectadmin;
10:
11: /**
12: * Allows users to create new email accounts for themselves on DirectAdmin.
13: *
14: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
15: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
16: * @copyright Copyright (c) 2023, Afterlogic Corp.
17: *
18: * @property Settings $oModuleSettings
19: *
20: * @package Modules
21: */
22: class Module extends \Aurora\System\Module\AbstractModule
23: {
24: /**
25: * @var \DirectAdminSignAPI
26: */
27: private $oDAApi;
28:
29: /**
30: * @return Module
31: */
32: public static function getInstance()
33: {
34: return parent::getInstance();
35: }
36:
37: /**
38: * @return Module
39: */
40: public static function Decorator()
41: {
42: return parent::Decorator();
43: }
44:
45: public function init()
46: {
47: $this->subscribeEvent('MailSignup::Signup::before', [$this, 'onAfterSignup']);
48:
49: require_once __DIR__ . '/da_api_sign.php';
50:
51: $sDaURL = $this->oModuleSettings->DirectAdminURL;
52: $sDaAdminUser = $this->oModuleSettings->AdminUser;
53: $sDaAdminPassword = $this->oModuleSettings->AdminPassword;
54: if ($sDaAdminPassword && !\Aurora\System\Utils::IsEncryptedValue($sDaAdminPassword)) {
55: $this->setConfig('AdminPassword', \Aurora\System\Utils::EncryptValue($sDaAdminPassword));
56: $this->saveModuleConfig();
57: } else {
58: $sDaAdminPassword = \Aurora\System\Utils::DecryptValue($this->oModuleSettings->AdminPassword);
59: }
60: $iPos = strpos($sDaURL, '://');
61: $sDaFullURL = substr($sDaURL, 0, $iPos + 3) . $sDaAdminUser . ':' . $sDaAdminPassword . '@' . substr($sDaURL, $iPos + 3);
62: $this->oDAApi = new \DirectAdminSignAPI($sDaFullURL);
63: }
64:
65: /**
66: * Creates account with credentials specified in registration form
67: *
68: * @param array $aArgs New account credentials.
69: * @param mixed $mResult Is passed by reference.
70: */
71: public function onAfterSignup($aArgs, &$mResult)
72: {
73: if (isset($aArgs['Login']) && isset($aArgs['Password']) && !empty(trim($aArgs['Password'])) && !empty(trim($aArgs['Login']))) {
74: $sLogin = trim($aArgs['Login']);
75: $sPassword = trim($aArgs['Password']);
76: $sFriendlyName = isset($aArgs['Name']) ? trim($aArgs['Name']) : '';
77: $bSignMe = isset($aArgs['SignMe']) ? (bool) $aArgs['SignMe'] : false;
78: $iQuota = (int) $this->oModuleSettings->UserDefaultQuotaMB;
79:
80: $bPrevState = \Aurora\System\Api::skipCheckUserRole(true);
81: [$sUsername, $sDomain] = explode("@", $sLogin);
82: if (!empty($sDomain)) {
83: $aResult = array();
84: try {
85: $mResultDA = $this->oDAApi->CMD_API_POP("create", $sDomain, $sUsername, $sPassword, $sPassword, $iQuota, '');
86: parse_str(urldecode($mResultDA), $aResult);
87: \Aurora\System\Api::Log('API call result:\n' . $mResultDA, \Aurora\System\Enums\LogLevel::Full);
88: } catch(\Exception $oException) {
89: throw new \Aurora\System\Exceptions\ApiException(0, $oException, $oException->getMessage());
90: }
91: if (is_array($aResult) && isset($aResult['error']) && ($aResult['error'] != "1")) {
92: $iUserId = \Aurora\Modules\Core\Module::Decorator()->CreateUser(0, $sLogin);
93: $oUser = \Aurora\System\Api::getUserById((int) $iUserId);
94: try {
95: $oAccount = \Aurora\Modules\Mail\Module::Decorator()->CreateAccount($oUser->Id, $sFriendlyName, $sLogin, $sLogin, $sPassword);
96: if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) {
97: $iTime = $bSignMe ? 0 : time();
98: $sAuthToken = \Aurora\System\Api::UserSession()->Set(
99: [
100: 'token' => 'auth',
101: 'sign-me' => $bSignMe,
102: 'id' => $oAccount->IdUser,
103: 'account' => $oAccount->Id,
104: 'account_type' => $oAccount->getName()
105: ],
106: $iTime
107: );
108: $mResult = [\Aurora\System\Application::AUTH_TOKEN_KEY => $sAuthToken];
109: }
110: } catch (\Exception $oException) {
111: if ($oException instanceof \Aurora\Modules\Mail\Exceptions\Exception &&
112: $oException->getCode() === \Aurora\Modules\Mail\Enums\ErrorCodes::CannotLoginCredentialsIncorrect) {
113: \Aurora\Modules\Core\Module::Decorator()->DeleteUser($oUser->Id);
114: }
115: throw $oException;
116: }
117: } elseif (is_array($aResult) && isset($aResult['details'])) {
118: $bPrevState = \Aurora\System\Api::skipCheckUserRole(true);
119: \Aurora\System\Api::skipCheckUserRole($bPrevState);
120: throw new \Aurora\System\Exceptions\ApiException(0, null, trim(str_replace("<br>", "", $aResult['details'])));
121: }
122: }
123: \Aurora\System\Api::skipCheckUserRole($bPrevState);
124: }
125: return true; // break subscriptions to prevent account creation in other modules
126: }
127: }
128: