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

293 lines
8.8 KiB
PHP
Raw Normal View History

<?php
/**
* Holds the appearance section controller.
* @package Sakura
*/
namespace Sakura\Controllers\Settings;
2016-08-07 14:10:27 +00:00
use Sakura\CurrentSession;
2016-04-03 21:29:46 +00:00
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-08-05 02:35:37 +00:00
/**
* Handles file uploads.
* @param string $mode
* @param array $file
* @return array
*/
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}!";
}
2016-08-07 14:10:27 +00:00
$userId = CurrentSession::$user->id;
2016-04-24 23:26:36 +00:00
$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
2016-08-07 14:10:27 +00:00
$file = File::create(file_get_contents($tmpName), $filename, CurrentSession::$user);
2016-04-24 23:26:36 +00:00
// Delete the old file
$this->deleteFile($mode);
$column = "user_{$mode}";
// Save new avatar
DB::table('users')
2016-08-07 14:10:27 +00:00
->where('user_id', CurrentSession::$user->id)
2016-04-24 23:26:36 +00:00
->update([
$column => $file->id,
]);
return null;
}
2016-08-05 02:35:37 +00:00
/**
* Deletes a file.
* @param string $mode
*/
2016-04-24 23:26:36 +00:00
public function deleteFile($mode)
{
2016-08-07 14:10:27 +00:00
$fileId = CurrentSession::$user->{$mode};
2016-08-04 21:24:08 +00:00
if ($fileId) {
(new File($fileId))->delete();
}
2016-04-24 23:26:36 +00:00
}
2016-08-05 02:35:37 +00:00
/**
* Renders the avatar changing page
* @return string
*/
public function avatar()
{
2016-04-03 21:29:46 +00:00
// Check permission
2016-08-07 14:10:27 +00:00
if (!CurrentSession::$user->permission(Site::CHANGE_AVATAR)) {
2016-04-03 21:29:46 +00:00
$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');
}
2016-08-05 02:35:37 +00:00
/**
* Renders the background changing page.
* @return string
*/
public function background()
{
2016-04-03 21:29:46 +00:00
// Check permission
2016-08-07 14:10:27 +00:00
if (!CurrentSession::$user->permission(Site::CHANGE_BACKGROUND)) {
2016-04-03 21:29:46 +00:00
$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');
}
2016-08-05 02:35:37 +00:00
/**
* Renders the banner changing page.
* @return string
*/
public function header()
{
2016-04-03 21:29:46 +00:00
// Check permission
2016-08-07 14:10:27 +00:00
if (!CurrentSession::$user->permission(Site::CHANGE_HEADER)) {
2016-04-03 21:29:46 +00:00
$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');
}
2016-08-05 02:35:37 +00:00
/**
* Renders the userpage editing page.
*/
public function userpage()
{
2016-04-03 21:29:46 +00:00
// Check permission
if (!(
2016-08-07 14:10:27 +00:00
CurrentSession::$user->page
&& CurrentSession::$user->permission(Site::CHANGE_USERPAGE)
) && !CurrentSession::$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')
2016-08-07 14:10:27 +00:00
->where('user_id', CurrentSession::$user->id)
2016-08-02 20:35:12 +00:00
->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'));
}
2016-08-05 02:35:37 +00:00
/**
* Renders the signature changing page.
* @return string
*/
public function signature()
{
2016-04-03 21:29:46 +00:00
// Check permission
2016-08-07 14:10:27 +00:00
if (!CurrentSession::$user->permission(Site::CHANGE_SIGNATURE)) {
2016-04-03 21:29:46 +00:00
$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')
2016-08-07 14:10:27 +00:00
->where('user_id', CurrentSession::$user->id)
2016-08-02 20:35:12 +00:00
->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'));
}
}