helpers.php 8.7 KB

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