]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mediafile.php
Extract media upload stuff into its own library class.
[quix0rs-gnu-social.git] / lib / mediafile.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source mMediaFileicroblogging tool
4  *
5  * Abstraction for a media files in general
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Media
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2008-2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 class MediaFile 
35 {
36     
37     var $filename      = null;
38     var $fileRecord    = null;
39     var $user          = null;
40     var $fileurl       = null;
41     var $short_fileurl = null;
42     var $mimetype      = null;
43
44     function __construct($user = null, $filename = null, $mimetype = null)
45     {
46         if ($user == null) {
47             $this->user = common_current_user();
48         }
49         
50         common_debug('in MediaFile constructor');
51         
52         $this->filename = $filename;
53         $this->mimetype = $mimetype;
54         
55         common_debug('storing file');
56         $this->fileRecord = $this->storeFile();
57         common_debug('finished storing file');
58         
59         $this->fileurl = common_local_url('attachment',
60                                     array('attachment' => $this->fileRecord->id));
61
62         common_debug('$this->fileurl() = ' . $this->fileurl);
63
64         // not sure this is necessary -- Zach
65         $this->maybeAddRedir($this->fileRecord->id, $this->fileurl);
66
67         common_debug('shortening file url');
68         $this->short_fileurl = common_shorten_url($this->fileurl);
69         common_debug('shortened file url =  ' . $short_fileurl);
70         
71         // Also, not sure this is necessary -- Zach
72         $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl);
73      
74         common_debug("MediaFile: end of constructor");
75     }
76     
77     function attachToNotice($notice)
78     {
79         common_debug('MediaFile::attachToNotice() -- doing File_to_post');
80         File_to_post::processNew($this->fileRecord->id, $notice->id);
81         common_debug('MediaFile done doing File_to_post');
82         
83         $this->maybeAddRedir($this->fileRecord->id,
84                              common_local_url('file', array('notice' => $notice->id)));                
85     }
86     
87     function shortUrl() 
88     {
89         return $this->short_fileurl;
90     }
91
92     function delete()
93     {
94         $filepath = File::path($this->filename);
95         @unlink($filepath);
96     }
97
98     function storeFile() {
99
100         $file = new File;
101         $file->filename = $this->filename;
102
103         common_debug('storing ' . $this->filename);
104
105         $file->url = File::url($this->filename);
106         common_debug('file->url = ' . $file->url);
107
108         $filepath = File::path($this->filename);
109         common_debug('filepath = ' . $filepath);
110
111         $file->size = filesize($filepath);
112         $file->date = time();
113         $file->mimetype = $this->mimetype;
114
115         $file_id = $file->insert();
116
117         if (!$file_id) {
118             
119             common_debug("storeFile: problem inserting new file");
120             common_log_db_error($file, "INSERT", __FILE__);
121             throw new ClientException(_('There was a database error while saving your file. Please try again.'));
122         }
123         
124         common_debug('finished storing file');
125
126         return $file;
127     }
128
129     function rememberFile($file, $short)
130     {
131         $this->maybeAddRedir($file->id, $short);
132     }
133
134     function maybeAddRedir($file_id, $url)
135     {
136         
137         common_debug("maybeAddRedir: looking up url: $url for file id $file_id");
138
139         $file_redir = File_redirection::staticGet('url', $url);
140
141         if (empty($file_redir)) {
142             
143             common_debug("maybeAddRedir: $url is not in the db");
144             
145             $file_redir = new File_redirection;
146             $file_redir->url = $url;
147             $file_redir->file_id = $file_id;
148
149             $result = $file_redir->insert();
150
151             if (!$result) {
152                 common_log_db_error($file_redir, "INSERT", __FILE__);
153                 throw new ClientException(_('There was a database error while saving your file. Please try again.'));
154             }
155         } else {
156          
157             common_debug("maybeAddRedir: no need to add $url, it's already in the db");
158         }
159     }
160
161     static function fromUpload($param = 'media')
162     {
163         common_debug("fromUpload: param = $param");
164         
165         if (!isset($_FILES[$param]['error'])){
166             common_debug('no file found');
167             return;
168         }
169         
170         switch ($_FILES[$param]['error']) {
171         case UPLOAD_ERR_OK: // success, jump out
172             break;
173         case UPLOAD_ERR_INI_SIZE:
174             throw new ClientException(_('The uploaded file exceeds the ' .
175                 'upload_max_filesize directive in php.ini.'));
176             return;
177         case UPLOAD_ERR_FORM_SIZE:
178             throw new ClientException(
179                 _('The uploaded file exceeds the MAX_FILE_SIZE directive' .
180                 ' that was specified in the HTML form.'));
181             return;
182         case UPLOAD_ERR_PARTIAL:
183             @unlink($_FILES[$param]['tmp_name']);
184             throw new ClientException(_('The uploaded file was only' .
185                 ' partially uploaded.'));
186             return;   
187         case UPLOAD_ERR_NO_TMP_DIR:
188             throw new ClientException(_('Missing a temporary folder.'));
189             return;
190         case UPLOAD_ERR_CANT_WRITE:
191             throw new ClientException(_('Failed to write file to disk.'));
192             return;
193         case UPLOAD_ERR_EXTENSION:
194             throw new ClientException(_('File upload stopped by extension.'));
195             return;
196         default:
197             throw new ClientException(_('System error uploading file.'));
198             return;
199         }
200         
201         $user = common_current_user();
202         
203         if (!MediaFile::respectsQuota($user, $_FILES['attach']['size'])) {
204             
205             // Should never actually get here
206             
207             @unlink($_FILES[$param]['tmp_name']);
208             throw new ClientException(_('File exceeds user\'s quota!'));
209             return;
210         }
211
212         $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name']);
213         
214         $filename = null;
215         
216         if (isset($mimetype)) {
217             
218             $basename = basename($_FILES[$param]['name']);
219             $filename = File::filename($user->getProfile(), $basename, $mimetype);
220             $filepath = File::path($filename);
221
222             common_debug("filepath = " . $filepath);
223
224             $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
225             
226             if (!$result) {                
227                 throw new ClientException(_('File could not be moved to destination directory.'));
228                 return;
229             }
230             
231         } else {
232             throw new ClientException(_('Could not determine file\'s mime-type!'));
233             return;
234         }
235                 
236         return new MediaFile($user, $filename, $mimetype);
237     }
238
239     static function fromFilehandle($user, $fh) {
240
241         $stream = stream_get_meta_data($fh);
242
243         if (MediaFile::respectsQuota($user, filesize($stream['uri']))) {
244             
245             // Should never actually get here
246                         
247             throw new ClientException(_('File exceeds user\'s quota!'));
248             return;
249         }
250
251         $mimetype = MediaFile::getUploadedFileType($fh);
252         
253         $filename = null;
254         
255         if (isset($mimetype)) {
256
257             $filename = File::filename($user->getProfile(), "email", $mimetype);
258
259             $filepath = File::path($filename);
260         
261             $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
262         
263             if (!$result) {
264                 throw new ClientException(_('File could not be moved to destination directory.' .
265                     $stream['uri'] . ' ' . $filepath));
266             }
267         } else {
268             throw new ClientException(_('Could not determine file\'s mime-type!'));
269             return;
270         }
271         
272         return new MediaFile($user, $filename, $mimetype);
273     }
274
275     static function getUploadedFileType($f) {
276         require_once 'MIME/Type.php';
277         
278         common_debug("in getUploadedFileType");
279
280         $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd');
281         $cmd = common_config('attachments', 'filecommand');
282         
283         $filetype = null;
284         
285         if (is_string($f)) {
286             
287             // assuming a filename
288             
289             $filetype = MIME_Type::autoDetect($f);
290         } else {
291             
292             // assuming a filehandle
293             
294             $stream  = stream_get_meta_data($f);
295             $filetype = MIME_Type::autoDetect($stream['uri']);
296         }
297         
298         if (in_array($filetype, common_config('attachments', 'supported'))) {
299             return $filetype;
300         }
301         $media = MIME_Type::getMedia($filetype);
302         if ('application' !== $media) {
303             $hint = sprintf(_(' Try using another %s format.'), $media);
304         } else {
305             $hint = '';
306         }
307         throw new ClientException(sprintf(
308             _('%s is not a supported filetype on this server.'), $filetype) . $hint);
309     }
310
311     static function respectsQuota($user, $filesize) 
312     {
313         $file = new File;
314         $result = $file->isRespectsQuota($user, $filesize);
315         if ($result === true) {
316             return true;
317         } else {
318             throw new ClientException($result);
319         }
320     }
321
322 }