Minor cleanups and changing the links for downloading/viewing attachments
[squirrelmail.git] / functions / attachment_common.php
1 <?php
2
3 /**
4 * attachment_common.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This file provides the handling of often-used attachment types.
10 *
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /**
16 * FIXME Needs phpDocumentator style documentation
17 */
18 require_once(SM_PATH . 'functions/global.php');
19
20 global $attachment_common_show_images_list;
21 $attachment_common_show_images_list = array();
22
23 global $FileExtensionToMimeType, $attachment_common_types;
24 $FileExtensionToMimeType = array('bmp' => 'image/x-bitmap',
25 'gif' => 'image/gif',
26 'htm' => 'text/html',
27 'html' => 'text/html',
28 'jpg' => 'image/jpeg',
29 'jpeg' => 'image/jpeg',
30 'php' => 'text/plain',
31 'png' => 'image/png',
32 'rtf' => 'text/richtext',
33 'txt' => 'text/plain',
34 'vcf' => 'text/x-vcard');
35
36 /* Register browser-supported image types */
37 sqgetGlobalVar('attachment_common_types', $attachment_common_types);
38 if (isset($attachment_common_types)) {
39 /* Don't run this before being logged in. That may happen
40 when plugins include mime.php */
41 foreach ($attachment_common_types as $val => $v) {
42 if ($val == 'image/gif')
43 register_attachment_common('image/gif', 'link_image');
44 elseif (($val == 'image/jpeg' || $val == 'image/pjpeg') and
45 (!isset($jpeg_done))) {
46 $jpeg_done = 1;
47 register_attachment_common('image/jpeg', 'link_image');
48 register_attachment_common('image/pjpeg', 'link_image');
49 }
50 elseif ($val == 'image/png')
51 register_attachment_common('image/png', 'link_image');
52 elseif ($val == 'image/x-xbitmap')
53 register_attachment_common('image/x-xbitmap', 'link_image');
54 }
55 unset($jpeg_done);
56 }
57
58 /* Register text-type attachments */
59 register_attachment_common('message/rfc822', 'link_message');
60 register_attachment_common('text/plain', 'link_text');
61 register_attachment_common('text/richtext', 'link_text');
62
63 /* Register HTML */
64 register_attachment_common('text/html', 'link_html');
65
66
67 /* Register vcards */
68 register_attachment_common('text/x-vcard', 'link_vcard');
69 register_attachment_common('text/directory', 'link_vcard');
70
71 /* Register rules for general types.
72 * These will be used if there isn't a more specific rule available. */
73 register_attachment_common('text/*', 'link_text');
74 register_attachment_common('message/*', 'link_text');
75
76 /* Register "unknown" attachments */
77 register_attachment_common('application/octet-stream', 'octet_stream');
78
79
80 /* Function which optimizes readability of the above code */
81
82 function register_attachment_common($type, $func) {
83 global $squirrelmail_plugin_hooks;
84 $squirrelmail_plugin_hooks['attachment ' . $type]['attachment_common'] =
85 'attachment_common_' . $func;
86 }
87
88
89 function attachment_common_link_text(&$Args) {
90 /* If there is a text attachment, we would like to create a "View" button
91 that links to the text attachment viewer.
92
93 $Args[1] = the array of actions
94
95 Use the name of this file for adding an action
96 $Args[1]['attachment_common'] = Array for href and text
97
98 $Args[1]['attachment_common']['text'] = What is displayed
99 $Args[1]['attachment_common']['href'] = Where it links to */
100 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
101
102 $Args[1]['attachment_common']['href'] = SM_PATH . 'src/view_text.php?'. $QUERY_STRING;
103 $Args[1]['attachment_common']['href'] =
104 set_url_var($Args[1]['attachment_common']['href'],
105 'ent_id',$Args[5]);
106
107 /* The link that we created needs a name. */
108 $Args[1]['attachment_common']['text'] = _("View");
109
110 /* Each attachment has a filename on the left, which is a link.
111 Where that link points to can be changed. Just in case the link above
112 for viewing text attachments is not the same as the default link for
113 this file, we'll change it.
114
115 This is a lot better in the image links, since the defaultLink will just
116 download the image, but the one that we set it to will format the page
117 to have an image tag in the center (looking a lot like this text viewer) */
118 $Args[6] = $Args[1]['attachment_common']['href'];
119 }
120
121 function attachment_common_link_message(&$Args) {
122 $Args[1]['attachment_common']['href'] = SM_PATH . 'src/read_body.php?startMessage=' .
123 $Args[2] . '&amp;passed_id=' . $Args[3] . '&amp;mailbox=' . $Args[4] .
124 '&amp;passed_ent_id=' . $Args[5] . '&amp;override_type0=message&amp;override_type1=rfc822';
125
126 $Args[1]['attachment_common']['text'] = _("View");
127
128 $Args[6] = $Args[1]['attachment_common']['href'];
129 }
130
131
132 function attachment_common_link_html(&$Args) {
133 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
134
135 $Args[1]['attachment_common']['href'] = SM_PATH . 'src/view_text.php?'. $QUERY_STRING.
136 /* why use the overridetype? can this be removed */
137 '&amp;override_type0=text&amp;override_type1=html';
138 $Args[1]['attachment_common']['href'] =
139 set_url_var($Args[1]['attachment_common']['href'],
140 'ent_id',$Args[5]);
141
142 $Args[1]['attachment_common']['text'] = _("View");
143
144 $Args[6] = $Args[1]['attachment_common']['href'];
145 }
146
147 function attachment_common_link_image(&$Args) {
148 global $attachment_common_show_images, $attachment_common_show_images_list;
149
150 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
151
152 $info['passed_id'] = $Args[3];
153 $info['mailbox'] = $Args[4];
154 $info['ent_id'] = $Args[5];
155
156 $attachment_common_show_images_list[] = $info;
157
158 $Args[1]['attachment_common']['href'] = SM_PATH . 'src/image.php?'. $QUERY_STRING;
159 $Args[1]['attachment_common']['href'] =
160 set_url_var($Args[1]['attachment_common']['href'],
161 'ent_id',$Args[5]);
162
163 $Args[1]['attachment_common']['text'] = _("View");
164
165 $Args[6] = $Args[1]['attachment_common']['href'];
166 }
167
168
169 function attachment_common_link_vcard(&$Args) {
170 sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
171
172 $Args[1]['attachment_common']['href'] = SM_PATH . 'src/vcard.php?'. $QUERY_STRING;
173 $Args[1]['attachment_common']['href'] =
174 set_url_var($Args[1]['attachment_common']['href'],
175 'ent_id',$Args[5]);
176
177 $Args[1]['attachment_common']['text'] = _("Business Card");
178
179 $Args[6] = $Args[1]['attachment_common']['href'];
180 }
181
182
183 function attachment_common_octet_stream(&$Args) {
184 global $FileExtensionToMimeType;
185
186 do_hook('attachment_common-load_mime_types');
187
188 ereg('\\.([^\\.]+)$', $Args[7], $Regs);
189
190 $Ext = strtolower($Regs[1]);
191
192 if ($Ext == '' || ! isset($FileExtensionToMimeType[$Ext]))
193 return;
194
195 $Ret = do_hook('attachment ' . $FileExtensionToMimeType[$Ext],
196 $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6],
197 $Args[7], $Args[8], $Args[9]);
198
199 foreach ($Ret as $a => $b) {
200 $Args[$a] = $b;
201 }
202 }
203
204 ?>