2016-02-15 21:20:46 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Holds the file controller.
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura\Controllers;
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
|
|
|
|
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
|
2016-02-15 21:20:46 +00:00
|
|
|
use Sakura\Config;
|
2016-09-13 22:05:03 +00:00
|
|
|
use Sakura\CurrentSession;
|
|
|
|
use Sakura\DB;
|
|
|
|
use Sakura\Exceptions\FileException;
|
2016-02-15 21:20:46 +00:00
|
|
|
use Sakura\File;
|
2016-04-01 15:31:05 +00:00
|
|
|
use Sakura\Template;
|
2016-03-27 21:18:57 +00:00
|
|
|
use Sakura\User;
|
2016-02-15 21:20:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* File controller, handles user uploads like avatars.
|
|
|
|
* @package Sakura
|
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
|
|
*/
|
2016-02-27 16:46:16 +00:00
|
|
|
class FileController extends Controller
|
2016-02-15 21:20:46 +00:00
|
|
|
{
|
2016-09-13 22:05:03 +00:00
|
|
|
/**
|
|
|
|
* Possible modes.
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
private const MODES = [
|
2016-09-13 22:05:03 +00:00
|
|
|
'avatar',
|
|
|
|
'background',
|
|
|
|
'header',
|
|
|
|
];
|
|
|
|
|
2016-03-27 21:18:57 +00:00
|
|
|
/**
|
|
|
|
* The base for serving a file.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param string $data
|
|
|
|
* @param string $mime
|
|
|
|
* @param string $name
|
2016-03-27 21:18:57 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
private function serve(string $data, string $mime, string $name): string
|
2016-02-15 21:20:46 +00:00
|
|
|
{
|
2016-03-27 21:18:57 +00:00
|
|
|
header("Content-Disposition: inline; filename={$name}");
|
|
|
|
header("Content-Type: {$mime}");
|
2016-02-15 21:20:46 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2016-03-27 21:18:57 +00:00
|
|
|
/**
|
2016-09-13 22:05:03 +00:00
|
|
|
* Handles file uploads.
|
|
|
|
* @param string $mode
|
|
|
|
* @param array $file
|
2016-12-04 16:33:52 +00:00
|
|
|
* @param User $user
|
|
|
|
* @throws FileException
|
2016-03-27 21:18:57 +00:00
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
private function upload(string $mode, array $file, User $user): void
|
2016-02-15 21:20:46 +00:00
|
|
|
{
|
2016-09-13 22:05:03 +00:00
|
|
|
// Handle errors
|
|
|
|
switch ($file['error']) {
|
|
|
|
case UPLOAD_ERR_OK:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UPLOAD_ERR_INI_SIZE:
|
|
|
|
case UPLOAD_ERR_FORM_SIZE:
|
|
|
|
throw new FileException("Your file was too large!");
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
case UPLOAD_ERR_PARTIAL:
|
|
|
|
throw new FileException("The upload failed!");
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
case UPLOAD_ERR_NO_TMP_DIR:
|
|
|
|
case UPLOAD_ERR_CANT_WRITE:
|
|
|
|
throw new FileException("Wasn't able to save the file, contact a staff member!");
|
|
|
|
|
|
|
|
case UPLOAD_ERR_EXTENSION:
|
|
|
|
default:
|
|
|
|
throw new FileException("Something prevented the file upload!");
|
2016-02-15 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
// Get the temp filename
|
|
|
|
$tmpName = $file['tmp_name'];
|
|
|
|
|
|
|
|
// Get the image meta data
|
|
|
|
$meta = getimagesize($tmpName);
|
|
|
|
|
|
|
|
// Check if image
|
|
|
|
if (!$meta
|
|
|
|
|| (
|
|
|
|
$meta[2] !== IMAGETYPE_GIF
|
|
|
|
&& $meta[2] !== IMAGETYPE_JPEG
|
|
|
|
&& $meta[2] !== IMAGETYPE_PNG
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new FileException("Please upload a valid image!");
|
2016-02-15 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
// Check dimensions
|
|
|
|
$maxWidth = config("file.{$mode}.max_width");
|
|
|
|
$maxHeight = config("file.{$mode}.max_height");
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
if ($meta[0] > $maxWidth
|
|
|
|
|| $meta[1] > $maxHeight) {
|
|
|
|
throw new FileException("Your image can't be bigger than {$maxWidth}x{$maxHeight}" .
|
|
|
|
", yours was {$meta[0]}x{$meta[1]}!");
|
2016-02-15 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
// Check file size
|
|
|
|
$maxFileSize = config("file.{$mode}.max_file_size");
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
if (filesize($tmpName) > $maxFileSize) {
|
|
|
|
$maxSizeFmt = byte_symbol($maxFileSize);
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
throw new FileException("Your image is not allowed to be larger than {$maxSizeFmt}!");
|
2016-02-15 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
$userId = $user->id;
|
|
|
|
$ext = image_type_to_extension($meta[2]);
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
$filename = "{$mode}_{$userId}{$ext}";
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
// Create the file
|
|
|
|
$file = File::create(file_get_contents($tmpName), $filename, $user);
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
// Delete the old file
|
|
|
|
$this->delete($mode, $user);
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
$column = "user_{$mode}";
|
|
|
|
|
|
|
|
// Save new avatar
|
|
|
|
DB::table('users')
|
|
|
|
->where('user_id', $user->id)
|
|
|
|
->update([
|
|
|
|
$column => $file->id,
|
|
|
|
]);
|
2016-02-15 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 21:18:57 +00:00
|
|
|
/**
|
2016-09-13 22:05:03 +00:00
|
|
|
* Deletes a file.
|
|
|
|
* @param string $mode
|
2016-12-04 16:33:52 +00:00
|
|
|
* @param User $user
|
2016-09-13 22:05:03 +00:00
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function delete(string $mode, User $user): void
|
2016-09-13 22:05:03 +00:00
|
|
|
{
|
|
|
|
$fileId = $user->{$mode};
|
|
|
|
|
|
|
|
if ($fileId) {
|
|
|
|
(new File($fileId))->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Catchall serve.
|
|
|
|
* @param string $method
|
|
|
|
* @param array $params
|
2016-03-27 21:18:57 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function __call(string $method, array $params): ?string
|
2016-02-15 21:20:46 +00:00
|
|
|
{
|
2016-09-13 22:05:03 +00:00
|
|
|
if (!in_array($method, self::MODES)) {
|
|
|
|
throw new HttpRouteNotFoundException;
|
|
|
|
}
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
$user = User::construct($params[0] ?? 0);
|
|
|
|
|
|
|
|
if (session_check()) {
|
2016-11-01 21:14:02 +00:00
|
|
|
$perm_var = "change" . ucfirst(strtolower($method));
|
|
|
|
|
|
|
|
if (!CurrentSession::$user->perms->manageProfileImages
|
2016-09-13 22:05:03 +00:00
|
|
|
&& ($user->id !== CurrentSession::$user->id
|
2016-11-01 21:14:02 +00:00
|
|
|
|| !$user->perms->{$perm_var}
|
|
|
|
|| !$user->activated
|
|
|
|
|| $user->restricted)
|
2016-09-13 22:05:03 +00:00
|
|
|
) {
|
|
|
|
throw new HttpMethodNotAllowedException;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
$error = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->upload($method, $_FILES['file'] ?? null, $user);
|
|
|
|
} catch (FileException $e) {
|
|
|
|
$error = $e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->json(compact('error'));
|
|
|
|
} elseif ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
|
|
|
|
$this->delete($method, $user);
|
2016-12-04 16:33:52 +00:00
|
|
|
return null;
|
2016-09-13 22:05:03 +00:00
|
|
|
}
|
2016-02-15 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:37:00 +00:00
|
|
|
$noFile = path('public/' . str_replace(
|
2016-09-13 22:05:03 +00:00
|
|
|
'%tplname%',
|
|
|
|
Template::$name,
|
|
|
|
config("user.{$method}_none")
|
2016-10-07 17:37:00 +00:00
|
|
|
));
|
2016-09-13 22:05:03 +00:00
|
|
|
$none = [
|
|
|
|
'name' => basename($noFile),
|
|
|
|
'data' => file_get_contents($noFile),
|
|
|
|
'mime' => getimagesizefromstring($noFile)['mime'],
|
|
|
|
];
|
2016-02-15 21:20:46 +00:00
|
|
|
|
2016-11-01 21:14:02 +00:00
|
|
|
if (!$user->activated
|
|
|
|
|| $user->restricted
|
2016-09-13 22:05:03 +00:00
|
|
|
|| !$user->{$method}) {
|
2016-03-27 21:18:57 +00:00
|
|
|
return $this->serve($none['data'], $none['mime'], $none['name']);
|
2016-02-15 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
$serve = new File($user->{$method});
|
2016-02-15 21:20:46 +00:00
|
|
|
|
|
|
|
if (!$serve->id) {
|
2016-03-27 21:18:57 +00:00
|
|
|
return $this->serve($none['data'], $none['mime'], $none['name']);
|
2016-02-15 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 21:18:57 +00:00
|
|
|
return $this->serve($serve->data, $serve->mime, $serve->name);
|
2016-02-15 21:20:46 +00:00
|
|
|
}
|
|
|
|
}
|