The $messages cache was checked but not imported from the session.
[squirrelmail.git] / src / printer_friendly_bottom.php
1 <?php
2
3 /**
4 * printer_friendly_bottom.php
5 *
6 * with javascript on, it is the bottom frame of printer_friendly_main.php
7 * else, it is alone in a new window
8 *
9 * - this is the page that does all the work, really.
10 *
11 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @version $Id$
14 * @package squirrelmail
15 */
16
17 /** This is the printer_friendly_bottom page */
18 define('PAGE_NAME', 'printer_friendly_bottom');
19
20 /**
21 * Include the SquirrelMail initialization file.
22 */
23 require('../include/init.php');
24
25 /* SquirrelMail required files. */
26 require_once(SM_PATH . 'functions/imap_general.php');
27 require_once(SM_PATH . 'functions/imap_messages.php');
28 require_once(SM_PATH . 'functions/date.php');
29 require_once(SM_PATH . 'functions/mime.php');
30 require_once(SM_PATH . 'functions/url_parser.php');
31
32 /* get some of these globals */
33 sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
34 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
35 sqgetGlobalVar('messages', $messages, SQ_SESSION);
36
37 if (! sqgetGlobalVar('passed_ent_id', $passed_ent_id, SQ_GET) ) {
38 $passed_ent_id = '';
39 }
40 sqgetGlobalVar('show_html_default', $show_html_default, SQ_FORM);
41 /* end globals */
42
43 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
44 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
45 if (isset($messages[$mbx_response['UIDVALIDITY']][$passed_id])) {
46 $message = $messages[$mbx_response['UIDVALIDITY']][$passed_id];
47 } else {
48 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
49 }
50 if ($passed_ent_id) {
51 $message = $message->getEntity($passed_ent_id);
52 }
53
54 /* --start display setup-- */
55
56 $rfc822_header = $message->rfc822_header;
57 /* From and Date are usually fine as they are... */
58 $from = $rfc822_header->getAddr_s('from');
59 $date = getLongDateString($rfc822_header->date, $rfc822_header->date_unparsed);
60 $subject = trim($rfc822_header->subject);
61
62 /* we can clean these up if the list is too long... */
63 $cc = $rfc822_header->getAddr_s('cc');
64 $to = $rfc822_header->getAddr_s('to');
65
66 if ($show_html_default == 1) {
67 $ent_ar = $message->findDisplayEntity(array());
68 } else {
69 $ent_ar = $message->findDisplayEntity(array(), array('text/plain'));
70 }
71 $body = '';
72 if ($ent_ar[0] != '') {
73 for ($i = 0; $i < count($ent_ar); $i++) {
74 $body .= formatBody($imapConnection, $message, $color, $wrap_at, $ent_ar[$i], $passed_id, $mailbox, TRUE);
75 if ($i < count($ent_ar)-1) {
76 $body .= '<hr />';
77 }
78 }
79 /* Note that $body is passed to this hook (and modified) by reference as of 1.5.2 */
80 do_hook('message_body', $body);
81 } else {
82 $body = _("Message not printable");
83 }
84
85 /* now we clean up the display a bit... */
86
87 $num_leading_spaces = 9; // nine leading spaces for indentation
88
89 // sometimes I see ',,' instead of ',' separating addresses *shrug*
90 $cc = pf_clean_string(str_replace(',,', ',', $cc), $num_leading_spaces);
91 $to = pf_clean_string(str_replace(',,', ',', $to), $num_leading_spaces);
92
93 // clean up everything else...
94 $subject = pf_clean_string($subject, $num_leading_spaces);
95 $from = pf_clean_string($from, $num_leading_spaces);
96 $date = pf_clean_string($date, $num_leading_spaces);
97
98 // end cleanup
99
100 $to = decodeHeader($to);
101 $cc = decodeHeader($cc);
102 $from = decodeHeader($from);
103 $subject = decodeHeader($subject);
104
105 // --end display setup--
106
107
108 /* --start browser output-- */
109 displayHtmlHeader($subject);
110
111 $aHeaders = array();
112 $aHeaders[ _("From") ] = $from;
113 $aHeaders[ _("Subject") ] = $subject;
114 $aHeaders[ _("Date") ] = htmlspecialchars($date);
115 $aHeaders[ _("To") ] = $to;
116 $aHeaders[ _("Cc") ] = $cc;
117
118 $attachments_ar = buildAttachmentArray($message, $ent_ar, $mailbox, $passed_id);
119
120 $oTemplate->assign('headers', $aHeaders);
121 $oTemplate->assign('message_body', $body);
122 $oTemplate->assign('attachments', $attachments_ar);
123
124 $oTemplate->display('printer_friendly_bottom.tpl');
125 $oTemplate->display('footer.tpl');
126
127 /* --end browser output-- */
128
129
130 /* --start pf-specific functions-- */
131
132 /**
133 * Function should clean layout of printed messages when user
134 * enables "Printer Friendly Clean Display" option.
135 * For example: $string = pf_clean_string($string, 9);
136 *
137 * @param string unclean_string
138 * @param integer num_leading_spaces
139 * @return string
140 * @access private
141 */
142 function pf_clean_string ( $unclean_string, $num_leading_spaces ) {
143 global $data_dir, $username;
144 $unclean_string = str_replace('&nbsp;',' ',$unclean_string);
145 $wrap_at = getPref($data_dir, $username, 'wrap_at', 86);
146 $wrap_at = $wrap_at - $num_leading_spaces; /* header stuff */
147
148 $leading_spaces = '';
149 while ( strlen($leading_spaces) < $num_leading_spaces )
150 $leading_spaces .= ' ';
151
152 $clean_string = '';
153 while ( strlen($unclean_string) > $wrap_at )
154 {
155 $this_line = substr($unclean_string, 0, $wrap_at);
156 if ( strrpos( $this_line, "\n" ) ) /* this should NEVER happen with anything but the $body */
157 {
158 $clean_string .= substr( $this_line, 0, strrpos( $this_line, "\n" ));
159 $clean_string .= $leading_spaces;
160 $unclean_string = substr($unclean_string, strrpos( $this_line, "\n" ));
161 }
162 else
163 {
164 $i = strrpos( $this_line, ' ');
165 $clean_string .= substr( $this_line, 0, $i);
166 $clean_string .= "\n" . $leading_spaces;
167 $unclean_string = substr($unclean_string, 1+$i);
168 }
169 }
170 $clean_string .= $unclean_string;
171
172 return $clean_string;
173 } /* end pf_clean_string() function */