466 lines
11 KiB
PHP
466 lines
11 KiB
PHP
<?php
|
|
|
|
namespace phpcommon;
|
|
|
|
function timestamp_to_datetime($timestamp)
|
|
{
|
|
// return strftime('%Y-%m-%d %H:%M:%S', $timestamp + 8 * 3600);
|
|
return strftime('%Y-%m-%d %H:%M:%S', $timestamp);
|
|
}
|
|
|
|
function timestamp_to_date($timestamp)
|
|
{
|
|
return strftime('%Y-%m-%d', $timestamp);
|
|
}
|
|
|
|
function timestamp_to_date2($timestamp)
|
|
{
|
|
return strftime('%Y%m%d', $timestamp);
|
|
}
|
|
|
|
function between_days($strtime, $endtime){
|
|
$day = $endtime/3600/24 - $strtime/3600/24;
|
|
return ceil($day); //时间戳之间相差的天数
|
|
}
|
|
|
|
function date_time($date){
|
|
return $date + 8 * 3600;
|
|
}
|
|
|
|
function getdayseconds($time)
|
|
{
|
|
return intval(($time + 8 * 3600)/3600/24) * 3600 * 24 - 3600 * 8;
|
|
}
|
|
|
|
function getNextDaySeconds($time)
|
|
{
|
|
return intval(($time + 8 * 3600)/3600/24) * 3600 * 24 - 3600 * 8 + 3600 * 24;
|
|
}
|
|
|
|
function getMondaySeconds($time)
|
|
{
|
|
$week = date('w', $time);
|
|
if ($week == 0) {
|
|
$day = 6;
|
|
} else {
|
|
$day = $week - 1;
|
|
}
|
|
return (intval(($time + 8 * 3600)/3600/24) - $day)* 3600 * 24 - 3600 * 8;
|
|
}
|
|
|
|
function getThisMonthFirstDaySeconds($time)
|
|
{
|
|
$day = date('j', $time) - 1;
|
|
return (intval(($time + 8 * 3600)/3600/24) - $day) * 3600 * 24 - 3600 * 8;
|
|
}
|
|
|
|
function getNextMonthFirstDaySeconds($time)
|
|
{
|
|
$day = date('j', $time);
|
|
if ($day < 20) {
|
|
$time = $time + 3600 * 24 * 31;
|
|
} else {
|
|
$time = $time + 3600 * 24 * 20;
|
|
}
|
|
return getThisMonthFirstDaySeconds($time);
|
|
}
|
|
|
|
function getThisYearFirstDaySeconds($time)
|
|
{
|
|
$day = date('z', $time) - 1;
|
|
return (intval(($time + 8 * 3600)/3600/24) - $day) * 3600 * 24 - 3600 * 8;
|
|
}
|
|
|
|
function sendOk($succ_msg = '')
|
|
{
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg' => $succ_msg
|
|
));
|
|
}
|
|
|
|
function sendError($errcode, $errmsg)
|
|
{
|
|
echo json_encode(array(
|
|
'errcode' => $errcode,
|
|
'errmsg' => $errmsg
|
|
));
|
|
}
|
|
|
|
function wxBizDataDecryptData($appid, $sessionKey, $encryptedData, $iv, &$data )
|
|
{
|
|
if (strlen($sessionKey) != 24) {
|
|
return false;
|
|
}
|
|
$aesKey=base64_decode($sessionKey);
|
|
|
|
|
|
if (strlen($iv) != 24) {
|
|
return false;
|
|
}
|
|
$aesIV=base64_decode($iv);
|
|
|
|
$aesCipher=base64_decode($encryptedData);
|
|
|
|
$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
|
|
|
|
$dataObj=json_decode( $result );
|
|
if( $dataObj == NULL )
|
|
{
|
|
return false;
|
|
}
|
|
if( $dataObj->watermark->appid != $appid )
|
|
{
|
|
return false;
|
|
}
|
|
$data = $result;
|
|
return true;
|
|
}
|
|
|
|
function extractChannel($accountid)
|
|
{
|
|
$str_list = explode('_', $accountid);
|
|
if (count($str_list) < 3) {
|
|
return 0;
|
|
}
|
|
$channel = $str_list[0];
|
|
$gameid = $str_list[1];
|
|
$openid = $str_list[2];
|
|
return $channel;
|
|
}
|
|
|
|
function extractGameId($accountid)
|
|
{
|
|
$str_list = explode('_', $accountid);
|
|
if (count($str_list) < 3) {
|
|
return 0;
|
|
}
|
|
$channel = $str_list[0];
|
|
$gameid = $str_list[1];
|
|
$openid = $str_list[2];
|
|
return $gameid;
|
|
}
|
|
|
|
function extractOpenId($accountid)
|
|
{
|
|
$str_list = explode('_', $accountid);
|
|
if (count($str_list) < 3) {
|
|
return 0;
|
|
}
|
|
$channel = $str_list[0];
|
|
$gameid = $str_list[1];
|
|
$openid = substr($accountid,
|
|
strlen($channel . '_' . $gameid . '_')
|
|
);
|
|
return $openid;
|
|
}
|
|
|
|
function createAccountId($channel, $gameid, $unionid)
|
|
{
|
|
return $channel . '_' . $gameid . '_' . $unionid;
|
|
}
|
|
|
|
function getIPv4()
|
|
{
|
|
//不允许就使用getenv获取
|
|
if(getenv("HTTP_X_FORWARDED_FOR")){
|
|
$realip = getenv( "HTTP_X_FORWARDED_FOR");
|
|
}elseif(getenv("HTTP_CLIENT_IP")) {
|
|
$realip = getenv("HTTP_CLIENT_IP");
|
|
}else{
|
|
$realip = getenv("REMOTE_ADDR");
|
|
}
|
|
|
|
return $realip;
|
|
}
|
|
|
|
function getIPv6()
|
|
{
|
|
//不允许就使用getenv获取
|
|
if(getenv("HTTP_X_FORWARDED_FOR")){
|
|
$realip = getenv( "HTTP_X_FORWARDED_FOR");
|
|
}elseif(getenv("HTTP_CLIENT_IP")) {
|
|
$realip = getenv("HTTP_CLIENT_IP");
|
|
}else{
|
|
$realip = getenv("REMOTE_ADDR");
|
|
}
|
|
|
|
return $realip;
|
|
}
|
|
|
|
function getProvinceZoneId($country, $province)
|
|
{
|
|
$north_zone = array(
|
|
'黑龙江' => 1,
|
|
'吉林' => 1,
|
|
'辽宁' => 1,
|
|
'内蒙古' => 1,
|
|
'北京' => 1,
|
|
'天津' => 1,
|
|
'河北' => 1,
|
|
'河南' => 1,
|
|
'山东' => 1,
|
|
'新疆' => 1,
|
|
'西藏' => 1,
|
|
'甘肃' => 1,
|
|
'青海' => 1,
|
|
'宁夏' => 1,
|
|
'陕西' => 1,
|
|
'山西' => 1
|
|
);
|
|
$south_zone = array(
|
|
'海南' => 1,
|
|
'广东' => 1,
|
|
'台湾' => 1,
|
|
'香港' => 1,
|
|
'澳门' => 1,
|
|
'云南' => 1,
|
|
'广西' => 1,
|
|
'贵州' => 1,
|
|
'江西' => 1,
|
|
'福建' => 1,
|
|
'江苏' => 1,
|
|
'安徽' => 1,
|
|
'湖南' => 1,
|
|
'湖北' => 1,
|
|
'四川' => 1,
|
|
'重庆' => 1,
|
|
'上海' => 1,
|
|
'浙江' => 1
|
|
);
|
|
if (array_key_exists($province, $north_zone)) {
|
|
return 1;
|
|
} else if (array_key_exists($province, $south_zone)) {
|
|
return 2;
|
|
} else {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
function md5Sign($params, $secret, $timestamp, $connstr = '&', $secret_constr = ':'){
|
|
ksort($params);
|
|
$params_str = '';
|
|
foreach($params as $key => $val){
|
|
$params_str = $params_str . $key . '=' . $val . $connstr;
|
|
}
|
|
if($params_str != '' && $connstr != ''){
|
|
$params_str = substr($params_str, 0, -1);
|
|
|
|
}
|
|
return md5($params_str . $secret_constr . $timestamp . $secret);
|
|
}
|
|
|
|
function sdkMd5Sign($params, $secret, $connstr = '&', $secret_constr = ':'){
|
|
ksort($params);
|
|
$params_str = '';
|
|
foreach($params as $key => $val){
|
|
$params_str = $params_str . $key . '=' . $val . $connstr;
|
|
}
|
|
if($params_str != '' && $connstr != ''){
|
|
$params_str = substr($params_str, 0, -1);
|
|
|
|
}
|
|
return md5($params_str . $secret_constr . $secret);
|
|
}
|
|
|
|
function checkParams($params, $fields)
|
|
{
|
|
foreach ($fields as $value) {
|
|
if (!array_key_exists($value, $params)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function checkValidParams($params, $fields)
|
|
{
|
|
foreach ($fields as $value) {
|
|
if (!array_key_exists($value, $params) || empty($value)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function checkRegisterTimeInSessionId($accountid, $session_id)
|
|
{
|
|
$str_list = explode('_', $session_id);
|
|
if (count($str_list) < 4) {
|
|
return false;
|
|
}
|
|
$session_cratetime = $str_list[0];
|
|
$account_registertime = $str_list[1];
|
|
$md51 = $str_list[2];
|
|
$md52 = $str_list[3];
|
|
return md5($accountid .
|
|
'f3a6a9a5-217a-4079-ab99-b5d69b8212be' .
|
|
$account_registertime .
|
|
$session_cratetime) == $md51;
|
|
}
|
|
|
|
function extractRegisterTimeFromSessionId($session_id)
|
|
{
|
|
$str_list = explode('_', $session_id);
|
|
if (count($str_list) < 4) {
|
|
return 0;
|
|
}
|
|
$session_cratetime = $str_list[0];
|
|
$account_registertime = $str_list[1];
|
|
$md51 = $str_list[2];
|
|
$md52 = $str_list[3];
|
|
return $account_registertime;
|
|
}
|
|
|
|
function isValidSessionId($account_id, $session_id)
|
|
{
|
|
$str_list = explode('_', $session_id);
|
|
if (count($str_list) < 4) {
|
|
return false;
|
|
}
|
|
if (SERVER_ENV == _ONLINE) {
|
|
$channel = extractChannel($account_id);
|
|
if ($channel == 6000) {
|
|
return false;
|
|
}
|
|
}
|
|
$session_cratetime = $str_list[0];
|
|
$account_registertime = $str_list[1];
|
|
$md51 = $str_list[2];
|
|
$md52 = $str_list[3];
|
|
return md5($account_id .
|
|
'f3a6a9a5-217a-4079-ab99-b5d69b8212be' .
|
|
$account_registertime .
|
|
$session_cratetime) == $md51;
|
|
}
|
|
|
|
function jsonEncodeAsObject($value)
|
|
{
|
|
return json_encode((object)$value);
|
|
}
|
|
|
|
function loginParamsCheck()
|
|
{
|
|
if (!checkValidParams($_REQUEST,
|
|
array(
|
|
'account_id',
|
|
'session_id'
|
|
))
|
|
) {
|
|
echo json_encode(array(
|
|
'errcode' => 100,
|
|
'errmsg' => '参数错误',
|
|
));
|
|
die();
|
|
return false;
|
|
}
|
|
if (!isValidSessionId($_REQUEST['account_id'], $_REQUEST['session_id'])) {
|
|
echo json_encode(array(
|
|
'errcode' => 100,
|
|
'errmsg' => '参数错误',
|
|
));
|
|
die();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function cutMBStr(&$str, $len)
|
|
{
|
|
if (strlen($str) <= $len || $len < 2) {
|
|
$tmpstr = $str;
|
|
$str = '';
|
|
return $tmpstr;
|
|
}
|
|
$tmpstr = substr($str, 0, $len);
|
|
{
|
|
$old_mb_len = mb_strlen($tmpstr, 'utf-8');
|
|
while (mb_strlen($tmpstr, 'utf-8') == $old_mb_len) {
|
|
$tmpstr = substr($str, 0, --$len);
|
|
}
|
|
}
|
|
$tmpstr = substr($str, 0, $len);
|
|
$str = substr($str, $len);
|
|
return $tmpstr;
|
|
}
|
|
|
|
function isMobile($mobile){
|
|
if (!is_numeric($mobile)) {
|
|
return false;
|
|
}
|
|
$re_mobile = '#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#';
|
|
return preg_match($re_mobile, $mobile) ? true : false;
|
|
}
|
|
|
|
function safeDiv($a, $b)
|
|
{
|
|
return !empty($b) ? $a / $b : 0;
|
|
}
|
|
|
|
function genNonceStr()
|
|
{
|
|
return md5(posix_getpid() . uniqid() . getIPv4());
|
|
}
|
|
|
|
function objectToXml($params, $root_node = '<xml/>')
|
|
{
|
|
$xml = new \SimpleXMLElement($root_node);
|
|
foreach ($params as $key => $value) {
|
|
if (is_array($value)) {
|
|
$child_node = $xml->AddChild($key);
|
|
foreach ($value as $key1 => $value1) {
|
|
$child_node->addChild($key1, $value1);
|
|
}
|
|
} else {
|
|
$xml->addChild($key, $value);
|
|
}
|
|
}
|
|
return $xml->asXML();
|
|
}
|
|
|
|
function wxPaySign($params, $secret)
|
|
{
|
|
ksort($params);
|
|
$params_str = '';
|
|
foreach($params as $key => $val){
|
|
if (!empty($val)) {
|
|
$params_str = $params_str . $key . '=' . $val . '&';
|
|
}
|
|
}
|
|
if($params_str != ''){
|
|
$params_str = substr($params_str, 0, -1);
|
|
|
|
}
|
|
$params_str = $params_str . '&key=' . $secret;
|
|
$sign = md5($params_str);
|
|
return $sign;
|
|
}
|
|
|
|
function readPubKey($pub_key)
|
|
{
|
|
$pem = "-----BEGIN PUBLIC KEY-----\n" .
|
|
chunk_split($pub_key, 64, "\n") .
|
|
"-----END PUBLIC KEY-----\n";
|
|
return openssl_pkey_get_public($pem);
|
|
}
|
|
|
|
function readPriKey($pri_key)
|
|
{
|
|
$pem = "-----BEGIN RSA PRIVATE KEY-----\n" .
|
|
chunk_split($pri_key, 64, "\n") .
|
|
"-----END RSA PRIVATE KEY-----\n";
|
|
return openssl_pkey_get_private($pem);
|
|
}
|
|
|
|
function aesEncrypt($str_content, $iv, $key)
|
|
{
|
|
$str_encrypted = openssl_encrypt($str_content, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv);
|
|
return $str_encrypted;
|
|
}
|
|
|
|
function aesDecrypt($str_encrypted, $iv, $key)
|
|
{
|
|
$str_decrypted = openssl_decrypt($str_encrypted, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv);
|
|
return $str_decrypted;
|
|
}
|