| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * addressbook.php |
| 5 | * |
| 6 | * Manage personal address book. |
| 7 | * |
| 8 | * @copyright © 1999-2007 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 | /** This is the addressbook page */ |
| 16 | define('PAGE_NAME', 'addressbook'); |
| 17 | |
| 18 | /** |
| 19 | * Include the SquirrelMail initialization file. |
| 20 | */ |
| 21 | include('../include/init.php'); |
| 22 | |
| 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'); |
| 27 | |
| 28 | /* form functions */ |
| 29 | require_once(SM_PATH . 'functions/forms.php'); |
| 30 | |
| 31 | /** lets get the global vars we may need */ |
| 32 | |
| 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); |
| 41 | |
| 42 | /* Get sorting order */ |
| 43 | $abook_sort_order = get_abook_sort(); |
| 44 | |
| 45 | /* Create page header before addressbook_init in order to display error messages correctly. */ |
| 46 | displayPageHeader($color); |
| 47 | |
| 48 | /* Open addressbook with error messages on. |
| 49 | remote backends (LDAP) are enabled because they can be used. (list_addr function) |
| 50 | */ |
| 51 | $abook = addressbook_init(true, false); |
| 52 | |
| 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.")); |
| 56 | exit(); |
| 57 | } |
| 58 | |
| 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; |
| 62 | } |
| 63 | |
| 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; |
| 70 | } |
| 71 | } |
| 72 | if (count($list_backends)>1) { |
| 73 | $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true) |
| 74 | .addSubmit(_("Change"),'change_abook'); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | $defdata = array(); |
| 79 | $formerror = ''; |
| 80 | $abortform = false; |
| 81 | $showaddrlist = true; |
| 82 | $defselected = array(); |
| 83 | $form_url = 'addressbook.php'; |
| 84 | |
| 85 | /* Handle user's actions */ |
| 86 | if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') { |
| 87 | |
| 88 | /************************************************** |
| 89 | * Add new address * |
| 90 | **************************************************/ |
| 91 | if (isset($addaddr)) { |
| 92 | if (isset($backend)) { |
| 93 | $r = $abook->add($addaddr, $backend); |
| 94 | } else { |
| 95 | $r = $abook->add($addaddr, $abook->localbackend); |
| 96 | } |
| 97 | |
| 98 | /* Handle error messages */ |
| 99 | if (!$r) { |
| 100 | /* Remove backend name from error string */ |
| 101 | $errstr = $abook->error; |
| 102 | $errstr = ereg_replace('^\[.*\] *', '', $errstr); |
| 103 | |
| 104 | $formerror = $errstr; |
| 105 | $showaddrlist = false; |
| 106 | $defdata = $addaddr; |
| 107 | } |
| 108 | } else { |
| 109 | |
| 110 | /************************************************ |
| 111 | * Delete address(es) * |
| 112 | ************************************************/ |
| 113 | if ((!empty($deladdr)) && sizeof($sel) > 0) { |
| 114 | $orig_sel = $sel; |
| 115 | sort($sel); |
| 116 | |
| 117 | /* The selected addresses are identidied by "nickname_backend". * |
| 118 | * Sort the list and process one backend at the time */ |
| 119 | $prevback = -1; |
| 120 | $subsel = array(); |
| 121 | $delfailed = false; |
| 122 | |
| 123 | for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) { |
| 124 | list($snick, $sbackend) = explode('_', $sel[$i]); |
| 125 | |
| 126 | /* When we get to a new backend, process addresses in * |
| 127 | * previous one. */ |
| 128 | if ($prevback != $sbackend && $prevback != -1) { |
| 129 | |
| 130 | $r = $abook->remove($subsel, $prevback); |
| 131 | if (!$r) { |
| 132 | $formerror = $abook->error; |
| 133 | $i = sizeof($sel); |
| 134 | $delfailed = true; |
| 135 | break; |
| 136 | } |
| 137 | $subsel = array(); |
| 138 | } |
| 139 | |
| 140 | /* Queue for processing */ |
| 141 | array_push($subsel, $snick); |
| 142 | $prevback = $sbackend; |
| 143 | } |
| 144 | |
| 145 | if (!$delfailed) { |
| 146 | $r = $abook->remove($subsel, $prevback); |
| 147 | if (!$r) { /* Handle errors */ |
| 148 | $formerror = $abook->error; |
| 149 | $delfailed = true; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if ($delfailed) { |
| 154 | $showaddrlist = true; |
| 155 | $defselected = $orig_sel; |
| 156 | } |
| 157 | |
| 158 | } else { |
| 159 | |
| 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; |
| 169 | $defselected = $sel; |
| 170 | } else { |
| 171 | $abortform = 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 |
| 175 | if (!$olddata) { |
| 176 | error_box(nl2br(htmlspecialchars($abook->error))); |
| 177 | } else { |
| 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'). |
| 183 | '</form>'; |
| 184 | } |
| 185 | } |
| 186 | } elseif ($doedit == 1) { |
| 187 | /* Stage two: Write new data */ |
| 188 | $newdata = $editaddr; |
| 189 | $r = $abook->modify($oldnick, $newdata, $backend); |
| 190 | |
| 191 | /* Handle error messages */ |
| 192 | if (!$r) { |
| 193 | /* Display error */ |
| 194 | plain_error_message( nl2br(htmlspecialchars($abook->error))); |
| 195 | |
| 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'). |
| 201 | "\n" . '</form>'; |
| 202 | $abortform = true; |
| 203 | } |
| 204 | } else { |
| 205 | /** |
| 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. |
| 209 | */ |
| 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 */ |
| 216 | |
| 217 | // Some times we end output before forms are printed |
| 218 | if($abortform) { |
| 219 | echo "</body></html>\n"; |
| 220 | exit(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 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 | * =================================================================== */ |
| 229 | |
| 230 | /* Display error messages */ |
| 231 | if (!empty($formerror)) { |
| 232 | plain_error_message(nl2br(htmlspecialchars($formerror))); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | /* Display the address management part */ |
| 237 | $addresses = array(); |
| 238 | while (list($k, $backend) = each ($abook->backends)) { |
| 239 | $a = array(); |
| 240 | $a['BackendID'] = $backend->bnum; |
| 241 | $a['BackendSource'] = $backend->sname; |
| 242 | $a['BackendWritable'] = $backend->writeable; |
| 243 | $a['Addresses'] = array(); |
| 244 | |
| 245 | $alist = $abook->list_addr($backend->bnum); |
| 246 | |
| 247 | /* check return (array with data or boolean false) */ |
| 248 | if (is_array($alist)) { |
| 249 | usort($alist,'alistcmp'); |
| 250 | |
| 251 | $a['Addresses'] = formatAddressList($alist); |
| 252 | |
| 253 | $addresses[$backend->bnum] = $a; |
| 254 | } else { |
| 255 | // list_addr() returns boolean |
| 256 | plain_error_message(nl2br(htmlspecialchars($abook->error))); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | |
| 261 | if ($showaddrlist) { |
| 262 | echo addForm($form_url, 'post', 'address_book_form'); |
| 263 | |
| 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); |
| 268 | |
| 269 | $oTemplate->display('addressbook_list.tpl'); |
| 270 | |
| 271 | //FIXME: Remove HTML from here! |
| 272 | echo "</form>\n"; |
| 273 | } |
| 274 | |
| 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); |
| 279 | echo "</form>\n"; |
| 280 | |
| 281 | /* Hook for extra address book blocks */ |
| 282 | do_hook('addressbook_bottom', $null); |
| 283 | |
| 284 | $oTemplate->display('footer.tpl'); |