460dbb511ea67aedd4af7330419eeaf610f944bd
[squirrelmail.git] / src / printer_friendly_bottom.php
1 <?php
2
3 /**
4 * printer_friendly_bottom.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * with javascript on, it is the bottom frame of printer_friendly_main.php
10 * else, it is alone in a new window
11 *
12 * - this is the page that does all the work, really.
13 *
14 * @version $Id$
15 * @package squirrelmail
16 */
17
18 /**
19 * Path for SquirrelMail required files.
20 * @ignore
21 */
22 define('SM_PATH','../');
23
24 /* SquirrelMail required files. */
25 require_once(SM_PATH . 'include/validate.php');
26 require_once(SM_PATH . 'functions/imap.php');
27
28 /* get some of these globals */
29 sqgetGlobalVar('username', $username, SQ_SESSION);
30 sqgetGlobalVar('key', $key, SQ_COOKIE);
31 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
32
33 sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
34 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
35
36 if (! sqgetGlobalVar('passed_ent_id', $passed_ent_id, SQ_GET) ) {
37 $passed_ent_id = '';
38 }
39 /* end globals */
40
41 $pf_cleandisplay = getPref($data_dir, $username, 'pf_cleandisplay', false);
42 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
43 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
44 if (isset($messages[$mbx_response['UIDVALIDITY']][$passed_id])) {
45 $message = &$messages[$mbx_response['UIDVALIDITY']][$passed_id];
46 } else {
47 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
48 }
49 if ($passed_ent_id) {
50 $message = &$message->getEntity($passed_ent_id);
51 }
52
53 /* --start display setup-- */
54
55 $rfc822_header = $message->rfc822_header;
56 /* From and Date are usually fine as they are... */
57 $from = $rfc822_header->getAddr_s('from');
58 $date = getLongDateString($rfc822_header->date);
59 $subject = trim($rfc822_header->subject);
60
61 /* we can clean these up if the list is too long... */
62 $cc = $rfc822_header->getAddr_s('cc');
63 $to = $rfc822_header->getAddr_s('to');
64
65 if ($show_html_default == 1) {
66 $ent_ar = $message->findDisplayEntity(array());
67 } else {
68 $ent_ar = $message->findDisplayEntity(array(), array('text/plain'));
69 }
70 $body = '';
71 if ($ent_ar[0] != '') {
72 for ($i = 0; $i < count($ent_ar); $i++) {
73 $body .= formatBody($imapConnection, $message, $color, $wrap_at, $ent_ar[$i], $passed_id, $mailbox);
74 $body .= '<hr style="height: 1px;" />';
75 }
76 $hookResults = do_hook('message_body', $body);
77 $body = $hookResults[1];
78 } else {
79 $body = _("Message not printable");
80 }
81
82 /* now, if they choose to, we clean up the display a bit... */
83
84 if ($pf_cleandisplay) {
85
86 $num_leading_spaces = 9; // nine leading spaces for indentation
87
88 // sometimes I see ',,' instead of ',' seperating addresses *shrug*
89 $cc = pf_clean_string(str_replace(',,', ',', $cc), $num_leading_spaces);
90 $to = pf_clean_string(str_replace(',,', ',', $to), $num_leading_spaces);
91
92 // the body should have no leading zeros
93 // disabled because it destroys html mail
94
95 // $body = pf_clean_string($body, 0);
96
97 // clean up everything else...
98 $subject = pf_clean_string($subject, $num_leading_spaces);
99 $from = pf_clean_string($from, $num_leading_spaces);
100 $date = pf_clean_string($date, $num_leading_spaces);
101
102 } // end cleanup
103
104 $to = decodeHeader($to);
105 $cc = decodeHeader($cc);
106 $from = decodeHeader($from);
107 $subject = decodeHeader($subject);
108
109 // --end display setup--
110
111
112 /* --start browser output-- */
113 displayHtmlHeader( $subject, '', FALSE );
114
115 echo '<body text="#000000" bgcolor="#FFFFFF" link="#000000" vlink="#000000" alink="#000000">'."\n" .
116 /* headers (we use table because translations are not all the same width) */
117 html_tag( 'table', '', 'center', '', 'cellspacing="0" cellpadding="0" border="0" width="100%"' ) .
118 html_tag( 'tr',
119 html_tag( 'td', _("From").'&nbsp;', 'left' ,'','valign="top"') .
120 html_tag( 'td', $from, 'left' )
121 ) . "\n" .
122 html_tag( 'tr',
123 html_tag( 'td', _("Subject").'&nbsp;', 'left','','valign="top"' ) .
124 html_tag( 'td', $subject, 'left' )
125 ) . "\n" .
126 html_tag( 'tr',
127 html_tag( 'td', _("Date").'&nbsp;', 'left' ) .
128 html_tag( 'td', htmlspecialchars($date), 'left' )
129 ) . "\n" .
130 html_tag( 'tr',
131 html_tag( 'td', _("To").'&nbsp;', 'left','','valign="top"' ) .
132 html_tag( 'td', $to, 'left' )
133 ) . "\n";
134 if ( strlen($cc) > 0 ) { /* only show Cc: if it's there... */
135 echo html_tag( 'tr',
136 html_tag( 'td', _("Cc").'&nbsp;', 'left','','valign="top"' ) .
137 html_tag( 'td', $cc, 'left' )
138 );
139 }
140 /* body */
141 echo html_tag( 'tr',
142 html_tag( 'td', '<hr style="height: 1px;" /><br />' . "\n" . $body, 'left', '', 'colspan="2"' )
143 ) . "\n" .
144
145 '</table>' . "\n" .
146 '</body></html>';
147
148 /* --end browser output-- */
149
150
151 /* --start pf-specific functions-- */
152
153 /**
154 * Function should clean layout of printed messages when user
155 * enables "Printer Friendly Clean Display" option.
156 * For example: $string = pf_clean_string($string, 9);
157 *
158 * @param string unclean_string
159 * @param integer num_leading_spaces
160 * @return string
161 * @access private
162 */
163 function pf_clean_string ( $unclean_string, $num_leading_spaces ) {
164 global $data_dir, $username;
165 $unclean_string = str_replace('&nbsp;',' ',$unclean_string);
166 $wrap_at = getPref($data_dir, $username, 'wrap_at', 86);
167 $wrap_at = $wrap_at - $num_leading_spaces; /* header stuff */
168
169 $leading_spaces = '';
170 while ( strlen($leading_spaces) < $num_leading_spaces )
171 $leading_spaces .= ' ';
172
173 $clean_string = '';
174 while ( strlen($unclean_string) > $wrap_at )
175 {
176 $this_line = substr($unclean_string, 0, $wrap_at);
177 if ( strrpos( $this_line, "\n" ) ) /* this should NEVER happen with anything but the $body */
178 {
179 $clean_string .= substr( $this_line, 0, strrpos( $this_line, "\n" ));
180 $clean_string .= $leading_spaces;
181 $unclean_string = substr($unclean_string, strrpos( $this_line, "\n" ));
182 }
183 else
184 {
185 $i = strrpos( $this_line, ' ');
186 $clean_string .= substr( $this_line, 0, $i);
187 $clean_string .= "\n" . $leading_spaces;
188 $unclean_string = substr($unclean_string, 1+$i);
189 }
190 }
191 $clean_string .= $unclean_string;
192
193 return $clean_string;
194 } /* end pf_clean_string() function */
195
196 /* --end pf-specific functions */
197 ?>