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

Documentation generated on Sat, 07 Oct 2006 16:10:46 +0300 by phpDocumentor 1.3.0RC6