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