phpDocumentor updates
[squirrelmail.git] / src / addressbook.php
1 <?php
2
3 /**
4 * addressbook.php
5 *
6 * Manage personal address book.
7 *
8 * @copyright &copy; 1999-2005 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 while(list($undef,$row) = each($alist)) {
251
252 /* New table header for each backend */
253 if($prevbackend != $row['backend']) {
254 if($prevbackend < 0) {
255 echo html_tag( 'table',
256 html_tag( 'tr',
257 html_tag( 'td',
258 addSubmit(_("Edit selected"), 'editaddr').
259 addSubmit(_("Delete selected"), 'deladdr'),
260 'center', '', 'colspan="5"' )
261 ) .
262 html_tag( 'tr',
263 html_tag( 'td', '&nbsp;<br />', 'center', '', 'colspan="5"' )
264 ),
265 'center' );
266 echo "\n<!-- start of address book table -->\n" .
267 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
268 html_tag( 'tr', "\n" .
269 html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) . "\n" .
270 html_tag( 'th', _("Nickname") .
271 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
272 'left', '', 'width="1%"' ) . "\n" .
273 html_tag( 'th', _("Name") .
274 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
275 'left', '', 'width="1%"' ) . "\n" .
276 html_tag( 'th', _("E-mail") .
277 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
278 'left', '', 'width="1%"' ) . "\n" .
279 html_tag( 'th', _("Info") .
280 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
281 'left', '', 'width="1%"' ) . "\n",
282 '', $color[9] ) . "\n";
283 }
284
285 // Separate different backends with <hr />
286 if($prevbackend > 0) {
287 echo html_tag( 'tr',
288 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
289 );
290 }
291
292 // Print backend name
293 echo html_tag( 'tr',
294 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
295 );
296
297 $line = 0;
298 $headerprinted = true;
299 } /* End of header */
300
301 $prevbackend = $row['backend'];
302
303 /* Check if this user is selected */
304 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
305
306 /* Print one row, with alternating color */
307 if ($line % 2) {
308 $tr_bgcolor = $color[12];
309 } else {
310 $tr_bgcolor = $color[4];
311 }
312 if ($squirrelmail_language == 'ja_JP') {
313 echo html_tag( 'tr', '', '', $tr_bgcolor);
314 if ($abook->backends[$row['backend']]->writeable) {
315 echo html_tag( 'td',
316 '<small>' .
317 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
318 '</small>' ,
319 'center', '', 'valign="top" width="1%"' );
320 } else {
321 echo html_tag( 'td',
322 '&nbsp;' ,
323 'center', '', 'valign="top" width="1%"' );
324 }
325 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
326 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
327 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
328 } else {
329 echo html_tag( 'tr', '', '', $tr_bgcolor);
330 if ($abook->backends[$row['backend']]->writeable) {
331 echo html_tag( 'td',
332 '<small>' .
333 addCheckBox('sel[]', $selected, $row['backend'] . ':' . $row['nickname']).
334 '</small>' ,
335 'center', '', 'valign="top" width="1%"' );
336 } else {
337 echo html_tag( 'td',
338 '&nbsp;' ,
339 'center', '', 'valign="top" width="1%"' );
340 }
341 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
342 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
343 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
344 }
345 $email = $abook->full_address($row);
346 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
347 htmlspecialchars($row['email'])).
348 '&nbsp;</td>'."\n".
349 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
350 "</tr>\n";
351 $line++;
352 }
353 echo "</table>" .
354 "\n<!-- end of address book table -->\n";
355
356 /* End of list. Add edit/delete select buttons */
357 if ($headerprinted) {
358 echo html_tag( 'table',
359 html_tag( 'tr',
360 html_tag( 'td',
361 addSubmit(_("Edit selected"), 'editaddr') .
362 addSubmit(_("Delete selected"), 'deladdr'),
363 'center', '', 'colspan="5"' )
364 ),
365 'center' );
366 }
367 echo "</form>\n";
368 }
369 } /* end of addresslist */
370
371
372 /* Display the "new address" form */
373 echo '<a name="AddAddress"></a>' . "\n";
374 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
375 echo "</form>\n";
376
377 /* Add hook for anything that wants on the bottom */
378 echo "<!-- start of addressbook_bottom hook-->\n";
379 do_hook('addressbook_bottom');
380 echo "\n<!-- end of addressbook_bottom hook-->\n";
381
382 ?>
383 </body></html>