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.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-15 21:20:46 +00:00
|
|
|
public function avatar($id = 0)
|
|
|
|
{
|
|
|
|
$noAvatar = ROOT . str_replace(
|
|
|
|
'{{ TPL }}',
|
2016-04-01 15:31:05 +00:00
|
|
|
Template::$name,
|
2016-02-15 21:20:46 +00:00
|
|
|
Config::get('no_avatar_img')
|
|
|
|
);
|
|
|
|
$none = [
|
|
|
|
'name' => basename($noAvatar),
|
|
|
|
'data' => file_get_contents($noAvatar),
|
|
|
|
'mime' => getimagesizefromstring($noAvatar)['mime'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$deactivePath = ROOT . str_replace(
|
|
|
|
'{{ TPL }}',
|
2016-04-01 15:31:05 +00:00
|
|
|
Template::$name,
|
2016-02-15 21:20:46 +00:00
|
|
|
Config::get('deactivated_avatar_img')
|
|
|
|
);
|
|
|
|
$deactive = [
|
|
|
|
'name' => basename($deactivePath),
|
|
|
|
'data' => file_get_contents($deactivePath),
|
|
|
|
'mime' => getimagesizefromstring($deactivePath)['mime'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$bannedPath = ROOT . str_replace(
|
|
|
|
'{{ TPL }}',
|
2016-04-01 15:31:05 +00:00
|
|
|
Template::$name,
|
2016-02-15 21:20:46 +00:00
|
|
|
Config::get('banned_avatar_img')
|
|
|
|
);
|
|
|
|
$banned = [
|
|
|
|
'name' => basename($bannedPath),
|
|
|
|
'data' => file_get_contents($bannedPath),
|
|
|
|
'mime' => getimagesizefromstring($bannedPath)['mime'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$user = User::construct($id);
|
|
|
|
|
|
|
|
if ($user->permission(Site::DEACTIVATED)) {
|
2016-03-27 21:18:57 +00:00
|
|
|
return $this->serve($deactive['data'], $deactive['mime'], $deactive['name']);
|
2016-02-15 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
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->avatar) {
|
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.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-15 21:20:46 +00:00
|
|
|
public function background($id = 0)
|
|
|
|
{
|
|
|
|
$noBg = ROOT . Config::get('no_background_img');
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-15 21:20:46 +00:00
|
|
|
public function header($id = 0)
|
|
|
|
{
|
|
|
|
$noHeader = ROOT . Config::get('no_header_img');
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|