| 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: | class Module extends \Aurora\System\Module\AbstractModule |
| 21: | { |
| 22: | |
| 23: | |
| 24: | |
| 25: | private $oClient; |
| 26: | |
| 27: | public function init() |
| 28: | { |
| 29: | $this->subscribeEvent('MailSignup::Signup::before', [$this, 'onAfterSignup']); |
| 30: | |
| 31: | $sPleskHost = $this->getConfig('PleskHostname', 'localhost'); |
| 32: | $this->oClient = new \PleskX\Api\Client($sPleskHost); |
| 33: | |
| 34: | $sPleskUser = $this->getConfig('PleskAdminUser', ''); |
| 35: | $sPleskPass = $this->getConfig('PleskAdminPassword', ''); |
| 36: | $this->oClient->setCredentials($sPleskUser, $sPleskPass); |
| 37: | } |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | public function onAfterSignup($aArgs, &$mResult) |
| 46: | { |
| 47: | if (isset($aArgs['Login']) && isset($aArgs['Password']) && !empty(trim($aArgs['Password'])) && !empty(trim($aArgs['Login']))) { |
| 48: | $sLogin = trim($aArgs['Login']); |
| 49: | $sPassword = trim($aArgs['Password']); |
| 50: | $sFriendlyName = isset($aArgs['Name']) ? trim($aArgs['Name']) : ''; |
| 51: | $bSignMe = isset($aArgs['SignMe']) ? (bool) $aArgs['SignMe'] : false; |
| 52: | |
| 53: | $bPrevState = \Aurora\System\Api::skipCheckUserRole(true); |
| 54: | [$sUsername, $sDomain] = explode("@", $sLogin); |
| 55: | try { |
| 56: | $mResult = $this->oClient->site()->get("name", $sDomain); |
| 57: | } catch (\Exception $oException) { |
| 58: | throw new \Aurora\System\Exceptions\ApiException(0, null, $oException->getMessage()); |
| 59: | } |
| 60: | |
| 61: | if (is_object($mResult) && isset($mResult->id) && is_numeric($mResult->id)) { |
| 62: | $iSiteId = intval($mResult->id); |
| 63: | $aResult = array(); |
| 64: | try { |
| 65: | $mResult2 = $this->oClient->mail()->create($sUsername, $iSiteId, true, $sPassword); |
| 66: | } catch(\Exception $oException) { |
| 67: | throw new \Aurora\System\Exceptions\ApiException(0, $oException, $oException->getMessage()); |
| 68: | } |
| 69: | try { |
| 70: | $iUserId = \Aurora\Modules\Core\Module::Decorator()->CreateUser(0, $sLogin); |
| 71: | $oUser = \Aurora\System\Api::getUserById((int) $iUserId); |
| 72: | $oAccount = \Aurora\Modules\Mail\Module::Decorator()->CreateAccount($oUser->Id, $sFriendlyName, $sLogin, $sLogin, $sPassword); |
| 73: | if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) { |
| 74: | $iTime = $bSignMe ? 0 : time(); |
| 75: | $sAuthToken = \Aurora\System\Api::UserSession()->Set( |
| 76: | [ |
| 77: | 'token' => 'auth', |
| 78: | 'sign-me' => $bSignMe, |
| 79: | 'id' => $oAccount->IdUser, |
| 80: | 'account' => $oAccount->Id, |
| 81: | 'account_type' => $oAccount->getName() |
| 82: | ], |
| 83: | $iTime |
| 84: | ); |
| 85: | $mResult = ['AuthToken' => $sAuthToken]; |
| 86: | } |
| 87: | } catch (\Exception $oException) { |
| 88: | if ($oException instanceof \Aurora\Modules\Mail\Exceptions\Exception && |
| 89: | $oException->getCode() === \Aurora\Modules\Mail\Enums\ErrorCodes::CannotLoginCredentialsIncorrect) { |
| 90: | \Aurora\Modules\Core\Module::Decorator()->DeleteUser($oUser->Id); |
| 91: | } |
| 92: | throw $oException; |
| 93: | } |
| 94: | } else { |
| 95: | throw new \Aurora\System\Exceptions\ApiException(0, null, "Site not found"); |
| 96: | } |
| 97: | \Aurora\System\Api::skipCheckUserRole($bPrevState); |
| 98: | } |
| 99: | return true; |
| 100: | } |
| 101: | } |
| 102: | |