d460f6379acb59f01e7b0314601b8f9fcf79ec2c
[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 include_once(SM_PATH . 'include/validate.php');
23 /* plain_error_message() */
24 include_once(SM_PATH . 'functions/display_messages.php');
25 /* address book functions */
26 include_once(SM_PATH . 'functions/addressbook.php');
27 /* form functions */
28 include_once(SM_PATH . 'functions/forms.php');
29
30 /** lets get the global vars we may need */
31
32 /* From the address form */
33 sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
34 sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
35 sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
36 sqgetGlobalVar('sel', $sel, SQ_POST);
37 sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
38 sqgetGlobalVar('backend', $backend, SQ_POST);
39 sqgetGlobalVar('doedit', $doedit, SQ_POST);
40
41 /* Get sorting order */
42 $abook_sort_order = get_abook_sort();
43
44 /* Create page header before addressbook_init in order to display error messages correctly. */
45 displayPageHeader($color, 'None');
46
47 /* Open addressbook with error messages on.
48 remote backends (LDAP) are enabled because they can be used. (list_addr function)
49 */
50 $abook = addressbook_init(true, false);
51
52 // FIXME: do we have to stop use of address book, when localbackend is not present.
53 if($abook->localbackend == 0) {
54 plain_error_message(
55 _("No personal address book is defined. Contact administrator."),
56 $color);
57 exit();
58 }
59
60 $defdata = array();
61 $formerror = '';
62 $abortform = false;
63 $showaddrlist = true;
64 $defselected = array();
65 $form_url = 'addressbook.php';
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 /* Stage one: Copy data into form */
147 if (isset($sel) && sizeof($sel) > 0) {
148 if(sizeof($sel) > 1) {
149 $formerror = _("You can only edit one address at the time");
150 $showaddrlist = true;
151 $defselected = $sel;
152 } else {
153 $abortform = true;
154 list($ebackend, $enick) = explode(':', $sel[0]);
155 $olddata = $abook->lookup($enick, $ebackend);
156 // FIXME: Test if $olddata really contains anything and return an error message if it doesn't
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 } elseif ($doedit == 1) {
166 /* Stage two: Write new data */
167 $newdata = $editaddr;
168 $r = $abook->modify($oldnick, $newdata, $backend);
169
170 /* Handle error messages */
171 if (!$r) {
172 /* Display error */
173 echo html_tag( 'table',
174 html_tag( 'tr',
175 html_tag( 'td',
176 "\n". '<strong><font color="' . $color[2] .
177 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
178 'center' )
179 ),
180 'center', '', 'width="100%"' );
181
182 /* Display the "new address" form again */
183 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
184 echo addHidden('oldnick', $oldnick).
185 addHidden('backend', $backend).
186 addHidden('doedit', '1').
187 "\n" . '</form>';
188 $abortform = true;
189 }
190 } else {
191 /**
192 * $editaddr is set, but $sel (address selection in address listing)
193 * and $doedit (address edit form) are not set.
194 * Assume that user clicked on "Edit address" without selecting any address.
195 */
196 $formerror = _("Please select address that you want to edit");
197 $showaddrlist = true;
198 } /* end of edit stage detection */
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 if ($abook->add_extra_field) {
248 $abook_fields = 6;
249 } else {
250 $abook_fields = 5;
251 }
252 while(list($undef,$row) = each($alist)) {
253
254 /* New table header for each backend */
255 if($prevbackend != $row['backend']) {
256 if($prevbackend < 0) {
257 echo html_tag( 'table',
258 html_tag( 'tr',
259 html_tag( 'td',
260 addSubmit(_("Edit selected"), 'editaddr').
261 addSubmit(_("Delete selected"), 'deladdr'),
262 'center', '', "colspan=\"$abook_fields\"" )
263 ) .
264 html_tag( 'tr',
265 html_tag( 'td', '&nbsp;<br />', 'center', '', "colspan=\"$abook_fields\"" )
266 ),
267 'center' );
268 echo "\n<!-- start of address book table -->\n" .
269 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
270 html_tag( 'tr', "\n" .
271 html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) . "\n" .
272 html_tag( 'th', _("Nickname") .
273 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
274 'left', '', 'width="1%"' ) . "\n" .
275 html_tag( 'th', _("Name") .
276 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
277 'left', '', 'width="1%"' ) . "\n" .
278 html_tag( 'th', _("E-mail") .
279 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
280 'left', '', 'width="1%"' ) . "\n" .
281 html_tag( 'th', _("Info") .
282 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
283 'left', '', 'width="1%"' ) .
284 ($abook->add_extra_field ? html_tag( 'th', '&nbsp;','left', '', 'width="1%"'): '') .
285 "\n",
286 '', $color[9] ) . "\n";
287 }
288
289 // Separate different backends with <hr />
290 if($prevbackend > 0) {
291 echo html_tag( 'tr',
292 html_tag( 'td', "<hr />", 'center', '' ,"colspan=\"$abook_fields\"" )
293 );
294 }
295
296 // Print backend name
297 echo html_tag( 'tr',
298 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,"colspan=\"$abook_fields\"" )
299 );
300
301 $line = 0;
302 $headerprinted = true;
303 } /* End of header */
304
305 $prevbackend = $row['backend'];
306
307 /* Check if this user is selected */
308 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
309
310 /* Print one row, with alternating color */
311 if ($line % 2) {
312 $tr_bgcolor = $color[12];
313 } else {
314 $tr_bgcolor = $color[4];
315 }
316 echo html_tag( 'tr', '', '', $tr_bgcolor);
317 if ($abook->backends[$row['backend']]->writeable) {
318 echo html_tag( 'td',
319 '<small>' .
320 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
321 '</small>' ,
322 'center', '', 'valign="top" width="1%"' );
323 } else {
324 echo html_tag( 'td',
325 '&nbsp;' ,
326 'center', '', 'valign="top" width="1%"' );
327 }
328 echo html_tag( 'td',
329 '&nbsp;' . htmlspecialchars($row['nickname']) . '&nbsp;',
330 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
331
332 // different full name display formating for Japanese translation
333 if ($squirrelmail_language == 'ja_JP') {
334 /*
335 * translation uses euc-jp character set internally.
336 * htmlspecialchars() should not break any characters.
337 */
338 echo html_tag( 'td',
339 '&nbsp;' . htmlspecialchars($row['lastname']) . ' ' . htmlspecialchars($row['firstname']) . '&nbsp;',
340 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
341 } else {
342 echo html_tag( 'td',
343 '&nbsp;' . htmlspecialchars($row['name']) . '&nbsp;',
344 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
345 }
346
347 // email address column
348 echo html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
349 $email = $abook->full_address($row);
350 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
351 htmlspecialchars($row['email'])).
352 '&nbsp;</td>'."\n";
353
354 // info column
355 echo html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' );
356
357 // add extra column if third party backend needs it
358 if ($abook->add_extra_field) {
359 echo html_tag( 'td',
360 '&nbsp;' . (isset($row['extra']) ? $row['extra'] : '') . '&nbsp;',
361 'left', '', 'valign="top" width="1%"' );
362 }
363 echo "</tr>\n";
364 $line++;
365 }
366 echo "</table>" .
367 "\n<!-- end of address book table -->\n";
368
369 /* End of list. Add edit/delete select buttons */
370 if ($headerprinted) {
371 echo html_tag( 'table',
372 html_tag( 'tr',
373 html_tag( 'td',
374 addSubmit(_("Edit selected"), 'editaddr') .
375 addSubmit(_("Delete selected"), 'deladdr'),
376 'center', '', "colspan=\"$abook_fields\"" )
377 ),
378 'center' );
379 }
380 echo "</form>\n";
381 }
382 } /* end of addresslist */
383
384
385 /* Display the "new address" form */
386 echo '<a name="AddAddress"></a>' . "\n";
387 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
388 echo "</form>\n";
389
390 /* Hook for extra address book blocks */
391 echo "<!-- start of addressbook_bottom hook-->\n";
392 do_hook('addressbook_bottom');
393 echo "\n<!-- end of addressbook_bottom hook-->\n";
394 $oTemplate->display('footer.tpl');
395 ?>