copyright update
[squirrelmail.git] / src / addressbook.php
1 <?php
2
3 /**
4 * addressbook.php
5 *
6 * Manage personal address book.
7 *
8 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage addressbook
13 */
14
15 /**
16 * Path for SquirrelMail required files.
17 * @ignore
18 */
19 define('SM_PATH','../');
20
21 /** SquirrelMail required files. */
22 require_once(SM_PATH . 'include/validate.php');
23 require_once(SM_PATH . 'functions/display_messages.php');
24 require_once(SM_PATH . 'functions/addressbook.php');
25 require_once(SM_PATH . 'functions/forms.php');
26
27 /** lets get the global vars we may need */
28 sqgetGlobalVar('key', $key, SQ_COOKIE);
29
30 sqgetGlobalVar('username', $username, SQ_SESSION);
31 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
32 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
33 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
34
35 /* From the address form */
36 sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
37 sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
38 sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
39 sqgetGlobalVar('sel', $sel, SQ_POST);
40 sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
41 sqgetGlobalVar('backend', $backend, SQ_POST);
42 sqgetGlobalVar('doedit', $doedit, SQ_POST);
43
44 /* Get sorting order */
45 $abook_sort_order = get_abook_sort();
46
47 /* Create page header before addressbook_init in order to display error messages correctly. */
48 displayPageHeader($color, 'None');
49
50 /* Open addressbook with error messages on.
51 remote backends (LDAP) are enabled because they can be used. (list_addr function)
52 */
53 $abook = addressbook_init(true, false);
54
55 // FIXME: do we have to stop use of address book, when localbackend is not present.
56 if($abook->localbackend == 0) {
57 plain_error_message(
58 _("No personal address book is defined. Contact administrator."),
59 $color);
60 exit();
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 /* Stage one: Copy data into form */
150 if (isset($sel) && sizeof($sel) > 0) {
151 if(sizeof($sel) > 1) {
152 $formerror = _("You can only edit one address at the time");
153 $showaddrlist = true;
154 $defselected = $sel;
155 } else {
156 $abortform = true;
157 list($ebackend, $enick) = explode(':', $sel[0]);
158 $olddata = $abook->lookup($enick, $ebackend);
159 // FIXME: Test if $olddata really contains anything and return an error message if it doesn't
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 } elseif ($doedit == 1) {
169 /* Stage two: Write new data */
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 * $editaddr is set, but $sel (address selection in address listing)
196 * and $doedit (address edit form) are not set.
197 * Assume that user clicked on "Edit address" without selecting any address.
198 */
199 $formerror = _("Please select address that you want to edit");
200 $showaddrlist = true;
201 } /* end of edit stage detection */
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 if ($abook->add_extra_field) {
251 $abook_fields = 6;
252 } else {
253 $abook_fields = 5;
254 }
255 while(list($undef,$row) = each($alist)) {
256
257 /* New table header for each backend */
258 if($prevbackend != $row['backend']) {
259 if($prevbackend < 0) {
260 echo html_tag( 'table',
261 html_tag( 'tr',
262 html_tag( 'td',
263 addSubmit(_("Edit selected"), 'editaddr').
264 addSubmit(_("Delete selected"), 'deladdr'),
265 'center', '', "colspan=\"$abook_fields\"" )
266 ) .
267 html_tag( 'tr',
268 html_tag( 'td', '&nbsp;<br />', 'center', '', "colspan=\"$abook_fields\"" )
269 ),
270 'center' );
271 echo "\n<!-- start of address book table -->\n" .
272 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
273 html_tag( 'tr', "\n" .
274 html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) . "\n" .
275 html_tag( 'th', _("Nickname") .
276 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
277 'left', '', 'width="1%"' ) . "\n" .
278 html_tag( 'th', _("Name") .
279 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
280 'left', '', 'width="1%"' ) . "\n" .
281 html_tag( 'th', _("E-mail") .
282 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
283 'left', '', 'width="1%"' ) . "\n" .
284 html_tag( 'th', _("Info") .
285 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
286 'left', '', 'width="1%"' ) .
287 ($abook->add_extra_field ? html_tag( 'th', '&nbsp;','left', '', 'width="1%"'): '') .
288 "\n",
289 '', $color[9] ) . "\n";
290 }
291
292 // Separate different backends with <hr />
293 if($prevbackend > 0) {
294 echo html_tag( 'tr',
295 html_tag( 'td', "<hr />", 'center', '' ,"colspan=\"$abook_fields\"" )
296 );
297 }
298
299 // Print backend name
300 echo html_tag( 'tr',
301 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,"colspan=\"$abook_fields\"" )
302 );
303
304 $line = 0;
305 $headerprinted = true;
306 } /* End of header */
307
308 $prevbackend = $row['backend'];
309
310 /* Check if this user is selected */
311 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
312
313 /* Print one row, with alternating color */
314 if ($line % 2) {
315 $tr_bgcolor = $color[12];
316 } else {
317 $tr_bgcolor = $color[4];
318 }
319 echo html_tag( 'tr', '', '', $tr_bgcolor);
320 if ($abook->backends[$row['backend']]->writeable) {
321 echo html_tag( 'td',
322 '<small>' .
323 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
324 '</small>' ,
325 'center', '', 'valign="top" width="1%"' );
326 } else {
327 echo html_tag( 'td',
328 '&nbsp;' ,
329 'center', '', 'valign="top" width="1%"' );
330 }
331 echo html_tag( 'td',
332 '&nbsp;' . htmlspecialchars($row['nickname']) . '&nbsp;',
333 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
334
335 // different full name display formating for Japanese translation
336 if ($squirrelmail_language == 'ja_JP') {
337 /*
338 * translation uses euc-jp character set internally.
339 * htmlspecialchars() should not break any characters.
340 */
341 echo html_tag( 'td',
342 '&nbsp;' . htmlspecialchars($row['lastname']) . ' ' . htmlspecialchars($row['firstname']) . '&nbsp;',
343 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
344 } else {
345 echo html_tag( 'td',
346 '&nbsp;' . htmlspecialchars($row['name']) . '&nbsp;',
347 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
348 }
349
350 // email address column
351 echo html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
352 $email = $abook->full_address($row);
353 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
354 htmlspecialchars($row['email'])).
355 '&nbsp;</td>'."\n";
356
357 // info column
358 echo html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' );
359
360 // add extra column if third party backend needs it
361 if ($abook->add_extra_field) {
362 echo html_tag( 'td',
363 '&nbsp;' . (isset($row['extra']) ? $row['extra'] : '') . '&nbsp;',
364 'left', '', 'valign="top" width="1%"' );
365 }
366 echo "</tr>\n";
367 $line++;
368 }
369 echo "</table>" .
370 "\n<!-- end of address book table -->\n";
371
372 /* End of list. Add edit/delete select buttons */
373 if ($headerprinted) {
374 echo html_tag( 'table',
375 html_tag( 'tr',
376 html_tag( 'td',
377 addSubmit(_("Edit selected"), 'editaddr') .
378 addSubmit(_("Delete selected"), 'deladdr'),
379 'center', '', "colspan=\"$abook_fields\"" )
380 ),
381 'center' );
382 }
383 echo "</form>\n";
384 }
385 } /* end of addresslist */
386
387
388 /* Display the "new address" form */
389 echo '<a name="AddAddress"></a>' . "\n";
390 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
391 echo "</form>\n";
392
393 /* Add hook for anything that wants on the bottom */
394 echo "<!-- start of addressbook_bottom hook-->\n";
395 do_hook('addressbook_bottom');
396 echo "\n<!-- end of addressbook_bottom hook-->\n";
397
398 ?>
399 </body></html>