View file app/Controllers/GuestbookController.php

File size: 4.29Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Flood;
  9. use App\Models\Guestbook;
  10. use Illuminate\Http\Request;
  11.  
  12. class GuestbookController extends BaseController
  13. {
  14. /**
  15. * Главная страница
  16. *
  17. * @return string
  18. */
  19. public function index(): string
  20. {
  21. $posts = Guestbook::query()
  22. ->orderByDesc('created_at')
  23. ->with('user', 'editUser')
  24. ->paginate(setting('bookpost'));
  25.  
  26. return view('guestbook/index', compact('posts'));
  27. }
  28.  
  29. /**
  30. * Добавление сообщения
  31. *
  32. * @param Request $request
  33. * @param Validator $validator
  34. * @param Flood $flood
  35. *
  36. * @return void
  37. */
  38. public function add(Request $request, Validator $validator, Flood $flood): void
  39. {
  40. $msg = $request->input('msg');
  41. $user = getUser();
  42.  
  43. $validator->equal($request->input('token'), $_SESSION['token'], ['msg' => __('validator.token')])
  44. ->length($msg, 5, setting('guesttextlength'), ['msg' => __('validator.text')])
  45. ->false($flood->isFlood(), ['msg' => __('validator.flood', ['sec' => $flood->getPeriod()])]);
  46.  
  47. /* Проверка для гостей */
  48. if (! $user && setting('bookadds')) {
  49. $validator->true(captchaVerify(), ['protect' => __('validator.captcha')]);
  50. $validator->false(strpos($msg, '//'), ['msg' => __('guestbook.without_links')]);
  51. $validator->length($request->input('guest_name'), 3, 20, ['guest_name' => __('users.name_short_or_long')], false);
  52. } else {
  53. $validator->true($user, ['msg' => __('main.not_authorized')]);
  54. }
  55.  
  56. if ($validator->isValid()) {
  57. $msg = antimat($msg);
  58. $guestName = $request->input('guest_name');
  59.  
  60. if ($user) {
  61. $guestName = null;
  62. $bookscores = setting('bookscores') ? 1 : 0;
  63.  
  64. $user->increment('allguest');
  65. $user->increment('point', $bookscores);
  66. $user->increment('money', 5);
  67. }
  68.  
  69. Guestbook::query()->create([
  70. 'user_id' => $user->id ?? null,
  71. 'text' => $msg,
  72. 'ip' => getIp(),
  73. 'brow' => getBrowser(),
  74. 'guest_name' => $guestName,
  75. 'created_at' => SITETIME,
  76. ]);
  77.  
  78. clearCache('statGuestbook');
  79. $flood->saveState();
  80.  
  81. sendNotify($msg, '/guestbook', __('index.guestbook'));
  82. setFlash('success', __('main.message_added_success'));
  83. } else {
  84. setInput($request->all());
  85. setFlash('danger', $validator->getErrors());
  86. }
  87.  
  88. redirect('/guestbook');
  89. }
  90.  
  91. /**
  92. * Редактирование сообщения
  93. *
  94. * @param int $id
  95. * @param Request $request
  96. * @param Validator $validator
  97. *
  98. * @return string
  99. */
  100. public function edit(int $id, Request $request, Validator $validator): string
  101. {
  102. if (! $user = getUser()) {
  103. abort(403);
  104. }
  105.  
  106. $msg = $request->input('msg');
  107.  
  108. /** @var Guestbook $post */
  109. $post = Guestbook::query()->where('user_id', $user->id)->find($id);
  110.  
  111. if (! $post) {
  112. abort('default', __('main.message_not_found'));
  113. }
  114.  
  115. if ($post->created_at + 600 < SITETIME) {
  116. abort('default', __('main.editing_impossible'));
  117. }
  118.  
  119. if ($request->isMethod('post')) {
  120. $validator->equal($request->input('token'), $_SESSION['token'], ['msg' => __('validator.token')])
  121. ->length($msg, 5, setting('guesttextlength'), ['msg' => __('validator.text')]);
  122.  
  123. if ($validator->isValid()) {
  124. $post->update([
  125. 'text' => antimat($msg),
  126. 'edit_user_id' => $user->id,
  127. 'updated_at' => SITETIME,
  128. ]);
  129.  
  130. setFlash('success', __('main.message_edited_success'));
  131. redirect('/guestbook');
  132. } else {
  133. setInput($request->all());
  134. setFlash('danger', $validator->getErrors());
  135. }
  136. }
  137.  
  138. return view('guestbook/edit', compact('post'));
  139. }
  140. }