common.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. if (!function_exists('attr_format')) {
  13. /**
  14. * 格式化属性
  15. * @param $arr
  16. * @return array
  17. */
  18. function attr_format($arr)
  19. {
  20. $data = [];
  21. $res = [];
  22. $count = count($arr);
  23. if ($count > 1) {
  24. for ($i = 0; $i < $count - 1; $i++) {
  25. if ($i == 0) $data = $arr[$i]['detail'];
  26. //替代变量1
  27. $rep1 = [];
  28. foreach ($data as $v) {
  29. $v = trim($v);
  30. foreach ($arr[$i + 1]['detail'] as $g) {
  31. $g = trim($g);
  32. //替代变量2
  33. $rep2 = ($i != 0 ? '' : $arr[$i]['value'] . '_$_') . $v . '-$-' . $arr[$i + 1]['value'] . '_$_' . $g;
  34. $tmp[] = $rep2;
  35. if ($i == $count - 2) {
  36. foreach (explode('-$-', $rep2) as $k => $h) {
  37. //替代变量3
  38. $rep3 = explode('_$_', $h);
  39. //替代变量4
  40. $rep4['detail'][$rep3[0]] = isset($rep3[1]) ? $rep3[1] : '';
  41. }
  42. if($count == count($rep4['detail']))
  43. $res[] = $rep4;
  44. }
  45. }
  46. }
  47. $data = isset($tmp) ? $tmp : [];
  48. }
  49. } else {
  50. $dataArr = [];
  51. foreach ($arr as $k => $v) {
  52. foreach ($v['detail'] as $kk => $vv) {
  53. $vv = trim($vv);
  54. $dataArr[$kk] = $v['value'] . '_' . $vv;
  55. $res[$kk]['detail'][$v['value']] = $vv;
  56. }
  57. }
  58. $data[] = implode('-', $dataArr);
  59. }
  60. return [$data, $res];
  61. }
  62. }
  63. if (!function_exists('get_month')) {
  64. /**
  65. * 格式化月份
  66. * @param string $time
  67. * @param int $ceil
  68. * @return array
  69. */
  70. function get_month($time = '', $ceil = 0)
  71. {
  72. if (empty($time)) {
  73. $firstday = date("Y-m-01", time());
  74. $lastday = date("Y-m-d", strtotime("$firstday +1 month -1 day"));
  75. } else if ($time == 'n') {
  76. if ($ceil != 0)
  77. $season = ceil(date('n') / 3) - $ceil;
  78. else
  79. $season = ceil(date('n') / 3);
  80. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  81. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  82. } else if ($time == 'y') {
  83. $firstday = date('Y-01-01');
  84. $lastday = date('Y-12-31');
  85. } else if ($time == 'h') {
  86. $firstday = date('Y-m-d', strtotime('this week +' . $ceil . ' day')) . ' 00:00:00';
  87. $lastday = date('Y-m-d', strtotime('this week +' . ($ceil + 1) . ' day')) . ' 23:59:59';
  88. }
  89. return array($firstday, $lastday);
  90. }
  91. }
  92. if (!function_exists('clearfile')) {
  93. /**删除目录下所有文件
  94. * @param $path 目录或者文件路径
  95. * @param string $ext
  96. * @return bool
  97. */
  98. function clearfile($path, $ext = '*.log')
  99. {
  100. $files = (array)glob($path . DS . '*');
  101. foreach ($files as $path) {
  102. if (is_dir($path)) {
  103. $matches = glob($path . '/' . $ext);
  104. if (is_array($matches)) {
  105. array_map('unlink', $matches);
  106. }
  107. rmdir($path);
  108. } else {
  109. unlink($path);
  110. }
  111. }
  112. return true;
  113. }
  114. }
  115. if (!function_exists('get_this_class_methods')) {
  116. /**获取当前类方法
  117. * @param $class
  118. * @return array
  119. */
  120. function get_this_class_methods($class, $unarray = [])
  121. {
  122. $arrayall = get_class_methods($class);
  123. if ($parent_class = get_parent_class($class)) {
  124. $arrayparent = get_class_methods($parent_class);
  125. $arraynow = array_diff($arrayall, $arrayparent);//去除父级的
  126. } else {
  127. $arraynow = $arrayall;
  128. }
  129. return array_diff($arraynow, $unarray);//去除无用的
  130. }
  131. }
  132. if (!function_exists('verify_domain')) {
  133. /**
  134. * 验证域名是否合法
  135. * @param string $domain
  136. * @return bool
  137. */
  138. function verify_domain(string $domain): bool
  139. {
  140. $res = "/^(?=^.{3,255}$)(http(s)?:\/\/)(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/";
  141. if (preg_match($res, $domain))
  142. return true;
  143. else
  144. return false;
  145. }
  146. }
  147. if (!function_exists('getFileHeaders')) {
  148. function getFileHeaders(string $url, $isData = true) {
  149. stream_context_set_default(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]]);
  150. $header['size'] = 0;
  151. $header['type'] = 'image/jpeg';
  152. if (!$isData) {
  153. return $header;
  154. }
  155. try {
  156. $headerArray = get_headers(str_replace('\\', '/', $url), true);
  157. if (!isset($headerArray['Content-Length'])) {
  158. $header['size'] = 0;
  159. }
  160. if (!isset($headerArray['Content-Type'])) {
  161. $header['type'] = 'image/jpeg';
  162. }
  163. if (is_array($headerArray['Content-Length']) && count($headerArray['Content-Length']) == 2) {
  164. $header['size'] = $headerArray['Content-Length'][1];
  165. }
  166. if (is_array($headerArray['Content-Type']) && count($headerArray['Content-Type']) == 2) {
  167. $header['type'] = $headerArray['Content-Type'][1];
  168. }
  169. } catch (\Exception $e) {
  170. }
  171. return $header;
  172. }
  173. }
  174. if (!function_exists('getUrlImgInfo')) {
  175. function getUrlImgInfo($url)
  176. {
  177. $headers = getFileHeaders($url);
  178. return [
  179. 'name' => basename($url), // 所以仅支持 .jpg/.jpeg 等结尾的 url
  180. 'size' => $headers['size'] ?? 0,
  181. 'type' => $headers['type'] ?? 'image/jpeg',
  182. 'dir' => $url,
  183. 'thumb_path' => $url,
  184. 'time' => time(),
  185. ];
  186. }
  187. }