php-file-layer/FileHandler.php

219 lines
7.1 KiB
PHP
Raw Normal View History

<?php
/**
* Простой слой загрузки файлов на сервер
2018-09-10 02:33:47 +03:00
* Версия 2018-09-10
* (c) Виталий Филиппов 2018
*/
class FileHandler
{
2018-09-10 02:33:47 +03:00
const ONLY_BINARY = 1;
const ONLY_IMAGES = 2;
const ONLY_SWF = 4;
const ONLY_VIDEO = 8;
const IMAGES_VIDEO = 10;
const ANY_MEDIA = 14;
const ANYTHING = 15;
2018-09-10 02:33:47 +03:00
const CROP_XY = 1;
const CROP_Y = 2;
const CROP_X = 3;
public static $uploadCurl;
public function __construct($db, $config)
{
2018-09-10 02:33:47 +03:00
$this->db = $db;
$this->config = $config + [
'basedir' => '',
'baseurl' => '',
'table' => 'files',
'get_user_id' => NULL,
'mime_blacklist' => '#'.
// HTML may contain cookie-stealing JavaScript and web bugs
'^text/(html|(x-)?javascript)$|^application/x-shellscript$'.
// PHP/Perl/Bash/etc scripts may execute arbitrary code on the server
'|php|perl|python|bash|x-c?sh(e|$)'.
// Client-side hazards on Internet Explorer
'|^text/scriptlet$|^application/x-msdownload$'.
// Windows metafile, client-side vulnerability on some systems
'|^application/x-msmetafile$'.
'#is',
'thumb_path' => 'thumb/',
];
}
2018-09-10 02:33:47 +03:00
public function getPath($web_or_fs, $file)
{
$s = $file['sha1'];
2018-09-10 02:33:47 +03:00
return $this->config[$web_or_fs ? 'baseurl' : 'basedir'] . '/' .
substr($s, 0, 1) . '/' . substr($s, 0, 2) . '/' . $s . '.' . $file['format'];
}
2018-09-10 02:33:47 +03:00
public function getThumbPath($web_or_fs, $file, $type)
{
$s = $file['sha1'];
$ext = $file['format'] == 'jpg' || $file['format'] == 'png' || $file['format'] == 'gif'
? $file['format'] : 'jpg';
2018-09-10 02:33:47 +03:00
return $this->config[$web_or_fs ? 'baseurl' : 'basedir'] . '/' .
$this->config['thumb_path'] . '/' . $type . '/' .substr($s, 0, 1) . '/' . substr($s, 0, 2) . '/' . $s . '.' . $ext;
}
2018-09-10 02:33:47 +03:00
public function getGPS($file)
{
$props = $file['props'];
if (empty($props['GPSLatitude']) && empty($props['GPSLongitude']))
{
return NULL;
}
$latitude = FileUtils::exifGPS($props['GPSLatitude'], $props['GPSLatitudeRef']);
$longitude = FileUtils::exifGPS($props['GPSLongitude'], $props['GPSLongitudeRef']);
return [ $latitude, $longitude ];
}
2018-09-10 02:33:47 +03:00
public function upload(LocalFile $localFile, $allowedFormats = FileHandler::ANYTHING)
{
$tmp_name = $localFile->getLocalPath();
2018-09-10 02:33:47 +03:00
$props = FileUtils::getProps($allowedFormats, $this->config['mime_blacklist'], $tmp_name, $localFile->getFileName());
$row = [
'id' => NULL,
'added' => time(),
] + $props + [ 'props' => [] ];
2018-09-10 02:33:47 +03:00
if ($this->config['get_user_id'])
{
$row['user_id'] = $this->config['get_user_id']() ?: NULL;
}
$exist = $this->db->select($this->config['table'], '*', [ 'sha1' => $row['sha1'] ], NULL, MS_ROW);
if ($exist)
{
$exist['props'] = json_decode($exist['props'], true);
return $exist;
}
2018-09-10 02:33:47 +03:00
$row['id'] = $this->db->insert_row($this->config['table'], [
'props' => json_encode($row['props'], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
] + $row);
2018-09-10 02:33:47 +03:00
$fn = $this->getPath(false, $row);
FileUtils::mkpath(dirname($fn), true);
$m = $localFile->shouldMove ? 'rename' : 'copy';
if (!@$m($tmp_name, $fn))
{
$error = error_get_last();
throw new Exception($error['message']);
}
chmod($fn, 0666 & ~umask());
return $row;
}
2018-09-10 02:33:47 +03:00
public function uploadUrl($url, $flags = FileHandler::ONLY_IMAGES, $curl_options = [])
{
if (!$url)
{
return NULL;
}
$file = NULL;
if (substr($url, 0, 2) == '//')
{
$url = "http:$url";
}
if (!self::$uploadCurl)
{
// Reuse handle to use keepalive when possible
self::$uploadCurl = curl_init();
}
curl_setopt_array(self::$uploadCurl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
] + $curl_options);
$s = curl_exec(self::$uploadCurl);
if ($s)
{
$tmp = tempnam(sys_get_temp_dir(), 'upl');
file_put_contents($tmp, $s);
unset($s);
2018-09-10 02:33:47 +03:00
$file = $this->upload(new LocalFile($tmp, true), $flags);
@unlink($tmp);
}
else
{
// Log it as E_USER_NOTICE
trigger_error(curl_error(self::$uploadCurl));
}
return $file;
}
2018-09-10 02:33:47 +03:00
public function deleteFiles($where)
{
2018-09-10 02:33:47 +03:00
$files = $this->db->select($this->config['table'], '*', $where);
foreach ($existing as $e)
{
2018-09-10 02:33:47 +03:00
$disk_name = $this->getPath(false, $e);
if (file_exists($disk_name))
{
// Remove old file
// FIXME unlink thumbnails
unlink($disk_name);
}
}
}
/**
* Get or generate a thumbnail and return its URL
*/
2018-09-10 02:33:47 +03:00
public function getThumb($file, $width, $height, $force = false, $crop = false, $alignY = 0.5)
{
$size = FileUtils::getThumbSize($file, $width, $height, $crop);
if (!$size)
{
2018-09-10 02:33:47 +03:00
return $this->getPath(true, $file);
}
list($width, $height) = $size;
if (!$crop)
{
$type = intval($width);
}
else
{
2018-09-10 02:33:47 +03:00
$p = $crop == FileHandler::CROP_Y ? 'cy' : ($crop == FileHandler::CROP_X ? 'cx' : 'c');
$type = intval($width).'x'.intval($height).'_'.$alignY;
}
2018-09-10 02:33:47 +03:00
$fn = $this->getThumbPath(false, $file, $type);
if (!file_exists($fn) || $force)
{
if (substr($file['mimetype'], 0, 6) === 'video/')
{
2018-09-10 02:33:47 +03:00
$sourcefn = $this->getThumbPath(false, $file, 'src');
}
else
{
2018-09-10 02:33:47 +03:00
$sourcefn = $this->getPath(false, $file);
}
try
{
$im = FileUtils::magick();
$im->readImage($sourcefn);
$props = $file['props'];
if (!empty($props['Orientation']) && $props['Orientation'] > 5)
{
/* swap width & height */
$t = $width;
$width = $height;
$height = $t;
2018-09-10 02:33:47 +03:00
if ($crop == FileHandler::CROP_X || $crop == FileHandler::CROP_Y)
$crop = 5-$crop;
}
FileUtils::makeThumb($im, $width, $height, $crop, isset($props['Orientation']) ? $props['Orientation'] : NULL, $alignY);
$im->setCompressionQuality(FileUtils::$quality);
FileUtils::mkpath(dirname($fn));
$im->writeImage($fn);
}
catch (Exception $e)
{
trigger_error("$e");
return false;
}
}
2018-09-10 02:33:47 +03:00
return $this->getThumbPath(true, $file, $type);
}
}