RegisterController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 = '/i/web';
  31. /**
  32. * Create a new controller instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct()
  37. {
  38. $this->middleware('guest');
  39. }
  40. public function getRegisterToken()
  41. {
  42. return \Cache::remember('pf:register:rt', 900, function() {
  43. return str_random(40);
  44. });
  45. }
  46. /**
  47. * Get a validator for an incoming registration request.
  48. *
  49. * @param array $data
  50. *
  51. * @return \Illuminate\Contracts\Validation\Validator
  52. */
  53. protected function validator(array $data)
  54. {
  55. if(config('database.default') == 'pgsql') {
  56. $data['username'] = strtolower($data['username']);
  57. $data['email'] = strtolower($data['email']);
  58. }
  59. $usernameRules = [
  60. 'required',
  61. 'min:2',
  62. 'max:15',
  63. 'unique:users',
  64. function ($attribute, $value, $fail) {
  65. $dash = substr_count($value, '-');
  66. $underscore = substr_count($value, '_');
  67. $period = substr_count($value, '.');
  68. if(ends_with($value, ['.php', '.js', '.css'])) {
  69. return $fail('Username is invalid.');
  70. }
  71. if(($dash + $underscore + $period) > 1) {
  72. return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
  73. }
  74. if (!ctype_alnum($value[0])) {
  75. return $fail('Username is invalid. Must start with a letter or number.');
  76. }
  77. if (!ctype_alnum($value[strlen($value) - 1])) {
  78. return $fail('Username is invalid. Must end with a letter or number.');
  79. }
  80. $val = str_replace(['_', '.', '-'], '', $value);
  81. if(!ctype_alnum($val)) {
  82. return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
  83. }
  84. $restricted = RestrictedNames::get();
  85. if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
  86. return $fail('Username cannot be used.');
  87. }
  88. },
  89. ];
  90. $emailRules = [
  91. 'required',
  92. 'string',
  93. 'email',
  94. 'max:255',
  95. 'unique:users',
  96. function ($attribute, $value, $fail) {
  97. $banned = EmailService::isBanned($value);
  98. if($banned) {
  99. return $fail('Email is invalid.');
  100. }
  101. },
  102. ];
  103. $rt = [
  104. 'required',
  105. function ($attribute, $value, $fail) {
  106. if($value !== $this->getRegisterToken()) {
  107. return $fail('Something went wrong');
  108. }
  109. }
  110. ];
  111. $rules = [
  112. 'agecheck' => 'required|accepted',
  113. 'rt' => $rt,
  114. 'name' => 'nullable|string|max:'.config('pixelfed.max_name_length'),
  115. 'username' => $usernameRules,
  116. 'email' => $emailRules,
  117. 'password' => 'required|string|min:'.config('pixelfed.min_password_length').'|confirmed',
  118. ];
  119. if(config('captcha.enabled')) {
  120. $rules['h-captcha-response'] = 'required|captcha';
  121. }
  122. return Validator::make($data, $rules);
  123. }
  124. /**
  125. * Create a new user instance after a valid registration.
  126. *
  127. * @param array $data
  128. *
  129. * @return \App\User
  130. */
  131. protected function create(array $data)
  132. {
  133. if(config('database.default') == 'pgsql') {
  134. $data['username'] = strtolower($data['username']);
  135. $data['email'] = strtolower($data['email']);
  136. }
  137. return User::create([
  138. 'name' => $data['name'],
  139. 'username' => $data['username'],
  140. 'email' => $data['email'],
  141. 'password' => Hash::make($data['password']),
  142. ]);
  143. }
  144. /**
  145. * Show the application registration form.
  146. *
  147. * @return \Illuminate\Http\Response
  148. */
  149. public function showRegistrationForm()
  150. {
  151. if(config_cache('pixelfed.open_registration')) {
  152. $limit = config('pixelfed.max_users');
  153. if($limit) {
  154. abort_if($limit <= User::count(), 404);
  155. return view('auth.register');
  156. } else {
  157. return view('auth.register');
  158. }
  159. } else {
  160. abort(404);
  161. }
  162. }
  163. /**
  164. * Handle a registration request for the application.
  165. *
  166. * @param \Illuminate\Http\Request $request
  167. * @return \Illuminate\Http\Response
  168. */
  169. public function register(Request $request)
  170. {
  171. abort_if(config_cache('pixelfed.open_registration') == false, 400);
  172. $count = User::count();
  173. $limit = config('pixelfed.max_users');
  174. if(false == config_cache('pixelfed.open_registration') || $limit && $limit <= $count) {
  175. return abort(403);
  176. }
  177. $this->validator($request->all())->validate();
  178. event(new Registered($user = $this->create($request->all())));
  179. $this->guard()->login($user);
  180. return $this->registered($request, $user)
  181. ?: redirect($this->redirectPath());
  182. }
  183. }