[ Index ] |
|
Code source de IMP H3 (4.1.5) |
1 <?php 2 // Compose encryption options 3 /** 4 * Send Message w/no encryption. 5 */ 6 define('IMP_ENCRYPT_NONE', 1); 7 8 /** 9 * Send Message - PGP Encrypt. 10 */ 11 define('IMP_PGP_ENCRYPT', 2); 12 13 /** 14 * Send Message - PGP Sign. 15 */ 16 define('IMP_PGP_SIGN', 3); 17 18 /** 19 * Send Message - PGP Sign/Encrypt. 20 */ 21 define('IMP_PGP_SIGNENC', 4); 22 23 /** 24 * Send Message - S/MIME Encrypt. 25 */ 26 define('IMP_SMIME_ENCRYPT', 5); 27 28 /** 29 * Send Message - S/MIME Sign. 30 */ 31 define('IMP_SMIME_SIGN', 6); 32 33 /** 34 * Send Message - S/MIME Sign/Encrypt. 35 */ 36 define('IMP_SMIME_SIGNENC', 7); 37 38 // IMAP Flags 39 /** 40 * Match all IMAP flags. 41 */ 42 define('IMP_ALL', 0); 43 44 /** 45 * \\UNSEEN flag 46 .*/ 47 define('IMP_UNSEEN', 1); 48 49 /** 50 * \\DELETED flag 51 .*/ 52 define('IMP_DELETED', 2); 53 54 /** 55 * \\ANSWERED flag. 56 */ 57 define('IMP_ANSWERED', 4); 58 59 /** 60 * \\FLAGGED flag. 61 */ 62 define('IMP_FLAGGED', 8); 63 64 /** 65 * \\DRAFT flag. 66 */ 67 define('IMP_DRAFT', 16); 68 69 /** 70 * An email is personal. 71 */ 72 define('IMP_PERSONAL', 32); 73 74 // IMAP Sorting Constant 75 /** 76 * Sort By Thread. 77 */ 78 @define('SORTTHREAD', 161); 79 80 // IMP Mailbox view constants 81 /** 82 * Start on the page with the first unseen message. 83 */ 84 define('IMP_MAILBOXSTART_FIRSTUNSEEN', 1); 85 86 /** 87 * Start on the page with the last unseen message. 88 */ 89 define('IMP_MAILBOXSTART_LASTUNSEEN', 2); 90 91 /** 92 * Start on the first page. 93 */ 94 define('IMP_MAILBOXSTART_FIRSTPAGE', 3); 95 96 /** 97 * Start on the last page. 98 */ 99 define('IMP_MAILBOXSTART_LASTPAGE', 4); 100 101 // IMP mailbox labels 102 /** 103 * The mailbox name to use for search results. 104 */ 105 define('IMP_SEARCH_MBOX', '**search_'); 106 107 // IMP internal indexing strings 108 /** 109 * String used to separate messages. 110 */ 111 define('IMP_MSG_SEP', "\0"); 112 113 /** 114 * String used to separate indexes. 115 */ 116 define('IMP_IDX_SEP', "\1"); 117 118 /** 119 * IMP Base Class. 120 * 121 * $Horde: imp/lib/IMP.php,v 1.449.4.71 2007/05/04 16:36:38 jan Exp $ 122 * 123 * Copyright 1999-2007 Chuck Hagenbuch <chuck@horde.org> 124 * Copyright 1999-2007 Jon Parise <jon@horde.org> 125 * Copyright 2002-2007 Michael Slusarz <slusarz@bigworm.colorado.edu> 126 * 127 * See the enclosed file COPYING for license information (GPL). If you did not 128 * receive this file, see http://www.fsf.org/copyleft/gpl.html. 129 * 130 * @author Chuck Hagenbuch <chuck@horde.org> 131 * @author Jon Parise <jon@horde.org> 132 * @author Michael Slusarz <slusarz@bigworm.colorado.edu> 133 * @since IMP 2.3.5 134 * @package IMP 135 */ 136 class IMP { 137 138 /** 139 * Returns the AutoLogin server key. 140 * 141 * @param boolean $first Return the first value? 142 * 143 * @return string The server key. 144 */ 145 function getAutoLoginServer($first = false) 146 { 147 require IMP_BASE . '/config/servers.php'; 148 foreach ($servers as $key => $curServer) { 149 if (empty($server_key) && substr($key, 0, 1) != '_') { 150 $server_key = $key; 151 } 152 if (IMP::isPreferredServer($curServer, ($first) ? $key : null)) { 153 $server_key = $key; 154 if ($first) { 155 break; 156 } 157 } 158 } 159 160 return $server_key; 161 } 162 163 /** 164 * Returns whether we can log in without a login screen for $server_key. 165 * 166 * @param string $server_key The server to check. Defaults to 167 * IMP::getCurrentServer(). 168 * @param boolean $force If true, check $server_key even if there is 169 * more than one server available. 170 * 171 * @return boolean True or false. 172 */ 173 function canAutoLogin($server_key = null, $force = false) 174 { 175 require IMP_BASE . '/config/servers.php'; 176 177 $auto_server = IMP::getAutoLoginServer(); 178 if (is_null($server_key)) { 179 $server_key = $auto_server; 180 } 181 182 return (((count($auto_server) == 1) || $force) && 183 Auth::getAuth() && 184 !empty($servers[$server_key]['hordeauth'])); 185 } 186 187 /** 188 * Makes sure the user has been authenticated to view the page. 189 * 190 * @param mixed $flags Any flags to pass to imap_open(). See 191 * Auth_imp::authenticate(). However, if this is 192 * the string 'horde', we just check for Horde auth 193 * and don't bother the IMAP server. 194 * @param boolean $return If this is true, return false instead of 195 * exiting/redirecting if authentication fails. 196 * 197 * @return boolean True on success, false on error. 198 */ 199 function checkAuthentication($flags = 0, $return = false) 200 { 201 if ($flags === 'horde') { 202 $reason = Auth::isAuthenticated(); 203 } else { 204 $auth_imp = &Auth::singleton(array('imp', 'imp')); 205 $auth_imp->authenticateOptions(array('flags' => $flags)); 206 $reason = $auth_imp->authenticate(); 207 } 208 209 if ($reason !== true) { 210 if ($return) { 211 return false; 212 } 213 214 if (Util::getFormData('popup')) { 215 Util::closeWindowJS(); 216 } else { 217 $url = Auth::addLogoutParameters(IMP::logoutUrl()); 218 $url = Util::addParameter($url, 'url', Horde::selfUrl(true)); 219 header('Location: ' . $url); 220 } 221 exit; 222 } 223 224 return true; 225 } 226 227 /** 228 * Determines if the given mail server is the "preferred" mail server for 229 * this web server. This decision is based on the global 'SERVER_NAME' 230 * and 'HTTP_HOST' server variables and the contents of the 'preferred' 231 * either field in the server's definition. The 'preferred' field may 232 * take a single value or an array of multiple values. 233 * 234 * @param string $server A complete server entry from the $servers hash. 235 * @param TODO $key TODO 236 * 237 * @return boolean True if this entry is "preferred". 238 */ 239 function isPreferredServer($server, $key = null) 240 { 241 static $urlServer; 242 243 if (!isset($urlServer)) { 244 $urlServer = Util::getFormData('server'); 245 } 246 247 if (!empty($urlServer)) { 248 return ($key == $urlServer); 249 } 250 251 if (!empty($server['preferred'])) { 252 if (is_array($server['preferred'])) { 253 if (in_array($_SERVER['SERVER_NAME'], $server['preferred']) || 254 in_array($_SERVER['HTTP_HOST'], $server['preferred'])) { 255 return true; 256 } 257 } elseif (($server['preferred'] == $_SERVER['SERVER_NAME']) || 258 ($server['preferred'] == $_SERVER['HTTP_HOST'])) { 259 return true; 260 } 261 } 262 263 return false; 264 } 265 266 /** 267 * Generates a full c-client server specification string. 268 * 269 * @param string $mbox The mailbox to append to end of the server 270 * string. 271 * @param string $protocol Override the protocol currently being used. 272 * 273 * @return string The full spec string. 274 */ 275 function serverString($mbox = null, $protocol = null) 276 { 277 $srvstr = '{' . $_SESSION['imp']['server']; 278 279 /* If port is not specified, don't include it in the string. */ 280 if (!empty($_SESSION['imp']['port'])) { 281 $srvstr .= ':' . $_SESSION['imp']['port']; 282 } 283 284 if (is_null($protocol)) { 285 $protocol = $_SESSION['imp']['protocol']; 286 } 287 288 /* If protocol is not specified, don't include it in the string. */ 289 if (!empty($protocol)) { 290 $srvstr .= '/' . $protocol; 291 } 292 293 return $srvstr . '}' . $mbox; 294 } 295 296 /** 297 * Returns the plain text label that is displayed for the current mailbox, 298 * replacing IMP_SEARCH_MBOX with an appropriate string and removing 299 * namespace and folder prefix information from what is shown to the user. 300 * 301 * @return string The plain text label. 302 */ 303 function getLabel() 304 { 305 $label = ''; 306 307 if ($GLOBALS['imp_search']->searchMboxID()) { 308 $label = $GLOBALS['imp_search']->getLabel(); 309 } else { 310 $label = IMP::displayFolder($_SESSION['imp']['mailbox']); 311 } 312 313 return $label; 314 } 315 316 /** 317 * Returns the bare address. 318 * 319 * @param string $address The address string. 320 * @param boolean $multiple Should we return multiple results? 321 * 322 * @return mixed See {@link MIME::bareAddress}. 323 */ 324 function bareAddress($address, $multiple = false) 325 { 326 static $addresses; 327 328 if (!isset($addresses[(string)$multiple][$address])) { 329 require_once 'Horde/MIME.php'; 330 $addresses[(string)$multiple][$address] = MIME::bareAddress($address, $_SESSION['imp']['maildomain'], $multiple); 331 } 332 333 return $addresses[(string)$multiple][$address]; 334 } 335 336 /** 337 * Uses the Registry to expand names and returning error information for 338 * any address that is either not valid or fails to expand. 339 * 340 * @param string $addrString The name(s) or address(es) to expand. 341 * @param boolean $full If true generate a full, rfc822-valid address 342 * list. 343 * 344 * @return mixed Either a string containing all expanded addresses or an 345 * array containing all matching address or an error 346 * object. 347 */ 348 function expandAddresses($addrString, $full = false) 349 { 350 if (!preg_match('|[^\s]|', $addrString)) { 351 return ''; 352 } 353 354 global $prefs; 355 356 require_once 'Mail/RFC822.php'; 357 require_once 'Horde/MIME.php'; 358 359 $parser = &new Mail_RFC822(null, '@INVALID'); 360 $search_fields = array(); 361 362 $src = explode("\t", $prefs->getValue('search_sources')); 363 if ((count($src) == 1) && empty($src[0])) { 364 $src = array(); 365 } 366 367 if (($val = $prefs->getValue('search_fields'))) { 368 $field_arr = explode("\n", $val); 369 foreach ($field_arr as $field) { 370 $field = trim($field); 371 if (!empty($field)) { 372 $tmp = explode("\t", $field); 373 if (count($tmp) > 1) { 374 $source = array_splice($tmp, 0, 1); 375 $search_fields[$source[0]] = $tmp; 376 } 377 } 378 } 379 } 380 381 $arr = MIME::rfc822Explode($addrString, ','); 382 $arr = array_map('trim', $arr); 383 $arr = array_filter($arr); 384 385 $results = $GLOBALS['registry']->call('contacts/search', array($arr, $src, $search_fields)); 386 if (is_a($results, 'PEAR_Error')) { 387 return $results; 388 } 389 390 /* Remove any results with empty email addresses. */ 391 $keys = array_keys($results); 392 foreach ($keys as $key) { 393 $subTotal = count($results[$key]); 394 for ($i = 0; $i < $subTotal; ++$i) { 395 if (empty($results[$key][$i]['email'])) { 396 unset($results[$key][$i]); 397 } 398 } 399 } 400 401 $ambiguous = false; 402 $error = false; 403 $missing = array(); 404 405 foreach ($arr as $i => $tmp) { 406 $address = MIME::encodeAddress($tmp, null, ''); 407 if (!is_a($address, 'PEAR_Error') && 408 ($parser->validateMailbox($address) || 409 $parser->_isGroup($address))) { 410 // noop 411 } elseif (!isset($results[$tmp]) || !count($results[$tmp])) { 412 /* Handle the missing/invalid case - we should return error 413 * info on each address that couldn't be 414 * expanded/validated. */ 415 $error = true; 416 if (!$ambiguous) { 417 $arr[$i] = PEAR::raiseError(null, null, null, null, $arr[$i]); 418 $missing[$i] = $arr[$i]; 419 } 420 } else { 421 $res = $results[$tmp]; 422 if (count($res) == 1) { 423 if ($full) { 424 if (strpos($res[0]['email'], ',') !== false) { 425 $vars = get_class_vars('MIME'); 426 $arr[$i] = MIME::_rfc822Encode($res[0]['name'], $vars['rfc822_filter'] . '.') . ': ' . $res[0]['email'] . ';'; 427 } else { 428 list($mbox, $host) = explode('@', $res[0]['email']); 429 $arr[$i] = MIME::rfc822WriteAddress($mbox, $host, $res[0]['name']); 430 } 431 } else { 432 $arr[$i] = $res[0]['name']; 433 } 434 } else { 435 /* Handle the multiple case - we return an array 436 * with all found addresses. */ 437 $arr[$i] = array($arr[$i]); 438 foreach ($res as $one_res) { 439 if ($full) { 440 if (strpos($one_res['email'], ',') !== false) { 441 $vars = get_class_vars('MIME'); 442 $arr[$i][] = MIME::_rfc822Encode($one_res['name'], $vars['rfc822_filter'] . '.') . ': ' . $one_res['email'] . ';'; 443 } else { 444 $mbox_host = explode('@', $one_res['email']); 445 if (isset($mbox_host[1])) { 446 $arr[$i][] = MIME::rfc822WriteAddress($mbox_host[0], $mbox_host[1], $one_res['name']); 447 } 448 } 449 } else { 450 $arr[$i][] = $one_res['name']; 451 } 452 } 453 $ambiguous = true; 454 } 455 } 456 } 457 458 if ($ambiguous) { 459 foreach ($missing as $i => $addr) { 460 $arr[$i] = $addr->getUserInfo(); 461 } 462 return $arr; 463 } elseif ($error) { 464 return PEAR::raiseError(_("Please resolve ambiguous or invalid addresses."), null, null, null, $arr); 465 } else { 466 $list = ''; 467 foreach ($arr as $elm) { 468 if (substr($list, -1) == ';') { 469 $list .= ' '; 470 } elseif (!empty($list)) { 471 $list .= ', '; 472 } 473 $list .= $elm; 474 } 475 return $list; 476 } 477 } 478 479 /** 480 * Adds a contact to the user defined address book. 481 * 482 * @param string $newAddress The contact's email address. 483 * @param string $newName The contact's name. 484 * 485 * @return string A link or message to show in the notification area. 486 */ 487 function addAddress($newAddress, $newName) 488 { 489 global $registry, $prefs; 490 491 if (empty($newName)) { 492 $newName = $newAddress; 493 } 494 495 $result = $registry->call('contacts/import', 496 array(array('name' => $newName, 'email' => $newAddress), 497 'array', $prefs->getValue('add_source'))); 498 if (is_a($result, 'PEAR_Error')) { 499 return $result; 500 } else { 501 $contact_link = $registry->link('contacts/show', array('uid' => $result, 'source' => $prefs->getValue('add_source'))); 502 if (!empty($contact_link) && !is_a($contact_link, 'PEAR_Error')) { 503 $contact_link = Horde::link(Horde::url($contact_link), sprintf(_("Go to address book entry of \"%s\""), $newName)) . @htmlspecialchars($newName, ENT_COMPAT, NLS::getCharset()) . '</a>'; 504 } else { 505 $contact_link = @htmlspecialchars($newName, ENT_COMPAT, NLS::getCharset()); 506 } 507 return $contact_link; 508 } 509 } 510 511 /** 512 * Parses an address or address list into the address components, 513 * automatically choosing the best suited parsing method. 514 * 515 * @param string $address An address or address list. 516 * 517 * @return array A list of address objects. 518 */ 519 function parseAddressList($address) 520 { 521 static $parser; 522 523 if (Util::extensionExists('imap') && 524 strpos($address, ':') === false) { 525 $result = @imap_rfc822_parse_adrlist($address, ''); 526 if (is_array($result)) { 527 return $result; 528 } 529 } 530 531 if (!isset($parser)) { 532 require_once 'Mail/RFC822.php'; 533 $parser = &new Mail_RFC822(); 534 } 535 536 return $parser->parseAddressList($address, '', false, false); 537 } 538 539 /** 540 * Wrapper around IMP_Folder::flist() which generates the body of a 541 * <select> form input from the generated folder list. The 542 * <select> and </select> tags are NOT included in the output 543 * of this function. 544 * 545 * @param string $heading The label for an empty-value option at 546 * the top of the list. 547 * @param boolean $abbrev If true, abbreviate long mailbox names 548 * by replacing the middle of the name with 549 * '...'. 550 * @param array $filter An array of mailboxes to ignore. 551 * @param string $selected The mailbox to have selected by default. 552 * @param boolean $new_folder If true, display an option to create a 553 * new folder. 554 * @param boolean $inc_tasklists Should the user's editable tasklists be 555 * included in the list? 556 * @param boolean $inc_vfolder Should the user's virtual folders be 557 * included in the list? 558 * @param boolean $inc_tasklists Should the user's editable notepads be 559 * included in the list? 560 * 561 * @return string A string containing <option> elements for each mailbox 562 * in the list. 563 */ 564 function flistSelect($heading = '', $abbrev = true, $filter = array(), 565 $selected = null, $new_folder = false, 566 $inc_tasklists = false, $inc_vfolder = false, 567 $inc_notepads = false) 568 { 569 require_once 'Horde/Text.php'; 570 require_once IMP_BASE . '/lib/Folder.php'; 571 572 $imp_folder = &IMP_Folder::singleton(); 573 574 /* Don't filter here - since we are going to parse through every 575 * member of the folder list below anyway, we can filter at that time. 576 * This allows us the have a single cached value for the folder list 577 * rather than a cached value for each different mailbox we may 578 * visit. */ 579 $mailboxes = $imp_folder->flist_IMP(); 580 $text = ''; 581 582 if (strlen($heading) > 0) { 583 $text .= '<option value="">' . $heading . "</option>\n"; 584 } 585 586 if ($new_folder && 587 (!empty($GLOBALS['conf']['hooks']['permsdenied']) || 588 (IMP::hasPermission('create_folders') && 589 IMP::hasPermission('max_folders')))) { 590 $text .= '<option value="">----</option>' . "\n"; 591 $text .= '<option value="*new*">' . _("New Folder") . "</option>\n"; 592 $text .= '<option value="">----</option>' . "\n"; 593 } 594 595 /* Add the list of mailboxes to the lists. */ 596 $filter = array_flip($filter); 597 foreach ($mailboxes as $mbox) { 598 if (isset($filter[$mbox['val']])) { 599 continue; 600 } 601 602 $val = isset($filter[$mbox['val']]) ? '' : htmlspecialchars($mbox['val']); 603 $sel = ($mbox['val'] && ($mbox['val'] === $selected)) ? ' selected="selected"' : ''; 604 $label = ($abbrev) ? $mbox['abbrev'] : $mbox['label']; 605 $text .= sprintf('<option value="%s"%s>%s</option>%s', $val, $sel, Text::htmlSpaces($label), "\n"); 606 } 607 608 /* Add the list of virtual folders to the list. */ 609 if ($inc_vfolder) { 610 $vfolders = $GLOBALS['imp_search']->listQueries(true); 611 if (!empty($vfolders)) { 612 $vfolder_sel = $GLOBALS['imp_search']->searchMboxID(); 613 $text .= '<option value="">----</option>' . "\n"; 614 foreach ($vfolders as $id => $val) { 615 $text .= sprintf('<option value="%s"%s>%s</option>%s', $GLOBALS['imp_search']->createSearchID($id), ($vfolder_sel == $id) ? ' selected="selected"' : '', Text::htmlSpaces($val), "\n"); 616 } 617 } 618 } 619 620 /* Add the list of editable tasklists to the list. */ 621 if ($inc_tasklists && $_SESSION['imp']['tasklistavail']) { 622 $tasklists = $GLOBALS['registry']->call('tasks/listTasklists', 623 array(false, PERMS_EDIT)); 624 625 if (!is_a($tasklists, 'PEAR_Error') && count($tasklists)) { 626 $text .= '<option value="">----</option>' . "\n"; 627 628 foreach ($tasklists as $id => $tasklist) { 629 $text .= sprintf('<option value="%s">%s</option>%s', 630 '_tasklist_' . $id, 631 Text::htmlSpaces($tasklist->get('name')), 632 "\n"); 633 } 634 } 635 } 636 637 /* Add the list of editable notepads to the list. */ 638 if ($inc_notepads && $_SESSION['imp']['notepadavail']) { 639 $notepads = $GLOBALS['registry']->call('notes/listNotepads', 640 array(false, PERMS_EDIT)); 641 642 if (!is_a($notepads, 'PEAR_Error') && count($notepads)) { 643 $text .= '<option value="">----</option>' . "\n"; 644 645 foreach ($notepads as $id => $notepad) { 646 $text .= sprintf('<option value="%s">%s</option>%s', 647 '_notepad_' . $id, 648 Text::htmlSpaces($notepad->get('name')), 649 "\n"); 650 } 651 } 652 } 653 654 return $text; 655 } 656 657 /** 658 * Checks for To:, Subject:, Cc:, and other compose window arguments and 659 * pass back either a URI fragment or an associative array with any of 660 * them which are present. 661 * 662 * @param string $format Either 'uri' or 'array'. 663 * 664 * @return string A URI fragment or an associative array with any compose 665 * arguments present. 666 */ 667 function getComposeArgs() 668 { 669 $args = array(); 670 $fields = array('to', 'cc', 'bcc', 'message', 'subject'); 671 672 foreach ($fields as $val) { 673 if (($$val = Util::getFormData($val))) { 674 $args[$val] = $$val; 675 } 676 } 677 678 /* Decode mailto: URLs. */ 679 if (isset($args['to']) && (strpos($args['to'], 'mailto:') === 0)) { 680 $mailto = @parse_url($args['to']); 681 if (is_array($mailto)) { 682 $args['to'] = $mailto['path']; 683 if (!empty($mailto['query'])) { 684 parse_str($mailto['query'], $vals); 685 foreach ($fields as $val) { 686 if (isset($vals[$val])) { 687 $args[$val] = $vals[$val]; 688 } 689 } 690 } 691 } 692 } 693 694 return $args; 695 } 696 697 /** 698 * Open a compose window. 699 */ 700 function openComposeWin($options = array()) 701 { 702 global $prefs; 703 704 if ($prefs->getValue('compose_popup')) { 705 return true; 706 } else { 707 $options += IMP::getComposeArgs(); 708 $url = Util::addParameter(Horde::applicationUrl('compose.php', true), 709 $options, null, false); 710 header('Location: ' . $url); 711 return false; 712 } 713 } 714 715 /** 716 * Returns the appropriate link to call the message composition screen. 717 * 718 * @param mixed $args List of arguments to pass to compose.php. If this 719 * is passed in as a string, it will be parsed as a 720 * toaddress?subject=foo&cc=ccaddress (mailto-style) 721 * string. 722 * @param array $extra Hash of extra, non-standard arguments to pass to 723 * compose.php. 724 * 725 * @return string The link to the message composition screen. 726 */ 727 function composeLink($args = array(), $extra = array()) 728 { 729 global $prefs, $browser; 730 731 /* Make sure the compose window always knows which mailbox it's in, 732 for replying, forwarding, marking as answered, etc. */ 733 /* $_SESSION['imp'] may not be available here. */ 734 if (!isset($extra['thismailbox']) && 735 isset($_SESSION['imp']['thismailbox'])) { 736 $extra['thismailbox'] = $_SESSION['imp']['thismailbox']; 737 } 738 739 if (is_string($args)) { 740 $string = $args; 741 $args = array(); 742 if (($pos = strpos($string, '?')) !== false) { 743 parse_str(substr($string, $pos + 1), $args); 744 $args['to'] = substr($string, 0, $pos); 745 } else { 746 $args['to'] = $string; 747 } 748 } 749 750 /* Merge the two argument arrays. */ 751 if (is_array($extra)) { 752 $args = array_merge($args, $extra); 753 } 754 755 /* Convert the $args hash into proper URL parameters. */ 756 $url = substr(Util::addParameter('', array_diff($args, array('')), null, false), 1); 757 758 if ($prefs->getValue('compose_popup') && 759 $browser->hasFeature('javascript')) { 760 Horde::addScriptFile('popup.js'); 761 return "javascript:popup_imp('" . Horde::applicationUrl('compose.php') . "',800,650,'" . htmlspecialchars($browser->escapeJSCode(addslashes($url))) . "');"; 762 } else { 763 return Horde::applicationUrl(empty($url) ? 'compose.php' : 'compose.php?' . $url); 764 } 765 } 766 767 /** 768 * Generates an URL to the logout screen that includes any known 769 * information, such as username, server, etc., that can be filled in on 770 * the login form. 771 * 772 * @return string Logout URL with logout parameters added. 773 */ 774 function logoutUrl() 775 { 776 $params = array( 777 'imapuser' => isset($_SESSION['imp']['user']) ? 778 $_SESSION['imp']['user'] : 779 Util::getFormData('imapuser'), 780 'server' => isset($_SESSION['imp']['server']) ? 781 $_SESSION['imp']['server'] : 782 Util::getFormData('server'), 783 'port' => isset($_SESSION['imp']['port']) ? 784 $_SESSION['imp']['port'] : 785 Util::getFormData('port'), 786 'protocol' => isset($_SESSION['imp']['protocol']) ? 787 $_SESSION['imp']['protocol'] : 788 Util::getFormData('protocol'), 789 'language' => isset($_SESSION['imp']['language']) ? 790 $_SESSION['imp']['language'] : 791 Util::getFormData('language'), 792 'smtphost' => isset($_SESSION['imp']['smtphost']) ? 793 $_SESSION['imp']['smtphost'] : 794 Util::getFormData('smtphost'), 795 'smtpport' => isset($_SESSION['imp']['smtpport']) ? 796 $_SESSION['imp']['smtpport'] : 797 Util::getFormData('smtpport'), 798 ); 799 800 $url = Util::addParameter('login.php', array_diff($params, array('')), null, false); 801 return Horde::applicationUrl($url, true); 802 } 803 804 /** 805 * If there is information available to tell us about a prefix in front of 806 * mailbox names that shouldn't be displayed to the user, then use it to 807 * strip that prefix out. 808 * 809 * @param string $folder The folder name to display. 810 * 811 * @return string The folder, with any prefix gone. 812 */ 813 function displayFolder($folder) 814 { 815 static $cache = array(); 816 817 if (isset($cache[$folder])) { 818 return $cache[$folder]; 819 } 820 821 if ($folder == 'INBOX') { 822 $cache[$folder] = _("Inbox"); 823 } else { 824 $namespace_info = IMP::getNamespace($folder); 825 if (!is_null($namespace_info) && 826 !empty($namespace_info['name']) && 827 ($namespace_info['type'] == 'personal') && 828 substr($folder, 0, strlen($namespace_info['name'])) == $namespace_info['name']) { 829 $cache[$folder] = substr($folder, strlen($namespace_info['name'])); 830 } else { 831 $cache[$folder] = $folder; 832 } 833 834 $cache[$folder] = String::convertCharset($cache[$folder], 'UTF7-IMAP'); 835 } 836 837 return $cache[$folder]; 838 } 839 840 /** 841 * Filters a string, if requested. 842 * 843 * @param string $text The text to filter. 844 * 845 * @return string The filtered text (if requested). 846 */ 847 function filterText($text) 848 { 849 global $conf, $prefs; 850 851 if ($prefs->getValue('filtering')) { 852 require_once 'Horde/Text/Filter.php'; 853 $text = Text_Filter::filter($text, 'words', array('words_file' => $conf['msgsettings']['filtering']['words'], 'replacement' => $conf['msgsettings']['filtering']['replacement'])); 854 } 855 856 return $text; 857 } 858 859 /** 860 * Returns the specified permission for the current user. 861 * 862 * @since IMP 4.1 863 * 864 * @param string $permission A permission, either 'create_folders' or 865 * 'max_folders'. 866 * @param boolean $value If true, the method returns the value of a 867 * scalar permission, otherwise whether the 868 * permission limit has been hit already. 869 * 870 * @return mixed The value of the specified permission. 871 */ 872 function hasPermission($permission, $value = false) 873 { 874 global $perms; 875 876 if (!$perms->exists('imp:' . $permission)) { 877 return true; 878 } 879 880 $allowed = $perms->getPermissions('imp:' . $permission); 881 if (is_array($allowed)) { 882 switch ($permission) { 883 case 'create_folders': 884 $allowed = array_reduce($allowed, create_function('$a, $b', 'return $a | $b;'), false); 885 break; 886 case 'max_folders': 887 $allowed = array_reduce($allowed, create_function('$a, $b', 'return max($a, $b);'), 0); 888 break; 889 } 890 } 891 if ($permission == 'max_folders' && !$value) { 892 $folder = &IMP_Folder::singleton(); 893 $allowed = $allowed > count($folder->flist_IMP(array(), false)); 894 } 895 896 return $allowed; 897 } 898 899 /** 900 * Build IMP's list of menu items. 901 */ 902 function getMenu($returnType = 'object') 903 { 904 global $conf, $prefs, $registry; 905 906 require_once 'Horde/Menu.php'; 907 908 $menu_search_url = Horde::applicationUrl('search.php'); 909 $menu_mailbox_url = Horde::applicationUrl('mailbox.php'); 910 911 $spam_folder = IMP::folderPref($prefs->getValue('spam_folder'), true); 912 913 $menu = new Menu(HORDE_MENU_MASK_ALL & ~HORDE_MENU_MASK_LOGIN); 914 915 $menu->add(Util::addParameter($menu_mailbox_url, 'mailbox', 'INBOX'), _("_Inbox"), 'folders/inbox.png'); 916 if (($_SESSION['imp']['base_protocol'] != 'pop3') && 917 ($prefs->getValue('use_trash') || $prefs->getValue('use_vtrash')) && 918 $prefs->getValue('empty_trash_menu')) { 919 $mailbox = null; 920 if ($prefs->getValue('use_vtrash')) { 921 $mailbox = $GLOBALS['imp_search']->createSearchID($prefs->getValue('vtrash_id')); 922 } else { 923 $trash_folder = IMP::folderPref($prefs->getValue('trash_folder'), true); 924 if (!is_null($trash_folder)) { 925 $mailbox = $trash_folder; 926 } 927 } 928 929 if (!empty($mailbox)) { 930 $menu_trash_url = Util::addParameter($menu_mailbox_url, 931 array('mailbox' => $mailbox, 932 'actionID' => 'empty_mailbox')); 933 $menu->add($menu_trash_url, _("Empty _Trash"), 'empty_trash.png', null, null, "return window.confirm('" . addslashes(_("Are you sure you wish to empty your trash folder?")) . "');", '__noselection'); 934 } 935 } 936 937 if (($_SESSION['imp']['base_protocol'] != 'pop3') && 938 !empty($spam_folder) && 939 $prefs->getValue('empty_spam_menu')) { 940 $menu_spam_url = Util::addParameter($menu_mailbox_url, array('mailbox' => $spam_folder, 941 'actionID' => 'empty_mailbox')); 942 $menu->add($menu_spam_url, _("Empty _Spam"), 'empty_spam.png', null, null, "return window.confirm('" . addslashes(_("Are you sure you wish to empty your spam folder?")) . "');", '__noselection'); 943 } 944 945 $menu->add(IMP::composeLink(), _("_New Message"), 'compose.png'); 946 947 if ($conf['user']['allow_folders']) { 948 $menu->add(Util::nocacheUrl(Horde::applicationUrl('folders.php')), _("_Folders"), 'folders/folder.png'); 949 } 950 $menu->add($menu_search_url, _("_Search"), 'search.png', $registry->getImageDir('horde')); 951 if (($_SESSION['imp']['base_protocol'] != 'pop3') && $prefs->getValue('fetchmail_menu')) { 952 if ($prefs->getValue('fetchmail_popup')) { 953 $menu->add(Horde::applicationUrl('fetchmail.php'), _("F_etch Mail"), 'fetchmail.png', null, 'fetchmail', 'window.open(this.href, \'fetchmail\', \'toolbar=no,location=no,status=yes,scrollbars=yes,resizable=yes,width=300,height=450,left=100,top=100\'); return false;'); 954 } else { 955 $menu->add(Horde::applicationUrl('fetchmail.php'), _("F_etch Mail"), 'fetchmail.png'); 956 } 957 } 958 if ($prefs->getValue('filter_menuitem')) { 959 $menu->add(Horde::applicationUrl('filterprefs.php'), _("Fi_lters"), 'filters.png'); 960 } 961 962 /* Logout. If IMP can auto login or IMP is providing 963 * authentication, then we only show the logout link if the 964 * sidebar isn't shown or if the configuration says to always 965 * show the current user a logout link. */ 966 $impAuth = Auth::getProvider() == 'imp'; 967 $impAutoLogin = IMP::canAutoLogin(); 968 if (!($impAuth || $impAutoLogin) || 969 !$prefs->getValue('show_sidebar') || 970 Horde::showService('logout')) { 971 972 /* If IMP provides authentication and the sidebar isn't 973 * always on, target the main frame for logout to hide the 974 * sidebar while logged out. */ 975 $logout_target = null; 976 if ($impAuth || $impAutoLogin) { 977 $logout_target = '_parent'; 978 } 979 980 /* If IMP doesn't provide Horde authentication then we 981 * need to use IMP's logout screen since logging out 982 * should *not* end a Horde session. */ 983 if ($impAuth || $impAutoLogin) { 984 $logout_url = Horde::getServiceLink('logout', 'horde', true); 985 } else { 986 $logout_url = Auth::addLogoutParameters(Horde::applicationUrl('login.php'), AUTH_REASON_LOGOUT); 987 } 988 989 $id = $menu->add($logout_url, _("_Log out"), 'logout.png', $registry->getImageDir('horde'), $logout_target); 990 $menu->setPosition($id, HORDE_MENU_POS_LAST); 991 } 992 993 if ($returnType == 'object') { 994 return $menu; 995 } else { 996 return $menu->render(); 997 } 998 } 999 1000 /** 1001 * Outputs IMP's status/notification bar. 1002 */ 1003 function status() 1004 { 1005 global $notification; 1006 1007 if (isset($_SESSION['imp']['stream'])) { 1008 $alerts = imap_alerts(); 1009 if (is_array($alerts)) { 1010 $alerts = str_replace('[ALERT] ', '', $alerts); 1011 foreach ($alerts as $alert) { 1012 $notification->push($alert, 'horde.warning'); 1013 } 1014 } 1015 } 1016 1017 /* BC check. */ 1018 if (class_exists('Notification_Listener_audio')) { 1019 $notification->notify(array('listeners' => array('status', 'audio'))); 1020 } 1021 } 1022 1023 /** 1024 * Returns the javascript for a new message notification popup. 1025 * 1026 * @param array $newmsgs Associative array with mailbox names as the keys 1027 * and the message count as the values 1028 * 1029 * @return string The javascript for the popup message 1030 */ 1031 function getNewMessagePopup($newmsgs) 1032 { 1033 $_alert = ''; 1034 $count = 0; 1035 foreach ($newmsgs as $mb => $nm) { 1036 $count++; 1037 $_mailbox_message = $mb; 1038 $_alert .= IMP::displayFolder($_mailbox_message) . 1039 ($nm > 1 ? _(" - $nm new messages") : _(" - $nm new message")) . '\n'; 1040 } 1041 if (!empty($_alert)) { 1042 if ($count == 1) { 1043 $mailboxOpenUrl = Horde::applicationUrl('mailbox.php', true); 1044 $mailboxOpenUrl = Util::addParameter($mailboxOpenUrl, array('no_newmail_popup' => 1, 'mailbox' => $_mailbox_message)); 1045 1046 return "if (confirm('" . addslashes(_("You have new mail in the following folder:")) . '\n' . 1047 $_alert . addslashes(_("Do you want to open that folder?")) . 1048 "')) { window.location.href = '" . str_replace('&', '&', $mailboxOpenUrl) . 1049 "'; window.focus(); }"; 1050 } else { 1051 return "alert('" . addslashes(_("You have new mail in the following folders:")) . 1052 '\n' . $_alert ."');"; 1053 } 1054 } 1055 } 1056 1057 /** 1058 * Generates the URL to the prefs page. 1059 * 1060 * @param boolean $full Generate full URL? 1061 * 1062 * @return string The URL to the IMP prefs page. 1063 */ 1064 function prefsURL($full = false) 1065 { 1066 return Horde::url($GLOBALS['registry']->get('webroot', 'horde') . '/services/prefs.php?app=imp', $full); 1067 } 1068 1069 /** 1070 * Are we currently in "print" mode? 1071 * 1072 * @param boolean $mode True if in print mode, false if not. 1073 * 1074 * @return boolean Returns true if in "print" mode. 1075 */ 1076 function printMode($mode = null) 1077 { 1078 static $print = false; 1079 if (!is_null($mode)) { 1080 $print = $mode; 1081 } 1082 return $print; 1083 } 1084 1085 /** 1086 * Get message indices list. 1087 * 1088 * @param mixed $indices The following inputs are allowed: 1089 * <pre> 1090 * 1. An array of messages indices in the following format: 1091 * msg_id IMP_IDX_SEP msg_folder 1092 * msg_id = Message index of the message 1093 * IMP_IDX_SEP = IMP constant used to separate index/folder 1094 * msg_folder = The full folder name containing the message index 1095 * 2. An array with the full folder name as keys and an array of message 1096 * indices as the values. 1097 * 3. An IMP_Mailbox object, which will use the current index/folder 1098 * as determined by the object. If an IMP_Mailbox object is used, it 1099 * will be updated after the action is performed. 1100 * </pre> 1101 * 1102 * @return mixed Returns an array with the folder as key and an array 1103 * of message indices as the value (See #2 above). 1104 * Else, returns false. 1105 */ 1106 function parseIndicesList($indices) 1107 { 1108 $msgList = array(); 1109 1110 if (is_a($indices, 'IMP_Mailbox')) { 1111 $msgIdx = $indices->getIMAPIndex(); 1112 if (empty($msgIdx)) { 1113 return false; 1114 } 1115 $msgList[$msgIdx['mailbox']][] = $msgIdx['index']; 1116 return $msgList; 1117 } 1118 1119 if (!is_array($indices)) { 1120 return false; 1121 } 1122 if (!count($indices)) { 1123 return array(); 1124 } 1125 1126 reset($indices); 1127 if (!is_array(current($indices))) { 1128 /* Build the list of indices/mailboxes to delete if input 1129 is of format #1. */ 1130 foreach ($indices as $msgIndex) { 1131 if (strpos($msgIndex, IMP_IDX_SEP) === false) { 1132 return false; 1133 } else { 1134 list($val, $key) = explode(IMP_IDX_SEP, $msgIndex); 1135 $msgList[$key][] = $val; 1136 } 1137 } 1138 } else { 1139 /* We are dealing with format #2. */ 1140 foreach ($indices as $key => $val) { 1141 if ($GLOBALS['imp_search']->isSearchMbox($key)) { 1142 $msgList += IMP::parseIndicesList($val); 1143 } else { 1144 /* Make sure we don't have any duplicate keys. */ 1145 $msgList[$key] = is_array($val) ? array_unique($val) : array($val); 1146 } 1147 } 1148 } 1149 1150 return $msgList; 1151 } 1152 1153 /** 1154 * Either sets or checks the value of the logintasks flag. 1155 * 1156 * @param integer $set The value of the flag. 1157 * 1158 * @return integer The value of the flag. 1159 * 0 = No login tasks pending 1160 * 1 = Login tasks pending 1161 * 2 = Login tasks pending, previous tasks interrupted 1162 */ 1163 function loginTasksFlag($set = null) 1164 { 1165 if (!is_null($set)) { 1166 $_SESSION['imp']['_logintasks'] = $set; 1167 } 1168 1169 return isset($_SESSION['imp']['_logintasks']) ? $_SESSION['imp']['_logintasks'] : 0; 1170 } 1171 1172 /** 1173 * Get namespace info for a full folder path. 1174 * 1175 * @since IMP 4.1 1176 * 1177 * @param string $mailbox The folder path. If empty, will return info 1178 * on the default namespace (i.e. the first 1179 * personal namespace). 1180 * @param boolean $empty If true and no matching namespace is found, 1181 * return the empty namespace, if it exists. 1182 * 1183 * @return mixed The namespace info for the folder path or null if the 1184 * path doesn't exist. 1185 */ 1186 function getNamespace($mailbox = null, $empty = true) 1187 { 1188 static $cache = array(); 1189 1190 if ($_SESSION['imp']['base_protocol'] == 'pop3') { 1191 return null; 1192 } 1193 1194 if ($mailbox === null) { 1195 reset($_SESSION['imp']['namespace']); 1196 $mailbox = key($_SESSION['imp']['namespace']); 1197 } 1198 1199 $key = (int)$empty; 1200 if (isset($cache[$key][$mailbox])) { 1201 return $cache[$key][$mailbox]; 1202 } 1203 1204 foreach ($_SESSION['imp']['namespace'] as $key => $val) { 1205 if (!empty($key) && (strpos($mailbox, $key) === 0)) { 1206 $cache[$key][$mailbox] = $val; 1207 return $val; 1208 } 1209 } 1210 1211 if ($empty && isset($_SESSION['imp']['namespace'][''])) { 1212 $cache[$key][$mailbox] = $_SESSION['imp']['namespace']['']; 1213 } else { 1214 $cache[$key][$mailbox] = null; 1215 } 1216 1217 return $cache[$key][$mailbox]; 1218 } 1219 1220 /** 1221 * Get the default personal namespace. 1222 * 1223 * @since IMP 4.1 1224 * 1225 * @return mixed The default personal namespace info. 1226 */ 1227 function defaultNamespace() 1228 { 1229 static $default = null; 1230 1231 if ($_SESSION['imp']['base_protocol'] == 'pop3') { 1232 return null; 1233 } 1234 1235 if (!$default) { 1236 foreach ($_SESSION['imp']['namespace'] as $val) { 1237 if ($val['type'] == 'personal') { 1238 $default = $val; 1239 break; 1240 } 1241 } 1242 } 1243 1244 return $default; 1245 } 1246 1247 /** 1248 * Convert a preference value to/from the value stored in the preferences. 1249 * 1250 * Preferences that need to call this function before storing/retrieving: 1251 * trash_folder, spam_folder, drafts_folder, sent_mail_folder 1252 * To allow folders from the personal namespace to be stored without this 1253 * prefix for portability, we strip the personal namespace. To tell apart 1254 * folders from the personal and any empty namespace, we prefix folders 1255 * from the empty namespace with the delimiter. 1256 * 1257 * @since IMP 4.1 1258 * 1259 * @param string $mailbox The folder path. 1260 * @param boolean $append True - convert from preference value. 1261 * False - convert to preference value. 1262 * 1263 * @return string The folder name. 1264 */ 1265 function folderPref($folder, $append) 1266 { 1267 $def_ns = IMP::defaultNamespace(); 1268 $empty_ns = IMP::getNamespace(''); 1269 if ($append) { 1270 /* Converting from preference value. */ 1271 if (!is_null($empty_ns) && 1272 strpos($folder, $empty_ns['delimiter']) === 0) { 1273 /* Prefixed with delimiter => from empty namespace. */ 1274 $folder = substr($folder, strlen($empty_ns['delimiter'])); 1275 } elseif (is_null($ns = IMP::getNamespace($folder, false))) { 1276 /* No namespace prefix => from personal namespace. */ 1277 $folder = $def_ns['name'] . $folder; 1278 } 1279 } elseif (!$append && !is_null($ns = IMP::getNamespace($folder))) { 1280 /* Converting to preference value. */ 1281 if ($ns['name'] == $def_ns['name']) { 1282 /* From personal namespace => strip namespace. */ 1283 $folder = substr($folder, strlen($def_ns['name'])); 1284 } elseif ($ns['name'] == $empty_ns['name']) { 1285 /* From empty namespace => prefix with delimiter. */ 1286 $folder = $empty_ns['delimiter'] . $folder; 1287 } 1288 } 1289 return $folder; 1290 } 1291 1292 /** 1293 * Make sure a user-entered mailbox contains namespace information. 1294 * 1295 * @since IMP 4.1 1296 * 1297 * @param string $mbox The user-entered mailbox string. 1298 * 1299 * @return string The mailbox string with any necessary namespace info 1300 * added. 1301 */ 1302 function appendNamespace($mbox) 1303 { 1304 $ns_info = IMP::getNamespace($mbox, false); 1305 if (is_null($ns_info)) { 1306 $ns_info = IMP::defaultNamespace(); 1307 } 1308 return $ns_info['name'] . $mbox; 1309 } 1310 1311 /** 1312 * Generates a URL with any necessary information required for handling a 1313 * search mailbox added to the parameters. 1314 * 1315 * @since IMP 4.1 1316 * 1317 * @param string $page Page name to link to. 1318 * @param string $mailbox The mailbox to use on the linked page. 1319 * @param string $index The index to use on the linked page. 1320 * 1321 * @return string URL to $page with any necessary search information 1322 * added to the parameter list of the URL. 1323 */ 1324 function generateSearchUrl($page, $mailbox, $index = null) 1325 { 1326 $link = Horde::applicationUrl($page); 1327 1328 foreach (IMP::getSearchParameters($mailbox, $index) as $key => $val) { 1329 $link = Util::addParameter($link, $key, $val); 1330 } 1331 1332 return $link; 1333 } 1334 1335 /** 1336 * Returns a list of parameters necessary for handling a search mailbox. 1337 * 1338 * @since IMP 4.1 1339 * 1340 * @param string $mailbox The mailbox to use on the linked page. 1341 * @param string $index The index to use on the linked page. 1342 * 1343 * @return array The list of parameters needed for handling a search 1344 * mailbox (may be empty if not currently in a search 1345 * mailbox). 1346 */ 1347 function getSearchParameters($mailbox, $index = null) 1348 { 1349 $params = array(); 1350 1351 if ($GLOBALS['imp_search']->searchMboxID()) { 1352 $params['thismailbox'] = $mailbox; 1353 $params['mailbox'] = $GLOBALS['imp']['mailbox']; 1354 } 1355 if (!is_null($index)) { 1356 $params['index'] = $index; 1357 } 1358 1359 return $params; 1360 } 1361 1362 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 12:30:07 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |