6 * Manage personal address book.
8 * @copyright © 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
12 * @subpackage addressbook
15 /** This is the addressbook page */
16 define('PAGE_NAME', 'addressbook');
19 * Include the SquirrelMail initialization file.
21 include('../include/init.php');
23 /** SquirrelMail required files. */
24 /* address book functions */
25 require_once(SM_PATH
. 'functions/addressbook.php');
26 include_once(SM_PATH
. 'templates/util_addressbook.php');
29 require_once(SM_PATH
. 'functions/forms.php');
31 /** lets get the global vars we may need */
33 /* From the address form */
34 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
35 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
36 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
37 sqgetGlobalVar('sel', $sel, SQ_POST
);
38 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
39 sqgetGlobalVar('backend', $backend, SQ_POST
);
40 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
42 /* Get sorting order */
43 $abook_sort_order = get_abook_sort();
45 /* Create page header before addressbook_init in order to display error messages correctly. */
46 displayPageHeader($color);
48 /* Open addressbook with error messages on.
49 remote backends (LDAP) are enabled because they can be used. (list_addr function)
51 $abook = addressbook_init(true, false);
53 // FIXME: do we have to stop use of address book, when localbackend is not present.
54 if($abook->localbackend
== 0) {
55 plain_error_message(_("No personal address book is defined. Contact administrator."));
59 $current_backend = $abook->localbackend
;
60 if (sqgetGlobalVar('new_bnum',$new_backend,SQ_POST
) && array_key_exists($new_backend,$abook->backends
)) {
61 $current_backend = (int) $new_backend;
64 $abook_selection = ' ';
65 $list_backends = array();
66 if (count($abook->backends
) > 1) {
67 foreach($abook->get_backend_list() as $oBackend) {
68 if ($oBackend->listing
) {
69 $list_backends[$oBackend->bnum
]=$oBackend->sname
;
72 if (count($list_backends)>1) {
73 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
74 .addSubmit(_("Change"),'change_abook');
82 $defselected = array();
83 $form_url = 'addressbook.php';
85 /* Handle user's actions */
86 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
88 /**************************************************
90 **************************************************/
91 if (isset($addaddr)) {
92 if (isset($backend)) {
93 $r = $abook->add($addaddr, $backend);
95 $r = $abook->add($addaddr, $abook->localbackend
);
98 /* Handle error messages */
100 /* Remove backend name from error string */
101 $errstr = $abook->error
;
102 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
104 $formerror = $errstr;
105 $showaddrlist = false;
110 /************************************************
111 * Delete address(es) *
112 ************************************************/
113 if ((!empty($deladdr)) && sizeof($sel) > 0) {
117 /* The selected addresses are identidied by "nickname_backend". *
118 * Sort the list and process one backend at the time */
123 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
124 list($snick, $sbackend) = explode('_', $sel[$i]);
126 /* When we get to a new backend, process addresses in *
128 if ($prevback != $sbackend && $prevback != -1) {
130 $r = $abook->remove($subsel, $prevback);
132 $formerror = $abook->error
;
140 /* Queue for processing */
141 array_push($subsel, $snick);
142 $prevback = $sbackend;
146 $r = $abook->remove($subsel, $prevback);
147 if (!$r) { /* Handle errors */
148 $formerror = $abook->error
;
154 $showaddrlist = true;
155 $defselected = $orig_sel;
160 /***********************************************
161 * Update/modify address *
162 ***********************************************/
163 if (!empty($editaddr)) {
164 /* Stage one: Copy data into form */
165 if (isset($sel) && sizeof($sel) > 0) {
166 if(sizeof($sel) > 1) {
167 $formerror = _("You can only edit one address at the time");
168 $showaddrlist = true;
172 list($enick, $ebackend) = explode('_', current($sel));
173 $olddata = $abook->lookup($enick, $ebackend);
174 // Test if $olddata really contains anything and return an error message if it doesn't
176 error_box(nl2br(htmlspecialchars($abook->error
)));
178 /* Display the "new address" form */
179 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
180 echo addHidden('oldnick', $olddata['nickname']).
181 addHidden('backend', $olddata['backend']).
182 addHidden('doedit', '1').
186 } elseif ($doedit == 1) {
187 /* Stage two: Write new data */
188 $newdata = $editaddr;
189 $r = $abook->modify($oldnick, $newdata, $backend);
191 /* Handle error messages */
194 plain_error_message( nl2br(htmlspecialchars($abook->error
)));
196 /* Display the "new address" form again */
197 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
198 echo addHidden('oldnick', $oldnick).
199 addHidden('backend', $backend).
200 addHidden('doedit', '1').
206 * $editaddr is set, but $sel (address selection in address listing)
207 * and $doedit (address edit form) are not set.
208 * Assume that user clicked on "Edit address" without selecting any address.
210 $formerror = _("Please select address that you want to edit");
211 $showaddrlist = true;
212 } /* end of edit stage detection */
213 } /* !empty($editaddr) - Update/modify address */
214 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
215 } /* !empty($addaddr['nickname']) - Add new address */
217 // Some times we end output before forms are printed
219 echo "</body></html>\n";
225 /* =================================================================== *
226 * The following is only executed on a GET request, or on a POST when *
227 * a user is added, or when "delete" or "modify" was successful. *
228 * =================================================================== */
230 /* Display error messages */
231 if (!empty($formerror)) {
232 plain_error_message(nl2br(htmlspecialchars($formerror)));
236 /* Display the address management part */
237 $addresses = array();
238 while (list($k, $backend) = each ($abook->backends
)) {
240 $a['BackendID'] = $backend->bnum
;
241 $a['BackendSource'] = $backend->sname
;
242 $a['BackendWritable'] = $backend->writeable
;
243 $a['Addresses'] = array();
245 $alist = $abook->list_addr($backend->bnum
);
247 /* check return (array with data or boolean false) */
248 if (is_array($alist)) {
249 usort($alist,'alistcmp');
251 $a['Addresses'] = formatAddressList($alist);
253 $addresses[$backend->bnum
] = $a;
255 // list_addr() returns boolean
256 plain_error_message(nl2br(htmlspecialchars($abook->error
)));
262 echo addForm($form_url, 'post', 'address_book_form');
264 $oTemplate->assign('addresses', $addresses);
265 $oTemplate->assign('current_backend', $current_backend);
266 $oTemplate->assign('backends', $list_backends);
267 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field
);
269 $oTemplate->display('addressbook_list.tpl');
271 //FIXME: Remove HTML from here!
275 /* Display the "new address" form */
276 //FIXME: Remove HTML from here!
277 echo '<a name="AddAddress"></a>' . "\n";
278 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
281 /* Hook for extra address book blocks */
282 do_hook('addressbook_bottom', $null);
284 $oTemplate->display('footer.tpl');