Account for extra fields in address book more correctly. Also account for no global...
[squirrelmail.git] / src / addressbook.php
1 <?php
2
3 /**
4 * addressbook.php
5 *
6 * Manage personal address book.
7 *
8 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage addressbook
13 */
14
15 /**
16 * Include the SquirrelMail initialization file.
17 */
18 include('../include/init.php');
19
20 /** SquirrelMail required files. */
21 /* address book functions */
22 require_once(SM_PATH . 'functions/addressbook.php');
23 /* form functions */
24 require_once(SM_PATH . 'functions/forms.php');
25
26 /** lets get the global vars we may need */
27
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);
36
37 /* Get sorting order */
38 $abook_sort_order = get_abook_sort();
39
40 /* Create page header before addressbook_init in order to display error messages correctly. */
41 displayPageHeader($color, 'None');
42
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);
47
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."));
51 exit();
52 }
53
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 = '&nbsp;';
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
73 $defdata = array();
74 $formerror = '';
75 $abortform = false;
76 $showaddrlist = true;
77 $defselected = array();
78 $form_url = 'addressbook.php';
79
80 /* Handle user's actions */
81 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
82
83 /**************************************************
84 * Add new address *
85 **************************************************/
86 if (isset($addaddr)) {
87 if (isset($backend)) {
88 $r = $abook->add($addaddr, $backend);
89 } else {
90 $r = $abook->add($addaddr, $abook->localbackend);
91 }
92
93 /* Handle error messages */
94 if (!$r) {
95 /* Remove backend name from error string */
96 $errstr = $abook->error;
97 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
98
99 $formerror = $errstr;
100 $showaddrlist = false;
101 $defdata = $addaddr;
102 }
103 } else {
104
105 /************************************************
106 * Delete address(es) *
107 ************************************************/
108 if ((!empty($deladdr)) && sizeof($sel) > 0) {
109 $orig_sel = $sel;
110 sort($sel);
111
112 /* The selected addresses are identidied by "backend:nickname". *
113 * Sort the list and process one backend at the time */
114 $prevback = -1;
115 $subsel = array();
116 $delfailed = false;
117
118 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
119 list($sbackend, $snick) = explode(':', $sel[$i]);
120
121 /* When we get to a new backend, process addresses in *
122 * previous one. */
123 if ($prevback != $sbackend && $prevback != -1) {
124
125 $r = $abook->remove($subsel, $prevback);
126 if (!$r) {
127 $formerror = $abook->error;
128 $i = sizeof($sel);
129 $delfailed = true;
130 break;
131 }
132 $subsel = array();
133 }
134
135 /* Queue for processing */
136 array_push($subsel, $snick);
137 $prevback = $sbackend;
138 }
139
140 if (!$delfailed) {
141 $r = $abook->remove($subsel, $prevback);
142 if (!$r) { /* Handle errors */
143 $formerror = $abook->error;
144 $delfailed = true;
145 }
146 }
147
148 if ($delfailed) {
149 $showaddrlist = true;
150 $defselected = $orig_sel;
151 }
152
153 } else {
154
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;
164 $defselected = $sel;
165 } else {
166 $abortform = 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
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 }
180 }
181 } elseif ($doedit == 1) {
182 /* Stage two: Write new data */
183 $newdata = $editaddr;
184 $r = $abook->modify($oldnick, $newdata, $backend);
185
186 /* Handle error messages */
187 if (!$r) {
188 /* Display error */
189 echo html_tag( 'table',
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%"' );
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>';
204 $abortform = true;
205 }
206 } else {
207 /**
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.
211 */
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 */
218
219 // Some times we end output before forms are printed
220 if($abortform) {
221 echo "</body></html>\n";
222 exit();
223 }
224 }
225
226
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 * =================================================================== */
231
232 /* Display error messages */
233 if (!empty($formerror)) {
234 plain_error_message(nl2br(htmlspecialchars($formerror)));
235 }
236
237
238 /* Display the address management part */
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);
248 usort($alist,'alistcmp');
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;
266 }
267
268 $addresses[$backend->bnum] = $a;
269 }
270
271
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);
278 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field);
279
280 $oTemplate->display('addressbook_list.tpl');
281
282 echo "</form>\n";
283 }
284
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);
288 echo "</form>\n";
289
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";
294
295 $oTemplate->display('footer.tpl');
296 ?>