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