abdfb4d0 |
1 | <?php |
895905c0 |
2 | |
35586184 |
3 | /** |
4 | * addressbook.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * Manage personal address book. |
10 | * |
11 | * $Id$ |
8f6f9ba5 |
12 | * @package squirrelmail |
35586184 |
13 | */ |
14 | |
8f6f9ba5 |
15 | /** Path for SquirrelMail required files. */ |
86725763 |
16 | define('SM_PATH','../'); |
17 | |
8f6f9ba5 |
18 | /** SquirrelMail required files. */ |
08185f2a |
19 | require_once(SM_PATH . 'include/validate.php'); |
1e12d1ff |
20 | require_once(SM_PATH . 'functions/global.php'); |
86725763 |
21 | require_once(SM_PATH . 'functions/display_messages.php'); |
22 | require_once(SM_PATH . 'functions/addressbook.php'); |
23 | require_once(SM_PATH . 'functions/strings.php'); |
24 | require_once(SM_PATH . 'functions/html.php'); |
ffd8224c |
25 | |
8f6f9ba5 |
26 | /** lets get the global vars we may need */ |
1e12d1ff |
27 | sqgetGlobalVar('key', $key, SQ_COOKIE); |
0b97a708 |
28 | |
1e12d1ff |
29 | sqgetGlobalVar('username', $username, SQ_SESSION); |
30 | sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION); |
31 | sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION); |
32 | sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); |
0b97a708 |
33 | |
34 | /* From the address form */ |
1e12d1ff |
35 | sqgetGlobalVar('addaddr', $addaddr, SQ_POST); |
36 | sqgetGlobalVar('editaddr', $editaddr, SQ_POST); |
37 | sqgetGlobalVar('deladdr', $deladdr, SQ_POST); |
38 | sqgetGlobalVar('sel', $sel, SQ_POST); |
39 | sqgetGlobalVar('oldnick', $oldnick, SQ_POST); |
40 | sqgetGlobalVar('backend', $backend, SQ_POST); |
41 | sqgetGlobalVar('doedit', $doedit, SQ_POST); |
0b97a708 |
42 | |
8f6f9ba5 |
43 | /** |
44 | * Make an input field |
45 | * @param string $label |
46 | * @param string $field |
47 | * @param string $name |
48 | * @param string $size |
49 | * @param array $values |
50 | * @param string $add |
51 | */ |
daba719e |
52 | function adressbook_inp_field($label, $field, $name, $size, $values, $add) { |
53 | global $color; |
ac987a56 |
54 | $td_str = '<INPUT NAME="' . $name . '[' . $field . ']" SIZE="' . $size . '" VALUE="'; |
daba719e |
55 | if (isset($values[$field])) { |
c6554ec0 |
56 | $td_str .= htmlspecialchars( strip_tags( $values[$field] ) ); |
ffd8224c |
57 | } |
ac987a56 |
58 | $td_str .= '">' . $add . ''; |
59 | return html_tag( 'tr' , |
60 | html_tag( 'td', $label . ':', 'right', $color[4]) . |
61 | html_tag( 'td', $td_str, 'left', $color[4]) |
62 | ) |
63 | . "\n"; |
daba719e |
64 | } |
ffd8224c |
65 | |
8f6f9ba5 |
66 | /** |
67 | * Output form to add and modify address data |
68 | */ |
daba719e |
69 | function address_form($name, $submittext, $values = array()) { |
e842b215 |
70 | global $color, $squirrelmail_language; |
c6554ec0 |
71 | |
e842b215 |
72 | if ($squirrelmail_language == 'ja_JP') |
73 | { |
74 | echo html_tag( 'table', |
75 | adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, |
76 | ' <SMALL>' . _("Must be unique") . '</SMALL>') . |
77 | adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') . |
78 | adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') . |
79 | adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') . |
80 | adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') . |
81 | html_tag( 'tr', |
82 | html_tag( 'td', |
83 | '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' . |
84 | $submittext . '">', |
85 | 'center', $color[4], 'colspan="2"') |
86 | ) |
87 | , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n"; |
88 | } else { |
ac987a56 |
89 | echo html_tag( 'table', |
90 | adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, |
c6554ec0 |
91 | ' <SMALL>' . _("Must be unique") . '</SMALL>') . |
ac987a56 |
92 | adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') . |
93 | adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') . |
94 | adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') . |
95 | adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') . |
96 | html_tag( 'tr', |
97 | html_tag( 'td', |
98 | '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' . |
99 | $submittext . '">', |
100 | 'center', $color[4], 'colspan="2"') |
101 | ) |
ac50138c |
102 | , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n"; |
daba719e |
103 | } |
e842b215 |
104 | } |
ffd8224c |
105 | |
f6c945b9 |
106 | /* Open addressbook, with error messages on but without LDAP (the * |
107 | * second "true"). Don't need LDAP here anyway */ |
daba719e |
108 | $abook = addressbook_init(true, true); |
109 | if($abook->localbackend == 0) { |
110 | plain_error_message( |
111 | _("No personal address book is defined. Contact administrator."), |
112 | $color); |
113 | exit(); |
114 | } |
ffd8224c |
115 | |
daba719e |
116 | displayPageHeader($color, 'None'); |
ffd8224c |
117 | |
daba719e |
118 | $defdata = array(); |
119 | $formerror = ''; |
120 | $abortform = false; |
121 | $showaddrlist = true; |
122 | $defselected = array(); |
07dcee9f |
123 | $form_url = 'addressbook.php'; |
daba719e |
124 | |
125 | |
f6c945b9 |
126 | /* Handle user's actions */ |
1e12d1ff |
127 | if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') { |
daba719e |
128 | |
f6c945b9 |
129 | /************************************************** |
130 | * Add new address * |
131 | **************************************************/ |
39b26252 |
132 | if (isset($addaddr)) { |
c6554ec0 |
133 | foreach( $addaddr as $k => $adr ) { |
134 | $addaddr[$k] = strip_tags( $adr ); |
135 | } |
ffd8224c |
136 | $r = $abook->add($addaddr, $abook->localbackend); |
137 | |
f6c945b9 |
138 | /* Handle error messages */ |
139 | if (!$r) { |
140 | /* Remove backend name from error string */ |
ffd8224c |
141 | $errstr = $abook->error; |
142 | $errstr = ereg_replace('^\[.*\] *', '', $errstr); |
143 | |
144 | $formerror = $errstr; |
145 | $showaddrlist = false; |
146 | $defdata = $addaddr; |
147 | } |
daba719e |
148 | } else { |
ffd8224c |
149 | |
f6c945b9 |
150 | /************************************************ |
151 | * Delete address(es) * |
152 | ************************************************/ |
153 | if ((!empty($deladdr)) && sizeof($sel) > 0) { |
daba719e |
154 | $orig_sel = $sel; |
155 | sort($sel); |
156 | |
f6c945b9 |
157 | /* The selected addresses are identidied by "backend:nickname". * |
158 | * Sort the list and process one backend at the time */ |
daba719e |
159 | $prevback = -1; |
160 | $subsel = array(); |
161 | $delfailed = false; |
162 | |
f6c945b9 |
163 | for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) { |
daba719e |
164 | list($sbackend, $snick) = explode(':', $sel[$i]); |
165 | |
f6c945b9 |
166 | /* When we get to a new backend, process addresses in * |
167 | * previous one. */ |
168 | if ($prevback != $sbackend && $prevback != -1) { |
daba719e |
169 | |
170 | $r = $abook->remove($subsel, $prevback); |
f6c945b9 |
171 | if (!$r) { |
daba719e |
172 | $formerror = $abook->error; |
173 | $i = sizeof($sel); |
174 | $delfailed = true; |
175 | break; |
176 | } |
177 | $subsel = array(); |
178 | } |
179 | |
f6c945b9 |
180 | /* Queue for processing */ |
daba719e |
181 | array_push($subsel, $snick); |
182 | $prevback = $sbackend; |
ffd8224c |
183 | } |
ffd8224c |
184 | |
f6c945b9 |
185 | if (!$delfailed) { |
daba719e |
186 | $r = $abook->remove($subsel, $prevback); |
f6c945b9 |
187 | if (!$r) { /* Handle errors */ |
daba719e |
188 | $formerror = $abook->error; |
189 | $delfailed = true; |
190 | } |
ffd8224c |
191 | } |
ffd8224c |
192 | |
f6c945b9 |
193 | if ($delfailed) { |
daba719e |
194 | $showaddrlist = true; |
195 | $defselected = $orig_sel; |
ffd8224c |
196 | } |
ffd8224c |
197 | |
daba719e |
198 | } else { |
199 | |
f6c945b9 |
200 | /*********************************************** |
201 | * Update/modify address * |
202 | ***********************************************/ |
203 | if (!empty($editaddr)) { |
daba719e |
204 | |
f6c945b9 |
205 | /* Stage one: Copy data into form */ |
daba719e |
206 | if (isset($sel) && sizeof($sel) > 0) { |
207 | if(sizeof($sel) > 1) { |
208 | $formerror = _("You can only edit one address at the time"); |
209 | $showaddrlist = true; |
210 | $defselected = $sel; |
211 | } else { |
212 | $abortform = true; |
213 | list($ebackend, $enick) = explode(':', $sel[0]); |
214 | $olddata = $abook->lookup($enick, $ebackend); |
215 | |
f6c945b9 |
216 | /* Display the "new address" form */ |
07dcee9f |
217 | echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' . |
f6c945b9 |
218 | "\n" . |
ac987a56 |
219 | html_tag( 'table', |
220 | html_tag( 'tr', |
221 | html_tag( 'td', |
222 | "\n". '<strong>' . _("Update address") . '</strong>' ."\n", |
223 | 'center', $color[0] ) |
224 | ), |
bd9c880b |
225 | 'center', '', 'width="100%" ' ); |
daba719e |
226 | address_form("editaddr", _("Update address"), $olddata); |
f6c945b9 |
227 | echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' . |
228 | htmlspecialchars($olddata["nickname"]) . "\">\n" . |
229 | '<INPUT TYPE=hidden NAME=backend VALUE="' . |
230 | htmlspecialchars($olddata["backend"]) . "\">\n" . |
231 | '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" . |
232 | '</FORM>'; |
daba719e |
233 | } |
234 | } else { |
235 | |
f6c945b9 |
236 | /* Stage two: Write new data */ |
237 | if ($doedit = 1) { |
daba719e |
238 | $newdata = $editaddr; |
239 | $r = $abook->modify($oldnick, $newdata, $backend); |
240 | |
f6c945b9 |
241 | /* Handle error messages */ |
242 | if (!$r) { |
243 | /* Display error */ |
ac987a56 |
244 | echo html_tag( 'table', |
245 | html_tag( 'tr', |
246 | html_tag( 'td', |
09743787 |
247 | "\n". '<strong><font color="' . $color[2] . |
ac987a56 |
248 | '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n", |
249 | 'center' ) |
250 | ), |
ac50138c |
251 | 'center', '', 'width="100%"' ); |
f6c945b9 |
252 | |
253 | /* Display the "new address" form again */ |
07dcee9f |
254 | echo '<FORM ACTION="' . $form_url . |
f6c945b9 |
255 | '" METHOD="POST">' . "\n" . |
ac987a56 |
256 | html_tag( 'table', |
257 | html_tag( 'tr', |
258 | html_tag( 'td', |
09743787 |
259 | "\n". '<strong>' . _("Update address") . '</strong>' ."\n", |
ac987a56 |
260 | 'center', $color[0] ) |
261 | ), |
09743787 |
262 | 'center', '', 'width="100%"' ); |
daba719e |
263 | address_form("editaddr", _("Update address"), $newdata); |
f6c945b9 |
264 | echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' . |
265 | htmlspecialchars($oldnick) . "\">\n" . |
266 | '<INPUT TYPE=hidden NAME=backend VALUE="' . |
267 | htmlspecialchars($backend) . "\">\n" . |
268 | '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . |
269 | "\n" . '</FORM>'; |
daba719e |
270 | $abortform = true; |
271 | } |
272 | } else { |
273 | |
f6c945b9 |
274 | /* Should not get here... */ |
daba719e |
275 | plain_error_message(_("Unknown error"), $color); |
276 | $abortform = true; |
277 | } |
278 | } |
279 | } /* !empty($editaddr) - Update/modify address */ |
280 | } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */ |
281 | } /* !empty($addaddr['nickname']) - Add new address */ |
282 | |
283 | // Some times we end output before forms are printed |
284 | if($abortform) { |
dcc1cc82 |
285 | echo "</BODY></HTML>\n"; |
daba719e |
286 | exit(); |
ffd8224c |
287 | } |
daba719e |
288 | } |
ffd8224c |
289 | |
290 | |
f6c945b9 |
291 | /* =================================================================== * |
292 | * The following is only executed on a GET request, or on a POST when * |
293 | * a user is added, or when "delete" or "modify" was successful. * |
294 | * =================================================================== */ |
ffd8224c |
295 | |
f6c945b9 |
296 | /* Display error messages */ |
297 | if (!empty($formerror)) { |
ac987a56 |
298 | echo html_tag( 'table', |
299 | html_tag( 'tr', |
300 | html_tag( 'td', |
301 | "\n". '<br><strong><font color="' . $color[2] . |
302 | '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n", |
303 | 'center' ) |
304 | ), |
ac50138c |
305 | 'center', '', 'width="100%"' ); |
daba719e |
306 | } |
ffd8224c |
307 | |
308 | |
f6c945b9 |
309 | /* Display the address management part */ |
310 | if ($showaddrlist) { |
311 | /* Get and sort address list */ |
daba719e |
312 | $alist = $abook->list_addr(); |
313 | if(!is_array($alist)) { |
ffd8224c |
314 | plain_error_message($abook->error, $color); |
315 | exit; |
daba719e |
316 | } |
ffd8224c |
317 | |
daba719e |
318 | usort($alist,'alistcmp'); |
319 | $prevbackend = -1; |
320 | $headerprinted = false; |
ffd8224c |
321 | |
ac987a56 |
322 | echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n"; |
ffd8224c |
323 | |
f6c945b9 |
324 | /* List addresses */ |
91821fc0 |
325 | if (count($alist) > 0) { |
07dcee9f |
326 | echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' . "\n"; |
91821fc0 |
327 | while(list($undef,$row) = each($alist)) { |
328 | |
329 | /* New table header for each backend */ |
330 | if($prevbackend != $row['backend']) { |
92cd1e8e |
331 | if($prevbackend < 0) { |
ac987a56 |
332 | echo html_tag( 'table', |
333 | html_tag( 'tr', |
334 | html_tag( 'td', |
335 | '<INPUT TYPE=submit NAME=editaddr VALUE="' . |
336 | _("Edit selected") . "\">\n" . |
337 | '<INPUT TYPE=submit NAME=deladdr VALUE="' . |
338 | _("Delete selected") . "\">\n", |
339 | 'center', '', 'colspan="5"' ) |
340 | ) . |
341 | html_tag( 'tr', |
342 | html_tag( 'td', ' <br>', 'center', '', 'colspan="5"' ) |
343 | ) , |
344 | 'center' ); |
91821fc0 |
345 | } |
346 | |
ac987a56 |
347 | echo html_tag( 'table', |
348 | html_tag( 'tr', |
349 | html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ) |
350 | ) , |
ac50138c |
351 | 'center', '', 'width="95%"' ) ."\n" |
352 | . html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) . |
ac987a56 |
353 | html_tag( 'tr', "\n" . |
354 | html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . |
355 | html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) . |
356 | html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) . |
357 | html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) . |
358 | html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) , |
359 | '', $color[9] ) . "\n"; |
91821fc0 |
360 | |
361 | $line = 0; |
362 | $headerprinted = true; |
363 | } /* End of header */ |
364 | |
365 | $prevbackend = $row['backend']; |
366 | |
367 | /* Check if this user is selected */ |
368 | if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) { |
369 | $selected = 'CHECKED'; |
370 | } else { |
371 | $selected = ''; |
ffd8224c |
372 | } |
91821fc0 |
373 | |
374 | /* Print one row */ |
ac987a56 |
375 | $tr_bgcolor = ''; |
376 | if ($line % 2) { $tr_bgcolor = $color[0]; } |
e842b215 |
377 | if ($squirrelmail_language == 'ja_JP') |
378 | { |
ef5bea7d |
379 | echo html_tag( 'tr', '', '', $tr_bgcolor) . |
e842b215 |
380 | html_tag( 'td', |
381 | '<SMALL>' . |
382 | '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' . |
383 | $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' , |
384 | 'center', '', 'valign="top" width="1%"' ) . |
385 | html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) . |
386 | html_tag( 'td', ' ' . $row['lastname'] . ' ' . $row['firstname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) . |
387 | html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' '; |
388 | } else { |
ef5bea7d |
389 | echo html_tag( 'tr', '', '', $tr_bgcolor) . |
ac987a56 |
390 | html_tag( 'td', |
391 | '<SMALL>' . |
392 | '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' . |
393 | $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' , |
394 | 'center', '', 'valign="top" width="1%"' ) . |
395 | html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) . |
396 | html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) . |
397 | html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' '; |
e842b215 |
398 | } |
3d0cada3 |
399 | $email = $abook->full_address($row); |
d62c4938 |
400 | echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email), |
401 | htmlspecialchars($row['email']) ) . |
402 | ' </td>'."\n". |
4e160237 |
403 | html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) . |
ac987a56 |
404 | "</tr>\n"; |
91821fc0 |
405 | $line++; |
daba719e |
406 | } |
91821fc0 |
407 | |
408 | /* End of list. Close table. */ |
409 | if ($headerprinted) { |
ac987a56 |
410 | echo html_tag( 'tr', |
411 | html_tag( 'td', |
412 | '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") . |
413 | "\">\n" . |
414 | '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") . |
415 | "\">\n", |
416 | 'center', '', 'colspan="5"' ) |
417 | ); |
91821fc0 |
418 | } |
ac987a56 |
419 | echo '</table></FORM>'; |
daba719e |
420 | } |
f6c945b9 |
421 | } /* end of addresslist */ |
daba719e |
422 | |
423 | |
f6c945b9 |
424 | /* Display the "new address" form */ |
425 | echo '<a name="AddAddress"></a>' . "\n" . |
07dcee9f |
426 | '<FORM ACTION="' . $form_url . '" NAME=f_add METHOD="POST">' . "\n" . |
c6554ec0 |
427 | html_tag( 'table', |
ac987a56 |
428 | html_tag( 'tr', |
429 | html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname) . '</strong>' . "\n", |
430 | 'center', $color[0] |
431 | ) |
432 | ) |
ac50138c |
433 | , 'center', '', 'width="100%"' ) ."\n"; |
daba719e |
434 | address_form('addaddr', _("Add address"), $defdata); |
435 | echo '</FORM>'; |
436 | |
f6c945b9 |
437 | /* Add hook for anything that wants on the bottom */ |
daba719e |
438 | do_hook('addressbook_bottom'); |
dcc1cc82 |
439 | ?> |
440 | |
441 | </BODY></HTML> |