ApiController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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((new User)->where([
  118. ['nickname', $nickname],
  119. ['user_id', UID]
  120. ])->exists()) {
  121. throw new Exception;
  122. }
  123. // 유효성 확인
  124. if(!(new AllowNickname)->passes(null, $nickname)) {
  125. throw new Exception;
  126. }
  127. return true;
  128. }catch(Exception) {
  129. return false;
  130. }
  131. }
  132. /**
  133. * 정기 비밀번호 변경 다음에 하기
  134. */
  135. public function passwordCampaignSkip()
  136. {
  137. return (new User)->where('id', UID)->update([
  138. 'password_updated_at' => now()
  139. ]);
  140. }
  141. /**
  142. * TinyMCE 에디터 이미지 첨부 화면
  143. */
  144. public function uploader()
  145. {
  146. return view('component.uploader');
  147. }
  148. /**
  149. * 영화 평점 및 후기
  150. * @method GET
  151. * @see /api/moview/review/latest/{movieCd}
  152. */
  153. public function movieReviewLatest(string $base64, MovieReview $movieReviewModel)
  154. {
  155. try {
  156. if(!$base64) {
  157. throw new Exception('잘못된 접근입니다. [1]');
  158. }
  159. $movieCd = base64_decode($base64);
  160. if(!$movieCd) {
  161. throw new Exception('잘못된 접근입니다. [2]');
  162. }
  163. $latest = $movieReviewModel->latest($movieCd, UID);
  164. if($latest->total > 0) {
  165. foreach($latest->list as &$row) {
  166. $length = strlen($row->sid);
  167. $row->owner = ($row->name . '(' . Str::mask($row->sid, '*', intval($length / 2), $length) . ')');
  168. $row->rate = round($row->rate, 2);
  169. $row->createdAt = date('Y.m.d', strtotime($row->created_at));
  170. }
  171. }
  172. return view(layout('movie.review.latest'), [
  173. 'latest' => $latest
  174. ]);
  175. }catch(Exception $e) {
  176. abort($e->getCode(), $e->getMessage());
  177. }
  178. }
  179. }