common.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. * 定义常量
  13. */
  14. $server_name = $_SERVER['SERVER_NAME'];
  15. $server_name_html = "www" . substr($server_name, strpos($server_name, '.'));
  16. define("URL", $server_name_html);
  17. //国际化
  18. define("LANG", ['zh-cn', 'en-us']);
  19. /**
  20. * 获取request
  21. */
  22. function requestInfo() {
  23. $requestInfo = \think\Request::instance();
  24. return $requestInfo;
  25. }
  26. /**
  27. * 获取header
  28. */
  29. function headerInfo() {
  30. $requestInfo = requestInfo ();
  31. $headerInfo = $requestInfo->header();
  32. return $headerInfo;
  33. }
  34. /**
  35. * 随机盐值
  36. */
  37. function GenEncryption() {
  38. // 生成随机数.
  39. srand((double) microtime() * 1000000);
  40. $ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
  41. $list = explode(",", $ychar);
  42. $authnum = "";
  43. for ($i = 0; $i < 6; $i++) {
  44. $randnum = rand(0, 61); // 10+26;
  45. $authnum .= $list[$randnum];
  46. }
  47. return $authnum;
  48. }
  49. /**
  50. * 密码加密
  51. */
  52. function GenPassword($password) {
  53. $Enc = GenEncryption();
  54. $Pwd = md5(md5($Enc . $password));
  55. return array("encryption" => $Enc, "password" => $Pwd);
  56. }
  57. /**
  58. * 生成token
  59. *
  60. * @return string
  61. */
  62. function getToken() {
  63. $enc = GenEncryption();
  64. $time = time();
  65. $token = substr(uniqid($enc . $time), 0, 35);
  66. return $token;
  67. }
  68. /**
  69. * UUID 生成
  70. */
  71. function UUID() {
  72. $prefix = '';
  73. $str = md5(uniqid(mt_rand(), true));
  74. $uuid = substr($str, 0, 8) . '-';
  75. $uuid .= substr($str, 8, 4) . '-';
  76. $uuid .= substr($str, 12, 4) . '-';
  77. $uuid .= substr($str, 16, 4) . '-';
  78. $uuid .= substr($str, 20, 12);
  79. return $prefix . $uuid;
  80. }
  81. /**
  82. * 订单 生成
  83. */
  84. function OrderID($prefix = '') {
  85. $num = mt_rand(100, 999);
  86. list($s, $m) = explode(' ', microtime());
  87. $order = date("YmdHis") . ($s * 1000000) . $num;
  88. return $prefix . $order;
  89. }
  90. /**
  91. * 获取客户端真实IP
  92. */
  93. function GETIP() {
  94. global $ip;
  95. if (getenv("HTTP_CLIENT_IP")) {
  96. $ip = getenv("HTTP_CLIENT_IP");
  97. } else if (getenv("HTTP_X_FORWARDED_FOR")) {
  98. $ip = getenv("HTTP_X_FORWARDED_FOR");
  99. } else if (getenv("REMOTE_ADDR")) {
  100. $ip = getenv("REMOTE_ADDR");
  101. } else {
  102. $ip = "Unknow";
  103. }
  104. return $ip;
  105. }
  106. /**
  107. * 数据加密
  108. */
  109. function lock_url($txt, $key)
  110. {
  111. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  112. $nh = rand(0, 64);
  113. $ch = $chars[$nh];
  114. $mdKey = md5($key.$ch);
  115. $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  116. $txt = base64_encode($txt);
  117. $tmp = '';
  118. $i = 0;
  119. $j = 0;
  120. $k = 0;
  121. for ($i=0; $i<strlen($txt); $i++) {
  122. $k = $k == strlen($mdKey) ? 0 : $k;
  123. $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
  124. $tmp .= $chars[$j];
  125. }
  126. return urlencode($ch.$tmp);
  127. }
  128. /**
  129. * 数据解密
  130. */
  131. function unlock_url($txt, $key)
  132. {
  133. $txt = urldecode($txt);
  134. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  135. $ch = $txt[0];
  136. $nh = strpos($chars,$ch);
  137. $mdKey = md5($key.$ch);
  138. $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  139. $txt = substr($txt,1);
  140. $tmp = '';
  141. $i = 0;
  142. $j = 0;
  143. $k = 0;
  144. for ($i=0; $i<strlen($txt); $i++) {
  145. $k = $k == strlen($mdKey) ? 0 : $k;
  146. $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
  147. while ($j<0) $j+=64;
  148. $tmp .= $chars[$j];
  149. }
  150. return base64_decode($tmp);
  151. }
  152. /**
  153. * 分页
  154. */
  155. function getPage($count, $pageSize, $currentPage)
  156. {
  157. // 总页数.
  158. $allPage = ceil($count/$pageSize);
  159. $page = [];
  160. if ($allPage > 5) {
  161. // 判断开始页码.
  162. if ($currentPage <= 3) {
  163. $n = 1;
  164. } else if ($currentPage >= ($allPage - 2)) {
  165. $n = ($allPage - 4);
  166. } else {
  167. $n = ($currentPage - 2);
  168. }
  169. for ($i = 0; $i <= 4; $i++) {
  170. $page[$i] = $n;
  171. $n++;
  172. }
  173. } else {
  174. for ($i = 0; $i <= ($allPage - 1); $i++) {
  175. $page[$i] = $i + 1;
  176. }
  177. }
  178. return $page;
  179. }
  180. /**
  181. * CODE生成
  182. */
  183. function getCode() {
  184. $format = 8;
  185. $dics = array(
  186. 0=>'0', 1=>'1', 2=>'2', 3=>'3', 4=>'4', 5=>'5', 6=>'6', 7=>'7', 8=>'8',
  187. 9=>'9', 10=>'A', 11=>'B', 12=>'C', 13=>'D', 14=>'E', 15=>'F', 16=>'G', 17=>'H',
  188. 18=>'I',19=>'J', 20=>'K', 21=>'L', 22=>'M', 23=>'N', 24=>'O', 25=>'P', 26=>'Q',
  189. 27=>'R',28=>'S', 29=>'T', 30=>'U', 31=>'V', 32=>'W', 33=>'X', 34=>'Y', 35=>'Z'
  190. );
  191. list($msec, $sec) = explode(' ', microtime());
  192. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  193. $int = substr($msectime,0,13);
  194. $dnum = 36; //进制数
  195. $arr = array ();
  196. $loop = true;
  197. while ($loop) {
  198. $arr[] = $dics[bcmod($int, $dnum)];
  199. $int = bcdiv($int, $dnum, 0);
  200. if ($int == '0') {
  201. $loop = false;
  202. }
  203. }
  204. if (count($arr) < $format)
  205. $arr = array_pad($arr, $format, $dics[0]);
  206. return implode('', array_reverse($arr));
  207. }
  208. /**
  209. * 生成随机密码
  210. */
  211. function randomPassword($len = 32, $keyword = '') {
  212. if (strlen($keyword) > $len) {//关键字不能比总长度长
  213. return false;
  214. }
  215. $str = '';
  216. $chars = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHIJKMNPQRSTUVWXYZ'; //去掉1跟字母l防混淆
  217. if ($len > strlen($chars)) {//位数过长重复字符串一定次数
  218. $chars = str_repeat($chars, ceil($len / strlen($chars)));
  219. }
  220. $chars = str_shuffle($chars); //打乱字符串
  221. $str = substr($chars, 0, $len);
  222. if (!empty($keyword)) {
  223. $start = $len - strlen($keyword);
  224. $str = substr_replace($str, $keyword, mt_rand(0, $start), strlen($keyword)); //从随机位置插入关键字
  225. }
  226. return $str;
  227. }