1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\MailChangePasswordPoppassdExtendedPlugin;
9:
10: use function Sabre\Uri\split;
11:
12: /**
13: * Allows users to change passwords on their email accounts using POPPASSD protocol.
14: *
15: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
16: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
17: * @copyright Copyright (c) 2023, Afterlogic Corp.
18: *
19: * @package Modules
20: */
21: class Module extends \Aurora\Modules\MailChangePasswordPoppassdPlugin\Module
22: {
23: public function init()
24: {
25: parent::init();
26: $this->subscribeEvent('Mail::ChangePassword::before', array($this, 'onBeforeChangePassword'));
27: $this->subscribeEvent('Core::Login::before', array($this, 'onBeforeLogin'));
28: }
29:
30: public function onBeforeChangePassword(&$aArgs, &$mResult)
31: {
32: if (!isset($aArgs['AccountId'])) {
33: $aUserInfo = \Aurora\System\Api::getAuthenticatedUserInfo(
34: \Aurora\System\Api::getAuthenticatedUserAuthToken()
35: );
36:
37: if (isset($aUserInfo['account'])) {
38: $aArgs['AccountId'] = (int) $aUserInfo['account'];
39: }
40: }
41: }
42:
43:
44: public function onBeforeLogin($aArgs, &$mResult, &$mSubResult)
45: {
46: if (null === $this->oPopPassD) {
47: $this->oPopPassD = new \Aurora\Modules\MailChangePasswordPoppassdPlugin\Poppassd(
48: $this->getConfig('Host', '127.0.0.1'),
49: $this->getConfig('Port', 106)
50: );
51: }
52:
53: if ($this->oPopPassD && $this->oPopPassD->Connect()) {
54: $sLogin = $aArgs['Login'];
55: $sPassword = $aArgs['Password'];
56: try {
57: $this->oPopPassD->SendLine('getuser '.$sLogin);
58:
59: if ($this->oPopPassD->CheckResponse($this->oPopPassD->ReadLine())) {
60: if ($this->oPopPassD->SendLine('pass '.$sPassword) && $this->oPopPassD->CheckResponse($this->oPopPassD->ReadLine())) {
61: while ($sLine = $this->oPopPassD->ReadLine()) {
62: $aLine = \explode(' ', $sLine);
63: if ($aLine[0] == 200) {
64: if (\count($aLine) === 3) {
65: $aResult[$aLine[1]] = \trim($aLine[2]);
66: }
67: if (\strtolower(\trim($aLine[1])) === 'complete.') {
68: break;
69: }
70: }
71: }
72:
73: $iExpire = isset($aResult['EXPIRE']) ? (int) $aResult['EXPIRE'] : 0;
74: $iCfgGrace = isset($aResult['CFGGRACE']) ? (int) $aResult['CFGGRACE'] : 0;
75: $iCfgWarn = isset($aResult['CFGWARN']) ? (int) $aResult['CFGWARN'] : 0;
76:
77: $mSubResult = [
78: 'CallHelpdesk' => ($iExpire < 0 && abs($iExpire) > $iCfgGrace) ? true : false,
79: 'ChangePassword' => ($iExpire <= $iCfgWarn) ? true : false,
80: 'DaysBeforeExpire' => $iExpire,
81: ];
82: }
83: }
84: } catch (\Exception $oException) {
85: $this->oPopPassD->Disconnect();
86: throw $oException;
87: }
88: }
89: }
90: }
91: