helpers.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. use App\Exceptions\ApiException;
  3. use App\Services\CallApiService;
  4. use Illuminate\Support\Str;
  5. use Jenssegers\Agent\Agent as Agent;
  6. use Unirest\Request;
  7. function getHashSorderNo($sorderNo)
  8. {
  9. $sorderNoExplode = explode('-', $sorderNo);
  10. return $sorderNoExplode[0] . '-' . generate4DigitHash($sorderNo);
  11. }
  12. function generate4DigitHash($input)
  13. {
  14. // Generate a SHA-256 hash
  15. $hash = hash('sha256', $input);
  16. // Convert hash to base 10 and get the first 4 digits
  17. $decimalHash = base_convert($hash, 16, 10);
  18. // Truncate or pad to ensure it's 4 digits long
  19. $fourDigitHash = substr($decimalHash, 0, 4);
  20. // Ensure it's always 4 digits
  21. return str_pad($fourDigitHash, 4, '0', STR_PAD_LEFT);
  22. }
  23. function detect($view)
  24. {
  25. $agent = new Agent();
  26. if ($agent->isDesktop()) {
  27. $device = 'desktop';
  28. } else if ($agent->isTablet()) {
  29. $device = 'tablet';
  30. } else {
  31. $device = 'mobile';
  32. }
  33. return preg_replace('/(\.)(index)/', '$1' . $device . '.$2', $view);
  34. }
  35. function daboryPath($filename = null)
  36. {
  37. if ($filename) {
  38. return base_path('dabory/' . $filename);
  39. }
  40. return base_path('dabory');
  41. }
  42. function checkIgroupCode($code, $targetCode)
  43. {
  44. $codeList = explode('|', $code);
  45. foreach ($codeList as $code) {
  46. if ($code === $targetCode) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. // MenuCode 뒤에 00붙은거 자르기
  53. function formatDateString($inputString)
  54. {
  55. // Split the input string into an array of substrings with two characters each
  56. $chunks = str_split($inputString, 2);
  57. // Initialize an empty result array
  58. $result = '';
  59. // Iterate through the chunks and process accordingly
  60. foreach ($chunks as $chunk) {
  61. // Check if the chunk is "00"
  62. if ($chunk == "00") {
  63. // If it is, break out of the loop
  64. break;
  65. }
  66. // Otherwise, add the chunk to the result array
  67. $result .= $chunk;
  68. }
  69. return $result;
  70. }
  71. function getFirstTwoDigits($input)
  72. {
  73. // Ensure the string is at least 2 characters long
  74. if (strlen($input) >= 2) {
  75. return substr($input, 0, 2) . '000000';
  76. }
  77. // If the string is shorter than 2 characters, return the string itself
  78. return $input . '000000';
  79. }
  80. function saveJavaScriptCookie( $cookieName )
  81. {
  82. if ( empty( $_COOKIE[$cookieName] ))
  83. {
  84. return null;
  85. }
  86. return $_COOKIE[$cookieName];
  87. }
  88. function getLocale()
  89. {
  90. return app()->getLocale() ?? config('app.locale');
  91. }
  92. function convNum($num)
  93. {
  94. return str_replace(',', '', $num);
  95. }
  96. function isHex($txt)
  97. {
  98. if (strpos($txt, '0x') === 0) {
  99. return true;
  100. }
  101. return false;
  102. }
  103. function strimEmail(string $email)
  104. {
  105. $div = explode('@', $email);
  106. $strim = substr($div[0], '0', -3) . "***";
  107. $strimEmail = $strim . '@' . $div[1];
  108. return $strimEmail;
  109. }
  110. function setupPasswdPolicy($brandCode)
  111. {
  112. return app(CallApiService::class)->callApi([
  113. 'url' => 'setup-get',
  114. 'data' => [
  115. 'SetupCode' => 'passwd-policy',
  116. 'BrandCode' => $brandCode,
  117. ]
  118. ]);
  119. }
  120. function makePasswdRules($passwdPolicy)
  121. {
  122. $passwdRules = [
  123. 'required', 'confirmed', 'min:'.$passwdPolicy['MinLength'],
  124. ];
  125. if ($passwdPolicy['IsUpperMixed']) { $passwdRules[] = 'regex:/[A-Z]/'; }
  126. if ($passwdPolicy['IsSpecialMixed']) { $passwdRules[] = 'regex:/[@$!%*#?&]/'; }
  127. return $passwdRules;
  128. }
  129. function formatPhone($phone)
  130. {
  131. $phone = preg_replace("/[^0-9]/", "", $phone);
  132. $length = strlen($phone);
  133. switch($length){
  134. case 11 :
  135. return preg_replace("/([0-9]{3})([0-9]{4})([0-9]{4})/", "$1-$2-$3", $phone);
  136. break;
  137. case 10:
  138. return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone);
  139. break;
  140. default :
  141. return $phone;
  142. break;
  143. }
  144. }
  145. function msset($mediaPath)
  146. {
  147. return env('MEDIA_URL') . $mediaPath;
  148. }
  149. function csset($assetPath, $versionDate = null)
  150. {
  151. if (env('BROWSER_CACHE')) {
  152. $assetUrl = env('CSSJS_URL');
  153. if (env('VERSION_DATE')) {
  154. if ($assetUrl === 'local') {
  155. $url = asset($assetPath);
  156. } else {
  157. $url = $assetUrl . $assetPath;
  158. }
  159. return $url . '?v' . env('VERSION_DATE');
  160. }
  161. if ($assetUrl === 'local') {
  162. return asset($assetPath);
  163. }
  164. $url = $assetUrl . $assetPath;
  165. if ($versionDate) {
  166. $url = $url . '?v' . $versionDate;
  167. }
  168. return $url;
  169. } else {
  170. return asset($assetPath . '?' . date('YmdHis'));
  171. }
  172. }
  173. function isSecure()
  174. {
  175. $isSecure = false;
  176. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  177. $isSecure = true;
  178. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
  179. $isSecure = true;
  180. }
  181. return $isSecure;
  182. }
  183. function loadEnvFor($envName)
  184. {
  185. $environmentPath = __DIR__ . '/../' . $envName;
  186. if (file_exists($environmentPath))
  187. {
  188. $dotenv = \Dotenv\Dotenv::createImmutable(__DIR__ . '/../', $envName);
  189. $dotenv->load(); //this is important
  190. }
  191. }
  192. function getDisk(): string
  193. {
  194. switch (env('CDN_TYPE')) {
  195. case 'aws-s3':
  196. $disk = 's3';
  197. break;
  198. default:
  199. $disk = 'erp';
  200. break;
  201. }
  202. return $disk;
  203. }
  204. function setupMemberPagemove()
  205. {
  206. return app(CallApiService::class)->callApi([
  207. 'url' => 'setup-get',
  208. 'data' => [
  209. 'SetupCode' => 'member-pagemove',
  210. 'BrandCode' => ''
  211. ]
  212. ]);
  213. }
  214. function getLoginRedirectUrl($previousUrl, $memberId = null)
  215. {
  216. if ($memberId) {
  217. $response = app(CallApiService::class)->callApi([
  218. 'url' => 'member-pick',
  219. 'data' => [
  220. 'Page' => [
  221. [ 'Id' => $memberId ]
  222. ]
  223. ]
  224. ]);
  225. $member = $response['Page'][0];
  226. if ($member['SsoBrand'] !== '' && $member['FirstName'] === '') {
  227. return '/my-page/member-edit';
  228. }
  229. } else {
  230. $memberPagemove = setupMemberPagemove();
  231. if (app(CallApiService::class)->verifyApiError($memberPagemove)) {
  232. return '/';
  233. }
  234. $afterLoginApplyPage = preg_replace('/\s+/', '', explode(',', $memberPagemove['AfterLoginApplyPage']));
  235. $slice = Str::after($previousUrl, url('/'));
  236. if (empty($previousUrl) || Str::contains($slice, $afterLoginApplyPage)) {
  237. return $memberPagemove['AfterLoginUri'];
  238. }
  239. }
  240. return $previousUrl;
  241. }
  242. function setupSsoClientOrLoginInfo($brandCode = 'member')
  243. {
  244. $setup = app(CallApiService::class)->callApi([
  245. 'url' => 'setup-page',
  246. 'data' => [
  247. 'PageVars' => [
  248. 'Query' => "((setup_code = 'sso-client' and brand_code like '{$brandCode}%') or setup_code = 'login-info') and is_on_use = '1'",
  249. 'Limit' => 100
  250. ]
  251. ],
  252. ]);
  253. // dd($setup);
  254. $oauth2InfoList = collect($setup['Page'])->filter(function ($setup) {
  255. return $setup['SetupCode'] !== 'login-info';
  256. })->flatMap(function ($setup) use ($brandCode) {
  257. $key = Str::replace($brandCode . '-', '', $setup['BrandCode']);
  258. return [$key => json_decode($setup['SetupJson'], true)];
  259. })->toArray();
  260. $develLoginInfo = collect($setup['Page'])->filter(function ($setup) {
  261. return $setup['SetupCode'] === 'login-info';
  262. })->map(function ($setup) {
  263. return json_decode($setup['SetupJson'], true);
  264. })->first();
  265. // dd($develLoginInfo);
  266. return [$oauth2InfoList, $develLoginInfo];
  267. }
  268. function breadcrumb($igroupCode, $sort = 'primary'): array
  269. {
  270. $mainMenuPerm = \App\Helpers\Utils::getProMainMenu();
  271. $mainMenuPermPage = collect($mainMenuPerm['Page'])->filter(function ($menu) use ($sort) {
  272. return $menu['Sort'] === $sort;
  273. })->toArray();
  274. $mainMenuList = \App\Helpers\Utils::formatMenuList($mainMenuPermPage, 'MenuCode');
  275. $menuCodeSplit = str_split($igroupCode, 2);
  276. $firstMenu = collect($mainMenuList)->filter(function ($q) use ($menuCodeSplit) {
  277. return Str::startsWith($q['MenuCode'], $menuCodeSplit[0]);
  278. })->first();
  279. if (empty($firstMenu['child']) || ! isset($menuCodeSplit[1])) { return [ $firstMenu ]; }
  280. $secondMenu = collect($firstMenu['child'])->filter(function ($q) use ($menuCodeSplit) {
  281. return Str::startsWith($q['MenuCode'], $menuCodeSplit[0] . $menuCodeSplit[1]);
  282. })->first();
  283. if (empty($secondMenu['child']) || ! isset($menuCodeSplit[2])) { return [ $firstMenu, $secondMenu ]; }
  284. $thirdMenu = collect($secondMenu['child'])->filter(function ($q) use ($menuCodeSplit) {
  285. return Str::startsWith($q['MenuCode'], $menuCodeSplit[0] . $menuCodeSplit[1] . $menuCodeSplit[2]);
  286. })->first();
  287. return [ $firstMenu, $secondMenu, $thirdMenu ];
  288. }
  289. function recaptchaSiteverify($secret, $response)
  290. {
  291. $data = [
  292. 'secret' => $secret,
  293. 'response' => $response,
  294. 'remoteip' => request()->ip()
  295. ];
  296. $response = Request::get('https://www.google.com/recaptcha/api/siteverify',
  297. [ 'Accept' => 'application/json' ],
  298. $data
  299. );
  300. return json_decode(json_encode($response->body ?? []), true)['success'];
  301. }