Replacing tabs with spaces, trimming white space at EOL and newline at EOF
[squirrelmail.git] / plugins / change_password / backend / poppassd.php
1 <?php
2
3 /**
4 * Poppassd change password backend
5 *
6 * @author Seth Randall <sethr@missoulafcu.org>
7 * @version $Id$
8 * @package plugins
9 * @subpackage change_password
10 */
11
12 /**
13 * Config vars
14 */
15
16 /**
17 * Set the address of the server your poppass daemon runs on.
18 * If it's the same as your imap server, you can leave it blank
19 */
20 global $poppassd_server;
21
22 $poppassd_server = '';
23
24 /**
25 * Define here the name of your password changing function.
26 */
27 global $squirrelmail_plugin_hooks;
28 $squirrelmail_plugin_hooks['change_password_dochange']['poppassd'] = 'cpw_poppassd_dochange';
29 $squirrelmail_plugin_hooks['change_password_init']['template'] = 'cpw_poppassd_init';
30
31
32 /**
33 * Use this function to do any backend-specific initialization,
34 * e.g. checking requirements, before the password change form
35 * is displayed to the user.
36 */
37 function cpw_poppassd_init() {
38 }
39
40 /**
41 * This is the function that is specific to your backend. It takes
42 * the current password (as supplied by the user) and the desired
43 * new password. It will return an array of messages. If everything
44 * was successful, the array will be empty. Else, it will contain
45 * the errormessage(s).
46 * Constants to be used for these messages:
47 * CPW_CURRENT_NOMATCH -> "Your current password is not correct."
48 * CPW_INVALID_PW -> "Your new password contains invalid characters."
49 *
50 * @param array data The username/currentpw/newpw data.
51 * @return array Array of error messages.
52 */
53 function cpw_poppassd_dochange($data) {
54 // unfortunately, we can only pass one parameter to a hook function,
55 // so we have to pass it as an array.
56 $username = $data['username'];
57 $curpw = $data['curpw'];
58 $newpw = $data['newpw'];
59
60 $msgs = array();
61
62 // your code here to change the password for $username from
63 // $currentpw into $newpw.
64 $msgs = cpw_poppassd_go($username, $curpw, $newpw, 0);
65
66 return $msgs;
67 }
68
69 function cpw_poppassd_closeport($pop_socket, &$messages, $debug = 0) {
70 if ($debug) {
71 array_push($messages, _("Closing Connection"));
72 }
73 fputs($pop_socket, "quit\r\n");
74 fclose($pop_socket);
75 }
76
77 function cpw_poppassd_readfb($pop_socket, &$result, &$messages, $debug = 0) {
78 $strResp = '';
79 $result = '';
80
81 if (!feof($pop_socket)) {
82 $strResp = fgets($pop_socket, 1024);
83 $result = substr(trim($strResp), 0, 3); // 200, 500
84 if(!preg_match('/^[23]\d\d/', $result) || $debug) {
85 $messages[] = "--> $strResp";
86 }
87 }
88 }
89
90 function cpw_poppassd_go($username, $old_pw, $new_pw, $debug = 0) {
91 global $poppassd_server;
92 global $imapServerAddress;
93
94 if($poppassd_server == '') {
95 $poppassd_server = $imapServerAddress;
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 }
138
139 ?>