| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 流年 <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- /**
- * 定义常量
- */
- $server_name = $_SERVER['SERVER_NAME'];
- $server_name_html = "www" . substr($server_name, strpos($server_name, '.'));
- define("URL", $server_name_html);
- //国际化
- define("LANG", ['zh-cn', 'en-us']);
- /**
- * 获取request
- */
- function requestInfo() {
- $requestInfo = \think\Request::instance();
- return $requestInfo;
- }
- /**
- * 获取header
- */
- function headerInfo() {
- $requestInfo = requestInfo ();
- $headerInfo = $requestInfo->header();
- return $headerInfo;
- }
- /**
- * 随机盐值
- */
- function GenEncryption() {
- // 生成随机数.
- srand((double) microtime() * 1000000);
- $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";
- $list = explode(",", $ychar);
- $authnum = "";
- for ($i = 0; $i < 6; $i++) {
- $randnum = rand(0, 61); // 10+26;
- $authnum .= $list[$randnum];
- }
- return $authnum;
- }
- /**
- * 密码加密
- */
- function GenPassword($password) {
- $Enc = GenEncryption();
- $Pwd = md5(md5($Enc . $password));
- return array("encryption" => $Enc, "password" => $Pwd);
- }
- /**
- * 生成token
- *
- * @return string
- */
- function getToken() {
- $enc = GenEncryption();
- $time = time();
- $token = substr(uniqid($enc . $time), 0, 35);
- return $token;
- }
- /**
- * UUID 生成
- */
- function UUID() {
- $prefix = '';
- $str = md5(uniqid(mt_rand(), true));
- $uuid = substr($str, 0, 8) . '-';
- $uuid .= substr($str, 8, 4) . '-';
- $uuid .= substr($str, 12, 4) . '-';
- $uuid .= substr($str, 16, 4) . '-';
- $uuid .= substr($str, 20, 12);
- return $prefix . $uuid;
- }
- /**
- * 订单 生成
- */
- function OrderID($prefix = '') {
- $num = mt_rand(100, 999);
- list($s, $m) = explode(' ', microtime());
- $order = date("YmdHis") . ($s * 1000000) . $num;
- return $prefix . $order;
- }
- /**
- * 获取客户端真实IP
- */
- function GETIP() {
- global $ip;
- if (getenv("HTTP_CLIENT_IP")) {
- $ip = getenv("HTTP_CLIENT_IP");
- } else if (getenv("HTTP_X_FORWARDED_FOR")) {
- $ip = getenv("HTTP_X_FORWARDED_FOR");
- } else if (getenv("REMOTE_ADDR")) {
- $ip = getenv("REMOTE_ADDR");
- } else {
- $ip = "Unknow";
- }
- return $ip;
- }
- /**
- * 数据加密
- */
- function lock_url($txt, $key)
- {
- $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
- $nh = rand(0, 64);
- $ch = $chars[$nh];
- $mdKey = md5($key.$ch);
- $mdKey = substr($mdKey,$nh%8, $nh%8+7);
- $txt = base64_encode($txt);
- $tmp = '';
- $i = 0;
- $j = 0;
- $k = 0;
- for ($i=0; $i<strlen($txt); $i++) {
- $k = $k == strlen($mdKey) ? 0 : $k;
- $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
- $tmp .= $chars[$j];
- }
- return urlencode($ch.$tmp);
- }
- /**
- * 数据解密
- */
- function unlock_url($txt, $key)
- {
- $txt = urldecode($txt);
- $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
- $ch = $txt[0];
- $nh = strpos($chars,$ch);
- $mdKey = md5($key.$ch);
- $mdKey = substr($mdKey,$nh%8, $nh%8+7);
- $txt = substr($txt,1);
- $tmp = '';
- $i = 0;
- $j = 0;
- $k = 0;
- for ($i=0; $i<strlen($txt); $i++) {
- $k = $k == strlen($mdKey) ? 0 : $k;
- $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
- while ($j<0) $j+=64;
- $tmp .= $chars[$j];
- }
- return base64_decode($tmp);
- }
- /**
- * 分页
- */
- function getPage($count, $pageSize, $currentPage)
- {
- // 总页数.
- $allPage = ceil($count/$pageSize);
- $page = [];
- if ($allPage > 5) {
- // 判断开始页码.
- if ($currentPage <= 3) {
- $n = 1;
- } else if ($currentPage >= ($allPage - 2)) {
- $n = ($allPage - 4);
- } else {
- $n = ($currentPage - 2);
- }
- for ($i = 0; $i <= 4; $i++) {
- $page[$i] = $n;
- $n++;
- }
- } else {
- for ($i = 0; $i <= ($allPage - 1); $i++) {
- $page[$i] = $i + 1;
- }
- }
- return $page;
- }
- /**
- * CODE生成
- */
- function getCode() {
- $format = 8;
- $dics = array(
- 0=>'0', 1=>'1', 2=>'2', 3=>'3', 4=>'4', 5=>'5', 6=>'6', 7=>'7', 8=>'8',
- 9=>'9', 10=>'A', 11=>'B', 12=>'C', 13=>'D', 14=>'E', 15=>'F', 16=>'G', 17=>'H',
- 18=>'I',19=>'J', 20=>'K', 21=>'L', 22=>'M', 23=>'N', 24=>'O', 25=>'P', 26=>'Q',
- 27=>'R',28=>'S', 29=>'T', 30=>'U', 31=>'V', 32=>'W', 33=>'X', 34=>'Y', 35=>'Z'
- );
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- $int = substr($msectime,0,13);
- $dnum = 36; //进制数
- $arr = array ();
- $loop = true;
- while ($loop) {
- $arr[] = $dics[bcmod($int, $dnum)];
- $int = bcdiv($int, $dnum, 0);
- if ($int == '0') {
- $loop = false;
- }
- }
- if (count($arr) < $format)
- $arr = array_pad($arr, $format, $dics[0]);
- return implode('', array_reverse($arr));
- }
- /**
- * 生成随机密码
- */
- function randomPassword($len = 32, $keyword = '') {
- if (strlen($keyword) > $len) {//关键字不能比总长度长
- return false;
- }
- $str = '';
- $chars = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHIJKMNPQRSTUVWXYZ'; //去掉1跟字母l防混淆
- if ($len > strlen($chars)) {//位数过长重复字符串一定次数
- $chars = str_repeat($chars, ceil($len / strlen($chars)));
- }
- $chars = str_shuffle($chars); //打乱字符串
- $str = substr($chars, 0, $len);
- if (!empty($keyword)) {
- $start = $len - strlen($keyword);
- $str = substr_replace($str, $keyword, mt_rand(0, $start), strlen($keyword)); //从随机位置插入关键字
- }
- return $str;
- }
|