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