Helpers.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. if (!function_exists('micro_time')) {
  3. /**
  4. * 获取当前时间的微秒时间戳
  5. * @return string
  6. */
  7. function micro_time()
  8. {
  9. list($usec, $sec) = explode(" ", microtime());
  10. $time = ($sec . substr($usec, 2, 3));
  11. return $time;
  12. }
  13. }
  14. #是否微信浏览器
  15. function isWeChatBrowser()
  16. {
  17. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
  18. return true;
  19. } else {
  20. return false;
  21. }
  22. }
  23. #获取随机字符串
  24. function getChars($lenth = 16)
  25. {
  26. $string = 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789';
  27. $key = '';
  28. for ($i = 0; $i < $lenth; $i++) {
  29. $key .= substr($string, mt_rand(0, 51), 1);
  30. }
  31. return $key;
  32. }
  33. function imageGetBase64($url)
  34. {
  35. $imgs = [
  36. 'image/jpeg' => 'jpeg',
  37. 'image/jpg' => 'jpg',
  38. 'image/gif' => 'gif',
  39. 'image/png' => 'png',
  40. 'text/html' => 'html',
  41. 'text/plain' => 'txt',
  42. 'image/pjpeg' => 'jpg',
  43. 'image/x-png' => 'png',
  44. 'image/x-icon' => 'ico'
  45. ];
  46. if (!stristr($url, 'http')) {
  47. throw new \Exception("url不正确", 2101);
  48. }
  49. $dir = pathinfo($url);
  50. $host = $dir['dirname'];
  51. $refer = $host . '/';
  52. $ch = curl_init($url);
  53. curl_setopt($ch, CURLOPT_REFERER, $refer); //伪造来源地址
  54. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回变量内容还是直接输出字符串,0输出,1返回内容
  55. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);//在启用CURLOPT_RETURNTRANSFER的时候,返回原生的(Raw)输出
  56. curl_setopt($ch, CURLOPT_HEADER, 0); //是否输出HEADER头信息 0否1是
  57. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); //超时时间
  58. $data = curl_exec($ch);
  59. //$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
  60. //$httpContentType = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
  61. $info = curl_getinfo($ch);
  62. curl_close($ch);
  63. $httpCode = intval($info['http_code']);
  64. $httpContentType = $info['content_type'];
  65. $httpSizeDownload = intval($info['size_download']);
  66. if ($httpCode != '200') {
  67. throw new \Exception("url返回内容不正确", 2102);
  68. }
  69. if (!isset($imgs[$httpContentType])) {
  70. throw new \Exception("url资源类型未知", 2103);
  71. }
  72. if ($httpSizeDownload < 1) {
  73. throw new \Exception("内容大小不正确", 2104);
  74. }
  75. $base_64 = base64_encode($data);
  76. $data = "data:{$httpContentType};base64,{$base_64}";
  77. unset($info, $base_64);
  78. return $data;
  79. }
  80. function encryptString(string $string, string $key)
  81. {
  82. $key = md5($key);
  83. $x = 0;
  84. $len = strlen($string);
  85. $l = strlen($key);
  86. $char = null;
  87. $str = null;
  88. for ($i = 0; $i < $len; $i++) {
  89. if ($x == $l) {
  90. $x = 0;
  91. }
  92. $char .= $key{$x};
  93. $x++;
  94. }
  95. for ($i = 0; $i < $len; $i++) {
  96. $str .= chr(ord($string{$i}) + (ord($char{$i})) % 256);
  97. }
  98. return base64_encode($str);
  99. }
  100. // #解密
  101. function decryptString(string $string, string $key)
  102. {
  103. $key = md5($key);
  104. $x = 0;
  105. $string = base64_decode($string);
  106. $len = strlen($string);
  107. $l = strlen($key);
  108. $char = null;
  109. $str = null;
  110. for ($i = 0; $i < $len; $i++) {
  111. if ($x == $l) {
  112. $x = 0;
  113. }
  114. $char .= substr($key, $x, 1);
  115. $x++;
  116. }
  117. for ($i = 0; $i < $len; $i++) {
  118. if (ord(substr($string, $i, 1)) < ord(substr($char, $i, 1))) {
  119. $str .= chr((ord(substr($string, $i, 1)) + 256) - ord(substr($char, $i, 1)));
  120. } else {
  121. $str .= chr(ord(substr($string, $i, 1)) - ord(substr($char, $i, 1)));
  122. }
  123. }
  124. return $str;
  125. }
  126. /**
  127. * @param array $array
  128. * @return xml
  129. */
  130. function array2xml(array $array)
  131. {
  132. $xml = "<xml>";
  133. foreach ($array as $key => $val) {
  134. if (is_numeric($val)) {
  135. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  136. } else {
  137. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  138. }
  139. }
  140. $xml .= "</xml>";
  141. return $xml;
  142. }
  143. /**
  144. * 将xml转为array
  145. * @param string $xml
  146. * @return array
  147. */
  148. function xml2array($xml)
  149. {
  150. //将XML转为array
  151. //禁止引用外部xml实体
  152. libxml_disable_entity_loader(true);
  153. $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  154. return $values;
  155. }
  156. function https_request($url, $data = null)
  157. {
  158. $curl = curl_init();
  159. curl_setopt($curl, CURLOPT_URL, $url);
  160. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  161. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  162. if (!empty($data)) {
  163. curl_setopt($curl, CURLOPT_POST, 1);
  164. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  165. }
  166. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  167. $output = curl_exec($curl);
  168. curl_close($curl);
  169. return $output;
  170. }
  171. function getUTF8Sting(string $string)
  172. {
  173. //reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ?
  174. $string = preg_replace(
  175. '/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]' .
  176. '|[\x00-\x7F][\x80-\xBF]+' .
  177. '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*' .
  178. '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})' .
  179. '|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
  180. '?',
  181. $string
  182. );
  183. //reject overly long 3 byte sequences and UTF-16 surrogates and replace with ?
  184. $string = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]' .
  185. '|\xED[\xA0-\xBF][\x80-\xBF]/S', '?', $string);
  186. return $string;
  187. }
  188. function uuid()
  189. {
  190. return \Ramsey\Uuid\Uuid::uuid4()->toString();
  191. return md5(uniqid(mt_rand(100000000, 999999999)));
  192. }
  193. /**
  194. * 版本检测
  195. * @param string $client_version 客户端版本
  196. * @param string $low_version 最低兼容版本
  197. * @param string $latest_version 最新版本
  198. * @throws \App\Exceptions\VersionException
  199. */
  200. function version(string $client_version, string $low_version, string $latest_version)
  201. {
  202. if (strcmp($client_version, $low_version) < 0) {
  203. // 不兼容
  204. throw new \App\Exceptions\VersionException("不兼容", 208);
  205. }
  206. if (strcmp($client_version, $latest_version) < 0) {
  207. // 有更新
  208. }
  209. }
  210. /**
  211. * 调试打印
  212. * @param \Illuminate\Http\Request $request
  213. * @param $var
  214. * @return array
  215. */
  216. function debugdump(\Illuminate\Http\Request $request, $var)
  217. {
  218. if (98047 == $request->get('debug')) {
  219. foreach (array_slice(func_get_args(), 1) as $v) {
  220. dump($v);
  221. }
  222. if (2 < func_num_args()) {
  223. return func_get_args();
  224. }
  225. return $var;
  226. }
  227. }