Source for file download.php

Documentation is available at download.php

  1. <?php
  2.  
  3. /**
  4.  * download.php
  5.  *
  6.  * Handles attachment downloads to the users computer.
  7.  * Also allows displaying of attachments when possible.
  8.  *
  9.  * @copyright 1999-2012 The SquirrelMail Project Team
  10.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11.  * @version $Id: download.php 14249 2012-01-02 02:09:17Z pdontthink $
  12.  * @package squirrelmail
  13.  */
  14.  
  15. /** This is the download page */
  16. define('PAGE_NAME''download');
  17.  
  18. /**
  19.  * Include the SquirrelMail initialization file.
  20.  */
  21. require('../include/init.php');
  22.  
  23. /* SquirrelMail required files. */
  24. require(SM_PATH 'functions/imap_general.php');
  25. require(SM_PATH 'functions/mailbox_display.php');
  26. require(SM_PATH 'functions/mime.php');
  27.  
  28. /**
  29.  * If a message is viewed from the search page, $aMailbox[$passed_id]['MESSAGE_OBJECT']
  30.  * is not initialized, which makes this page error out on line 65 with an
  31.  * undefined function.  We need to include some additional files in case the
  32.  * object has not been initialized.
  33.  * 
  34.  * TODO: Determine why the object in question is not initialized when coming from
  35.  *       a search page and correct.  Once that is done, we can remove these
  36.  *       includes.
  37.  */
  38. require(SM_PATH 'functions/imap_messages.php');
  39. require(SM_PATH 'functions/date.php');
  40.  
  41. header('Pragma: ');
  42. header('Cache-Control: cache');
  43.  
  44. /* globals */
  45. sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
  46. sqgetGlobalVar('messages',   $messages,     SQ_SESSION);
  47. sqgetGlobalVar('mailbox',    $mailbox,      SQ_GET);
  48. sqgetGlobalVar('ent_id',     $ent_id,       SQ_GET);
  49. sqgetGlobalVar('absolute_dl',$absolute_dl,  SQ_GET);
  50. sqgetGlobalVar('force_crlf'$force_crlf,   SQ_GET);
  51. sqgetGlobalVar('passed_id'$passed_idSQ_GETNULLSQ_TYPE_BIGINT);
  52. if (!sqgetGlobalVar('account'$accountSQ_GET) ) {
  53.     $account 0;
  54. }
  55.  
  56. global $default_charset;
  57.  
  58. /* end globals */
  59.  
  60. $imapConnection sqimap_login($usernamefalse$imapServerAddress$imapPort0);
  61. $aMailbox sqm_api_mailbox_select($imapConnection$account$mailbox,array(),array());
  62.  
  63. if (isset($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']&&
  64.     is_object($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) ) {
  65.     $message $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'];
  66. else {
  67.     $message sqimap_get_message($imapConnection$passed_id$mailbox);
  68.     $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'$message;
  69. }
  70.  
  71. $subject $message->rfc822_header->subject;
  72. if ($ent_id{
  73.     // replace message with message part, if message part is requested.
  74.     $message $message->getEntity($ent_id);
  75.     $header $message->header;
  76.  
  77.     if ($message->rfc822_header{
  78.        $subject $message->rfc822_header->subject;
  79.     else {
  80.        $header $message->header;
  81.     }
  82.     $type0 $header->type0;
  83.     $type1 $header->type1;
  84.     $encoding strtolower($header->encoding);
  85. else {
  86.     /* raw message */
  87.     $type0 'message';
  88.     $type1 'rfc822';
  89.     $encoding '7bit';
  90.     $header $message->header;
  91. }
  92.  
  93. /*
  94.  * lets redefine message as this particular entity that we wish to display.
  95.  * it should hold only the header for this entity.  We need to fetch the body
  96.  * yet before we can display anything.
  97.  */
  98.  
  99. if (isset($override_type0)) {
  100.     $type0 $override_type0;
  101. }
  102. if (isset($override_type1)) {
  103.     $type1 $override_type1;
  104. }
  105. $filename '';
  106. if (is_object($message->header->disposition)) {
  107.     $filename $header->disposition->getProperty('filename');
  108.     if (!$filename{
  109.         $filename $header->disposition->getProperty('name');
  110.     }
  111.     if (!$filename{
  112.         $filename $header->getParameter('name');
  113.     }
  114. else {
  115.     $filename $header->getParameter('name');
  116. }
  117.  
  118. $filename decodeHeader($filename,true,false);
  119. $filename charset_encode($filename,$default_charset,false);
  120.  
  121. // If name is not set, use subject of email
  122. if (strlen($filename1{
  123.     $filename decodeHeader($subjecttruetrue);
  124.     $filename charset_encode($filename,$default_charset,false);
  125.     if ($type1 == 'plain' && $type0 == 'text')
  126.         $suffix 'txt';
  127.     else if ($type1 == 'richtext' && $type0 == 'text')
  128.         $suffix 'rtf';
  129.     else if ($type1 == 'postscript' && $type0 == 'application')
  130.         $suffix 'ps';
  131.     else if ($type1 == 'rfc822' && $type0 == 'message')
  132.         $suffix 'eml';
  133.     else
  134.         $suffix $type1;
  135.  
  136.     if ($filename == '')
  137.         $filename 'untitled' strip_tags($ent_id);
  138.     $filename $filename '.' $suffix;
  139. }
  140.  
  141. /**
  142.  * Update mailbox_cache and close session in order to prevent
  143.  * script locking on larger downloads. SendDownloadHeaders() and
  144.  * mime_print_body_lines() don't write information to session.
  145.  * mime_print_body_lines() call duration depends on size of
  146.  * attachment and script can cause interface lockups, if session
  147.  * is not closed.
  148.  */
  149. $mailbox_cache[$aMailbox['NAME']] $aMailbox;
  150. sqsession_register($mailbox_cache,'mailbox_cache');
  151.  
  152. /*
  153.  * Note:
  154.  *    The following sections display the attachment in different
  155.  *    ways depending on how they choose.  The first way will download
  156.  *    under any circumstance.  This sets the Content-type to be
  157.  *    applicatin/octet-stream, which should be interpreted by the
  158.  *    browser as "download me".
  159.  *      The second method (view) is used for images or other formats
  160.  *    that should be able to be handled by the browser.  It will
  161.  *    most likely display the attachment inline inside the browser.
  162.  *      And finally, the third one will be used by default.  If it
  163.  *    is displayable (text or html), it will load them up in a text
  164.  *    viewer (built in to SquirrelMail).  Otherwise, it sets the
  165.  *    content-type as application/octet-stream
  166.  */
  167. if (isset($absolute_dl&& $absolute_dl{
  168.     SendDownloadHeaders($type0$type1$filename1);
  169. else {
  170.     SendDownloadHeaders($type0$type1$filename0);
  171. }
  172. /* be aware that any warning caused by download.php will corrupt the
  173.  * attachment in case of ERROR reporting = E_ALL and the output is the screen */
  174. mime_print_body_lines ($imapConnection$passed_id$ent_id$encoding'php://stdout'$force_crlf);

Documentation generated on Sat, 18 May 2013 04:18:23 +0200 by phpDocumentor 1.4.3