Thomas Bruederli
2013-06-20 07c6c69eca8751c0e96a846afb30c24ab2638b1f
commit | author | age
48e9c1 1 <?php
T 2 /**
3  * XMail Password Driver
4  *
5  * Driver for XMail password
6  *
7  * @version 2.0
8  * @author Helio Cavichiolo Jr <helio@hcsistemas.com.br>
9  *
10  * Setup xmail_host, xmail_user, xmail_pass and xmail_port into
11  * config.inc.php of password plugin as follows:
12  *
13  * $rcmail_config['xmail_host'] = 'localhost';
14  * $rcmail_config['xmail_user'] = 'YourXmailControlUser';
15  * $rcmail_config['xmail_pass'] = 'YourXmailControlPass';
16  * $rcmail_config['xmail_port'] = 6017;
17  *
18  */
19
20 class rcube_xmail_password
21 {
22     function save($currpass, $newpass)
23     {
24         $rcmail = rcmail::get_instance();
25         list($user,$domain) = explode('@', $_SESSION['username']);
26
27         $xmail = new XMail;
28
29         $xmail->hostname = $rcmail->config->get('xmail_host');
30         $xmail->username = $rcmail->config->get('xmail_user');
31         $xmail->password = $rcmail->config->get('xmail_pass');
32         $xmail->port = $rcmail->config->get('xmail_port');
33
34         if (!$xmail->connect()) {
61be82 35             rcube::raise_error(array(
48e9c1 36                 'code' => 600,
T 37                 'type' => 'php',
38                 'file' => __FILE__, 'line' => __LINE__,
39                 'message' => "Password plugin: Unable to connect to mail server"
40             ), true, false);
41             return PASSWORD_CONNECT_ERROR;
42         }
43         else if (!$xmail->send("userpasswd\t".$domain."\t".$user."\t".$newpass."\n")) {
44             $xmail->close();
61be82 45             rcube::raise_error(array(
48e9c1 46                 'code' => 600,
T 47                 'type' => 'php',
48                 'file' => __FILE__, 'line' => __LINE__,
49                 'message' => "Password plugin: Unable to change password"
50             ), true, false);
51             return PASSWORD_ERROR;
52         }
53         else {
54             $xmail->close();
55             return PASSWORD_SUCCESS;
56         }
57     }
58 }
59
60 class XMail {
61     var $socket;
62     var $hostname = 'localhost';
63     var $username = 'xmail';
64     var $password = '';
65     var $port = 6017;
66
67     function send($msg)
68     {
69         socket_write($this->socket,$msg);
2193f6 70         if (substr(socket_read($this->socket, 512, PHP_BINARY_READ),0,1) != "+") {
48e9c1 71             return false;
T 72         }
73         return true;
74     }
75
76     function connect()
77     {
78         $this->socket = socket_create(AF_INET, SOCK_STREAM, 0);
79         if ($this->socket < 0)
80             return false;
81
82         $result = socket_connect($this->socket, $this->hostname, $this->port);
83         if ($result < 0) {
84             socket_close($this->socket);
85             return false;
86         }
87
2193f6 88         if (substr(socket_read($this->socket, 512, PHP_BINARY_READ),0,1) != "+") {
48e9c1 89             socket_close($this->socket);
T 90             return false;
91         }
92
93         if (!$this->send("$this->username\t$this->password\n")) {
94             socket_close($this->socket);
95             return false;
96         }
97         return true;
98     }
99
100     function close()
101     {
102         $this->send("quit\n");
103         socket_close($this->socket);
104     }
105 }
106