1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | namespace Aurora\Modules\MailSignupDirectadmin; |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | class Module extends \Aurora\System\Module\AbstractModule |
23: | { |
24: | |
25: | |
26: | |
27: | private $oDAApi; |
28: | |
29: | |
30: | |
31: | |
32: | public static function getInstance() |
33: | { |
34: | return parent::getInstance(); |
35: | } |
36: | |
37: | |
38: | |
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: | |
67: | |
68: | |
69: | |
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; |
126: | } |
127: | } |
128: | |