da14209be1fe48268ff760adc86f0ac92f73ad7e
[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
36 if (! sqgetGlobalVar('passed_ent_id', $passed_ent_id, SQ_GET) ) {
37 $passed_ent_id = '';
38 }
39 sqgetGlobalVar('show_html_default', $show_html_default, SQ_FORM);
40 /* end globals */
41
42 $imapConnection = sqimap_login($username, false, $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, $rfc822_header->date_unparsed);
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, TRUE);
74 if ($i < count($ent_ar)-1) {
75 $body .= '<hr />';
76 }
77 }
78 /* Note that $body is passed to this hook (and modified) by reference as of 1.5.2 */
79 do_hook('message_body', $body);
80 } else {
81 $body = _("Message not printable");
82 }
83
84 /* now we clean up the display a bit... */
85
86 $num_leading_spaces = 9; // nine leading spaces for indentation
87
88 // sometimes I see ',,' instead of ',' separating 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 // clean up everything else...
93 $subject = pf_clean_string($subject, $num_leading_spaces);
94 $from = pf_clean_string($from, $num_leading_spaces);
95 $date = pf_clean_string($date, $num_leading_spaces);
96
97 // end cleanup
98
99 $to = decodeHeader($to);
100 $cc = decodeHeader($cc);
101 $from = decodeHeader($from);
102 $subject = decodeHeader($subject);
103
104 // --end display setup--
105
106
107 /* --start browser output-- */
108 displayHtmlHeader($subject);
109
110 $aHeaders = array();
111 $aHeaders[ _("From") ] = $from;
112 $aHeaders[ _("Subject") ] = $subject;
113 $aHeaders[ _("Date") ] = htmlspecialchars($date);
114 $aHeaders[ _("To") ] = $to;
115 $aHeaders[ _("Cc") ] = $cc;
116
117 $attachments_ar = buildAttachmentArray($message, $ent_ar, $mailbox, $passed_id);
118
119 $oTemplate->assign('headers', $aHeaders);
120 $oTemplate->assign('message_body', $body);
121 $oTemplate->assign('attachments', $attachments_ar);
122
123 $oTemplate->display('printer_friendly_bottom.tpl');
124 $oTemplate->display('footer.tpl');
125
126 /* --end browser output-- */
127
128
129 /* --start pf-specific functions-- */
130
131 /**
132 * Function should clean layout of printed messages when user
133 * enables "Printer Friendly Clean Display" option.
134 * For example: $string = pf_clean_string($string, 9);
135 *
136 * @param string unclean_string
137 * @param integer num_leading_spaces
138 * @return string
139 * @access private
140 */
141 function pf_clean_string ( $unclean_string, $num_leading_spaces ) {
142 global $data_dir, $username;
143 $unclean_string = str_replace('&nbsp;',' ',$unclean_string);
144 $wrap_at = getPref($data_dir, $username, 'wrap_at', 86);
145 $wrap_at = $wrap_at - $num_leading_spaces; /* header stuff */
146
147 $leading_spaces = '';
148 while ( strlen($leading_spaces) < $num_leading_spaces )
149 $leading_spaces .= ' ';
150
151 $clean_string = '';
152 while ( strlen($unclean_string) > $wrap_at )
153 {
154 $this_line = substr($unclean_string, 0, $wrap_at);
155 if ( strrpos( $this_line, "\n" ) ) /* this should NEVER happen with anything but the $body */
156 {
157 $clean_string .= substr( $this_line, 0, strrpos( $this_line, "\n" ));
158 $clean_string .= $leading_spaces;
159 $unclean_string = substr($unclean_string, strrpos( $this_line, "\n" ));
160 }
161 else
162 {
163 $i = strrpos( $this_line, ' ');
164 $clean_string .= substr( $this_line, 0, $i);
165 $clean_string .= "\n" . $leading_spaces;
166 $unclean_string = substr($unclean_string, 1+$i);
167 }
168 }
169 $clean_string .= $unclean_string;
170
171 return $clean_string;
172 } /* end pf_clean_string() function */