Allow underscores in nicknames by changing id used in forms, add more labels for...
[squirrelmail.git] / src / addressbook.php
1 <?php
2
3 /**
4 * addressbook.php
5 *
6 * Manage personal address book.
7 *
8 * @copyright &copy; 1999-2007 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 /** This is the addressbook page */
16 define('PAGE_NAME', 'addressbook');
17
18 /**
19 * Include the SquirrelMail initialization file.
20 */
21 include('../include/init.php');
22
23 /** SquirrelMail required files. */
24 /* address book functions */
25 require_once(SM_PATH . 'functions/addressbook.php');
26 include_once(SM_PATH . 'templates/util_addressbook.php');
27
28 /* form functions */
29 require_once(SM_PATH . 'functions/forms.php');
30
31 /** lets get the global vars we may need */
32
33 /* From the address form */
34 sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
35 sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
36 sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
37 sqgetGlobalVar('compose_to', $compose_to, SQ_POST);
38 sqgetGlobalVar('sel', $sel, SQ_POST);
39 sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
40 sqgetGlobalVar('backend', $backend, SQ_POST);
41 sqgetGlobalVar('doedit', $doedit, SQ_POST);
42
43 /* Get sorting order */
44 $abook_sort_order = get_abook_sort();
45
46 /* Create page header before addressbook_init in order to display error messages correctly. */
47 displayPageHeader($color);
48
49 /* Open addressbook with error messages on.
50 remote backends (LDAP) are enabled because they can be used. (list_addr function)
51 */
52 $abook = addressbook_init(true, false);
53
54 // FIXME: do we have to stop use of address book, when localbackend is not present.
55 if($abook->localbackend == 0) {
56 plain_error_message(_("No personal address book is defined. Contact administrator."));
57 exit();
58 }
59
60 $current_backend = $abook->localbackend;
61 if (sqgetGlobalVar('new_bnum', $new_backend, SQ_FORM)
62 && array_key_exists($new_backend, $abook->backends)) {
63 $current_backend = (int) $new_backend;
64 }
65
66 $abook_selection = '&nbsp;';
67 $list_backends = array();
68 if (count($abook->backends) > 1) {
69 foreach($abook->get_backend_list() as $oBackend) {
70 if ($oBackend->listing) {
71 $list_backends[$oBackend->bnum]=$oBackend->sname;
72 }
73 }
74 if (count($list_backends)>1) {
75 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
76 .addSubmit(_("Change"),'change_abook');
77 }
78 }
79
80 $defdata = array();
81 $formerror = '';
82 $abortform = false;
83 $showaddrlist = true;
84 $defselected = array();
85 $form_url = 'addressbook.php';
86
87 /* Handle user's actions */
88 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
89
90 /**************************************************
91 * Add new address *
92 **************************************************/
93 if (isset($addaddr)) {
94 if (isset($backend)) {
95 $r = $abook->add($addaddr, $backend);
96 } else {
97 $r = $abook->add($addaddr, $abook->localbackend);
98 }
99
100 /* Handle error messages */
101 if (!$r) {
102 /* Remove backend name from error string */
103 $errstr = $abook->error;
104 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
105
106 $formerror = $errstr;
107 $showaddrlist = false;
108 $defdata = $addaddr;
109 }
110 } else {
111
112 /************************************************
113 * Delete address(es) *
114 ************************************************/
115 if ((!empty($deladdr)) && sizeof($sel) > 0) {
116 $orig_sel = $sel;
117 sort($sel);
118
119 /* The selected addresses are identified by "backend_nickname". *
120 * Sort the list and process one backend at the time */
121 $prevback = -1;
122 $subsel = array();
123 $delfailed = false;
124
125 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
126 list($sbackend, $snick) = explode('_', $sel[$i], 2);
127
128 /* When we get to a new backend, process addresses in *
129 * previous one. */
130 if ($prevback != $sbackend && $prevback != -1) {
131
132 $r = $abook->remove($subsel, $prevback);
133 if (!$r) {
134 $formerror = $abook->error;
135 $i = sizeof($sel);
136 $delfailed = true;
137 break;
138 }
139 $subsel = array();
140 }
141
142 /* Queue for processing */
143 array_push($subsel, $snick);
144 $prevback = $sbackend;
145 }
146
147 if (!$delfailed) {
148 $r = $abook->remove($subsel, $prevback);
149 if (!$r) { /* Handle errors */
150 $formerror = $abook->error;
151 $delfailed = true;
152 }
153 }
154
155 if ($delfailed) {
156 $showaddrlist = true;
157 $defselected = $orig_sel;
158 }
159
160 /************************************************
161 * Compose to selected address(es) *
162 ************************************************/
163 } else if ((!empty($compose_to)) && sizeof($sel) > 0) {
164 $orig_sel = $sel;
165 sort($sel);
166
167 // The selected addresses are identified by "backend_nickname"
168 $lookup_failed = false;
169 $send_to = '';
170
171 for ($i = 0 ; (($i < sizeof($sel)) && !$lookup_failed) ; $i++) {
172 list($sbackend, $snick) = explode('_', $sel[$i], 2);
173
174 $data = $abook->lookup($snick, $sbackend);
175
176 if (!$data) {
177 $formerror = $abook->error;
178 $lookup_failed = true;
179 break;
180 } else {
181 $addr = $abook->full_address($data);
182 if (!empty($addr))
183 $send_to .= $addr . ', ';
184 }
185 }
186
187
188 if ($lookup_failed || empty($send_to)) {
189 $showaddrlist = true;
190 $defselected = $sel;
191 }
192
193
194 // send off to compose screen
195 else {
196 $send_to = trim($send_to, ', ');
197 header('Location: ' . $base_uri . 'src/compose.php?send_to=' . rawurlencode($send_to));
198 exit;
199 }
200
201 } else {
202
203 /***********************************************
204 * Update/modify address *
205 ***********************************************/
206 if (!empty($editaddr)) {
207 /* Stage one: Copy data into form */
208 if (isset($sel) && sizeof($sel) > 0) {
209 if(sizeof($sel) > 1) {
210 $formerror = _("You can only edit one address at the time");
211 $showaddrlist = true;
212 $defselected = $sel;
213 } else {
214 $abortform = true;
215 list($ebackend, $enick) = explode('_', current($sel), 2);
216 $olddata = $abook->lookup($enick, $ebackend);
217 // Test if $olddata really contains anything and return an error message if it doesn't
218 if (!$olddata) {
219 error_box(nl2br(htmlspecialchars($abook->error)));
220 } else {
221 /* Display the "new address" form */
222 echo abook_create_form($form_url, 'editaddr',
223 _("Update address"),
224 _("Update address"),
225 $current_backend,
226 $olddata);
227 echo addHidden('oldnick', $olddata['nickname']).
228 addHidden('backend', $olddata['backend']).
229 addHidden('doedit', '1').
230 '</form>';
231 }
232 }
233 } elseif ($doedit == 1) {
234 /* Stage two: Write new data */
235 $newdata = $editaddr;
236 $r = $abook->modify($oldnick, $newdata, $backend);
237
238 /* Handle error messages */
239 if (!$r) {
240 /* Display error */
241 plain_error_message( nl2br(htmlspecialchars($abook->error)));
242
243 /* Display the "new address" form again */
244 echo abook_create_form($form_url, 'editaddr',
245 _("Update address"),
246 _("Update address"),
247 $current_backend,
248 $newdata);
249 echo addHidden('oldnick', $oldnick).
250 addHidden('backend', $backend).
251 addHidden('doedit', '1').
252 "\n" . '</form>';
253 $abortform = true;
254 }
255 } else {
256 /**
257 * $editaddr is set, but $sel (address selection in address listing)
258 * and $doedit (address edit form) are not set.
259 * Assume that user clicked on "Edit address" without selecting any address.
260 */
261 $formerror = _("Please select address that you want to edit");
262 $showaddrlist = true;
263 } /* end of edit stage detection */
264 } /* !empty($editaddr) - Update/modify address */
265 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es)
266 or (!empty($compose_to)) && sizeof($sel) > 0 - Compose to address(es) */
267 } /* !empty($addaddr['nickname']) - Add new address */
268
269 // Some times we end output before forms are printed
270 if($abortform) {
271 echo "</body></html>\n";
272 exit();
273 }
274 }
275
276
277 /* =================================================================== *
278 * The following is only executed on a GET request, or on a POST when *
279 * a user is added, or when "delete" or "modify" was successful. *
280 * =================================================================== */
281
282 /* Display error messages */
283 if (!empty($formerror)) {
284 plain_error_message(nl2br(htmlspecialchars($formerror)));
285 }
286
287
288 /* Display the address management part */
289 $addresses = array();
290 while (list($k, $backend) = each ($abook->backends)) {
291 $a = array();
292 $a['BackendID'] = $backend->bnum;
293 $a['BackendSource'] = $backend->sname;
294 $a['BackendWritable'] = $backend->writeable;
295 $a['Addresses'] = array();
296
297 $alist = $abook->list_addr($backend->bnum);
298
299 /* check return (array with data or boolean false) */
300 if (is_array($alist)) {
301 usort($alist,'alistcmp');
302
303 $a['Addresses'] = formatAddressList($alist);
304
305 $addresses[$backend->bnum] = $a;
306 } else {
307 // list_addr() returns boolean
308 plain_error_message(nl2br(htmlspecialchars($abook->error)));
309 }
310 }
311
312
313 if ($showaddrlist) {
314 //FIXME: Remove HTML from here!
315 echo addForm($form_url, 'post', 'address_book_form');
316
317 $oTemplate->assign('compose_new_win', $compose_new_win);
318 $oTemplate->assign('compose_height', $compose_height);
319 $oTemplate->assign('compose_width', $compose_width);
320 $oTemplate->assign('addresses', $addresses);
321 $oTemplate->assign('current_backend', $current_backend);
322 $oTemplate->assign('backends', $list_backends);
323 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field);
324
325 $oTemplate->display('addressbook_list.tpl');
326
327 //FIXME: Remove HTML from here!
328 echo "</form>\n";
329 }
330
331 /* Display the "new address" form */
332 //FIXME: Remove HTML from here! (echo abook_create_form() is OK, since it is all template based output
333 echo '<a name="AddAddress"></a>' . "\n";
334 echo abook_create_form($form_url, 'addaddr',
335 _("Add to address book"),
336 _("Add address"),
337 $current_backend,
338 $defdata);
339 echo "</form>\n";
340
341 /* Hook for extra address book blocks */
342 do_hook('addressbook_bottom', $null);
343
344 $oTemplate->display('footer.tpl');