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