ApiController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Str;
  5. use App\Models\Config;
  6. use App\Models\User;
  7. use App\Models\Movie\MovieReview;
  8. use App\Models\DTO\ResponseData;
  9. use App\Rules\DeniedEmail;
  10. use App\Rules\SpecialCharLength;
  11. use App\Rules\UppercaseLength;
  12. use App\Rules\NumberLength;
  13. use App\Rules\AllowNickname;
  14. use Exception;
  15. class ApiController extends Controller
  16. {
  17. public function __construct()
  18. {
  19. }
  20. /**
  21. * 로그인 확인
  22. */
  23. public function loginCheck(): int
  24. {
  25. return intval(auth()->check());
  26. }
  27. /**
  28. * 금지 단어 확인
  29. */
  30. public function filterSpamKeyword(Request $request, ResponseData $response): ResponseData
  31. {
  32. $subject = $request->input('subject');
  33. $content = $request->input('content');
  34. $response->subject = "";
  35. $response->content = "";
  36. $spamWord = explode(',', trim((new Config)->item("spam_word")));
  37. if ($spamWord) {
  38. for ($i = 0; $i < count($spamWord); $i++) {
  39. $str = trim($spamWord[$i]);
  40. if ($subject) {
  41. $pos = stripos($subject, $str);
  42. if ($pos !== false) {
  43. $response->subject = $str;
  44. break;
  45. }
  46. }
  47. if ($content) {
  48. $pos = stripos($content, $str);
  49. if ($pos !== false) {
  50. $response->content = $str;
  51. break;
  52. }
  53. }
  54. }
  55. }
  56. return $response;
  57. }
  58. /**
  59. * 중복 이메일 여부
  60. */
  61. public function isEmailAble(Request $request): bool
  62. {
  63. try {
  64. $email = $request->input('email');
  65. if (!$email) {
  66. throw new Exception;
  67. }
  68. // 중복 여부
  69. if ((new User)->where('email', $email)->exists()) {
  70. throw new Exception;
  71. }
  72. // 유효성 확인
  73. if (!(new DeniedEmail)->passes(null, $email)) {
  74. throw new Exception;
  75. }
  76. return true;
  77. } catch (Exception) {
  78. return false;
  79. }
  80. }
  81. /**
  82. * 비밀번호 유효성 검사
  83. */
  84. public function isPasswordAble(Request $request): bool
  85. {
  86. try {
  87. $password = $request->input('password');
  88. if (!$password) {
  89. throw new Exception;
  90. }
  91. // 유효성 확인
  92. if (!(new SpecialCharLength)->passes(null, $password)) {
  93. throw new Exception;
  94. }
  95. if (!(new UppercaseLength)->passes(null, $password)) {
  96. throw new Exception;
  97. }
  98. if (!(new NumberLength)->passes(null, $password)) {
  99. throw new Exception;
  100. }
  101. return true;
  102. } catch (Exception) {
  103. return false;
  104. }
  105. }
  106. /**
  107. * 중복 닉네임 여부
  108. */
  109. public function isNicknameAble(Request $request): bool
  110. {
  111. try {
  112. $nickname = $request->input('nickname');
  113. if (!$nickname) {
  114. throw new Exception;
  115. }
  116. // 중복 여부
  117. if (
  118. (new User)->where([
  119. ['nickname', $nickname],
  120. ['user_id', UID]
  121. ])->exists()
  122. ) {
  123. throw new Exception;
  124. }
  125. // 유효성 확인
  126. if (!(new AllowNickname)->passes(null, $nickname)) {
  127. throw new Exception;
  128. }
  129. return true;
  130. } catch (Exception) {
  131. return false;
  132. }
  133. }
  134. /**
  135. * 정기 비밀번호 변경 다음에 하기
  136. */
  137. public function passwordCampaignSkip()
  138. {
  139. return (new User)->where('id', UID)->update([
  140. 'password_updated_at' => now()
  141. ]);
  142. }
  143. /**
  144. * TinyMCE 에디터 이미지 첨부 화면
  145. */
  146. public function uploader()
  147. {
  148. return view('component.uploader');
  149. }
  150. /**
  151. * 영화 평점 및 후기
  152. * @method GET
  153. * @see /api/moview/review/latest/{movieCd}
  154. */
  155. public function movieReviewLatest(string $base64, MovieReview $movieReviewModel)
  156. {
  157. try {
  158. if(!$base64) {
  159. throw new Exception('잘못된 접근입니다. [1]');
  160. }
  161. $movieCd = base64_decode($base64);
  162. if(!$movieCd) {
  163. throw new Exception('잘못된 접근입니다. [2]');
  164. }
  165. $latest = $movieReviewModel->latest($movieCd, UID);
  166. if($latest->total > 0) {
  167. foreach($latest->list as &$row) {
  168. $length = strlen($row->sid);
  169. $row->owner = ($row->name . '(' . Str::mask($row->sid, '*', intval($length / 2), $length) . ')');
  170. $row->rate = round($row->rate, 2);
  171. $row->createdAt = date('Y.m.d', strtotime($row->created_at));
  172. }
  173. }
  174. return view(layout('movie.review.latest'), [
  175. 'latest' => $latest
  176. ]);
  177. }catch(Exception $e) {
  178. abort($e->getCode(), $e->getMessage());
  179. }
  180. }
  181. }