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');
23 include_once(SM_PATH
. 'templates/util_addressbook.php');
24 include_once(SM_PATH
. 'templates/util_global.php');
27 require_once(SM_PATH
. 'functions/forms.php');
29 /** lets get the global vars we may need */
31 /* From the address form */
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
);
40 /* Get sorting order */
41 $abook_sort_order = get_abook_sort();
43 /* Create page header before addressbook_init in order to display error messages correctly. */
44 displayPageHeader($color, 'None');
46 /* Open addressbook with error messages on.
47 remote backends (LDAP) are enabled because they can be used. (list_addr function)
49 $abook = addressbook_init(true, false);
51 // FIXME: do we have to stop use of address book, when localbackend is not present.
52 if($abook->localbackend
== 0) {
53 plain_error_message(_("No personal address book is defined. Contact administrator."));
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;
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
;
70 if (count($list_backends)>1) {
71 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
72 .addSubmit(_("Change"),'change_abook');
80 $defselected = array();
81 $form_url = 'addressbook.php';
83 /* Handle user's actions */
84 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
86 /**************************************************
88 **************************************************/
89 if (isset($addaddr)) {
90 if (isset($backend)) {
91 $r = $abook->add($addaddr, $backend);
93 $r = $abook->add($addaddr, $abook->localbackend
);
96 /* Handle error messages */
98 /* Remove backend name from error string */
99 $errstr = $abook->error
;
100 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
102 $formerror = $errstr;
103 $showaddrlist = false;
108 /************************************************
109 * Delete address(es) *
110 ************************************************/
111 if ((!empty($deladdr)) && sizeof($sel) > 0) {
115 /* The selected addresses are identidied by "backend:nickname". *
116 * Sort the list and process one backend at the time */
121 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
122 list($sbackend, $snick) = explode(':', $sel[$i]);
124 /* When we get to a new backend, process addresses in *
126 if ($prevback != $sbackend && $prevback != -1) {
128 $r = $abook->remove($subsel, $prevback);
130 $formerror = $abook->error
;
138 /* Queue for processing */
139 array_push($subsel, $snick);
140 $prevback = $sbackend;
144 $r = $abook->remove($subsel, $prevback);
145 if (!$r) { /* Handle errors */
146 $formerror = $abook->error
;
152 $showaddrlist = true;
153 $defselected = $orig_sel;
158 /***********************************************
159 * Update/modify address *
160 ***********************************************/
161 if (!empty($editaddr)) {
162 /* Stage one: Copy data into form */
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;
170 list($ebackend, $enick) = explode(':', current($sel));
171 $olddata = $abook->lookup($enick, $ebackend);
172 // Test if $olddata really contains anything and return an error message if it doesn't
174 error_box(nl2br(htmlspecialchars($abook->error
)));
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').
184 } elseif ($doedit == 1) {
185 /* Stage two: Write new data */
186 $newdata = $editaddr;
187 $r = $abook->modify($oldnick, $newdata, $backend);
189 /* Handle error messages */
192 plain_error_message( nl2br(htmlspecialchars(_("ERROR") .': '. $abook->error
)));
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').
204 * $editaddr is set, but $sel (address selection in address listing)
205 * and $doedit (address edit form) are not set.
206 * Assume that user clicked on "Edit address" without selecting any address.
208 $formerror = _("Please select address that you want to edit");
209 $showaddrlist = true;
210 } /* end of edit stage detection */
211 } /* !empty($editaddr) - Update/modify address */
212 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
213 } /* !empty($addaddr['nickname']) - Add new address */
215 // Some times we end output before forms are printed
217 echo "</body></html>\n";
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 * =================================================================== */
228 /* Display error messages */
229 if (!empty($formerror)) {
230 plain_error_message(nl2br(htmlspecialchars($formerror)));
234 /* Display the address management part */
235 $addresses = array();
236 while (list($k, $backend) = each ($abook->backends
)) {
238 $a['BackendID'] = $backend->bnum
;
239 $a['BackendSource'] = $backend->sname
;
240 $a['BackendWritable'] = $backend->writeable
;
241 $a['Addresses'] = array();
243 $alist = $abook->list_addr($backend->bnum
);
245 /* check return (array with data or boolean false) */
246 if (is_array($alist)) {
247 usort($alist,'alistcmp');
249 $a['Addresses'] = formatAddressList($alist);
251 $addresses[$backend->bnum
] = $a;
253 // list_addr() returns boolean
254 plain_error_message(nl2br(htmlspecialchars($abook->error
)));
260 echo addForm($form_url, 'post');
262 $oTemplate->assign('addresses', $addresses);
263 $oTemplate->assign('current_backend', $current_backend);
264 $oTemplate->assign('backends', $list_backends);
265 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field
);
267 $oTemplate->display('addressbook_list.tpl');
272 /* Display the "new address" form */
273 echo '<a name="AddAddress"></a>' . "\n";
274 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
277 /* Hook for extra address book blocks */
278 echo "<!-- start of addressbook_bottom hook-->\n";
279 do_hook('addressbook_bottom');
280 echo "\n<!-- end of addressbook_bottom hook-->\n";
282 $oTemplate->display('footer.tpl');