Просмотр файла app/Http/Controllers/GuestbookController.php

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