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