30a4bda7 |
1 | <?php |
2 | |
3 | /** |
30a4bda7 |
4 | * Poppassd change password backend |
21b8ca51 |
5 | * |
6 | * @author Seth Randall <sethr@missoulafcu.org> |
7 | * @version $Id$ |
8 | * @package plugins |
9 | * @subpackage change_password |
30a4bda7 |
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 | |
76063016 |
24 | /* get overrides from config.php */ |
25 | if (isset($cpw_poppassd['server'])) $poppassd_server=$cpw_poppassd['server']; |
26 | |
30a4bda7 |
27 | /** |
28 | * Define here the name of your password changing function. |
29 | */ |
30 | global $squirrelmail_plugin_hooks; |
31 | $squirrelmail_plugin_hooks['change_password_dochange']['poppassd'] = 'cpw_poppassd_dochange'; |
30a4bda7 |
32 | |
33 | /** |
34 | * This is the function that is specific to your backend. It takes |
35 | * the current password (as supplied by the user) and the desired |
36 | * new password. It will return an array of messages. If everything |
37 | * was successful, the array will be empty. Else, it will contain |
38 | * the errormessage(s). |
39 | * Constants to be used for these messages: |
40 | * CPW_CURRENT_NOMATCH -> "Your current password is not correct." |
41 | * CPW_INVALID_PW -> "Your new password contains invalid characters." |
42 | * |
91e0dccc |
43 | * @param array data The username/currentpw/newpw data. |
30a4bda7 |
44 | * @return array Array of error messages. |
45 | */ |
46 | function cpw_poppassd_dochange($data) { |
47 | // unfortunately, we can only pass one parameter to a hook function, |
48 | // so we have to pass it as an array. |
49 | $username = $data['username']; |
50 | $curpw = $data['curpw']; |
51 | $newpw = $data['newpw']; |
52 | |
53 | $msgs = array(); |
54 | |
55 | // your code here to change the password for $username from |
56 | // $currentpw into $newpw. |
57 | $msgs = cpw_poppassd_go($username, $curpw, $newpw, 0); |
58 | |
59 | return $msgs; |
60 | } |
61 | |
62 | function cpw_poppassd_closeport($pop_socket, &$messages, $debug = 0) { |
63 | if ($debug) { |
64 | array_push($messages, _("Closing Connection")); |
65 | } |
66 | fputs($pop_socket, "quit\r\n"); |
67 | fclose($pop_socket); |
68 | } |
69 | |
70 | function cpw_poppassd_readfb($pop_socket, &$result, &$messages, $debug = 0) { |
71 | $strResp = ''; |
72 | $result = ''; |
73 | |
74 | if (!feof($pop_socket)) { |
75 | $strResp = fgets($pop_socket, 1024); |
76 | $result = substr(trim($strResp), 0, 3); // 200, 500 |
77 | if(!preg_match('/^[23]\d\d/', $result) || $debug) { |
78 | $messages[] = "--> $strResp"; |
79 | } |
80 | } |
81 | } |
82 | |
83 | function cpw_poppassd_go($username, $old_pw, $new_pw, $debug = 0) { |
84 | global $poppassd_server; |
85 | global $imapServerAddress; |
86 | |
4d2ff38f |
87 | /** @ignore */ |
88 | if (!defined('SM_PATH')) define('SM_PATH','../../../'); |
89 | /** sqimap_get_user_server() function */ |
90 | include_once(SM_PATH . 'functions/imap_general.php'); |
91 | |
30a4bda7 |
92 | if($poppassd_server == '') { |
4d2ff38f |
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); |
30a4bda7 |
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) { |
723ab15e |
105 | $messages[] = _("ERROR") . ': ' . "$errstr ($errno)"; |
30a4bda7 |
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 | |
91e0dccc |
139 | ?> |