| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * addressbook.php |
| 5 | * |
| 6 | * Manage personal address book. |
| 7 | * |
| 8 | * @copyright © 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( |
| 51 | _("No personal address book is defined. Contact administrator."), |
| 52 | $color); |
| 53 | exit(); |
| 54 | } |
| 55 | |
| 56 | $defdata = array(); |
| 57 | $formerror = ''; |
| 58 | $abortform = false; |
| 59 | $showaddrlist = true; |
| 60 | $defselected = array(); |
| 61 | $form_url = 'addressbook.php'; |
| 62 | |
| 63 | /* Handle user's actions */ |
| 64 | if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') { |
| 65 | |
| 66 | /************************************************** |
| 67 | * Add new address * |
| 68 | **************************************************/ |
| 69 | if (isset($addaddr)) { |
| 70 | if (isset($backend)) { |
| 71 | $r = $abook->add($addaddr, $backend); |
| 72 | } else { |
| 73 | $r = $abook->add($addaddr, $abook->localbackend); |
| 74 | } |
| 75 | |
| 76 | /* Handle error messages */ |
| 77 | if (!$r) { |
| 78 | /* Remove backend name from error string */ |
| 79 | $errstr = $abook->error; |
| 80 | $errstr = ereg_replace('^\[.*\] *', '', $errstr); |
| 81 | |
| 82 | $formerror = $errstr; |
| 83 | $showaddrlist = false; |
| 84 | $defdata = $addaddr; |
| 85 | } |
| 86 | } else { |
| 87 | |
| 88 | /************************************************ |
| 89 | * Delete address(es) * |
| 90 | ************************************************/ |
| 91 | if ((!empty($deladdr)) && sizeof($sel) > 0) { |
| 92 | $orig_sel = $sel; |
| 93 | sort($sel); |
| 94 | |
| 95 | /* The selected addresses are identidied by "backend:nickname". * |
| 96 | * Sort the list and process one backend at the time */ |
| 97 | $prevback = -1; |
| 98 | $subsel = array(); |
| 99 | $delfailed = false; |
| 100 | |
| 101 | for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) { |
| 102 | list($sbackend, $snick) = explode(':', $sel[$i]); |
| 103 | |
| 104 | /* When we get to a new backend, process addresses in * |
| 105 | * previous one. */ |
| 106 | if ($prevback != $sbackend && $prevback != -1) { |
| 107 | |
| 108 | $r = $abook->remove($subsel, $prevback); |
| 109 | if (!$r) { |
| 110 | $formerror = $abook->error; |
| 111 | $i = sizeof($sel); |
| 112 | $delfailed = true; |
| 113 | break; |
| 114 | } |
| 115 | $subsel = array(); |
| 116 | } |
| 117 | |
| 118 | /* Queue for processing */ |
| 119 | array_push($subsel, $snick); |
| 120 | $prevback = $sbackend; |
| 121 | } |
| 122 | |
| 123 | if (!$delfailed) { |
| 124 | $r = $abook->remove($subsel, $prevback); |
| 125 | if (!$r) { /* Handle errors */ |
| 126 | $formerror = $abook->error; |
| 127 | $delfailed = true; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if ($delfailed) { |
| 132 | $showaddrlist = true; |
| 133 | $defselected = $orig_sel; |
| 134 | } |
| 135 | |
| 136 | } else { |
| 137 | |
| 138 | /*********************************************** |
| 139 | * Update/modify address * |
| 140 | ***********************************************/ |
| 141 | if (!empty($editaddr)) { |
| 142 | /* Stage one: Copy data into form */ |
| 143 | if (isset($sel) && sizeof($sel) > 0) { |
| 144 | if(sizeof($sel) > 1) { |
| 145 | $formerror = _("You can only edit one address at the time"); |
| 146 | $showaddrlist = true; |
| 147 | $defselected = $sel; |
| 148 | } else { |
| 149 | $abortform = true; |
| 150 | list($ebackend, $enick) = explode(':', $sel[0]); |
| 151 | $olddata = $abook->lookup($enick, $ebackend); |
| 152 | // FIXME: Test if $olddata really contains anything and return an error message if it doesn't |
| 153 | |
| 154 | /* Display the "new address" form */ |
| 155 | abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata); |
| 156 | echo addHidden('oldnick', $olddata['nickname']). |
| 157 | addHidden('backend', $olddata['backend']). |
| 158 | addHidden('doedit', '1'). |
| 159 | '</form>'; |
| 160 | } |
| 161 | } elseif ($doedit == 1) { |
| 162 | /* Stage two: Write new data */ |
| 163 | $newdata = $editaddr; |
| 164 | $r = $abook->modify($oldnick, $newdata, $backend); |
| 165 | |
| 166 | /* Handle error messages */ |
| 167 | if (!$r) { |
| 168 | /* Display error */ |
| 169 | echo html_tag( 'table', |
| 170 | html_tag( 'tr', |
| 171 | html_tag( 'td', |
| 172 | "\n". '<strong><font color="' . $color[2] . |
| 173 | '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n", |
| 174 | 'center' ) |
| 175 | ), |
| 176 | 'center', '', 'width="100%"' ); |
| 177 | |
| 178 | /* Display the "new address" form again */ |
| 179 | abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata); |
| 180 | echo addHidden('oldnick', $oldnick). |
| 181 | addHidden('backend', $backend). |
| 182 | addHidden('doedit', '1'). |
| 183 | "\n" . '</form>'; |
| 184 | $abortform = true; |
| 185 | } |
| 186 | } else { |
| 187 | /** |
| 188 | * $editaddr is set, but $sel (address selection in address listing) |
| 189 | * and $doedit (address edit form) are not set. |
| 190 | * Assume that user clicked on "Edit address" without selecting any address. |
| 191 | */ |
| 192 | $formerror = _("Please select address that you want to edit"); |
| 193 | $showaddrlist = true; |
| 194 | } /* end of edit stage detection */ |
| 195 | } /* !empty($editaddr) - Update/modify address */ |
| 196 | } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */ |
| 197 | } /* !empty($addaddr['nickname']) - Add new address */ |
| 198 | |
| 199 | // Some times we end output before forms are printed |
| 200 | if($abortform) { |
| 201 | echo "</body></html>\n"; |
| 202 | exit(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /* =================================================================== * |
| 208 | * The following is only executed on a GET request, or on a POST when * |
| 209 | * a user is added, or when "delete" or "modify" was successful. * |
| 210 | * =================================================================== */ |
| 211 | |
| 212 | /* Display error messages */ |
| 213 | if (!empty($formerror)) { |
| 214 | echo html_tag( 'table', |
| 215 | html_tag( 'tr', |
| 216 | html_tag( 'td', |
| 217 | "\n". '<br /><strong><font color="' . $color[2] . |
| 218 | '">' . _("ERROR") . ': ' . nl2br(htmlspecialchars($formerror)) . '</font></strong>' ."\n", |
| 219 | 'center' ) |
| 220 | ), |
| 221 | 'center', '', 'width="100%"' ); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /* Display the address management part */ |
| 226 | if ($showaddrlist) { |
| 227 | /* Get and sort address list */ |
| 228 | $alist = $abook->list_addr(); |
| 229 | if(!is_array($alist)) { |
| 230 | plain_error_message(nl2br(htmlspecialchars($abook->error)), $color); |
| 231 | exit; |
| 232 | } |
| 233 | |
| 234 | usort($alist,'alistcmp'); |
| 235 | $prevbackend = -1; |
| 236 | $headerprinted = false; |
| 237 | |
| 238 | echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n"; |
| 239 | |
| 240 | /* List addresses */ |
| 241 | if (count($alist) > 0) { |
| 242 | echo addForm($form_url, 'post'); |
| 243 | if ($abook->add_extra_field) { |
| 244 | $abook_fields = 6; |
| 245 | } else { |
| 246 | $abook_fields = 5; |
| 247 | } |
| 248 | while(list($undef,$row) = each($alist)) { |
| 249 | |
| 250 | /* New table header for each backend */ |
| 251 | if($prevbackend != $row['backend']) { |
| 252 | if($prevbackend < 0) { |
| 253 | echo html_tag( 'table', |
| 254 | html_tag( 'tr', |
| 255 | html_tag( 'td', |
| 256 | addSubmit(_("Edit selected"), 'editaddr'). |
| 257 | addSubmit(_("Delete selected"), 'deladdr'), |
| 258 | 'center', '', "colspan=\"$abook_fields\"" ) |
| 259 | ) . |
| 260 | html_tag( 'tr', |
| 261 | html_tag( 'td', ' <br />', 'center', '', "colspan=\"$abook_fields\"" ) |
| 262 | ), |
| 263 | 'center' ); |
| 264 | echo "\n<!-- start of address book table -->\n" . |
| 265 | html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) . |
| 266 | html_tag( 'tr', "\n" . |
| 267 | html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" . |
| 268 | html_tag( 'th', _("Nickname") . |
| 269 | show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1), |
| 270 | 'left', '', 'width="1%"' ) . "\n" . |
| 271 | html_tag( 'th', _("Name") . |
| 272 | show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3), |
| 273 | 'left', '', 'width="1%"' ) . "\n" . |
| 274 | html_tag( 'th', _("E-mail") . |
| 275 | show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5), |
| 276 | 'left', '', 'width="1%"' ) . "\n" . |
| 277 | html_tag( 'th', _("Info") . |
| 278 | show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7), |
| 279 | 'left', '', 'width="1%"' ) . |
| 280 | ($abook->add_extra_field ? html_tag( 'th', ' ','left', '', 'width="1%"'): '') . |
| 281 | "\n", |
| 282 | '', $color[9] ) . "\n"; |
| 283 | } |
| 284 | |
| 285 | // Separate different backends with <hr /> |
| 286 | if($prevbackend > 0) { |
| 287 | echo html_tag( 'tr', |
| 288 | html_tag( 'td', "<hr />", 'center', '' ,"colspan=\"$abook_fields\"" ) |
| 289 | ); |
| 290 | } |
| 291 | |
| 292 | // Print backend name |
| 293 | echo html_tag( 'tr', |
| 294 | html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,"colspan=\"$abook_fields\"" ) |
| 295 | ); |
| 296 | |
| 297 | $line = 0; |
| 298 | $headerprinted = true; |
| 299 | } /* End of header */ |
| 300 | |
| 301 | $prevbackend = $row['backend']; |
| 302 | |
| 303 | /* Check if this user is selected */ |
| 304 | $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected); |
| 305 | |
| 306 | /* Print one row, with alternating color */ |
| 307 | if ($line % 2) { |
| 308 | $tr_bgcolor = $color[12]; |
| 309 | } else { |
| 310 | $tr_bgcolor = $color[4]; |
| 311 | } |
| 312 | echo html_tag( 'tr', '', '', $tr_bgcolor); |
| 313 | if ($abook->backends[$row['backend']]->writeable) { |
| 314 | $id = $row['backend'].':'.$row['nickname']; |
| 315 | echo html_tag( 'td', |
| 316 | '<small>' . |
| 317 | addCheckBox("sel[$id]", $selected, $id). |
| 318 | '</small>' , |
| 319 | 'center', '', 'valign="top" width="1%"' ); |
| 320 | $label1 = '<label for="sel_'.$id.'_">'; $label2='</label>'; |
| 321 | } else { |
| 322 | echo html_tag( 'td', |
| 323 | ' ' , |
| 324 | 'center', '', 'valign="top" width="1%"' ); |
| 325 | $label1 = $label2 = ''; |
| 326 | } |
| 327 | echo html_tag( 'td', |
| 328 | ' ' . $label1 . htmlspecialchars($row['nickname']) . $label2 . ' ', |
| 329 | 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ); |
| 330 | |
| 331 | echo html_tag( 'td', |
| 332 | ' ' . $label1 . htmlspecialchars($row['name']) . $label2 . ' ', |
| 333 | 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ); |
| 334 | |
| 335 | // email address column |
| 336 | echo html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' '; |
| 337 | $email = $abook->full_address($row); |
| 338 | echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email), |
| 339 | htmlspecialchars($row['email'])). |
| 340 | ' </td>'."\n"; |
| 341 | |
| 342 | // info column |
| 343 | echo html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ); |
| 344 | |
| 345 | // add extra column if third party backend needs it |
| 346 | if ($abook->add_extra_field) { |
| 347 | echo html_tag( 'td', |
| 348 | ' ' . (isset($row['extra']) ? $row['extra'] : '') . ' ', |
| 349 | 'left', '', 'valign="top" width="1%"' ); |
| 350 | } |
| 351 | echo "</tr>\n"; |
| 352 | $line++; |
| 353 | } |
| 354 | echo "</table>" . |
| 355 | "\n<!-- end of address book table -->\n"; |
| 356 | |
| 357 | /* End of list. Add edit/delete select buttons */ |
| 358 | if ($headerprinted) { |
| 359 | echo html_tag( 'table', |
| 360 | html_tag( 'tr', |
| 361 | html_tag( 'td', |
| 362 | addSubmit(_("Edit selected"), 'editaddr') . |
| 363 | addSubmit(_("Delete selected"), 'deladdr'), |
| 364 | 'center', '', "colspan=\"$abook_fields\"" ) |
| 365 | ), |
| 366 | 'center' ); |
| 367 | } |
| 368 | echo "</form>\n"; |
| 369 | } |
| 370 | } /* end of addresslist */ |
| 371 | |
| 372 | |
| 373 | /* Display the "new address" form */ |
| 374 | echo '<a name="AddAddress"></a>' . "\n"; |
| 375 | abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata); |
| 376 | echo "</form>\n"; |
| 377 | |
| 378 | /* Hook for extra address book blocks */ |
| 379 | echo "<!-- start of addressbook_bottom hook-->\n"; |
| 380 | do_hook('addressbook_bottom'); |
| 381 | echo "\n<!-- end of addressbook_bottom hook-->\n"; |
| 382 | $oTemplate->display('footer.tpl'); |
| 383 | ?> |