123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class MediaFile
- {
- protected $scoped = null;
- var $filename = null;
- var $fileRecord = null;
- var $fileurl = null;
- var $short_fileurl = null;
- var $mimetype = null;
- function __construct(Profile $scoped, $filename = null, $mimetype = null)
- {
- $this->scoped = $scoped;
- $this->filename = $filename;
- $this->mimetype = $mimetype;
- $this->fileRecord = $this->storeFile();
- $this->fileurl = common_local_url('attachment',
- array('attachment' => $this->fileRecord->id));
- $this->maybeAddRedir($this->fileRecord->id, $this->fileurl);
- $this->short_fileurl = common_shorten_url($this->fileurl);
- $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl);
- }
- public function attachToNotice(Notice $notice)
- {
- File_to_post::processNew($this->fileRecord->id, $notice->id);
- }
- public function getPath()
- {
- return File::path($this->filename);
- }
- function shortUrl()
- {
- return $this->short_fileurl;
- }
- function delete()
- {
- $filepath = File::path($this->filename);
- @unlink($filepath);
- }
- public function getFile()
- {
- if (!$this->fileRecord instanceof File) {
- throw new ServerException('File record did not exist for MediaFile');
- }
- return $this->fileRecord;
- }
- protected function storeFile()
- {
- $file = new File;
- $file->filename = $this->filename;
- $file->url = File::url($this->filename);
- $filepath = File::path($this->filename);
- $file->size = filesize($filepath);
- $file->date = time();
- $file->mimetype = $this->mimetype;
- $file_id = $file->insert();
- if ($file_id===false) {
- common_log_db_error($file, "INSERT", __FILE__);
-
- throw new ClientException(_('There was a database error while saving your file. Please try again.'));
- }
-
- try {
- $image = ImageFile::fromFileObject($file);
- $orig = clone($file);
- $file->width = $image->width;
- $file->height = $image->height;
- $file->update($orig);
-
-
-
-
- if ($image->getPath() != $file->getPath()) {
- $image->unlink();
- }
- } catch (ServerException $e) {
-
-
-
- }
- return $file;
- }
- function rememberFile($file, $short)
- {
- $this->maybeAddRedir($file->id, $short);
- }
- function maybeAddRedir($file_id, $url)
- {
- $file_redir = File_redirection::getKV('url', $url);
- if (!$file_redir instanceof File_redirection) {
- $file_redir = new File_redirection;
- $file_redir->url = $url;
- $file_redir->file_id = $file_id;
- $result = $file_redir->insert();
- if ($result===false) {
- common_log_db_error($file_redir, "INSERT", __FILE__);
-
- throw new ClientException(_('There was a database error while saving your file. Please try again.'));
- }
- }
- }
- static function fromUpload($param='media', Profile $scoped=null)
- {
- if (is_null($scoped)) {
- $scoped = Profile::current();
- }
-
- if (!isset($_FILES[$param]) || !isset($_FILES[$param]['error'])) {
- throw new NoUploadedMediaException($param);
- }
- switch ($_FILES[$param]['error']) {
- case UPLOAD_ERR_OK:
- break;
- case UPLOAD_ERR_INI_SIZE:
-
- throw new ClientException(_('The uploaded file exceeds the ' .
- 'upload_max_filesize directive in php.ini.'));
- case UPLOAD_ERR_FORM_SIZE:
- throw new ClientException(
-
- _('The uploaded file exceeds the MAX_FILE_SIZE directive' .
- ' that was specified in the HTML form.'));
- case UPLOAD_ERR_PARTIAL:
- @unlink($_FILES[$param]['tmp_name']);
-
- throw new ClientException(_('The uploaded file was only' .
- ' partially uploaded.'));
- case UPLOAD_ERR_NO_FILE:
-
- throw new NoUploadedMediaException($param);
- case UPLOAD_ERR_NO_TMP_DIR:
-
- throw new ClientException(_('Missing a temporary folder.'));
- case UPLOAD_ERR_CANT_WRITE:
-
- throw new ClientException(_('Failed to write file to disk.'));
- case UPLOAD_ERR_EXTENSION:
-
- throw new ClientException(_('File upload stopped by extension.'));
- default:
- common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
- $_FILES[$param]['error']);
-
- throw new ClientException(_('System error uploading file.'));
- }
-
- File::respectsQuota($scoped, $_FILES[$param]['size']);
- $mimetype = self::getUploadedMimeType($_FILES[$param]['tmp_name'],
- $_FILES[$param]['name']);
- $basename = basename($_FILES[$param]['name']);
- $filename = File::filename($scoped, $basename, $mimetype);
- $filepath = File::path($filename);
- $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
- if (!$result) {
-
-
- throw new ClientException(_('File could not be moved to destination directory.'));
- }
- return new MediaFile($scoped, $filename, $mimetype);
- }
- static function fromFilehandle($fh, Profile $scoped) {
- $stream = stream_get_meta_data($fh);
- File::respectsQuota($scoped, filesize($stream['uri']));
- $mimetype = self::getUploadedMimeType($stream['uri']);
- $filename = File::filename($scoped, "email", $mimetype);
- $filepath = File::path($filename);
- $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
- if (!$result) {
-
-
- throw new ClientException(_('File could not be moved to destination directory.' .
- $stream['uri'] . ' ' . $filepath));
- }
- return new MediaFile($scoped, $filename, $mimetype);
- }
-
- static function getUploadedMimeType($filepath, $originalFilename=false) {
-
- $mimelookup = new finfo(FILEINFO_MIME_TYPE);
- $mimetype = $mimelookup->file($filepath);
-
-
- $unclearTypes = array('application/octet-stream',
- 'application/vnd.ms-office',
- 'application/zip',
-
- 'text/xml');
- $supported = common_config('attachments', 'supported');
-
- if ($originalFilename && (!$mimetype || in_array($mimetype, $unclearTypes))) {
- try {
- $type = common_supported_ext_to_mime($originalFilename);
- return $type;
- } catch (Exception $e) {
-
- }
- }
-
- if ($supported === true || array_key_exists($mimetype, $supported)) {
-
-
-
- return $mimetype;
- }
-
- $media = common_get_mime_media($mimetype);
- if ('application' !== $media) {
-
-
-
- $hint = sprintf(_('"%1$s" is not a supported file type on this server. ' .
- 'Try using another %2$s format.'), $mimetype, $media);
- } else {
-
-
- $hint = sprintf(_('"%s" is not a supported file type on this server.'), $mimetype);
- }
- throw new ClientException($hint);
- }
- }
|