6 * Manage personal address book.
8 * @copyright © 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
12 * @subpackage addressbook
16 * Include the SquirrelMail initialization file.
18 include('../include/init.php');
20 /** SquirrelMail required files. */
21 /* address book functions */
22 require_once(SM_PATH
. 'functions/addressbook.php');
24 require_once(SM_PATH
. 'functions/forms.php');
26 /** lets get the global vars we may need */
28 /* From the address form */
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
);
37 /* Get sorting order */
38 $abook_sort_order = get_abook_sort();
40 /* Create page header before addressbook_init in order to display error messages correctly. */
41 displayPageHeader($color, 'None');
43 /* Open addressbook with error messages on.
44 remote backends (LDAP) are enabled because they can be used. (list_addr function)
46 $abook = addressbook_init(true, false);
48 // FIXME: do we have to stop use of address book, when localbackend is not present.
49 if($abook->localbackend
== 0) {
50 plain_error_message(_("No personal address book is defined. Contact administrator."));
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;
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
;
67 if (count($list_backends)>1) {
68 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
69 .addSubmit(_("Change"),'change_abook');
77 $defselected = array();
78 $form_url = 'addressbook.php';
80 /* Handle user's actions */
81 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
83 /**************************************************
85 **************************************************/
86 if (isset($addaddr)) {
87 if (isset($backend)) {
88 $r = $abook->add($addaddr, $backend);
90 $r = $abook->add($addaddr, $abook->localbackend
);
93 /* Handle error messages */
95 /* Remove backend name from error string */
96 $errstr = $abook->error
;
97 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
100 $showaddrlist = false;
105 /************************************************
106 * Delete address(es) *
107 ************************************************/
108 if ((!empty($deladdr)) && sizeof($sel) > 0) {
112 /* The selected addresses are identidied by "backend:nickname". *
113 * Sort the list and process one backend at the time */
118 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
119 list($sbackend, $snick) = explode(':', $sel[$i]);
121 /* When we get to a new backend, process addresses in *
123 if ($prevback != $sbackend && $prevback != -1) {
125 $r = $abook->remove($subsel, $prevback);
127 $formerror = $abook->error
;
135 /* Queue for processing */
136 array_push($subsel, $snick);
137 $prevback = $sbackend;
141 $r = $abook->remove($subsel, $prevback);
142 if (!$r) { /* Handle errors */
143 $formerror = $abook->error
;
149 $showaddrlist = true;
150 $defselected = $orig_sel;
155 /***********************************************
156 * Update/modify address *
157 ***********************************************/
158 if (!empty($editaddr)) {
159 /* Stage one: Copy data into form */
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;
167 list($ebackend, $enick) = explode(':', current($sel));
168 $olddata = $abook->lookup($enick, $ebackend);
169 // Test if $olddata really contains anything and return an error message if it doesn't
171 error_box(nl2br(htmlspecialchars($abook->error
)));
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').
181 } elseif ($doedit == 1) {
182 /* Stage two: Write new data */
183 $newdata = $editaddr;
184 $r = $abook->modify($oldnick, $newdata, $backend);
186 /* Handle error messages */
189 echo html_tag( 'table',
192 "\n". '<strong><font color="' . $color[2] .
193 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
196 'center', '', 'width="100%"' );
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').
208 * $editaddr is set, but $sel (address selection in address listing)
209 * and $doedit (address edit form) are not set.
210 * Assume that user clicked on "Edit address" without selecting any address.
212 $formerror = _("Please select address that you want to edit");
213 $showaddrlist = true;
214 } /* end of edit stage detection */
215 } /* !empty($editaddr) - Update/modify address */
216 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
217 } /* !empty($addaddr['nickname']) - Add new address */
219 // Some times we end output before forms are printed
221 echo "</body></html>\n";
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 * =================================================================== */
232 /* Display error messages */
233 if (!empty($formerror)) {
234 plain_error_message(nl2br(htmlspecialchars($formerror)));
238 /* Display the address management part */
239 $addresses = array();
240 while (list($k, $backend) = each ($abook->backends
)) {
242 $a['BackendID'] = $backend->bnum
;
243 $a['BackendSource'] = $backend->sname
;
244 $a['BackendWritable'] = $backend->writeable
;
245 $a['Addresses'] = array();
247 $alist = $abook->list_addr($backend->bnum
);
248 usort($alist,'alistcmp');
250 $count = count($alist);
251 if ($start >= $count) $start = 0;
252 $alist = array_slice($alist,$start,15);
254 while(list($undef,$row) = each($alist)) {
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),
265 $a['Addresses'][] = $contact;
268 $addresses[$backend->bnum
] = $a;
273 echo addForm($form_url, 'post');
275 $oTemplate->assign('addresses', $addresses);
276 $oTemplate->assign('current_backend', $current_backend);
277 $oTemplate->assign('backends', $list_backends);
278 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field
);
280 $oTemplate->display('addressbook_list.tpl');
285 /* Display the "new address" form */
286 echo '<a name="AddAddress"></a>' . "\n";
287 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
290 /* Hook for extra address book blocks */
291 echo "<!-- start of addressbook_bottom hook-->\n";
292 do_hook('addressbook_bottom');
293 echo "\n<!-- end of addressbook_bottom hook-->\n";
295 $oTemplate->display('footer.tpl');