This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Controllers/FileController.php

159 lines
4.1 KiB
PHP
Raw Normal View History

2016-02-15 21:20:46 +00:00
<?php
/**
* Holds the file controller.
* @package Sakura
*/
namespace Sakura\Controllers;
use Sakura\Config;
use Sakura\File;
use Sakura\Perms\Site;
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-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
*/
private function serve($data, $mime, $name)
2016-02-15 21:20:46 +00:00
{
// Add original filename
2016-03-27 21:18:57 +00:00
header("Content-Disposition: inline; filename={$name}");
2016-02-15 21:20:46 +00:00
// Set content type
2016-03-27 21:18:57 +00:00
header("Content-Type: {$mime}");
2016-02-15 21:20:46 +00:00
// Return image data
return $data;
}
2016-03-27 21:18:57 +00:00
/**
* Attempt to get an avatar.
2016-08-05 02:35:37 +00:00
* @param int $id
2016-03-27 21:18:57 +00:00
* @return string
*/
2016-02-15 21:20:46 +00:00
public function avatar($id = 0)
{
$noAvatar = ROOT . 'public/' . str_replace(
2016-07-26 17:29:53 +00:00
'%tplname%',
2016-04-01 15:31:05 +00:00
Template::$name,
2016-07-26 17:29:53 +00:00
config('user.avatar_none')
2016-02-15 21:20:46 +00:00
);
$none = [
'name' => basename($noAvatar),
'data' => file_get_contents($noAvatar),
'mime' => getimagesizefromstring($noAvatar)['mime'],
];
$bannedPath = ROOT . 'public/' . str_replace(
2016-07-26 17:29:53 +00:00
'%tplname%',
2016-04-01 15:31:05 +00:00
Template::$name,
2016-07-26 17:29:53 +00:00
config('user.avatar_ban')
2016-02-15 21:20:46 +00:00
);
$banned = [
'name' => basename($bannedPath),
'data' => file_get_contents($bannedPath),
'mime' => getimagesizefromstring($bannedPath)['mime'],
];
$user = User::construct($id);
2016-03-28 14:47:43 +00:00
if ($user->permission(Site::RESTRICTED)) {
2016-03-27 21:18:57 +00:00
return $this->serve($banned['data'], $banned['mime'], $banned['name']);
2016-02-15 21:20:46 +00:00
}
if ($user->id < 1 || !$user->avatar || $user->permission(Site::DEACTIVATED)) {
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
}
$serve = new File($user->avatar);
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
}
2016-03-27 21:18:57 +00:00
/**
* Attempt to get a background.
2016-08-05 02:35:37 +00:00
* @param int $id
2016-03-27 21:18:57 +00:00
* @return string
*/
2016-02-15 21:20:46 +00:00
public function background($id = 0)
{
$noBg = ROOT . "public/images/pixel.png";
2016-02-15 21:20:46 +00:00
$none = [
'name' => basename($noBg),
'data' => file_get_contents($noBg),
'mime' => getimagesizefromstring($noBg)['mime'],
];
if (!$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
}
$user = User::construct($id);
2016-03-27 21:18:57 +00:00
if ($user->permission(Site::DEACTIVATED)
|| $user->permission(Site::RESTRICTED)
|| !$user->background) {
return $this->serve($none['data'], $none['mime'], $none['name']);
2016-02-15 21:20:46 +00:00
}
$serve = new File($user->background);
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
}
2016-03-27 21:18:57 +00:00
/**
* Attempt to get a profile header.
2016-08-05 02:35:37 +00:00
* @param int $id
2016-03-27 21:18:57 +00:00
* @return string
*/
2016-02-15 21:20:46 +00:00
public function header($id = 0)
{
$noHeader = ROOT . "public/images/pixel.png";
2016-02-15 21:20:46 +00:00
$none = [
'name' => basename($noHeader),
'data' => file_get_contents($noHeader),
'mime' => getimagesizefromstring($noHeader)['mime'],
];
if (!$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
}
$user = User::construct($id);
2016-03-27 21:18:57 +00:00
if ($user->permission(Site::DEACTIVATED)
|| $user->permission(Site::RESTRICTED)
|| !$user->header) {
return $this->serve($none['data'], $none['mime'], $none['name']);
2016-02-15 21:20:46 +00:00
}
$serve = new File($user->header);
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
}
}