helpers.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 saveJavaScriptCookie( $cookieName )
  56. {
  57. if ( empty( $_COOKIE[$cookieName] ))
  58. {
  59. return null;
  60. }
  61. return $_COOKIE[$cookieName];
  62. }
  63. function getLocale()
  64. {
  65. return app()->getLocale() ?? config('app.locale');
  66. }
  67. function convNum($num)
  68. {
  69. return str_replace(',', '', $num);
  70. }
  71. function isHex($txt)
  72. {
  73. if (strpos($txt, '0x') === 0) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. function strimEmail(string $email)
  79. {
  80. $div = explode('@', $email);
  81. $strim = substr($div[0], '0', -3) . "***";
  82. $strimEmail = $strim . '@' . $div[1];
  83. return $strimEmail;
  84. }
  85. function setupPasswdPolicy($brandCode)
  86. {
  87. return app(CallApiService::class)->callApi([
  88. 'url' => 'setup-get',
  89. 'data' => [
  90. 'SetupCode' => 'passwd-policy',
  91. 'BrandCode' => $brandCode,
  92. ]
  93. ]);
  94. }
  95. function makePasswdRules($passwdPolicy)
  96. {
  97. $passwdRules = [
  98. 'required', 'confirmed', 'min:'.$passwdPolicy['MinLength'],
  99. ];
  100. if ($passwdPolicy['IsUpperMixed']) { $passwdRules[] = 'regex:/[A-Z]/'; }
  101. if ($passwdPolicy['IsSpecialMixed']) { $passwdRules[] = 'regex:/[@$!%*#?&]/'; }
  102. return $passwdRules;
  103. }
  104. function formatPhone($phone)
  105. {
  106. $phone = preg_replace("/[^0-9]/", "", $phone);
  107. $length = strlen($phone);
  108. switch($length){
  109. case 11 :
  110. return preg_replace("/([0-9]{3})([0-9]{4})([0-9]{4})/", "$1-$2-$3", $phone);
  111. break;
  112. case 10:
  113. return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone);
  114. break;
  115. default :
  116. return $phone;
  117. break;
  118. }
  119. }
  120. function msset($mediaPath)
  121. {
  122. return env('MEDIA_URL') . $mediaPath;
  123. }
  124. function csset($assetPath, $versionDate = null)
  125. {
  126. if (env('BROWSER_CACHE')) {
  127. $assetUrl = env('CSSJS_URL');
  128. if (env('VERSION_DATE')) {
  129. if ($assetUrl === 'local') {
  130. $url = asset($assetPath);
  131. } else {
  132. $url = $assetUrl . $assetPath;
  133. }
  134. return $url . '?v' . env('VERSION_DATE');
  135. }
  136. if ($assetUrl === 'local') {
  137. return asset($assetPath);
  138. }
  139. $url = $assetUrl . $assetPath;
  140. if ($versionDate) {
  141. $url = $url . '?v' . $versionDate;
  142. }
  143. return $url;
  144. } else {
  145. return asset($assetPath . '?' . date('YmdHis'));
  146. }
  147. }
  148. function isSecure()
  149. {
  150. $isSecure = false;
  151. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  152. $isSecure = true;
  153. } 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') {
  154. $isSecure = true;
  155. }
  156. return $isSecure;
  157. }
  158. function loadEnvFor($envName)
  159. {
  160. $environmentPath = __DIR__ . '/../' . $envName;
  161. if (file_exists($environmentPath))
  162. {
  163. $dotenv = \Dotenv\Dotenv::createImmutable(__DIR__ . '/../', $envName);
  164. $dotenv->load(); //this is important
  165. }
  166. }
  167. function getDisk(): string
  168. {
  169. switch (env('CDN_TYPE')) {
  170. case 'aws-s3':
  171. $disk = 's3';
  172. break;
  173. default:
  174. $disk = 'erp';
  175. break;
  176. }
  177. return $disk;
  178. }
  179. function setupMemberPagemove()
  180. {
  181. return app(CallApiService::class)->callApi([
  182. 'url' => 'setup-get',
  183. 'data' => [
  184. 'SetupCode' => 'member-pagemove',
  185. 'BrandCode' => ''
  186. ]
  187. ]);
  188. }
  189. function getLoginRedirectUrl($previousUrl, $memberId = null)
  190. {
  191. if ($memberId) {
  192. $response = app(CallApiService::class)->callApi([
  193. 'url' => 'member-pick',
  194. 'data' => [
  195. 'Page' => [
  196. [ 'Id' => $memberId ]
  197. ]
  198. ]
  199. ]);
  200. $member = $response['Page'][0];
  201. if ($member['SsoBrand'] !== '' && $member['FirstName'] === '') {
  202. return '/my-page/member-edit';
  203. }
  204. } else {
  205. $memberPagemove = setupMemberPagemove();
  206. if (app(CallApiService::class)->verifyApiError($memberPagemove)) {
  207. return '/';
  208. }
  209. $afterLoginApplyPage = preg_replace('/\s+/', '', explode(',', $memberPagemove['AfterLoginApplyPage']));
  210. $slice = Str::after($previousUrl, url('/'));
  211. if (empty($previousUrl) || Str::contains($slice, $afterLoginApplyPage)) {
  212. return $memberPagemove['AfterLoginUri'];
  213. }
  214. }
  215. return $previousUrl;
  216. }
  217. function setupSsoClientOrLoginInfo($brandCode = 'member')
  218. {
  219. $setup = app(CallApiService::class)->callApi([
  220. 'url' => 'setup-page',
  221. 'data' => [
  222. 'PageVars' => [
  223. 'Query' => "((setup_code = 'sso-client' and brand_code like '{$brandCode}%') or setup_code = 'login-info') and is_on_use = '1'",
  224. 'Limit' => 100
  225. ]
  226. ],
  227. ]);
  228. // dd($setup);
  229. $oauth2InfoList = collect($setup['Page'])->filter(function ($setup) {
  230. return $setup['SetupCode'] !== 'login-info';
  231. })->flatMap(function ($setup) use ($brandCode) {
  232. $key = Str::replace($brandCode . '-', '', $setup['BrandCode']);
  233. return [$key => json_decode($setup['SetupJson'], true)];
  234. })->toArray();
  235. $develLoginInfo = collect($setup['Page'])->filter(function ($setup) {
  236. return $setup['SetupCode'] === 'login-info';
  237. })->map(function ($setup) {
  238. return json_decode($setup['SetupJson'], true);
  239. })->first();
  240. // dd($develLoginInfo);
  241. return [$oauth2InfoList, $develLoginInfo];
  242. }
  243. function breadcrumb($igroupCode, $sort = 'primary'): array
  244. {
  245. $mainMenuPerm = \App\Helpers\Utils::getProMainMenu();
  246. $mainMenuPermPage = collect($mainMenuPerm['Page'])->filter(function ($menu) use ($sort) {
  247. return $menu['Sort'] === $sort;
  248. })->toArray();
  249. $mainMenuList = \App\Helpers\Utils::formatMenuList($mainMenuPermPage, 'MenuCode');
  250. $menuCodeSplit = str_split($igroupCode, 2);
  251. $firstMenu = collect($mainMenuList)->filter(function ($q) use ($menuCodeSplit) {
  252. return Str::startsWith($q['MenuCode'], $menuCodeSplit[0]);
  253. })->first();
  254. if (empty($firstMenu['child']) || ! isset($menuCodeSplit[1])) { return [ $firstMenu ]; }
  255. $secondMenu = collect($firstMenu['child'])->filter(function ($q) use ($menuCodeSplit) {
  256. return Str::startsWith($q['MenuCode'], $menuCodeSplit[0] . $menuCodeSplit[1]);
  257. })->first();
  258. if (empty($secondMenu['child']) || ! isset($menuCodeSplit[2])) { return [ $firstMenu, $secondMenu ]; }
  259. $thirdMenu = collect($secondMenu['child'])->filter(function ($q) use ($menuCodeSplit) {
  260. return Str::startsWith($q['MenuCode'], $menuCodeSplit[0] . $menuCodeSplit[1] . $menuCodeSplit[2]);
  261. })->first();
  262. return [ $firstMenu, $secondMenu, $thirdMenu ];
  263. }
  264. function recaptchaSiteverify($secret, $response)
  265. {
  266. $data = [
  267. 'secret' => $secret,
  268. 'response' => $response,
  269. 'remoteip' => request()->ip()
  270. ];
  271. $response = Request::get('https://www.google.com/recaptcha/api/siteverify',
  272. [ 'Accept' => 'application/json' ],
  273. $data
  274. );
  275. return json_decode(json_encode($response->body ?? []), true)['success'];
  276. }