2009-10-28 02:11:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2019-06-07 14:08:27 +01:00
|
|
|
* GNU social - a federating social network
|
2009-10-28 02:11:18 +00:00
|
|
|
*
|
2019-06-07 14:08:27 +01:00
|
|
|
* Abstraction for media files
|
2009-10-28 02:11:18 +00:00
|
|
|
*
|
|
|
|
* LICENCE: This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category Media
|
2019-06-07 14:08:27 +01:00
|
|
|
* @package GNUsocial
|
2009-10-29 00:47:14 +00:00
|
|
|
* @author Robin Millette <robin@millette.info>
|
2019-06-07 14:08:27 +01:00
|
|
|
* @author Miguel Dantas <biodantas@gmail.com>
|
2009-10-28 02:11:18 +00:00
|
|
|
* @author Zach Copley <zach@status.net>
|
2019-06-07 14:08:27 +01:00
|
|
|
* @author Mikael Nordfeldth <mmn@hethane.se>
|
|
|
|
* @copyright 2008-2009, 2019 Free Software Foundation http://fsf.org
|
2009-10-28 02:11:18 +00:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
2019-06-07 14:08:27 +01:00
|
|
|
* @link https://www.gnu.org/software/social/
|
2009-10-28 02:11:18 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-09 23:26:48 +01:00
|
|
|
if (!defined('GNUSOCIAL')) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-10-28 02:11:18 +00:00
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class responsible for abstracting media files
|
|
|
|
*/
|
2009-10-28 04:45:56 +00:00
|
|
|
class MediaFile
|
2009-10-28 02:11:18 +00:00
|
|
|
{
|
2019-06-07 14:08:27 +01:00
|
|
|
public $id = null;
|
|
|
|
public $filepath = null;
|
|
|
|
public $filename = null;
|
|
|
|
public $fileRecord = null;
|
|
|
|
public $fileurl = null;
|
|
|
|
public $short_fileurl = null;
|
|
|
|
public $mimetype = null;
|
2009-10-28 02:11:18 +00:00
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
/**
|
|
|
|
* @param string $filepath The path of the file this media refers to. Required
|
|
|
|
* @param string $mimetype The mimetype of the file. Required
|
|
|
|
* @param $filehash The hash of the file, if known. Optional
|
|
|
|
* @param int|null $id The DB id of the file. Int if known, null if not.
|
|
|
|
* If null, it searches for it. If -1, it skips all DB
|
|
|
|
* interactions (useful for temporary objects)
|
|
|
|
* @throws ClientException
|
|
|
|
* @throws NoResultException
|
|
|
|
* @throws ServerException
|
|
|
|
*/
|
|
|
|
public function __construct(string $filepath, string $mimetype, $filehash = null, $id = null)
|
2009-10-28 02:11:18 +00:00
|
|
|
{
|
2019-06-07 14:08:27 +01:00
|
|
|
$this->filepath = $filepath;
|
|
|
|
$this->filename = basename($this->filepath);
|
|
|
|
$this->mimetype = $mimetype;
|
|
|
|
$this->filehash = self::getHashOfFile($this->filepath, $filehash);
|
|
|
|
$this->id = $id;
|
|
|
|
|
|
|
|
// If id is -1, it means we're dealing with a temporary object and don't want to store it in the DB,
|
|
|
|
// or add redirects
|
|
|
|
if ($this->id !== -1) {
|
|
|
|
if (!empty($this->id)) {
|
|
|
|
// If we have an id, load it
|
|
|
|
$this->fileRecord = new File();
|
|
|
|
$this->fileRecord->id = $this->id;
|
|
|
|
if (!$this->fileRecord->find(true)) {
|
|
|
|
// If we have set an ID, we need that ID to exist!
|
|
|
|
throw new NoResultException($this->fileRecord);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, store it
|
|
|
|
$this->fileRecord = $this->storeFile();
|
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
$this->fileurl = common_local_url(
|
|
|
|
'attachment',
|
|
|
|
array('attachment' => $this->fileRecord->id)
|
|
|
|
);
|
2009-10-28 02:11:18 +00:00
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
$this->maybeAddRedir($this->fileRecord->id, $this->fileurl);
|
|
|
|
$this->short_fileurl = common_shorten_url($this->fileurl);
|
|
|
|
$this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl);
|
|
|
|
}
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2014-04-16 22:17:27 +01:00
|
|
|
public function attachToNotice(Notice $notice)
|
2009-10-28 02:11:18 +00:00
|
|
|
{
|
2015-06-04 16:36:11 +01:00
|
|
|
File_to_post::processNew($this->fileRecord, $notice);
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2014-04-16 18:14:26 +01:00
|
|
|
public function getPath()
|
|
|
|
{
|
|
|
|
return File::path($this->filename);
|
|
|
|
}
|
|
|
|
|
2019-06-09 23:26:48 +01:00
|
|
|
public function shortUrl()
|
2009-10-28 02:11:18 +00:00
|
|
|
{
|
|
|
|
return $this->short_fileurl;
|
|
|
|
}
|
|
|
|
|
2019-06-09 23:26:48 +01:00
|
|
|
public function getEnclosure()
|
2016-01-01 19:18:54 +00:00
|
|
|
{
|
|
|
|
return $this->getFile()->getEnclosure();
|
|
|
|
}
|
|
|
|
|
2019-06-09 23:26:48 +01:00
|
|
|
public function delete()
|
2009-10-28 02:11:18 +00:00
|
|
|
{
|
2019-06-07 14:08:27 +01:00
|
|
|
@unlink($this->filepath);
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 13:46:47 +00:00
|
|
|
public function getFile()
|
|
|
|
{
|
|
|
|
if (!$this->fileRecord instanceof File) {
|
|
|
|
throw new ServerException('File record did not exist for MediaFile');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->fileRecord;
|
|
|
|
}
|
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
/**
|
|
|
|
* Calculate the hash of a file.
|
|
|
|
*
|
|
|
|
* This won't work for files >2GiB because PHP uses only 32bit.
|
|
|
|
* @param string $filepath
|
|
|
|
* @param string|null $filehash
|
|
|
|
* @return string
|
|
|
|
* @throws ServerException
|
|
|
|
*/
|
|
|
|
public static function getHashOfFile(string $filepath, $filehash = null)
|
2014-04-22 11:09:24 +01:00
|
|
|
{
|
2019-06-07 14:08:27 +01:00
|
|
|
assert(!empty($filepath), __METHOD__ . ": filepath cannot be null");
|
|
|
|
if ($filehash === null) {
|
2015-02-24 20:11:25 +00:00
|
|
|
// Calculate if we have an older upload method somewhere (Qvitter) that
|
|
|
|
// doesn't do this before calling new MediaFile on its local files...
|
2019-06-07 14:08:27 +01:00
|
|
|
$filehash = hash_file(File::FILEHASH_ALG, $filepath);
|
|
|
|
if ($filehash === false) {
|
2015-02-24 20:11:25 +00:00
|
|
|
throw new ServerException('Could not read file for hashing');
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 14:08:27 +01:00
|
|
|
return $filehash;
|
|
|
|
}
|
2015-02-24 20:11:25 +00:00
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
/**
|
|
|
|
* Retrieve or insert as a file in the DB
|
|
|
|
*
|
|
|
|
* @return object File
|
|
|
|
* @throws ClientException
|
|
|
|
* @throws ServerException
|
|
|
|
*/
|
|
|
|
protected function storeFile()
|
|
|
|
{
|
2015-02-24 20:11:25 +00:00
|
|
|
try {
|
|
|
|
$file = File::getByHash($this->filehash);
|
|
|
|
// We're done here. Yes. Already. We assume sha256 won't collide on us anytime soon.
|
|
|
|
return $file;
|
|
|
|
} catch (NoResultException $e) {
|
|
|
|
// Well, let's just continue below.
|
|
|
|
}
|
|
|
|
|
2015-02-19 17:59:28 +00:00
|
|
|
$fileurl = File::url($this->filename);
|
2009-10-28 02:11:18 +00:00
|
|
|
|
|
|
|
$file = new File;
|
|
|
|
|
2009-10-29 00:12:22 +00:00
|
|
|
$file->filename = $this->filename;
|
2015-02-19 17:59:28 +00:00
|
|
|
$file->urlhash = File::hashurl($fileurl);
|
|
|
|
$file->url = $fileurl;
|
2015-02-24 20:11:25 +00:00
|
|
|
$file->filehash = $this->filehash;
|
2019-06-07 14:08:27 +01:00
|
|
|
$file->size = filesize($this->filepath);
|
2015-02-24 20:11:25 +00:00
|
|
|
if ($file->size === false) {
|
|
|
|
throw new ServerException('Could not read file to get its size');
|
|
|
|
}
|
2009-10-29 00:12:22 +00:00
|
|
|
$file->date = time();
|
2009-10-28 02:11:18 +00:00
|
|
|
$file->mimetype = $this->mimetype;
|
|
|
|
|
|
|
|
$file_id = $file->insert();
|
|
|
|
|
2014-04-22 11:09:24 +01:00
|
|
|
if ($file_id===false) {
|
2009-10-28 02:11:18 +00:00
|
|
|
common_log_db_error($file, "INSERT", __FILE__);
|
2010-09-12 17:24:44 +01:00
|
|
|
// TRANS: Client exception thrown when a database error was thrown during a file upload operation.
|
2009-10-28 02:11:18 +00:00
|
|
|
throw new ClientException(_('There was a database error while saving your file. Please try again.'));
|
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2014-04-22 11:09:24 +01:00
|
|
|
// Set file geometrical properties if available
|
|
|
|
try {
|
|
|
|
$image = ImageFile::fromFileObject($file);
|
|
|
|
$orig = clone($file);
|
|
|
|
$file->width = $image->width;
|
|
|
|
$file->height = $image->height;
|
|
|
|
$file->update($orig);
|
|
|
|
|
|
|
|
// We have to cleanup after ImageFile, since it
|
|
|
|
// may have generated a temporary file from a
|
|
|
|
// video support plugin or something.
|
|
|
|
// FIXME: Do this more automagically.
|
|
|
|
if ($image->getPath() != $file->getPath()) {
|
|
|
|
$image->unlink();
|
|
|
|
}
|
|
|
|
} catch (ServerException $e) {
|
|
|
|
// We just couldn't make out an image from the file. This
|
|
|
|
// does not have to be UnsupportedMediaException, as we can
|
|
|
|
// also get ServerException from files not existing etc.
|
|
|
|
}
|
|
|
|
|
2009-10-28 02:11:18 +00:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2019-06-09 23:26:48 +01:00
|
|
|
public function rememberFile($file, $short)
|
2009-10-28 02:11:18 +00:00
|
|
|
{
|
|
|
|
$this->maybeAddRedir($file->id, $short);
|
|
|
|
}
|
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
/**
|
|
|
|
* Adds Redir if needed.
|
|
|
|
*
|
|
|
|
* @param $file_id
|
|
|
|
* @param $url
|
|
|
|
* @return bool false if no need to add, true if added
|
|
|
|
* @throws ClientException If failed adding
|
|
|
|
*/
|
|
|
|
public function maybeAddRedir($file_id, $url)
|
2009-10-28 02:11:18 +00:00
|
|
|
{
|
2015-02-19 18:12:16 +00:00
|
|
|
try {
|
2019-06-07 14:08:27 +01:00
|
|
|
File_redirection::getByUrl($url);
|
|
|
|
return false;
|
2015-02-19 18:12:16 +00:00
|
|
|
} catch (NoResultException $e) {
|
2009-10-28 02:11:18 +00:00
|
|
|
$file_redir = new File_redirection;
|
2015-02-19 18:12:16 +00:00
|
|
|
$file_redir->urlhash = File::hashurl($url);
|
2009-10-28 02:11:18 +00:00
|
|
|
$file_redir->url = $url;
|
|
|
|
$file_redir->file_id = $file_id;
|
|
|
|
|
|
|
|
$result = $file_redir->insert();
|
|
|
|
|
2014-05-10 11:29:34 +01:00
|
|
|
if ($result===false) {
|
2009-10-28 02:11:18 +00:00
|
|
|
common_log_db_error($file_redir, "INSERT", __FILE__);
|
2010-09-12 17:24:44 +01:00
|
|
|
// TRANS: Client exception thrown when a database error was thrown during a file upload operation.
|
2009-10-28 02:11:18 +00:00
|
|
|
throw new ClientException(_('There was a database error while saving your file. Please try again.'));
|
|
|
|
}
|
2019-06-07 14:08:27 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum allowed file size, as a string
|
|
|
|
*/
|
2019-06-09 23:26:48 +01:00
|
|
|
public static function maxFileSize()
|
2019-06-07 14:08:27 +01:00
|
|
|
{
|
|
|
|
$value = self::maxFileSizeInt();
|
|
|
|
if ($value > 1024 * 1024) {
|
|
|
|
$value = $value/(1024*1024);
|
|
|
|
// TRANS: Number of megabytes. %d is the number.
|
2019-06-09 23:26:48 +01:00
|
|
|
return sprintf(_m('%dMB', '%dMB', $value), $value);
|
|
|
|
} elseif ($value > 1024) {
|
2019-06-07 14:08:27 +01:00
|
|
|
$value = $value/1024;
|
|
|
|
// TRANS: Number of kilobytes. %d is the number.
|
2019-06-09 23:26:48 +01:00
|
|
|
return sprintf(_m('%dkB', '%dkB', $value), $value);
|
2019-06-07 14:08:27 +01:00
|
|
|
} else {
|
|
|
|
// TRANS: Number of bytes. %d is the number.
|
2019-06-09 23:26:48 +01:00
|
|
|
return sprintf(_m('%dB', '%dB', $value), $value);
|
2019-06-07 14:08:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum allowed file size, as an int
|
|
|
|
*/
|
2019-06-09 23:26:48 +01:00
|
|
|
public static function maxFileSizeInt()
|
2019-06-07 14:08:27 +01:00
|
|
|
{
|
2019-06-09 23:26:48 +01:00
|
|
|
return min(
|
|
|
|
self::sizeStrToInt(ini_get('post_max_size')),
|
|
|
|
self::sizeStrToInt(ini_get('upload_max_filesize')),
|
|
|
|
self::sizeStrToInt(ini_get('memory_limit'))
|
|
|
|
);
|
2019-06-07 14:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a string representing a file size (with units), to an int
|
|
|
|
* @param $str
|
|
|
|
* @return bool|int|string
|
|
|
|
*/
|
|
|
|
public static function sizeStrToInt($str)
|
|
|
|
{
|
|
|
|
$unit = substr($str, -1);
|
|
|
|
$num = substr($str, 0, -1);
|
2019-06-09 23:26:48 +01:00
|
|
|
switch (strtoupper($unit)) {
|
2019-06-07 14:08:27 +01:00
|
|
|
case 'G':
|
|
|
|
$num *= 1024;
|
2019-06-09 23:26:48 +01:00
|
|
|
// no break
|
2019-06-07 14:08:27 +01:00
|
|
|
case 'M':
|
|
|
|
$num *= 1024;
|
2019-06-09 23:26:48 +01:00
|
|
|
// no break
|
2019-06-07 14:08:27 +01:00
|
|
|
case 'K':
|
|
|
|
$num *= 1024;
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
2019-06-07 14:08:27 +01:00
|
|
|
return $num;
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
/**
|
|
|
|
* Create a new MediaFile or ImageFile object from an upload
|
|
|
|
*
|
|
|
|
* Tries to set the mimetype correctly, using the most secure method available and rejects the file otherwise.
|
|
|
|
* In case the upload is an image, this function returns an new ImageFile (which extends MediaFile)
|
|
|
|
* @param string $param
|
|
|
|
* @param Profile|null $scoped
|
|
|
|
* @return ImageFile|MediaFile
|
|
|
|
* @throws ClientException
|
|
|
|
* @throws NoResultException
|
|
|
|
* @throws NoUploadedMediaException
|
|
|
|
* @throws ServerException
|
|
|
|
* @throws UnsupportedMediaException
|
|
|
|
* @throws UseFileAsThumbnailException
|
|
|
|
*/
|
|
|
|
public static function fromUpload(string $param='media', Profile $scoped=null)
|
2009-10-28 02:11:18 +00:00
|
|
|
{
|
2015-01-21 16:32:57 +00:00
|
|
|
// The existence of the "error" element means PHP has processed it properly even if it was ok.
|
|
|
|
if (!isset($_FILES[$param]) || !isset($_FILES[$param]['error'])) {
|
|
|
|
throw new NoUploadedMediaException($param);
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2009-10-28 02:11:18 +00:00
|
|
|
switch ($_FILES[$param]['error']) {
|
2014-04-22 11:09:24 +01:00
|
|
|
case UPLOAD_ERR_OK: // success, jump out
|
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_INI_SIZE:
|
|
|
|
case UPLOAD_ERR_FORM_SIZE:
|
2019-06-07 14:08:27 +01:00
|
|
|
// TRANS: Exception thrown when too large a file is uploaded.
|
|
|
|
// TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB".
|
2019-06-09 23:26:48 +01:00
|
|
|
throw new ClientException(sprintf(
|
|
|
|
_('That file is too big. The maximum file size is %s.'),
|
|
|
|
self::maxFileSize()
|
|
|
|
));
|
2014-04-22 11:09:24 +01:00
|
|
|
case UPLOAD_ERR_PARTIAL:
|
|
|
|
@unlink($_FILES[$param]['tmp_name']);
|
2010-09-12 17:24:44 +01:00
|
|
|
// TRANS: Client exception.
|
2019-06-07 14:08:27 +01:00
|
|
|
throw new ClientException(_('The uploaded file was only partially uploaded.'));
|
2014-04-22 11:09:24 +01:00
|
|
|
case UPLOAD_ERR_NO_FILE:
|
|
|
|
// No file; probably just a non-AJAX submission.
|
2015-01-22 11:38:57 +00:00
|
|
|
throw new NoUploadedMediaException($param);
|
2014-04-22 11:09:24 +01:00
|
|
|
case UPLOAD_ERR_NO_TMP_DIR:
|
|
|
|
// TRANS: Client exception thrown when a temporary folder is not present to store a file upload.
|
|
|
|
throw new ClientException(_('Missing a temporary folder.'));
|
|
|
|
case UPLOAD_ERR_CANT_WRITE:
|
|
|
|
// TRANS: Client exception thrown when writing to disk is not possible during a file upload operation.
|
|
|
|
throw new ClientException(_('Failed to write file to disk.'));
|
|
|
|
case UPLOAD_ERR_EXTENSION:
|
|
|
|
// TRANS: Client exception thrown when a file upload operation has been stopped by an extension.
|
|
|
|
throw new ClientException(_('File upload stopped by extension.'));
|
|
|
|
default:
|
2019-06-07 14:08:27 +01:00
|
|
|
common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . $_FILES[$param]['error']);
|
2014-04-22 11:09:24 +01:00
|
|
|
// TRANS: Client exception thrown when a file upload operation has failed with an unknown reason.
|
|
|
|
throw new ClientException(_('System error uploading file.'));
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
$filehash = strtolower(self::getHashOfFile($_FILES[$param]['tmp_name']));
|
2015-02-24 20:11:25 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$file = File::getByHash($filehash);
|
|
|
|
// If no exception is thrown the file exists locally, so we'll use that and just add redirections.
|
2016-03-05 11:05:12 +00:00
|
|
|
// but if the _actual_ locally stored file doesn't exist, getPath will throw FileNotFoundException
|
2019-06-07 14:08:27 +01:00
|
|
|
$filepath = $file->getPath();
|
2015-02-24 20:11:25 +00:00
|
|
|
$mimetype = $file->mimetype;
|
2019-06-09 23:26:48 +01:00
|
|
|
// XXX PHP: Upgrade to PHP 7.1
|
2019-06-07 14:08:27 +01:00
|
|
|
// catch (FileNotFoundException | NoResultException $e)
|
|
|
|
} catch (Exception $e) {
|
2015-02-24 20:11:25 +00:00
|
|
|
// We have to save the upload as a new local file. This is the normal course of action.
|
2016-03-05 10:59:46 +00:00
|
|
|
if ($scoped instanceof Profile) {
|
|
|
|
// Throws exception if additional size does not respect quota
|
|
|
|
// This test is only needed, of course, if we're uploading something new.
|
|
|
|
File::respectsQuota($scoped, $_FILES[$param]['size']);
|
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2015-02-24 20:11:25 +00:00
|
|
|
$mimetype = self::getUploadedMimeType($_FILES[$param]['tmp_name'], $_FILES[$param]['name']);
|
2019-06-07 14:08:27 +01:00
|
|
|
$media = common_get_mime_media($mimetype);
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
$basename = basename($_FILES[$param]['name']);
|
|
|
|
$filename = $filehash . '.' . File::guessMimeExtension($mimetype, $basename);
|
2015-02-24 20:11:25 +00:00
|
|
|
$filepath = File::path($filename);
|
|
|
|
$result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
// TRANS: Client exception thrown when a file upload operation fails because the file could
|
|
|
|
// TRANS: not be moved from the temporary folder to the permanent file location.
|
2019-06-07 14:08:27 +01:00
|
|
|
// UX: too specific
|
2015-02-24 20:11:25 +00:00
|
|
|
throw new ClientException(_('File could not be moved to destination directory.'));
|
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2019-06-07 14:08:27 +01:00
|
|
|
if ($media === 'image') {
|
|
|
|
// Use -1 for the id to avoid adding this temporary file to the DB
|
|
|
|
$img = new ImageFile(-1, $filepath);
|
|
|
|
// Validate the image by reencoding it. Additionally normalizes old formats to PNG,
|
|
|
|
// keeping JPEG and GIF untouched
|
|
|
|
$outpath = $img->resizeTo($img->filepath);
|
|
|
|
$ext = image_type_to_extension($img->preferredType());
|
|
|
|
$filename = $filehash . $ext;
|
|
|
|
$filepath = File::path($filename);
|
|
|
|
$result = rename($outpath, $filepath);
|
|
|
|
return new ImageFile(null, $filepath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new MediaFile($filepath, $mimetype, $filehash);
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 23:26:48 +01:00
|
|
|
public static function fromFilehandle($fh, Profile $scoped=null)
|
|
|
|
{
|
2009-10-28 02:11:18 +00:00
|
|
|
$stream = stream_get_meta_data($fh);
|
2015-02-24 20:11:25 +00:00
|
|
|
// So far we're only handling filehandles originating from tmpfile(),
|
|
|
|
// so we can always do hash_file on $stream['uri'] as far as I can tell!
|
|
|
|
$filehash = hash_file(File::FILEHASH_ALG, $stream['uri']);
|
2009-10-28 02:11:18 +00:00
|
|
|
|
2015-02-24 20:11:25 +00:00
|
|
|
try {
|
|
|
|
$file = File::getByHash($filehash);
|
|
|
|
// Already have it, so let's reuse the locally stored File
|
2016-03-05 00:26:34 +00:00
|
|
|
// by using getPath we also check whether the file exists
|
|
|
|
// and throw a FileNotFoundException with the path if it doesn't.
|
|
|
|
$filename = basename($file->getPath());
|
2015-02-24 20:11:25 +00:00
|
|
|
$mimetype = $file->mimetype;
|
2016-03-05 00:26:34 +00:00
|
|
|
} catch (FileNotFoundException $e) {
|
|
|
|
// This happens if the file we have uploaded has disappeared
|
|
|
|
// from the local filesystem for some reason. Since we got the
|
|
|
|
// File object from a sha256 check in fromFilehandle, it's safe
|
|
|
|
// to just copy the uploaded data to disk!
|
|
|
|
|
|
|
|
fseek($fh, 0); // just to be sure, go to the beginning
|
|
|
|
// dump the contents of our filehandle to the path from our exception
|
|
|
|
// and report error if it failed.
|
|
|
|
if (false === file_put_contents($e->path, fread($fh, filesize($stream['uri'])))) {
|
|
|
|
// TRANS: Client exception thrown when a file upload operation fails because the file could
|
|
|
|
// TRANS: not be moved from the temporary folder to the permanent file location.
|
|
|
|
throw new ClientException(_('File could not be moved to destination directory.'));
|
|
|
|
}
|
|
|
|
if (!chmod($e->path, 0664)) {
|
|
|
|
common_log(LOG_ERR, 'Could not chmod uploaded file: '._ve($e->path));
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = basename($file->getPath());
|
|
|
|
$mimetype = $file->mimetype;
|
2015-02-24 20:11:25 +00:00
|
|
|
} catch (NoResultException $e) {
|
2016-03-05 10:59:46 +00:00
|
|
|
if ($scoped instanceof Profile) {
|
|
|
|
File::respectsQuota($scoped, filesize($stream['uri']));
|
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2015-02-24 20:11:25 +00:00
|
|
|
$mimetype = self::getUploadedMimeType($stream['uri']);
|
2009-10-28 02:11:18 +00:00
|
|
|
|
2016-03-05 10:59:46 +00:00
|
|
|
$filename = strtolower($filehash) . '.' . File::guessMimeExtension($mimetype);
|
2015-02-24 20:11:25 +00:00
|
|
|
$filepath = File::path($filename);
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2015-02-24 20:11:25 +00:00
|
|
|
$result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2015-02-24 20:11:25 +00:00
|
|
|
if (!$result) {
|
2016-03-04 23:59:39 +00:00
|
|
|
common_log(LOG_ERR, 'File could not be moved (or chmodded) from '._ve($stream['uri']) . ' to ' . _ve($filepath));
|
2015-02-24 20:11:25 +00:00
|
|
|
// TRANS: Client exception thrown when a file upload operation fails because the file could
|
|
|
|
// TRANS: not be moved from the temporary folder to the permanent file location.
|
2019-06-09 23:26:48 +01:00
|
|
|
throw new ClientException(_('File could not be moved to destination directory.'));
|
2015-02-24 20:11:25 +00:00
|
|
|
}
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2016-03-05 10:59:46 +00:00
|
|
|
return new MediaFile($filename, $mimetype, $filehash);
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
|
|
|
|
2010-05-11 00:18:29 +01:00
|
|
|
/**
|
|
|
|
* Attempt to identify the content type of a given file.
|
2019-06-07 14:08:27 +01:00
|
|
|
*
|
2014-03-08 02:03:04 +00:00
|
|
|
* @param string $filepath filesystem path as string (file must exist)
|
2019-06-07 14:08:27 +01:00
|
|
|
* @param bool $originalFilename (optional) for extension-based detection
|
2010-05-11 00:18:29 +01:00
|
|
|
* @return string
|
2019-06-07 14:08:27 +01:00
|
|
|
*
|
2010-05-11 00:18:29 +01:00
|
|
|
* @throws ClientException if type is known, but not supported for local uploads
|
2019-06-07 14:08:27 +01:00
|
|
|
* @throws ServerException
|
|
|
|
* @fixme this seems to tie a front-end error message in, kinda confusing
|
|
|
|
*
|
2010-05-11 00:18:29 +01:00
|
|
|
*/
|
2019-06-09 23:26:48 +01:00
|
|
|
public static function getUploadedMimeType(string $filepath, $originalFilename=false)
|
|
|
|
{
|
2014-03-08 00:42:24 +00:00
|
|
|
// We only accept filenames to existing files
|
2019-06-07 14:08:27 +01:00
|
|
|
|
|
|
|
$mimetype = null;
|
|
|
|
|
|
|
|
// From CodeIgniter
|
|
|
|
// We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
|
|
|
|
$regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
|
|
|
|
/**
|
|
|
|
* Fileinfo extension - most reliable method
|
|
|
|
*
|
|
|
|
* Apparently XAMPP, CentOS, cPanel and who knows what
|
|
|
|
* other PHP distribution channels EXPLICITLY DISABLE
|
|
|
|
* ext/fileinfo, which is otherwise enabled by default
|
|
|
|
* since PHP 5.3 ...
|
|
|
|
*/
|
2019-06-09 23:26:48 +01:00
|
|
|
if (function_exists('finfo_file')) {
|
2019-06-07 14:08:27 +01:00
|
|
|
$finfo = @finfo_open(FILEINFO_MIME);
|
|
|
|
// It is possible that a FALSE value is returned, if there is no magic MIME database
|
|
|
|
// file found on the system
|
2019-06-09 23:26:48 +01:00
|
|
|
if (is_resource($finfo)) {
|
2019-06-07 14:08:27 +01:00
|
|
|
$mime = @finfo_file($finfo, $filepath);
|
|
|
|
finfo_close($finfo);
|
|
|
|
/* According to the comments section of the PHP manual page,
|
|
|
|
* it is possible that this function returns an empty string
|
|
|
|
* for some files (e.g. if they don't exist in the magic MIME database)
|
|
|
|
*/
|
2019-06-09 23:26:48 +01:00
|
|
|
if (is_string($mime) && preg_match($regexp, $mime, $matches)) {
|
2019-06-07 14:08:27 +01:00
|
|
|
$mimetype = $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
|
|
|
|
* which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
|
|
|
|
* was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
|
|
|
|
* than mime_content_type() as well, hence the attempts to try calling the command line with
|
|
|
|
* three different functions.
|
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
|
|
|
|
* - many system admins would disable the exec(), shell_exec(), popen() and similar functions
|
|
|
|
* due to security concerns, hence the function_usable() checks
|
|
|
|
*/
|
|
|
|
if (DIRECTORY_SEPARATOR !== '\\') {
|
|
|
|
$cmd = 'file --brief --mime '.escapeshellarg($filepath).' 2>&1';
|
|
|
|
if (function_exists('exec')) {
|
|
|
|
/* This might look confusing, as $mime is being populated with all of the output
|
|
|
|
* when set in the second parameter. However, we only need the last line, which is
|
|
|
|
* the actual return value of exec(), and as such - it overwrites anything that could
|
|
|
|
* already be set for $mime previously. This effectively makes the second parameter a
|
|
|
|
* dummy value, which is only put to allow us to get the return status code.
|
|
|
|
*/
|
|
|
|
$mime = @exec($cmd, $mime, $return_status);
|
|
|
|
if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches)) {
|
|
|
|
$mimetype = $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (function_exists('shell_exec')) {
|
|
|
|
$mime = @shell_exec($cmd);
|
|
|
|
if (strlen($mime) > 0) {
|
|
|
|
$mime = explode("\n", trim($mime));
|
|
|
|
if (preg_match($regexp, $mime[(count($mime) - 1)], $matches)) {
|
|
|
|
$mimetype = $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (function_exists('popen')) {
|
|
|
|
$proc = @popen($cmd, 'r');
|
|
|
|
if (is_resource($proc)) {
|
|
|
|
$mime = @fread($proc, 512);
|
|
|
|
@pclose($proc);
|
|
|
|
if ($mime !== false) {
|
|
|
|
$mime = explode("\n", trim($mime));
|
|
|
|
if (preg_match($regexp, $mime[(count($mime) - 1)], $matches)) {
|
|
|
|
$mimetype = $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fall back to mime_content_type(), if available (still better than $_FILES[$field]['type'])
|
2019-06-09 23:26:48 +01:00
|
|
|
if (function_exists('mime_content_type')) {
|
2019-06-07 14:08:27 +01:00
|
|
|
$mimetype = @mime_content_type($filepath);
|
|
|
|
// It's possible that mime_content_type() returns FALSE or an empty string
|
2019-06-09 23:26:48 +01:00
|
|
|
if ($mimetype == false && strlen($mimetype) > 0) {
|
2019-06-07 14:08:27 +01:00
|
|
|
throw new ServerException(_m('Could not determine file\'s MIME type.'));
|
|
|
|
}
|
|
|
|
}
|
2009-10-28 04:45:56 +00:00
|
|
|
|
2014-03-08 02:03:04 +00:00
|
|
|
// Unclear types are such that we can't really tell by the auto
|
|
|
|
// detect what they are (.bin, .exe etc. are just "octet-stream")
|
2010-05-11 00:18:29 +01:00
|
|
|
$unclearTypes = array('application/octet-stream',
|
|
|
|
'application/vnd.ms-office',
|
2010-12-22 21:56:19 +00:00
|
|
|
'application/zip',
|
2016-07-06 08:34:09 +01:00
|
|
|
'text/plain',
|
2015-03-05 10:54:30 +00:00
|
|
|
'text/html', // Ironically, Wikimedia Commons' SVG_logo.svg is identified as text/html
|
2010-12-22 21:56:19 +00:00
|
|
|
// TODO: for XML we could do better content-based sniffing too
|
|
|
|
'text/xml');
|
2010-05-11 00:18:29 +01:00
|
|
|
|
2014-03-08 02:03:04 +00:00
|
|
|
$supported = common_config('attachments', 'supported');
|
|
|
|
|
|
|
|
// If we didn't match, or it is an unclear match
|
|
|
|
if ($originalFilename && (!$mimetype || in_array($mimetype, $unclearTypes))) {
|
2014-03-08 02:51:47 +00:00
|
|
|
try {
|
2016-07-06 08:34:09 +01:00
|
|
|
$type = common_supported_filename_to_mime($originalFilename);
|
2014-03-08 02:03:04 +00:00
|
|
|
return $type;
|
2016-07-06 08:34:09 +01:00
|
|
|
} catch (UnknownExtensionMimeException $e) {
|
|
|
|
// FIXME: I think we should keep the file extension here (supported should be === true here)
|
2014-03-08 02:51:47 +00:00
|
|
|
} catch (Exception $e) {
|
2016-07-06 08:34:09 +01:00
|
|
|
// Extension parsed but no connected mimetype, so $mimetype is our best guess
|
2010-05-11 00:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 02:03:04 +00:00
|
|
|
// If $config['attachments']['supported'] equals boolean true, accept any mimetype
|
|
|
|
if ($supported === true || array_key_exists($mimetype, $supported)) {
|
|
|
|
// FIXME: Don't know if it always has a mimetype here because
|
|
|
|
// finfo->file CAN return false on error: http://php.net/finfo_file
|
|
|
|
// so if $supported === true, this may return something unexpected.
|
|
|
|
return $mimetype;
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
2014-03-08 02:03:04 +00:00
|
|
|
|
|
|
|
// We can conclude that we have failed to get the MIME type
|
|
|
|
$media = common_get_mime_media($mimetype);
|
2009-10-28 02:11:18 +00:00
|
|
|
if ('application' !== $media) {
|
2010-09-12 17:11:28 +01:00
|
|
|
// TRANS: Client exception thrown trying to upload a forbidden MIME type.
|
|
|
|
// TRANS: %1$s is the file type that was denied, %2$s is the application part of
|
|
|
|
// TRANS: the MIME type that was denied.
|
|
|
|
$hint = sprintf(_('"%1$s" is not a supported file type on this server. ' .
|
2014-03-08 02:03:04 +00:00
|
|
|
'Try using another %2$s format.'), $mimetype, $media);
|
2009-10-28 02:11:18 +00:00
|
|
|
} else {
|
2010-09-12 17:11:28 +01:00
|
|
|
// TRANS: Client exception thrown trying to upload a forbidden MIME type.
|
|
|
|
// TRANS: %s is the file type that was denied.
|
2014-03-08 02:03:04 +00:00
|
|
|
$hint = sprintf(_('"%s" is not a supported file type on this server.'), $mimetype);
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
2010-09-12 17:11:28 +01:00
|
|
|
throw new ClientException($hint);
|
2009-10-28 02:11:18 +00:00
|
|
|
}
|
2010-01-10 11:26:24 +00:00
|
|
|
}
|