Avoid E_STRICT errors
[squirrelmail.git] / functions / attachment_common.php
CommitLineData
7baf86a9 1<?php
7350889b 2
35586184 3/**
4 * attachment_common.php
5 *
35586184 6 * This file provides the handling of often-used attachment types.
7 *
4b5049de 8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
4b4abf93 9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 10 * @version $Id$
d6c32258 11 * @package squirrelmail
3169f064 12 * @todo document attachment $type hook arguments
35586184 13 */
7baf86a9 14
fdbac73b 15$attachment_common_show_images_list = array();
3169f064 16
4691e788 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 */
5bc6067b 25$FileExtensionToMimeType = array('bmp' => 'image/x-bitmap',
26 'gif' => 'image/gif',
27 'htm' => 'text/html',
28 'html' => 'text/html',
6cbe465c 29 'jpe' => 'image/jpeg',
5bc6067b 30 'jpg' => 'image/jpeg',
31 'jpeg' => 'image/jpeg',
32 'php' => 'text/plain',
33 'png' => 'image/png',
34 'rtf' => 'text/richtext',
35 'txt' => 'text/plain',
ab8b558f 36 'patch'=> 'text/plain',
5bc6067b 37 'vcf' => 'text/x-vcard');
38
39/* Register browser-supported image types */
b455793d 40sqgetGlobalVar('attachment_common_types', $attachment_common_types);
4691e788 41// FIXME: do we use $attachment_common_types that is not extracted by sqgetGlobalVar() ?
5bc6067b 42if (isset($attachment_common_types)) {
4691e788 43 // var is used to detect activation of jpeg image types
44 unset($jpeg_done);
5bc6067b 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) {
7baf86a9 48 if ($val == 'image/gif')
5bc6067b 49 register_attachment_common('image/gif', 'link_image');
6cbe465c 50 elseif (($val == 'image/jpeg' || $val == 'image/pjpeg' || $val == 'image/jpg') and
7baf86a9 51 (!isset($jpeg_done))) {
5bc6067b 52 $jpeg_done = 1;
6cbe465c 53 register_attachment_common('image/jpg', 'link_image');
5bc6067b 54 register_attachment_common('image/jpeg', 'link_image');
55 register_attachment_common('image/pjpeg', 'link_image');
7baf86a9 56 }
57 elseif ($val == 'image/png')
5bc6067b 58 register_attachment_common('image/png', 'link_image');
7baf86a9 59 elseif ($val == 'image/x-xbitmap')
5bc6067b 60 register_attachment_common('image/x-xbitmap', 'link_image');
4691e788 61 elseif ($val == '*/*' || $val == 'image/*') {
62 /**
202bcbcc 63 * browser (Firefox) declared that anything is acceptable.
4691e788 64 * Lets register some common image types.
65 */
66 if (! isset($jpeg_done)) {
67 $jpeg_done = 1;
6cbe465c 68 register_attachment_common('image/jpg', 'link_image');
4691e788 69 register_attachment_common('image/jpeg', 'link_image');
70 register_attachment_common('image/pjpeg', 'link_image');
4691e788 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 }
5bc6067b 80 }
81 unset($jpeg_done);
82}
7baf86a9 83
5bc6067b 84/* Register text-type attachments */
679f13b7 85register_attachment_common('message/rfc822', 'link_message');
5bc6067b 86register_attachment_common('text/plain', 'link_text');
87register_attachment_common('text/richtext', 'link_text');
7baf86a9 88
5bc6067b 89/* Register HTML */
90register_attachment_common('text/html', 'link_html');
7baf86a9 91
5bc6067b 92/* Register vcards */
93register_attachment_common('text/x-vcard', 'link_vcard');
35036cf9 94register_attachment_common('text/directory', 'link_vcard');
7baf86a9 95
ae2f65a9 96/* Register rules for general types.
97 * These will be used if there isn't a more specific rule available. */
98register_attachment_common('text/*', 'link_text');
99register_attachment_common('message/*', 'link_text');
100
5bc6067b 101/* Register "unknown" attachments */
102register_attachment_common('application/octet-stream', 'octet_stream');
7baf86a9 103
104
3169f064 105/**
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.
110 * @since 1.2.0
111 */
7baf86a9 112function register_attachment_common($type, $func) {
5bc6067b 113 global $squirrelmail_plugin_hooks;
114 $squirrelmail_plugin_hooks['attachment ' . $type]['attachment_common'] =
7baf86a9 115 'attachment_common_' . $func;
116}
117
3169f064 118/**
119 * Adds href and text keys to attachment_common array for text attachments
120 * @param array $Args attachment $type hook arguments
121 * @since 1.2.0
122 */
21dab2dc 123function attachment_common_link_text(&$Args) {
202bcbcc 124 global $base_uri;
21dab2dc 125 /* If there is a text attachment, we would like to create a "View" button
5bc6067b 126 that links to the text attachment viewer.
62f7daa5 127
d849b570 128 $Args[0] = the array of actions
62f7daa5 129
21dab2dc 130 Use the name of this file for adding an action
d849b570 131 $Args[0]['attachment_common'] = Array for href and text
62f7daa5 132
d849b570 133 $Args[0]['attachment_common']['text'] = What is displayed
134 $Args[0]['attachment_common']['href'] = Where it links to */
961ca3d8 135 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
846a3a21 136
3169f064 137 // if htmlspecialchars() breaks something - find other way to encode & in url.
d849b570 138 $Args[0]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING);
139 $Args[0]['attachment_common']['href'] =
140 set_url_var($Args[0]['attachment_common']['href'],
141 'ent_id',$Args[4]);
62f7daa5 142
21dab2dc 143 /* The link that we created needs a name. */
d849b570 144 $Args[0]['attachment_common']['text'] = _("View");
679f13b7 145
5bc6067b 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.
62f7daa5 150
5bc6067b 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) */
d849b570 154 $Args[5] = $Args[1]['attachment_common']['href'];
7baf86a9 155}
156
3169f064 157/**
158 * Adds href and text keys to attachment_common array for rfc822 attachments
159 * @param array $Args attachment $type hook arguments
160 * @since 1.2.6
161 */
21dab2dc 162function attachment_common_link_message(&$Args) {
202bcbcc 163 global $base_uri;
d849b570 164 $Args[0]['attachment_common']['href'] = $base_uri . 'src/read_body.php?startMessage=' .
165 $Args[1] . '&amp;passed_id=' . $Args[2] . '&amp;mailbox=' . $Args[3] .
166 '&amp;passed_ent_id=' . $Args[4] . '&amp;override_type0=message&amp;override_type1=rfc822';
21dab2dc 167
d849b570 168 $Args[0]['attachment_common']['text'] = _("View");
f4c8a5ab 169
d849b570 170 $Args[5] = $Args[0]['attachment_common']['href'];
679f13b7 171}
172
3169f064 173/**
174 * Adds href and text keys to attachment_common array for html attachments
175 * @param array $Args attachment $type hook arguments
176 * @since 1.2.0
177 */
21dab2dc 178function attachment_common_link_html(&$Args) {
202bcbcc 179 global $base_uri;
961ca3d8 180 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
846a3a21 181
d849b570 182 $Args[0]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING).
3169f064 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 */
47f9a69d 185 '&amp;override_type0=text&amp;override_type1=html';
d849b570 186 $Args[0]['attachment_common']['href'] =
187 set_url_var($Args[0]['attachment_common']['href'],
188 'ent_id',$Args[4]);
5bc6067b 189
d849b570 190 $Args[0]['attachment_common']['text'] = _("View");
5bc6067b 191
d849b570 192 $Args[5] = $Args[0]['attachment_common']['href'];
7baf86a9 193}
194
3169f064 195/**
f8a1ed5a 196 * Adds href and text keys to attachment_common array for image attachments
3169f064 197 * @param array $Args attachment $type hook arguments
198 * @since 1.2.0
199 */
21dab2dc 200function attachment_common_link_image(&$Args) {
202bcbcc 201 global $attachment_common_show_images_list, $base_uri ;
846a3a21 202
961ca3d8 203 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
62f7daa5 204
d849b570 205 $info['passed_id'] = $Args[2];
206 $info['mailbox'] = $Args[3];
207 $info['ent_id'] = $Args[4];
208 $info['name'] = $Args[6];
209 $info['download_href'] = isset($Args[0]['download link']) ? $Args[0]['download link']['href'] : '';
d2dcc193 210
5bc6067b 211 $attachment_common_show_images_list[] = $info;
62f7daa5 212
d849b570 213 $Args[0]['attachment_common']['href'] = $base_uri . 'src/image.php?'. htmlspecialchars($QUERY_STRING);
214 $Args[0]['attachment_common']['href'] =
215 set_url_var($Args[0]['attachment_common']['href'],
216 'ent_id',$Args[4]);
62f7daa5 217
d849b570 218 $Args[0]['attachment_common']['text'] = _("View");
62f7daa5 219
d849b570 220 $Args[5] = $Args[0]['attachment_common']['href'];
7baf86a9 221}
222
3169f064 223/**
f8a1ed5a 224 * Adds href and text keys to attachment_common array for vcard attachments
3169f064 225 * @param array $Args attachment $type hook arguments
226 * @since 1.2.0
227 */
21dab2dc 228function attachment_common_link_vcard(&$Args) {
202bcbcc 229 global $base_uri;
961ca3d8 230 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
62f7daa5 231
d849b570 232 $Args[0]['attachment_common']['href'] = $base_uri . 'src/vcard.php?'. htmlspecialchars($QUERY_STRING);
233 $Args[0]['attachment_common']['href'] =
234 set_url_var($Args[0]['attachment_common']['href'],
235 'ent_id',$Args[4]);
62f7daa5 236
d849b570 237 $Args[0]['attachment_common']['text'] = _("View Business Card");
62f7daa5 238
d849b570 239 $Args[5] = $Args[0]['attachment_common']['href'];
7baf86a9 240}
241
3169f064 242/**
243 * Processes octet-stream attachments.
f8a1ed5a 244 * Calls attachment_common-load_mime_types and attachment $type hooks.
3169f064 245 * @param array $Args attachment $type hook arguments
246 * @since 1.2.0
247 */
21dab2dc 248function attachment_common_octet_stream(&$Args) {
d849b570 249 global $FileExtensionToMimeType, $null;
62f7daa5 250
d7c9c0d4 251//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.
d849b570 252 do_hook('attachment_common-load_mime_types', $null);
62f7daa5 253
d849b570 254 ereg('\\.([^\\.]+)$', $Args[6], $Regs);
62f7daa5 255
5bc6067b 256 $Ext = strtolower($Regs[1]);
62f7daa5 257
5bc6067b 258 if ($Ext == '' || ! isset($FileExtensionToMimeType[$Ext]))
62f7daa5 259 return;
260
d7c9c0d4 261 $temp=array(&$Args[0], &$Args[1], &$Args[2], &$Args[3], &$Args[4], &$Args[5],
262 &$Args[6], &$Args[7], &$Args[8]);
263 do_hook('attachment ' . $FileExtensionToMimeType[$Ext], $temp);
62f7daa5 264
7baf86a9 265}