747325d4f45caa1ecb6d5816a448eca32d17c0dc
[squirrelmail.git] / functions / attachment_common.php
1 <?php
2
3 /**
4 * attachment_common.php
5 *
6 * This file provides the handling of often-used attachment types.
7 *
8 * @copyright 1999-2019 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 * @todo document attachment $type hook arguments
13 */
14
15 $attachment_common_show_images_list = array();
16
17 /**
18 * Mapping of file extensions to mime types
19 *
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
24 */
25 $FileExtensionToMimeType = array('bmp' => 'image/x-bitmap',
26 'gif' => 'image/gif',
27 'htm' => 'text/html',
28 'html' => 'text/html',
29 'jpe' => 'image/jpeg',
30 'jpg' => 'image/jpeg',
31 'jpeg' => 'image/jpeg',
32 'php' => 'text/plain',
33 'png' => 'image/png',
34 'rtf' => 'text/richtext',
35 'txt' => 'text/plain',
36 'patch'=> 'text/plain',
37 'vcf' => 'text/x-vcard');
38
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
44 unset($jpeg_done);
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))) {
52 $jpeg_done = 1;
53 register_attachment_common('image/jpg', 'link_image');
54 register_attachment_common('image/jpeg', 'link_image');
55 register_attachment_common('image/pjpeg', 'link_image');
56 }
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/*') {
62 /**
63 * browser (Firefox) declared that anything is acceptable.
64 * Lets register some common image types.
65 */
66 if (! isset($jpeg_done)) {
67 $jpeg_done = 1;
68 register_attachment_common('image/jpg', 'link_image');
69 register_attachment_common('image/jpeg', 'link_image');
70 register_attachment_common('image/pjpeg', 'link_image');
71 }
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');
79 }
80 }
81 unset($jpeg_done);
82 }
83
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');
88
89 /* Register HTML */
90 register_attachment_common('text/html', 'link_html');
91
92 /* Register vcards */
93 register_attachment_common('text/x-vcard', 'link_vcard');
94 register_attachment_common('text/directory', 'link_vcard');
95
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');
100
101 /* Register "unknown" attachments */
102 register_attachment_common('application/octet-stream', 'octet_stream');
103
104
105 /**
106 * Function which optimizes readability of the above code, and also
107 * ensures that the attachment_common code is exectuted before any
108 * plugin, so that the latter may override the default processing.
109 *
110 * Registers 'attachment $type' hooks.
111 *
112 * @param string $type Attachment type
113 * @param string $func Suffix of attachment_common_* function, which
114 * handles $type attachments.
115 *
116 * @since 1.2.0
117 */
118 function register_attachment_common($type, $func) {
119 global $squirrelmail_plugin_hooks;
120 $plugin_type = 'attachment ' . $type;
121 $fn = 'attachment_common_' . $func;
122 if (!empty($squirrelmail_plugin_hooks[$plugin_type])) {
123 $plugins = $squirrelmail_plugin_hooks[$plugin_type];
124 $plugins = array_merge(array('attachment_common', $fn), $plugins);
125 $squirrelmail_plugin_hooks[$plugin_type] = $plugins;
126 } else {
127 $squirrelmail_plugin_hooks[$plugin_type]['attachment_common'] = $fn;
128 }
129 }
130
131 /**
132 * Adds href and text keys to attachment_common array for text attachments
133 * @param array $Args attachment $type hook arguments
134 * @since 1.2.0
135 */
136 function attachment_common_link_text(&$Args) {
137 global $base_uri;
138 /* If there is a text attachment, we would like to create a "View" button
139 that links to the text attachment viewer.
140
141 $Args[0] = the array of actions
142
143 Use the name of this file for adding an action
144 $Args[0]['attachment_common'] = Array for href and text
145
146 $Args[0]['attachment_common']['text'] = What is displayed
147 $Args[0]['attachment_common']['href'] = Where it links to */
148 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
149
150 // if sm_encode_html_special_chars() breaks something - find other way to encode & in url.
151 $Args[0]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. $QUERY_STRING;
152 $Args[0]['attachment_common']['href'] =
153 set_url_var($Args[0]['attachment_common']['href'],
154 'ent_id',$Args[4]);
155
156 /* The link that we created needs a name. */
157 $Args[0]['attachment_common']['text'] = _("View");
158
159 /* Each attachment has a filename on the left, which is a link.
160 Where that link points to can be changed. Just in case the link above
161 for viewing text attachments is not the same as the default link for
162 this file, we'll change it.
163
164 This is a lot better in the image links, since the defaultLink will just
165 download the image, but the one that we set it to will format the page
166 to have an image tag in the center (looking a lot like this text viewer) */
167 $Args[5] = $Args[0]['attachment_common']['href'];
168 }
169
170 /**
171 * Adds href and text keys to attachment_common array for rfc822 attachments
172 * @param array $Args attachment $type hook arguments
173 * @since 1.2.6
174 */
175 function attachment_common_link_message(&$Args) {
176 global $base_uri;
177 $Args[0]['attachment_common']['href'] = $base_uri . 'src/read_body.php?startMessage=' .
178 $Args[1] . '&amp;passed_id=' . $Args[2] . '&amp;mailbox=' . $Args[3] .
179 '&amp;passed_ent_id=' . $Args[4] . '&amp;override_type0=message&amp;override_type1=rfc822';
180
181 $Args[0]['attachment_common']['text'] = _("View");
182
183 $Args[5] = $Args[0]['attachment_common']['href'];
184 }
185
186 /**
187 * Adds href and text keys to attachment_common array for html attachments
188 * @param array $Args attachment $type hook arguments
189 * @since 1.2.0
190 */
191 function attachment_common_link_html(&$Args) {
192 global $base_uri;
193 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
194
195 $Args[0]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. $QUERY_STRING.
196 /* why use the overridetype? can this be removed */
197 /* override_type might be needed only when we want view other type of messages as html */
198 '&amp;override_type0=text&amp;override_type1=html';
199 $Args[0]['attachment_common']['href'] =
200 set_url_var($Args[0]['attachment_common']['href'],
201 'ent_id',$Args[4]);
202
203 $Args[0]['attachment_common']['text'] = _("View");
204
205 $Args[5] = $Args[0]['attachment_common']['href'];
206 }
207
208 /**
209 * Adds href and text keys to attachment_common array for image attachments
210 * @param array $Args attachment $type hook arguments
211 * @since 1.2.0
212 */
213 function attachment_common_link_image(&$Args) {
214 global $attachment_common_show_images_list, $base_uri ;
215
216 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
217
218 $info['passed_id'] = $Args[2];
219 $info['mailbox'] = $Args[3];
220 $info['ent_id'] = $Args[4];
221 $info['name'] = $Args[6];
222 $info['download_href'] = isset($Args[0]['download link']) ? $Args[0]['download link']['href'] : '';
223
224 $attachment_common_show_images_list[] = $info;
225
226 $Args[0]['attachment_common']['href'] = $base_uri . 'src/image.php?'. $QUERY_STRING;
227 $Args[0]['attachment_common']['href'] =
228 set_url_var($Args[0]['attachment_common']['href'],
229 'ent_id',$Args[4]);
230
231 $Args[0]['attachment_common']['text'] = _("View");
232
233 $Args[5] = $Args[0]['attachment_common']['href'];
234 }
235
236 /**
237 * Adds href and text keys to attachment_common array for vcard attachments
238 * @param array $Args attachment $type hook arguments
239 * @since 1.2.0
240 */
241 function attachment_common_link_vcard(&$Args) {
242 global $base_uri;
243 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
244
245 $Args[0]['attachment_common']['href'] = $base_uri . 'src/vcard.php?'. $QUERY_STRING;
246 $Args[0]['attachment_common']['href'] =
247 set_url_var($Args[0]['attachment_common']['href'],
248 'ent_id',$Args[4]);
249
250 $Args[0]['attachment_common']['text'] = _("View Business Card");
251
252 $Args[5] = $Args[0]['attachment_common']['href'];
253 }
254
255 /**
256 * Processes octet-stream attachments.
257 * Calls attachment_common-load_mime_types and attachment $type hooks.
258 * @param array $Args attachment $type hook arguments
259 * @since 1.2.0
260 */
261 function attachment_common_octet_stream(&$Args) {
262 global $FileExtensionToMimeType, $null;
263
264 //FIXME: I propose removing this hook; I don't like having two hooks close together, but moreover, this hook appears to merely give plugins the chance to add to the global $FileExtensionToMimeType variable, which they can do in any hook before now - I'd recommend prefs_backend (which is what config_override used to be) because it's the one hook run at the beginning of almost all page requests in init.php -- the con is that we don't need it run on ALL page requests, do we? There may be another hook in THIS page request that we can recommend, in which case, we *really should* remove this hook here.
265 //FIXME: or at least we can move this hook up to the top of this file where $FileExtensionToMimeType is defined. What else is this hook here for? What plugins use it?
266 do_hook('attachment_common-load_mime_types', $null);
267
268 preg_match('/\.([^.]+)$/', $Args[7], $Regs);
269
270 $Ext = '';
271 if (is_array($Regs) && isset($Regs[1])) {
272 $Ext = $Regs[1];
273 $Ext = strtolower($Regs[1]);
274 }
275
276 if ($Ext == '' || ! isset($FileExtensionToMimeType[$Ext]))
277 return;
278
279 $temp = array(&$Args[0], &$Args[1], &$Args[2], &$Args[3], &$Args[4], &$Args[5],
280 &$Args[6], &$Args[7], &$Args[8]);
281 do_hook('attachment ' . $FileExtensionToMimeType[$Ext], $temp);
282
283 }