fix bug #481886 with anonymous hint from sourceforge trackers :-)
[squirrelmail.git] / functions / attachment_common.php
1 <?php
2 /**
3 ** attachment_common.php
4 **
5 ** This file provides the handling of often-used attachment types.
6 **
7 ** $Id$
8 **/
9
10 global $FileExtensionToMimeType, $attachment_common_types;
11 $FileExtensionToMimeType = array('bmp' => 'image/x-bitmap',
12 'gif' => 'image/gif',
13 'htm' => 'text/html',
14 'html' => 'text/html',
15 'jpg' => 'image/jpeg',
16 'jpeg' => 'image/jpeg',
17 'php' => 'text/plain',
18 'png' => 'image/png',
19 'rtf' => 'text/richtext',
20 'txt' => 'text/plain',
21 'vcf' => 'text/x-vcard');
22
23 // Register browser-supported image types
24 if (isset($attachment_common_types)) {
25 // Don't run this before being logged in. That may happen
26 // when plugins include mime.php
27 foreach ($attachment_common_types as $val => $v) {
28 if ($val == 'image/gif')
29 register_attachment_common('image/gif', 'link_image');
30 elseif (($val == 'image/jpeg' || $val == 'image/pjpeg') and
31 (!isset($jpeg_done))) {
32 $jpeg_done = 1;
33 register_attachment_common('image/jpeg', 'link_image');
34 register_attachment_common('image/pjpeg', 'link_image');
35 }
36 elseif ($val == 'image/png')
37 register_attachment_common('image/png', 'link_image');
38 elseif ($val == 'image/x-xbitmap')
39 register_attachment_common('image/x-xbitmap', 'link_image');
40 }
41 unset($jpeg_done);
42 }
43
44 // Register text-type attachments
45 register_attachment_common('message/rfc822', 'link_text');
46 register_attachment_common('text/plain', 'link_text');
47 register_attachment_common('text/richtext', 'link_text');
48
49 // Register HTML
50 register_attachment_common('text/html', 'link_html');
51
52 // Register vcards
53 register_attachment_common('text/x-vcard', 'link_vcard');
54
55 // Register "unknown" attachments
56 register_attachment_common('application/octet-stream', 'octet_stream');
57
58
59 /* Function which optimizes readability of the above code */
60
61 function register_attachment_common($type, $func) {
62 global $squirrelmail_plugin_hooks;
63 $squirrelmail_plugin_hooks['attachment ' . $type]['attachment_common'] =
64 'attachment_common_' . $func;
65 }
66
67
68 function attachment_common_link_text(&$Args)
69 {
70 // If there is a text attachment, we would like to create a 'view' button
71 // that links to the text attachment viewer.
72 //
73 // $Args[1] = the array of actions
74 //
75 // Use our plugin name for adding an action
76 // $Args[1]['attachment_common'] = array for href and text
77 //
78 // $Args[1]['attachment_common']['text'] = What is displayed
79 // $Args[1]['attachment_common']['href'] = Where it links to
80 //
81 // This sets the 'href' of this plugin for a new link.
82 $Args[1]['attachment_common']['href'] = '../src/download.php?startMessage=' .
83 $Args[2] . '&passed_id=' . $Args[3] . '&mailbox=' . $Args[4] .
84 '&passed_ent_id=' . $Args[5] . '&override_type0=text&override_type1=plain';
85
86 // If we got here from a search, we should preserve these variables
87 if ($Args[8] && $Args[9])
88 $Args[1]['attachment_common']['href'] .= '&where=' .
89 urlencode($Args[8]) . '&what=' . urlencode($Args[9]);
90
91 // The link that we created needs a name. "view" will be displayed for
92 // all text attachments handled by this plugin.
93 $Args[1]['attachment_common']['text'] = _("view");
94
95 // Each attachment has a filename on the left, which is a link.
96 // Where that link points to can be changed. Just in case the link above
97 // for viewing text attachments is not the same as the default link for
98 // this file, we'll change it.
99 //
100 // This is a lot better in the image links, since the defaultLink will just
101 // download the image, but the one that we set it to will format the page
102 // to have an image tag in the center (looking a lot like this text viewer)
103 $Args[6] = $Args[1]['attachment_common']['href'];
104 }
105
106
107 function attachment_common_link_html(&$Args)
108 {
109 $Args[1]['attachment_common']['href'] = '../src/download.php?startMessage=' .
110 $Args[2] . '&passed_id=' . $Args[3] . '&mailbox=' . $Args[4] .
111 '&passed_ent_id=' . $Args[5] . '&override_type0=text&override_type1=html';
112
113 if ($Args[8] && $Args[9])
114 $Args[1]['attachment_common']['href'] .= '&where=' .
115 urlencode($Args[8]) . '&what=' . urlencode($Args[9]);
116
117 $Args[1]['attachment_common']['text'] = _("view");
118
119 $Args[6] = $Args[1]['attachment_common']['href'];
120 }
121
122
123 function attachment_common_link_image(&$Args)
124 {
125 global $attachment_common_show_images, $attachment_common_show_images_list;
126
127 $info['passed_id'] = $Args[3];
128 $info['mailbox'] = $Args[4];
129 $info['ent_id'] = $Args[5];
130
131 $attachment_common_show_images_list[] = $info;
132
133 $Args[1]['attachment_common']['href'] = '../src/image.php?startMessage=' .
134 $Args[2] . '&passed_id=' . $Args[3] . '&mailbox=' . $Args[4] .
135 '&passed_ent_id=' . $Args[5];
136
137 if ($Args[8] && $Args[9])
138 $Args[1]['attachment_common']['href'] .= '&where=' .
139 urlencode($Args[8]) . '&what=' . urlencode($Args[9]);
140
141 $Args[1]['attachment_common']['text'] = _("view");
142
143 $Args[6] = $Args[1]['attachment_common']['href'];
144 }
145
146
147 function attachment_common_link_vcard(&$Args)
148 {
149 $Args[1]['attachment_common']['href'] = '../src/vcard.php?startMessage=' .
150 $Args[2] . '&passed_id=' . $Args[3] . '&mailbox=' . $Args[4] .
151 '&passed_ent_id=' . $Args[5];
152
153 if (isset($where) && isset($what))
154 $Args[1]['attachment_common']['href'] .= '&where=' .
155 urlencode($Args[8]) . '&what=' . urlencode($Args[9]);
156
157 $Args[1]['attachment_common']['text'] = _("Business Card");
158
159 $Args[6] = $Args[1]['attachment_common']['href'];
160 }
161
162
163 function attachment_common_octet_stream(&$Args)
164 {
165 global $FileExtensionToMimeType;
166
167 do_hook('attachment_common-load_mime_types');
168
169 ereg('\\.([^\\.]+)$', $Args[7], $Regs);
170
171 $Ext = strtolower($Regs[1]);
172
173 if ($Ext == '' || ! isset($FileExtensionToMimeType[$Ext]))
174 return;
175
176 $Ret = do_hook('attachment ' . $FileExtensionToMimeType[$Ext],
177 $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6],
178 $Args[7], $Args[8], $Args[9]);
179
180 foreach ($Ret as $a => $b) {
181 $Args[$a] = $b;
182 }
183 }
184
185 ?>