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