| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | namespace Aurora\Modules\MailChangePasswordPoppassdPlugin; |
| 9: | |
| 10: | use Aurora\Modules\Mail\Models\MailAccount; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | class Module extends \Aurora\System\Module\AbstractModule |
| 24: | { |
| 25: | |
| 26: | |
| 27: | |
| 28: | protected $oPopPassD; |
| 29: | |
| 30: | public function init() |
| 31: | { |
| 32: | $this->oPopPassD = null; |
| 33: | |
| 34: | $this->subscribeEvent('Mail::Account::ToResponseArray', array($this, 'onMailAccountToResponseArray')); |
| 35: | $this->subscribeEvent('ChangeAccountPassword', array($this, 'onChangeAccountPassword')); |
| 36: | } |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | public static function getInstance() |
| 42: | { |
| 43: | return parent::getInstance(); |
| 44: | } |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | public static function Decorator() |
| 50: | { |
| 51: | return parent::Decorator(); |
| 52: | } |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | public function getModuleSettings() |
| 58: | { |
| 59: | return $this->oModuleSettings; |
| 60: | } |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | public function onMailAccountToResponseArray($aArguments, &$mResult) |
| 68: | { |
| 69: | $oAccount = $aArguments['Account']; |
| 70: | |
| 71: | if ($oAccount && $this->checkCanChangePassword($oAccount)) { |
| 72: | if (!isset($mResult['Extend']) || !is_array($mResult['Extend'])) { |
| 73: | $mResult['Extend'] = []; |
| 74: | } |
| 75: | $mResult['Extend']['AllowChangePasswordOnMailServer'] = true; |
| 76: | } |
| 77: | } |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | public function onChangeAccountPassword($aArguments, &$mResult) |
| 85: | { |
| 86: | $bPasswordChanged = false; |
| 87: | $bBreakSubscriptions = false; |
| 88: | |
| 89: | $oAccount = $aArguments['Account'] instanceof MailAccount ? $aArguments['Account'] : false; |
| 90: | if ($oAccount && $this->checkCanChangePassword($oAccount) && $oAccount->getPassword() === $aArguments['CurrentPassword']) { |
| 91: | $bPasswordChanged = $this->changePassword($oAccount, $aArguments['NewPassword']); |
| 92: | $bBreakSubscriptions = true; |
| 93: | } |
| 94: | |
| 95: | if (is_array($mResult)) { |
| 96: | $mResult['AccountPasswordChanged'] = $mResult['AccountPasswordChanged'] || $bPasswordChanged; |
| 97: | } |
| 98: | |
| 99: | return $bBreakSubscriptions; |
| 100: | } |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | protected function checkCanChangePassword($oAccount) |
| 108: | { |
| 109: | $bFound = in_array('*', $this->oModuleSettings->SupportedServers); |
| 110: | |
| 111: | if (!$bFound) { |
| 112: | $oServer = $oAccount->getServer(); |
| 113: | |
| 114: | if ($oServer && in_array($oServer->IncomingServer, $this->oModuleSettings->SupportedServers)) { |
| 115: | $bFound = true; |
| 116: | } |
| 117: | } |
| 118: | |
| 119: | return $bFound; |
| 120: | } |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | protected function changePassword($oAccount, $sPassword) |
| 130: | { |
| 131: | $bResult = false; |
| 132: | |
| 133: | if (0 < strlen($oAccount->getPassword()) && $oAccount->getPassword() !== $sPassword) { |
| 134: | if (null === $this->oPopPassD) { |
| 135: | $this->oPopPassD = new Poppassd( |
| 136: | $this->oModuleSettings->Host, |
| 137: | $this->oModuleSettings->Port |
| 138: | ); |
| 139: | } |
| 140: | |
| 141: | if ($this->oPopPassD && $this->oPopPassD->Connect()) { |
| 142: | try { |
| 143: | if ($this->oPopPassD->Login($oAccount->IncomingLogin, $oAccount->getPassword())) { |
| 144: | $aNewPasswordResult = $this->oPopPassD->NewPass($sPassword); |
| 145: | if (!$aNewPasswordResult[0]) { |
| 146: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Exceptions\Errs::UserManager_AccountNewPasswordRejected, null, $aNewPasswordResult[1]); |
| 147: | } else { |
| 148: | $bResult = true; |
| 149: | } |
| 150: | } else { |
| 151: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Exceptions\Errs::UserManager_AccountOldPasswordNotCorrect); |
| 152: | } |
| 153: | } catch (\Exception $oException) { |
| 154: | $this->oPopPassD->Disconnect(); |
| 155: | throw $oException; |
| 156: | } |
| 157: | } else { |
| 158: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Exceptions\Errs::UserManager_AccountNewPasswordUpdateError); |
| 159: | } |
| 160: | } |
| 161: | |
| 162: | return $bResult; |
| 163: | } |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | public function GetSettings() |
| 170: | { |
| 171: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); |
| 172: | |
| 173: | $sSupportedServers = implode("\n", $this->oModuleSettings->SupportedServers); |
| 174: | |
| 175: | $aAppData = array( |
| 176: | 'SupportedServers' => $sSupportedServers, |
| 177: | 'Host' => $this->oModuleSettings->Host, |
| 178: | 'Port' => $this->oModuleSettings->Port, |
| 179: | ); |
| 180: | |
| 181: | return $aAppData; |
| 182: | } |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | public function UpdateSettings($SupportedServers, $Host, $Port) |
| 192: | { |
| 193: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); |
| 194: | |
| 195: | $aSupportedServers = preg_split('/\r\n|[\r\n]/', $SupportedServers); |
| 196: | |
| 197: | $this->setConfig('SupportedServers', $aSupportedServers); |
| 198: | $this->setConfig('Host', $Host); |
| 199: | $this->setConfig('Port', $Port); |
| 200: | |
| 201: | return $this->saveModuleConfig(); |
| 202: | } |
| 203: | } |
| 204: | |