[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/MIME/ -> Contents.php (source)

   1  <?php
   2  
   3  require_once dirname(__FILE__) . '/Message.php';
   4  
   5  /**
   6   * The name of the URL parameter that holds the MIME_Contents cache
   7   * identifier.
   8   */
   9  define('MIME_CONTENTS_CACHE', 'mimecache');
  10  
  11  /**
  12   * Display attachment information in list format.
  13   */
  14  define('MIME_CONTENTS_DISPLAY_TYPE_LIST', 0);
  15  
  16  /**
  17   * Display attachment information inline with attachment.
  18   */
  19  define('MIME_CONTENTS_DISPLAY_TYPE_INLINE', 1);
  20  
  21  /**
  22   * Display attachment information both in list format and inline with
  23   * attachment.
  24   */
  25  define('MIME_CONTENTS_DISPLAY_TYPE_BOTH', 2);
  26  
  27  /**
  28   * The MIME_Contents:: class contains functions related to handling the output
  29   * of MIME content.
  30   *
  31   * $Horde: framework/MIME/MIME/Contents.php,v 1.129.4.35 2006/05/05 03:30:22 slusarz Exp $
  32   *
  33   * Copyright 2002-2006 Michael Slusarz <slusarz@bigworm.colorado.edu>
  34   *
  35   * See the enclosed file COPYING for license information (GPL). If you did not
  36   * receive this file, see http://www.fsf.org/copyleft/gpl.html.
  37   *
  38   * @author  Michael Slusarz <slusarz@bigworm.colorado.edu>
  39   * @since   Horde 3.0
  40   * @package Horde_MIME
  41   */
  42  class MIME_Contents {
  43  
  44      /**
  45       * The MIME_Message object for the message.
  46       *
  47       * @var MIME_Message
  48       */
  49      var $_message;
  50  
  51      /**
  52       * The MIME_Message object we use when caching.
  53       *
  54       * @var MIME_Message
  55       */
  56      var $_cachemessage;
  57  
  58      /**
  59       * The attachments list.
  60       *
  61       * @var array
  62       */
  63      var $_atc = array();
  64  
  65      /**
  66       * The message parts list.
  67       *
  68       * @var array
  69       */
  70      var $_parts = array();
  71  
  72      /**
  73       * The summary parts list.
  74       *
  75       * @var array
  76       */
  77      var $_summary = array();
  78  
  79      /**
  80       * The summary type.
  81       *
  82       * @var string
  83       */
  84      var $_summaryType = null;
  85  
  86      /**
  87       * The Cache_session identifier.
  88       *
  89       * @var string
  90       */
  91      var $_sessionCacheID = null;
  92  
  93      /**
  94       * The MIME_Viewer object cache.
  95       *
  96       * @var array
  97       */
  98      var $_viewerCache = array();
  99  
 100      /**
 101       * The attachment display type to use.
 102       *
 103       * @var integer
 104       */
 105      var $_displayType = MIME_CONTENTS_DISPLAY_TYPE_BOTH;
 106  
 107      /**
 108       * The MIME index key to use.
 109       *
 110       * @var string
 111       */
 112      var $_mimekey = null;
 113  
 114      /**
 115       * The actionID value for various actions.
 116       * 'download'  --  Downloading a part/attachment.
 117       * 'view'      --  Viewing a part/attachment.
 118       *
 119       * @var array
 120       */
 121      var $_viewID = array();
 122  
 123      /**
 124       * Show the links in the summaries?
 125       *
 126       * @var boolean
 127       */
 128      var $_links = true;
 129  
 130      /**
 131       * The base MIME_Contents object.
 132       *
 133       * @var MIME_Contents
 134       */
 135      var $_base = null;
 136  
 137      /**
 138       * The number of message/rfc822 levels labeled as 'attachments' of the
 139       * current part.
 140       *
 141       * @var integer
 142       */
 143      var $_attach822 = 0;
 144  
 145      /**
 146       * Constructor.
 147       *
 148       * @param MIME_Message $messageOb  The object to work with.
 149       * @param array $viewID            The actionID values for viewing
 150       *                                 parts/attachments.
 151       * @param array &$contents         Array containing a single value:
 152       *                                 a reference to the base object.
 153       * (This last parameter needs to be handled via an array because PHP <
 154       *  5.0 doesn't handle optional pointer parameters otherwise.)
 155       */
 156      function MIME_Contents($messageOb, $viewID = array(), $contents = array())
 157      {
 158          $this->_message = $messageOb;
 159          $this->_cachemessage = Util::cloneObject($messageOb);
 160          $this->_viewID = $viewID;
 161  
 162          /* Create the pointer to the base object. */
 163          if (!empty($contents)) {
 164              $ptr = reset($contents);
 165              $old_ptr = &$ptr->getBaseObjectPtr();
 166              $this->_base = $old_ptr;
 167          }
 168      }
 169  
 170      /**
 171       * Returns the entire body of the message.
 172       * You probably want to override this function in any subclass.
 173       *
 174       * @return string  The text of the body of the message.
 175       */
 176      function getBody()
 177      {
 178          return $this->_message->toString();
 179      }
 180  
 181      /**
 182       * Returns the raw text for one section of the message.
 183       * You probably want to override this function in any subclass.
 184       *
 185       * @param string $id  The ID of the MIME_Part.
 186       *
 187       * @return string  The text of the part.
 188       */
 189      function getBodyPart($id)
 190      {
 191          if (($part = &$this->getMIMEPart($id))) {
 192              return $part->getContents();
 193          } else {
 194              return '';
 195          }
 196      }
 197  
 198      /**
 199       * Returns the MIME_Message object for the mail message.
 200       *
 201       * @return MIME_Message  A MIME_Message object.
 202       */
 203      function getMIMEMessage()
 204      {
 205          return $this->_message;
 206      }
 207  
 208      /**
 209       * Fetch a part of a MIME message.
 210       *
 211       * @param integer $id  The MIME index of the part requested.
 212       *
 213       * @return MIME_Part  The raw MIME part asked for.
 214       */
 215      function &getMIMEPart($id)
 216      {
 217          $part = $this->_message->getPart($id);
 218          return $part;
 219      }
 220  
 221      /**
 222       * Rebuild the MIME_Part structure of a message.
 223       * You probably want to override this function in any subclass.
 224       *
 225       * @return MIME_Message  A MIME_Message object with all of the body text
 226       *                       stored in the individual MIME_Parts.
 227       */
 228      function rebuildMessage()
 229      {
 230          return $this->_message;
 231      }
 232  
 233      /**
 234       * Fetch part of a MIME message.
 235       *
 236       * @param integer $id   The MIME ID of the part requested.
 237       * @param boolean $all  If this is a header part, should we return all text
 238       *                      in the body?
 239       *
 240       * @return MIME_Part  The MIME_Part.
 241       */
 242      function &getRawMIMEPart($id, $all = false)
 243      {
 244          $mime_part = &$this->getMIMEPart($id);
 245          if (!is_a($mime_part, 'MIME_Part')) {
 246              $mime_part = null;
 247              return $mime_part;
 248          }
 249  
 250          /* If all text is requested, change the ID now. */
 251          if ($all && $mime_part->getInformation('header')) {
 252              $id = substr($id, 0, strrpos($id, '.'));
 253          }
 254  
 255          /* Only set contents if there is currently none in the MIME Part. */
 256          if (!$mime_part->getContents()) {
 257              $mime_part->setContents($this->getBodyPart($id));
 258          }
 259  
 260          return $mime_part;
 261      }
 262  
 263      /**
 264       * Fetch part of a MIME message and decode it, if it is base_64 or
 265       * qprint encoded.
 266       *
 267       * @param integer $id   The MIME ID of the part requested.
 268       * @param boolean $all  If this is a header part, should we return all text
 269       *                      in the body?
 270       *
 271       * @return MIME_Part  The MIME_Part with its contents decoded.
 272       */
 273      function &getDecodedMIMEPart($id, $all = false)
 274      {
 275          if (($mime_part = &$this->getRawMIMEPart($id, $all))) {
 276              $mime_part->transferDecodeContents();
 277              // Noop.
 278          } else {
 279              $mime_part = null;
 280          }
 281          return $mime_part;
 282      }
 283  
 284      /**
 285       * Return the attachment list (HTML table format).
 286       *
 287       * @return string  The list of attachments formatted into HTML.
 288       */
 289      function getAttachments()
 290      {
 291          $msg = '';
 292  
 293          $akeys = array_keys($this->_atc);
 294          natsort($akeys);
 295          foreach ($akeys as $key) {
 296              $msg .= $this->_arrayToTableRow($this->_atc[$key]);
 297          }
 298  
 299          return $msg;
 300      }
 301  
 302      /**
 303       * Return the message list (HTML table format).
 304       *
 305       * @param boolean $oneframe  Should the output be designed for display in a
 306       *                           single frame?
 307       *
 308       * @return string  The message formatted into HTML.
 309       */
 310      function getMessage($oneframe = false)
 311      {
 312          $msg = '';
 313          $msgCount = count($this->_parts);
 314          $partDisplayed = false;
 315  
 316          if ($oneframe) {
 317              $msg .= '<style type="text/css">.onefr {padding:1px; margin-bottom:1px; border:1px dashed #00c; background:#eef; color:#006}</style>' . "\n";
 318          }
 319  
 320          // TODO: Temporary hack to display header info for a message with one
 321          // MIME part that cannot be displayed inline.
 322          if (!$msgCount || ($msgCount == 1 && !reset($this->_parts))) {
 323              $this->setSummary($this->_message, 'part');
 324              $msgCount = 1;
 325          }
 326              
 327          foreach ($this->_parts as $key => $value) {
 328              if (isset($this->_summary[$key]) &&
 329                  ((($msgCount == 1) && empty($value)) || ($msgCount > 1))) {
 330                  $msg .= '<tr><td>';
 331                  if ($oneframe) {
 332                      $msg .= '<table class="onefr">';
 333                      $summary = $this->_summary[$key];
 334                      $summary = array_merge(array_splice($summary, 0, 1), array_splice($summary, 1));
 335                      $msg .= $this->_arrayToTableRow($summary);
 336                  } else {
 337                      $msg .= '<table>';
 338                      $msg .= $this->_arrayToTableRow($this->_summary[$key]);
 339                  }
 340                  $msg .= '</table></td></tr>';
 341              }
 342              if (!empty($value)) {
 343                  $msg .= '<tr><td class="text">' . $value . '</td></tr>';
 344                  $partDisplayed = true;
 345              }
 346          }
 347  
 348          if (!$partDisplayed) {
 349              $msg .= '<tr><td class="text" align="left"><em>' . _("There are no parts that can be displayed inline.") . '</em></td></tr>';
 350          }
 351  
 352          return $msg;
 353      }
 354  
 355      /**
 356       * Expands an array into a table row.
 357       *
 358       * @access private
 359       *
 360       * @param array $array  The array to expand.
 361       *
 362       * @return string  The array expanded to a HTML table row.
 363       */
 364      function _arrayToTableRow($array)
 365      {
 366          $text = '<tr valign="middle">';
 367  
 368          foreach ($array as $elem) {
 369              if (!empty($elem)) {
 370                  $text .= "<td>$elem&nbsp;</td>\n";
 371              }
 372          }
 373  
 374          return $text . "</tr>\n";
 375      }
 376  
 377      /**
 378       * Returns the data for a specific MIME index.
 379       *
 380       * @param string $id     The MIME index.
 381       * @param string $field  The field to return (message, atc, summary)
 382       *
 383       * @return string  The text currently set for that index.
 384       */
 385      function getIndex($id, $field)
 386      {
 387          $field = '_' . $field;
 388          if (is_array($this->$field) && array_key_exists($id, $this->$field)) {
 389              $entry = $this->$field;
 390              return $entry[$id];
 391          } else {
 392              return null;
 393          }
 394      }
 395  
 396      /**
 397       * Removes the message text and summary for a specific MIME index.
 398       *
 399       * @param string $id  The MIME index.
 400       */
 401      function removeIndex($id)
 402      {
 403          unset($this->_parts[$id]);
 404          unset($this->_summary[$id]);
 405          unset($this->_atc[$id]);
 406      }
 407  
 408      /**
 409       * Determine if we can (and know how to) inline a MIME Part.
 410       *
 411       * @param MIME_Part &$mime_part  A MIME_Part object.
 412       *
 413       * @return boolean  True if part can be inlined.
 414       *                  False if it cannot.
 415       */
 416      function canDisplayInline(&$mime_part)
 417      {
 418          $viewer = &$this->getMIMEViewer($mime_part);
 419  
 420          /* First check: The MIME headers allow the part to be inlined.
 421           * However, if we are already in view mode, then we can skip this
 422           * check. */
 423          if (!$this->viewAsAttachment() &&
 424              ($mime_part->getDisposition() != 'inline') &&
 425              !$viewer->forceInlineView()) {
 426              return false;
 427          }
 428  
 429          /* Second check (save the most expensive for last):
 430           * Check to see if the driver is set to inline. */
 431          return (is_a($viewer, 'MIME_Viewer') && $viewer->canDisplayInline());
 432      }
 433  
 434      /**
 435       * Get MIME_Viewer object.
 436       *
 437       * @param MIME_Part &$mime_part  A MIME_Part object.
 438       *
 439       * @return MIME_Viewer  The MIME_Viewer object, or false on error.
 440       */
 441      function &getMIMEViewer(&$mime_part)
 442      {
 443          /* Make sure we have a MIME_Part to process. */
 444          if (empty($mime_part)) {
 445              return false;
 446          }
 447  
 448          require_once dirname(__FILE__) . '/Viewer.php';
 449  
 450          $key = $mime_part->getUniqueID() . '|' . $mime_part->getType();
 451          if (!isset($this->_viewerCache[$key])) {
 452              $this->_viewerCache[$key] = &MIME_Viewer::factory($mime_part);
 453          }
 454  
 455          return $this->_viewerCache[$key];
 456      }
 457  
 458      /**
 459       * Get the MIME Content-Type output by a MIME_Viewer for a particular
 460       * MIME_Part.
 461       *
 462       * @param MIME_Part &$mime_part  A MIME_Part object.
 463       *
 464       * @return string  The MIME type output by the MIME_Viewer, or false on
 465       *                 error.
 466       */
 467      function getMIMEViewerType(&$mime_part)
 468      {
 469          if (($viewer = &$this->getMIMEViewer($mime_part))) {
 470              return $viewer->getType();
 471          } else {
 472              return false;
 473          }
 474      }
 475  
 476      /**
 477       * Returns the key to use for a particular MIME_Part.
 478       *
 479       * @access private
 480       *
 481       * @param MIME_Part &$mime_part  A MIME_Part object.
 482       * @param boolean $override      Respect the MIME key override value?
 483       *
 484       * @return string  The unique identifier of the MIME_Part.
 485       *                 Returns false if no key found.
 486       */
 487      function _getMIMEKey(&$mime_part, $override = true)
 488      {
 489          $id = $this->getMIMEKeyOverride();
 490  
 491          if ($override && !is_null($id)) {
 492              return $id;
 493          } else {
 494              $id = $mime_part->getMIMEId();
 495              if (is_null($id)) {
 496                  return false;
 497              } else {
 498                  return $id;
 499              }
 500          }
 501      }
 502  
 503      /**
 504       * Gets the MIME key override.
 505       *
 506       * @return string  The MIME key override - null if no override.
 507       */
 508      function getMIMEKeyOverride()
 509      {
 510          return $this->_mimekey;
 511      }
 512  
 513      /**
 514       * Sets an override for the MIME key.
 515       *
 516       * @param string $mimekey
 517       */
 518      function setMIMEKeyOverride($mimekey = null)
 519      {
 520          $this->_mimekey = $mimekey;
 521      }
 522  
 523      /**
 524       * Should we display links for the summaries?
 525       *
 526       * @param boolean $show  Show the summary links?
 527       */
 528      function showSummaryLinks($show = null)
 529      {
 530          if (!is_null($show)) {
 531              $this->_links = $show;
 532          }
 533  
 534          return $this->_links;
 535      }
 536  
 537      /**
 538       * Render a MIME Part.
 539       *
 540       * @param MIME_Part &$mime_part  A MIME_Part object.
 541       *
 542       * @return string  The rendered data.
 543       */
 544      function renderMIMEPart(&$mime_part)
 545      {
 546          return $this->_renderMIMEPart($mime_part, false);
 547      }
 548  
 549      /**
 550       * Render MIME Part attachment info.
 551       *
 552       * @param MIME_Part &$mime_part  A MIME_Part object.
 553       *
 554       * @return string  The rendered data.
 555       */
 556      function renderMIMEAttachmentInfo(&$mime_part)
 557      {
 558          return $this->_renderMIMEPart($mime_part, true);
 559      }
 560  
 561      /**
 562       * Render MIME Part data.
 563       *
 564       * @access private
 565       *
 566       * @param MIME_Part &$mime_part  A MIME_Part object.
 567       * @param boolean $attachment    Render MIME Part attachment info?
 568       *
 569       * @return string  The rendered data.
 570       */
 571      function _renderMIMEPart(&$mime_part, $attachment = false)
 572      {
 573          /* Get the MIME_Viewer object for this MIME part */
 574          $viewer = &$this->getMIMEViewer($mime_part);
 575          if (!is_a($viewer, 'MIME_Viewer')) {
 576              return '';
 577          }
 578  
 579          $msg = '';
 580  
 581          $mime_part->transferDecodeContents();
 582  
 583          /* If this is a text/* part, AND the text is in a different character
 584           * set than the browser, convert to the current character set.
 585           * Additionally, if the browser does not support UTF-8, give the
 586           * user a link to open the part in a new window with the correct
 587           * character set. */
 588          $charset = $mime_part->getCharset();
 589          if ($charset) {
 590              $charset_upper = String::upper($charset);
 591              if (($charset_upper != 'US-ASCII') &&
 592                  !$this->viewAsAttachment()) {
 593                  $default_charset = String::upper(NLS::getCharset());
 594                  if ($charset_upper != $default_charset) {
 595                      $mime_part->setContents(String::convertCharset($mime_part->getContents(), $charset, $default_charset));
 596                      $mime_part->setCharset($default_charset);
 597                      if ($default_charset != 'UTF-8') {
 598                          $status = array(
 599                              sprintf(_("This message was written in a character set (%s) other than your own."), htmlspecialchars($charset)),
 600                              sprintf(_("If it is not displayed correctly, %s to open it in a new window."), $this->linkViewJS($mime_part, 'view_attach', _("click here")))
 601                          );
 602                          $msg = $this->formatStatusMsg($status, null, false) . $msg;
 603                      }
 604                  }
 605              }
 606          }
 607  
 608          $viewer->setMIMEPart($mime_part);
 609          $params = array(&$this);
 610          if ($attachment) {
 611              $msg .= $viewer->renderAttachmentInfo($params);
 612          } else {
 613              $msg .= $viewer->render($params);
 614          }
 615  
 616          return $msg;
 617      }
 618  
 619      /**
 620       * Build the message deciding what MIME Parts to show.
 621       *
 622       * @return boolean  False on error.
 623       */
 624      function buildMessage()
 625      {
 626          $this->_atc = array();
 627          $this->_parts = array();
 628          $this->_summary = array();
 629  
 630          if (!is_a($this->_message, 'MIME_Message')) {
 631              return false;
 632          }
 633  
 634          /* Now display the parts. */
 635          $mime_part = $this->_message->getBasePart();
 636          $this->buildMessagePart($mime_part);
 637  
 638          return true;
 639      }
 640  
 641      /**
 642       * Processes a MIME_Part and stores the display information in the internal
 643       * class variables.
 644       *
 645       * @param MIME_Part &$mime_part  The MIME_Part object to process.
 646       *
 647       * @return string  The rendered text.
 648       */
 649      function buildMessagePart(&$mime_part)
 650      {
 651          $msg = '';
 652  
 653          /* If we can't display the part inline, add it to the attachment
 654             list. If the MIME ID of the current part is '0', then force a
 655             render of the part (since it is the base part and, without
 656             attempting to render, the message will ALWAYS appear empty. */
 657          if (!$this->canDisplayInline($mime_part) &&
 658              ($mime_part->getMIMEId() != 0)) {
 659              /* Not displaying inline; add to the attachments list. */
 660              if (($this->_displayType == MIME_CONTENTS_DISPLAY_TYPE_LIST) ||
 661                  ($this->_displayType == MIME_CONTENTS_DISPLAY_TYPE_BOTH)) {
 662                  $this->setSummary($mime_part, 'attachment');
 663              }
 664              if (($this->_displayType == MIME_CONTENTS_DISPLAY_TYPE_INLINE) ||
 665                  ($this->_displayType == MIME_CONTENTS_DISPLAY_TYPE_BOTH)) {
 666                  $this->setSummary($mime_part, 'part');
 667              }
 668  
 669              /* Check to see if any attachment information can be rendered by
 670                 the MIME_Viewer. */
 671              $msg = $this->renderMIMEAttachmentInfo($mime_part);
 672              if (!empty($msg)) {
 673                  $key = $this->_getMIMEKey($mime_part);
 674                  $this->_parts[$key] = $msg;
 675              }
 676          } else {
 677              $msg = $this->renderMIMEPart($mime_part);
 678              $key = $this->_getMIMEKey($mime_part);
 679              if (!$this->_attach822) {
 680                  $this->_parts[$key] = $msg;
 681              }
 682              /* Some MIME_Viewers set the summary by themelves, so only
 683               * add to attachment/inline lists if nothing has been set
 684               * as of yet. */
 685              if ((($mime_part->getType() != 'multipart/mixed') ||
 686                   !empty($msg)) &&
 687                  !empty($key) &&
 688                  !$this->getIndex($key, 'summary')) {
 689                  $this->setSummary($mime_part, 'part');
 690                  if ($this->_attach822 &&
 691                      (($this->_displayType == MIME_CONTENTS_DISPLAY_TYPE_LIST) ||
 692                      ($this->_displayType == MIME_CONTENTS_DISPLAY_TYPE_BOTH))) {
 693                      $this->setSummary($mime_part, 'attachment');
 694                  }
 695              }
 696          }
 697  
 698          if ($mime_part->getInformation('header')) {
 699              /* If this is message/rfc822 part, and it is marked as an
 700               * attachment, we need to let future calls to buildMessagePart()
 701               * know that it should mark embedded parts as not viewable
 702               * inline. */
 703              $increment_822 = false;
 704              if (($mime_part->getType() == 'message/rfc822') &&
 705                  ($mime_part->getDisposition() == 'attachment')) {
 706                  $this->_attach822++;
 707                  $increment_822 = true;
 708              }
 709  
 710              foreach ($mime_part->getParts() as $part) {
 711                  $msg .= $this->buildMessagePart($part);
 712              }
 713  
 714              if ($increment_822) {
 715                  $this->_attach822--;
 716              }
 717          }
 718  
 719          return $msg;
 720      }
 721  
 722      /**
 723       * Are we viewing this page as an attachment through view.php?
 724       * This method can also be called via MIME_Contents::viewAsAttachment().
 725       *
 726       * @param boolean $popup  If true, also check if we are viewing attachment
 727       *                        in popup view window.
 728       *
 729       * @return boolean  True if we are viewing this part as an attachment
 730       *                  through view.php.
 731       */
 732      function viewAsAttachment($popup = false)
 733      {
 734          return ((strpos($_SERVER['PHP_SELF'], 'view.php') !== false) &&
 735                  (!$popup || Util::getFormData('popup_view')));
 736      }
 737  
 738      /**
 739       * Sets a summary entry.
 740       *
 741       * @param MIME_Part &$mime_part  The MIME_Part object.
 742       * @param string $type           The summary cache to use.
 743       */
 744      function setSummary(&$mime_part, $type)
 745      {
 746          if ($type == 'attachment') {
 747              $cache = &$this->_atc;
 748          } elseif ($type == 'part') {
 749              $cache = &$this->_summary;
 750          } else {
 751              return;
 752          }
 753  
 754          $key = $this->_getMIMEKey($mime_part);
 755  
 756          $this->_summaryType = $type;
 757          $summary = $this->partSummary($mime_part, null);
 758          $this->_summaryType = null;
 759  
 760          if (!empty($summary)) {
 761              if (!isset($this->_parts[$key])) {
 762                  $this->_parts[$key] = null;
 763              }
 764              $cache[$key] = $summary;
 765          }
 766      }
 767  
 768      /**
 769       * Returns an array summarizing a part of a MIME message.
 770       *
 771       * @param MIME_Part &$mime_part  The MIME_Part to summarize.
 772       * @param boolean $guess         Is this a temporary guessed-type part?
 773       *
 774       * @return array  The summary of the part.
 775       *                [0] = Icon
 776       *                [1] = IMAP ID
 777       *                [2] = Description
 778       *                [3] = MIME Type
 779       *                [4] = Size
 780       *                [5] = Download link/icon
 781       */
 782      function partSummary(&$mime_part, $guess = false)
 783      {
 784          $attachment = ($mime_part->getDisposition() == 'attachment');
 785          $bytes = $mime_part->getBytes();
 786          $description = htmlspecialchars($mime_part->getDescription(true, true));
 787          $summary = array();
 788          $type = htmlspecialchars($mime_part->getType());
 789          $viewer = &$this->getMIMEViewer($mime_part);
 790          if (!$viewer) {
 791              return $summary;
 792          }
 793  
 794          /* Return if we still didn't find a native mime viewer
 795           * for a guessed mime type. */
 796          if ($guess && is_a($viewer, 'MIME_Viewer_default')) {
 797              return $summary;
 798          }
 799  
 800          /* Get the MIME id index. */
 801          $id = $this->_getMIMEKey($mime_part);
 802  
 803          /* Icon column. */
 804          $summary[] = Horde::img($viewer->getIcon($type), null, null, '');
 805  
 806          /* Number column. */
 807          if (($this->_displayType == MIME_CONTENTS_DISPLAY_TYPE_BOTH) &&
 808              !is_null($this->_summaryType)) {
 809              $summary[] = '<a id="mime_contents_' . $this->_summaryType . '_' . $id . '" href="#mime_contents_' . (($this->_summaryType == 'attachment') ?'part' : 'attachment') . '_' . $id . '">' . $id . '</a>';
 810          } else {
 811              $summary[] = $id;
 812          }
 813  
 814          /* Name/text part column. */
 815          if (!$this->_links ||
 816              (!$attachment && empty($bytes))  ||
 817              !isset($this->_viewID['view']) ||
 818              is_a($viewer, 'MIME_Viewer_default')) {
 819              $summary[] = $description;
 820          } else {
 821              $param_array = array();
 822              if ($guess === true) {
 823                  $param_array['ctype'] = $type;
 824              }
 825              $summary[] = $this->linkViewJS($mime_part, $this->_viewID['view'], $description, null, null, $param_array);
 826          }
 827  
 828          /* MIME type column. */
 829          if ($guess) {
 830              $summary[] = '<em>' . sprintf(_("View as %s"), $type) . '</em>';
 831          } else {
 832              $summary[] = '[' . $type . ']';
 833          }
 834  
 835          /* Size Column. */
 836          $size = $mime_part->getSize(true);
 837          if (!empty($bytes) &&
 838              ($mime_part->getCurrentEncoding() == 'base64')) {
 839              /* From RFC 2045 [6.8]: "...the encoded data are consistently
 840                 only about 33 percent larger than the unencoded data." */
 841              $size = number_format(max((($bytes * 0.75) / 1024), 1));
 842              $summary[] = '<acronym title="' . _("Approximate Size") . '">' . sprintf(_("%s KB"), $size) . '</acronym>';
 843          } else {
 844              $summary[] = sprintf(_("%s KB"), $size);
 845          }
 846  
 847          /* Download column. */
 848          if (!$this->_links ||
 849              is_null($size) ||
 850              !isset($this->_viewID['download'])) {
 851              $summary[] = null;
 852          } else {
 853              $summary[] = $this->linkView($mime_part, $this->_viewID['download'], Horde::img('download.png', _("Download"), null, $GLOBALS['registry']->getImageDir('horde')), array('jstext' => sprintf(_("Download %s"), $mime_part->getDescription(true, true))), true);
 854          }
 855  
 856          /* If the MIME Type is application/octet-stream, try to use
 857             the file extension to determine the actual MIME type. */
 858          if ($this->_links &&
 859              !$guess &&
 860              !is_null($size) &&
 861              ($type == 'application/octet-stream') ||
 862              ($type == 'application/base64')) {
 863              require_once dirname(__FILE__) . '/Magic.php';
 864              $new_type = MIME_Magic::filenameToMIME(MIME::decode($mime_part->getName()));
 865              if ($new_type != 'application/octet-stream') {
 866                  $temp_part = $mime_part;
 867                  $temp_part->setType($new_type);
 868                  $summary = array_map(
 869                      create_function('$a, $b', 'return empty($b) ? $a : $a . \'<br />\' . $b;'),
 870                      $summary,
 871                      $this->partSummary($temp_part, true));
 872              }
 873          }
 874  
 875          return $summary;
 876      }
 877  
 878      /**
 879       * Return the URL to the view.php page.
 880       *
 881       * @param MIME_Part &$mime_part  The MIME_Part object to view.
 882       * @param integer $actionID      The ActionID to perform.
 883       * @param array $params          A list of any additional parameters that
 884       *                               need to be passed to view.php. (key =
 885       *                               name)
 886       * @param boolean $dload         Should we generate a download link?
 887       *
 888       * @return string  The URL to view.php.
 889       */
 890      function urlView(&$mime_part, $actionID, $params = array(), $dload = false)
 891      {
 892          /* Get the MIME ID for this part. */
 893          $id = (isset($params['id'])) ? $params['id'] : $mime_part->getMIMEId();
 894  
 895          /* Add the necessary local parameters. */
 896          $params['actionID'] = $actionID;
 897          $params['id'] = $id;
 898          $params = array_merge($params, $this->cacheIDURLParam());
 899  
 900          if ($dload) {
 901              $url = Horde::downloadUrl($mime_part->getName(true, true), $params);
 902          } else {
 903              $url = Util::addParameter(Horde::applicationUrl('view.php'), $params);
 904          }
 905  
 906          return $url;
 907      }
 908  
 909      /**
 910       * Generate a link to the view.php page.
 911       *
 912       * @param MIME_Part &$mime_part  The MIME_Part object to view.
 913       * @param integer $actionID      The actionID value.
 914       * @param string $text           The link text.
 915       * @param array $params          A list of additional parameters.
 916       *   'class'       -  The CSS class to use.
 917       *   'jstext'      -  The JS text to use.
 918       *   'viewparams'  -  A list of any additional parameters that need to be
 919       *                    passed to view.php.
 920       * @param boolean $dload         Should we generate a download link?
 921       *
 922       * @return string  A HTML href link to view.php.
 923       */
 924      function linkView(&$mime_part, $actionID, $text, $params = array(),
 925                        $dload = false)
 926      {
 927          if (!isset($params['class'])) {
 928              $params['class'] = null;
 929          }
 930          if (!isset($params['jstext'])) {
 931              $params['jstext'] = $text;
 932          }
 933          if (!isset($params['viewparams'])) {
 934              $params['viewparams'] = array();
 935          }
 936  
 937          if ($dload) {
 938              $window = null;
 939          } else {
 940              $window = 'view_' . abs(crc32(microtime()));
 941          }
 942  
 943          return Horde::link($this->urlView($mime_part, $actionID, $params['viewparams'], $dload), $params['jstext'], $params['class'], $window) . $text . '</a>';
 944      }
 945  
 946      /**
 947       * Generate a javascript link to the view.php page.
 948       *
 949       * @param MIME_Part &$mime_part  The MIME_Part object to view.
 950       * @param integer $actionID      The ActionID to perform.
 951       * @param string $text           The link text.
 952       * @param string $jstext         The Javascript link text.
 953       * @param string $css            The CSS class to use.
 954       * @param array $params          A list of any additional parameters that
 955       *                               need to be passed to view.php. (key =
 956       *                               name)
 957       * @param boolean $widget        If true use Horde::widget() to generate,
 958       *                               Horde::link() otherwise.
 959       *
 960       * @return string  A HTML href link to view.php.
 961       */
 962      function linkViewJS(&$mime_part, $actionID, $text, $jstext = null,
 963                          $css = null, $params = array(), $widget = false)
 964      {
 965          /* If viewing via view.php, we don't want a JS link. */
 966          if ($this->viewAsAttachment()) {
 967              return $this->linkView($mime_part, $actionID, $text, $params);
 968          }
 969  
 970          if (empty($jstext)) {
 971              $jstext = sprintf(_("View %s"), $mime_part->getDescription(true, true));
 972          }
 973          $params['popup_view'] = 1;
 974  
 975          $url = $this->urlView($mime_part, $actionID, $params);
 976  
 977          if (!($id = $mime_part->getMIMEId())) {
 978              $id = abs(crc32(serialize($mime_part)));
 979          }
 980  
 981          if ($widget) {
 982              return Horde::widget('#', $jstext, $css, null, "view('" . $url . "', '" . $id . "'); return false;", $text);
 983          } else {
 984              return Horde::link('#', $jstext, $css, null, "view('" . $url . "', '" . $id . "'); return false;") . $text . '</a>';
 985          }
 986      }
 987  
 988      /**
 989       * Prints out the status message for a given MIME Part.
 990       *
 991       * @param string $msg     The message to output.
 992       * @param string $img     An image link to add to the beginning of the
 993       *                        message.
 994       * @param boolean $print  Output this message when in a print view?
 995       * @param string $class   An optional style for the status box.
 996       *
 997       * @return string  The formatted status message string.
 998       */
 999      function formatStatusMsg($msg, $img = null, $printable = true,
1000                               $class = null)
1001      {
1002          if (!is_array($msg)) {
1003              $msg = array($msg);
1004          }
1005  
1006          /* If we are viewing as an attachment, don't print HTML code. */
1007          if ($this->viewAsAttachment()) {
1008              return implode("\n", $msg);
1009          }
1010  
1011          if (is_null($class)) {
1012              $class = 'mimeStatusMessage';
1013          }
1014          $text = '<table class="' . $class . '">';
1015  
1016          /* If no image, simply print out the message. */
1017          if (is_null($img)) {
1018              foreach ($msg as $val) {
1019                  $text .= '<tr><td>' . $val . '</td></tr>' . "\n";
1020              }
1021          } else {
1022              $text .= '<tr><td valign="middle">' . $img . '&nbsp;</td>';
1023              $text .= '<td>';
1024  
1025              if (count($msg) == 1) {
1026                  $text .= $msg[0];
1027              } else {
1028                  $text .= '<table>';
1029                  foreach ($msg as $val) {
1030                      $text .= '<tr><td>' . $val . '</td></tr>' . "\n";
1031                  }
1032                  $text .= '</table>';
1033              }
1034              $text .= '</td></tr>' . "\n";
1035          }
1036  
1037          return $text . '</table>';
1038      }
1039  
1040      /**
1041       * Return a pointer to the base object.
1042       *
1043       * @return mixed  Returns a pointer to the base object.
1044       *                Returns false if there is no base object.
1045       */
1046      function &getBaseObjectPtr()
1047      {
1048          if (is_null($this->_base)) {
1049              return $this;
1050          } else {
1051              return $this->_base;
1052          }
1053      }
1054  
1055      /**
1056       * Set the MIME_Contents:: object to be cached.
1057       *
1058       * @access private
1059       *
1060       * @param string  The cache OID.
1061       */
1062      function _addCache()
1063      {
1064          if (is_null($this->_sessionCacheID)) {
1065              $this->_sessionCacheID = $this->_createCacheID();
1066              register_shutdown_function(array(&$this, '_addCacheShutdown'));
1067          }
1068  
1069          return $this->_sessionCacheID;
1070      }
1071  
1072      /**
1073       * Creates a unique cache ID for this object.
1074       *
1075       * @access private
1076       *
1077       * @return string  A unique cache ID.
1078       */
1079      function _createCacheID()
1080      {
1081          return md5(microtime());
1082      }
1083  
1084      /**
1085       * Saves a copy of the MIME_Contents object at the end of a request.
1086       *
1087       * @access private
1088       */
1089      function _addCacheShutdown()
1090      {
1091          require_once 'Horde/SessionObjects.php';
1092          $cache = &Horde_SessionObjects::singleton();
1093  
1094          /* Don't save the MIME_Viewer cached objects since there is no easy
1095             way to regenerate them on cache reload. */
1096          $this->_viewerCache = array();
1097  
1098          /* Copy the MIME_Message cache object to the _message variable. */
1099          $this->_message = $this->_cachemessage;
1100  
1101          $cache->store($this->_sessionCacheID, $this);
1102      }
1103  
1104      /**
1105       * Returns the cached MIME_Contents:: object.
1106       * This function should be called statically e.g.:
1107       * $ptr = &MIME_Contents::getCache().
1108       *
1109       * @param string $cacheid  The cache ID to use.  If empty, will use the
1110       *                         cache ID located in the URL parameter named
1111       *                         MIME_CONTENTS_CACHE.
1112       *
1113       * @return MIME_Contents  The MIME_Contents object, or null if it does not
1114       *                        exist.
1115       */
1116      function &getCache($cacheid = null)
1117      {
1118          if (is_null($cacheid)) {
1119              $cacheid = Util::getFormData(MIME_CONTENTS_CACHE);
1120          }
1121  
1122          require_once 'Horde/SessionObjects.php';
1123          $cache = &Horde_SessionObjects::singleton();
1124          $contents = &$cache->query($cacheid);
1125          return $contents;
1126      }
1127  
1128      /**
1129       * Add the current object to the cache, and return the cache identifier
1130       * to be used in URLs.
1131       *
1132       * @return array  The parameter key/value set to use in URLs.
1133       */
1134      function cacheIDURLParam()
1135      {
1136          return array(MIME_CONTENTS_CACHE => $this->_addCache());
1137      }
1138  
1139  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7