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