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/Settings/AppearanceController.php

266 lines
8.2 KiB
PHP
Raw Normal View History

<?php
/**
* Holds the appearance section controller.
*
* @package Sakura
*/
namespace Sakura\Controllers\Settings;
2016-04-03 21:29:46 +00:00
use Sakura\ActiveUser;
use Sakura\DB;
2016-04-24 23:26:36 +00:00
use Sakura\File;
2016-04-03 21:29:46 +00:00
use Sakura\Perms\Site;
/**
* Appearance settings.
*
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class AppearanceController extends Controller
{
2016-04-24 23:26:36 +00:00
private function handleUpload($mode, $file)
{
// Handle errors
switch ($file['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
return "Your file was too large!";
case UPLOAD_ERR_PARTIAL:
return "The upload failed!";
case UPLOAD_ERR_NO_TMP_DIR:
case UPLOAD_ERR_CANT_WRITE:
return "Wasn't able to save the file, contact a staff member!";
case UPLOAD_ERR_EXTENSION:
default:
return "Something prevented the file upload!";
}
// Get the temp filename
$tmpName = $_FILES[$mode]['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
)
) {
return "Please upload a valid image!";
}
2016-07-26 17:29:53 +00:00
$confp = $mode === 'header' ? 'cover' : $mode;
2016-04-24 23:26:36 +00:00
// Check dimensions
2016-07-26 17:29:53 +00:00
$maxWidth = config("file.{$confp}.max_width");
$maxHeight = config("file.{$confp}.max_height");
2016-04-24 23:26:36 +00:00
2016-07-30 13:48:09 +00:00
if ($meta[0] > $maxWidth
2016-04-24 23:26:36 +00:00
|| $meta[1] > $maxHeight) {
return "Your image has to be at least {$minWidth}x{$minHeight}"
. " and not bigger than {$maxWidth}x{$maxHeight}, yours was {$meta[0]}x{$meta[1]}!";
}
// Check file size
2016-07-26 17:29:53 +00:00
$maxFileSize = config("file.{$confp}.max_file_size");
2016-04-24 23:26:36 +00:00
if (filesize($tmpName) > $maxFileSize) {
$maxSizeFmt = byte_symbol($maxFileSize);
return "Your image is not allowed to be larger than {$maxSizeFmt}!";
}
$userId = ActiveUser::$user->id;
$ext = image_type_to_extension($meta[2]);
2016-08-04 21:24:08 +00:00
$filename = "{$mode}_{$userId}{$ext}";
2016-04-24 23:26:36 +00:00
// Create the file
$file = File::create(file_get_contents($tmpName), $filename, ActiveUser::$user);
// Delete the old file
$this->deleteFile($mode);
$column = "user_{$mode}";
// Save new avatar
DB::table('users')
->where('user_id', ActiveUser::$user->id)
->update([
$column => $file->id,
]);
return null;
}
public function deleteFile($mode)
{
2016-08-04 21:24:08 +00:00
$fileId = ActiveUser::$user->{$mode};
if ($fileId) {
(new File($fileId))->delete();
}
2016-04-24 23:26:36 +00:00
}
public function avatar()
{
2016-04-03 21:29:46 +00:00
// Check permission
if (!ActiveUser::$user->permission(Site::CHANGE_AVATAR)) {
$message = "You aren't allowed to change your avatar.";
2016-08-02 20:35:12 +00:00
$redirect = route('settings.index');
return view('global/information', compact('message', 'redirect'));
2016-04-03 21:29:46 +00:00
}
2016-08-02 20:35:12 +00:00
if (session_check()) {
2016-04-24 23:26:36 +00:00
$avatar = $_FILES['avatar'] ?? null;
2016-08-02 20:35:12 +00:00
$redirect = route('settings.appearance.avatar');
2016-04-24 23:26:36 +00:00
if ($avatar && $avatar['error'] !== UPLOAD_ERR_NO_FILE) {
$upload = $this->handleUpload('avatar', $_FILES['avatar']);
$message = $upload !== null ? $upload : "Changed your avatar!";
} else {
$this->deleteFile('avatar');
$message = "Deleted your avatar!";
}
2016-08-02 20:35:12 +00:00
return view('global/information', compact('message', 'redirect'));
2016-04-24 23:26:36 +00:00
}
2016-08-02 20:35:12 +00:00
return view('settings/appearance/avatar');
}
public function background()
{
2016-04-03 21:29:46 +00:00
// Check permission
if (!ActiveUser::$user->permission(Site::CHANGE_BACKGROUND)) {
$message = "You aren't allowed to change your background.";
2016-08-02 20:35:12 +00:00
$redirect = route('settings.index');
return view('global/information', compact('message', 'redirect'));
2016-04-03 21:29:46 +00:00
}
2016-08-02 20:35:12 +00:00
if (session_check()) {
2016-04-24 23:26:36 +00:00
$background = $_FILES['background'] ?? null;
2016-08-02 20:35:12 +00:00
$redirect = route('settings.appearance.background');
2016-04-24 23:26:36 +00:00
if ($background && $background['error'] !== UPLOAD_ERR_NO_FILE) {
$upload = $this->handleUpload('background', $_FILES['background']);
$message = $upload !== null ? $upload : "Changed your background!";
} else {
$this->deleteFile('background');
$message = "Deleted your background!";
}
2016-08-02 20:35:12 +00:00
return view('global/information', compact('message', 'redirect'));
2016-04-24 23:26:36 +00:00
}
2016-08-02 20:35:12 +00:00
return view('settings/appearance/background');
}
public function header()
{
2016-04-03 21:29:46 +00:00
// Check permission
if (!ActiveUser::$user->permission(Site::CHANGE_HEADER)) {
$message = "You aren't allowed to change your profile header.";
2016-08-02 20:35:12 +00:00
$redirect = route('settings.index');
return view('global/information', compact('message', 'redirect'));
2016-04-03 21:29:46 +00:00
}
2016-08-02 20:35:12 +00:00
if (session_check()) {
2016-04-24 23:26:36 +00:00
$header = $_FILES['header'] ?? null;
2016-08-02 20:35:12 +00:00
$redirect = route('settings.appearance.header');
2016-04-24 23:26:36 +00:00
if ($header && $header['error'] !== UPLOAD_ERR_NO_FILE) {
$upload = $this->handleUpload('header', $_FILES['header']);
$message = $upload !== null ? $upload : "Changed your header!";
} else {
$this->deleteFile('header');
$message = "Deleted your header!";
}
2016-08-02 20:35:12 +00:00
return view('global/information', compact('message', 'redirect'));
2016-04-24 23:26:36 +00:00
}
2016-04-03 21:29:46 +00:00
2016-08-02 20:35:12 +00:00
return view('settings/appearance/header');
}
public function userpage()
{
2016-04-03 21:29:46 +00:00
// Check permission
if (!(
ActiveUser::$user->page
&& ActiveUser::$user->permission(Site::CHANGE_USERPAGE)
2016-08-02 20:35:12 +00:00
) && !ActiveUser::$user->permission(Site::CREATE_USERPAGE)) {
2016-04-03 21:29:46 +00:00
$message = "You aren't allowed to change your userpage.";
2016-08-02 20:35:12 +00:00
$redirect = route('settings.index');
return view('global/information', compact('message', 'redirect'));
2016-04-03 21:29:46 +00:00
}
$userpage = $_POST['userpage'] ?? null;
2016-08-02 20:35:12 +00:00
$maxLength = config('user.page_max');
2016-04-03 21:29:46 +00:00
2016-08-02 20:35:12 +00:00
if (session_check() && $userpage) {
$redirect = route('settings.appearance.userpage');
2016-04-03 21:29:46 +00:00
if (strlen($userpage) > $maxLength) {
$message = 'Your userpage is too long, shorten it a little!';
2016-08-02 20:35:12 +00:00
} else {
DB::table('users')
->where('user_id', ActiveUser::$user->id)
->update([
'user_page' => $userpage,
]);
2016-04-03 21:29:46 +00:00
2016-08-02 20:35:12 +00:00
$message = 'Updated your userpage!';
}
2016-04-03 21:29:46 +00:00
2016-08-02 20:35:12 +00:00
return view('global/information', compact('message', 'redirect'));
2016-04-03 21:29:46 +00:00
}
2016-08-02 20:35:12 +00:00
return view('settings/appearance/userpage', compact('maxLength'));
}
public function signature()
{
2016-04-03 21:29:46 +00:00
// Check permission
if (!ActiveUser::$user->permission(Site::CHANGE_SIGNATURE)) {
$message = "You aren't allowed to change your signature.";
2016-08-02 20:35:12 +00:00
$redirect = route('settings.index');
return view('global/information', compact('message', 'redirect'));
2016-04-03 21:29:46 +00:00
}
$signature = $_POST['signature'] ?? null;
2016-08-02 20:35:12 +00:00
$maxLength = config('user.signature_max');
2016-04-03 21:29:46 +00:00
2016-08-02 20:35:12 +00:00
if (session_check() && $signature) {
$redirect = route('settings.appearance.signature');
2016-04-03 21:29:46 +00:00
if (strlen($signature) > $maxLength) {
$message = 'Your signature is too long, shorten it a little!';
2016-08-02 20:35:12 +00:00
} else {
DB::table('users')
->where('user_id', ActiveUser::$user->id)
->update([
'user_signature' => $signature,
]);
2016-04-03 21:29:46 +00:00
2016-08-02 20:35:12 +00:00
$message = 'Updated your signature!';
}
2016-04-03 21:29:46 +00:00
2016-08-02 20:35:12 +00:00
return view('global/information', compact('message', 'redirect'));
2016-04-03 21:29:46 +00:00
}
2016-08-02 20:35:12 +00:00
return view('settings/appearance/signature', compact('maxLength'));
}
}