RegisterController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\BouncerService;
  5. use App\Services\EmailService;
  6. use App\User;
  7. use App\Util\Lexer\RestrictedNames;
  8. use Illuminate\Auth\Events\Registered;
  9. use Illuminate\Foundation\Auth\RegistersUsers;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Hash;
  12. use Illuminate\Support\Facades\Validator;
  13. use Purify;
  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. *
  52. * @return \Illuminate\Contracts\Validation\Validator
  53. */
  54. public function validator(array $data)
  55. {
  56. if (config('database.default') == 'pgsql') {
  57. $data['username'] = strtolower($data['username']);
  58. $data['email'] = strtolower($data['email']);
  59. }
  60. $usernameRules = [
  61. 'required',
  62. 'min:2',
  63. 'max:15',
  64. 'unique:users',
  65. function ($attribute, $value, $fail) {
  66. $dash = substr_count($value, '-');
  67. $underscore = substr_count($value, '_');
  68. $period = substr_count($value, '.');
  69. if (ends_with($value, ['.php', '.js', '.css'])) {
  70. return $fail('Username is invalid.');
  71. }
  72. if (($dash + $underscore + $period) > 1) {
  73. return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
  74. }
  75. if (! ctype_alnum($value[0])) {
  76. return $fail('Username is invalid. Must start with a letter or number.');
  77. }
  78. if (! ctype_alnum($value[strlen($value) - 1])) {
  79. return $fail('Username is invalid. Must end with a letter or number.');
  80. }
  81. $val = str_replace(['_', '.', '-'], '', $value);
  82. if (! ctype_alnum($val)) {
  83. return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
  84. }
  85. if (! preg_match('/[a-zA-Z]/', $value)) {
  86. return $fail('Username is invalid. Must contain at least one alphabetical character.');
  87. }
  88. $restricted = RestrictedNames::get();
  89. if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
  90. return $fail('Username cannot be used.');
  91. }
  92. },
  93. ];
  94. $emailRules = [
  95. 'required',
  96. 'string',
  97. 'email',
  98. 'max:255',
  99. 'unique:users',
  100. function ($attribute, $value, $fail) {
  101. $banned = EmailService::isBanned($value);
  102. if ($banned) {
  103. return $fail('Email is invalid.');
  104. }
  105. },
  106. ];
  107. $rt = [
  108. 'required',
  109. function ($attribute, $value, $fail) {
  110. if ($value !== $this->getRegisterToken()) {
  111. return $fail('Something went wrong');
  112. }
  113. },
  114. ];
  115. $rules = [
  116. 'agecheck' => 'required|accepted',
  117. 'rt' => $rt,
  118. 'name' => 'nullable|string|max:'.config('pixelfed.max_name_length'),
  119. 'username' => $usernameRules,
  120. 'email' => $emailRules,
  121. 'password' => 'required|string|min:'.config('pixelfed.min_password_length').'|confirmed',
  122. ];
  123. if ((bool) config_cache('captcha.enabled') && (bool) config_cache('captcha.active.register')) {
  124. $rules['h-captcha-response'] = 'required|captcha';
  125. }
  126. return Validator::make($data, $rules);
  127. }
  128. /**
  129. * Create a new user instance after a valid registration.
  130. *
  131. *
  132. * @return \App\User
  133. */
  134. public function create(array $data)
  135. {
  136. if (config('database.default') == 'pgsql') {
  137. $data['username'] = strtolower($data['username']);
  138. $data['email'] = strtolower($data['email']);
  139. }
  140. return User::create([
  141. 'name' => Purify::clean($data['name']),
  142. 'username' => $data['username'],
  143. 'email' => $data['email'],
  144. 'password' => Hash::make($data['password']),
  145. 'app_register_ip' => request()->ip(),
  146. ]);
  147. }
  148. /**
  149. * Show the application registration form.
  150. *
  151. * @return \Illuminate\Http\Response
  152. */
  153. public function showRegistrationForm()
  154. {
  155. if ((bool) config_cache('pixelfed.open_registration')) {
  156. if (config('pixelfed.bouncer.cloud_ips.ban_signups')) {
  157. abort_if(BouncerService::checkIp(request()->ip()), 404);
  158. }
  159. $hasLimit = config('pixelfed.enforce_max_users');
  160. if ($hasLimit) {
  161. $limit = config('pixelfed.max_users');
  162. $count = User::where(function ($q) {
  163. return $q->whereNull('status')->orWhereNotIn('status', ['deleted', 'delete']);
  164. })->count();
  165. if ($limit <= $count) {
  166. return redirect(route('help.instance-max-users-limit'));
  167. }
  168. abort_if($limit <= $count, 404);
  169. return view('auth.register');
  170. } else {
  171. return view('auth.register');
  172. }
  173. } else {
  174. if ((bool) config_cache('instance.curated_registration.enabled') && config('instance.curated_registration.state.fallback_on_closed_reg')) {
  175. return redirect('/auth/sign_up');
  176. } else {
  177. abort(404);
  178. }
  179. }
  180. }
  181. /**
  182. * Handle a registration request for the application.
  183. *
  184. * @return \Illuminate\Http\Response
  185. */
  186. public function register(Request $request)
  187. {
  188. abort_if(config_cache('pixelfed.open_registration') == false, 400);
  189. if (config('pixelfed.bouncer.cloud_ips.ban_signups')) {
  190. abort_if(BouncerService::checkIp($request->ip()), 404);
  191. }
  192. $hasLimit = config('pixelfed.enforce_max_users');
  193. if ($hasLimit) {
  194. $count = User::where(function ($q) {
  195. return $q->whereNull('status')->orWhereNotIn('status', ['deleted', 'delete']);
  196. })->count();
  197. $limit = config('pixelfed.max_users');
  198. if ($limit && $limit <= $count) {
  199. return redirect(route('help.instance-max-users-limit'));
  200. }
  201. }
  202. $this->validator($request->all())->validate();
  203. event(new Registered($user = $this->create($request->all())));
  204. $this->guard()->login($user);
  205. return $this->registered($request, $user)
  206. ?: redirect($this->redirectPath());
  207. }
  208. }