1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Passwd_Driver_Servuftp extends Passwd_Driver
15: {
16: const CONNECTED = '220';
17: const GOODBYE = '221';
18: const PASSWORDOK = '230';
19: const USERNAMEOK = '331';
20: const PASSWORDBAD = '530';
21:
22: protected $_fp;
23:
24: 25: 26: 27: 28: 29: 30:
31: public function __construct($params = array())
32: {
33: if (empty($params['host']) || empty($params['port'])) {
34: throw new Passwd_Exception(_("Password module is missing required parameters."));
35: }
36: parent::__construct(array_merge(array('timeout' => 30), $params));
37: }
38:
39: 40: 41: 42: 43: 44: 45: 46: 47:
48: protected function changePassword($user_name, $old_password, $new_password)
49: {
50: if ($this->_connect() != self::CONNECTED) {
51: throw new Passwd_Exception(_("Connection failed"));
52: }
53: if ($this->_sendCommand('user', $user_name) != self::USERNAMEOK) {
54: $this->_disconnect();
55: throw new Passwd_Exception(_("Unknown user"));
56: }
57: if ($this->_sendCommand('pass', $old_password) != self::PASSWORDOK) {
58: $this->_disconnect();
59: throw new Passwd_Exception(_("Incorrect password"));
60: }
61: if ($this->_sendCommand('site pswd', '"' . $old_password . '" "' . $new_password . '"') != self::PASSWORDOK) {
62: $this->_disconnect();
63: throw new Passwd_Exception(_("Cannot change password"));
64: }
65: $this->_disconnect();
66: }
67:
68: protected function _connect()
69: {
70: $this->_fp = fsockopen($this->_params['host'], $this->_params['port'],
71: $errno, $errstr, $this->_params['timeout']);
72: if (!$this->_fp) {
73: throw new Passwd_Exception($errstr);
74: }
75: return $this->_getPrompt();
76: }
77:
78: protected function _disconnect()
79: {
80: if ($this->_fp) {
81: fputs($this->_fp, "quit\n");
82: fclose($this->_fp);
83: }
84: }
85:
86: protected function _getPrompt()
87: {
88: $prompt = fgets($this->_fp, 4096);
89:
90: if (preg_match('/^[1-5][0-9][0-9]/', $prompt, $res)) {
91: return $res[1];
92: }
93: }
94:
95: protected function _sendCommand($cmd, $arg)
96: {
97: $line = $cmd . ' ' . $arg . "\r\n";
98: fputs($this->_fp, $line);
99: return $this->_getPrompt();
100: }
101: }
102: