using sq_is8bit function instead of ereg. Code reuse and fixes some problems
[squirrelmail.git] / src / addressbook.php
CommitLineData
abdfb4d0 1<?php
134e4174 2
35586184 3/**
4 * addressbook.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Manage personal address book.
10 *
30967a1e 11 * @version $Id$
8f6f9ba5 12 * @package squirrelmail
35586184 13 */
14
30967a1e 15/**
16 * Path for SquirrelMail required files.
17 * @ignore
18 */
86725763 19define('SM_PATH','../');
20
8f6f9ba5 21/** SquirrelMail required files. */
08185f2a 22require_once(SM_PATH . 'include/validate.php');
86725763 23require_once(SM_PATH . 'functions/display_messages.php');
24require_once(SM_PATH . 'functions/addressbook.php');
19347763 25require_once(SM_PATH . 'functions/forms.php');
ffd8224c 26
8f6f9ba5 27/** lets get the global vars we may need */
1e12d1ff 28sqgetGlobalVar('key', $key, SQ_COOKIE);
0b97a708 29
1e12d1ff 30sqgetGlobalVar('username', $username, SQ_SESSION);
31sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
32sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
33sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
0b97a708 34
35/* From the address form */
1e12d1ff 36sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
37sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
38sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
39sqgetGlobalVar('sel', $sel, SQ_POST);
40sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
41sqgetGlobalVar('backend', $backend, SQ_POST);
2741cc97 42sqgetGlobalVar('doedit', $doedit, SQ_POST);
0b97a708 43
08e71631 44/* Get sorting order */
45$abook_sort_order = get_abook_sort();
46
f6c945b9 47/* Open addressbook, with error messages on but without LDAP (the *
48 * second "true"). Don't need LDAP here anyway */
daba719e 49$abook = addressbook_init(true, true);
50if($abook->localbackend == 0) {
51 plain_error_message(
056a005c 52 _("No personal address book is defined. Contact administrator."),
53 $color);
daba719e 54 exit();
55}
ffd8224c 56
daba719e 57displayPageHeader($color, 'None');
ffd8224c 58
daba719e 59$defdata = array();
60$formerror = '';
61$abortform = false;
62$showaddrlist = true;
63$defselected = array();
07dcee9f 64$form_url = 'addressbook.php';
daba719e 65
66
f6c945b9 67/* Handle user's actions */
1e12d1ff 68if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
daba719e 69
f6c945b9 70 /**************************************************
71 * Add new address *
72 **************************************************/
39b26252 73 if (isset($addaddr)) {
a123157f 74 if (isset($backend)) {
75 $r = $abook->add($addaddr, $backend);
76 } else {
77 $r = $abook->add($addaddr, $abook->localbackend);
c6554ec0 78 }
ffd8224c 79
f6c945b9 80 /* Handle error messages */
81 if (!$r) {
82 /* Remove backend name from error string */
ffd8224c 83 $errstr = $abook->error;
84 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
85
86 $formerror = $errstr;
87 $showaddrlist = false;
88 $defdata = $addaddr;
89 }
daba719e 90 } else {
ffd8224c 91
f6c945b9 92 /************************************************
93 * Delete address(es) *
94 ************************************************/
95 if ((!empty($deladdr)) && sizeof($sel) > 0) {
daba719e 96 $orig_sel = $sel;
97 sort($sel);
98
f6c945b9 99 /* The selected addresses are identidied by "backend:nickname". *
100 * Sort the list and process one backend at the time */
daba719e 101 $prevback = -1;
102 $subsel = array();
103 $delfailed = false;
104
f6c945b9 105 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
daba719e 106 list($sbackend, $snick) = explode(':', $sel[$i]);
107
f6c945b9 108 /* When we get to a new backend, process addresses in *
109 * previous one. */
110 if ($prevback != $sbackend && $prevback != -1) {
daba719e 111
112 $r = $abook->remove($subsel, $prevback);
f6c945b9 113 if (!$r) {
daba719e 114 $formerror = $abook->error;
115 $i = sizeof($sel);
116 $delfailed = true;
117 break;
118 }
119 $subsel = array();
120 }
121
f6c945b9 122 /* Queue for processing */
daba719e 123 array_push($subsel, $snick);
124 $prevback = $sbackend;
ffd8224c 125 }
ffd8224c 126
f6c945b9 127 if (!$delfailed) {
daba719e 128 $r = $abook->remove($subsel, $prevback);
f6c945b9 129 if (!$r) { /* Handle errors */
daba719e 130 $formerror = $abook->error;
131 $delfailed = true;
132 }
ffd8224c 133 }
ffd8224c 134
f6c945b9 135 if ($delfailed) {
daba719e 136 $showaddrlist = true;
137 $defselected = $orig_sel;
ffd8224c 138 }
ffd8224c 139
daba719e 140 } else {
141
f6c945b9 142 /***********************************************
143 * Update/modify address *
144 ***********************************************/
145 if (!empty($editaddr)) {
daba719e 146
f6c945b9 147 /* Stage one: Copy data into form */
daba719e 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
f6c945b9 158 /* Display the "new address" form */
77fe415a 159 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
2741cc97 160 echo addHidden('oldnick', $olddata['nickname']).
056a005c 161 addHidden('backend', $olddata['backend']).
162 addHidden('doedit', '1').
163 '</form>';
daba719e 164 }
165 } else {
166
f6c945b9 167 /* Stage two: Write new data */
168 if ($doedit = 1) {
daba719e 169 $newdata = $editaddr;
170 $r = $abook->modify($oldnick, $newdata, $backend);
171
f6c945b9 172 /* Handle error messages */
173 if (!$r) {
174 /* Display error */
056a005c 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%"' );
f6c945b9 183
184 /* Display the "new address" form again */
77fe415a 185 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
c1ac62d4 186 echo addHidden('oldnick', $oldnick).
056a005c 187 addHidden('backend', $backend).
188 addHidden('doedit', '1').
189 "\n" . '</form>';
daba719e 190 $abortform = true;
191 }
192 } else {
193
f6c945b9 194 /* Should not get here... */
daba719e 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) {
056a005c 205 echo "</body></html>\n";
206 exit();
ffd8224c 207 }
daba719e 208}
ffd8224c 209
210
f6c945b9 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 * =================================================================== */
ffd8224c 215
f6c945b9 216/* Display error messages */
217if (!empty($formerror)) {
ac987a56 218 echo html_tag( 'table',
056a005c 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%"' );
daba719e 226}
ffd8224c 227
228
f6c945b9 229/* Display the address management part */
230if ($showaddrlist) {
231 /* Get and sort address list */
daba719e 232 $alist = $abook->list_addr();
233 if(!is_array($alist)) {
ffd8224c 234 plain_error_message($abook->error, $color);
235 exit;
daba719e 236 }
ffd8224c 237
daba719e 238 usort($alist,'alistcmp');
239 $prevbackend = -1;
240 $headerprinted = false;
ffd8224c 241
ac987a56 242 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
ffd8224c 243
f6c945b9 244 /* List addresses */
91821fc0 245 if (count($alist) > 0) {
056a005c 246 echo addForm($form_url, 'post');
91821fc0 247 while(list($undef,$row) = each($alist)) {
056a005c 248
91821fc0 249 /* New table header for each backend */
250 if($prevbackend != $row['backend']) {
92cd1e8e 251 if($prevbackend < 0) {
ac987a56 252 echo html_tag( 'table',
134e4174 253 html_tag( 'tr',
254 html_tag( 'td',
056a005c 255 addSubmit(_("Edit selected"), 'editaddr').
256 addSubmit(_("Delete selected"), 'deladdr'),
134e4174 257 'center', '', 'colspan="5"' )
258 ) .
259 html_tag( 'tr',
260 html_tag( 'td', '&nbsp;<br />', 'center', '', 'colspan="5"' )
261 ),
262 'center' );
2741cc97 263 echo "\n<!-- start of address book table -->\n" .
056a005c 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";
d9879e29 280 }
281
2741cc97 282 // Separate different backends with <hr />
d9879e29 283 if($prevbackend > 0) {
284 echo html_tag( 'tr',
056a005c 285 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
286 );
2741cc97 287 }
288
289 // Print backend name
d9879e29 290 echo html_tag( 'tr',
291 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
056a005c 292 );
d9879e29 293
91821fc0 294 $line = 0;
295 $headerprinted = true;
296 } /* End of header */
fc93f97c 297
91821fc0 298 $prevbackend = $row['backend'];
056a005c 299
91821fc0 300 /* Check if this user is selected */
19347763 301 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
056a005c 302
9f6c97c3 303 /* Print one row, with alternating color */
056a005c 304 if ($line % 2) {
9f6c97c3 305 $tr_bgcolor = $color[12];
306 } else {
307 $tr_bgcolor = $color[4];
308 }
056a005c 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%"' );
e842b215 317 } else {
056a005c 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;';
19d01c6b 325 } else {
056a005c 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%"' );
e842b215 337 }
056a005c 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 }
3d0cada3 342 $email = $abook->full_address($row);
d62c4938 343 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
056a005c 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";
91821fc0 348 $line++;
daba719e 349 }
2741cc97 350 echo "</table>" .
056a005c 351 "\n<!-- end of address book table -->\n";
d9879e29 352
fc93f97c 353 /* End of list. Add edit/delete select buttons */
91821fc0 354 if ($headerprinted) {
fc93f97c 355 echo html_tag( 'table',
134e4174 356 html_tag( 'tr',
357 html_tag( 'td',
056a005c 358 addSubmit(_("Edit selected"), 'editaddr') .
359 addSubmit(_("Delete selected"), 'deladdr'),
134e4174 360 'center', '', 'colspan="5"' )
361 ),
056a005c 362 'center' );
91821fc0 363 }
fc93f97c 364 echo "</form>\n";
daba719e 365 }
f6c945b9 366} /* end of addresslist */
daba719e 367
368
f6c945b9 369/* Display the "new address" form */
c1ac62d4 370echo '<a name="AddAddress"></a>' . "\n";
371abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
fc93f97c 372echo "</form>\n";
daba719e 373
f6c945b9 374/* Add hook for anything that wants on the bottom */
daba719e 375do_hook('addressbook_bottom');
dcc1cc82 376?>
9f6c97c3 377</body></html>