commonFun.class.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /* Desc : 修改 common.php 为Class版本,方便使用
  3. *
  4. * Para :
  5. *
  6. * Author:
  7. *
  8. **/
  9. class commonFunc{
  10. /*
  11. * curl 调用
  12. */
  13. //echo post_request("http://www.baidu.com");
  14. public static function post_request($url, $params=array()) {
  15. $ch = curl_init();
  16. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  17. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
  18. //curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/html"));
  19. curl_setopt($ch, CURLOPT_URL, $url);
  20. curl_setopt($ch, CURLOPT_POST, true);
  21. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  22. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  23. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  24. $result = curl_exec($ch);
  25. $httpinfo= curl_getinfo($ch);
  26. curl_close($ch);
  27. //print_r($httpinfo);
  28. $info = "{$httpinfo['http_code']}|$url|$result";
  29. Log::save_run_log($info,'curl');
  30. if($httpinfo['http_code'] == 200){
  31. return $result;
  32. } else {
  33. return false;
  34. }
  35. }
  36. public static function makeRequest($url, $params,$timeout=3,$post=true) {
  37. $ch = curl_init();
  38. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  39. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
  40. curl_setopt($ch, CURLOPT_URL, $url);
  41. curl_setopt($ch, CURLOPT_POST, $post);
  42. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  44. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  45. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  46. $result = curl_exec($ch);
  47. curl_close($ch);
  48. return $result;
  49. }
  50. public static function get_request_https($url){
  51. $ch = curl_init();
  52. curl_setopt($ch, CURLOPT_URL,$url);
  53. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  54. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  56. curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  57. if(TEST) curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8086');
  58. $result = curl_exec($ch);
  59. $httpinfo = curl_getinfo($ch);
  60. $info = "{$httpinfo['http_code']}|$url|$result";
  61. Log::save_run_log($info,'curlhttps');
  62. curl_close($ch);
  63. if($httpinfo['http_code'] == 200){
  64. return $result;
  65. } else {
  66. return false;
  67. }
  68. }
  69. public static function xml_unserialize(&$xml, $isnormal = FALSE) {
  70. $xml_parser = new XML($isnormal);
  71. $data = $xml_parser->parse($xml);
  72. $xml_parser->destruct();
  73. return $data;
  74. }
  75. /**
  76. * 获得请求端的xml
  77. */
  78. public static function post_request_xml($url, $params=array()) {
  79. $ch = curl_init();
  80. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  81. //curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
  82. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/html"));
  83. curl_setopt($ch, CURLOPT_URL, $url);
  84. curl_setopt($ch, CURLOPT_POST, true);
  85. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  86. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  87. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  88. $result = file_get_contents('php://input');
  89. $httpinfo= curl_getinfo($ch);
  90. curl_close($ch);
  91. //print_r($httpinfo);
  92. $info = "{$httpinfo['http_code']}|$url|$result";
  93. Log::save_run_log($info,'curl');
  94. if($httpinfo['http_code'] == 200){
  95. return $result;
  96. } else {
  97. return false;
  98. }
  99. }
  100. /**
  101. * 获得请求端的ip地址
  102. */
  103. public static function get_client_ip() {
  104. global $_SERVER;
  105. if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
  106. $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  107. } elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
  108. $ip = $_SERVER["HTTP_CLIENT_IP"];
  109. } else {
  110. $ip = $_SERVER["REMOTE_ADDR"];
  111. }
  112. return $ip;
  113. }
  114. /**
  115. * 接口999操作失败
  116. * forexample: response_999(__FILE__,__LINE__,__FUNCTION__,"SQL");
  117. */
  118. public static function response_999($file,$line,$function,$err_type){
  119. $parameters = get_parameters();
  120. $file = strstr($file,"u.");
  121. $log = "|response:999|file:$file|line:$line|function:$function|err_type:$err_type|$parameters";
  122. Log::save_run_log($log,"run");
  123. echo "-999";
  124. exit;
  125. }
  126. /**
  127. * 接口返回结果函数
  128. */
  129. public static function response($result){
  130. echo $result;
  131. exit;
  132. }
  133. /**
  134. * 取得精确时间值
  135. */
  136. public static function microtime_float(){
  137. list($usec, $sec) = explode(" ", microtime());
  138. return ((float)$usec + (float)$sec);
  139. }
  140. /**
  141. * 取得请求参数,并组成字符串
  142. * 格式为 {parameter:value|parameter:value|parameter:value|parameter:value……………………}
  143. */
  144. public static function get_parameters() {
  145. global $_GET,$_POST;
  146. $parameters = "";
  147. if($_GET){
  148. foreach($_GET as $key => $val){
  149. if($key == 'sign')
  150. continue;
  151. else
  152. $parameters .= "$key:$val|";
  153. }
  154. }
  155. if($_POST){
  156. foreach($_POST as $key => $val){
  157. if($key == 'sign')
  158. continue;
  159. else
  160. $parameters .= "$key:$val|";
  161. }
  162. }
  163. if(strlen($parameters) > 1 )
  164. $parameters = substr($parameters,0,-1);
  165. return "{".$parameters."}";
  166. }
  167. public static function get_browser_language() {
  168. if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
  169. $accept_lang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  170. return $accept_lang[0];
  171. } else {
  172. return false;
  173. }
  174. }
  175. public static function filterInput(&$info){
  176. if(empty($info)) return false;
  177. if(is_array($info)){
  178. foreach ($info as $key=>$val){
  179. $info[$key] = addslashes($val);
  180. }
  181. } else {
  182. $info = addslashes($info);
  183. }
  184. }
  185. public static function matchSex($total_friend,$sex,$friends){
  186. $ran = rand(0,$total_friend-1);
  187. if($sex == 2) return $ran;
  188. if ($sex == 1 and $friends[$ran]['sex']=='male'){
  189. return $ran;
  190. } else if ($sex == 0 and $friends[$ran]['sex']=='female'){
  191. return $ran;
  192. } else {
  193. $ran = matchSex($total_friend,$sex,$friends);
  194. }
  195. }
  196. public static function get_GetParameters() {
  197. global $_GET;
  198. $parameters = "";
  199. if($_GET){
  200. foreach($_GET as $key => $val){
  201. $parameters .= "$key:$val|";
  202. }
  203. }
  204. if(strlen($parameters) > 1 )
  205. $parameters = substr($parameters,0,-1);
  206. return $parameters;
  207. }
  208. public static function getRandomStr( $length = 8 )
  209. {
  210. // character dictionary
  211. $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
  212. 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
  213. 't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
  214. 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
  215. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
  216. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  217. // get keys
  218. $max = count($chars)-1;
  219. for ($i = 0; $i < $length; $i++){
  220. $keys[] = mt_rand(0,$max);
  221. }
  222. $password = '';
  223. for($i = 0; $i < $length; $i++)
  224. {
  225. $password .= $chars[$keys[$i]];
  226. }
  227. return $password;
  228. }
  229. public static function getRandomNum( $length = 6 )
  230. {
  231. // character dictionary
  232. $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  233. // get keys
  234. $max = count($chars)-1;
  235. for ($i = 0; $i < $length; $i++){
  236. $keys[] = mt_rand(0,$max);
  237. }
  238. $password = '';
  239. for($i = 0; $i < $length; $i++)
  240. {
  241. $password .= $chars[$keys[$i]];
  242. }
  243. return $password;
  244. }
  245. public static function getUrlHost($url){
  246. $host_end_pos = strpos($url,'/',7);
  247. if(!empty($host_end_pos)){
  248. $hostUrl = substr($url,0,$host_end_pos);
  249. }
  250. else{
  251. $hostUrl = $url;
  252. }
  253. return $hostUrl;
  254. }
  255. public static function handlePageRefer($logFile_pre){
  256. // Save refer log
  257. $refer = $_SERVER['HTTP_REFERER'];
  258. if(!empty($refer) && strpos($refer,'oasgames.com') === FALSE){
  259. $refer = commonFunc::getUrlHost($refer);
  260. Log::save_run_log($refer,$logFile_pre);
  261. // set cookie
  262. setcookie("oas_lp_refer",$refer,0,'/','.oasgames.com');
  263. }
  264. }
  265. public static function sqlInjectCheck($input){
  266. // $sqlKeyWords = array(
  267. // 'select','insert','update',
  268. // 'delete','drop','create',
  269. // 'truncate','database',
  270. // 'table','column','add',
  271. // 'load_file','grant','privileges',
  272. // ' ',
  273. // );
  274. //
  275. // $input = strtolower($input);
  276. //
  277. // foreach ($sqlKeyWords as $kw){
  278. // if(strpos($input, $kw) !== FALSE){
  279. // return true;
  280. // }
  281. // }
  282. return false;
  283. }
  284. // Desc: Method to make request at server side
  285. // Para:
  286. // @$default_ip can be set with the ip of the url host
  287. //
  288. // Implemented by Huchunmei at 2013.2.27
  289. public static function curlMakeRequest($url, $params,$timeout=3,$isPostMethod=true,$default_ip='') {
  290. $curl = curl_init();
  291. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  292. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
  293. curl_setopt($curl, CURLOPT_URL, $url);
  294. curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 0);
  295. curl_setopt($curl, CURLOPT_POST, $isPostMethod);
  296. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  297. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  298. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
  299. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  300. if(!empty($default_ip))
  301. curl_setopt($curl, CURLOPT_PROXY,$default_ip);
  302. $output = curl_exec($curl);
  303. $httpinfo = curl_getinfo($curl);
  304. curl_close($curl);
  305. if($httpinfo['http_code'] == 200){
  306. return $output;
  307. } else {
  308. return false;
  309. }
  310. }
  311. public static function curlRequest($url,$postData='',$timeout=3){
  312. $curl = curl_init();
  313. curl_setopt($curl, CURLOPT_URL, $url);
  314. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  315. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
  316. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  317. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  318. if(!empty($postData)){
  319. curl_setopt($curl, CURLOPT_POST, 1);
  320. curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
  321. }
  322. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  323. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  324. $resultData = curl_exec($curl);
  325. $httpInfo = curl_getinfo($curl);
  326. if (curl_errno($curl)) {
  327. curl_close($curl);
  328. return false;
  329. }else{
  330. curl_close($curl);
  331. return $resultData;
  332. }
  333. }
  334. // Class end
  335. }
  336. ?>