RegisterController.php 5.9 KB

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