RegisterController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\User;
  5. use App\Util\Lexer\RestrictedNames;
  6. use Illuminate\Foundation\Auth\RegistersUsers;
  7. use Illuminate\Support\Facades\Hash;
  8. use Illuminate\Support\Facades\Validator;
  9. use Illuminate\Auth\Events\Registered;
  10. use Illuminate\Http\Request;
  11. use App\Services\EmailService;
  12. class RegisterController extends Controller
  13. {
  14. /*
  15. |--------------------------------------------------------------------------
  16. | Register Controller
  17. |--------------------------------------------------------------------------
  18. |
  19. | This controller handles the registration of new users as well as their
  20. | validation and creation. By default this controller uses a trait to
  21. | provide this functionality without requiring any additional code.
  22. |
  23. */
  24. use RegistersUsers;
  25. /**
  26. * Where to redirect users after registration.
  27. *
  28. * @var string
  29. */
  30. protected $redirectTo = '/';
  31. /**
  32. * Create a new controller instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct()
  37. {
  38. $this->middleware('guest');
  39. }
  40. /**
  41. * Get a validator for an incoming registration request.
  42. *
  43. * @param array $data
  44. *
  45. * @return \Illuminate\Contracts\Validation\Validator
  46. */
  47. protected function validator(array $data)
  48. {
  49. if(config('database.default') == 'pgsql') {
  50. $data['username'] = strtolower($data['username']);
  51. $data['email'] = strtolower($data['email']);
  52. }
  53. $usernameRules = [
  54. 'required',
  55. 'min:2',
  56. 'max:15',
  57. 'unique:users',
  58. function ($attribute, $value, $fail) {
  59. $dash = substr_count($value, '-');
  60. $underscore = substr_count($value, '_');
  61. $period = substr_count($value, '.');
  62. if(ends_with($value, ['.php', '.js', '.css'])) {
  63. return $fail('Username is invalid.');
  64. }
  65. if(($dash + $underscore + $period) > 1) {
  66. return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
  67. }
  68. if (!ctype_alpha($value[0])) {
  69. return $fail('Username is invalid. Must start with a letter or number.');
  70. }
  71. if (!ctype_alnum($value[strlen($value) - 1])) {
  72. return $fail('Username is invalid. Must end with a letter or number.');
  73. }
  74. $val = str_replace(['_', '.', '-'], '', $value);
  75. if(!ctype_alnum($val)) {
  76. return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
  77. }
  78. $restricted = RestrictedNames::get();
  79. if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
  80. return $fail('Username cannot be used.');
  81. }
  82. },
  83. ];
  84. $emailRules = [
  85. 'required',
  86. 'string',
  87. 'email',
  88. 'max:255',
  89. 'unique:users',
  90. function ($attribute, $value, $fail) {
  91. $banned = EmailService::isBanned($value);
  92. if($banned) {
  93. return $fail('Email is invalid.');
  94. }
  95. },
  96. ];
  97. $rules = [
  98. 'agecheck' => 'required|accepted',
  99. 'name' => 'nullable|string|max:'.config('pixelfed.max_name_length'),
  100. 'username' => $usernameRules,
  101. 'email' => $emailRules,
  102. 'password' => 'required|string|min:'.config('pixelfed.min_password_length').'|confirmed',
  103. ];
  104. if(config('captcha.enabled')) {
  105. $rules['h-captcha-response'] = 'required|captcha';
  106. }
  107. return Validator::make($data, $rules);
  108. }
  109. /**
  110. * Create a new user instance after a valid registration.
  111. *
  112. * @param array $data
  113. *
  114. * @return \App\User
  115. */
  116. protected function create(array $data)
  117. {
  118. if(config('database.default') == 'pgsql') {
  119. $data['username'] = strtolower($data['username']);
  120. $data['email'] = strtolower($data['email']);
  121. }
  122. return User::create([
  123. 'name' => $data['name'],
  124. 'username' => $data['username'],
  125. 'email' => $data['email'],
  126. 'password' => Hash::make($data['password']),
  127. ]);
  128. }
  129. /**
  130. * Show the application registration form.
  131. *
  132. * @return \Illuminate\Http\Response
  133. */
  134. public function showRegistrationForm()
  135. {
  136. if(config('pixelfed.open_registration')) {
  137. $limit = config('pixelfed.max_users');
  138. if($limit) {
  139. abort_if($limit <= User::count(), 404);
  140. return view('auth.register');
  141. } else {
  142. return view('auth.register');
  143. }
  144. } else {
  145. abort(404);
  146. }
  147. }
  148. /**
  149. * Handle a registration request for the application.
  150. *
  151. * @param \Illuminate\Http\Request $request
  152. * @return \Illuminate\Http\Response
  153. */
  154. public function register(Request $request)
  155. {
  156. abort_if(config('pixelfed.open_registration') == false, 400);
  157. $count = User::count();
  158. $limit = config('pixelfed.max_users');
  159. if(false == config('pixelfed.open_registration') || $limit && $limit <= $count) {
  160. return abort(403);
  161. }
  162. $this->validator($request->all())->validate();
  163. event(new Registered($user = $this->create($request->all())));
  164. $this->guard()->login($user);
  165. return $this->registered($request, $user)
  166. ?: redirect($this->redirectPath());
  167. }
  168. }