| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * addressbook.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2001 The Squirrelmail Development Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * Manage personal address book. |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | |
| 14 | /*****************************************************************/ |
| 15 | /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/ |
| 16 | /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/ |
| 17 | /*** + Base level indent should begin at left margin, as ***/ |
| 18 | /*** the require_once below looks. ***/ |
| 19 | /*** + All identation should consist of four space blocks ***/ |
| 20 | /*** + Tab characters are evil. ***/ |
| 21 | /*** + all comments should use "slash-star ... star-slash" ***/ |
| 22 | /*** style -- no pound characters, no slash-slash style ***/ |
| 23 | /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/ |
| 24 | /*** ALWAYS USE { AND } CHARACTERS!!! ***/ |
| 25 | /*** + Please use ' instead of ", when possible. Note " ***/ |
| 26 | /*** should always be used in _( ) function calls. ***/ |
| 27 | /*** Thank you for your help making the SM code more readable. ***/ |
| 28 | /*****************************************************************/ |
| 29 | |
| 30 | require_once('../src/validate.php'); |
| 31 | require_once('../functions/array.php'); |
| 32 | require_once('../functions/display_messages.php'); |
| 33 | require_once('../functions/addressbook.php'); |
| 34 | |
| 35 | /* Sort array by the key "name" */ |
| 36 | function alistcmp($a,$b) { |
| 37 | if($a['backend'] > $b['backend']) { |
| 38 | return 1; |
| 39 | } else { |
| 40 | if($a['backend'] < $b['backend']) { |
| 41 | return -1; |
| 42 | } |
| 43 | } |
| 44 | return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1; |
| 45 | } |
| 46 | |
| 47 | /* Make an input field */ |
| 48 | function adressbook_inp_field($label, $field, $name, $size, $values, $add) { |
| 49 | global $color; |
| 50 | echo '<TR><TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . |
| 51 | $label . ':</TD>' . |
| 52 | '<TD BGCOLOR="' . $color[4] . '" ALIGN=left>' . |
| 53 | '<INPUT NAME="' . $name . '[' . $field . ']" SIZE="' . $size . '" VALUE="'; |
| 54 | if (isset($values[$field])) { |
| 55 | echo htmlspecialchars($values[$field]); |
| 56 | } |
| 57 | echo '">' . $add . '</TD></TR>' . "\n"; |
| 58 | } |
| 59 | |
| 60 | /* Output form to add and modify address data */ |
| 61 | function address_form($name, $submittext, $values = array()) { |
| 62 | global $color; |
| 63 | |
| 64 | echo '<TABLE BORDER=0 CELLPADDING=1 COLS=2 WIDTH="90%" ALIGN=center>' ."\n"; |
| 65 | |
| 66 | adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, |
| 67 | '<SMALL>' . _("Must be unique") . '</SMALL>'); |
| 68 | adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, ''); |
| 69 | adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, ''); |
| 70 | adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, ''); |
| 71 | adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, ''); |
| 72 | |
| 73 | echo '<TR><TD COLSPAN=2 BGCOLOR="' . $color[4] . '" ALIGN=center>' . "\n" . |
| 74 | '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' . |
| 75 | $submittext . '"></TD></TR>' . |
| 76 | "\n</TABLE>\n"; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | // Open addressbook, with error messages on but without LDAP (the |
| 81 | // second "true"). Don't need LDAP here anyway |
| 82 | $abook = addressbook_init(true, true); |
| 83 | if($abook->localbackend == 0) { |
| 84 | plain_error_message( |
| 85 | _("No personal address book is defined. Contact administrator."), |
| 86 | $color); |
| 87 | exit(); |
| 88 | } |
| 89 | |
| 90 | displayPageHeader($color, 'None'); |
| 91 | |
| 92 | |
| 93 | $defdata = array(); |
| 94 | $formerror = ''; |
| 95 | $abortform = false; |
| 96 | $showaddrlist = true; |
| 97 | $defselected = array(); |
| 98 | |
| 99 | |
| 100 | // Handle user's actions |
| 101 | if($REQUEST_METHOD == 'POST') { |
| 102 | |
| 103 | // *********************************************** |
| 104 | // Add new address |
| 105 | // *********************************************** |
| 106 | if(!empty($addaddr['nickname'])) { |
| 107 | |
| 108 | $r = $abook->add($addaddr, $abook->localbackend); |
| 109 | |
| 110 | // Handle error messages |
| 111 | if(!$r) { |
| 112 | // Remove backend name from error string |
| 113 | $errstr = $abook->error; |
| 114 | $errstr = ereg_replace('^\[.*\] *', '', $errstr); |
| 115 | |
| 116 | $formerror = $errstr; |
| 117 | $showaddrlist = false; |
| 118 | $defdata = $addaddr; |
| 119 | } |
| 120 | |
| 121 | } else { |
| 122 | |
| 123 | // *********************************************** |
| 124 | // Delete address(es) |
| 125 | // *********************************************** |
| 126 | if((!empty($deladdr)) && sizeof($sel) > 0) { |
| 127 | $orig_sel = $sel; |
| 128 | sort($sel); |
| 129 | |
| 130 | // The selected addresses are identidied by "backend:nickname". |
| 131 | // Sort the list and process one backend at the time |
| 132 | $prevback = -1; |
| 133 | $subsel = array(); |
| 134 | $delfailed = false; |
| 135 | |
| 136 | for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) { |
| 137 | list($sbackend, $snick) = explode(':', $sel[$i]); |
| 138 | |
| 139 | // When we get to a new backend, process addresses in |
| 140 | // previous one. |
| 141 | if($prevback != $sbackend && $prevback != -1) { |
| 142 | |
| 143 | $r = $abook->remove($subsel, $prevback); |
| 144 | if(!$r) { |
| 145 | $formerror = $abook->error; |
| 146 | $i = sizeof($sel); |
| 147 | $delfailed = true; |
| 148 | break; |
| 149 | } |
| 150 | $subsel = array(); |
| 151 | } |
| 152 | |
| 153 | // Queue for processing |
| 154 | array_push($subsel, $snick); |
| 155 | $prevback = $sbackend; |
| 156 | } |
| 157 | |
| 158 | if(!$delfailed) { |
| 159 | $r = $abook->remove($subsel, $prevback); |
| 160 | if(!$r) { // Handle errors |
| 161 | $formerror = $abook->error; |
| 162 | $delfailed = true; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if($delfailed) { |
| 167 | $showaddrlist = true; |
| 168 | $defselected = $orig_sel; |
| 169 | } |
| 170 | |
| 171 | } else { |
| 172 | |
| 173 | // *********************************************** |
| 174 | // Update/modify address |
| 175 | // *********************************************** |
| 176 | if(!empty($editaddr)) { |
| 177 | |
| 178 | // Stage one: Copy data into form |
| 179 | if (isset($sel) && sizeof($sel) > 0) { |
| 180 | if(sizeof($sel) > 1) { |
| 181 | $formerror = _("You can only edit one address at the time"); |
| 182 | $showaddrlist = true; |
| 183 | $defselected = $sel; |
| 184 | } else { |
| 185 | $abortform = true; |
| 186 | list($ebackend, $enick) = explode(':', $sel[0]); |
| 187 | $olddata = $abook->lookup($enick, $ebackend); |
| 188 | |
| 189 | // Display the "new address" form |
| 190 | print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n"; |
| 191 | print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n"; |
| 192 | print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>"; |
| 193 | print _("Update address"); |
| 194 | print "<STRONG>\n</TD></TR>\n"; |
| 195 | print "</TABLE>\n"; |
| 196 | address_form("editaddr", _("Update address"), $olddata); |
| 197 | printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n", |
| 198 | htmlspecialchars($olddata["nickname"])); |
| 199 | printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n", |
| 200 | htmlspecialchars($olddata["backend"])); |
| 201 | print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n"; |
| 202 | print '</FORM>'; |
| 203 | } |
| 204 | } else { |
| 205 | |
| 206 | // Stage two: Write new data |
| 207 | if($doedit = 1) { |
| 208 | $newdata = $editaddr; |
| 209 | $r = $abook->modify($oldnick, $newdata, $backend); |
| 210 | |
| 211 | // Handle error messages |
| 212 | if(!$r) { |
| 213 | // Display error |
| 214 | print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n"; |
| 215 | print "<TR><TD ALIGN=CENTER>\n<br><STRONG>"; |
| 216 | print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ". |
| 217 | $abook->error."</FONT>"; |
| 218 | print "<STRONG>\n</TD></TR>\n"; |
| 219 | print "</TABLE>\n"; |
| 220 | |
| 221 | // Display the "new address" form again |
| 222 | printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF); |
| 223 | print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n"; |
| 224 | print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>"; |
| 225 | print _("Update address"); |
| 226 | print "<STRONG>\n</TD></TR>\n"; |
| 227 | print "</TABLE>\n"; |
| 228 | address_form("editaddr", _("Update address"), $newdata); |
| 229 | printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n", |
| 230 | htmlspecialchars($oldnick)); |
| 231 | printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n", |
| 232 | htmlspecialchars($backend)); |
| 233 | print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n"; |
| 234 | print '</FORM>'; |
| 235 | |
| 236 | $abortform = true; |
| 237 | } |
| 238 | } else { |
| 239 | |
| 240 | // Should not get here... |
| 241 | plain_error_message(_("Unknown error"), $color); |
| 242 | $abortform = true; |
| 243 | } |
| 244 | } |
| 245 | } /* !empty($editaddr) - Update/modify address */ |
| 246 | } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */ |
| 247 | } /* !empty($addaddr['nickname']) - Add new address */ |
| 248 | |
| 249 | // Some times we end output before forms are printed |
| 250 | if($abortform) { |
| 251 | print "</BODY></HTML>\n"; |
| 252 | exit(); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | |
| 257 | // =================================================================== |
| 258 | // The following is only executed on a GET request, or on a POST when |
| 259 | // a user is added, or when "delete" or "modify" was successful. |
| 260 | // =================================================================== |
| 261 | |
| 262 | // Display error messages |
| 263 | if(!empty($formerror)) { |
| 264 | print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n"; |
| 265 | print "<TR><TD ALIGN=CENTER>\n<br><STRONG>"; |
| 266 | print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>"; |
| 267 | print "<STRONG>\n</TD></TR>\n"; |
| 268 | print "</TABLE>\n"; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | // Display the address management part |
| 273 | if($showaddrlist) { |
| 274 | // Get and sort address list |
| 275 | $alist = $abook->list_addr(); |
| 276 | if(!is_array($alist)) { |
| 277 | plain_error_message($abook->error, $color); |
| 278 | exit; |
| 279 | } |
| 280 | |
| 281 | usort($alist,'alistcmp'); |
| 282 | $prevbackend = -1; |
| 283 | $headerprinted = false; |
| 284 | |
| 285 | echo "<p align=center><a href=\"#AddAddress\">" . |
| 286 | _("Add address") . "</a></p>\n"; |
| 287 | |
| 288 | // List addresses |
| 289 | printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF); |
| 290 | while(list($undef,$row) = each($alist)) { |
| 291 | |
| 292 | // New table header for each backend |
| 293 | if($prevbackend != $row["backend"]) { |
| 294 | if($prevbackend >= 0) { |
| 295 | print "<TR><TD COLSPAN=5 ALIGN=center>\n"; |
| 296 | printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n", |
| 297 | _("Edit selected")); |
| 298 | printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n", |
| 299 | _("Delete selected")); |
| 300 | echo "</tr>\n"; |
| 301 | print '<TR><TD COLSPAN="5" ALIGN=center>'; |
| 302 | print " <BR></TD></TR></TABLE>\n"; |
| 303 | } |
| 304 | |
| 305 | print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n"; |
| 306 | print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>"; |
| 307 | print $row["source"]; |
| 308 | print "<STRONG>\n</TD></TR>\n"; |
| 309 | print "</TABLE>\n"; |
| 310 | |
| 311 | print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">'; |
| 312 | printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s"> '. |
| 313 | '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'. |
| 314 | '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'. |
| 315 | "</TR>\n", $color[9], "1%", |
| 316 | "1%", _("Nickname"), |
| 317 | "1%", _("Name"), |
| 318 | "1%", _("E-mail"), |
| 319 | "%", _("Info")); |
| 320 | $line = 0; |
| 321 | $headerprinted = true; |
| 322 | } // End of header |
| 323 | |
| 324 | $prevbackend = $row['backend']; |
| 325 | |
| 326 | // Check if this user is selected |
| 327 | if(in_array($row['backend'].':'.$row['nickname'], $defselected)) { |
| 328 | $selected = 'CHECKED'; |
| 329 | } else { |
| 330 | $selected = ''; |
| 331 | } |
| 332 | |
| 333 | // Print one row |
| 334 | printf("<TR%s>", |
| 335 | (($line % 2) ? " bgcolor=\"$color[0]\"" : "")); |
| 336 | print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>'; |
| 337 | printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>', |
| 338 | $selected, $row["backend"], $row["nickname"]); |
| 339 | printf('<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>'. |
| 340 | '<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>', |
| 341 | "1%", $row["nickname"], |
| 342 | "1%", $row["name"]); |
| 343 | printf('<TD VALIGN=top NOWRAP WIDTH="%s"> <A HREF="compose.php?send_to=%s">%s</A> </TD>'."\n", |
| 344 | "1%", rawurlencode($row["email"]), $row["email"]); |
| 345 | printf('<TD VALIGN=top WIDTH="%s"> %s </TD>', |
| 346 | "%", $row["label"]); |
| 347 | print "</TR>\n"; |
| 348 | $line++; |
| 349 | } |
| 350 | |
| 351 | // End of list. Close table. |
| 352 | if($headerprinted) { |
| 353 | print "<TR><TD COLSPAN=5 ALIGN=center>\n"; |
| 354 | printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n", |
| 355 | _("Edit selected")); |
| 356 | printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n", |
| 357 | _("Delete selected")); |
| 358 | print "</TR></TABLE></FORM>"; |
| 359 | } |
| 360 | } // end of addresslist |
| 361 | |
| 362 | |
| 363 | // Display the "new address" form |
| 364 | echo "<a name=\"AddAddress\"></a>\n" . |
| 365 | "<FORM ACTION=\"$PHP_SELF\" NAME=f_add METHOD=\"POST\">\n". |
| 366 | "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n". |
| 367 | "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>"; |
| 368 | printf(_("Add to %s"), $abook->localbackendname); |
| 369 | echo "<STRONG>\n</TD></TR>\n". |
| 370 | "</TABLE>\n"; |
| 371 | address_form('addaddr', _("Add address"), $defdata); |
| 372 | echo '</FORM>'; |
| 373 | |
| 374 | // Add hook for anything that wants on the bottom |
| 375 | do_hook('addressbook_bottom'); |
| 376 | ?> |
| 377 | |
| 378 | </BODY></HTML> |