1
0
Fork 0
forked from mirror/pixelfed
pixelfed/app/Http/Controllers/SettingsController.php

258 lines
7.1 KiB
PHP
Raw Normal View History

2018-05-26 22:53:21 +00:00
<?php
namespace App\Http\Controllers;
2018-08-28 03:07:36 +00:00
use App\AccountLog;
use App\EmailVerification;
use App\Media;
use App\Profile;
use App\User;
2018-08-10 03:17:24 +00:00
use App\Util\Lexer\PrettyNumber;
2018-08-28 03:07:36 +00:00
use Auth;
use DB;
use Illuminate\Http\Request;
2018-05-26 22:53:21 +00:00
class SettingsController extends Controller
{
public function __construct()
{
2018-08-10 03:17:24 +00:00
$this->middleware('auth');
2018-05-26 22:53:21 +00:00
}
public function home()
{
2018-08-28 03:07:36 +00:00
$id = Auth::user()->profile->id;
$storage = [];
$used = Media::whereProfileId($id)->sum('size');
$storage['limit'] = config('pixelfed.max_account_size') * 1024;
$storage['used'] = $used;
$storage['percentUsed'] = ceil($storage['used'] / $storage['limit'] * 100);
$storage['limitPretty'] = PrettyNumber::size($storage['limit']);
$storage['usedPretty'] = PrettyNumber::size($storage['used']);
return view('settings.home', compact('storage'));
2018-05-26 22:53:21 +00:00
}
public function homeUpdate(Request $request)
{
2018-08-28 03:07:36 +00:00
$this->validate($request, [
'name' => 'required|string|max:'.config('pixelfed.max_name_length'),
'bio' => 'nullable|string|max:'.config('pixelfed.max_bio_length'),
2018-08-13 02:50:49 +00:00
'website' => 'nullable|url',
2018-08-28 03:07:36 +00:00
'email' => 'nullable|email',
2018-05-26 22:53:21 +00:00
]);
2018-08-28 03:07:36 +00:00
$changes = false;
$name = $request->input('name');
$bio = $request->input('bio');
$website = $request->input('website');
$email = $request->input('email');
$user = Auth::user();
$profile = $user->profile;
2018-05-26 22:53:21 +00:00
2018-08-28 03:07:36 +00:00
$validate = config('pixelfed.enforce_email_verification');
2018-08-13 02:50:49 +00:00
2018-08-28 03:07:36 +00:00
if ($user->email != $email) {
$changes = true;
$user->email = $email;
2018-08-28 03:07:36 +00:00
if ($validate) {
$user->email_verified_at = null;
// Prevent old verifications from working
EmailVerification::whereUserId($user->id)->delete();
}
2018-08-13 02:50:49 +00:00
}
2018-08-28 03:07:36 +00:00
// Only allow email to be updated if not yet verified
if (!$validate || !$changes && $user->email_verified_at) {
if ($profile->name != $name) {
$changes = true;
$user->name = $name;
$profile->name = $name;
}
2018-08-13 02:50:49 +00:00
2018-08-28 03:07:36 +00:00
if (!$profile->website || $profile->website != $website) {
$changes = true;
$profile->website = $website;
}
if (!$profile->bio || !$profile->bio != $bio) {
$changes = true;
$profile->bio = $bio;
}
2018-08-13 02:50:49 +00:00
}
2018-05-26 22:53:21 +00:00
2018-08-28 03:07:36 +00:00
if ($changes === true) {
$user->save();
$profile->save();
return redirect('/settings/home')->with('status', 'Profile successfully updated!');
}
2018-06-01 03:11:56 +00:00
2018-08-28 03:07:36 +00:00
return redirect('/settings/home');
2018-05-26 22:53:21 +00:00
}
public function password()
{
2018-08-28 03:07:36 +00:00
return view('settings.password');
2018-05-26 22:53:21 +00:00
}
2018-06-01 03:11:56 +00:00
public function passwordUpdate(Request $request)
{
2018-08-28 03:07:36 +00:00
$this->validate($request, [
'current' => 'required|string',
'password' => 'required|string',
2018-06-01 03:11:56 +00:00
'password_confirmation' => 'required|string',
]);
2018-08-28 03:07:36 +00:00
$current = $request->input('current');
$new = $request->input('password');
$confirm = $request->input('password_confirmation');
$user = Auth::user();
2018-06-01 03:11:56 +00:00
2018-08-28 03:07:36 +00:00
if (password_verify($current, $user->password) && $new === $confirm) {
$user->password = bcrypt($new);
$user->save();
2018-06-01 03:11:56 +00:00
2018-08-28 03:07:36 +00:00
return redirect('/settings/home')->with('status', 'Password successfully updated!');
}
2018-06-01 03:11:56 +00:00
2018-08-28 03:07:36 +00:00
return redirect('/settings/home')->with('error', 'There was an error with your request!');
2018-06-01 03:11:56 +00:00
}
2018-05-26 22:53:21 +00:00
public function email()
{
2018-08-28 03:07:36 +00:00
return view('settings.email');
2018-05-26 22:53:21 +00:00
}
public function avatar()
{
2018-08-28 03:07:36 +00:00
return view('settings.avatar');
2018-05-26 22:53:21 +00:00
}
public function accessibility()
{
2018-08-28 03:07:36 +00:00
$settings = Auth::user()->settings;
return view('settings.accessibility', compact('settings'));
}
public function accessibilityStore(Request $request)
{
2018-08-28 03:07:36 +00:00
$settings = Auth::user()->settings;
$fields = [
'compose_media_descriptions',
'reduce_motion',
'optimize_screen_reader',
'high_contrast_mode',
2018-08-28 03:07:36 +00:00
'video_autoplay',
];
2018-08-28 03:07:36 +00:00
foreach ($fields as $field) {
$form = $request->input($field);
if ($form == 'on') {
$settings->{$field} = true;
} else {
$settings->{$field} = false;
}
$settings->save();
}
return redirect(route('settings.accessibility'))->with('status', 'Settings successfully updated!');
}
2018-05-26 22:53:21 +00:00
public function notifications()
{
2018-08-28 03:07:36 +00:00
return view('settings.notifications');
2018-05-26 22:53:21 +00:00
}
public function privacy()
{
2018-08-28 03:07:36 +00:00
$settings = Auth::user()->settings;
$is_private = Auth::user()->profile->is_private;
$settings['is_private'] = (bool) $is_private;
return view('settings.privacy', compact('settings'));
}
public function privacyStore(Request $request)
{
2018-08-28 03:07:36 +00:00
$settings = Auth::user()->settings;
$profile = Auth::user()->profile;
$fields = [
'is_private',
'crawlable',
2018-08-22 03:18:14 +00:00
'show_profile_follower_count',
2018-08-28 03:07:36 +00:00
'show_profile_following_count',
];
2018-08-28 03:07:36 +00:00
foreach ($fields as $field) {
$form = $request->input($field);
if ($field == 'is_private') {
if ($form == 'on') {
$profile->{$field} = true;
$settings->show_guests = false;
$settings->show_discover = false;
$profile->save();
} else {
$profile->{$field} = false;
$profile->save();
}
} elseif ($field == 'crawlable') {
if ($form == 'on') {
$settings->{$field} = false;
} else {
$settings->{$field} = true;
}
} else {
2018-08-28 03:07:36 +00:00
if ($form == 'on') {
$settings->{$field} = true;
} else {
$settings->{$field} = false;
}
}
2018-08-28 03:07:36 +00:00
$settings->save();
}
return redirect(route('settings.privacy'))->with('status', 'Settings successfully updated!');
2018-05-26 22:53:21 +00:00
}
public function security()
{
2018-08-28 03:07:36 +00:00
$sessions = DB::table('sessions')
->whereUserId(Auth::id())
->limit(20)
->get();
2018-08-28 03:07:36 +00:00
$activity = AccountLog::whereUserId(Auth::id())
->orderBy('created_at', 'desc')
->limit(50)
->get();
2018-08-28 03:07:36 +00:00
return view('settings.security', compact('sessions', 'activity'));
2018-05-26 22:53:21 +00:00
}
public function applications()
{
2018-08-28 03:07:36 +00:00
return view('settings.applications');
2018-05-26 22:53:21 +00:00
}
public function dataExport()
{
2018-08-28 03:07:36 +00:00
return view('settings.dataexport');
2018-05-26 22:53:21 +00:00
}
public function dataImport()
{
2018-08-28 03:07:36 +00:00
return view('settings.import.home');
2018-05-26 22:53:21 +00:00
}
public function dataImportInstagram()
{
2018-08-28 03:07:36 +00:00
return view('settings.import.instagram.home');
2018-05-26 22:53:21 +00:00
}
public function developers()
{
2018-08-28 03:07:36 +00:00
return view('settings.developers');
2018-05-26 22:53:21 +00:00
}
}