4 * attachment_common.php
6 * This file provides the handling of often-used attachment types.
8 * @copyright © 1999-2005 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
16 if (! defined('SM_PATH')) define('SM_PATH','../');
18 /** sqgetGlobalVar() */
19 include_once(SM_PATH
. 'functions/global.php');
21 include_once(SM_PATH
. 'functions/display_messages.php');
23 global $attachment_common_show_images_list;
24 $attachment_common_show_images_list = array();
26 global $FileExtensionToMimeType, $attachment_common_types;
28 * Mapping of file extensions to mime types
30 * Used for application/octet-stream mime type detection.
31 * Supported extensions: bmp, gif, htm, html, jpg, jpeg, php,
32 * png, rtf, txt, patch (since 1.4.2), vcf
33 * @global array $FileExtensionToMimeType
35 $FileExtensionToMimeType = array('bmp' => 'image/x-bitmap',
38 'html' => 'text/html',
39 'jpe' => 'image/jpeg',
40 'jpg' => 'image/jpeg',
41 'jpeg' => 'image/jpeg',
42 'php' => 'text/plain',
44 'rtf' => 'text/richtext',
45 'txt' => 'text/plain',
46 'patch'=> 'text/plain',
47 'vcf' => 'text/x-vcard');
49 /* Register browser-supported image types */
50 sqgetGlobalVar('attachment_common_types', $attachment_common_types);
51 // FIXME: do we use $attachment_common_types that is not extracted by sqgetGlobalVar() ?
52 if (isset($attachment_common_types)) {
53 // var is used to detect activation of jpeg image types
55 /* Don't run this before being logged in. That may happen
56 when plugins include mime.php */
57 foreach ($attachment_common_types as $val => $v) {
58 if ($val == 'image/gif')
59 register_attachment_common('image/gif', 'link_image');
60 elseif (($val == 'image/jpeg' ||
$val == 'image/pjpeg' ||
$val == 'image/jpg') and
61 (!isset($jpeg_done))) {
63 register_attachment_common('image/jpg', 'link_image');
64 register_attachment_common('image/jpeg', 'link_image');
65 register_attachment_common('image/pjpeg', 'link_image');
67 elseif ($val == 'image/png')
68 register_attachment_common('image/png', 'link_image');
69 elseif ($val == 'image/x-xbitmap')
70 register_attachment_common('image/x-xbitmap', 'link_image');
71 elseif ($val == '*/*' ||
$val == 'image/*') {
73 * browser (Firefox) declared that anything is acceptable.
74 * Lets register some common image types.
76 if (! isset($jpeg_done)) {
78 register_attachment_common('image/jpg', 'link_image');
79 register_attachment_common('image/jpeg', 'link_image');
80 register_attachment_common('image/pjpeg', 'link_image');
82 register_attachment_common('image/gif', 'link_image');
83 register_attachment_common('image/png', 'link_image');
84 register_attachment_common('image/x-xbitmap', 'link_image');
85 // register_attachment_common('image/x-ico', 'link_image');
86 // register_attachment_common('image/x-icon', 'link_image');
87 // register_attachment_common('image/bmp', 'link_image');
88 // register_attachment_common('image/x-ms-bmp', 'link_image');
94 /* Register text-type attachments */
95 register_attachment_common('message/rfc822', 'link_message');
96 register_attachment_common('text/plain', 'link_text');
97 register_attachment_common('text/richtext', 'link_text');
100 register_attachment_common('text/html', 'link_html');
102 /* Register vcards */
103 register_attachment_common('text/x-vcard', 'link_vcard');
104 register_attachment_common('text/directory', 'link_vcard');
106 /* Register rules for general types.
107 * These will be used if there isn't a more specific rule available. */
108 register_attachment_common('text/*', 'link_text');
109 register_attachment_common('message/*', 'link_text');
111 /* Register "unknown" attachments */
112 register_attachment_common('application/octet-stream', 'octet_stream');
116 * Function which optimizes readability of the above code
117 * Registers 'attachment $type' hooks.
118 * @param string $type attachment type
119 * @param string $func suffix of attachment_common_* function, which handles $type attachments.
122 function register_attachment_common($type, $func) {
123 global $squirrelmail_plugin_hooks;
124 $squirrelmail_plugin_hooks['attachment ' . $type]['attachment_common'] =
125 'attachment_common_' . $func;
129 * Adds href and text keys to attachment_common array for text attachments
130 * @param array $Args attachment $type hook arguments
133 function attachment_common_link_text(&$Args) {
134 /* If there is a text attachment, we would like to create a "View" button
135 that links to the text attachment viewer.
137 $Args[1] = the array of actions
139 Use the name of this file for adding an action
140 $Args[1]['attachment_common'] = Array for href and text
142 $Args[1]['attachment_common']['text'] = What is displayed
143 $Args[1]['attachment_common']['href'] = Where it links to */
144 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER
);
146 // if htmlspecialchars() breaks something - find other way to encode & in url.
147 $Args[1]['attachment_common']['href'] = sqm_baseuri() . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING);
148 $Args[1]['attachment_common']['href'] =
149 set_url_var($Args[1]['attachment_common']['href'],
152 /* The link that we created needs a name. */
153 $Args[1]['attachment_common']['text'] = _("View");
155 /* Each attachment has a filename on the left, which is a link.
156 Where that link points to can be changed. Just in case the link above
157 for viewing text attachments is not the same as the default link for
158 this file, we'll change it.
160 This is a lot better in the image links, since the defaultLink will just
161 download the image, but the one that we set it to will format the page
162 to have an image tag in the center (looking a lot like this text viewer) */
163 $Args[6] = $Args[1]['attachment_common']['href'];
167 * Adds href and text keys to attachment_common array for rfc822 attachments
168 * @param array $Args attachment $type hook arguments
171 function attachment_common_link_message(&$Args) {
172 $Args[1]['attachment_common']['href'] = sqm_baseuri() . 'src/read_body.php?startMessage=' .
173 $Args[2] . '&passed_id=' . $Args[3] . '&mailbox=' . $Args[4] .
174 '&passed_ent_id=' . $Args[5] . '&override_type0=message&override_type1=rfc822';
176 $Args[1]['attachment_common']['text'] = _("View");
178 $Args[6] = $Args[1]['attachment_common']['href'];
182 * Adds href and text keys to attachment_common array for html attachments
183 * @param array $Args attachment $type hook arguments
186 function attachment_common_link_html(&$Args) {
187 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER
);
189 $Args[1]['attachment_common']['href'] = sqm_baseuri() . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING).
190 /* why use the overridetype? can this be removed */
191 /* override_type might be needed only when we want view other type of messages as html */
192 '&override_type0=text&override_type1=html';
193 $Args[1]['attachment_common']['href'] =
194 set_url_var($Args[1]['attachment_common']['href'],
197 $Args[1]['attachment_common']['text'] = _("View");
199 $Args[6] = $Args[1]['attachment_common']['href'];
203 * Adds href and text keys to attachment_common array for image attachments
204 * @param array $Args attachment $type hook arguments
207 function attachment_common_link_image(&$Args) {
208 global $attachment_common_show_images_list;
210 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER
);
212 $info['passed_id'] = $Args[3];
213 $info['mailbox'] = $Args[4];
214 $info['ent_id'] = $Args[5];
216 $attachment_common_show_images_list[] = $info;
218 $Args[1]['attachment_common']['href'] = sqm_baseuri() . 'src/image.php?'. htmlspecialchars($QUERY_STRING);
219 $Args[1]['attachment_common']['href'] =
220 set_url_var($Args[1]['attachment_common']['href'],
223 $Args[1]['attachment_common']['text'] = _("View");
225 $Args[6] = $Args[1]['attachment_common']['href'];
229 * Adds href and text keys to attachment_common array for vcard attachments
230 * @param array $Args attachment $type hook arguments
233 function attachment_common_link_vcard(&$Args) {
234 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER
);
236 $Args[1]['attachment_common']['href'] = sqm_baseuri() . 'src/vcard.php?'. htmlspecialchars($QUERY_STRING);
237 $Args[1]['attachment_common']['href'] =
238 set_url_var($Args[1]['attachment_common']['href'],
241 $Args[1]['attachment_common']['text'] = _("View Business Card");
243 $Args[6] = $Args[1]['attachment_common']['href'];
247 * Processes octet-stream attachments.
248 * Calls attachment_common-load_mime_types and attachment $type hooks.
249 * @param array $Args attachment $type hook arguments
252 function attachment_common_octet_stream(&$Args) {
253 global $FileExtensionToMimeType;
255 do_hook('attachment_common-load_mime_types');
257 ereg('\\.([^\\.]+)$', $Args[7], $Regs);
259 $Ext = strtolower($Regs[1]);
261 if ($Ext == '' ||
! isset($FileExtensionToMimeType[$Ext]))
264 $Ret = do_hook('attachment ' . $FileExtensionToMimeType[$Ext],
265 $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6],
266 $Args[7], $Args[8], $Args[9]);
268 foreach ($Ret as $a => $b) {