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'); |
24 | |
df4162cb |
25 | /* form functions */ |
202bcbcc |
26 | require_once(SM_PATH . 'functions/forms.php'); |
ffd8224c |
27 | |
8f6f9ba5 |
28 | /** lets get the global vars we may need */ |
0b97a708 |
29 | |
30 | /* From the address form */ |
a71b394e |
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); |
0b97a708 |
38 | |
08e71631 |
39 | /* Get sorting order */ |
40 | $abook_sort_order = get_abook_sort(); |
41 | |
703fa6b5 |
42 | /* Create page header before addressbook_init in order to display error messages correctly. */ |
43 | displayPageHeader($color, 'None'); |
44 | |
de00443c |
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); |
ced653f3 |
49 | |
50 | // FIXME: do we have to stop use of address book, when localbackend is not present. |
daba719e |
51 | if($abook->localbackend == 0) { |
a71b394e |
52 | plain_error_message(_("No personal address book is defined. Contact administrator.")); |
daba719e |
53 | exit(); |
54 | } |
ffd8224c |
55 | |
a71b394e |
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 = ' '; |
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 | |
daba719e |
75 | $defdata = array(); |
76 | $formerror = ''; |
77 | $abortform = false; |
78 | $showaddrlist = true; |
79 | $defselected = array(); |
07dcee9f |
80 | $form_url = 'addressbook.php'; |
daba719e |
81 | |
f6c945b9 |
82 | /* Handle user's actions */ |
1e12d1ff |
83 | if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') { |
daba719e |
84 | |
f6c945b9 |
85 | /************************************************** |
86 | * Add new address * |
87 | **************************************************/ |
39b26252 |
88 | if (isset($addaddr)) { |
a123157f |
89 | if (isset($backend)) { |
90 | $r = $abook->add($addaddr, $backend); |
91 | } else { |
92 | $r = $abook->add($addaddr, $abook->localbackend); |
c6554ec0 |
93 | } |
ffd8224c |
94 | |
f6c945b9 |
95 | /* Handle error messages */ |
96 | if (!$r) { |
97 | /* Remove backend name from error string */ |
ffd8224c |
98 | $errstr = $abook->error; |
99 | $errstr = ereg_replace('^\[.*\] *', '', $errstr); |
100 | |
101 | $formerror = $errstr; |
102 | $showaddrlist = false; |
103 | $defdata = $addaddr; |
104 | } |
daba719e |
105 | } else { |
ffd8224c |
106 | |
f6c945b9 |
107 | /************************************************ |
108 | * Delete address(es) * |
109 | ************************************************/ |
110 | if ((!empty($deladdr)) && sizeof($sel) > 0) { |
daba719e |
111 | $orig_sel = $sel; |
112 | sort($sel); |
113 | |
f6c945b9 |
114 | /* The selected addresses are identidied by "backend:nickname". * |
115 | * Sort the list and process one backend at the time */ |
daba719e |
116 | $prevback = -1; |
117 | $subsel = array(); |
118 | $delfailed = false; |
119 | |
f6c945b9 |
120 | for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) { |
daba719e |
121 | list($sbackend, $snick) = explode(':', $sel[$i]); |
122 | |
f6c945b9 |
123 | /* When we get to a new backend, process addresses in * |
124 | * previous one. */ |
125 | if ($prevback != $sbackend && $prevback != -1) { |
daba719e |
126 | |
127 | $r = $abook->remove($subsel, $prevback); |
f6c945b9 |
128 | if (!$r) { |
daba719e |
129 | $formerror = $abook->error; |
130 | $i = sizeof($sel); |
131 | $delfailed = true; |
132 | break; |
133 | } |
134 | $subsel = array(); |
135 | } |
136 | |
f6c945b9 |
137 | /* Queue for processing */ |
daba719e |
138 | array_push($subsel, $snick); |
139 | $prevback = $sbackend; |
ffd8224c |
140 | } |
ffd8224c |
141 | |
f6c945b9 |
142 | if (!$delfailed) { |
daba719e |
143 | $r = $abook->remove($subsel, $prevback); |
f6c945b9 |
144 | if (!$r) { /* Handle errors */ |
daba719e |
145 | $formerror = $abook->error; |
146 | $delfailed = true; |
147 | } |
ffd8224c |
148 | } |
ffd8224c |
149 | |
f6c945b9 |
150 | if ($delfailed) { |
daba719e |
151 | $showaddrlist = true; |
152 | $defselected = $orig_sel; |
ffd8224c |
153 | } |
ffd8224c |
154 | |
daba719e |
155 | } else { |
156 | |
f6c945b9 |
157 | /*********************************************** |
158 | * Update/modify address * |
159 | ***********************************************/ |
160 | if (!empty($editaddr)) { |
f6c945b9 |
161 | /* Stage one: Copy data into form */ |
daba719e |
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; |
5b61a40d |
169 | list($ebackend, $enick) = explode(':', current($sel)); |
daba719e |
170 | $olddata = $abook->lookup($enick, $ebackend); |
5b61a40d |
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 | } |
daba719e |
182 | } |
3e87f870 |
183 | } elseif ($doedit == 1) { |
f6c945b9 |
184 | /* Stage two: Write new data */ |
3e87f870 |
185 | $newdata = $editaddr; |
186 | $r = $abook->modify($oldnick, $newdata, $backend); |
daba719e |
187 | |
3e87f870 |
188 | /* Handle error messages */ |
189 | if (!$r) { |
190 | /* Display error */ |
191 | echo html_tag( 'table', |
a2b193bc |
192 | html_tag( 'tr', |
193 | html_tag( 'td', |
194 | "\n". '<strong><font color="' . $color[2] . |
195 | '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n", |
196 | 'center' ) |
197 | ), |
198 | 'center', '', 'width="100%"' ); |
3e87f870 |
199 | |
200 | /* Display the "new address" form again */ |
201 | abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata); |
202 | echo addHidden('oldnick', $oldnick). |
203 | addHidden('backend', $backend). |
204 | addHidden('doedit', '1'). |
205 | "\n" . '</form>'; |
daba719e |
206 | $abortform = true; |
207 | } |
3e87f870 |
208 | } else { |
209 | /** |
f8a1ed5a |
210 | * $editaddr is set, but $sel (address selection in address listing) |
211 | * and $doedit (address edit form) are not set. |
3e87f870 |
212 | * Assume that user clicked on "Edit address" without selecting any address. |
213 | */ |
214 | $formerror = _("Please select address that you want to edit"); |
215 | $showaddrlist = true; |
216 | } /* end of edit stage detection */ |
daba719e |
217 | } /* !empty($editaddr) - Update/modify address */ |
218 | } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */ |
219 | } /* !empty($addaddr['nickname']) - Add new address */ |
220 | |
221 | // Some times we end output before forms are printed |
222 | if($abortform) { |
056a005c |
223 | echo "</body></html>\n"; |
224 | exit(); |
ffd8224c |
225 | } |
daba719e |
226 | } |
ffd8224c |
227 | |
228 | |
f6c945b9 |
229 | /* =================================================================== * |
230 | * The following is only executed on a GET request, or on a POST when * |
231 | * a user is added, or when "delete" or "modify" was successful. * |
232 | * =================================================================== */ |
ffd8224c |
233 | |
f6c945b9 |
234 | /* Display error messages */ |
235 | if (!empty($formerror)) { |
a71b394e |
236 | plain_error_message(nl2br(htmlspecialchars($formerror))); |
daba719e |
237 | } |
ffd8224c |
238 | |
239 | |
f6c945b9 |
240 | /* Display the address management part */ |
a71b394e |
241 | $addresses = array(); |
242 | while (list($k, $backend) = each ($abook->backends)) { |
243 | $a = array(); |
244 | $a['BackendID'] = $backend->bnum; |
245 | $a['BackendSource'] = $backend->sname; |
246 | $a['BackendWritable'] = $backend->writeable; |
247 | $a['Addresses'] = array(); |
248 | |
249 | $alist = $abook->list_addr($backend->bnum); |
daba719e |
250 | usort($alist,'alistcmp'); |
a71b394e |
251 | $start = 200; |
252 | $count = count($alist); |
253 | if ($start >= $count) $start = 0; |
254 | $alist = array_slice($alist,$start,15); |
255 | |
3f92c0c7 |
256 | $a['Addresses'] = formatAddressList($alist); |
a71b394e |
257 | |
258 | $addresses[$backend->bnum] = $a; |
259 | } |
daba719e |
260 | |
261 | |
a71b394e |
262 | if ($showaddrlist) { |
263 | echo addForm($form_url, 'post'); |
264 | |
265 | $oTemplate->assign('addresses', $addresses); |
266 | $oTemplate->assign('current_backend', $current_backend); |
267 | $oTemplate->assign('backends', $list_backends); |
a140c3b1 |
268 | $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field); |
a71b394e |
269 | |
270 | $oTemplate->display('addressbook_list.tpl'); |
271 | |
272 | echo "</form>\n"; |
273 | } |
274 | |
f6c945b9 |
275 | /* Display the "new address" form */ |
c1ac62d4 |
276 | echo '<a name="AddAddress"></a>' . "\n"; |
277 | abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata); |
fc93f97c |
278 | echo "</form>\n"; |
daba719e |
279 | |
df4162cb |
280 | /* Hook for extra address book blocks */ |
8811538c |
281 | echo "<!-- start of addressbook_bottom hook-->\n"; |
daba719e |
282 | do_hook('addressbook_bottom'); |
8811538c |
283 | echo "\n<!-- end of addressbook_bottom hook-->\n"; |
a71b394e |
284 | |
5c4ff7bf |
285 | $oTemplate->display('footer.tpl'); |
a71b394e |
286 | ?> |