1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | namespace Aurora\Modules\MailSignupPlesk; |
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 $oClient; |
28: | |
29: | public function init() |
30: | { |
31: | $this->subscribeEvent('MailSignup::Signup::before', [$this, 'onAfterSignup']); |
32: | |
33: | $sPleskHost = $this->oModuleSettings->PleskHostname; |
34: | $this->oClient = new \PleskX\Api\Client($sPleskHost); |
35: | |
36: | $sPleskUser = $this->oModuleSettings->PleskAdminUser; |
37: | $sPleskPass = $this->oModuleSettings->PleskAdminPassword; |
38: | |
39: | if ($sPleskPass && !\Aurora\System\Utils::IsEncryptedValue($sPleskPass)) { |
40: | $this->setConfig('PleskAdminPassword', \Aurora\System\Utils::EncryptValue($sPleskPass)); |
41: | $this->saveModuleConfig(); |
42: | } else { |
43: | $sPleskPass = \Aurora\System\Utils::DecryptValue($sPleskPass); |
44: | } |
45: | |
46: | $this->oClient->setCredentials($sPleskUser, $sPleskPass); |
47: | } |
48: | |
49: | |
50: | |
51: | |
52: | public static function getInstance() |
53: | { |
54: | return parent::getInstance(); |
55: | } |
56: | |
57: | |
58: | |
59: | |
60: | public static function Decorator() |
61: | { |
62: | return parent::Decorator(); |
63: | } |
64: | |
65: | |
66: | |
67: | |
68: | public function getModuleSettings() |
69: | { |
70: | return $this->oModuleSettings; |
71: | } |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | public function onAfterSignup($aArgs, &$mResult) |
80: | { |
81: | if (isset($aArgs['Login']) && isset($aArgs['Password']) && !empty(trim($aArgs['Password'])) && !empty(trim($aArgs['Login']))) { |
82: | $sLogin = trim($aArgs['Login']); |
83: | $sPassword = trim($aArgs['Password']); |
84: | $sFriendlyName = isset($aArgs['Name']) ? trim($aArgs['Name']) : ''; |
85: | $bSignMe = isset($aArgs['SignMe']) ? (bool) $aArgs['SignMe'] : false; |
86: | |
87: | $bPrevState = \Aurora\System\Api::skipCheckUserRole(true); |
88: | [$sUsername, $sDomain] = explode("@", $sLogin); |
89: | try { |
90: | $mResult = $this->oClient->site()->get("name", $sDomain); |
91: | } catch (\Exception $oException) { |
92: | throw new \Aurora\System\Exceptions\ApiException(0, null, $oException->getMessage()); |
93: | } |
94: | |
95: | if (is_object($mResult) && isset($mResult->id) && is_numeric($mResult->id)) { |
96: | $iSiteId = intval($mResult->id); |
97: | $aResult = array(); |
98: | try { |
99: | $mResult2 = $this->oClient->mail()->create($sUsername, $iSiteId, true, $sPassword); |
100: | } catch(\Exception $oException) { |
101: | throw new \Aurora\System\Exceptions\ApiException(0, $oException, $oException->getMessage()); |
102: | } |
103: | $iUserId = null; |
104: | try { |
105: | $iUserId = \Aurora\Modules\Core\Module::Decorator()->CreateUser(0, $sLogin); |
106: | $oUser = \Aurora\System\Api::getUserById((int) $iUserId); |
107: | $oAccount = \Aurora\Modules\Mail\Module::Decorator()->CreateAccount($oUser->Id, $sFriendlyName, $sLogin, $sLogin, $sPassword); |
108: | if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) { |
109: | $iTime = $bSignMe ? 0 : time(); |
110: | $sAuthToken = \Aurora\System\Api::UserSession()->Set( |
111: | [ |
112: | 'token' => 'auth', |
113: | 'sign-me' => $bSignMe, |
114: | 'id' => $oAccount->IdUser, |
115: | 'account' => $oAccount->Id, |
116: | 'account_type' => $oAccount->getName() |
117: | ], |
118: | $iTime |
119: | ); |
120: | $mResult = [\Aurora\System\Application::AUTH_TOKEN_KEY => $sAuthToken]; |
121: | } |
122: | } catch (\Exception $oException) { |
123: | if ($oException instanceof \Aurora\Modules\Mail\Exceptions\Exception && |
124: | $oException->getCode() === \Aurora\Modules\Mail\Enums\ErrorCodes::CannotLoginCredentialsIncorrect && |
125: | is_int($iUserId) && ($iUserId > 0)) { |
126: | \Aurora\Modules\Core\Module::Decorator()->DeleteUser($iUserId); |
127: | } |
128: | throw $oException; |
129: | } |
130: | } else { |
131: | throw new \Aurora\System\Exceptions\ApiException(0, null, "Site not found"); |
132: | } |
133: | \Aurora\System\Api::skipCheckUserRole($bPrevState); |
134: | } |
135: | return true; |
136: | } |
137: | } |
138: | |