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