1
0
Fork 0
pixelfed/app/Http/Controllers/Auth/RegisterController.php

237 lines
6.1 KiB
PHP
Raw Normal View History

2018-04-15 23:56:48 +00:00
<?php
namespace App\Http\Controllers\Auth;
2018-08-28 03:07:36 +00:00
use App\Http\Controllers\Controller;
2018-04-15 23:56:48 +00:00
use App\User;
use Purify;
use App\Util\Lexer\RestrictedNames;
2018-08-28 03:07:36 +00:00
use Illuminate\Foundation\Auth\RegistersUsers;
2018-04-15 23:56:48 +00:00
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
2018-10-24 17:56:56 +00:00
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
2019-08-09 19:33:02 +00:00
use App\Services\EmailService;
2023-04-20 07:08:54 +00:00
use App\Services\BouncerService;
2018-04-15 23:56:48 +00:00
class RegisterController extends Controller
{
2021-05-11 05:23:09 +00:00
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
2022-01-25 06:26:38 +00:00
protected $redirectTo = '/i/web';
2021-05-11 05:23:09 +00:00
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
2021-12-29 07:38:08 +00:00
public function getRegisterToken()
{
return \Cache::remember('pf:register:rt', 900, function() {
return str_random(40);
});
}
2021-05-11 05:23:09 +00:00
/**
* Get a validator for an incoming registration request.
*
* @param array $data
*
* @return \Illuminate\Contracts\Validation\Validator
*/
2024-01-11 08:35:15 +00:00
public function validator(array $data)
2021-05-11 05:23:09 +00:00
{
if(config('database.default') == 'pgsql') {
$data['username'] = strtolower($data['username']);
$data['email'] = strtolower($data['email']);
}
$usernameRules = [
'required',
'min:2',
'max:15',
'unique:users',
function ($attribute, $value, $fail) {
$dash = substr_count($value, '-');
$underscore = substr_count($value, '_');
$period = substr_count($value, '.');
if(ends_with($value, ['.php', '.js', '.css'])) {
return $fail('Username is invalid.');
}
if(($dash + $underscore + $period) > 1) {
return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
}
if (!ctype_alnum($value[0])) {
2021-05-11 05:23:09 +00:00
return $fail('Username is invalid. Must start with a letter or number.');
}
if (!ctype_alnum($value[strlen($value) - 1])) {
return $fail('Username is invalid. Must end with a letter or number.');
}
$val = str_replace(['_', '.', '-'], '', $value);
if(!ctype_alnum($val)) {
return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
}
$restricted = RestrictedNames::get();
if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
return $fail('Username cannot be used.');
}
},
];
$emailRules = [
'required',
'string',
'email',
'max:255',
'unique:users',
function ($attribute, $value, $fail) {
$banned = EmailService::isBanned($value);
if($banned) {
return $fail('Email is invalid.');
}
},
];
2021-12-29 07:38:08 +00:00
$rt = [
'required',
function ($attribute, $value, $fail) {
if($value !== $this->getRegisterToken()) {
return $fail('Something went wrong');
}
}
];
2021-05-11 05:23:09 +00:00
$rules = [
'agecheck' => 'required|accepted',
2021-12-29 07:38:08 +00:00
'rt' => $rt,
2021-05-11 05:23:09 +00:00
'name' => 'nullable|string|max:'.config('pixelfed.max_name_length'),
'username' => $usernameRules,
'email' => $emailRules,
'password' => 'required|string|min:'.config('pixelfed.min_password_length').'|confirmed',
];
if(config('captcha.enabled') || config('captcha.active.register')) {
2021-05-11 05:23:09 +00:00
$rules['h-captcha-response'] = 'required|captcha';
}
return Validator::make($data, $rules);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
*
* @return \App\User
*/
2024-01-11 08:35:15 +00:00
public function create(array $data)
2021-05-11 05:23:09 +00:00
{
if(config('database.default') == 'pgsql') {
$data['username'] = strtolower($data['username']);
$data['email'] = strtolower($data['email']);
}
return User::create([
'name' => Purify::clean($data['name']),
2021-05-11 05:23:09 +00:00
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'app_register_ip' => request()->ip()
2021-05-11 05:23:09 +00:00
]);
}
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
2024-02-19 11:00:31 +00:00
if((bool) config_cache('pixelfed.open_registration')) {
2023-04-20 07:08:54 +00:00
if(config('pixelfed.bouncer.cloud_ips.ban_signups')) {
abort_if(BouncerService::checkIp(request()->ip()), 404);
}
2023-06-20 11:03:49 +00:00
$hasLimit = config('pixelfed.enforce_max_users');
if($hasLimit) {
$limit = config('pixelfed.max_users');
$count = User::where(function($q){ return $q->whereNull('status')->orWhereNotIn('status', ['deleted','delete']); })->count();
if($limit <= $count) {
return redirect(route('help.instance-max-users-limit'));
}
abort_if($limit <= $count, 404);
2021-05-11 05:23:09 +00:00
return view('auth.register');
} else {
return view('auth.register');
}
} else {
2024-02-19 11:00:31 +00:00
if((bool) config_cache('instance.curated_registration.enabled') && config('instance.curated_registration.state.fallback_on_closed_reg')) {
return redirect('/auth/sign_up');
} else {
abort(404);
}
2021-05-11 05:23:09 +00:00
}
}
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
abort_if(config_cache('pixelfed.open_registration') == false, 400);
2023-04-20 07:08:54 +00:00
if(config('pixelfed.bouncer.cloud_ips.ban_signups')) {
abort_if(BouncerService::checkIp($request->ip()), 404);
}
2023-06-20 11:03:49 +00:00
$hasLimit = config('pixelfed.enforce_max_users');
if($hasLimit) {
$count = User::where(function($q){ return $q->whereNull('status')->orWhereNotIn('status', ['deleted','delete']); })->count();
$limit = config('pixelfed.max_users');
2021-05-11 05:23:09 +00:00
2023-06-20 11:03:49 +00:00
if($limit && $limit <= $count) {
return redirect(route('help.instance-max-users-limit'));
}
2021-05-11 05:23:09 +00:00
}
2023-06-20 11:03:49 +00:00
2021-05-11 05:23:09 +00:00
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
2018-04-15 23:56:48 +00:00
}