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