添加后台代理代码
This commit is contained in:
20
codes/agent/game/api/framework/function/cache.func.php
Normal file
20
codes/agent/game/api/framework/function/cache.func.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
defined('IN_IA') or exit('Access Denied');
|
||||
|
||||
load()->func('cache.mysql');
|
||||
|
||||
// 加载系统指定的配置项(core_cache表中)
|
||||
function cache_load($key, $db,$unserialize = false) {
|
||||
global $_W;
|
||||
if (!empty($_W['cache'][$key])) {
|
||||
return $_W['cache'][$key];
|
||||
}
|
||||
// 读取指定的系统配置项(core_cache表中)
|
||||
$data = $_W['cache'][$key] = cache_read($key,$db);
|
||||
return $unserialize ? iunserializer($data) : $data;
|
||||
}
|
||||
|
||||
|
||||
function &cache_global($key) {
|
||||
|
||||
}
|
||||
64
codes/agent/game/api/framework/function/cache.mysql.func.php
Normal file
64
codes/agent/game/api/framework/function/cache.mysql.func.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
defined('IN_IA') or exit('Access Denied');
|
||||
use phprs\ezsql\Sql;
|
||||
|
||||
// 读取指定的系统缓存项 core_cache表中
|
||||
function cache_read($key,$db) {
|
||||
$val = Sql::select('syweb_core_cache.`value`')
|
||||
->from('syweb_core_cache')
|
||||
->where('syweb_core_cache.`key`=?', $key)
|
||||
->get($db ,null);
|
||||
if(count($val)>0){
|
||||
return iunserializer($val[0]["value"]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function cache_search($prefix,$db) {
|
||||
$rs = Sql::select('syweb_core_cache.*')
|
||||
->from('syweb_core_cache')
|
||||
->where('syweb_core_cache.`key` like ?', "{$prefix}%")
|
||||
->get($db ,null);
|
||||
|
||||
$result = array();
|
||||
foreach ((array)$rs as $v) {
|
||||
$result[$v['key']] = iunserializer($v['value']);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
// 存储系统缓存
|
||||
function cache_write($key, $data,$db,$pdo) {
|
||||
if (empty($key) || !isset($data)) {
|
||||
return false;
|
||||
}
|
||||
$record = array();
|
||||
$record['`key`'] = $key;
|
||||
$record['`value`'] = iserializer($data);
|
||||
|
||||
$id = Sql::replaceInto('syweb_core_cache')->values($record)->exec($pdo)->lastInsertId();
|
||||
return $id;
|
||||
}
|
||||
|
||||
|
||||
function cache_delete($key) {
|
||||
return Sql::deleteFrom('syweb_core_cache')->where('`key`=?',$key)->exec($pdo);
|
||||
}
|
||||
|
||||
|
||||
function cache_clean($prefix = '') {
|
||||
global $_W;
|
||||
if (empty($prefix)) {
|
||||
$sql = 'DELETE FROM ' . tablename('core_cache');
|
||||
$result = pdo_query($sql);
|
||||
if ($result) {
|
||||
unset($_W['cache']);
|
||||
}
|
||||
} else {
|
||||
$sql = 'DELETE FROM ' . tablename('core_cache') . ' WHERE `key` LIKE :key';
|
||||
$params = array();
|
||||
$params[':key'] = "{$prefix}:%";
|
||||
$result = pdo_query($sql, $params);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
622
codes/agent/game/api/framework/function/communication.func.php
Normal file
622
codes/agent/game/api/framework/function/communication.func.php
Normal file
@@ -0,0 +1,622 @@
|
||||
<?php
|
||||
defined('IN_IA') or exit('Access Denied');
|
||||
|
||||
// 请求某个网页,并返回网页返回的内容
|
||||
// 参数1:请求地址
|
||||
// 参数2:请求参数
|
||||
function ihttp_request($url, $post = '', $needUrlEncode = true, $extra = array(), $timeout = 60)
|
||||
{
|
||||
$urlset = parse_url($url);
|
||||
if (empty($urlset['path'])) {
|
||||
$urlset['path'] = '/';
|
||||
}
|
||||
// 解析后的参数
|
||||
if (!empty($urlset['query'])) {
|
||||
$urlset['query'] = "?{$urlset['query']}";
|
||||
}
|
||||
// 请求端口
|
||||
if (empty($urlset['port'])) {
|
||||
$urlset['port'] = $urlset['scheme'] == 'https' ? '443' : '80';
|
||||
}
|
||||
// 假如是https请求,则判断是否开启了openssl模块,没有开启则提示错误
|
||||
if (strexists($url, 'https://') && !extension_loaded('openssl')) {
|
||||
if (!extension_loaded("openssl")) {
|
||||
message('请开启您PHP环境的openssl');
|
||||
}
|
||||
}
|
||||
if (function_exists('curl_init') && function_exists('curl_exec')) {
|
||||
$ch = curl_init();
|
||||
// PHP8兼容性修复:CURLOPT_SAFE_UPLOAD在PHP8中已被移除,安全上传现在是默认行为
|
||||
if (version_compare(phpversion(), '5.6') >= 0 && version_compare(phpversion(), '8.0') < 0) {
|
||||
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
|
||||
}
|
||||
if (!empty($extra['ip'])) {
|
||||
$extra['Host'] = $urlset['host'];
|
||||
$urlset['host'] = $extra['ip'];
|
||||
unset($extra['ip']);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_URL, $urlset['scheme'] . '://' . $urlset['host'] . ($urlset['port'] == '80' ? '' : ':' . $urlset['port']) . $urlset['path'] . $urlset['query']);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
||||
curl_setopt($ch, CURLOPT_HEADER, 1);
|
||||
@curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
|
||||
if ($post) {
|
||||
if (is_array($post)) {
|
||||
$filepost = false;
|
||||
foreach ($post as $name => $value) {
|
||||
if ((is_string($value) && substr($value, 0, 1) == '@') || (class_exists('CURLFile') && $value instanceof CURLFile)) {
|
||||
$filepost = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$filepost) {
|
||||
$post = http_build_query($post);
|
||||
}
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
|
||||
}
|
||||
if (!empty($GLOBALS['_W']['config']['setting']['proxy'])) {
|
||||
$urls = parse_url($GLOBALS['_W']['config']['setting']['proxy']['host']);
|
||||
if (!empty($urls['host'])) {
|
||||
curl_setopt($ch, CURLOPT_PROXY, "{$urls['host']}:{$urls['port']}");
|
||||
$proxytype = 'CURLPROXY_' . strtoupper($urls['scheme']);
|
||||
if (!empty($urls['scheme']) && defined($proxytype)) {
|
||||
curl_setopt($ch, CURLOPT_PROXYTYPE, constant($proxytype));
|
||||
} else {
|
||||
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
||||
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
|
||||
}
|
||||
if (!empty($GLOBALS['_W']['config']['setting']['proxy']['auth'])) {
|
||||
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $GLOBALS['_W']['config']['setting']['proxy']['auth']);
|
||||
}
|
||||
}
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
|
||||
if (defined('CURL_SSLVERSION_TLSv1')) {
|
||||
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');
|
||||
if (!empty($extra) && is_array($extra)) {
|
||||
$headers = array();
|
||||
foreach ($extra as $opt => $value) {
|
||||
if (strexists($opt, 'CURLOPT_')) {
|
||||
curl_setopt($ch, constant($opt), $value);
|
||||
} elseif (is_numeric($opt)) {
|
||||
curl_setopt($ch, $opt, $value);
|
||||
} else {
|
||||
$headers[] = "{$opt}: {$value}";
|
||||
}
|
||||
}
|
||||
if (!empty($headers)) {
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
}
|
||||
}
|
||||
$data = curl_exec($ch);
|
||||
$status = curl_getinfo($ch);
|
||||
$errno = curl_errno($ch);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
if ($errno || empty($data)) {
|
||||
return error(1, $error);
|
||||
} else {
|
||||
return ihttp_response_parse($data);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置http请求方式,假如没有post参数,则使用get方式,否则采用post方式
|
||||
$method = empty($post) ? 'GET' : 'POST';
|
||||
$fdata = "{$method} {$urlset['path']}{$urlset['query']} HTTP/1.1\r\n";
|
||||
$fdata .= "Host: {$urlset['host']}\r\n";
|
||||
if (function_exists('gzdecode')) {
|
||||
$fdata .= "Accept-Encoding: gzip, deflate\r\n";
|
||||
}
|
||||
$fdata .= "Connection: close\r\n";
|
||||
if (!empty($extra) && is_array($extra)) {
|
||||
foreach ($extra as $opt => $value) {
|
||||
if (!strexists($opt, 'CURLOPT_')) {
|
||||
$fdata .= "{$opt}: {$value}\r\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
$body = '';
|
||||
if ($post) {
|
||||
if (is_array($post)) {
|
||||
// 构建http请求参数
|
||||
$body = http_build_query($post);
|
||||
} else {
|
||||
if ($needUrlEncode) {
|
||||
$body = urlencode($post);
|
||||
} else {
|
||||
$body = $post;
|
||||
}
|
||||
}
|
||||
$fdata .= 'Content-Length: ' . strlen($body) . "\r\n\r\n{$body}";
|
||||
} else {
|
||||
$fdata .= "\r\n";
|
||||
}
|
||||
if ($urlset['scheme'] == 'https') {
|
||||
$fp = fsockopen('ssl://' . $urlset['host'], $urlset['port'], $errno, $error);
|
||||
} else {
|
||||
$fp = fsockopen($urlset['host'], $urlset['port'], $errno, $error);
|
||||
}
|
||||
stream_set_blocking($fp, true);
|
||||
stream_set_timeout($fp, $timeout);
|
||||
if (!$fp) {
|
||||
return error(1, $error);
|
||||
} else {
|
||||
fwrite($fp, $fdata);
|
||||
$content = '';
|
||||
while (!feof($fp)) {
|
||||
$content .= fgets($fp, 512);
|
||||
}
|
||||
fclose($fp);
|
||||
return ihttp_response_parse($content, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function ihttp_response_parse($data, $chunked = false)
|
||||
{
|
||||
$rlt = array();
|
||||
$headermeta = explode('HTTP/', $data);
|
||||
if (count($headermeta) > 2) {
|
||||
$data = 'HTTP/' . array_pop($headermeta);
|
||||
}
|
||||
$pos = strpos($data, "\r\n\r\n");
|
||||
$split1[0] = substr($data, 0, $pos);
|
||||
$split1[1] = substr($data, $pos + 4, strlen($data));
|
||||
|
||||
$split2 = explode("\r\n", $split1[0], 2);
|
||||
preg_match('/^(\S+) (\S+) (\S+)$/', $split2[0], $matches);
|
||||
$rlt['code'] = $matches[2];
|
||||
$rlt['status'] = $matches[3];
|
||||
$rlt['responseline'] = $split2[0];
|
||||
$header = explode("\r\n", $split2[1]);
|
||||
$isgzip = false;
|
||||
$ischunk = false;
|
||||
foreach ($header as $v) {
|
||||
$pos = strpos($v, ':');
|
||||
$key = substr($v, 0, $pos);
|
||||
$value = trim(substr($v, $pos + 1));
|
||||
if (is_array($rlt['headers'][$key])) {
|
||||
$rlt['headers'][$key][] = $value;
|
||||
} elseif (!empty($rlt['headers'][$key])) {
|
||||
$temp = $rlt['headers'][$key];
|
||||
unset($rlt['headers'][$key]);
|
||||
$rlt['headers'][$key][] = $temp;
|
||||
$rlt['headers'][$key][] = $value;
|
||||
} else {
|
||||
$rlt['headers'][$key] = $value;
|
||||
}
|
||||
if (!$isgzip && strtolower($key) == 'content-encoding' && strtolower($value) == 'gzip') {
|
||||
$isgzip = true;
|
||||
}
|
||||
if (!$ischunk && strtolower($key) == 'transfer-encoding' && strtolower($value) == 'chunked') {
|
||||
$ischunk = true;
|
||||
}
|
||||
}
|
||||
if ($chunked && $ischunk) {
|
||||
$rlt['content'] = ihttp_response_parse_unchunk($split1[1]);
|
||||
} else {
|
||||
$rlt['content'] = $split1[1];
|
||||
}
|
||||
if ($isgzip && function_exists('gzdecode')) {
|
||||
$rlt['content'] = gzdecode($rlt['content']);
|
||||
}
|
||||
|
||||
$rlt['meta'] = $data;
|
||||
if ($rlt['code'] == '100') {
|
||||
return ihttp_response_parse($rlt['content']);
|
||||
}
|
||||
return $rlt;
|
||||
}
|
||||
|
||||
function ihttp_response_parse_unchunk($str = null)
|
||||
{
|
||||
if (!is_string($str) or strlen($str) < 1) {
|
||||
return false;
|
||||
}
|
||||
$eol = "\r\n";
|
||||
$add = strlen($eol);
|
||||
$tmp = $str;
|
||||
$str = '';
|
||||
do {
|
||||
$tmp = ltrim($tmp);
|
||||
$pos = strpos($tmp, $eol);
|
||||
if ($pos === false) {
|
||||
return false;
|
||||
}
|
||||
$len = hexdec(substr($tmp, 0, $pos));
|
||||
if (!is_numeric($len) or $len < 0) {
|
||||
return false;
|
||||
}
|
||||
$str .= substr($tmp, ($pos + $add), $len);
|
||||
$tmp = substr($tmp, ($len + $pos + $add));
|
||||
$check = trim($tmp);
|
||||
} while (!empty($check));
|
||||
unset($tmp);
|
||||
return $str;
|
||||
}
|
||||
|
||||
|
||||
function ihttp_get($url)
|
||||
{
|
||||
return ihttp_request($url);
|
||||
}
|
||||
|
||||
|
||||
function ihttp_post($url, $data)
|
||||
{
|
||||
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
|
||||
return ihttp_request($url, $data, $headers);
|
||||
}
|
||||
|
||||
// 发送邮件
|
||||
// $to:接收人
|
||||
// $subject:邮件主题
|
||||
// $body:邮件正文
|
||||
function ihttp_email($to, $subject, $body, $global = false)
|
||||
{
|
||||
static $mailer;
|
||||
set_time_limit(0);
|
||||
|
||||
if (empty($mailer)) {
|
||||
if (!class_exists('PHPMailer')) {
|
||||
require IA_ROOT . '/framework/library/phpmailer/PHPMailerAutoload.php';
|
||||
}
|
||||
$mailer = new PHPMailer();
|
||||
global $_W;
|
||||
$config = $GLOBALS['_W']['setting']['mail'];
|
||||
if (!$global) {
|
||||
$row = pdo_fetch("SELECT `notify` FROM " . tablename('uni_settings') . " WHERE uniacid = :uniacid", array(':uniacid' => $_W['uniacid']));
|
||||
$row['notify'] = @iunserializer($row['notify']);
|
||||
if (!empty($row['notify']) && !empty($row['notify']['mail'])) {
|
||||
$config = $row['notify']['mail'];
|
||||
}
|
||||
}
|
||||
|
||||
$config['charset'] = 'utf-8';
|
||||
if ($config['smtp']['type'] == '163') {
|
||||
$config['smtp']['server'] = 'smtp.163.com';
|
||||
$config['smtp']['port'] = 25;
|
||||
} else {
|
||||
if (!empty($config['smtp']['authmode'])) {
|
||||
$config['smtp']['server'] = 'ssl://' . $config['smtp']['server'];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($config['smtp']['authmode'])) {
|
||||
if (!extension_loaded('openssl')) {
|
||||
return error(1, '请开启 php_openssl 扩展!');
|
||||
}
|
||||
}
|
||||
$mailer->signature = $config['signature'];
|
||||
$mailer->isSMTP();
|
||||
$mailer->CharSet = $config['charset'];
|
||||
$mailer->Host = $config['smtp']['server'];
|
||||
$mailer->Port = $config['smtp']['port'];
|
||||
$mailer->SMTPAuth = true;
|
||||
$mailer->Username = $config['username'];
|
||||
$mailer->Password = $config['password'];
|
||||
!empty($config['smtp']['authmode']) && $mailer->SMTPSecure = 'ssl';
|
||||
|
||||
$mailer->From = $config['username'];
|
||||
$mailer->FromName = $config['sender'];
|
||||
$mailer->isHTML(true);
|
||||
}
|
||||
if (!empty($mailer->signature)) {
|
||||
$body .= htmlspecialchars_decode($mailer->signature);
|
||||
}
|
||||
$mailer->Subject = $subject;
|
||||
$mailer->Body = $body;
|
||||
$mailer->addAddress($to);
|
||||
if ($mailer->send()) {
|
||||
return true;
|
||||
} else {
|
||||
return error(1, $mailer->ErrorInfo);
|
||||
}
|
||||
}
|
||||
|
||||
// 发送POST请求(模板消息尤其适用)
|
||||
function do_post_request($url, $data, $optional_headers = null)
|
||||
{
|
||||
$params = array(
|
||||
'http' => array(
|
||||
'method' => 'POST',
|
||||
'content' => $data,
|
||||
),
|
||||
);
|
||||
|
||||
if ($optional_headers !== null) {
|
||||
$params['http']['header'] = $optional_headers;
|
||||
}
|
||||
$ctx = stream_context_create($params);
|
||||
$fp = @fopen($url, 'rb', false, $ctx);
|
||||
if (!$fp) {
|
||||
throw new Exception("Problem with $url, $php_errormsg");
|
||||
}
|
||||
$response = @stream_get_contents($fp);
|
||||
if ($response === false) {
|
||||
throw new Exception("Problem reading data from $url, $php_errormsg");
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
// 字符集
|
||||
define('USEDCHARSET', 'utf-8');
|
||||
|
||||
/**
|
||||
* @note 转换分隔符
|
||||
* @param string $string
|
||||
* @return mixed
|
||||
*/
|
||||
function ConvertSeparator($string)
|
||||
{
|
||||
$result = $string;
|
||||
while (false !== mb_stripos($result, '\\', 0, USEDCHARSET)) {
|
||||
$result = str_replace('\\', '/', $result);
|
||||
}
|
||||
while (false !== mb_stripos($result, '//', 0, USEDCHARSET)) {
|
||||
$result = str_replace('//', '/', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @date 2017-03-04
|
||||
* @note 获取当前页面的url地址
|
||||
* @return string
|
||||
* @auth 应俊
|
||||
*/
|
||||
function GetLocaleUrl()
|
||||
{
|
||||
if (isset($_SERVER['PHP_SELF'])) {
|
||||
$objectname = $_SERVER['PHP_SELF'];
|
||||
} else {
|
||||
$filename = ConvertSeparator(__FILE__);
|
||||
$pathname = ConvertSeparator(isset($_SERVER['CONTEXT_DOCUMENT_ROOT']) ? $_SERVER['CONTEXT_DOCUMENT_ROOT'] : '');
|
||||
$pathlength = mb_strlen($pathname, USEDCHARSET);
|
||||
$filelength = mb_strlen($filename, USEDCHARSET);
|
||||
|
||||
if (false !== stripos($filename, $pathname))
|
||||
$objectname = mb_substr($filename, $pathlength, $filelength - $pathlength, USEDCHARSET);
|
||||
else
|
||||
$objectname = $filename;
|
||||
}
|
||||
|
||||
|
||||
$https = (isset($_SERVER['REQUEST_SCHEME']) && strcasecmp($_SERVER['REQUEST_SCHEME'], 'https') == 0) ||
|
||||
(isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') == 0 || strcasecmp($_SERVER['HTTPS'], '1') == 0));
|
||||
$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 0;
|
||||
$servername = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
|
||||
|
||||
if ($https)
|
||||
$uri = 'https://' . $servername . (443 == $port ? '' : ':' . $port) . $objectname;
|
||||
else
|
||||
$uri = 'http://' . $servername . (80 == $port ? '' : ':' . $port) . $objectname;
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @date 2017-03-04
|
||||
* @note 给参数按key的顺序排序。(支持递归)
|
||||
* @param mixed $parameter 要排序的参数
|
||||
* @return array
|
||||
* @auth 应俊
|
||||
*/
|
||||
function SortParam($parameter)
|
||||
{
|
||||
$parameter = (array)$parameter;
|
||||
foreach ($parameter as $k => $v) {
|
||||
if (is_array($v) || is_object($v)) {
|
||||
$parameter[$k] = SortParam($v);
|
||||
}
|
||||
}
|
||||
|
||||
// 调用strcmp函数来排序,该函数区分大小写。
|
||||
uksort($parameter, 'strcmp');
|
||||
return $parameter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @date 2017-03-06
|
||||
* @note 转换参数成字符串形式按key=value的形式,用&分隔)。
|
||||
* @param mixed $parameter 要转换的参数
|
||||
* @return string
|
||||
* @auth 应俊
|
||||
*/
|
||||
function ConvertParam($parameter)
|
||||
{
|
||||
$parameter = (array)$parameter;
|
||||
$return = '';
|
||||
foreach ($parameter as $k => $v) {
|
||||
if (is_array($v) || is_object($v)) {
|
||||
$return .= sprintf('&%s={%s}', $k, ConvertParam($v));
|
||||
} else {
|
||||
$return .= sprintf('&%s=%s', $k, $v);
|
||||
}
|
||||
}
|
||||
|
||||
$sublen = mb_strlen('&', USEDCHARSET);
|
||||
$retlen = mb_strlen($return, USEDCHARSET);
|
||||
$return = mb_substr($return, $sublen, $retlen - $sublen, USEDCHARSET);
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @date 2017-03-04
|
||||
* @note 为参数生成签名
|
||||
* @param mixed $parameter 要签名的参数
|
||||
* @param string $signkey 签名key
|
||||
* @return string
|
||||
* @auth 应俊
|
||||
*/
|
||||
function SignParameter($parameter, $signkey = '')
|
||||
{
|
||||
// 1:先把参数按参数名(key)从小到大排序
|
||||
$parameter = SortParam($parameter);
|
||||
|
||||
// 2:连接参数成一个字符串(按key=value的形式,用&分隔)。
|
||||
$return = ConvertParam($parameter);
|
||||
|
||||
// 3:结尾加上key=签名key
|
||||
$return .= '&key=' . $signkey;
|
||||
|
||||
// 4:md5加密这个字符串
|
||||
return md5($return);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @note 转换数据编码
|
||||
* @param $data
|
||||
* @param string $charset
|
||||
* @return string
|
||||
* @auther 应俊
|
||||
*/
|
||||
function Characet($data, $charset = USEDCHARSET)
|
||||
{
|
||||
if (!empty ($data)) {
|
||||
$encoding = mb_detect_encoding($data, array('ASCII', 'UTF-8', 'GBK', 'GB2312', 'LATIN1', 'BIG5',));
|
||||
if (0 != strcasecmp($encoding, $charset))
|
||||
$data = mb_convert_encoding($data, $charset, $encoding);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @return array|mixed
|
||||
*/
|
||||
function ChangePostData($data)
|
||||
{
|
||||
switch (gettype($data)) {
|
||||
case 'array': {
|
||||
foreach ($data as $key => $value) {
|
||||
$data[$key] = ChangePostData($value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'object': {
|
||||
$array = (array)$data;
|
||||
foreach ($array as $key => $value) {
|
||||
$data->$key = ChangePostData($value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
$data = preg_replace_callback('/\+/', function ($r) {
|
||||
return '%2B';
|
||||
}, $data);
|
||||
$data = preg_replace_callback('/\&/', function ($r) {
|
||||
return '%26';
|
||||
}, $data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @note 发送post请求
|
||||
* @param string $url
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
function SendPost($url, $data)
|
||||
{
|
||||
$data = http_build_query(ChangePostData($data));
|
||||
|
||||
$opts = array(
|
||||
'http' => array(
|
||||
'method' => 'POST',
|
||||
'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n",
|
||||
'content' => $data,
|
||||
),
|
||||
);
|
||||
|
||||
$context = stream_context_create($opts);
|
||||
$ret = file_get_contents($url, false, $context);
|
||||
$ret = trim($ret, "\xEF\xBB\xBF");
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @note 从参数中过滤不需要的参数
|
||||
* @param array|null $ignores 要过滤的参数列表(参数名)
|
||||
* @param array|null $parameters 目标参数列表
|
||||
* @return array
|
||||
*/
|
||||
function GetAttachParameters($ignores = null, $parameters = null)
|
||||
{
|
||||
$return = (array)(empty($parameters) ? $_REQUEST : $parameters);
|
||||
if (!empty($ignores)) {
|
||||
foreach ($return as $k => $v) {
|
||||
if (in_array($k, $ignores))
|
||||
unset($return[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
function GetClientAddress()
|
||||
{
|
||||
static $_ClientAddress = null;
|
||||
|
||||
if ($_ClientAddress !== null)
|
||||
return $_ClientAddress;
|
||||
|
||||
if (isset($_SERVER)) {
|
||||
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||
$List = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
||||
|
||||
/// 取X-Forwarded-For中第一个非unknown的有效IP字符串
|
||||
foreach ($List as $Item) {
|
||||
$Item = trim($Item);
|
||||
if (strcasecmp($Item, 'unknown') != 0) {
|
||||
$_ClientAddress = $Item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} elseif (isset($_SERVER['HTTP_CLIENT_IP']))
|
||||
$_ClientAddress = $_SERVER['HTTP_CLIENT_IP'];
|
||||
else {
|
||||
if (isset($_SERVER['REMOTE_ADDR']))
|
||||
$_ClientAddress = $_SERVER['REMOTE_ADDR'];
|
||||
else
|
||||
$_ClientAddress = '0.0.0.0';
|
||||
}
|
||||
} else {
|
||||
if (getenv('HTTP_X_FORWARDED_FOR'))
|
||||
$_ClientAddress = getenv('HTTP_X_FORWARDED_FOR');
|
||||
elseif (getenv('HTTP_CLIENT_IP'))
|
||||
$_ClientAddress = getenv('HTTP_CLIENT_IP');
|
||||
else
|
||||
$_ClientAddress = getenv('REMOTE_ADDR');
|
||||
}
|
||||
|
||||
preg_match('/[\d\.]{7,15}/', $_ClientAddress, $List);
|
||||
$_ClientAddress = !empty($List[0]) ? $List[0] : null;
|
||||
|
||||
return $_ClientAddress;
|
||||
}
|
||||
77
codes/agent/game/api/framework/function/compat.func.php
Normal file
77
codes/agent/game/api/framework/function/compat.func.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
defined('IN_IA') or exit('Access Denied');
|
||||
|
||||
if (!function_exists('json_encode')) {
|
||||
function json_encode($value) {
|
||||
static $jsonobj;
|
||||
if (!isset($jsonobj)) {
|
||||
include_once (IA_ROOT . '/framework/library/json/JSON.php');
|
||||
$jsonobj = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
|
||||
}
|
||||
return $jsonobj->encode($value);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('json_decode')) {
|
||||
// 将传入的字符串解码为一个json字符串
|
||||
function json_decode($jsonString) {
|
||||
// decode:解码为一个JSON字符串
|
||||
static $jsonobj;
|
||||
if (!isset($jsonobj)) {
|
||||
include_once (IA_ROOT . '/framework/library/json/JSON.php');
|
||||
$jsonobj = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
|
||||
}
|
||||
return $jsonobj->decode($jsonString);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('http_build_query')) {
|
||||
// 构建http请求参数
|
||||
function http_build_query($formdata, $numeric_prefix = null, $arg_separator = null) {
|
||||
// 假如参数不是数组的话,则直接返回false
|
||||
if (!is_array($formdata))
|
||||
return false;
|
||||
// 假如没有设置参数分割符,则默认设置为&
|
||||
if ($arg_separator == null)
|
||||
$arg_separator = '&';
|
||||
return http_build_recursive($formdata, $arg_separator);
|
||||
}
|
||||
// 采用递归的方式拼接参数为字符串
|
||||
// 参数1:要拼接的参数数组
|
||||
// 参数2:要拼接使用的参数分割符
|
||||
function http_build_recursive($formdata, $separator, $key = '', $prefix = '') {
|
||||
$rlt = '';
|
||||
foreach ($formdata as $k => $v) {
|
||||
if (is_array($v)) {
|
||||
if ($key)
|
||||
$rlt .= http_build_recursive($v, $separator, $key . '[' . $k . ']', $prefix);
|
||||
else
|
||||
$rlt .= http_build_recursive($v, $separator, $k, $prefix);
|
||||
} else {
|
||||
if ($key)
|
||||
$rlt .= $prefix . $key . '[' . urlencode($k) . ']=' . urldecode($v) . '&';
|
||||
else
|
||||
$rlt .= $prefix . urldecode($k) . '=' . urldecode($v) . '&';
|
||||
}
|
||||
}
|
||||
return $rlt;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('file_put_contents')) {
|
||||
function file_put_contents($file, $string) {
|
||||
$fp = @fopen($file, 'w') or exit("Can not open $file");
|
||||
flock($fp, LOCK_EX);
|
||||
$stringlen = @fwrite($fp, $string);
|
||||
flock($fp, LOCK_UN);
|
||||
@fclose($fp);
|
||||
return $stringlen;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('getimagesizefromstring')) {
|
||||
function getimagesizefromstring($string_data) {
|
||||
$uri = 'data://application/octet-stream;base64,' . base64_encode($string_data);
|
||||
return getimagesize($uri);
|
||||
}
|
||||
}
|
||||
1202
codes/agent/game/api/framework/function/global.func.php
Normal file
1202
codes/agent/game/api/framework/function/global.func.php
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user