Allow underscores in nicknames by changing id used in forms, add more labels for...
[squirrelmail.git] / src / addressbook.php
CommitLineData
abdfb4d0 1<?php
134e4174 2
35586184 3/**
4 * addressbook.php
5 *
35586184 6 * Manage personal address book.
7 *
4b5049de 8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
4b4abf93 9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
30967a1e 10 * @version $Id$
8f6f9ba5 11 * @package squirrelmail
de00443c 12 * @subpackage addressbook
35586184 13 */
14
ebd2391c 15/** This is the addressbook page */
16define('PAGE_NAME', 'addressbook');
17
30967a1e 18/**
202bcbcc 19 * Include the SquirrelMail initialization file.
30967a1e 20 */
202bcbcc 21include('../include/init.php');
86725763 22
8f6f9ba5 23/** SquirrelMail required files. */
df4162cb 24/* address book functions */
202bcbcc 25require_once(SM_PATH . 'functions/addressbook.php');
3f92c0c7 26include_once(SM_PATH . 'templates/util_addressbook.php');
27
df4162cb 28/* form functions */
202bcbcc 29require_once(SM_PATH . 'functions/forms.php');
ffd8224c 30
8f6f9ba5 31/** lets get the global vars we may need */
0b97a708 32
33/* From the address form */
a71b394e 34sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
35sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
36sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
f4f64c58 37sqgetGlobalVar('compose_to', $compose_to, SQ_POST);
a71b394e 38sqgetGlobalVar('sel', $sel, SQ_POST);
39sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
40sqgetGlobalVar('backend', $backend, SQ_POST);
41sqgetGlobalVar('doedit', $doedit, SQ_POST);
0b97a708 42
08e71631 43/* Get sorting order */
44$abook_sort_order = get_abook_sort();
45
703fa6b5 46/* Create page header before addressbook_init in order to display error messages correctly. */
876fdb60 47displayPageHeader($color);
703fa6b5 48
de00443c 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);
ced653f3 53
54// FIXME: do we have to stop use of address book, when localbackend is not present.
daba719e 55if($abook->localbackend == 0) {
a71b394e 56 plain_error_message(_("No personal address book is defined. Contact administrator."));
daba719e 57 exit();
58}
ffd8224c 59
a71b394e 60$current_backend = $abook->localbackend;
caa596b2 61if (sqgetGlobalVar('new_bnum', $new_backend, SQ_FORM)
62 && array_key_exists($new_backend, $abook->backends)) {
a71b394e 63 $current_backend = (int) $new_backend;
64}
65
66$abook_selection = '&nbsp;';
67$list_backends = array();
68if (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
daba719e 80$defdata = array();
81$formerror = '';
82$abortform = false;
83$showaddrlist = true;
84$defselected = array();
07dcee9f 85$form_url = 'addressbook.php';
daba719e 86
f6c945b9 87/* Handle user's actions */
1e12d1ff 88if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
daba719e 89
f6c945b9 90 /**************************************************
91 * Add new address *
92 **************************************************/
39b26252 93 if (isset($addaddr)) {
a123157f 94 if (isset($backend)) {
95 $r = $abook->add($addaddr, $backend);
96 } else {
97 $r = $abook->add($addaddr, $abook->localbackend);
c6554ec0 98 }
ffd8224c 99
f6c945b9 100 /* Handle error messages */
101 if (!$r) {
102 /* Remove backend name from error string */
ffd8224c 103 $errstr = $abook->error;
104 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
105
106 $formerror = $errstr;
107 $showaddrlist = false;
108 $defdata = $addaddr;
109 }
daba719e 110 } else {
ffd8224c 111
f6c945b9 112 /************************************************
113 * Delete address(es) *
114 ************************************************/
115 if ((!empty($deladdr)) && sizeof($sel) > 0) {
daba719e 116 $orig_sel = $sel;
117 sort($sel);
118
f4f64c58 119 /* The selected addresses are identified by "backend_nickname". *
f6c945b9 120 * Sort the list and process one backend at the time */
daba719e 121 $prevback = -1;
122 $subsel = array();
123 $delfailed = false;
124
f6c945b9 125 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
f4f64c58 126 list($sbackend, $snick) = explode('_', $sel[$i], 2);
daba719e 127
f6c945b9 128 /* When we get to a new backend, process addresses in *
129 * previous one. */
130 if ($prevback != $sbackend && $prevback != -1) {
daba719e 131
132 $r = $abook->remove($subsel, $prevback);
f6c945b9 133 if (!$r) {
daba719e 134 $formerror = $abook->error;
135 $i = sizeof($sel);
136 $delfailed = true;
137 break;
138 }
139 $subsel = array();
140 }
141
f6c945b9 142 /* Queue for processing */
daba719e 143 array_push($subsel, $snick);
144 $prevback = $sbackend;
ffd8224c 145 }
ffd8224c 146
f6c945b9 147 if (!$delfailed) {
daba719e 148 $r = $abook->remove($subsel, $prevback);
f6c945b9 149 if (!$r) { /* Handle errors */
daba719e 150 $formerror = $abook->error;
151 $delfailed = true;
152 }
ffd8224c 153 }
ffd8224c 154
f6c945b9 155 if ($delfailed) {
daba719e 156 $showaddrlist = true;
157 $defselected = $orig_sel;
ffd8224c 158 }
ffd8224c 159
f4f64c58 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
daba719e 201 } else {
202
f6c945b9 203 /***********************************************
204 * Update/modify address *
205 ***********************************************/
206 if (!empty($editaddr)) {
f6c945b9 207 /* Stage one: Copy data into form */
daba719e 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;
f4f64c58 215 list($ebackend, $enick) = explode('_', current($sel), 2);
daba719e 216 $olddata = $abook->lookup($enick, $ebackend);
5b61a40d 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 */
caa596b2 222 echo abook_create_form($form_url, 'editaddr',
223 _("Update address"),
224 _("Update address"),
225 $current_backend,
226 $olddata);
5b61a40d 227 echo addHidden('oldnick', $olddata['nickname']).
228 addHidden('backend', $olddata['backend']).
229 addHidden('doedit', '1').
230 '</form>';
231 }
daba719e 232 }
3e87f870 233 } elseif ($doedit == 1) {
f6c945b9 234 /* Stage two: Write new data */
3e87f870 235 $newdata = $editaddr;
236 $r = $abook->modify($oldnick, $newdata, $backend);
daba719e 237
3e87f870 238 /* Handle error messages */
239 if (!$r) {
240 /* Display error */
3c656c5f 241 plain_error_message( nl2br(htmlspecialchars($abook->error)));
3e87f870 242
243 /* Display the "new address" form again */
caa596b2 244 echo abook_create_form($form_url, 'editaddr',
245 _("Update address"),
246 _("Update address"),
247 $current_backend,
248 $newdata);
3e87f870 249 echo addHidden('oldnick', $oldnick).
250 addHidden('backend', $backend).
251 addHidden('doedit', '1').
252 "\n" . '</form>';
daba719e 253 $abortform = true;
254 }
3e87f870 255 } else {
256 /**
f8a1ed5a 257 * $editaddr is set, but $sel (address selection in address listing)
258 * and $doedit (address edit form) are not set.
3e87f870 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 */
f4f64c58 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 */
daba719e 268
269 // Some times we end output before forms are printed
270 if($abortform) {
056a005c 271 echo "</body></html>\n";
272 exit();
ffd8224c 273 }
daba719e 274}
ffd8224c 275
276
f6c945b9 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 * =================================================================== */
ffd8224c 281
f6c945b9 282/* Display error messages */
283if (!empty($formerror)) {
a71b394e 284 plain_error_message(nl2br(htmlspecialchars($formerror)));
daba719e 285}
ffd8224c 286
287
f6c945b9 288/* Display the address management part */
a71b394e 289$addresses = array();
290while (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);
5b1c13d3 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);
a71b394e 304
5b1c13d3 305 $addresses[$backend->bnum] = $a;
306 } else {
307 // list_addr() returns boolean
308 plain_error_message(nl2br(htmlspecialchars($abook->error)));
309 }
a71b394e 310}
daba719e 311
312
a71b394e 313if ($showaddrlist) {
caa596b2 314//FIXME: Remove HTML from here!
c7009c16 315 echo addForm($form_url, 'post', 'address_book_form');
a71b394e 316
f4f64c58 317 $oTemplate->assign('compose_new_win', $compose_new_win);
318 $oTemplate->assign('compose_height', $compose_height);
319 $oTemplate->assign('compose_width', $compose_width);
a71b394e 320 $oTemplate->assign('addresses', $addresses);
321 $oTemplate->assign('current_backend', $current_backend);
322 $oTemplate->assign('backends', $list_backends);
a140c3b1 323 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field);
a71b394e 324
325 $oTemplate->display('addressbook_list.tpl');
326
6e515418 327//FIXME: Remove HTML from here!
a71b394e 328 echo "</form>\n";
329}
330
f6c945b9 331/* Display the "new address" form */
caa596b2 332//FIXME: Remove HTML from here! (echo abook_create_form() is OK, since it is all template based output
c1ac62d4 333echo '<a name="AddAddress"></a>' . "\n";
caa596b2 334echo abook_create_form($form_url, 'addaddr',
335 _("Add to address book"),
336 _("Add address"),
337 $current_backend,
338 $defdata);
fc93f97c 339echo "</form>\n";
daba719e 340
df4162cb 341/* Hook for extra address book blocks */
6e515418 342do_hook('addressbook_bottom', $null);
a71b394e 343
5c4ff7bf 344$oTemplate->display('footer.tpl');