/[drupal]/contributions/modules/mmb/mmb.module
ViewVC logotype

Contents of /contributions/modules/mmb/mmb.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.17 - (show annotations) (download) (as text)
Thu Aug 23 18:47:22 2007 UTC (2 years, 3 months ago) by sanduhrs
Branch: MAIN
CVS Tags: HEAD
Changes since 1.16: +2 -1 lines
File MIME type: text/x-php
creat image derivatives
1 <?php
2 /* $Id: mmb.module,v 1.16 2007/08/18 18:49:00 sanduhrs Exp $ */
3
4 /**
5 * @file
6 * Mobile Media Blog allows you to post media via e-mail or mobile phone.
7 *
8 * @author
9 * Stefan Auditor <stefan.auditor@erdfisch.de>
10 * for erdfisch :: internetlösungen http://erdfisch.de
11 */
12
13 /**
14 * Implementation of hook_mailhandler().
15 */
16 function mmb_mailhandler($node, $result, $i, $header, $mailbox) {
17 global $user;
18
19 //Check if user is allowed to upload files
20 if (!user_access('upload files')) {
21 watchdog('error', t("The user '%u' may not upload files.", array('%u' => $user->name ? $user->name : variable_get('anonymous', t('Anonymous')))));
22 return $node;
23 }
24
25 //Get e-mail attachments
26 $files = array();
27 mmb_get_parts($files, $result, $i, $structure = false, $part_number = false);
28
29 //Attach them to the node
30 $node->files = $files;
31
32 //Save the attachments
33 _mmb_validate($node);
34
35 //Handle different node types
36 switch ($node->type) {
37 case 'image':
38 if (!module_exists('image')) {
39 watchdog('mmb', t('Install and setup !image to post image files.', array('!image' => l('image.module', 'http://drupal.org/project/image', array(), NULL, NULL, TRUE))), WATCHDOG_WARNING);
40 break;
41 }
42 //Get the first attached image file
43 foreach($node->files as $key => $file) {
44 $mime = explode('/', $file['filemime']);
45 if (strtolower($mime[0]) == 'image') {
46 //Prepare for image.module
47 $node->images[IMAGE_ORIGINAL] = $node->files[$key]['filepath'];
48 _image_build_derivatives($node);
49 break;
50 }
51 }
52 break;
53 case 'audio':
54 if (!module_exists('audio') OR !module_exists('audio_getid3')) {
55 watchdog('mmb', t('Install and setup !audio and !audio_getid3 to post audio files.', array('!audio' => l('audio.module', 'http://drupal.org/project/audio', array(), NULL, NULL, TRUE), '!audio_getid3' => l('audio_getid3.module', 'http://drupal.org/project/audio', array(), NULL, NULL, TRUE),)), WATCHDOG_WARNING);
56 break;
57 }
58 //Get the first attached audio file
59 foreach($node->files as $key => $file) {
60 $mime = explode('/', $file['filemime']);
61 if (strtolower($mime[0]) == 'audio') {
62 //Prepare for audio.module
63 $node = audio_api_insert($node->files[$key]['filepath'], $title_format = NULL, $tags = array());
64 //TODO: Check what was replaced by audio_api_insert and restore it
65 break;
66 }
67 }
68 break;
69 case 'video':
70 //TODO: video.module integration?
71 //This already works with flashvideo.module whithout modification
72 break;
73 }
74
75 return $node;
76 }
77
78 /**
79 * Helper function to save a file temporarily
80 */
81 function mmb_save_part($filename, $data) {
82 $filename = file_create_filename($filename, file_create_path());
83 $file = file_save_data($data, $filename);
84
85 return $file;
86 }
87
88 /**
89 * Get all parts of the e-mail an save the attachments
90 */
91 function mmb_get_parts(&$files, $stream, $msg_number, $mime_type, $structure = false, $part_number = false) {
92 $mimetypes = array('text', 'multipart', 'message', 'application', 'audio', 'image', 'video', 'other');
93
94 if (!$structure) {
95 $structure = imap_fetchstructure($stream, $msg_number);
96 }
97 if ($structure) {
98 foreach ($structure->parameters as $parameter) {
99
100 if (strtoupper($parameter->attribute) == 'CHARSET') {
101 $encoding = $parameter->value;
102 }
103 }
104
105 if (!$part_number) {
106 $part_number = '1';
107 }
108
109 $index = explode('.', $part_number);
110 $data = imap_fetchbody($stream, $msg_number, $index[1]);
111
112 if ($structure->encoding == 3) {
113 $data = base64_decode($data);
114 }
115 else if ($structure->encoding == 4) {
116 $data = quoted_printable_decode($data);
117 }
118 else {
119 $data = $data;
120 }
121
122 if (strtolower($structure->dparameters[0]->attribute) == 'filename' AND $structure->dparameters[0]->value != '') {
123 $key = 'upload_'. (count($files)+1);
124 $filename = imap_utf8($structure->dparameters[0]->value);
125
126 $files[$key]['fid'] = $key;
127 $files[$key]['source'] = $key;
128 $files[$key]['filename'] = $filename;
129 $files[$key]['filepath'] = mmb_save_part($filename, $data);
130 $files[$key]['filemime'] = $mimetypes[strtolower($structure->type)] .'/'. strtolower($structure->subtype);
131 $files[$key]['filesize'] = strlen($data);
132 $files[$key]['list'] = variable_get('upload_list_default', 1);
133 }
134
135 if ($structure->type == 1) { /* multipart */
136 while (list($index, $sub_structure) = each ($structure->parts)) {
137 if ($part_number) {
138 $prefix = $part_number .'.';
139 }
140 mmb_get_parts($files, $stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));
141 }
142 }
143 }
144
145 return;
146 }
147
148 /**
149 * From upload.module
150 * Changed from form_set_error- to watchdog-messages
151 */
152 function _mmb_validate(&$node) {
153 // Accumulator for disk space quotas.
154 $filesize = 0;
155
156 // Check if node->files exists, and if it contains something.
157 if (is_array($node->files)) {
158 // Update existing files with form data.
159 foreach ($node->files as $fid => $file) {
160 // Convert file to object for compatibility
161 $file = (object)$file;
162
163 // Validate new uploads.
164 if (strpos($fid, 'upload') !== false && !$file->remove) {
165 global $user;
166
167 // Bypass validation for uid = 1.
168 if ($user->uid != 1) {
169 // Update filesize accumulator.
170 $filesize += $file->filesize;
171
172 // Validate file against all users roles.
173 // Only denies an upload when all roles prevent it.
174
175 $total_usersize = upload_space_used($user->uid) + $filesize;
176 $error = array();
177 foreach ($user->roles as $rid => $name) {
178 $extensions = variable_get("upload_extensions_$rid", 'jpg jpeg gif png txt html doc xls pdf ppt pps odt ods odp');
179 $uploadsize = variable_get("upload_uploadsize_$rid", 1) * 1024 * 1024;
180 $usersize = variable_get("upload_usersize_$rid", 10) * 1024 * 1024;
181
182 $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
183
184 if (!preg_match($regex, $file->filename)) {
185 $error['extension']++;
186 }
187
188 if ($uploadsize && $file->filesize > $uploadsize) {
189 $error['uploadsize']++;
190 }
191
192 if ($usersize && $total_usersize + $file->filesize > $usersize) {
193 $error['usersize']++;
194 }
195 }
196
197 $user_roles = count($user->roles);
198 $valid = TRUE;
199 if ($error['extension'] == $user_roles) {
200 // form_set_error('upload', t('The selected file %name can not be attached to this post, because it is only possible to attach files with the following extensions: %files-allowed.', array('%name' => $file->filename, '%files-allowed' => $extensions)));
201 watchdog('mmb', t('The selected file %name can not be attached to this post, because it is only possible to attach files with the following extensions: %files-allowed.', array('%name' => $file->filename, '%files-allowed' => $extensions)));
202 $valid = FALSE;
203 }
204 elseif ($error['uploadsize'] == $user_roles) {
205 // form_set_error('upload', t('The selected file %name can not be attached to this post, because it exceeded the maximum filesize of %maxsize.', array('%name' => $file->filename, '%maxsize' => format_size($uploadsize))));
206 watchdog('mmb', t('The selected file %name can not be attached to this post, because it exceeded the maximum filesize of %maxsize.', array('%name' => $file->filename, '%maxsize' => format_size($uploadsize))));
207 $valid = FALSE;
208 }
209 elseif ($error['usersize'] == $user_roles) {
210 // form_set_error('upload', t('The selected file %name can not be attached to this post, because the disk quota of %quota has been reached.', array('%name' => $file->filename, '%quota' => format_size($usersize))));
211 watchdog('mmb', t('The selected file %name can not be attached to this post, because the disk quota of %quota has been reached.', array('%name' => $file->filename, '%quota' => format_size($usersize))));
212 $valid = FALSE;
213 }
214 elseif (strlen($file->filename) > 255) {
215 // form_set_error('upload', t('The selected file %name can not be attached to this post, because the filename is too long.', array('%name' => $file->filename)));
216 watchdog('mmb', t('The selected file %name can not be attached to this post, because the filename is too long.', array('%name' => $file->filename)));
217 $valid = FALSE;
218 }
219
220 if (!$valid) {
221 unset($node->files[$fid], $_SESSION['file_previews'][$fid]);
222 file_delete($file->filepath);
223 }
224 }
225 }
226 }
227 }
228 }
229

  ViewVC Help
Powered by ViewVC 1.1.2