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