4 * attachment_common.php
6 * This file provides the handling of often-used attachment types.
8 * @copyright © 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
12 * @todo document attachment $type hook arguments
15 $attachment_common_show_images_list = array();
18 * Mapping of file extensions to mime types
20 * Used for application/octet-stream mime type detection.
21 * Supported extensions: bmp, gif, htm, html, jpg, jpeg, php,
22 * png, rtf, txt, patch (since 1.4.2), vcf
23 * @global array $FileExtensionToMimeType
25 $FileExtensionToMimeType = array('bmp' => 'image/x-bitmap',
28 'html' => 'text/html',
29 'jpe' => 'image/jpeg',
30 'jpg' => 'image/jpeg',
31 'jpeg' => 'image/jpeg',
32 'php' => 'text/plain',
34 'rtf' => 'text/richtext',
35 'txt' => 'text/plain',
36 'patch'=> 'text/plain',
37 'vcf' => 'text/x-vcard');
39 /* Register browser-supported image types */
40 sqgetGlobalVar('attachment_common_types', $attachment_common_types);
41 // FIXME: do we use $attachment_common_types that is not extracted by sqgetGlobalVar() ?
42 if (isset($attachment_common_types)) {
43 // var is used to detect activation of jpeg image types
45 /* Don't run this before being logged in. That may happen
46 when plugins include mime.php */
47 foreach ($attachment_common_types as $val => $v) {
48 if ($val == 'image/gif')
49 register_attachment_common('image/gif', 'link_image');
50 elseif (($val == 'image/jpeg' ||
$val == 'image/pjpeg' ||
$val == 'image/jpg') and
51 (!isset($jpeg_done))) {
53 register_attachment_common('image/jpg', 'link_image');
54 register_attachment_common('image/jpeg', 'link_image');
55 register_attachment_common('image/pjpeg', 'link_image');
57 elseif ($val == 'image/png')
58 register_attachment_common('image/png', 'link_image');
59 elseif ($val == 'image/x-xbitmap')
60 register_attachment_common('image/x-xbitmap', 'link_image');
61 elseif ($val == '*/*' ||
$val == 'image/*') {
63 * browser (Firefox) declared that anything is acceptable.
64 * Lets register some common image types.
66 if (! isset($jpeg_done)) {
68 register_attachment_common('image/jpg', 'link_image');
69 register_attachment_common('image/jpeg', 'link_image');
70 register_attachment_common('image/pjpeg', 'link_image');
72 register_attachment_common('image/gif', 'link_image');
73 register_attachment_common('image/png', 'link_image');
74 register_attachment_common('image/x-xbitmap', 'link_image');
75 // register_attachment_common('image/x-ico', 'link_image');
76 // register_attachment_common('image/x-icon', 'link_image');
77 // register_attachment_common('image/bmp', 'link_image');
78 // register_attachment_common('image/x-ms-bmp', 'link_image');
84 /* Register text-type attachments */
85 register_attachment_common('message/rfc822', 'link_message');
86 register_attachment_common('text/plain', 'link_text');
87 register_attachment_common('text/richtext', 'link_text');
90 register_attachment_common('text/html', 'link_html');
93 register_attachment_common('text/x-vcard', 'link_vcard');
94 register_attachment_common('text/directory', 'link_vcard');
96 /* Register rules for general types.
97 * These will be used if there isn't a more specific rule available. */
98 register_attachment_common('text/*', 'link_text');
99 register_attachment_common('message/*', 'link_text');
101 /* Register "unknown" attachments */
102 register_attachment_common('application/octet-stream', 'octet_stream');
106 * Function which optimizes readability of the above code
107 * Registers 'attachment $type' hooks.
108 * @param string $type attachment type
109 * @param string $func suffix of attachment_common_* function, which handles $type attachments.
112 function register_attachment_common($type, $func) {
113 global $squirrelmail_plugin_hooks;
114 $squirrelmail_plugin_hooks['attachment ' . $type]['attachment_common'] =
115 'attachment_common_' . $func;
119 * Adds href and text keys to attachment_common array for text attachments
120 * @param array $Args attachment $type hook arguments
123 function attachment_common_link_text(&$Args) {
125 /* If there is a text attachment, we would like to create a "View" button
126 that links to the text attachment viewer.
128 $Args[1] = the array of actions
130 Use the name of this file for adding an action
131 $Args[1]['attachment_common'] = Array for href and text
133 $Args[1]['attachment_common']['text'] = What is displayed
134 $Args[1]['attachment_common']['href'] = Where it links to */
135 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER
);
137 // if htmlspecialchars() breaks something - find other way to encode & in url.
138 $Args[1]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING);
139 $Args[1]['attachment_common']['href'] =
140 set_url_var($Args[1]['attachment_common']['href'],
143 /* The link that we created needs a name. */
144 $Args[1]['attachment_common']['text'] = _("View");
146 /* Each attachment has a filename on the left, which is a link.
147 Where that link points to can be changed. Just in case the link above
148 for viewing text attachments is not the same as the default link for
149 this file, we'll change it.
151 This is a lot better in the image links, since the defaultLink will just
152 download the image, but the one that we set it to will format the page
153 to have an image tag in the center (looking a lot like this text viewer) */
154 $Args[6] = $Args[1]['attachment_common']['href'];
158 * Adds href and text keys to attachment_common array for rfc822 attachments
159 * @param array $Args attachment $type hook arguments
162 function attachment_common_link_message(&$Args) {
164 $Args[1]['attachment_common']['href'] = $base_uri . 'src/read_body.php?startMessage=' .
165 $Args[2] . '&passed_id=' . $Args[3] . '&mailbox=' . $Args[4] .
166 '&passed_ent_id=' . $Args[5] . '&override_type0=message&override_type1=rfc822';
168 $Args[1]['attachment_common']['text'] = _("View");
170 $Args[6] = $Args[1]['attachment_common']['href'];
174 * Adds href and text keys to attachment_common array for html attachments
175 * @param array $Args attachment $type hook arguments
178 function attachment_common_link_html(&$Args) {
180 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER
);
182 $Args[1]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING).
183 /* why use the overridetype? can this be removed */
184 /* override_type might be needed only when we want view other type of messages as html */
185 '&override_type0=text&override_type1=html';
186 $Args[1]['attachment_common']['href'] =
187 set_url_var($Args[1]['attachment_common']['href'],
190 $Args[1]['attachment_common']['text'] = _("View");
192 $Args[6] = $Args[1]['attachment_common']['href'];
196 * Adds href and text keys to attachment_common array for image attachments
197 * @param array $Args attachment $type hook arguments
200 function attachment_common_link_image(&$Args) {
201 global $attachment_common_show_images_list, $base_uri ;
203 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER
);
205 $info['passed_id'] = $Args[3];
206 $info['mailbox'] = $Args[4];
207 $info['ent_id'] = $Args[5];
209 $attachment_common_show_images_list[] = $info;
211 $Args[1]['attachment_common']['href'] = $base_uri . 'src/image.php?'. htmlspecialchars($QUERY_STRING);
212 $Args[1]['attachment_common']['href'] =
213 set_url_var($Args[1]['attachment_common']['href'],
216 $Args[1]['attachment_common']['text'] = _("View");
218 $Args[6] = $Args[1]['attachment_common']['href'];
222 * Adds href and text keys to attachment_common array for vcard attachments
223 * @param array $Args attachment $type hook arguments
226 function attachment_common_link_vcard(&$Args) {
228 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER
);
230 $Args[1]['attachment_common']['href'] = $base_uri . 'src/vcard.php?'. htmlspecialchars($QUERY_STRING);
231 $Args[1]['attachment_common']['href'] =
232 set_url_var($Args[1]['attachment_common']['href'],
235 $Args[1]['attachment_common']['text'] = _("View Business Card");
237 $Args[6] = $Args[1]['attachment_common']['href'];
241 * Processes octet-stream attachments.
242 * Calls attachment_common-load_mime_types and attachment $type hooks.
243 * @param array $Args attachment $type hook arguments
246 function attachment_common_octet_stream(&$Args) {
247 global $FileExtensionToMimeType;
249 do_hook('attachment_common-load_mime_types');
251 ereg('\\.([^\\.]+)$', $Args[7], $Regs);
253 $Ext = strtolower($Regs[1]);
255 if ($Ext == '' ||
! isset($FileExtensionToMimeType[$Ext]))
258 $Ret = do_hook('attachment ' . $FileExtensionToMimeType[$Ext],
259 $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6],
260 $Args[7], $Args[8], $Args[9]);
262 foreach ($Ret as $a => $b) {