since comments are not needed for @return
[squirrelmail.git] / src / addressbook.php
1 <?php
2
3 /**
4 * addressbook.php
5 *
6 * Copyright (c) 1999-2005 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 * @version $Id$
12 * @package squirrelmail
13 * @subpackage addressbook
14 */
15
16 /**
17 * Path for SquirrelMail required files.
18 * @ignore
19 */
20 define('SM_PATH','../');
21
22 /** SquirrelMail required files. */
23 require_once(SM_PATH . 'include/validate.php');
24 require_once(SM_PATH . 'functions/display_messages.php');
25 require_once(SM_PATH . 'functions/addressbook.php');
26 require_once(SM_PATH . 'functions/forms.php');
27
28 /** lets get the global vars we may need */
29 sqgetGlobalVar('key', $key, SQ_COOKIE);
30
31 sqgetGlobalVar('username', $username, SQ_SESSION);
32 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
33 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
34 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
35
36 /* From the address form */
37 sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
38 sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
39 sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
40 sqgetGlobalVar('sel', $sel, SQ_POST);
41 sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
42 sqgetGlobalVar('backend', $backend, SQ_POST);
43 sqgetGlobalVar('doedit', $doedit, SQ_POST);
44
45 /* Get sorting order */
46 $abook_sort_order = get_abook_sort();
47
48 /* Create page header before addressbook_init in order to display error messages correctly. */
49 displayPageHeader($color, 'None');
50
51 /* Open addressbook with error messages on.
52 remote backends (LDAP) are enabled because they can be used. (list_addr function)
53 */
54 $abook = addressbook_init(true, false);
55
56 // FIXME: do we have to stop use of address book, when localbackend is not present.
57 if($abook->localbackend == 0) {
58 plain_error_message(
59 _("No personal address book is defined. Contact administrator."),
60 $color);
61 exit();
62 }
63
64
65 $defdata = array();
66 $formerror = '';
67 $abortform = false;
68 $showaddrlist = true;
69 $defselected = array();
70 $form_url = 'addressbook.php';
71
72 /* Handle user's actions */
73 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
74
75 /**************************************************
76 * Add new address *
77 **************************************************/
78 if (isset($addaddr)) {
79 if (isset($backend)) {
80 $r = $abook->add($addaddr, $backend);
81 } else {
82 $r = $abook->add($addaddr, $abook->localbackend);
83 }
84
85 /* Handle error messages */
86 if (!$r) {
87 /* Remove backend name from error string */
88 $errstr = $abook->error;
89 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
90
91 $formerror = $errstr;
92 $showaddrlist = false;
93 $defdata = $addaddr;
94 }
95 } else {
96
97 /************************************************
98 * Delete address(es) *
99 ************************************************/
100 if ((!empty($deladdr)) && sizeof($sel) > 0) {
101 $orig_sel = $sel;
102 sort($sel);
103
104 /* The selected addresses are identidied by "backend:nickname". *
105 * Sort the list and process one backend at the time */
106 $prevback = -1;
107 $subsel = array();
108 $delfailed = false;
109
110 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
111 list($sbackend, $snick) = explode(':', $sel[$i]);
112
113 /* When we get to a new backend, process addresses in *
114 * previous one. */
115 if ($prevback != $sbackend && $prevback != -1) {
116
117 $r = $abook->remove($subsel, $prevback);
118 if (!$r) {
119 $formerror = $abook->error;
120 $i = sizeof($sel);
121 $delfailed = true;
122 break;
123 }
124 $subsel = array();
125 }
126
127 /* Queue for processing */
128 array_push($subsel, $snick);
129 $prevback = $sbackend;
130 }
131
132 if (!$delfailed) {
133 $r = $abook->remove($subsel, $prevback);
134 if (!$r) { /* Handle errors */
135 $formerror = $abook->error;
136 $delfailed = true;
137 }
138 }
139
140 if ($delfailed) {
141 $showaddrlist = true;
142 $defselected = $orig_sel;
143 }
144
145 } else {
146
147 /***********************************************
148 * Update/modify address *
149 ***********************************************/
150 if (!empty($editaddr)) {
151 /* Stage one: Copy data into form */
152 if (isset($sel) && sizeof($sel) > 0) {
153 if(sizeof($sel) > 1) {
154 $formerror = _("You can only edit one address at the time");
155 $showaddrlist = true;
156 $defselected = $sel;
157 } else {
158 $abortform = true;
159 list($ebackend, $enick) = explode(':', $sel[0]);
160 $olddata = $abook->lookup($enick, $ebackend);
161
162 /* Display the "new address" form */
163 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
164 echo addHidden('oldnick', $olddata['nickname']).
165 addHidden('backend', $olddata['backend']).
166 addHidden('doedit', '1').
167 '</form>';
168 }
169 } elseif ($doedit == 1) {
170 /* Stage two: Write new data */
171 $newdata = $editaddr;
172 $r = $abook->modify($oldnick, $newdata, $backend);
173
174 /* Handle error messages */
175 if (!$r) {
176 /* Display error */
177 echo html_tag( 'table',
178 html_tag( 'tr',
179 html_tag( 'td',
180 "\n". '<strong><font color="' . $color[2] .
181 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
182 'center' )
183 ),
184 'center', '', 'width="100%"' );
185
186 /* Display the "new address" form again */
187 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
188 echo addHidden('oldnick', $oldnick).
189 addHidden('backend', $backend).
190 addHidden('doedit', '1').
191 "\n" . '</form>';
192 $abortform = true;
193 }
194 } else {
195 /**
196 * $editaddr is set, but $sel (address selection in address listing)
197 * and $doedit (address edit form) are not set.
198 * Assume that user clicked on "Edit address" without selecting any address.
199 */
200 $formerror = _("Please select address that you want to edit");
201 $showaddrlist = true;
202 } /* end of edit stage detection */
203 } /* !empty($editaddr) - Update/modify address */
204 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
205 } /* !empty($addaddr['nickname']) - Add new address */
206
207 // Some times we end output before forms are printed
208 if($abortform) {
209 echo "</body></html>\n";
210 exit();
211 }
212 }
213
214
215 /* =================================================================== *
216 * The following is only executed on a GET request, or on a POST when *
217 * a user is added, or when "delete" or "modify" was successful. *
218 * =================================================================== */
219
220 /* Display error messages */
221 if (!empty($formerror)) {
222 echo html_tag( 'table',
223 html_tag( 'tr',
224 html_tag( 'td',
225 "\n". '<br /><strong><font color="' . $color[2] .
226 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
227 'center' )
228 ),
229 'center', '', 'width="100%"' );
230 }
231
232
233 /* Display the address management part */
234 if ($showaddrlist) {
235 /* Get and sort address list */
236 $alist = $abook->list_addr();
237 if(!is_array($alist)) {
238 plain_error_message($abook->error, $color);
239 exit;
240 }
241
242 usort($alist,'alistcmp');
243 $prevbackend = -1;
244 $headerprinted = false;
245
246 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
247
248 /* List addresses */
249 if (count($alist) > 0) {
250 echo addForm($form_url, 'post');
251 while(list($undef,$row) = each($alist)) {
252
253 /* New table header for each backend */
254 if($prevbackend != $row['backend']) {
255 if($prevbackend < 0) {
256 echo html_tag( 'table',
257 html_tag( 'tr',
258 html_tag( 'td',
259 addSubmit(_("Edit selected"), 'editaddr').
260 addSubmit(_("Delete selected"), 'deladdr'),
261 'center', '', 'colspan="5"' )
262 ) .
263 html_tag( 'tr',
264 html_tag( 'td', '&nbsp;<br />', 'center', '', 'colspan="5"' )
265 ),
266 'center' );
267 echo "\n<!-- start of address book table -->\n" .
268 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
269 html_tag( 'tr', "\n" .
270 html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) . "\n" .
271 html_tag( 'th', _("Nickname") .
272 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
273 'left', '', 'width="1%"' ) . "\n" .
274 html_tag( 'th', _("Name") .
275 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
276 'left', '', 'width="1%"' ) . "\n" .
277 html_tag( 'th', _("E-mail") .
278 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
279 'left', '', 'width="1%"' ) . "\n" .
280 html_tag( 'th', _("Info") .
281 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
282 'left', '', 'width="1%"' ) . "\n",
283 '', $color[9] ) . "\n";
284 }
285
286 // Separate different backends with <hr />
287 if($prevbackend > 0) {
288 echo html_tag( 'tr',
289 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
290 );
291 }
292
293 // Print backend name
294 echo html_tag( 'tr',
295 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
296 );
297
298 $line = 0;
299 $headerprinted = true;
300 } /* End of header */
301
302 $prevbackend = $row['backend'];
303
304 /* Check if this user is selected */
305 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
306
307 /* Print one row, with alternating color */
308 if ($line % 2) {
309 $tr_bgcolor = $color[12];
310 } else {
311 $tr_bgcolor = $color[4];
312 }
313 if ($squirrelmail_language == 'ja_JP') {
314 echo html_tag( 'tr', '', '', $tr_bgcolor);
315 if ($abook->backends[$row['backend']]->writeable) {
316 echo html_tag( 'td',
317 '<small>' .
318 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
319 '</small>' ,
320 'center', '', 'valign="top" width="1%"' );
321 } else {
322 echo html_tag( 'td',
323 '&nbsp;' ,
324 'center', '', 'valign="top" width="1%"' );
325 }
326 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
327 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
328 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
329 } else {
330 echo html_tag( 'tr', '', '', $tr_bgcolor);
331 if ($abook->backends[$row['backend']]->writeable) {
332 echo html_tag( 'td',
333 '<small>' .
334 addCheckBox('sel[]', $selected, $row['backend'] . ':' . $row['nickname']).
335 '</small>' ,
336 'center', '', 'valign="top" width="1%"' );
337 } else {
338 echo html_tag( 'td',
339 '&nbsp;' ,
340 'center', '', 'valign="top" width="1%"' );
341 }
342 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
343 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
344 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
345 }
346 $email = $abook->full_address($row);
347 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
348 htmlspecialchars($row['email'])).
349 '&nbsp;</td>'."\n".
350 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
351 "</tr>\n";
352 $line++;
353 }
354 echo "</table>" .
355 "\n<!-- end of address book table -->\n";
356
357 /* End of list. Add edit/delete select buttons */
358 if ($headerprinted) {
359 echo html_tag( 'table',
360 html_tag( 'tr',
361 html_tag( 'td',
362 addSubmit(_("Edit selected"), 'editaddr') .
363 addSubmit(_("Delete selected"), 'deladdr'),
364 'center', '', 'colspan="5"' )
365 ),
366 'center' );
367 }
368 echo "</form>\n";
369 }
370 } /* end of addresslist */
371
372
373 /* Display the "new address" form */
374 echo '<a name="AddAddress"></a>' . "\n";
375 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
376 echo "</form>\n";
377
378 /* Add hook for anything that wants on the bottom */
379 echo "<!-- start of addressbook_bottom hook-->\n";
380 do_hook('addressbook_bottom');
381 echo "\n<!-- end of addressbook_bottom hook-->\n";
382 ?>
383 </body></html>