SodiumController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Helpers\File;
  4. use Illuminate\Support\Facades\Validator;
  5. use Unirest\Request\Body;
  6. use Illuminate\Support\Str;
  7. use Illuminate\Http\Request;
  8. use Unirest\Request as Unirest;
  9. use App\Services\CallApiService;
  10. use App\Http\Controllers\Controller;
  11. use Illuminate\Support\Facades\Storage;
  12. class SodiumController extends Controller
  13. {
  14. private $callApiService;
  15. public function __construct(CallApiService $callApiService)
  16. {
  17. $this->callApiService = $callApiService;
  18. }
  19. public function store(Request $request)
  20. {
  21. $arr = $request->getContent();
  22. $data = json_decode($arr);
  23. try {
  24. $decrypted = sodium_crypto_box_seal_open(base64_decode($data->BeforeBase64), base64_decode($data->Key));
  25. return $decrypted;
  26. $retObj = array('AfterBase64' => base64_encode($decrypted));
  27. return response()->json($retObj);
  28. } catch (\Throwable $th) {
  29. return $this->apiArrayResponseBuilder(400, 'Error, data failed to request.'. $arr);
  30. }
  31. }
  32. public function apiArrayResponseBuilder($statusCode = null, $message = null, $data = [])
  33. {
  34. $arr = [
  35. 'status_code' => (isset($statusCode)) ? $statusCode : 500,
  36. 'message' => (isset($message)) ? $message : 'error'
  37. ];
  38. if (count($data) > 0) {
  39. $arr['data'] = $data;
  40. }
  41. return response()->json($arr, $arr['status_code']);
  42. }
  43. public function textSend2(Request $request)
  44. {
  45. // $request = Utils::getParamFile('/etc/request/text-send');
  46. $msgType = 'MMS';
  47. $mainText = $request['TextVars']['TemplateText'];
  48. if ($request['TextVars']['TemplateCode']) {
  49. $gateTokenResponse = $this->callApiService->callApi([
  50. 'url' => 'gate-token-get',
  51. 'data' => [
  52. 'ClientId' => config("app.api.main.ClientId"),
  53. 'BeforeBase64' => base64_encode(sodium_crypto_box_seal(json_encode(config("app.api.main.decrypted")),
  54. base64_decode(config("app.api.main.public_key")))),
  55. ],
  56. ]);
  57. if (isset($gateTokenResponse['apiStatus'])) { return response()->json($gateTokenResponse); }
  58. $response = $this->callApiService->callApi([
  59. 'url' => 'text-template-pick',
  60. 'data' => [
  61. 'Page' => [ [ 'textCode' => $request['TextVars']['TemplateCode'] ] ]
  62. ],
  63. 'headers' => ['GateToken' => $gateTokenResponse['GateToken']]
  64. ]);
  65. $mainText = $response['Page'][0]['MainText'];
  66. // dbr_text_template에서 TemplateCode이용해서 MmsImages 목록을 가져온다.
  67. $mmsImages = $response['Page'][0]['MmsImages'];
  68. switch ($response['Page'][0]['Sort']) {
  69. case '0':
  70. $msgType = 'SMS';
  71. break;
  72. case '1':
  73. $msgType = 'LMS';
  74. break;
  75. case '2':
  76. $msgType = 'MMS';
  77. $mmsImageUrl = explode(',', $mmsImages)[0];
  78. // 밑에 주석풀면 외부 이미지 url 테스트
  79. // $mmsImage = 'https://naxon.dev/assets/img/portrait.jpg';
  80. if (\Str::contains($mmsImageUrl, [url('/'), 'localhost', '127.0.0.1'])) {
  81. // 파일저장 url이 localhost -> file path 가져온다.
  82. $mmsImagePath = explode(url('/'), $mmsImageUrl)[1];
  83. $image = Body::file(Storage::disk('erp')->path($mmsImagePath));
  84. } else {
  85. // 파일저장 url이 외부이면 php 업로드 임시폴더 위치에 파일을 써준다.
  86. $file = File::createFromUrl($mmsImageUrl);
  87. $image = Body::file($file, File::mimeType($file->path()), "{$file->getClientOriginalName()}.{$file->extension()}");
  88. }
  89. break;
  90. }
  91. }
  92. $data = [
  93. 'key' => 'pdrtbljgiyisp6sgghouoxarlr1g8f7t',
  94. 'user_id' => 'bseyewear',
  95. 'sender' => '01090148146',
  96. 'title' => $request['TextVars']['TemplateTitle'],
  97. 'cnt' => count($request['Page']),
  98. 'msg_type' => $msgType,
  99. 'image' => isset($image) ? $image : '',
  100. ];
  101. foreach ($request['Page'] as $i => $item) {
  102. $data['rec_' . ($i + 1)] = $item['Receiver'];
  103. $templateText = $mainText;
  104. foreach ($item['ReplaceVars'] as $replaceVar) {
  105. if (isset($replaceVar['VarValue'])) {
  106. $templateText = Str::replace("{{$replaceVar['VarName']}}", $replaceVar['VarValue'], $templateText);
  107. }
  108. }
  109. $data['msg_' . ($i + 1)] = $templateText;
  110. }
  111. $response = Unirest::post(
  112. 'https://apis.aligo.in/send_mass/',
  113. ['Accept' => 'application/json'],
  114. $data,
  115. );
  116. if (isset($file)) {
  117. // php 업로드 임시폴더에서 파일을 지워준다.
  118. unlink($file->path());
  119. }
  120. return response()->json($response);
  121. }
  122. public function imageTest(Request $request)
  123. {
  124. $data['key'] = "pdrtbljgiyisp6sgghouoxarlr1g8f7t";//인증키
  125. $data['user_id'] = "bseyewear"; // SMS 아이디
  126. $data['sender'] ="01090148146"; // 발신번호
  127. $data['receiver'] = '01086276076'; // 수신번호
  128. $data['msg'] = '%고객명%님. 안녕하세요. API TEST SEND';
  129. $data['msg_type'] = 'MMS';
  130. $image = '/uploads/2021/10/수지1.jpg';
  131. $body = Body::multipart($data, [
  132. 'image' => Storage::disk('erp')->path($image)
  133. ]);
  134. $response = Unirest::post(
  135. 'https://apis.aligo.in/send/',
  136. ['Accept' => 'application/json'],
  137. $body
  138. );
  139. return response()->json($response);
  140. }
  141. public function textSend()
  142. {
  143. if (request('password') !== 'juhyeok') {
  144. return $this->apiArrayResponseBuilder(401, 'Unauthorized');
  145. }
  146. if (request('mode') === '.env.dabory') {
  147. return response()->json(config('app.api'));
  148. }
  149. $gateTokenResponse = $this->callApiService->callApi([
  150. 'url' => 'gate-token-get',
  151. 'data' => [
  152. 'ClientId' => request('ssohost_client_id'),
  153. 'BeforeBase64' => request('ssohost_before_base64')
  154. ],
  155. ]);
  156. if (request('mode') === 'sso-app-page') {
  157. $response = $this->callApiService->callApi([
  158. 'url' => 'sso-app-page',
  159. 'data' => [
  160. 'PageVars' => [
  161. 'Desc' => 'Id',
  162. 'Limit' => 999999,
  163. 'Offset' => 0
  164. ]
  165. ],
  166. 'headers' => ['GateToken' => $gateTokenResponse['GateToken']]
  167. ]);
  168. return response()->json($response['Page']);
  169. }
  170. $response = $this->callApiService->callApi([
  171. 'url' => 'sso-app-pick',
  172. 'data' => [
  173. 'Page' => [
  174. [ 'ClientId' => request('target_client_id', config('app.api.main.ClientId')) ]
  175. // [ 'ClientId' => request('tartarget_client_idget_client_id', config('app.api.main.ClientId')) ]
  176. ]
  177. ],
  178. 'headers' => ['GateToken' => $gateTokenResponse['GateToken']]
  179. ]);
  180. try {
  181. $decrypted = sodium_crypto_box_seal_open(
  182. base64_decode(request('target_before_base64', config('app.api.main.BeforeBase64'))),
  183. base64_decode($response['Page'][0]['DbrKeyPair'])
  184. );
  185. } catch (\SodiumException $e) {
  186. return $this->apiArrayResponseBuilder(400, 'Error, data failed to request.');
  187. }
  188. return response()->json($decrypted);
  189. }
  190. public function prjectIndex()
  191. {
  192. $response = json_decode( Storage::disk('erp')->get('json/project.json'), true );
  193. $project = $response['project'];
  194. $project = collect($project)->map(function ($project) {
  195. $project['desc'] = Str::limit($project['desc'], 30);
  196. return $project;
  197. });
  198. return response()->json([
  199. 'project' => $project,
  200. 'count' => count($project)
  201. ]);
  202. }
  203. public function prjectStore(Request $request)
  204. {
  205. $validator = Validator::make($request->all(), [
  206. 'title' => 'required|max:255',
  207. 'desc' => 'required',
  208. 'image' => 'required',
  209. 'type' => 'required',
  210. ]);
  211. if ($validator->fails()) {
  212. return response()->json($validator->messages());
  213. }
  214. $response = json_decode( Storage::disk('erp')->get('json/project.json'), true );
  215. $newProject = array_merge([ 'id' => $response['project'][count($response['project']) - 1]['id'] + 1 ], request()->all());
  216. $response['project'][] = $newProject;
  217. Storage::disk('erp')->put('json/project.json', json_encode($response));
  218. return response()->json($newProject);
  219. }
  220. public function prjectUpdate(Request $request)
  221. {
  222. $validator = Validator::make($request->all(), [
  223. 'id' => 'required',
  224. 'title' => 'required|max:255',
  225. 'desc' => 'required',
  226. 'image' => 'required',
  227. 'type' => 'required',
  228. ]);
  229. if ($validator->fails()) {
  230. return response()->json($validator->messages());
  231. }
  232. $response = json_decode( Storage::disk('erp')->get('json/project.json'), true );
  233. $updateProject = request()->all();
  234. $check = false;
  235. for ($i = 0; $i < count($response['project']); $i++) {
  236. if ($response['project'][$i]['id'] === $updateProject['id']) {
  237. $response['project'][$i] = $updateProject;
  238. $check = true;
  239. }
  240. }
  241. if (! $check) {
  242. return response()->json('업데이트 데이터가 존재하지 않습니다', 404);
  243. }
  244. Storage::disk('erp')->put('json/project.json', json_encode($response));
  245. return response()->json($updateProject);
  246. }
  247. public function prjectShow($id)
  248. {
  249. $response = json_decode( Storage::disk('erp')->get('json/project.json'), true );
  250. $data = collect($response['project'])->filter(function ($project) use ($id) {
  251. return $project['id'] === (int)$id;
  252. })->first();
  253. if (! $data) {
  254. return response()->json('상세 데이터가 존재하지 않습니다', 404);
  255. }
  256. return response()->json($data);
  257. }
  258. public function prjectDestroy(Request $request)
  259. {
  260. $validator = Validator::make($request->all(), [
  261. 'id' => 'required'
  262. ]);
  263. if ($validator->fails()) {
  264. return response()->json($validator->messages());
  265. }
  266. $response = json_decode( Storage::disk('erp')->get('json/project.json'), true );
  267. $deleteId = request('id');
  268. $deleteData = collect($response['project'])->filter(function ($project) use ($deleteId) {
  269. return $project['id'] === $deleteId;
  270. })->first();
  271. if (! $deleteData) {
  272. return response()->json('삭제 데이터가 존재하지 않습니다', 404);
  273. }
  274. $response['project'] = collect($response['project'])->filter(function ($project) use ($deleteId) {
  275. return $project['id'] !== $deleteId;
  276. })->values()->toArray();
  277. Storage::disk('erp')->put('json/project.json', json_encode($response));
  278. return response()->json($deleteData);
  279. }
  280. }