PageRenderTime 80ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/core/CPS360_api.class.php

https://github.com/j2thinker/360CPS-API-PHP
PHP | 388 lines | 304 code | 61 blank | 23 comment | 43 complexity | c28ef73e5b0288943909ea6bf1791b5c MD5 | raw file
  1. <?PHP
  2. define('CPS360_ROOT',dirname(dirname(__FILE__)));
  3. require_once(CPS360_ROOT . '/api_config.php');
  4. require_once(CPS360_ROOT . '/core/CPS360_plugin.class.php');
  5. require_once(CPS360_ROOT . '/core/CPS360_models.class.php');
  6. //时区设置
  7. ini_set('date.timezone',CPS360_config::TIME_ZONE);
  8. date_default_timezone_set(CPS360_config::TIME_ZONE);
  9. class CPS360_api{
  10. /********************************* API Inner Config *********************************/
  11. const VERSION = '0.1.4';
  12. const BUILD = '201207101200';
  13. const REPORT_URL = 'http://open.union.360.cn/gofailed';
  14. const ACTIVE_PERIOD = 900;
  15. const MAXNUM = 2000;
  16. static private $DEBUG = false;
  17. /********************************* API Inner Param *********************************/
  18. static private $_API_PARAMS = array(
  19. 'redirect' => array('bid','qihoo_id','url','from_url','active_time','ext','qid','qmail','qname','sign','verify'),
  20. 'order' => array('bid','order_ids','start_time','end_time','updstart_time','updend_time','last_order_id','active_time','sign'),
  21. 'check' => array('bid','bill_month','last_order_id','active_time','sign'),
  22. );
  23. /********************************* CPS API *********************************/
  24. static public function redirect(){
  25. $params = self::_param_get('redirect');
  26. $plugin = self::_plugin_load();
  27. //Cookie
  28. $cookies = array(
  29. 'id' => '360cps',
  30. 'qihoo_id' => $params['qihoo_id'],
  31. 'ext' => $params['ext'],
  32. 'qid' => $params['qid'],
  33. 'qmail' => $params['qmail'],
  34. 'qname' => $params['qname'],
  35. );
  36. $cur_domainroot = self::_domainroot();
  37. $cookie_param = array(
  38. 'name' => CPS360_config::COOKIE_NAME,
  39. 'value' => self::serialize($cookies),
  40. 'expires' => time() + (86400 * CPS360_config::RD),
  41. 'path' => '/',
  42. );
  43. self::_debug_output('Cookie Set',$cookie_param);
  44. foreach(array($cur_domainroot,'.'.$cur_domainroot) as $domain){
  45. setcookie($cookie_param['name'],$cookie_param['value'],$cookie_param['expires'],$cookie_param['path'],$domain);
  46. }
  47. //Activetime & Sign
  48. $check_activetime = self::_check_activeTime($params['active_time']);
  49. $check_sign = self::_check_sign($params,'redirect');
  50. if(!$check_activetime['isfine'] || !$check_sign['isfine']){
  51. //错误报告
  52. $data_report = array(
  53. 'bid' => CPS360_config::BID,
  54. 'active_time' => time(),
  55. 'sign' => $check_sign['resign'],
  56. 'pre_bid' => $params['bid'],
  57. 'pre_active_time' => $params['active_time'],
  58. 'pre_sign' => $params['sign'],
  59. 'qid' => $params['qid'],
  60. 'qname' => $params['qname'],
  61. 'qmail' => $params['qmail'],
  62. 'from_url' => $params['from_url'],
  63. 'from_ip' => self::_ip_get(),
  64. 'from_ua' => $_SERVER['HTTP_USER_AGENT'],
  65. );
  66. self::_http_request(self::REPORT_URL,'post',$data_report);
  67. self::_debug_output('Error Report',$data_report);
  68. //Clear Userinfo
  69. $params['qid'] = $params['qmail'] = $params['qname'] = false;
  70. }elseif($params['verify']){
  71. self::_verify();
  72. }
  73. //Auto login
  74. if($params['qid'] > 0){
  75. $plugin->login_auto($params['qid'],$params['qmail'],$params['qname']);
  76. }
  77. //Redirect
  78. if($params['url'] && self::_domainroot($params['url']) == $cur_domainroot){
  79. $url = $params['url'];
  80. }else{
  81. $url = CPS360_config::REDIRECT_DEFAULT;
  82. }
  83. if(!self::$DEBUG){
  84. header('Location:'.$url);
  85. }else{
  86. self::_debug_output('Header','Location:'.$url);
  87. }
  88. }
  89. static public function order(){
  90. $params = self::_param_get('order');
  91. $plugin = self::_plugin_load();
  92. //Activetime & Sign
  93. $check_activetime = self::_check_activeTime($params['active_time']);
  94. $check_sign = self::_check_sign($params,'order');
  95. if(!$check_activetime['isfine'] || !$check_sign['isfine']){
  96. self::_output($check_activetime['message'].$check_sign['message']);
  97. }
  98. //调用用户方法
  99. if($params['order_ids']){
  100. $result = $plugin->order_by_ids($params['order_ids']);
  101. }elseif($params['start_time'] && $params['end_time']){
  102. $result = $plugin->order_by_time($params['start_time'],$params['end_time'],$params['last_order_id']);
  103. }elseif($params['updstart_time'] && $params['updend_time']){
  104. $result = $plugin->order_by_updtime($params['updstart_time'],$params['updend_time'],$params['last_order_id']);
  105. }else{
  106. $result = false;
  107. }
  108. //Output
  109. $xmldoc = self::_xml_generate($result);
  110. self::_output($xmldoc);
  111. }
  112. static public function check(){
  113. $params = self::_param_get('check');
  114. $plugin = self::_plugin_load();
  115. //Activetime & Sign
  116. $check_activetime = self::_check_activeTime($params['active_time']);
  117. $check_sign = self::_check_sign($params,'check');
  118. if(!$check_activetime['isfine'] || !$check_sign['isfine']){
  119. self::_output($check_activetime['message'].$check_sign['message']);
  120. }
  121. //调用用户方法
  122. $result = $plugin->check_by_month($params['bill_month'],$params['last_order_id']);
  123. //Output
  124. $xmldoc = self::_xml_generate($result);
  125. self::_output($xmldoc);
  126. }
  127. static public function order_save($order_id,$data){
  128. $plugin = self::_plugin_load();
  129. $plugin->order_save($order_id,$data);
  130. }
  131. /********************************* CPS Utility *********************************/
  132. static private function _param_get($type = ''){
  133. $paramsneeds = isset(self::$_API_PARAMS[$type]) ? self::$_API_PARAMS[$type] : self::$_API_PARAMS['order'];
  134. $params = array();
  135. foreach($paramsneeds as $key){
  136. if(isset($_POST[$key])){
  137. $params[$key] = $_POST[$key];
  138. }elseif(self::$DEBUG && isset($_GET[$key])){
  139. $params[$key] = $_GET[$key];
  140. }else{
  141. $params[$key] = null;
  142. }
  143. }
  144. self::_debug_output('Params Get',$params);
  145. return $params;
  146. }
  147. static private function _check_activeTime($active_time = 0){
  148. if(abs(time() - $active_time) > self::ACTIVE_PERIOD){
  149. $result = array('isfine' => false,'message' => '参数已过期');
  150. }else{
  151. $result = array('isfine' => true,'message' => '');
  152. }
  153. return $result;
  154. }
  155. static private function _check_sign($params,$type = ''){
  156. if($type == 'redirect'){
  157. $resign = CPS360_config::BID
  158. .'#'.$params['active_time']
  159. .'#'.CPS360_config::CP_KEY
  160. .'#'.$params['qid']
  161. .'#'.$params['qmail']
  162. .'#'.$params['qname'];
  163. }else{
  164. $resign = CPS360_config::BID
  165. .'#'.$params['active_time']
  166. .'#'.CPS360_config::CP_KEY;
  167. }
  168. $resign = md5($resign);
  169. if ($params['sign'] && $params['sign'] !== $resign){
  170. $result = array('isfine' => false,'message' => '验证失败','resign' => $resign);
  171. }else{
  172. $result = array('isfine' => true,'message' => '');
  173. }
  174. return $result;
  175. }
  176. static private function _xml_generate($list){
  177. $list = is_array($list) ? $list : array();
  178. $xmldoc = $xmldoc_order = '';
  179. $i = 0;
  180. foreach($list as $obj){
  181. if(++$i > self::MAXNUM) break;
  182. $xmldoc_order .= $obj->to_xml()."\n";
  183. }
  184. $xmldoc = '<?xml version="1.0" encoding="utf-8"?>'."\n".'<orders>'."\n".$xmldoc_order.'</orders>';
  185. if(!mb_check_encoding($xmldoc,'UTF-8')){
  186. $xmldoc = mb_convert_encoding($xmldoc,'UTF-8','GBK');
  187. }
  188. return $xmldoc;
  189. }
  190. static private function _domainroot($url = ''){
  191. $url = $url ? $url : 'http://'.$_SERVER['HTTP_HOST'];
  192. $url = 'http://'.str_replace(array('http://','https://'),'',$url);
  193. $parsed_url = parse_url($url);
  194. $host_array = array_reverse(explode('.',$parsed_url['host']));
  195. $result = array();
  196. foreach($host_array as $key => $value){
  197. if(
  198. $key == 0
  199. ||
  200. $key == 1
  201. ||
  202. $key == 2 && in_array($host_array['1'],array('com','net','org','edu'))
  203. ){
  204. $result[] = $value;
  205. }
  206. }
  207. $result = implode('.',array_reverse($result));
  208. return $result;
  209. }
  210. static private function _http_request($url,$method = 'get',$data = array()){
  211. $ch = curl_init();
  212. curl_setopt_array($ch,array(
  213. CURLOPT_RETURNTRANSFER => true,
  214. CURLOPT_FOLLOWLOCATION => false,
  215. CURLOPT_USERAGENT => '',
  216. CURLOPT_TIMEOUT => 10,
  217. CURLOPT_URL => $url,
  218. )
  219. );
  220. //Post
  221. if($method == 'post') {
  222. $postdata = array();
  223. foreach($data as $key => $value){
  224. $postdata[] = urlencode($key).'='.urlencode($value);
  225. }
  226. curl_setopt_array($ch,array(
  227. CURLOPT_POST => true,
  228. CURLOPT_POSTFIELDS => implode('&',$postdata),
  229. )
  230. );
  231. }
  232. curl_exec($ch);
  233. curl_close($ch);
  234. }
  235. static private function _ip_get(){
  236. $ip = '';
  237. if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  238. $ip = getenv('HTTP_CLIENT_IP');
  239. } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  240. $ip = getenv('HTTP_X_FORWARDED_FOR');
  241. } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  242. $ip = getenv('REMOTE_ADDR');
  243. } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  244. $ip = $_SERVER['REMOTE_ADDR'];
  245. }
  246. return $ip;
  247. }
  248. static private function _plugin_load(){
  249. require_once(CPS360_ROOT . '/plugin/'.CPS360_config::PLUGIN_NAME.'.php');
  250. $classname = CPS360_config::PLUGIN_NAME;
  251. return new $classname;
  252. }
  253. static private function _output($content){
  254. if(self::$DEBUG){
  255. self::_debug_output('Output',$content);
  256. }else{
  257. if(stripos($content,'<?xml') !== false){
  258. header("Content-type: text/xml; charset=utf-8");
  259. }
  260. echo $content;
  261. exit;
  262. }
  263. }
  264. static private function _debug_output($title,$data){
  265. if(self::$DEBUG){
  266. $data = htmlspecialchars(print_r($data,true));
  267. echo '<h3>'.$title.'</h3>';
  268. echo '<pre>'.$data.'</pre>';
  269. }
  270. }
  271. static private function _verify(){
  272. $content =
  273. '<?xml version="1.0" encoding="utf-8"?>
  274. <verify>
  275. <version>'.self::VERSION.'</version>
  276. <build>'.self::BUILD.'</build>
  277. <report_url>'.self::REPORT_URL.'</report_url>
  278. <active_period>'.self::ACTIVE_PERIOD.'</active_period>
  279. <maxnum>'.self::MAXNUM.'</maxnum>
  280. <sign>
  281. <api>'.md5(file_get_contents(CPS360_ROOT . '/CPS360_api.class.php')).'</api>
  282. <model>'.md5(file_get_contents(CPS360_ROOT . '/CPS360_models.class.php')).'</model>
  283. <plugin>'.md5(file_get_contents(CPS360_ROOT . '/CPS360_plugin.class.php')).'</plugin>
  284. </sign>
  285. </verify>
  286. ';
  287. self::_output($content);
  288. }
  289. /********************************* CPS Global *********************************/
  290. static public function round($val,$precision = 0){
  291. /*
  292. * BUGFIX: Round() 在 PHP 5.27 前规范不正确
  293. * http://www.php.net/manual/en/function.round.php
  294. */
  295. if(version_compare(PHP_VERSION,'5.3.0','>')){
  296. $val = round($val,$precision);
  297. }else{
  298. $val = floatval(sprintf('%f',$val));
  299. $precision = intval($precision);
  300. $pow = pow(10,$precision + 1);
  301. $val = round($val * $pow,-1);
  302. $val = sprintf('%.'.$precision.'f',$val / $pow);
  303. $val = floatval($val);
  304. }
  305. return $val;
  306. }
  307. static public function serialize($var){
  308. $str = serialize($var);
  309. $md5 = md5(CPS360_config::CP_KEY.$str);
  310. return urlencode($md5.$str);
  311. }
  312. static public function unserialize($str){
  313. if(!$str) return false;
  314. $str = urldecode($str);
  315. $md5 = substr($str,0,32);
  316. $str = substr($str,32);
  317. $remd5 = md5(CPS360_config::CP_KEY.$str);
  318. if($md5 !== $remd5) return false;
  319. return unserialize($str);
  320. }
  321. static public function debug($val){
  322. self::$DEBUG = $val;
  323. if($val){
  324. error_reporting(E_ALL);
  325. }
  326. }
  327. }