common.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. // 전체 배열의 키를 대문자, 소문자로 변경해서 반환한다.
  3. function arrayChangeKeyCaseRecursive($arr = [], $case = CASE_LOWER): string
  4. {
  5. return array_map(function ($item) use ($case) {
  6. if (is_array($item)){
  7. $item = arrayChangeKeyCaseRecursive($item, $case);
  8. }
  9. return $item;
  10. }, array_change_key_case($arr, $case));
  11. }
  12. // Request Url
  13. function currentURL(): string
  14. {
  15. $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http");
  16. $host = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost');
  17. $query = (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '');
  18. return sprintf("%s://%s%s", $protocol, $host, $query);
  19. }
  20. // 데이터 단위 변환
  21. function byteFormat($num, $precision = 1): string
  22. {
  23. if ($num >= 1000000000000) {
  24. $num = round($num / 1099511627776, $precision);
  25. $unit = 'TB';
  26. } elseif ($num >= 1000000000) {
  27. $num = round($num / 1073741824, $precision);
  28. $unit = 'GB';
  29. } elseif ($num >= 1000000) {
  30. $num = round($num / 1048576, $precision);
  31. $unit = 'MB';
  32. } elseif ($num >= 1000) {
  33. $num = round($num / 1024, $precision);
  34. $unit = 'KB';
  35. } else {
  36. $unit = 'Bytes';
  37. return number_format($num) . ' ' . $unit;
  38. }
  39. return number_format($num, $precision) . ' ' . $unit;
  40. }
  41. // 데이터 단위 지정 변환
  42. function byteToFormat($bytes = 0, $unit = "", $decimals = 0): string
  43. {
  44. $units = ['B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4, 'PB' => 5, 'EB' => 6, 'ZB' => 7, 'YB' => 8];
  45. $value = 0;
  46. if ($bytes > 0) {
  47. if (!array_key_exists($unit, $units)) {
  48. $pow = floor(log($bytes) / log(1024));
  49. $unit = array_search($pow, $units);
  50. }
  51. $value = ($bytes / pow(1024, floor($units[$unit])));
  52. }
  53. if (!is_numeric($decimals) || $decimals < 0) {
  54. $decimals = 2;
  55. }
  56. return sprintf('%.' . $decimals . 'f ' . $unit, $value);
  57. }
  58. // 상태 보기
  59. function show($varName): string
  60. {
  61. switch ($result = get_cfg_var($varName)) {
  62. case 0:
  63. return '<span class="text-danger">Off</span>';
  64. case 1:
  65. return '<span class="text-success">On</span>';
  66. default:
  67. return $result;
  68. }
  69. }
  70. // 리눅스 시스템 보기 - 파일권한
  71. function isFun($funName = ""): string
  72. {
  73. if (!$funName || trim($funName) == '' || preg_match('~[^a-z0-9\_]+~i', $funName, $tmp)) {
  74. return '알수없음';
  75. } else {
  76. return (false !== function_exists($funName)) ? '<span class="text-success">사용가능</span>' : '<span class="text-danger">Г—</span>';
  77. }
  78. }
  79. // 리눅스 시스템 보기 - CPU 모델
  80. function GetCoreInformation(): array
  81. {
  82. $data = @file('/proc/stat');
  83. $cores = [];
  84. if($data) {
  85. foreach ($data as $line) {
  86. if (preg_match('/^cpu[0-9]/', $line)) {
  87. $info = explode(' ', $line);
  88. $cores[] = ['user' => $info[1], 'nice' => $info[2], 'sys' => $info[3], 'idle' => $info[4], 'iowait' => $info[5], 'irq' => $info[6], 'softirq' => $info[7]];
  89. }
  90. }
  91. }
  92. return $cores;
  93. }
  94. // 리눅스 시스템 보기 CPU 속도
  95. function GetCpuPercentages($stat1 = [], $stat2 = []): array
  96. {
  97. $cpus = [];
  98. if (count($stat1) !== count($stat2)) {
  99. return $cpus;
  100. }
  101. for ($i = 0, $l = count($stat1); $i < $l; $i++) {
  102. $dif = [];
  103. $dif['user'] = ($stat2[$i]['user'] - $stat1[$i]['user']);
  104. $dif['nice'] = ($stat2[$i]['nice'] - $stat1[$i]['nice']);
  105. $dif['sys'] = ($stat2[$i]['sys'] - $stat1[$i]['sys']);
  106. $dif['idle'] = ($stat2[$i]['idle'] - $stat1[$i]['idle']);
  107. $dif['iowait'] = ($stat2[$i]['iowait'] - $stat1[$i]['iowait']);
  108. $dif['irq'] = ($stat2[$i]['irq'] - $stat1[$i]['irq']);
  109. $dif['softirq'] = ($stat2[$i]['softirq'] - $stat1[$i]['softirq']);
  110. $total = array_sum($dif);
  111. $cpu = [];
  112. foreach ($dif as $x => $y) {
  113. $cpu[$x] = round($y / $total * 100, 2);
  114. }
  115. $cpus['cpu' . $i] = $cpu;
  116. }
  117. return $cpus;
  118. }
  119. // 현재 URL 이 대상 URL 과 같다면 active 상태
  120. function activeUrl($segment = ''): string
  121. {
  122. return (request()->is($segment) ? 'active' : '');
  123. }
  124. // 관리자 설정값 조회
  125. function configs($item = '', $default = ''): string|int|null
  126. {
  127. $config = cache('config-meta');
  128. if($item) {
  129. $ret = (property_exists($config, $item) ? $config->{$item} : $default);
  130. }else{
  131. $ret = $config;
  132. }
  133. return $ret;
  134. }
  135. // 게시판 에디터 출력
  136. function htmlEditor($name = '', $content = '', $className = '', $isDhtmlEditor = true, $emoticonYN = true, $placeholder = "", $rows = 0, $id = ''): string
  137. {
  138. // TINYMCE 에디터 추가
  139. $html = "";
  140. if ($isDhtmlEditor && !defined('LOAD_DHTML_EDITOR_JS'))
  141. {
  142. // tinymce iframe 허용
  143. $whiteIframe = configs('white_iframe');
  144. $whiteIframe = preg_replace("/[\r|\n|\r\n]+/", ",", $whiteIframe);
  145. $whiteIframe = preg_replace("/\s+/", "", $whiteIframe);
  146. $html .= PHP_EOL . '<script type="module">';
  147. // 아이프레임 주소 허용여부 결정
  148. $html .= sprintf('var whiteIframe = "%s";', $whiteIframe);
  149. // 이모티콘 사용여부
  150. $html .= sprintf('var useEmoticon = "%s";', ($emoticonYN ? 'Y' : 'N'));
  151. $html .= '</script>';
  152. define('LOAD_DHTML_EDITOR_JS', true);
  153. }
  154. $html .= '<textarea';
  155. $html .= sprintf(' class="tinymce-editor %s"', $className);
  156. if($name) {
  157. $html .= sprintf(' name="%s"', $name);
  158. }
  159. if($id) {
  160. $html .= sprintf(' id="%s"', $id);
  161. }
  162. if($placeholder) {
  163. $html .= sprintf(' placeholder="%s"', $placeholder);
  164. }
  165. if ($rows) {
  166. $html .= sprintf(' rows="%s"', $rows);
  167. }
  168. $html .= ('>' . htmlspecialchars($content) . '</textarea>');
  169. return $html;
  170. }
  171. // 관리자 Form Submit 정보 attributes 와 맵핑
  172. function mapAdminAttr(array $rules, array $posts): array
  173. {
  174. foreach(array_keys($rules) as $field) {
  175. if(array_key_exists($field, $posts)) {
  176. continue;
  177. }
  178. $posts[$field] = 0;
  179. }
  180. return $posts;
  181. }
  182. // FreeBSD 값 조회
  183. function getKey($keyName): bool
  184. {
  185. return doCommand('sysctl', "-n $keyName");
  186. }
  187. // FreeBSD 시스템 명령 실행
  188. function doCommand($commandName, $args): bool
  189. {
  190. $buffer = "";
  191. if (false === ($command = findCommand($commandName))) {
  192. return false;
  193. }
  194. if ($fp = @popen("$command $args", 'r')) {
  195. while (!@feof($fp)) {
  196. $buffer .= @fgets($fp, 4096);
  197. }
  198. return trim($buffer);
  199. }
  200. return false;
  201. }
  202. // FreeBSD 명령어 지정 조회
  203. function findCommand($commandName)
  204. {
  205. $path = ['/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin'];
  206. foreach ($path as $p) {
  207. if (@is_executable("$p/$commandName")) {
  208. return "$p/$commandName";
  209. }
  210. }
  211. return false;
  212. }
  213. // 날짜와 시간에 <br/>, \n 추가
  214. function dateBr($date = null, $default = ''): string
  215. {
  216. return ($date ? date('Y-m-d <\b\r /> H:i:s', strtotime($date)) : $default);
  217. }
  218. // Alert 띄우기
  219. function alert(string $msg, string|null $url = "", bool $redirect = true): Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse|Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
  220. {
  221. $msg = preg_replace('/(\r\n|\r|\n)/', '\n', addslashes($msg)); // 줄바꿈 되도록 출력
  222. $msg = strip_tags(trim($msg), '<br/>'); // 태그 제거, 공백 제거
  223. $msg = preg_replace('/<br[^>]*>/i', '\n\n', $msg); // br 태그를 \n로 개행 alert 에서 다음줄로 하기 위함.
  224. if ($redirect) {
  225. if ($url) {
  226. return redirect($url)->withErrors($msg)->withInput();
  227. } else {
  228. return back()->withErrors($msg)->withInput();
  229. }
  230. }else{
  231. return response(
  232. sprintf('<script>alert("%s");</script>', $msg)
  233. , 200, ['Content-Type', 'text/javascript']);
  234. }
  235. }
  236. // 로그인 확인
  237. function loginCheck(string $callback = null): Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
  238. {
  239. $s = "<script>";
  240. $s .= "if (confirm(\"로그인 후 이용하실 수 있습니다.\\n로그인 화면으로 이동하시겠습니까?\")) {";
  241. $s .= "location.href = \"" . route('login') . "?callback=" . urlencode(url($_SERVER['REQUEST_URI']) ?? $callback) . "\";";
  242. $s .= "}else{";
  243. $s .= "history.go(-1);";
  244. $s .= "}";
  245. $s .= "</script>";
  246. return response($s, 200, ['Content-Type', 'text/javascript']);
  247. }
  248. // Alert 후 창 닫음
  249. function alertClose(string $msg): Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
  250. {
  251. $s = "<script>";
  252. $s .= "alert(\"$msg\");";
  253. $s .= "self.close();";
  254. $s .= "window.close();";
  255. $s .= "</script>";
  256. return response($s, 200, ['Content-Type', 'text/javascript']);
  257. }
  258. // Send socket
  259. function getSock(string $url): string
  260. {
  261. // host 와 uri 를 분리
  262. $host = "";
  263. $get = "";
  264. if (preg_match("/http:\/\/([a-zA-Z0-9_\-\.]+)([^<]*)/", $url, $res)) {
  265. $host = $res[1];
  266. $get = $res[2];
  267. }
  268. // 80번 포트로 소캣접속 시도
  269. $fp = fsockopen($host, 80, $n, $s, 30);
  270. if (empty($fp)) {
  271. die($s . ' (' . $n . ")\n");
  272. } else {
  273. fputs($fp, "GET $get HTTP/1.0\r\n");
  274. fputs($fp, "Host: $host\r\n");
  275. fputs($fp, "\r\n");
  276. $header = '';
  277. // header 와 content 를 분리한다.
  278. while (trim($buffer = fgets($fp, 1024)) !== '') {
  279. $header .= $buffer;
  280. }
  281. while (!feof($fp)) {
  282. $buffer .= fgets($fp, 1024);
  283. }
  284. }
  285. fclose($fp);
  286. // content 만 return 한다.
  287. return $buffer;
  288. }
  289. // 휴대폰 번호 조회
  290. function getTelNumber($phone, $hyphen = 1): string
  291. {
  292. if ($hyphen) {
  293. $preg = "$1-$2-$3";
  294. } else {
  295. $preg = "$1$2$3";
  296. }
  297. $phone = str_replace('-', '', trim($phone));
  298. return preg_replace(
  299. "/^(01[016789])([0-9]{3,4})([0-9]{4})$/",
  300. $preg,
  301. $phone
  302. );
  303. }
  304. // 수행 측정시간
  305. function getTime(): float
  306. {
  307. $t = explode(' ', microtime());
  308. return (float)$t[0] + (float)$t[1];
  309. }
  310. /**
  311. * iframe tag 조회
  312. */
  313. function getIframeTag($string = '', $onlySrc = false)
  314. {
  315. preg_match('/<iframe.*src=\"(.*)\".*><\/iframe>/isU', $string, $matches);
  316. if ($onlySrc) {
  317. return (isset($matches[1])) ? $matches[1] : ""; // the src part. (http://www.youtube.com/embed/IIYeKGNNNf4?rel=0)
  318. } else {
  319. return (isset($matches[0])) ? $matches[0] : ""; // only the <iframe ...></iframe> part
  320. }
  321. }
  322. /*
  323. * 이미지 인지 여부
  324. */
  325. function isImage($path = ''): int
  326. {
  327. return intval(in_array(getimagesize($path)[2], [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP]));
  328. }
  329. /*
  330. * 문자 검색 후 강조
  331. */
  332. function highlight($search, $str): string
  333. {
  334. return (!is_null($search)) ? preg_replace( '#' . preg_quote($search,'#') . '#iu', '<span style="background: #ff9632;color: #000;">$0</span>', $str) : $str;
  335. }
  336. /*
  337. * 모든 공백 제거
  338. */
  339. function aTrim($str): string
  340. {
  341. return preg_replace('/\s+/', '', $str);
  342. }
  343. /*
  344. * 목록 시작 번호
  345. * currentItems: same as limit
  346. * currentPage: floor(start / limit)
  347. * totalPages: ceil(totalItems / limit)
  348. * last: totalPages * limit
  349. * previous: (currentPage-1) * limit
  350. * next: (currentPage+1) * limit
  351. */
  352. function listNum(int $total, int $page, int $perPage): int
  353. {
  354. return (($listNum = ($total - ($page - 1) * $perPage)) > 0 ? $listNum : $total);
  355. }
  356. /*
  357. * 단말기 구분으로 레이아웃 처리
  358. */
  359. function layout(string $viewPath): string
  360. {
  361. return (DEVICE_TYPE != DEVICE_TYPE_1 ? ('mobile.' . $viewPath) : 'desktop.' . $viewPath);
  362. }