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\MailChangePasswordPoppassdPlugin;
9:
10: /**
11: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
12: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
13: * @copyright Copyright (c) 2023, Afterlogic Corp.
14: *
15: * @package Api
16: * @subpackage Net
17: */
18: class Poppassd extends \Aurora\System\Net\AbstractProtocol
19: {
20: /**
21: * @return bool
22: */
23: public function Connect()
24: {
25: $bResult = false;
26: if (parent::Connect()) {
27: $bResult = $this->CheckResponse($this->GetNextLine(), 0);
28: }
29: return $bResult;
30: }
31:
32: /**
33: * @param string $sLogin
34: * @param string $sPassword
35: * @return bool
36: */
37: public function Login($sLogin, $sPassword)
38: {
39: return $this->SendCommand('user '.$sLogin, array(), 1) && $this->SendCommand('pass '.$sPassword, array($sPassword), 1);
40: }
41:
42: /**
43: * @param string $sLogin
44: * @param string $sPassword
45: * @return bool
46: */
47: public function ConnectAndLogin($sLogin, $sPassword)
48: {
49: return $this->Connect() && $this->Login($sLogin, $sPassword);
50: }
51:
52: /**
53: * @return bool
54: */
55: public function Disconnect()
56: {
57: return parent::Disconnect();
58: }
59:
60: /**
61: * @return bool
62: */
63: public function Logout()
64: {
65: return $this->SendCommand('quit', array(), 0);
66: }
67:
68: /**
69: * @return bool
70: */
71: public function LogoutAndDisconnect()
72: {
73: return $this->Logout() && $this->Disconnect();
74: }
75:
76: /**
77: * @param string $sNewPassword
78: * @return bool
79: */
80: public function NewPass($sNewPassword)
81: {
82: $bResult = false;
83: $sMessage = '';
84: $aLines = [];
85: if ($this->WriteLine('newpass '.$sNewPassword, array($sNewPassword))) {
86: while ($sLine = $this->GetNextLine()) {
87: $aLine = \explode(' ', $sLine);
88: if ($aLine[0] == 200) {
89: $bResult = true;
90: break;
91: }
92: if ($aLine[0] == 500) {
93: $bResult = false;
94: $aLines[] = substr($sLine, 4) ;
95: }
96: }
97: }
98: if (!$bResult) {
99: $sMessage = implode("\n", $aLines);
100: }
101:
102: return [$bResult, $sMessage];
103:
104: // return $this->SendCommand('newpass '.$sNewPassword, array($sNewPassword), 0);
105: }
106:
107: /**
108: * @param string $sCmd
109: * @return bool
110: */
111: public function SendLine($sCmd)
112: {
113: return $this->WriteLine($sCmd);
114: }
115:
116: /**
117: * @param string $sCmd
118: * @param array $aHideValues = array()
119: * @param int $iCheckType = 0
120: * @return bool
121: */
122: public function SendCommand($sCmd, $aHideValues = array(), $iCheckType = 0)
123: {
124: if ($this->WriteLine($sCmd, $aHideValues)) {
125: return $this->CheckResponse($this->GetNextLine(), $iCheckType);
126: }
127:
128: return false;
129: }
130:
131: /**
132: * @return string
133: */
134: public function GetNextLine()
135: {
136: return $this->ReadLine();
137: }
138:
139: /**
140: * @param string $sResponse
141: * @param int $iCheckType = 0
142: * @return bool
143: */
144: public function CheckResponse($sResponse, $iCheckType = 0)
145: {
146: switch ($iCheckType) {
147: case 0:
148: return (bool) preg_match('/^2\d\d/', $sResponse);
149: break;
150: case 1:
151: return (bool) preg_match('/^[23]\d\d/', $sResponse);
152: break;
153: }
154:
155: return false;
156: }
157: }
158: