Просмотр файла app/Controllers/Admin/BlacklistController.php

Размер файла: 3.77Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers\Admin;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\BlackList;
  9. use App\Models\User;
  10. use Illuminate\Http\Request;
  11.  
  12. class BlacklistController extends AdminController
  13. {
  14. /**
  15. * @var string
  16. */
  17. private $type;
  18.  
  19. /**
  20. * Конструктор
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25.  
  26. if (! isAdmin(User::ADMIN)) {
  27. abort(403, __('errors.forbidden'));
  28. }
  29.  
  30. $types = ['email', 'login', 'domain'];
  31.  
  32. $this->type = request()->input('type', 'email');
  33.  
  34. if (! in_array($this->type, $types, true)) {
  35. abort(404, __('admin.blacklists.type_not_found'));
  36. }
  37. }
  38.  
  39. /**
  40. * Главная страница
  41. *
  42. * @param Request $request
  43. * @param Validator $validator
  44. *
  45. * @return string
  46. */
  47. public function index(Request $request, Validator $validator): string
  48. {
  49. $type = $this->type;
  50.  
  51. if ($request->isMethod('post')) {
  52. $value = utfLower($request->input('value'));
  53.  
  54. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'))
  55. ->length($value, 1, 100, ['value' => __('validator.text')]);
  56.  
  57. if ($type === 'email') {
  58. $validator->regex($value, '#^([a-z0-9_\-\.])+\@([a-z0-9_\-\.])+(\.([a-z0-9])+)+$#', ['value' => __('validator.email')]);
  59. }
  60.  
  61. if ($type === 'login') {
  62. $validator->regex($value, '|^[a-z0-9\-]+$|', ['value' => __('admin.blacklists.invalid_login')])
  63. ->length($value, 3, 20, ['value' => __('validator.text')]);
  64. }
  65.  
  66. if ($type === 'domain') {
  67. $value = siteDomain($value);
  68. $validator->regex($value, '#([а-яa-z0-9_\-\.])+(\.([а-яa-z0-9\/])+)+$#u', ['value' => __('validator.site')]);
  69. }
  70.  
  71. $duplicate = BlackList::query()->where('type', $type)->where('value', $value)->first();
  72. $validator->empty($duplicate, ['value' => __('main.record_exists')]);
  73.  
  74. if ($validator->isValid()) {
  75. BlackList::query()->create([
  76. 'type' => $type,
  77. 'value' => $value,
  78. 'user_id' => getUser('id'),
  79. 'created_at' => SITETIME,
  80. ]);
  81.  
  82. setFlash('success', __('main.record_added_success'));
  83. redirect('/admin/blacklists?type=' . $type);
  84. } else {
  85. setInput($request->all());
  86. setFlash('danger', $validator->getErrors());
  87. }
  88. }
  89.  
  90. $lists = BlackList::query()
  91. ->where('type', $type)
  92. ->orderByDesc('created_at')
  93. ->with('user')
  94. ->paginate(setting('blacklist'))
  95. ->appends(['type' => $type]);
  96.  
  97. return view('admin/blacklists/index', compact('lists', 'type'));
  98. }
  99.  
  100. /**
  101. * Удаление записей
  102. *
  103. * @param Request $request
  104. * @param Validator $validator
  105. *
  106. * @return void
  107. */
  108. public function delete(Request $request, Validator $validator): void
  109. {
  110. $page = int($request->input('page', 1));
  111. $del = intar($request->input('del'));
  112. $type = $this->type;
  113.  
  114. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'))
  115. ->true($del, __('validator.deletion'));
  116.  
  117. if ($validator->isValid()) {
  118. BlackList::query()->where('type', $type)->whereIn('id', $del)->delete();
  119.  
  120. setFlash('success', __('main.records_deleted_success'));
  121. } else {
  122. setFlash('danger', $validator->getErrors());
  123. }
  124.  
  125. redirect('/admin/blacklists?type=' . $type . '&page=' . $page);
  126. }
  127. }