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