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 | /** |
16 | * Path for SquirrelMail required files. |
17 | * @ignore |
18 | */ |
86725763 |
19 | define('SM_PATH','../'); |
20 | |
8f6f9ba5 |
21 | /** SquirrelMail required files. */ |
5c4ff7bf |
22 | include_once(SM_PATH . 'include/validate.php'); |
df4162cb |
23 | /* plain_error_message() */ |
24 | include_once(SM_PATH . 'functions/display_messages.php'); |
25 | /* address book functions */ |
26 | include_once(SM_PATH . 'functions/addressbook.php'); |
27 | /* form functions */ |
28 | include_once(SM_PATH . 'functions/forms.php'); |
ffd8224c |
29 | |
8f6f9ba5 |
30 | /** lets get the global vars we may need */ |
0b97a708 |
31 | |
32 | /* From the address form */ |
1e12d1ff |
33 | sqgetGlobalVar('addaddr', $addaddr, SQ_POST); |
34 | sqgetGlobalVar('editaddr', $editaddr, SQ_POST); |
35 | sqgetGlobalVar('deladdr', $deladdr, SQ_POST); |
36 | sqgetGlobalVar('sel', $sel, SQ_POST); |
37 | sqgetGlobalVar('oldnick', $oldnick, SQ_POST); |
38 | sqgetGlobalVar('backend', $backend, SQ_POST); |
2741cc97 |
39 | sqgetGlobalVar('doedit', $doedit, SQ_POST); |
0b97a708 |
40 | |
08e71631 |
41 | /* Get sorting order */ |
42 | $abook_sort_order = get_abook_sort(); |
43 | |
703fa6b5 |
44 | /* Create page header before addressbook_init in order to display error messages correctly. */ |
45 | displayPageHeader($color, 'None'); |
46 | |
de00443c |
47 | /* Open addressbook with error messages on. |
48 | remote backends (LDAP) are enabled because they can be used. (list_addr function) |
49 | */ |
50 | $abook = addressbook_init(true, false); |
ced653f3 |
51 | |
52 | // FIXME: do we have to stop use of address book, when localbackend is not present. |
daba719e |
53 | if($abook->localbackend == 0) { |
54 | plain_error_message( |
056a005c |
55 | _("No personal address book is defined. Contact administrator."), |
56 | $color); |
daba719e |
57 | exit(); |
58 | } |
ffd8224c |
59 | |
daba719e |
60 | $defdata = array(); |
61 | $formerror = ''; |
62 | $abortform = false; |
63 | $showaddrlist = true; |
64 | $defselected = array(); |
07dcee9f |
65 | $form_url = 'addressbook.php'; |
daba719e |
66 | |
f6c945b9 |
67 | /* Handle user's actions */ |
1e12d1ff |
68 | if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') { |
daba719e |
69 | |
f6c945b9 |
70 | /************************************************** |
71 | * Add new address * |
72 | **************************************************/ |
39b26252 |
73 | if (isset($addaddr)) { |
a123157f |
74 | if (isset($backend)) { |
75 | $r = $abook->add($addaddr, $backend); |
76 | } else { |
77 | $r = $abook->add($addaddr, $abook->localbackend); |
c6554ec0 |
78 | } |
ffd8224c |
79 | |
f6c945b9 |
80 | /* Handle error messages */ |
81 | if (!$r) { |
82 | /* Remove backend name from error string */ |
ffd8224c |
83 | $errstr = $abook->error; |
84 | $errstr = ereg_replace('^\[.*\] *', '', $errstr); |
85 | |
86 | $formerror = $errstr; |
87 | $showaddrlist = false; |
88 | $defdata = $addaddr; |
89 | } |
daba719e |
90 | } else { |
ffd8224c |
91 | |
f6c945b9 |
92 | /************************************************ |
93 | * Delete address(es) * |
94 | ************************************************/ |
95 | if ((!empty($deladdr)) && sizeof($sel) > 0) { |
daba719e |
96 | $orig_sel = $sel; |
97 | sort($sel); |
98 | |
f6c945b9 |
99 | /* The selected addresses are identidied by "backend:nickname". * |
100 | * Sort the list and process one backend at the time */ |
daba719e |
101 | $prevback = -1; |
102 | $subsel = array(); |
103 | $delfailed = false; |
104 | |
f6c945b9 |
105 | for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) { |
daba719e |
106 | list($sbackend, $snick) = explode(':', $sel[$i]); |
107 | |
f6c945b9 |
108 | /* When we get to a new backend, process addresses in * |
109 | * previous one. */ |
110 | if ($prevback != $sbackend && $prevback != -1) { |
daba719e |
111 | |
112 | $r = $abook->remove($subsel, $prevback); |
f6c945b9 |
113 | if (!$r) { |
daba719e |
114 | $formerror = $abook->error; |
115 | $i = sizeof($sel); |
116 | $delfailed = true; |
117 | break; |
118 | } |
119 | $subsel = array(); |
120 | } |
121 | |
f6c945b9 |
122 | /* Queue for processing */ |
daba719e |
123 | array_push($subsel, $snick); |
124 | $prevback = $sbackend; |
ffd8224c |
125 | } |
ffd8224c |
126 | |
f6c945b9 |
127 | if (!$delfailed) { |
daba719e |
128 | $r = $abook->remove($subsel, $prevback); |
f6c945b9 |
129 | if (!$r) { /* Handle errors */ |
daba719e |
130 | $formerror = $abook->error; |
131 | $delfailed = true; |
132 | } |
ffd8224c |
133 | } |
ffd8224c |
134 | |
f6c945b9 |
135 | if ($delfailed) { |
daba719e |
136 | $showaddrlist = true; |
137 | $defselected = $orig_sel; |
ffd8224c |
138 | } |
ffd8224c |
139 | |
daba719e |
140 | } else { |
141 | |
f6c945b9 |
142 | /*********************************************** |
143 | * Update/modify address * |
144 | ***********************************************/ |
145 | if (!empty($editaddr)) { |
f6c945b9 |
146 | /* Stage one: Copy data into form */ |
daba719e |
147 | if (isset($sel) && sizeof($sel) > 0) { |
148 | if(sizeof($sel) > 1) { |
149 | $formerror = _("You can only edit one address at the time"); |
150 | $showaddrlist = true; |
151 | $defselected = $sel; |
152 | } else { |
153 | $abortform = true; |
154 | list($ebackend, $enick) = explode(':', $sel[0]); |
155 | $olddata = $abook->lookup($enick, $ebackend); |
a2b193bc |
156 | // FIXME: Test if $olddata really contains anything and return an error message if it doesn't |
daba719e |
157 | |
f6c945b9 |
158 | /* Display the "new address" form */ |
77fe415a |
159 | abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata); |
2741cc97 |
160 | echo addHidden('oldnick', $olddata['nickname']). |
056a005c |
161 | addHidden('backend', $olddata['backend']). |
162 | addHidden('doedit', '1'). |
163 | '</form>'; |
daba719e |
164 | } |
3e87f870 |
165 | } elseif ($doedit == 1) { |
f6c945b9 |
166 | /* Stage two: Write new data */ |
3e87f870 |
167 | $newdata = $editaddr; |
168 | $r = $abook->modify($oldnick, $newdata, $backend); |
daba719e |
169 | |
3e87f870 |
170 | /* Handle error messages */ |
171 | if (!$r) { |
172 | /* Display error */ |
173 | echo html_tag( 'table', |
a2b193bc |
174 | html_tag( 'tr', |
175 | html_tag( 'td', |
176 | "\n". '<strong><font color="' . $color[2] . |
177 | '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n", |
178 | 'center' ) |
179 | ), |
180 | 'center', '', 'width="100%"' ); |
3e87f870 |
181 | |
182 | /* Display the "new address" form again */ |
183 | abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata); |
184 | echo addHidden('oldnick', $oldnick). |
185 | addHidden('backend', $backend). |
186 | addHidden('doedit', '1'). |
187 | "\n" . '</form>'; |
daba719e |
188 | $abortform = true; |
189 | } |
3e87f870 |
190 | } else { |
191 | /** |
f8a1ed5a |
192 | * $editaddr is set, but $sel (address selection in address listing) |
193 | * and $doedit (address edit form) are not set. |
3e87f870 |
194 | * Assume that user clicked on "Edit address" without selecting any address. |
195 | */ |
196 | $formerror = _("Please select address that you want to edit"); |
197 | $showaddrlist = true; |
198 | } /* end of edit stage detection */ |
daba719e |
199 | } /* !empty($editaddr) - Update/modify address */ |
200 | } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */ |
201 | } /* !empty($addaddr['nickname']) - Add new address */ |
202 | |
203 | // Some times we end output before forms are printed |
204 | if($abortform) { |
056a005c |
205 | echo "</body></html>\n"; |
206 | exit(); |
ffd8224c |
207 | } |
daba719e |
208 | } |
ffd8224c |
209 | |
210 | |
f6c945b9 |
211 | /* =================================================================== * |
212 | * The following is only executed on a GET request, or on a POST when * |
213 | * a user is added, or when "delete" or "modify" was successful. * |
214 | * =================================================================== */ |
ffd8224c |
215 | |
f6c945b9 |
216 | /* Display error messages */ |
217 | if (!empty($formerror)) { |
ac987a56 |
218 | echo html_tag( 'table', |
056a005c |
219 | html_tag( 'tr', |
220 | html_tag( 'td', |
221 | "\n". '<br /><strong><font color="' . $color[2] . |
222 | '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n", |
223 | 'center' ) |
224 | ), |
225 | 'center', '', 'width="100%"' ); |
daba719e |
226 | } |
ffd8224c |
227 | |
228 | |
f6c945b9 |
229 | /* Display the address management part */ |
230 | if ($showaddrlist) { |
231 | /* Get and sort address list */ |
daba719e |
232 | $alist = $abook->list_addr(); |
233 | if(!is_array($alist)) { |
ffd8224c |
234 | plain_error_message($abook->error, $color); |
235 | exit; |
daba719e |
236 | } |
ffd8224c |
237 | |
daba719e |
238 | usort($alist,'alistcmp'); |
239 | $prevbackend = -1; |
240 | $headerprinted = false; |
ffd8224c |
241 | |
ac987a56 |
242 | echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n"; |
ffd8224c |
243 | |
f6c945b9 |
244 | /* List addresses */ |
91821fc0 |
245 | if (count($alist) > 0) { |
056a005c |
246 | echo addForm($form_url, 'post'); |
7b0ea860 |
247 | if ($abook->add_extra_field) { |
248 | $abook_fields = 6; |
249 | } else { |
250 | $abook_fields = 5; |
251 | } |
91821fc0 |
252 | while(list($undef,$row) = each($alist)) { |
056a005c |
253 | |
91821fc0 |
254 | /* New table header for each backend */ |
255 | if($prevbackend != $row['backend']) { |
92cd1e8e |
256 | if($prevbackend < 0) { |
ac987a56 |
257 | echo html_tag( 'table', |
134e4174 |
258 | html_tag( 'tr', |
259 | html_tag( 'td', |
056a005c |
260 | addSubmit(_("Edit selected"), 'editaddr'). |
261 | addSubmit(_("Delete selected"), 'deladdr'), |
7b0ea860 |
262 | 'center', '', "colspan=\"$abook_fields\"" ) |
134e4174 |
263 | ) . |
264 | html_tag( 'tr', |
7b0ea860 |
265 | html_tag( 'td', ' <br />', 'center', '', "colspan=\"$abook_fields\"" ) |
134e4174 |
266 | ), |
267 | 'center' ); |
2741cc97 |
268 | echo "\n<!-- start of address book table -->\n" . |
056a005c |
269 | html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) . |
270 | html_tag( 'tr', "\n" . |
271 | html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" . |
272 | html_tag( 'th', _("Nickname") . |
273 | show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1), |
274 | 'left', '', 'width="1%"' ) . "\n" . |
91e0dccc |
275 | html_tag( 'th', _("Name") . |
056a005c |
276 | show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3), |
277 | 'left', '', 'width="1%"' ) . "\n" . |
91e0dccc |
278 | html_tag( 'th', _("E-mail") . |
056a005c |
279 | show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5), |
280 | 'left', '', 'width="1%"' ) . "\n" . |
281 | html_tag( 'th', _("Info") . |
282 | show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7), |
7b0ea860 |
283 | 'left', '', 'width="1%"' ) . |
5c4ff7bf |
284 | ($abook->add_extra_field ? html_tag( 'th', ' ','left', '', 'width="1%"'): '') . |
7b0ea860 |
285 | "\n", |
056a005c |
286 | '', $color[9] ) . "\n"; |
d9879e29 |
287 | } |
288 | |
2741cc97 |
289 | // Separate different backends with <hr /> |
d9879e29 |
290 | if($prevbackend > 0) { |
291 | echo html_tag( 'tr', |
7b0ea860 |
292 | html_tag( 'td', "<hr />", 'center', '' ,"colspan=\"$abook_fields\"" ) |
056a005c |
293 | ); |
2741cc97 |
294 | } |
295 | |
296 | // Print backend name |
a2b193bc |
297 | echo html_tag( 'tr', |
7b0ea860 |
298 | html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,"colspan=\"$abook_fields\"" ) |
056a005c |
299 | ); |
d9879e29 |
300 | |
91821fc0 |
301 | $line = 0; |
302 | $headerprinted = true; |
303 | } /* End of header */ |
fc93f97c |
304 | |
91821fc0 |
305 | $prevbackend = $row['backend']; |
056a005c |
306 | |
91821fc0 |
307 | /* Check if this user is selected */ |
19347763 |
308 | $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected); |
056a005c |
309 | |
9f6c97c3 |
310 | /* Print one row, with alternating color */ |
056a005c |
311 | if ($line % 2) { |
9f6c97c3 |
312 | $tr_bgcolor = $color[12]; |
313 | } else { |
314 | $tr_bgcolor = $color[4]; |
315 | } |
7b0ea860 |
316 | echo html_tag( 'tr', '', '', $tr_bgcolor); |
317 | if ($abook->backends[$row['backend']]->writeable) { |
318 | echo html_tag( 'td', |
319 | '<small>' . |
320 | addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']). |
321 | '</small>' , |
322 | 'center', '', 'valign="top" width="1%"' ); |
323 | } else { |
324 | echo html_tag( 'td', |
325 | ' ' , |
326 | 'center', '', 'valign="top" width="1%"' ); |
327 | } |
5c4ff7bf |
328 | echo html_tag( 'td', |
329 | ' ' . htmlspecialchars($row['nickname']) . ' ', |
7b0ea860 |
330 | 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ); |
331 | |
332 | // different full name display formating for Japanese translation |
056a005c |
333 | if ($squirrelmail_language == 'ja_JP') { |
7b0ea860 |
334 | /* |
335 | * translation uses euc-jp character set internally. |
336 | * htmlspecialchars() should not break any characters. |
337 | */ |
5c4ff7bf |
338 | echo html_tag( 'td', |
7b0ea860 |
339 | ' ' . htmlspecialchars($row['lastname']) . ' ' . htmlspecialchars($row['firstname']) . ' ', |
340 | 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ); |
19d01c6b |
341 | } else { |
7b0ea860 |
342 | echo html_tag( 'td', |
343 | ' ' . htmlspecialchars($row['name']) . ' ', |
344 | 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ); |
056a005c |
345 | } |
7b0ea860 |
346 | |
347 | // email address column |
348 | echo html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' '; |
3d0cada3 |
349 | $email = $abook->full_address($row); |
d62c4938 |
350 | echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email), |
056a005c |
351 | htmlspecialchars($row['email'])). |
7b0ea860 |
352 | ' </td>'."\n"; |
353 | |
354 | // info column |
355 | echo html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ); |
356 | |
357 | // add extra column if third party backend needs it |
358 | if ($abook->add_extra_field) { |
5c4ff7bf |
359 | echo html_tag( 'td', |
7b0ea860 |
360 | ' ' . (isset($row['extra']) ? $row['extra'] : '') . ' ', |
361 | 'left', '', 'valign="top" width="1%"' ); |
362 | } |
363 | echo "</tr>\n"; |
91821fc0 |
364 | $line++; |
daba719e |
365 | } |
2741cc97 |
366 | echo "</table>" . |
056a005c |
367 | "\n<!-- end of address book table -->\n"; |
d9879e29 |
368 | |
fc93f97c |
369 | /* End of list. Add edit/delete select buttons */ |
91821fc0 |
370 | if ($headerprinted) { |
fc93f97c |
371 | echo html_tag( 'table', |
134e4174 |
372 | html_tag( 'tr', |
373 | html_tag( 'td', |
056a005c |
374 | addSubmit(_("Edit selected"), 'editaddr') . |
375 | addSubmit(_("Delete selected"), 'deladdr'), |
7b0ea860 |
376 | 'center', '', "colspan=\"$abook_fields\"" ) |
134e4174 |
377 | ), |
056a005c |
378 | 'center' ); |
91821fc0 |
379 | } |
fc93f97c |
380 | echo "</form>\n"; |
daba719e |
381 | } |
f6c945b9 |
382 | } /* end of addresslist */ |
daba719e |
383 | |
384 | |
f6c945b9 |
385 | /* Display the "new address" form */ |
c1ac62d4 |
386 | echo '<a name="AddAddress"></a>' . "\n"; |
387 | abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata); |
fc93f97c |
388 | echo "</form>\n"; |
daba719e |
389 | |
df4162cb |
390 | /* Hook for extra address book blocks */ |
8811538c |
391 | echo "<!-- start of addressbook_bottom hook-->\n"; |
daba719e |
392 | do_hook('addressbook_bottom'); |
8811538c |
393 | echo "\n<!-- end of addressbook_bottom hook-->\n"; |
5c4ff7bf |
394 | $oTemplate->display('footer.tpl'); |
395 | ?> |