Give main abook form a name
[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);
37sqgetGlobalVar('sel', $sel, SQ_POST);
38sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
39sqgetGlobalVar('backend', $backend, SQ_POST);
40sqgetGlobalVar('doedit', $doedit, SQ_POST);
0b97a708 41
08e71631 42/* Get sorting order */
43$abook_sort_order = get_abook_sort();
44
703fa6b5 45/* Create page header before addressbook_init in order to display error messages correctly. */
876fdb60 46displayPageHeader($color);
703fa6b5 47
de00443c 48/* Open addressbook with error messages on.
49 remote backends (LDAP) are enabled because they can be used. (list_addr function)
50*/
51$abook = addressbook_init(true, false);
ced653f3 52
53// FIXME: do we have to stop use of address book, when localbackend is not present.
daba719e 54if($abook->localbackend == 0) {
a71b394e 55 plain_error_message(_("No personal address book is defined. Contact administrator."));
daba719e 56 exit();
57}
ffd8224c 58
a71b394e 59$current_backend = $abook->localbackend;
60if (sqgetGlobalVar('new_bnum',$new_backend,SQ_POST) && array_key_exists($new_backend,$abook->backends)) {
61 $current_backend = (int) $new_backend;
62}
63
64$abook_selection = '&nbsp;';
65$list_backends = array();
66if (count($abook->backends) > 1) {
67 foreach($abook->get_backend_list() as $oBackend) {
68 if ($oBackend->listing) {
69 $list_backends[$oBackend->bnum]=$oBackend->sname;
70 }
71 }
72 if (count($list_backends)>1) {
73 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
74 .addSubmit(_("Change"),'change_abook');
75 }
76}
77
daba719e 78$defdata = array();
79$formerror = '';
80$abortform = false;
81$showaddrlist = true;
82$defselected = array();
07dcee9f 83$form_url = 'addressbook.php';
daba719e 84
f6c945b9 85/* Handle user's actions */
1e12d1ff 86if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
daba719e 87
f6c945b9 88 /**************************************************
89 * Add new address *
90 **************************************************/
39b26252 91 if (isset($addaddr)) {
a123157f 92 if (isset($backend)) {
93 $r = $abook->add($addaddr, $backend);
94 } else {
95 $r = $abook->add($addaddr, $abook->localbackend);
c6554ec0 96 }
ffd8224c 97
f6c945b9 98 /* Handle error messages */
99 if (!$r) {
100 /* Remove backend name from error string */
ffd8224c 101 $errstr = $abook->error;
102 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
103
104 $formerror = $errstr;
105 $showaddrlist = false;
106 $defdata = $addaddr;
107 }
daba719e 108 } else {
ffd8224c 109
f6c945b9 110 /************************************************
111 * Delete address(es) *
112 ************************************************/
113 if ((!empty($deladdr)) && sizeof($sel) > 0) {
daba719e 114 $orig_sel = $sel;
115 sort($sel);
116
3c656c5f 117 /* The selected addresses are identidied by "nickname_backend". *
f6c945b9 118 * Sort the list and process one backend at the time */
daba719e 119 $prevback = -1;
120 $subsel = array();
121 $delfailed = false;
122
f6c945b9 123 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
3c656c5f 124 list($snick, $sbackend) = explode('_', $sel[$i]);
daba719e 125
f6c945b9 126 /* When we get to a new backend, process addresses in *
127 * previous one. */
128 if ($prevback != $sbackend && $prevback != -1) {
daba719e 129
130 $r = $abook->remove($subsel, $prevback);
f6c945b9 131 if (!$r) {
daba719e 132 $formerror = $abook->error;
133 $i = sizeof($sel);
134 $delfailed = true;
135 break;
136 }
137 $subsel = array();
138 }
139
f6c945b9 140 /* Queue for processing */
daba719e 141 array_push($subsel, $snick);
142 $prevback = $sbackend;
ffd8224c 143 }
ffd8224c 144
f6c945b9 145 if (!$delfailed) {
daba719e 146 $r = $abook->remove($subsel, $prevback);
f6c945b9 147 if (!$r) { /* Handle errors */
daba719e 148 $formerror = $abook->error;
149 $delfailed = true;
150 }
ffd8224c 151 }
ffd8224c 152
f6c945b9 153 if ($delfailed) {
daba719e 154 $showaddrlist = true;
155 $defselected = $orig_sel;
ffd8224c 156 }
ffd8224c 157
daba719e 158 } else {
159
f6c945b9 160 /***********************************************
161 * Update/modify address *
162 ***********************************************/
163 if (!empty($editaddr)) {
f6c945b9 164 /* Stage one: Copy data into form */
daba719e 165 if (isset($sel) && sizeof($sel) > 0) {
166 if(sizeof($sel) > 1) {
167 $formerror = _("You can only edit one address at the time");
168 $showaddrlist = true;
169 $defselected = $sel;
170 } else {
171 $abortform = true;
3c656c5f 172 list($enick, $ebackend) = explode('_', current($sel));
daba719e 173 $olddata = $abook->lookup($enick, $ebackend);
5b61a40d 174 // Test if $olddata really contains anything and return an error message if it doesn't
175 if (!$olddata) {
176 error_box(nl2br(htmlspecialchars($abook->error)));
177 } else {
178 /* Display the "new address" form */
179 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
180 echo addHidden('oldnick', $olddata['nickname']).
181 addHidden('backend', $olddata['backend']).
182 addHidden('doedit', '1').
183 '</form>';
184 }
daba719e 185 }
3e87f870 186 } elseif ($doedit == 1) {
f6c945b9 187 /* Stage two: Write new data */
3e87f870 188 $newdata = $editaddr;
189 $r = $abook->modify($oldnick, $newdata, $backend);
daba719e 190
3e87f870 191 /* Handle error messages */
192 if (!$r) {
193 /* Display error */
3c656c5f 194 plain_error_message( nl2br(htmlspecialchars($abook->error)));
3e87f870 195
196 /* Display the "new address" form again */
197 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
198 echo addHidden('oldnick', $oldnick).
199 addHidden('backend', $backend).
200 addHidden('doedit', '1').
201 "\n" . '</form>';
daba719e 202 $abortform = true;
203 }
3e87f870 204 } else {
205 /**
f8a1ed5a 206 * $editaddr is set, but $sel (address selection in address listing)
207 * and $doedit (address edit form) are not set.
3e87f870 208 * Assume that user clicked on "Edit address" without selecting any address.
209 */
210 $formerror = _("Please select address that you want to edit");
211 $showaddrlist = true;
212 } /* end of edit stage detection */
daba719e 213 } /* !empty($editaddr) - Update/modify address */
214 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
215 } /* !empty($addaddr['nickname']) - Add new address */
216
217 // Some times we end output before forms are printed
218 if($abortform) {
056a005c 219 echo "</body></html>\n";
220 exit();
ffd8224c 221 }
daba719e 222}
ffd8224c 223
224
f6c945b9 225/* =================================================================== *
226 * The following is only executed on a GET request, or on a POST when *
227 * a user is added, or when "delete" or "modify" was successful. *
228 * =================================================================== */
ffd8224c 229
f6c945b9 230/* Display error messages */
231if (!empty($formerror)) {
a71b394e 232 plain_error_message(nl2br(htmlspecialchars($formerror)));
daba719e 233}
ffd8224c 234
235
f6c945b9 236/* Display the address management part */
a71b394e 237$addresses = array();
238while (list($k, $backend) = each ($abook->backends)) {
239 $a = array();
240 $a['BackendID'] = $backend->bnum;
241 $a['BackendSource'] = $backend->sname;
242 $a['BackendWritable'] = $backend->writeable;
243 $a['Addresses'] = array();
244
245 $alist = $abook->list_addr($backend->bnum);
5b1c13d3 246
247 /* check return (array with data or boolean false) */
248 if (is_array($alist)) {
249 usort($alist,'alistcmp');
250
251 $a['Addresses'] = formatAddressList($alist);
a71b394e 252
5b1c13d3 253 $addresses[$backend->bnum] = $a;
254 } else {
255 // list_addr() returns boolean
256 plain_error_message(nl2br(htmlspecialchars($abook->error)));
257 }
a71b394e 258}
daba719e 259
260
a71b394e 261if ($showaddrlist) {
c7009c16 262 echo addForm($form_url, 'post', 'address_book_form');
a71b394e 263
264 $oTemplate->assign('addresses', $addresses);
265 $oTemplate->assign('current_backend', $current_backend);
266 $oTemplate->assign('backends', $list_backends);
a140c3b1 267 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field);
a71b394e 268
269 $oTemplate->display('addressbook_list.tpl');
270
6e515418 271//FIXME: Remove HTML from here!
a71b394e 272 echo "</form>\n";
273}
274
f6c945b9 275/* Display the "new address" form */
6e515418 276//FIXME: Remove HTML from here!
c1ac62d4 277echo '<a name="AddAddress"></a>' . "\n";
278abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
fc93f97c 279echo "</form>\n";
daba719e 280
df4162cb 281/* Hook for extra address book blocks */
6e515418 282do_hook('addressbook_bottom', $null);
a71b394e 283
5c4ff7bf 284$oTemplate->display('footer.tpl');