draft_actions.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * draft_actions.php
  4. *
  5. * Copyright (c) 1999-2001 The Squirrelmail Development Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * $Id$
  9. */
  10. /*****************************************************************/
  11. /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
  12. /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
  13. /*** + Base level indent should begin at left margin, as ***/
  14. /*** the require_once below looks. ***/
  15. /*** + All identation should consist of four space blocks ***/
  16. /*** + Tab characters are evil. ***/
  17. /*** + all comments should use "slash-star ... star-slash" ***/
  18. /*** style -- no pound characters, no slash-slash style ***/
  19. /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
  20. /*** ALWAYS USE { AND } CHARACTERS!!! ***/
  21. /*** + Please use ' instead of ", when possible. Note " ***/
  22. /*** should always be used in _( ) function calls. ***/
  23. /*** Thank you for your help making the SM code more readable. ***/
  24. /*****************************************************************/
  25. require_once ('../src/validate.php');
  26. /* Print all the needed RFC822 headers */
  27. function write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers) {
  28. global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
  29. global $data_dir, $username, $popuser, $domain, $version, $useSendmail;
  30. global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
  31. global $REMOTE_HOST, $identity;
  32. // Storing the header to make sure the header is the same
  33. // everytime the header is printed.
  34. static $header, $headerlength;
  35. if ($header == '') {
  36. if (isset($identity) && ($identity != 'default')) {
  37. $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
  38. $from = getPref($data_dir, $username, 'full_name' . $identity);
  39. $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
  40. } else {
  41. $reply_to = getPref($data_dir, $username, 'reply_to');
  42. $from = getPref($data_dir, $username, 'full_name');
  43. $from_addr = getPref($data_dir, $username, 'email_address');
  44. }
  45. if ($from_addr == '') {
  46. $from_addr = $popuser.'@'.$domain;
  47. }
  48. /* Encoding 8-bit characters and making from line */
  49. $subject = encodeHeader($subject);
  50. if ($from == '') {
  51. $from = "<$from_addr>";
  52. } else {
  53. $from = '"' . encodeHeader($from) . "\" <$from_addr>";
  54. }
  55. /* This creates an RFC 822 date */
  56. $date = date("D, j M Y H:i:s ", mktime()) . timezone();
  57. /* Create a message-id */
  58. $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
  59. $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
  60. /* Insert header fields */
  61. $header = "Message-ID: $message_id\r\n";
  62. $header .= "Date: $date\r\n";
  63. $header .= "Subject: $subject\r\n";
  64. $header .= "From: $from\r\n";
  65. $header .= "To: $t\r\n"; // Who it's TO
  66. /* Insert headers from the $more_headers array */
  67. if(is_array($more_headers)) {
  68. reset($more_headers);
  69. while(list($h_name, $h_val) = each($more_headers)) {
  70. $header .= sprintf("%s: %s\r\n", $h_name, $h_val);
  71. }
  72. }
  73. if ($c) {
  74. $header .= "Cc: $c\r\n"; // Who the CCs are
  75. }
  76. if ($b) {
  77. $header .= "Bcc: $b\r\n"; // Who the BCCs are
  78. }
  79. if ($reply_to != '')
  80. $header .= "Reply-To: $reply_to\r\n";
  81. $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
  82. /* Do the MIME-stuff */
  83. $header .= "MIME-Version: 1.0\r\n";
  84. if (isMultipart()) {
  85. $header .= 'Content-Type: multipart/mixed; boundary="';
  86. $header .= mimeBoundary();
  87. $header .= "\"\r\n";
  88. } else {
  89. if ($default_charset != '')
  90. $header .= "Content-Type: text/plain; charset=$default_charset\r\n";
  91. else
  92. $header .= "Content-Type: text/plain;\r\n";
  93. $header .= "Content-Transfer-Encoding: 8bit\r\n";
  94. }
  95. $header .= "\r\n"; // One blank line to separate header and body
  96. $headerlength = strlen($header);
  97. }
  98. // Write the header
  99. fputs ($fp, $header);
  100. return $headerlength;
  101. }
  102. // Send the body
  103. function writeBodyForDraft ($fp, $passedBody) {
  104. global $default_charset;
  105. $attachmentlength = 0;
  106. if (isMultipart()) {
  107. $body = '--'.mimeBoundary()."\r\n";
  108. if ($default_charset != "")
  109. $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
  110. else
  111. $body .= "Content-Type: text/plain\r\n";
  112. $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
  113. $body .= $passedBody . "\r\n\r\n";
  114. fputs ($fp, $body);
  115. $attachmentlength = attachFiles($fp);
  116. if (!isset($postbody)) $postbody = "";
  117. $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
  118. fputs ($fp, $postbody);
  119. } else {
  120. $body = $passedBody . "\r\n";
  121. fputs ($fp, $body);
  122. $postbody = "\r\n";
  123. fputs ($fp, $postbody);
  124. }
  125. return (strlen($body) + strlen($postbody) + $attachmentlength);
  126. }
  127. function saveMessageAsDraft($t, $c, $b, $subject, $body, $reply_id) {
  128. global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad;
  129. global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
  130. global $draft_folder, $attachment_dir;
  131. $more_headers = Array();
  132. $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
  133. $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
  134. while ( file_exists($attachment_dir .$tmpDraftFile) )
  135. $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
  136. $fp = fopen($attachment_dir . $tmpDraftFile, 'w');
  137. $headerlength = write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers, FALSE);
  138. $bodylength = writeBodyForDraft ($fp, $body, FALSE);
  139. fclose($fp);
  140. $length = ($headerlength + $bodylength);
  141. if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
  142. sqimap_append ($imap_stream, $draft_folder, $length);
  143. write822HeaderForDraft ($imap_stream, $t, $c, $b, $subject, $more_headers, TRUE);
  144. writeBodyForDraft ($imap_stream, $body, TRUE);
  145. sqimap_append_done ($imap_stream);
  146. }
  147. sqimap_logout($imap_stream);
  148. if ($length)
  149. ClearAttachments();
  150. if (file_exists($attachment_dir . $tmpDraftFile) )
  151. unlink ($attachment_dir . $tmpDraftFile);
  152. return $length;
  153. }
  154. ?>