Plugin may be invisible when JS is not detected
[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
152 /* Stage one: Copy data into form */
153 if (isset($sel) && sizeof($sel) > 0) {
154 if(sizeof($sel) > 1) {
155 $formerror = _("You can only edit one address at the time");
156 $showaddrlist = true;
157 $defselected = $sel;
158 } else {
159 $abortform = true;
160 list($ebackend, $enick) = explode(':', $sel[0]);
161 $olddata = $abook->lookup($enick, $ebackend);
162
163 /* Display the "new address" form */
164 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
165 echo addHidden('oldnick', $olddata['nickname']).
166 addHidden('backend', $olddata['backend']).
167 addHidden('doedit', '1').
168 '</form>';
169 }
170 } else {
171
172 /* Stage two: Write new data */
173 if ($doedit = 1) {
174 $newdata = $editaddr;
175 $r = $abook->modify($oldnick, $newdata, $backend);
176
177 /* Handle error messages */
178 if (!$r) {
179 /* Display error */
180 echo html_tag( 'table',
181 html_tag( 'tr',
182 html_tag( 'td',
183 "\n". '<strong><font color="' . $color[2] .
184 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
185 'center' )
186 ),
187 'center', '', 'width="100%"' );
188
189 /* Display the "new address" form again */
190 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
191 echo addHidden('oldnick', $oldnick).
192 addHidden('backend', $backend).
193 addHidden('doedit', '1').
194 "\n" . '</form>';
195 $abortform = true;
196 }
197 } else {
198
199 /* Should not get here... */
200 plain_error_message(_("Unknown error"), $color);
201 $abortform = true;
202 }
203 }
204 } /* !empty($editaddr) - Update/modify address */
205 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
206 } /* !empty($addaddr['nickname']) - Add new address */
207
208 // Some times we end output before forms are printed
209 if($abortform) {
210 echo "</body></html>\n";
211 exit();
212 }
213 }
214
215
216 /* =================================================================== *
217 * The following is only executed on a GET request, or on a POST when *
218 * a user is added, or when "delete" or "modify" was successful. *
219 * =================================================================== */
220
221 /* Display error messages */
222 if (!empty($formerror)) {
223 echo html_tag( 'table',
224 html_tag( 'tr',
225 html_tag( 'td',
226 "\n". '<br /><strong><font color="' . $color[2] .
227 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
228 'center' )
229 ),
230 'center', '', 'width="100%"' );
231 }
232
233
234 /* Display the address management part */
235 if ($showaddrlist) {
236 /* Get and sort address list */
237 $alist = $abook->list_addr();
238 if(!is_array($alist)) {
239 plain_error_message($abook->error, $color);
240 exit;
241 }
242
243 usort($alist,'alistcmp');
244 $prevbackend = -1;
245 $headerprinted = false;
246
247 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
248
249 /* List addresses */
250 if (count($alist) > 0) {
251 echo addForm($form_url, 'post');
252 while(list($undef,$row) = each($alist)) {
253
254 /* New table header for each backend */
255 if($prevbackend != $row['backend']) {
256 if($prevbackend < 0) {
257 echo html_tag( 'table',
258 html_tag( 'tr',
259 html_tag( 'td',
260 addSubmit(_("Edit selected"), 'editaddr').
261 addSubmit(_("Delete selected"), 'deladdr'),
262 'center', '', 'colspan="5"' )
263 ) .
264 html_tag( 'tr',
265 html_tag( 'td', '&nbsp;<br />', 'center', '', 'colspan="5"' )
266 ),
267 'center' );
268 echo "\n<!-- start of address book table -->\n" .
269 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
270 html_tag( 'tr', "\n" .
271 html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) . "\n" .
272 html_tag( 'th', _("Nickname") .
273 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
274 'left', '', 'width="1%"' ) . "\n" .
275 html_tag( 'th', _("Name") .
276 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
277 'left', '', 'width="1%"' ) . "\n" .
278 html_tag( 'th', _("E-mail") .
279 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
280 'left', '', 'width="1%"' ) . "\n" .
281 html_tag( 'th', _("Info") .
282 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
283 'left', '', 'width="1%"' ) . "\n",
284 '', $color[9] ) . "\n";
285 }
286
287 // Separate different backends with <hr />
288 if($prevbackend > 0) {
289 echo html_tag( 'tr',
290 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
291 );
292 }
293
294 // Print backend name
295 echo html_tag( 'tr',
296 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
297 );
298
299 $line = 0;
300 $headerprinted = true;
301 } /* End of header */
302
303 $prevbackend = $row['backend'];
304
305 /* Check if this user is selected */
306 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
307
308 /* Print one row, with alternating color */
309 if ($line % 2) {
310 $tr_bgcolor = $color[12];
311 } else {
312 $tr_bgcolor = $color[4];
313 }
314 if ($squirrelmail_language == 'ja_JP') {
315 echo html_tag( 'tr', '', '', $tr_bgcolor);
316 if ($abook->backends[$row['backend']]->writeable) {
317 echo html_tag( 'td',
318 '<small>' .
319 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
320 '</small>' ,
321 'center', '', 'valign="top" width="1%"' );
322 } else {
323 echo html_tag( 'td',
324 '&nbsp;' ,
325 'center', '', 'valign="top" width="1%"' );
326 }
327 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
328 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
329 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
330 } else {
331 echo html_tag( 'tr', '', '', $tr_bgcolor);
332 if ($abook->backends[$row['backend']]->writeable) {
333 echo html_tag( 'td',
334 '<small>' .
335 addCheckBox('sel[]', $selected, $row['backend'] . ':' . $row['nickname']).
336 '</small>' ,
337 'center', '', 'valign="top" width="1%"' );
338 } else {
339 echo html_tag( 'td',
340 '&nbsp;' ,
341 'center', '', 'valign="top" width="1%"' );
342 }
343 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
344 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
345 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
346 }
347 $email = $abook->full_address($row);
348 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
349 htmlspecialchars($row['email'])).
350 '&nbsp;</td>'."\n".
351 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
352 "</tr>\n";
353 $line++;
354 }
355 echo "</table>" .
356 "\n<!-- end of address book table -->\n";
357
358 /* End of list. Add edit/delete select buttons */
359 if ($headerprinted) {
360 echo html_tag( 'table',
361 html_tag( 'tr',
362 html_tag( 'td',
363 addSubmit(_("Edit selected"), 'editaddr') .
364 addSubmit(_("Delete selected"), 'deladdr'),
365 'center', '', 'colspan="5"' )
366 ),
367 'center' );
368 }
369 echo "</form>\n";
370 }
371 } /* end of addresslist */
372
373
374 /* Display the "new address" form */
375 echo '<a name="AddAddress"></a>' . "\n";
376 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
377 echo "</form>\n";
378
379 /* Add hook for anything that wants on the bottom */
380 echo "<!-- start of addressbook_bottom hook-->\n";
381 do_hook('addressbook_bottom');
382 echo "\n<!-- end of addressbook_bottom hook-->\n";
383 ?>
384 </body></html>