1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Passwd_Driver_Poppassd extends Passwd_Driver
15: {
16: 17: 18: 19: 20:
21: public function __construct($params = array())
22: {
23: $this->_params = array_merge(
24: array('host' => 'localhost',
25: 'port' => 106),
26: $params);
27: }
28:
29: 30: 31:
32: protected function _connect()
33: {
34: $this->_fp = fsockopen($this->_params['host'],
35: $this->_params['port'],
36: $errno,
37: $errstr,
38: 30);
39: if (!$this->_fp) {
40: throw new Passwd_Exception($errstr);
41: }
42:
43: $this->_getPrompt();
44: }
45:
46: 47: 48:
49: protected function _disconnect()
50: {
51: if (isset($this->_fp)) {
52: fputs($this->_fp, "quit\n");
53: fclose($this->_fp);
54: }
55: }
56:
57: 58: 59:
60: protected function _getPrompt()
61: {
62: $prompt = fgets($this->_fp, 4096);
63: if (!$prompt) {
64: throw new Passwd_Exception(_("No prompt returned from server."));
65: }
66:
67: if (!preg_match('/^[1-5][0-9][0-9]/', $prompt)) {
68: throw new Passwd_Exception($prompt);
69: }
70:
71:
72: $rc = substr($prompt, 0, 3);
73: if ($rc != '200' && $rc != '220' && $rc != '250' && $rc != '300' ) {
74: throw new Passwd_Exception($prompt);
75: }
76: }
77:
78: 79: 80:
81: protected function _sendCommand($cmd, $arg)
82: {
83: $line = $cmd . ' ' . $arg . "\n";
84: $res_fputs = fputs($this->_fp, $line);
85: if (!$res_fputs) {
86: throw new Passwd_Exception(_("Cannot send command to server."));
87: }
88: $this->_getPrompt();
89: }
90:
91: 92: 93: 94: 95: 96: 97: 98: 99:
100: public function changePassword($username, $old_password, $new_password)
101: {
102: $this->_connect();
103:
104: try {
105: $this->_sendCommand('user', $username);
106: } catch (Passwd_Exception $e) {
107: $this->_disconnect();
108: throw new Passwd_Exception(_("User not found") . ': ' . $e->getMessage());
109: }
110:
111: try {
112: $this->_sendCommand('pass', $old_password);
113: } catch (Passwd_Exception $e) {
114: $this->_disconnect();
115: throw new Passwd_Exception(_("Incorrect old password.") . ': ' . $e->getMessage());
116: }
117:
118: try {
119: $this->_sendCommand('newpass', $new_password);
120: } catch (Passwd_Exception $e) {
121: $this->_disconnect();
122: throw $e;
123: }
124: }
125: }
126: