abdfb4d0 |
1 | <?php |
134e4174 |
2 | |
35586184 |
3 | /** |
4 | * addressbook.php |
5 | * |
35586184 |
6 | * Manage personal address book. |
7 | * |
47ccfad4 |
8 | * @copyright © 1999-2006 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 | |
30967a1e |
15 | /** |
202bcbcc |
16 | * Include the SquirrelMail initialization file. |
30967a1e |
17 | */ |
202bcbcc |
18 | include('../include/init.php'); |
86725763 |
19 | |
8f6f9ba5 |
20 | /** SquirrelMail required files. */ |
df4162cb |
21 | /* address book functions */ |
202bcbcc |
22 | require_once(SM_PATH . 'functions/addressbook.php'); |
3f92c0c7 |
23 | include_once(SM_PATH . 'templates/util_addressbook.php'); |
8ce0f071 |
24 | include_once(SM_PATH . 'templates/util_global.php'); |
3f92c0c7 |
25 | |
df4162cb |
26 | /* form functions */ |
202bcbcc |
27 | require_once(SM_PATH . 'functions/forms.php'); |
ffd8224c |
28 | |
8f6f9ba5 |
29 | /** lets get the global vars we may need */ |
0b97a708 |
30 | |
31 | /* From the address form */ |
a71b394e |
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); |
0b97a708 |
39 | |
08e71631 |
40 | /* Get sorting order */ |
41 | $abook_sort_order = get_abook_sort(); |
42 | |
703fa6b5 |
43 | /* Create page header before addressbook_init in order to display error messages correctly. */ |
44 | displayPageHeader($color, 'None'); |
45 | |
de00443c |
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); |
ced653f3 |
50 | |
51 | // FIXME: do we have to stop use of address book, when localbackend is not present. |
daba719e |
52 | if($abook->localbackend == 0) { |
a71b394e |
53 | plain_error_message(_("No personal address book is defined. Contact administrator.")); |
daba719e |
54 | exit(); |
55 | } |
ffd8224c |
56 | |
a71b394e |
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 = ' '; |
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 | |
daba719e |
76 | $defdata = array(); |
77 | $formerror = ''; |
78 | $abortform = false; |
79 | $showaddrlist = true; |
80 | $defselected = array(); |
07dcee9f |
81 | $form_url = 'addressbook.php'; |
daba719e |
82 | |
f6c945b9 |
83 | /* Handle user's actions */ |
1e12d1ff |
84 | if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') { |
daba719e |
85 | |
f6c945b9 |
86 | /************************************************** |
87 | * Add new address * |
88 | **************************************************/ |
39b26252 |
89 | if (isset($addaddr)) { |
a123157f |
90 | if (isset($backend)) { |
91 | $r = $abook->add($addaddr, $backend); |
92 | } else { |
93 | $r = $abook->add($addaddr, $abook->localbackend); |
c6554ec0 |
94 | } |
ffd8224c |
95 | |
f6c945b9 |
96 | /* Handle error messages */ |
97 | if (!$r) { |
98 | /* Remove backend name from error string */ |
ffd8224c |
99 | $errstr = $abook->error; |
100 | $errstr = ereg_replace('^\[.*\] *', '', $errstr); |
101 | |
102 | $formerror = $errstr; |
103 | $showaddrlist = false; |
104 | $defdata = $addaddr; |
105 | } |
daba719e |
106 | } else { |
ffd8224c |
107 | |
f6c945b9 |
108 | /************************************************ |
109 | * Delete address(es) * |
110 | ************************************************/ |
111 | if ((!empty($deladdr)) && sizeof($sel) > 0) { |
daba719e |
112 | $orig_sel = $sel; |
113 | sort($sel); |
114 | |
f6c945b9 |
115 | /* The selected addresses are identidied by "backend:nickname". * |
116 | * Sort the list and process one backend at the time */ |
daba719e |
117 | $prevback = -1; |
118 | $subsel = array(); |
119 | $delfailed = false; |
120 | |
f6c945b9 |
121 | for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) { |
daba719e |
122 | list($sbackend, $snick) = explode(':', $sel[$i]); |
123 | |
f6c945b9 |
124 | /* When we get to a new backend, process addresses in * |
125 | * previous one. */ |
126 | if ($prevback != $sbackend && $prevback != -1) { |
daba719e |
127 | |
128 | $r = $abook->remove($subsel, $prevback); |
f6c945b9 |
129 | if (!$r) { |
daba719e |
130 | $formerror = $abook->error; |
131 | $i = sizeof($sel); |
132 | $delfailed = true; |
133 | break; |
134 | } |
135 | $subsel = array(); |
136 | } |
137 | |
f6c945b9 |
138 | /* Queue for processing */ |
daba719e |
139 | array_push($subsel, $snick); |
140 | $prevback = $sbackend; |
ffd8224c |
141 | } |
ffd8224c |
142 | |
f6c945b9 |
143 | if (!$delfailed) { |
daba719e |
144 | $r = $abook->remove($subsel, $prevback); |
f6c945b9 |
145 | if (!$r) { /* Handle errors */ |
daba719e |
146 | $formerror = $abook->error; |
147 | $delfailed = true; |
148 | } |
ffd8224c |
149 | } |
ffd8224c |
150 | |
f6c945b9 |
151 | if ($delfailed) { |
daba719e |
152 | $showaddrlist = true; |
153 | $defselected = $orig_sel; |
ffd8224c |
154 | } |
ffd8224c |
155 | |
daba719e |
156 | } else { |
157 | |
f6c945b9 |
158 | /*********************************************** |
159 | * Update/modify address * |
160 | ***********************************************/ |
161 | if (!empty($editaddr)) { |
f6c945b9 |
162 | /* Stage one: Copy data into form */ |
daba719e |
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; |
5b61a40d |
170 | list($ebackend, $enick) = explode(':', current($sel)); |
daba719e |
171 | $olddata = $abook->lookup($enick, $ebackend); |
5b61a40d |
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 | } |
daba719e |
183 | } |
3e87f870 |
184 | } elseif ($doedit == 1) { |
f6c945b9 |
185 | /* Stage two: Write new data */ |
3e87f870 |
186 | $newdata = $editaddr; |
187 | $r = $abook->modify($oldnick, $newdata, $backend); |
daba719e |
188 | |
3e87f870 |
189 | /* Handle error messages */ |
190 | if (!$r) { |
191 | /* Display error */ |
6e3ec70b |
192 | plain_error_message( nl2br(htmlspecialchars(_("ERROR") .': '. $abook->error))); |
3e87f870 |
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>'; |
daba719e |
200 | $abortform = true; |
201 | } |
3e87f870 |
202 | } else { |
203 | /** |
f8a1ed5a |
204 | * $editaddr is set, but $sel (address selection in address listing) |
205 | * and $doedit (address edit form) are not set. |
3e87f870 |
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 */ |
daba719e |
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) { |
056a005c |
217 | echo "</body></html>\n"; |
218 | exit(); |
ffd8224c |
219 | } |
daba719e |
220 | } |
ffd8224c |
221 | |
222 | |
f6c945b9 |
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 | * =================================================================== */ |
ffd8224c |
227 | |
f6c945b9 |
228 | /* Display error messages */ |
229 | if (!empty($formerror)) { |
a71b394e |
230 | plain_error_message(nl2br(htmlspecialchars($formerror))); |
daba719e |
231 | } |
ffd8224c |
232 | |
233 | |
f6c945b9 |
234 | /* Display the address management part */ |
a71b394e |
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); |
5b1c13d3 |
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); |
a71b394e |
250 | |
5b1c13d3 |
251 | $addresses[$backend->bnum] = $a; |
252 | } else { |
253 | // list_addr() returns boolean |
254 | plain_error_message(nl2br(htmlspecialchars($abook->error))); |
255 | } |
a71b394e |
256 | } |
daba719e |
257 | |
258 | |
a71b394e |
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); |
a140c3b1 |
265 | $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field); |
a71b394e |
266 | |
267 | $oTemplate->display('addressbook_list.tpl'); |
268 | |
269 | echo "</form>\n"; |
270 | } |
271 | |
f6c945b9 |
272 | /* Display the "new address" form */ |
c1ac62d4 |
273 | echo '<a name="AddAddress"></a>' . "\n"; |
274 | abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata); |
fc93f97c |
275 | echo "</form>\n"; |
daba719e |
276 | |
df4162cb |
277 | /* Hook for extra address book blocks */ |
8811538c |
278 | echo "<!-- start of addressbook_bottom hook-->\n"; |
daba719e |
279 | do_hook('addressbook_bottom'); |
8811538c |
280 | echo "\n<!-- end of addressbook_bottom hook-->\n"; |
a71b394e |
281 | |
5c4ff7bf |
282 | $oTemplate->display('footer.tpl'); |
a71b394e |
283 | ?> |