Lots of changes for variable initialization - clean up, really,
[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;
60
61 echo html_tag( 'table',
62 adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
63 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
64 adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
65 adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
66 adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
67 adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
68 html_tag( 'tr',
69 html_tag( 'td',
70 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
71 $submittext . '">',
72 'center', $color[4], 'colspan="2"')
73 )
74 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
75 }
76
77 /* Open addressbook, with error messages on but without LDAP (the *
78 * second "true"). Don't need LDAP here anyway */
79 $abook = addressbook_init(true, true);
80 if($abook->localbackend == 0) {
81 plain_error_message(
82 _("No personal address book is defined. Contact administrator."),
83 $color);
84 exit();
85 }
86
87 displayPageHeader($color, 'None');
88
89 $defdata = array();
90 $formerror = '';
91 $abortform = false;
92 $showaddrlist = true;
93 $defselected = array();
94 $form_url = 'addressbook.php';
95
96
97 /* Handle user's actions */
98 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
99
100 /**************************************************
101 * Add new address *
102 **************************************************/
103 if (!empty($addaddr['nickname'])) {
104 foreach( $addaddr as $k => $adr ) {
105 $addaddr[$k] = strip_tags( $adr );
106 }
107 $r = $abook->add($addaddr, $abook->localbackend);
108
109 /* Handle error messages */
110 if (!$r) {
111 /* Remove backend name from error string */
112 $errstr = $abook->error;
113 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
114
115 $formerror = $errstr;
116 $showaddrlist = false;
117 $defdata = $addaddr;
118 }
119 } else {
120
121 /************************************************
122 * Delete address(es) *
123 ************************************************/
124 if ((!empty($deladdr)) && sizeof($sel) > 0) {
125 $orig_sel = $sel;
126 sort($sel);
127
128 /* The selected addresses are identidied by "backend:nickname". *
129 * Sort the list and process one backend at the time */
130 $prevback = -1;
131 $subsel = array();
132 $delfailed = false;
133
134 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
135 list($sbackend, $snick) = explode(':', $sel[$i]);
136
137 /* When we get to a new backend, process addresses in *
138 * previous one. */
139 if ($prevback != $sbackend && $prevback != -1) {
140
141 $r = $abook->remove($subsel, $prevback);
142 if (!$r) {
143 $formerror = $abook->error;
144 $i = sizeof($sel);
145 $delfailed = true;
146 break;
147 }
148 $subsel = array();
149 }
150
151 /* Queue for processing */
152 array_push($subsel, $snick);
153 $prevback = $sbackend;
154 }
155
156 if (!$delfailed) {
157 $r = $abook->remove($subsel, $prevback);
158 if (!$r) { /* Handle errors */
159 $formerror = $abook->error;
160 $delfailed = true;
161 }
162 }
163
164 if ($delfailed) {
165 $showaddrlist = true;
166 $defselected = $orig_sel;
167 }
168
169 } else {
170
171 /***********************************************
172 * Update/modify address *
173 ***********************************************/
174 if (!empty($editaddr)) {
175
176 /* Stage one: Copy data into form */
177 if (isset($sel) && sizeof($sel) > 0) {
178 if(sizeof($sel) > 1) {
179 $formerror = _("You can only edit one address at the time");
180 $showaddrlist = true;
181 $defselected = $sel;
182 } else {
183 $abortform = true;
184 list($ebackend, $enick) = explode(':', $sel[0]);
185 $olddata = $abook->lookup($enick, $ebackend);
186
187 /* Display the "new address" form */
188 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
189 "\n" .
190 html_tag( 'table',
191 html_tag( 'tr',
192 html_tag( 'td',
193 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
194 'center', $color[0] )
195 ),
196 'center', '', 'width="100%" ' );
197 address_form("editaddr", _("Update address"), $olddata);
198 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
199 htmlspecialchars($olddata["nickname"]) . "\">\n" .
200 '<INPUT TYPE=hidden NAME=backend VALUE="' .
201 htmlspecialchars($olddata["backend"]) . "\">\n" .
202 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
203 '</FORM>';
204 }
205 } else {
206
207 /* Stage two: Write new data */
208 if ($doedit = 1) {
209 $newdata = $editaddr;
210 $r = $abook->modify($oldnick, $newdata, $backend);
211
212 /* Handle error messages */
213 if (!$r) {
214 /* Display error */
215 echo html_tag( 'table',
216 html_tag( 'tr',
217 html_tag( 'td',
218 "\n". '<br><strong><font color="' . $color[2] .
219 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
220 'center' )
221 ),
222 'center', '', 'width="100%"' );
223
224 /* Display the "new address" form again */
225 echo '<FORM ACTION="' . $form_url .
226 '" METHOD="POST">' . "\n" .
227 html_tag( 'table',
228 html_tag( 'tr',
229 html_tag( 'td',
230 "\n". '<br><strong>' . _("Update address") . '</strong>' ."\n",
231 'center', $color[0] )
232 ),
233 'center', '', 'width="100%"' ) .
234 address_form("editaddr", _("Update address"), $newdata);
235 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
236 htmlspecialchars($oldnick) . "\">\n" .
237 '<INPUT TYPE=hidden NAME=backend VALUE="' .
238 htmlspecialchars($backend) . "\">\n" .
239 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' .
240 "\n" . '</FORM>';
241 $abortform = true;
242 }
243 } else {
244
245 /* Should not get here... */
246 plain_error_message(_("Unknown error"), $color);
247 $abortform = true;
248 }
249 }
250 } /* !empty($editaddr) - Update/modify address */
251 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
252 } /* !empty($addaddr['nickname']) - Add new address */
253
254 // Some times we end output before forms are printed
255 if($abortform) {
256 echo "</BODY></HTML>\n";
257 exit();
258 }
259 }
260
261
262 /* =================================================================== *
263 * The following is only executed on a GET request, or on a POST when *
264 * a user is added, or when "delete" or "modify" was successful. *
265 * =================================================================== */
266
267 /* Display error messages */
268 if (!empty($formerror)) {
269 echo html_tag( 'table',
270 html_tag( 'tr',
271 html_tag( 'td',
272 "\n". '<br><strong><font color="' . $color[2] .
273 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
274 'center' )
275 ),
276 'center', '', 'width="100%"' );
277 }
278
279
280 /* Display the address management part */
281 if ($showaddrlist) {
282 /* Get and sort address list */
283 $alist = $abook->list_addr();
284 if(!is_array($alist)) {
285 plain_error_message($abook->error, $color);
286 exit;
287 }
288
289 usort($alist,'alistcmp');
290 $prevbackend = -1;
291 $headerprinted = false;
292
293 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
294
295 /* List addresses */
296 if (count($alist) > 0) {
297 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' . "\n";
298 while(list($undef,$row) = each($alist)) {
299
300 /* New table header for each backend */
301 if($prevbackend != $row['backend']) {
302 if($prevbackend < 0) {
303 echo html_tag( 'table',
304 html_tag( 'tr',
305 html_tag( 'td',
306 '<INPUT TYPE=submit NAME=editaddr VALUE="' .
307 _("Edit selected") . "\">\n" .
308 '<INPUT TYPE=submit NAME=deladdr VALUE="' .
309 _("Delete selected") . "\">\n",
310 'center', '', 'colspan="5"' )
311 ) .
312 html_tag( 'tr',
313 html_tag( 'td', '&nbsp;<br>', 'center', '', 'colspan="5"' )
314 ) ,
315 'center' );
316 }
317
318 echo html_tag( 'table',
319 html_tag( 'tr',
320 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
321 ) ,
322 'center', '', 'width="95%"' ) ."\n"
323 . html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
324 html_tag( 'tr', "\n" .
325 html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) .
326 html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
327 html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
328 html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
329 html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
330 '', $color[9] ) . "\n";
331
332 $line = 0;
333 $headerprinted = true;
334 } /* End of header */
335
336 $prevbackend = $row['backend'];
337
338 /* Check if this user is selected */
339 if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
340 $selected = 'CHECKED';
341 } else {
342 $selected = '';
343 }
344
345 /* Print one row */
346 $tr_bgcolor = '';
347 if ($line % 2) { $tr_bgcolor = $color[0]; }
348 echo html_tag( 'tr', '') .
349 html_tag( 'td',
350 '<SMALL>' .
351 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
352 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
353 'center', '', 'valign="top" width="1%"' ) .
354 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
355 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
356 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
357 $email = $abook->full_address($row);
358 if ($compose_new_win == '1') {
359 echo '<a href="javascript:void(0)" onclick=comp_in_new(false,"compose.php?send_to='.rawurlencode($email).'")>';
360 }
361 else {
362 echo '<A HREF="compose.php?send_to=' . rawurlencode($email).'">';
363 }
364 echo htmlspecialchars($row['email']) . '</A>&nbsp;</td>'."\n".
365 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
366 "</tr>\n";
367 $line++;
368 }
369
370 /* End of list. Close table. */
371 if ($headerprinted) {
372 echo html_tag( 'tr',
373 html_tag( 'td',
374 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
375 "\">\n" .
376 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
377 "\">\n",
378 'center', '', 'colspan="5"' )
379 );
380 }
381 echo '</table></FORM>';
382 }
383 } /* end of addresslist */
384
385
386 /* Display the "new address" form */
387 echo '<a name="AddAddress"></a>' . "\n" .
388 '<FORM ACTION="' . $form_url . '" NAME=f_add METHOD="POST">' . "\n" .
389 html_tag( 'table',
390 html_tag( 'tr',
391 html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname) . '</strong>' . "\n",
392 'center', $color[0]
393 )
394 )
395 , 'center', '', 'width="100%"' ) ."\n";
396 address_form('addaddr', _("Add address"), $defdata);
397 echo '</FORM>';
398
399 /* Add hook for anything that wants on the bottom */
400 do_hook('addressbook_bottom');
401 ?>
402
403 </BODY></HTML>