www.gusucode.com > 仿163地方门户论坛网站源码程序 > 整站安装/整站源码/source/plugin/jjjgamewebgameplatform/core.class.php

    <?php
	if(!defined('IN_DISCUZ')) {
		exit('Access Denied');
	}
	abstract class JCore{
		public static $APPID;
		public static $APPKEY;
		public static $PARTNER;
		public static $REPORTERR = '0';
		protected static $curl = null;

		abstract protected function create_key($params);

		# 兼容构造函数
		function JCore() {
			$this->__construct();
		}

		# 构造函数
		function __construct()
		{
			global $_G;
			if (null===self::$curl)self::$curl = function_exists('curl_init');

			self::$APPID	= trim($_G['cache']['plugin']['jjjgamewebgameplatform']['appid']);
			self::$APPKEY	= trim($_G['cache']['plugin']['jjjgamewebgameplatform']['appkey']);
			self::$PARTNER	= (int)$_G['cache']['plugin']['jjjgamewebgameplatform']['partner'];
			self::$REPORTERR= $_G['cache']['plugin']['jjjgamewebgameplatform']['reporterr'];
		}

		# 更新缓存
		# @param string $key
		# @param string $value
		# @return boolean
		public static function setVars($key, $value) {
			global $_G;
			# 更新数据库
			$pluginid = DB::result_first("select `pluginid` from " . DB::table('common_plugin') . " where `identifier` = 'jjjgamewebgameplatform'");
			$result = DB::update('common_pluginvar', array('value' => $value), "`pluginid` = '$pluginid' and `variable` = '$key'");

			# 更新缓存
			setglobal('cache/plugin/jjjgamewebgameplatform/' . $key, $value);

			# 更新缓存
			save_syscache('plugin', $_G['cache']['plugin']);

			# 重新载入缓存
			return $result;
		}

		# HTTP GET
		# @param string $url
		# @return string
		public static function get_api($url)
		{
			if (self::$curl)
			{
				$curl = self::curl($url);
				$data = curl_exec($curl);
				curl_close($curl);
				return $data;
			}
			else
			{
				$snoopy = self::snoopy();
				$snoopy->fetch($url);
				return $snoopy->results;
			}
		}

		# HTTP POST
		# @param string $url
		# @param array $data
		# @return string
		public static function post_api($url, $data)
		{
			if (self::$curl)
			{
				$curl = self::curl($url);
				curl_setopt($curl, CURLOPT_POST, true);
				curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
				$data = curl_exec($curl);
				curl_close($curl);

				return $data;
			}
			else
			{
				$snoopy = self::snoopy();
				$snoopy->submit($url,$data);
				return $snoopy->results;
			}
		}

		# 获取Snoopy对象
		protected static function snoopy()
		{
			if ( !class_exists('Snoopy',false) )
			{
				include 'Snoopy.class.php';
			}
			return new Snoopy();
		}

		# curl通讯
		protected static function curl($url,$timeout=5)
		{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER, false);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

			if ( preg_match('#^https://#i', $url) )
			{
				curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			}
			curl_setopt($ch, CURLOPT_USERAGENT, 'jjjgamewebgameplatform plugin client');
			return $ch;
		}

		# 数组转编码
		# @param string $in_charset
		# @param string $out_charset
		# @param array  $arr
		public static function array_iconv($in_charset, $out_charset, $arr){
			return eval('return ' . iconv($in_charset, $out_charset, var_export($arr, true) . ';'));   
		}
	}

?>