023c88d7befd5b193836e998966de8e85cc8a6ee
[squirrelmail.git] / plugins / change_password / backend / poppassd.php
1 <?php
2
3 /**
4 * Poppassd change password backend
5 *
6 * @author Seth Randall <sethr at missoulafcu.org>
7 * @copyright 2004-2011 The SquirrelMail Project Team
8 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 * @version $Id$
10 * @package plugins
11 * @subpackage change_password
12 */
13
14 /**
15 * Config vars
16 */
17
18 /**
19 * Set the address of the server your poppass daemon runs on.
20 * If it's the same as your imap server, you can leave it blank
21 */
22 global $poppassd_server;
23
24 $poppassd_server = '';
25
26 /* get overrides from config.php */
27 if (isset($cpw_poppassd['server'])) $poppassd_server=$cpw_poppassd['server'];
28
29 /**
30 * Define here the name of your password changing function.
31 */
32 global $squirrelmail_plugin_hooks;
33 $squirrelmail_plugin_hooks['change_password_dochange']['poppassd'] = 'cpw_poppassd_dochange';
34
35 /**
36 * This is the function that is specific to your backend. It takes
37 * the current password (as supplied by the user) and the desired
38 * new password. It will return an array of messages. If everything
39 * was successful, the array will be empty. Else, it will contain
40 * the errormessage(s).
41 * Constants to be used for these messages:
42 * CPW_CURRENT_NOMATCH -> "Your current password is not correct."
43 * CPW_INVALID_PW -> "Your new password contains invalid characters."
44 *
45 * @param array data The username/currentpw/newpw data.
46 * @return array Array of error messages.
47 */
48 function cpw_poppassd_dochange($data) {
49 // unfortunately, we can only pass one parameter to a hook function,
50 // so we have to pass it as an array.
51 $username = $data['username'];
52 $curpw = $data['curpw'];
53 $newpw = $data['newpw'];
54
55 $msgs = array();
56
57 // your code here to change the password for $username from
58 // $currentpw into $newpw.
59 $msgs = cpw_poppassd_go($username, $curpw, $newpw, 0);
60
61 return $msgs;
62 }
63
64 function cpw_poppassd_closeport($pop_socket, &$messages, $debug = 0) {
65 if ($debug) {
66 array_push($messages, _("Closing Connection"));
67 }
68 fputs($pop_socket, "quit\r\n");
69 fclose($pop_socket);
70 }
71
72 function cpw_poppassd_readfb($pop_socket, &$result, &$messages, $debug = 0) {
73 $strResp = '';
74 $result = '';
75
76 if (!feof($pop_socket)) {
77 $strResp = fgets($pop_socket, 1024);
78 $result = substr(trim($strResp), 0, 3); // 200, 500
79 if(!preg_match('/^[23]\d\d/', $result) || $debug) {
80 $messages[] = "--> $strResp";
81 }
82 }
83 }
84
85 function cpw_poppassd_go($username, $old_pw, $new_pw, $debug = 0) {
86 global $poppassd_server;
87 global $imapServerAddress;
88
89 /** sqimap_get_user_server() function */
90 include_once(SM_PATH . 'functions/imap_general.php');
91
92 if($poppassd_server == '') {
93 // if poppassd address is not set, use imap server's address
94 // make sure that setting contains address and not mapping
95 $poppassd_server = sqimap_get_user_server($imapServerAddress,$username);
96 }
97
98 $messages = array();
99
100 if ($debug) {
101 $messages[] = _("Connecting to Password Server");
102 }
103 $pop_socket = fsockopen($poppassd_server, 106, $errno, $errstr);
104 if (!$pop_socket) {
105 $messages[] = _("ERROR") . ': ' . "$errstr ($errno)";
106 return $messages;
107 }
108
109 cpw_poppassd_readfb($pop_socket, $result, $messages, $debug);
110 if(!preg_match('/^2\d\d/', $result) ) {
111 cpw_poppassd_closeport($pop_socket, $messages, $debug);
112 return $messages;
113 }
114
115 fputs($pop_socket, "user $username\r\n");
116 cpw_poppassd_readfb($pop_socket, $result, $messages, $debug);
117 if(!preg_match('/^[23]\d\d/', $result) ) {
118 cpw_poppassd_closeport($pop_socket, $messages, $debug);
119 return $messages;
120 }
121
122 fputs($pop_socket, "pass $old_pw\r\n");
123 cpw_poppassd_readfb($pop_socket, $result, $messages, $debug);
124 if(!preg_match('/^[23]\d\d/', $result) ) {
125 cpw_poppassd_closeport($pop_socket, $messages, $debug);
126 return $messages;
127 }
128
129 fputs($pop_socket, "newpass $new_pw\r\n");
130 cpw_poppassd_readfb($pop_socket, $result, $messages, $debug);
131 cpw_poppassd_closeport($pop_socket, $messages, $debug);
132 if(!preg_match('/^2\d\d/', $result) ) {
133 return $messages;
134 }
135
136 return $messages;
137 }