removing trailing ?> from function scripts
[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 *
47ccfad4 8 * @copyright &copy; 1999-2006 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
3169f064 15
4691e788 16/**
17 * Mapping of file extensions to mime types
18 *
19 * Used for application/octet-stream mime type detection.
20 * Supported extensions: bmp, gif, htm, html, jpg, jpeg, php,
21 * png, rtf, txt, patch (since 1.4.2), vcf
22 * @global array $FileExtensionToMimeType
23 */
5bc6067b 24$FileExtensionToMimeType = array('bmp' => 'image/x-bitmap',
25 'gif' => 'image/gif',
26 'htm' => 'text/html',
27 'html' => 'text/html',
6cbe465c 28 'jpe' => 'image/jpeg',
5bc6067b 29 'jpg' => 'image/jpeg',
30 'jpeg' => 'image/jpeg',
31 'php' => 'text/plain',
32 'png' => 'image/png',
33 'rtf' => 'text/richtext',
34 'txt' => 'text/plain',
ab8b558f 35 'patch'=> 'text/plain',
5bc6067b 36 'vcf' => 'text/x-vcard');
37
38/* Register browser-supported image types */
b455793d 39sqgetGlobalVar('attachment_common_types', $attachment_common_types);
4691e788 40// FIXME: do we use $attachment_common_types that is not extracted by sqgetGlobalVar() ?
5bc6067b 41if (isset($attachment_common_types)) {
4691e788 42 // var is used to detect activation of jpeg image types
43 unset($jpeg_done);
5bc6067b 44 /* Don't run this before being logged in. That may happen
45 when plugins include mime.php */
46 foreach ($attachment_common_types as $val => $v) {
7baf86a9 47 if ($val == 'image/gif')
5bc6067b 48 register_attachment_common('image/gif', 'link_image');
6cbe465c 49 elseif (($val == 'image/jpeg' || $val == 'image/pjpeg' || $val == 'image/jpg') and
7baf86a9 50 (!isset($jpeg_done))) {
5bc6067b 51 $jpeg_done = 1;
6cbe465c 52 register_attachment_common('image/jpg', 'link_image');
5bc6067b 53 register_attachment_common('image/jpeg', 'link_image');
54 register_attachment_common('image/pjpeg', 'link_image');
7baf86a9 55 }
56 elseif ($val == 'image/png')
5bc6067b 57 register_attachment_common('image/png', 'link_image');
7baf86a9 58 elseif ($val == 'image/x-xbitmap')
5bc6067b 59 register_attachment_common('image/x-xbitmap', 'link_image');
4691e788 60 elseif ($val == '*/*' || $val == 'image/*') {
61 /**
202bcbcc 62 * browser (Firefox) declared that anything is acceptable.
4691e788 63 * Lets register some common image types.
64 */
65 if (! isset($jpeg_done)) {
66 $jpeg_done = 1;
6cbe465c 67 register_attachment_common('image/jpg', 'link_image');
4691e788 68 register_attachment_common('image/jpeg', 'link_image');
69 register_attachment_common('image/pjpeg', 'link_image');
4691e788 70 }
71 register_attachment_common('image/gif', 'link_image');
72 register_attachment_common('image/png', 'link_image');
73 register_attachment_common('image/x-xbitmap', 'link_image');
74 // register_attachment_common('image/x-ico', 'link_image');
75 // register_attachment_common('image/x-icon', 'link_image');
76 // register_attachment_common('image/bmp', 'link_image');
77 // register_attachment_common('image/x-ms-bmp', 'link_image');
78 }
5bc6067b 79 }
80 unset($jpeg_done);
81}
7baf86a9 82
5bc6067b 83/* Register text-type attachments */
679f13b7 84register_attachment_common('message/rfc822', 'link_message');
5bc6067b 85register_attachment_common('text/plain', 'link_text');
86register_attachment_common('text/richtext', 'link_text');
7baf86a9 87
5bc6067b 88/* Register HTML */
89register_attachment_common('text/html', 'link_html');
7baf86a9 90
5bc6067b 91/* Register vcards */
92register_attachment_common('text/x-vcard', 'link_vcard');
35036cf9 93register_attachment_common('text/directory', 'link_vcard');
7baf86a9 94
ae2f65a9 95/* Register rules for general types.
96 * These will be used if there isn't a more specific rule available. */
97register_attachment_common('text/*', 'link_text');
98register_attachment_common('message/*', 'link_text');
99
5bc6067b 100/* Register "unknown" attachments */
101register_attachment_common('application/octet-stream', 'octet_stream');
7baf86a9 102
103
3169f064 104/**
105 * Function which optimizes readability of the above code
106 * Registers 'attachment $type' hooks.
107 * @param string $type attachment type
108 * @param string $func suffix of attachment_common_* function, which handles $type attachments.
109 * @since 1.2.0
110 */
7baf86a9 111function register_attachment_common($type, $func) {
5bc6067b 112 global $squirrelmail_plugin_hooks;
113 $squirrelmail_plugin_hooks['attachment ' . $type]['attachment_common'] =
7baf86a9 114 'attachment_common_' . $func;
115}
116
3169f064 117/**
118 * Adds href and text keys to attachment_common array for text attachments
119 * @param array $Args attachment $type hook arguments
120 * @since 1.2.0
121 */
21dab2dc 122function attachment_common_link_text(&$Args) {
202bcbcc 123 global $base_uri;
21dab2dc 124 /* If there is a text attachment, we would like to create a "View" button
5bc6067b 125 that links to the text attachment viewer.
62f7daa5 126
5bc6067b 127 $Args[1] = the array of actions
62f7daa5 128
21dab2dc 129 Use the name of this file for adding an action
130 $Args[1]['attachment_common'] = Array for href and text
62f7daa5 131
5bc6067b 132 $Args[1]['attachment_common']['text'] = What is displayed
21dab2dc 133 $Args[1]['attachment_common']['href'] = Where it links to */
961ca3d8 134 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
846a3a21 135
3169f064 136 // if htmlspecialchars() breaks something - find other way to encode & in url.
202bcbcc 137 $Args[1]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING);
98eb617f 138 $Args[1]['attachment_common']['href'] =
62f7daa5 139 set_url_var($Args[1]['attachment_common']['href'],
21dab2dc 140 'ent_id',$Args[5]);
62f7daa5 141
21dab2dc 142 /* The link that we created needs a name. */
143 $Args[1]['attachment_common']['text'] = _("View");
679f13b7 144
5bc6067b 145 /* Each attachment has a filename on the left, which is a link.
146 Where that link points to can be changed. Just in case the link above
147 for viewing text attachments is not the same as the default link for
148 this file, we'll change it.
62f7daa5 149
5bc6067b 150 This is a lot better in the image links, since the defaultLink will just
151 download the image, but the one that we set it to will format the page
152 to have an image tag in the center (looking a lot like this text viewer) */
153 $Args[6] = $Args[1]['attachment_common']['href'];
7baf86a9 154}
155
3169f064 156/**
157 * Adds href and text keys to attachment_common array for rfc822 attachments
158 * @param array $Args attachment $type hook arguments
159 * @since 1.2.6
160 */
21dab2dc 161function attachment_common_link_message(&$Args) {
202bcbcc 162 global $base_uri;
163 $Args[1]['attachment_common']['href'] = $base_uri . 'src/read_body.php?startMessage=' .
679f13b7 164 $Args[2] . '&amp;passed_id=' . $Args[3] . '&amp;mailbox=' . $Args[4] .
165 '&amp;passed_ent_id=' . $Args[5] . '&amp;override_type0=message&amp;override_type1=rfc822';
21dab2dc 166
167 $Args[1]['attachment_common']['text'] = _("View");
f4c8a5ab 168
62f7daa5 169 $Args[6] = $Args[1]['attachment_common']['href'];
679f13b7 170}
171
3169f064 172/**
173 * Adds href and text keys to attachment_common array for html attachments
174 * @param array $Args attachment $type hook arguments
175 * @since 1.2.0
176 */
21dab2dc 177function attachment_common_link_html(&$Args) {
202bcbcc 178 global $base_uri;
961ca3d8 179 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
846a3a21 180
202bcbcc 181 $Args[1]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING).
3169f064 182 /* why use the overridetype? can this be removed */
183 /* override_type might be needed only when we want view other type of messages as html */
47f9a69d 184 '&amp;override_type0=text&amp;override_type1=html';
185 $Args[1]['attachment_common']['href'] =
62f7daa5 186 set_url_var($Args[1]['attachment_common']['href'],
21dab2dc 187 'ent_id',$Args[5]);
5bc6067b 188
21dab2dc 189 $Args[1]['attachment_common']['text'] = _("View");
5bc6067b 190
191 $Args[6] = $Args[1]['attachment_common']['href'];
7baf86a9 192}
193
3169f064 194/**
f8a1ed5a 195 * Adds href and text keys to attachment_common array for image attachments
3169f064 196 * @param array $Args attachment $type hook arguments
197 * @since 1.2.0
198 */
21dab2dc 199function attachment_common_link_image(&$Args) {
202bcbcc 200 global $attachment_common_show_images_list, $base_uri ;
846a3a21 201
961ca3d8 202 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
62f7daa5 203
5bc6067b 204 $info['passed_id'] = $Args[3];
205 $info['mailbox'] = $Args[4];
206 $info['ent_id'] = $Args[5];
62f7daa5 207
5bc6067b 208 $attachment_common_show_images_list[] = $info;
62f7daa5 209
202bcbcc 210 $Args[1]['attachment_common']['href'] = $base_uri . 'src/image.php?'. htmlspecialchars($QUERY_STRING);
ff9d4297 211 $Args[1]['attachment_common']['href'] =
62f7daa5 212 set_url_var($Args[1]['attachment_common']['href'],
21dab2dc 213 'ent_id',$Args[5]);
62f7daa5 214
21dab2dc 215 $Args[1]['attachment_common']['text'] = _("View");
62f7daa5 216
5bc6067b 217 $Args[6] = $Args[1]['attachment_common']['href'];
7baf86a9 218}
219
3169f064 220/**
f8a1ed5a 221 * Adds href and text keys to attachment_common array for vcard attachments
3169f064 222 * @param array $Args attachment $type hook arguments
223 * @since 1.2.0
224 */
21dab2dc 225function attachment_common_link_vcard(&$Args) {
202bcbcc 226 global $base_uri;
961ca3d8 227 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
62f7daa5 228
202bcbcc 229 $Args[1]['attachment_common']['href'] = $base_uri . 'src/vcard.php?'. htmlspecialchars($QUERY_STRING);
ff9d4297 230 $Args[1]['attachment_common']['href'] =
62f7daa5 231 set_url_var($Args[1]['attachment_common']['href'],
21dab2dc 232 'ent_id',$Args[5]);
62f7daa5 233
c9fec394 234 $Args[1]['attachment_common']['text'] = _("View Business Card");
62f7daa5 235
5bc6067b 236 $Args[6] = $Args[1]['attachment_common']['href'];
7baf86a9 237}
238
3169f064 239/**
240 * Processes octet-stream attachments.
f8a1ed5a 241 * Calls attachment_common-load_mime_types and attachment $type hooks.
3169f064 242 * @param array $Args attachment $type hook arguments
243 * @since 1.2.0
244 */
21dab2dc 245function attachment_common_octet_stream(&$Args) {
5bc6067b 246 global $FileExtensionToMimeType;
62f7daa5 247
5bc6067b 248 do_hook('attachment_common-load_mime_types');
62f7daa5 249
5bc6067b 250 ereg('\\.([^\\.]+)$', $Args[7], $Regs);
62f7daa5 251
5bc6067b 252 $Ext = strtolower($Regs[1]);
62f7daa5 253
5bc6067b 254 if ($Ext == '' || ! isset($FileExtensionToMimeType[$Ext]))
62f7daa5 255 return;
256
257 $Ret = do_hook('attachment ' . $FileExtensionToMimeType[$Ext],
258 $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6],
5bc6067b 259 $Args[7], $Args[8], $Args[9]);
62f7daa5 260
5bc6067b 261 foreach ($Ret as $a => $b) {
262 $Args[$a] = $b;
263 }
7baf86a9 264}