www.0g.com/source/myfun.php
2021-08-30 15:33:51 +08:00

672 lines
22 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 获取图片路径
* @param int $cover_id 图片路径ID
* @param string $field 图片路径类型
* @return boolean
*/
function get_cover($cover_id,$field=NULL){
if(empty($cover_id)){
$picture['path']='http://www.0g.com/templates/static/images/mobile_new/logo_ico.png';
return $picture;
}
$query_sql='SELECT path from sys_picture WHERE status=1 AND id='.$cover_id;
$picture=getonerecord2($query_sql);
if($field == 'path'){
$is_exit=stripos($picture[$field], 'http://img.0g.com');
if(!empty($picture['url'])){
$picture['url'] = str_replace('http://www.0g.com','' , $picture['url']);
if($is_exit==false){
$picture['url']='http://img.0g.com'.$picture['url'];
}
}else{
$picture['path'] = str_replace('http://www.0g.com','' , $picture['path']);
if($is_exit!==0){
$old_url=stripos($picture['path'], 'http://www.0g.com/');
if($old_url==0){
$picture['path'] = str_replace('http://www.0g.com/','http://img.0g.com/' , $picture['path']);
}else{
$picture['path']=str_replace('./', '', $picture['path']);
}
$picture['path']='http://img.0g.com/'.$picture['path'];
}
// else{
// $picture['path']=$picture['path'];
// }
}
}
return empty($field) ? $picture : $picture[$field];
}
/**
* 获取数据库的配置
*/
function getConfig(){
//$dblink= getdblink();
$query_str="SELECT * FROM sys_config WHERE status=1";
$result_resouce=exec_db2($query_str);
while($row= mysqli_fetch_array($result_resouce, MYSQLI_ASSOC)){
$data[]=$row;
}
//$data= getrecord_array($query_str, $dblink);
$config = array();
if($data && is_array($data)){
foreach ($data as $value) {
$config[$value['name']] = parse($value['type'], $value['value']);
}
}
return $config;
}
/**
* 根据配置类型解析配置
* @param integer $type 配置类型
* @param string $value 配置值
*/
function parse($type, $value){
switch ($type) {
case 3: //解析数组
$array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
if(strpos($value,':')){
$value = array();
foreach ($array as $val) {
list($k, $v) = explode(':', $val);
$value[$k] = $v;
}
}else{
$value = $array;
}
break;
}
return $value;
}
/**
* 获取友情链接
*/
function get_links(){
$dblink= getdblink();
$query_str="SELECT *FROM tab_links WHERE status=1";
$data= getrecord_array($query_str, $dblink);
if(empty($data)){return false;}
return $data;
}
/**
* 数据签名认证
* @param array $data 被认证的数据
* @return string 签名
*
*/
function data_auth_sign($data) {
//数据类型检测
if(!is_array($data)){
$data = (array)$data;
}
ksort($data); //排序
$code = http_build_query($data); //url编码并生成query字符串
$sign = sha1($code); //生成签名
return $sign;
}
/**
* Ajax方式返回数据到客户端
* @access protected
* @param mixed $data 要返回的数据
* @param String $type AJAX返回数据格式
* @return void
*/
function ajaxReturn($data,$type='') {
if(empty($type)) $type = 'JSON';
switch (strtoupper($type)){
case 'JSON' :
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data));
case 'XML' :
// 返回xml格式数据
header('Content-Type:text/xml; charset=utf-8');
exit(xml_encode($data));
case 'JSONP':
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
$handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
exit($handler.'('.json_encode($data).');');
case 'EVAL' :
// 返回可执行的js脚本
header('Content-Type:text/html; charset=utf-8');
exit($data);
}
}
/**
* 获取网页内容
* @param resource $fsock 打开的网页资源的resource
* @return boolean
*/
function GetHttpContent($fsock=null) {
$out = null;
while($buff = @fgets($fsock, 2048)){
$out .= $buff;
}
fclose($fsock);
$pos = strpos($out, "\r\n\r\n");
$head = substr($out, 0, $pos); //http head
$status = substr($head, 0, strpos($head, "\r\n")); //http status line
$body = substr($out, $pos + 4, strlen($out) - ($pos + 4));//page body
if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches)){
if(intval($matches[1]) / 100 == 2){
return $body;
}else{
return false;
}
}else{
return false;
}
}
/**
* 系统非常规MD5加密方法
* @param string $str 要加密的字符串
* @return string
*/
function think_ucenter_md5($str, $key = 'ThinkUCenter'){
return '' === $str ? '' : md5(sha1($str) . $key);
}
/**
* URL重定向
* @param string $url 重定向的URL地址
* @param integer $time 重定向的等待时间(秒)
* @param string $msg 重定向前的提示信息
* @return void
*/
function redirect($url, $time=0, $msg='') {
//多行URL地址支持
$url = str_replace(array("\n", "\r"), '', $url);
if (empty($msg))
$msg = "系统将在{$time}秒之后自动跳转到{$url}";
if (!headers_sent()) {
// redirect
if (0 === $time) {
header('Location: ' . $url);
} else {
header("refresh:{$time};url={$url}");
echo($msg);
}
exit();
} else {
$str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
if ($time != 0)
$str .= $msg;
exit($str);
}
}
/**
*
* @param int $num 错误类型
* @return string 返回错误提示语
*/
function getE($num="") {
switch($num) {
case -1: $error = '用户名长度必须在6-30个字符以内'; break;
case -2: $error = '用户名被禁止注册!'; break;
case -3: $error = '用户名被占用!'; break;
case -4: $error = '密码长度不合法'; break;
case -5: $error = '邮箱格式不正确!'; break;
case -6: $error = '邮箱长度必须在1-32个字符之间'; break;
case -7: $error = '邮箱被禁止注册!'; break;
case -8: $error = '邮箱被占用!'; break;
case -9: $error = '手机格式不正确!'; break;
case -10: $error = '手机被禁止注册!'; break;
case -11: $error = '手机号被占用!'; break;
case -12: $error = '手机号码必须由11位数字组成';break;
case -20: $error = '请填写正确的姓名';break;
case -21: $error = '用户名必须由字母、数字或下划线组成,以字母开头';break;
case -22: $error = '用户名必须由6~30位数字、字母或下划线组成';break;
case -31: $error = '密码错误';break;
case -32: $error = '用户不存在或被禁用';break;
case -41: $error = '身份证无效';break;
default: $error = '未知错误';
}
return $error;
}
/**
* 验证验证码是否正确
* @access public
* @param string $code 用户验证码
* @param string $id 验证码标识
* @return bool 用户验证码是否正确
*/
function check($code, $id = '') {
$key = authcode(SEKEY,'ENCODE',SEKEY).$id;
// 验证码不能为空
$secode = $_SESSION[$key];
if(empty($code) || empty($secode)) {
return false;
}
// session 过期
if(NOW_TIME - $secode['verify_time'] > CODE_EXPIRE) {
$_SESSION[$key]= null;
return false;
}
if(authcode(strtoupper($code),'ENCODE',SEKEY) == $secode['verify_code']) {
RESET && $_SESSION[$key]= null;
return true;
}
return false;
}
/**
* 验证手机验证码
* @param int $code 要验证的验证码
* @param string $key memcache存储的key
* @return boolean
*/
function checkPhoneCode(int $code,string $key){
$memObj= get_memcache(0);
$cachCode=memcache_get($memObj,$key);
if($cachCode['code']==$code){
memcache_delete($memObj,0);
memcache_close($memObj);
return 1;
}else{
memcache_close($memObj);
return 0;
}
}
/**
*根据不同字段返回游戏名称
*@param string $field 字段
*@param string $value 搜索条件
*@return string 游戏名字false 未找到
*@author 王贺
*/
function get_game_name($value='',$field='id'){
$dblink= getdblink();
$query_str="SELECT *FROM tab_game WHERE `{$field}`='{$value}' LIMIT 1";
$name= getonerecord($query_str);
if(empty($name['game_name'])){return false;}
return $name['game_name'];
}
/**
* 写日记
* @param type $msg
* @param type $datas
*/
function dolog($msg='',$datas='',$prefix=''){
$ip= getip();
$time = date('H:i:s',time());
if($prefix){
$filename = LOG_DIR.$prefix.'_'.date('Y-m-d',time()).'.log';
}else{
$filename = LOG_DIR.date('Y-m-d',time()).'.log';
}
if(is_array($datas)){
file_put_contents($filename, "[{$time}]\n"."{$ip}\n"."{$msg}\n".var_export($datas,true)."\n\n", FILE_APPEND);
}else{
file_put_contents($filename, "[$time]\n"."{$ip}\n"."{$msg}\n".$datas."\n\n", FILE_APPEND);
}
}
/**
* 指定目录打日记
* @param type $dir
* @param type $msg
* @param type $datas
*/
function dolog_dir($dir,$msg='',$datas=''){
$time = date('H:i:s',time());
createDir($dir);
$filename = $dir.date('Y-m-d',time()).'.log';
$fp= fopen($filename, 'a+');
if(is_array($datas)){
fwrite($fp, "[{$time}]\n"."{$msg}\n".$datas."\n\n");
}else{
fwrite($fp, "[$time]\n"."{$msg}\n".$datas."\n\n");
}
}
/**
* alipay 下单 RSA签名
*/
function alipay_sign($data){
//读取私钥文件
$priKey = file_get_contents(ALIPAY_PRIVATE_KEY);//私钥文件路径
//转换为openssl密钥必须是没有经过pkcs8转换的私钥
$res = openssl_get_privatekey($priKey);
//$res = openssl_pkey_get_private($priKey);
//调用openssl内置签名方法生成签名$sign
openssl_sign($data, $sign, $res);
//释放资源
openssl_free_key($res);
//base64编码
$sign = base64_encode($sign);
return $sign;
}
/**
* 支付订单随机数生成
* @param type $len
* @return string
*/
function sp_random_string($len = 6) {
$chars = array(
"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", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars);// 将数组打乱
$output = "";
for ($i = 0; $i < $len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
/**
* 获取数据的可分的页码
* @param type $total
*/
function getCountPage($total,$pageSize){
$totalPage= ceil($total/$pageSize);
return $totalPage;
}
//获取汉字首字母
function getfirstchar($str){
$firstchar_ord=ord(strtoupper($str{0}));
if (($firstchar_ord>=65 and $firstchar_ord<=91)or($firstchar_ord>=48 and $firstchar_ord<=57))
return $s0{0};
$s=iconv("UTF-8","gb2312", $str);
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319 and $asc<=-20284)return "A";
if($asc>=-20283 and $asc<=-19776)return "B";
if($asc>=-19775 and $asc<=-19219)return "C";
if($asc>=-19218 and $asc<=-18711)return "D";
if($asc>=-18710 and $asc<=-18527)return "E";
if($asc>=-18526 and $asc<=-18240)return "F";
if($asc>=-18239 and $asc<=-17923)return "G";
if($asc>=-17922 and $asc<=-17418)return "H";
if($asc>=-17417 and $asc<=-16475)return "J";
if($asc>=-16474 and $asc<=-16213)return "K";
if($asc>=-16212 and $asc<=-15641)return "L";
if($asc>=-15640 and $asc<=-15166)return "M";
if($asc>=-15165 and $asc<=-14923)return "N";
if($asc>=-14922 and $asc<=-14915)return "O";
if($asc>=-14914 and $asc<=-14631)return "P";
if($asc>=-14630 and $asc<=-14150)return "Q";
if($asc>=-14149 and $asc<=-14091)return "R";
if($asc>=-14090 and $asc<=-13319)return "S";
if($asc>=-13318 and $asc<=-12839)return "T";
if($asc>=-12838 and $asc<=-12557)return "W";
if($asc>=-12556 and $asc<=-11848)return "X";
if($asc>=-11847 and $asc<=-11056)return "Y";
if($asc>=-11055 and $asc<=-10247)return "Z";
return null;
}
/**
*
* @param type $param
* @return int
*/
function trimall($str)//删除空格
{
$spaceFlag=array(" "," ","\t","\n","\r");
$replaceWord=array("","","","","");
return str_replace($spaceFlag,$replaceWord,$str);
}
/**
* 获取轮播广告
* @param int $type client类型 1:pc,2:H5
* @return array $adv
*/
function getAdv($type=1){
$where=' WHERE status=1 AND pos_id=1 AND target=3';
if($type==1){
$field="title,pc_pic AS data,url,game_name";
}elseif($type==2){
$field="title,if(H5_pic,H5_pic,pc_pic) AS data,url,game_name";
}
$query_str="SELECT {$field} FROM tab_adv {$where} ORDER BY sort ASC";
$adv_resource=exec_db2($query_str);
while($adv_row= mysqli_fetch_array($adv_resource,MYSQLI_ASSOC)){
$adv[]=$adv_row;
}
return $adv;
}
function curl_get($url,$timeout=15,array $options = array())
{
$defaults = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_NOBODY=>false,
//CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0',
//CURLOPT_FOLLOWLOCATION=>1,
CURLOPT_TIMEOUT => $timeout
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
$result = curl_exec($ch);
//dolog('身份验证_exe'. curl_error($ch),$result);
curl_close($ch);
return $result;
}
/**
* 按日期分割重组数据
* @param array $data 需要分组的数据
* @param array $$date 分组的日期
* @return boolean
*/
function sliceDate($data,$date){
$list=array();
if(is_array($date)){
foreach ($date as $key =>$val){
$dateStart= strtotime($val['groud_date'].'00:00:00');
$dateEnd= strtotime($val['groud_date'].'23:59:59');
if(is_array($data) && !empty($data)){
foreach($data as $k =>$v){
if($dateStart<=$v['onlinetime'] && $v['onlinetime']<=$dateEnd){
$list[$val['groud_date']][]=$v;
}
}
}
}
return $list;
}else{
return false;
}
}
/**
* 下载函数
* @param type $file
* @param type $isLarge
* @param type $rename
* @return boolean
*/
function down($file, $isLarge = false, $rename = NULL){
if(headers_sent())return false;
if(!$file) {
cpmessage('文件不存在哦 亲!',INDEXFILE.'.php?tp=gamelist');
die('Error 404:The file not found!');
}
$file=getCDNURL($file);
if($rename==NULL){
if(strpos($file, '/')===false && strpos($file, '\\')===false)
$filename = $file;
else{
$filename = basename($file);
}
}else{
$filename = $rename;
}
$filesize= get_fileSize($file);
header('Content-Description: File Transfer');
header("Content-Type: application/force-download;");
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename={$filename}");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: '.$filesize);// size
header("Pragma: no-cache");
ob_clean();
$fp = fopen($file, "r");
while(!feof($fp)) {
echo fgets($fp, 4096);
}
fclose($fp);
}
/**
* 获取远程文件大小单位byte
* @param type $url
* @return string
*/
function get_fileSize($url){
if(!isset($url)||trim($url)==''){
return '';
}
ob_start();
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_NOBODY,1);
$okay=curl_exec($ch);
curl_close($ch);
$head=ob_get_contents();
ob_end_clean();
$regex='/Content-Length:\s([0-9].+?)\s/';
$count=preg_match($regex,$head,$matches);
return isset($matches[1])&&is_numeric($matches[1])?$matches[1]:'';
}
/**
*
* @param type $game_id
* @return type
*/
function add_down_stat($game_id){
$nowTime=time();
$insert_field="(`promote_id`,`game_id`,`number`,`type`,`create_time`)";
$insert_val="(0,{$game_id},1,0,{$nowTime})";
$query_str="INSERT INTO tab_down_stat {$insert_field} VALUES {$insert_val}";
$result= exec_db2($query_str);
return $result;
}
/**
* 获得下载地址
* @param type $down_url
* @return type
*/
function get_down_url($down_url){
$is_newDown=stripos($down_url,'./');
if($is_newDown===0){
$down_url=str_replace('./', '', $down_url);
$down_url='http://pkg.02wan.com/'.$down_url;
}
return $down_url;
}
/**
* 游戏下载处理
* @param type $game_id
* @param type $down_url
*/
function do_down($game_id,$down_url,$is_own=1){
$query_sql="UPDATE tab_game SET dow_num=dow_num+1 WHERE `id`={$game_id}";
$addDow=exec_db($query_sql);
add_down_stat($game_id);
if($is_own==1){
down($down_url);
}else{
Header("HTTP/1.1 303 See Other");
Header("Location: ".$down_url);
}
}
/**
* 合作伙伴logo
* @return type
*/
function partnerImg(){
$query_str="SELECT img_more FROM `tab_cooperative`";
$imgData= getonerecord2($query_str);
$imgArr=explode(',',$imgData['img_more']);
foreach ($imgArr as $k=>$v){
$partnerImg[]= get_cover($v,'path');
}
return $partnerImg;
}
/* 将一个字符串转变成键值对数组
* @param : string str 要处理的字符串 $str ='callback({"client_id":"101365493","openid":"5B79C69AE774557DA5C0EFE196A33FD5"});';
* @param : string sp 键值对分隔符
* @param : string kv 键值分隔符
* @return : array*/
function getCallbackArr2($str,$sp="({",$kv="})",$arr)
{
$str = trimall($str);
$arr2 = str_replace(array($sp,$kv),$arr,$str);
eval("\$arr"." = $arr2");
return $arr;
}
function callback($str){
return json_decode($str,true);
}
function isMobile($phone)
{
$regex='/^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/';
$matches = array();
if(preg_match($regex,$phone,$matches))
{
return true;
}
return false;
}
/**
* 失败返回false
*/
function curl_post_bbs($url, $timeout=10,array $post = array(), array $options = array())
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_POSTFIELDS => http_build_query($post)
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
# 根据用户主键ID生成抢玩通行ID
function get_sysid($uid){
$first = substr($uid, 0, 1);
$left = substr($uid, 1);
$new = strrev($left);
return 'qw' . str_pad($first.$new, 9, '0', STR_PAD_LEFT);
}
?>