helpers.php 10 KB

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