class.functions.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. <?php
  2. class functions{
  3. public $config;
  4. private $runStart;
  5. private $isPhpVersion;
  6. public function __construct( ){
  7. global $config;
  8. $this->config = $config;
  9. $this->runStart = $this->time( true);
  10. }
  11. /**
  12. * 设置COOKIE
  13. * @param unknown_type $name
  14. * @param unknown_type $value
  15. * @param unknown_type $time 过期时间,�?则关闭浏览器失效getFlashVars
  16. */
  17. public function setCookie($name, $value, $time=0){
  18. $expires = $time ? $this->time()+(int)$time : 0;
  19. setcookie( $name, $value, $expires, '/');
  20. }
  21. public function header(){
  22. header( "Content-Type:text/html; charset=utf-8" );
  23. }
  24. public function nocache(){
  25. header("Pragma:no-cache");
  26. header("Cache-Type:no-cache, must-revalidate");
  27. header("Expires: -1");
  28. }
  29. public function dp3p(){
  30. header("P3P:CP='ALL DSP CURa ADMa DEVa CONi OUT DELa IND PHY ONL PUR COM NAV DEM CNT STA PRE'");
  31. }
  32. /**
  33. * 获取IP
  34. * @return IP
  35. */
  36. function getip() {
  37. if ($_SERVER ["HTTP_X_FORWARDED_FOR"])
  38. $ip = $_SERVER ["HTTP_X_FORWARDED_FOR"];
  39. else if ($_SERVER ["HTTP_CLIENT_IP"])
  40. $ip = $_SERVER ["HTTP_CLIENT_IP"];
  41. else if ($_SERVER ["REMOTE_ADDR"])
  42. $ip = $_SERVER ["REMOTE_ADDR"];
  43. else if (getenv ( "HTTP_X_FORWARDED_FOR" ))
  44. $ip = getenv ( "HTTP_X_FORWARDED_FOR" );
  45. else if (getenv ( "HTTP_CLIENT_IP" ))
  46. $ip = getenv ( "HTTP_CLIENT_IP" );
  47. else if (getenv ( "REMOTE_ADDR" ))
  48. $ip = getenv ( "REMOTE_ADDR" );
  49. else
  50. $ip = "Unknown";
  51. return $ip;
  52. }
  53. /**
  54. * 返回浏览器信�?ver为版本号,nav为浏览器�? */
  55. function getbrowser(){
  56. $browsers = "mozilla msie gecko firefox ";
  57. $browsers.= "konqueror safari netscape navigator ";
  58. $browsers.= "opera mosaic lynx amaya omniweb";
  59. $browsers = split(" ", $browsers);
  60. $nua = strToLower( $_SERVER['HTTP_USER_AGENT']);
  61. $l = strlen($nua);
  62. for ($i=0; $i<count($browsers); $i++){
  63. $browser = $browsers[$i];
  64. $n = stristr($nua, $browser);
  65. if(strlen($n)>0){
  66. $arrInfo["ver"] = "";
  67. $arrInfo["nav"] = $browser;
  68. $j=strpos($nua, $arrInfo["nav"])+$n+strlen($arrInfo["nav"])+1;
  69. for (; $j<=$l; $j++){
  70. $s = substr ($nua, $j, 1);
  71. if(is_numeric($arrInfo["ver"].$s) )
  72. $arrInfo["ver"] .= $s;
  73. else
  74. break;
  75. }
  76. }
  77. }
  78. return $arrInfo;
  79. }
  80. public function magic_quote( $mixVar){
  81. if( get_magic_quotes_gpc()){
  82. if(is_array( $mixVar)){
  83. foreach ( $mixVar as $key => $value){
  84. $temp[$key] = $this->magic_quote( $value);
  85. }
  86. }else{
  87. $temp = stripslashes( $mixVar);
  88. }
  89. return $temp;
  90. }else{
  91. return $mixVar;
  92. }
  93. }
  94. public function stripslashes_deep($value)
  95. {
  96. $value = is_array($value) ?
  97. array_map('stripslashes_deep', $value) :
  98. stripslashes($value);
  99. return $value;
  100. }
  101. /**
  102. * 数组分页
  103. * @param unknown_type $array
  104. * @param unknown_type $num 每页显示个数
  105. * @param unknown_type $now 当前页码,0开�? * @param unknown_type $url 除去p=后的url
  106. * @return unknown
  107. */
  108. function apart_page( $array, $num, $now, $url){
  109. $count = count( $array);
  110. $now = min($now, floor($count/$num));
  111. if($count < $num){
  112. return array($array, '');
  113. }else{
  114. if($now!=0){
  115. $str .= '<a href="'.$url.'">|&lt;</a> <a href="'.$url.'?p='.($now-1).'">&lt;</a> ';
  116. }
  117. $str .= $num*$now+1;
  118. $str .= "~";
  119. $str .= ($num*$now)+$num;
  120. if($now!=floor($count/$num)){
  121. $str .= '<a href="'.$url.'?p='.($now+1).'">&gt;</a> <a href="'.$url.'?p='.floor($count/$num).'">&gt;|</a> ';
  122. }
  123. return array(array_slice($array, $num*$now, $num), $str);
  124. }
  125. }
  126. /**
  127. * �?arr的长和宽等比例缩小至$arrTo resize(array($array['width'],$array['height']), array(160,120))
  128. * @return unknown
  129. */
  130. function resize($arr, $arrTo ){
  131. $arr[0] = $arr[0]>10 ? $arr[0] : $arrTo[0];
  132. $arr[1] = $arr[1]>10 ? $arr[1] : $arrTo[1];
  133. $arrTo[0] = $arrTo[0]<=0 ? 160 : $arrTo[0];
  134. $arrTo[1] = $arrTo[1]<=0 ? 120 : $arrTo[1];
  135. $temp = $arr;
  136. //如果宽度超出
  137. if( $arr[0] > $arrTo[0]){
  138. $temp[0] = $arrTo[0];
  139. $temp[1] = (int)($temp[0]*$arr[1]/$arr[0]);
  140. if( $temp[1] > $arrTo[1]){
  141. $temp[1] = $arrTo[1];
  142. $temp[0] = (int)($arr[0]*$temp[1]/$arr[1]);
  143. }
  144. }
  145. //如果高度超出
  146. if( $arr[1] > $arrTo[1] ){
  147. $temp[1] = $arrTo[1];
  148. $temp[0] = (int)($arr[0]*$temp[1]/$arr[1]);
  149. if( $temp[0] > $arrTo[0]){
  150. $temp[0] = $arrTo[0];
  151. $temp[1] = (int)($temp[0]*$arr[1]/$arr[0]);
  152. }
  153. }
  154. return $temp;
  155. }
  156. /**
  157. * 返回UNIX时间�? * @param boolen $float 是否精确到微�? * @return int/float
  158. */
  159. public function time( $float=false){
  160. return $float ? microtime( true) : time();
  161. }
  162. public function runTime(){
  163. return $this->time( true) - $this->runStart;
  164. }
  165. /**
  166. * @param int $uid
  167. * @return string
  168. */
  169. function get_avatar($mid, $size=0) {
  170. $size = in_array($size, array(0, 1, 2)) ? $size : 1;
  171. $mid = abs(intval($mid));
  172. $mid = sprintf("%09d", $mid);
  173. $dir1 = substr($mid, 0, 3);
  174. $dir2 = substr($mid, 3, 2);
  175. $dir3 = substr($mid, 5, 2);
  176. return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($mid, -2)."$size.jpg";
  177. }
  178. //获取个人主页
  179. function get_site($sid, $sitemid, $url=''){
  180. if( $sid > 99){ //漫游个人主页地址
  181. return 'http://uchome.manyou.com/profile/' . $sitemid;
  182. }elseif ( $sid==19){ //雅虎
  183. return 'http://guanxi.koubei.com/showprofile.php?uid=0' . $sitemid;
  184. }elseif ( $sid==1){ //校内
  185. return 'http://xiaonei.com/profile.do?id=' . $sitemid;
  186. }
  187. return 'http://uchome.manyou.com/profile/' . $sitemid;
  188. }
  189. /**
  190. * 制作flash参数
  191. */
  192. function getFlashVars( $aLoad) {
  193. $aVars['uid'] = $aLoad['uid'];
  194. $aVars['sid'] = $aLoad['sid'];
  195. $aVars['sesskey'] = $aLoad['sesskey'];
  196. $aVars['name'] = $aLoad['name'];
  197. $aVars['gateway'] = $aLoad['gateway'];
  198. $aVars['flashUrl'] = $aLoad['flashUrl'];
  199. $aVars['dataUrl'] = $aLoad['dataUrl'];
  200. $aVars['imageUrl'] = $aLoad['imageUrl'];
  201. $aVars['webroot'] = $aLoad['webroot'];
  202. $aVars['isFirst'] = $aLoad['isFirst'];
  203. $aVars['flashver'] = json_encode($aLoad['flashver']);
  204. $aVars['hallip'] = json_encode($aLoad['hallip']);
  205. $aVars['other'] = json_encode($aLoad['other']);
  206. $aVars['time'] = time();
  207. $aVars['isFans'] = $aLoad['isFans'];
  208. $aVars['isCreate'] = $aLoad['isCreate'];
  209. $aVars['isPayUrl'] = $aLoad['isPayUrl'];
  210. $aVars['ispay'] = $aLoad['ispay'];
  211. $aVars['img'] = $aLoad['img'];
  212. $aVars['money'] = $aLoad['money'];
  213. $aVars['mtkey'] = $aLoad['mtkey'];
  214. $aVars['skey'] = $aLoad['skey'];
  215. foreach ( $aVars as $key => $value){
  216. $flashvars .= $key . '=' . urlencode($value) . '&';
  217. }
  218. return substr( $flashvars, 0, -1);
  219. }
  220. public function __destruct(){
  221. }
  222. public function writeErrorLog($msg){
  223. echo PATH_LOG;die;
  224. }
  225. static function uint( $num){
  226. return max(0, (int)$num);
  227. }
  228. /**
  229. * 安全性检测.调用escape存入的,一定要调unescape取出
  230. */
  231. static function escape( $string){
  232. if ( oo::functions()->isPhpVersion()) {
  233. return addslashes( trim($string));
  234. }
  235. return mysql_escape_string( trim($string));
  236. }
  237. /**
  238. * 判断当前字符串是否为指定格式
  239. * @param string $str 需要判断的字符串
  240. * @param string $format 日期格式
  241. */
  242. static function checkDateTimeFormat($str,$format='Y-m-d'){
  243. $unixTime=strtotime($str);
  244. $checkDate= date($format, $unixTime);
  245. if($checkDate==$str){
  246. return true;
  247. }else{
  248. return false;
  249. }
  250. }
  251. /**
  252. * 判断当前环境php版本是否大于大于等于指定的一个版本
  253. * @param sreing $version default=5.3.0
  254. * @return boolean 大于true,小于false
  255. */
  256. public function isPhpVersion( $version = '5.3.0' ) {
  257. if ( $this->isPhpVersion) {
  258. $php_version = $this->isPhpVersion;
  259. } else {
  260. $php_version = explode( '-', phpversion() );
  261. }
  262. $is_pass = strnatcasecmp( $php_version[0], $version ) >= 0 ? true : false;
  263. return $is_pass;
  264. }
  265. /**
  266. * 将字符串ID转换为对应的数字ID 不可逆 转换后不唯一
  267. */
  268. public function midToNumber( $mid){
  269. return is_numeric($mid) ? $mid : abs( crc32($mid));
  270. }
  271. /**
  272. * 把数组序列成Server识别的.有缺陷,不能是null类型的
  273. * @param Array $array
  274. */
  275. public static function serialize( $array ){
  276. return str_replace( '=', ':', http_build_query( $array, null, ',' ) );
  277. }
  278. /**
  279. * 把字符串反序列成索引数组
  280. * @param String $string
  281. */
  282. public static function unserialize( $string ){
  283. parse_str( str_replace( array( ':', ',' ), array( '=', '&' ), $string ), $array );
  284. return (array) $array;
  285. }
  286. /**
  287. * 获取网页数据
  288. * @param string $url
  289. * @param array $post_data post的数据,为空时表示get请求
  290. * @param string $json 返回数据格式,0表示json 1原数据返回
  291. * @return array/int
  292. */
  293. public static function curl( $url, $post_data=array(), $json=1, $timeout=20, $headers = [])
  294. {
  295. $ch = curl_init();
  296. curl_setopt($ch, CURLOPT_URL, $url);
  297. curl_setopt($ch, CURLOPT_HEADER, 0);
  298. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  299. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  300. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  301. if(!empty($headers)){
  302. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  303. }
  304. if(!empty($post_data))
  305. {
  306. curl_setopt($ch, CURLOPT_POST, true);
  307. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  308. }
  309. $result = curl_exec ( $ch );
  310. curl_close($ch);
  311. return $data = empty($json) ? $result : json_decode($result, true);
  312. }
  313. /**
  314. * 返回
  315. * @param unknown $array
  316. */
  317. public function sendData( $array, $stime){
  318. $etime = oo::functions()->time( true);
  319. $aRet = array();
  320. $aRet['code'] = $array['code'];
  321. $aRet['codemsg'] = $array['codemsg'];
  322. $aRet['sid'] = oo::$config['sid'];
  323. $aRet['data'] = $array['data'];
  324. $aRet['time'] = time();
  325. $aRet['exetime'] = $etime - $stime; //脚本执行时间
  326. // if (PRODUCTION_SERVER) {
  327. // echo gzcompress( json_encode( $aRet), 9);
  328. // } else {
  329. echo json_encode( $aRet);
  330. // }
  331. // echo json_encode( $aRet);
  332. // exit();
  333. }
  334. /**
  335. *
  336. * @param unknown $string
  337. * @return boolean|number
  338. */
  339. public static function phutil_is_utf8($string) {
  340. if (function_exists('mb_check_encoding')) {
  341. return mb_check_encoding($string, 'UTF-8');
  342. }
  343. $regex =
  344. "/^(".
  345. "[\x01-\x7F]+".
  346. "|([\xC2-\xDF][\x80-\xBF])".
  347. "|([\xE0-\xEF][\x80-\xBF][\x80-\xBF])".
  348. "|([\xF0-\xF4][\x80-\xBF][\x80-\xBF][\x80-\xBF]))*\$/";
  349. return preg_match($regex, $string);
  350. }
  351. /**
  352. *
  353. * @param unknown $string
  354. * @return unknown|string
  355. */
  356. public function phutil_utf8ize($string) {
  357. if ($this->phutil_is_utf8($string)) {
  358. return $string;
  359. }
  360. $result = array();
  361. $regex =
  362. "/([\x01-\x7F]".
  363. "|[\xC2-\xDF][\x80-\xBF]".
  364. "|[\xE0-\xEF][\x80-\xBF][\x80-\xBF]".
  365. "|[\xF0-\xF4][\x80-\xBF][\x80-\xBF][\x80-\xBF])".
  366. "|(.)/";
  367. $offset = 0;
  368. $matches = null;
  369. while (preg_match($regex, $string, $matches, 0, $offset)) {
  370. if (!isset($matches[2])) {
  371. $result[] = $matches[1];
  372. } else {
  373. $result[] = "\xEF\xBF\xBD";
  374. }
  375. $offset += strlen($matches[0]);
  376. }
  377. return implode('', $result);
  378. }
  379. /**
  380. * 验证是否有定义这个全局变量并且值是否为true
  381. * @param unknown $constName
  382. * @return mixed|boolean
  383. */
  384. public function flag($constName)
  385. {
  386. if (defined($constName))
  387. {
  388. return constant($constName);
  389. }
  390. return false;
  391. }
  392. /**
  393. * 客户端版本对比
  394. *
  395. * @param string $version1 版本号1,格式: 1.0.1
  396. * @param string $version2 版本号2
  397. * @return int 比较结果: 1>2结果大于0 1==2结果等于0 1<2结果小于0
  398. */
  399. public static function version_compare( $version1, $version2 ){
  400. $version1_arr = explode( '.', (string) $version1, 3 );
  401. $version2_arr = explode( '.', (string) $version2, 3 );
  402. $version1_int = intval( $version1_arr[0] ) * 1000000 + intval( $version1_arr[1] ) * 1000 + intval( $version1_arr[2] );
  403. $version2_int = intval( $version2_arr[0] ) * 1000000 + intval( $version2_arr[1] ) * 1000 + intval( $version2_arr[2] );
  404. return ($version1_int - $version2_int);
  405. }
  406. /**
  407. * 验证ip地址
  408. * @param string $ip, ip地址
  409. * @return bool 正确返回true, 否则返回false
  410. */
  411. public static function checkIP( $ip ){
  412. $ip = trim( $ip );
  413. $pt = '/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/';
  414. if( preg_match( $pt, $ip ) === 1 ){
  415. return true;
  416. }
  417. return false;
  418. }
  419. /**
  420. * @desc 检查来源IP是否属于boyaa公司网段
  421. * @param String $ip IP地址
  422. * @return Boolean 属于则返回true,否则返回false
  423. */
  424. public function isBoyaaIp($ip=0){
  425. $ip = $ip ? $ip : $this->getip();
  426. if(!self::checkIP($ip)) return false;
  427. $cidrs = array(
  428. '127.0.0.1/32', //表示:127.0.0.1
  429. '10.0.0.0/8', //表示:10.0.0.0 ~ 10.255.255.255 A类内网
  430. '172.16.0.0/12', //表示:172.16.0.0 ~ 172.31.255.255 B类内网
  431. '192.168.0.0/16', //表示:192.168.0.0 ~ 192.168.255.255 C类内网
  432. '183.62.196.0/27', //表示:183.62.196.0 ~ 183.62.196.31
  433. '103.29.141.72/30', //表示:103.29.141.72 ~ 103.29.141.75
  434. '210.5.191.162/32', //表示:210.5.191.162
  435. '202.60.204.52/32', //表示:202.60.204.52
  436. '210.213.48.45/32', //表示: 210.213.48.45
  437. '103.251.131.0/29', //新国际线路 14-11-10
  438. '119.81.169.160/27', //新增IP段 15-6-30
  439. '183.62.167.2/32',
  440. '203.160.93.103/32',
  441. '113.105.73.51/32', //cms迁移机房
  442. '202.170.112.6/32', //泰分公司
  443. '112.121.138.66/28', //泰分公司备用IP
  444. '107.178.195.143/28',
  445. //泰分IP调整,整理了下白名单,联系运维发现下属三个位置IP,先注释,没问题后删除 --- 2014.7.1
  446. //'202.153.93.171/32', //表示:202.153.93.171
  447. //'119.81.15.146/32', //表示:119.81.15.146
  448. //'59.152.235.224/27', //表示:59.152.235.224 ~ 59.152.235.255
  449. '175.45.5.253/32', //mf 机器
  450. '58.251.8.8/30',//联通线路
  451. '113.105.73.52/32', //cms迁移机房
  452. '119.81.186.192/28',//新加坡机房
  453. '119.81.149.240/29',
  454. '119.81.15.146/32',
  455. '119.81.87.28/32',
  456. '119.81.92.204/32',
  457. '103.61.193.12/32',
  458. '103.61.193.18/32',
  459. '103.61.193.125/32',
  460. '14.215.134.128/28',//内网服务器新增出口ip
  461. );
  462. foreach( $cidrs as $cidr ){
  463. list( $net, $mask ) = explode ( '/', $cidr );
  464. if( ( ip2long ( $ip ) & ~ ( ( 1 << ( 32 - $mask ) ) - 1) ) === ip2long ( $net ) ){
  465. return true;
  466. }
  467. }
  468. return false;
  469. }
  470. /**
  471. * 是否为国内的IP
  472. * @param string $ip
  473. * @return bool
  474. */
  475. public function isCnIP($ip = 0) {
  476. $ip = $ip ? $ip : $this->getip();
  477. if(!self::checkIP($ip)) return false;
  478. $cidrs = oo::cfg('cniplist');
  479. if (!$cidrs) return false;
  480. foreach( $cidrs as $cidr ){
  481. list( $net, $mask ) = explode ( '/', $cidr );
  482. if( ( ip2long ( $ip ) & ~ ( ( 1 << ( 32 - $mask ) ) - 1) ) === ip2long ( $net ) ){
  483. return true;
  484. }
  485. }
  486. return false;
  487. }
  488. /**
  489. * 是否泰国内的IP
  490. * @param string $ip
  491. * @return bool
  492. */
  493. public function isThIP($ip = 0) {
  494. $ip = $ip ? $ip : $this->getip();
  495. if(!self::checkIP($ip)) return false;
  496. $cidrs = oo::cfg('thiplist');
  497. if (!$cidrs) return false;
  498. foreach( $cidrs as $cidr ){
  499. list( $net, $mask ) = explode ( '/', $cidr );
  500. if( ( ip2long ( $ip ) & ~ ( ( 1 << ( 32 - $mask ) ) - 1) ) === ip2long ( $net ) ){
  501. return true;
  502. }
  503. }
  504. return false;
  505. }
  506. /**
  507. * 通过Ip获取国家信息
  508. * @param null $ip
  509. * @param string $purpose
  510. * @param bool $deep_detect
  511. * @return array|null|string
  512. */
  513. function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
  514. $output = NULL;
  515. if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
  516. $ip = $_SERVER["REMOTE_ADDR"];
  517. if ($deep_detect) {
  518. if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
  519. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  520. if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
  521. $ip = $_SERVER['HTTP_CLIENT_IP'];
  522. }
  523. }
  524. $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
  525. $support = array("country", "countrycode", "state", "region", "city", "location", "address");
  526. $continents = array(
  527. "AF" => "Africa",
  528. "AN" => "Antarctica",
  529. "AS" => "Asia",
  530. "EU" => "Europe",
  531. "OC" => "Australia (Oceania)",
  532. "NA" => "North America",
  533. "SA" => "South America"
  534. );
  535. if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
  536. $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
  537. if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
  538. switch ($purpose) {
  539. case "location":
  540. $output = array(
  541. "city" => @$ipdat->geoplugin_city,
  542. "state" => @$ipdat->geoplugin_regionName,
  543. "country" => @$ipdat->geoplugin_countryName,
  544. "country_code" => @$ipdat->geoplugin_countryCode,
  545. "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
  546. "continent_code" => @$ipdat->geoplugin_continentCode
  547. );
  548. break;
  549. case "address":
  550. $address = array($ipdat->geoplugin_countryName);
  551. if (@strlen($ipdat->geoplugin_regionName) >= 1)
  552. $address[] = $ipdat->geoplugin_regionName;
  553. if (@strlen($ipdat->geoplugin_city) >= 1)
  554. $address[] = $ipdat->geoplugin_city;
  555. $output = implode(", ", array_reverse($address));
  556. break;
  557. case "city":
  558. $output = @$ipdat->geoplugin_city;
  559. break;
  560. case "state":
  561. $output = @$ipdat->geoplugin_regionName;
  562. break;
  563. case "region":
  564. $output = @$ipdat->geoplugin_regionName;
  565. break;
  566. case "country":
  567. $output = @$ipdat->geoplugin_countryName;
  568. break;
  569. case "countrycode":
  570. $output = @$ipdat->geoplugin_countryCode;
  571. break;
  572. }
  573. }
  574. }
  575. return $output;
  576. }
  577. /**
  578. * 连接复杂数组.用于生成验证字串等
  579. * @param Mixed $params
  580. * @param String $key key前缀
  581. * @param Boolean $strip 是否要判断magic_quotes执行去反斜杠操作
  582. * @param Boolean $iskrsort 是否要排序
  583. * @return String
  584. */
  585. public static function joins( $params, $key = null, $strip = false, $iskrsort = true ){
  586. $ret = array( );
  587. if( is_array( $params ) ){
  588. $iskrsort && krsort( $params, SORT_STRING );
  589. foreach( $params as $k => $v ){
  590. if( (!empty( $key )) || ($key === 0) ){
  591. $k = $key . '[' . urlencode( $strip && get_magic_quotes_gpc() ? stripslashes( $k ) : $k ) . ']';
  592. }
  593. if( is_array( $v ) || is_object( $v ) ){
  594. array_push( $ret, self::joins( $v, $k, $strip ) );
  595. }else{
  596. array_push( $ret, $k . '=' . urlencode( $strip && get_magic_quotes_gpc() ? stripslashes( $v ) : $v ) );
  597. }
  598. }
  599. }
  600. return implode( '&', $ret );
  601. }
  602. /**
  603. * 从URL参数中获取有效
  604. * @param Array $param 键值对.如$_GET
  605. * @param int $expire 过期时间如7天内有效 7*24*60*60
  606. * @param String $prefix 前缀.对应生成方法
  607. * @param String $secret 密钥
  608. * @return Array 键值对(如果未通过验证则为空数组)
  609. */
  610. public static function get( $param, $expire = 0, $prefix = 'by_', $secret = '^$_$^' ){
  611. if( !(is_array( $param ) && count( $param )) ){ //没有参数
  612. return array( );
  613. }
  614. foreach( (array) $param as $k => $v ){
  615. if( strpos( $k, $prefix ) === 0 ){
  616. $aParams[$k] = $aReturn[str_replace( $prefix, '', $k )] = $v; //临时存储和需要返回的原始键值对
  617. }
  618. }
  619. $sig = $aParams[$prefix . 'sig'];
  620. unset( $aParams[$prefix . 'sig'] );
  621. if( $sig !== md5( $secret . self::joins( $aParams, null, true ) . $secret ) ){ //签名未通过验证
  622. return array( );
  623. }
  624. if( $expire && (time() - $aParams[$prefix . 'time'] > $expire) ){//有超时设置
  625. return array( );
  626. }
  627. return (array) $aReturn;
  628. }
  629. /**
  630. * 泰语单音节昵称问题
  631. * @param string $nick
  632. * @return string
  633. */
  634. public function escapeNick($nick) {
  635. if (empty($nick)) {
  636. return $nick;
  637. }
  638. $arr = array("ั", "ิ", "ี", "ึ", "ื", "ุ", "ู", "ฺ", "็", "่", "้", "๊", "๋", "์", "ํ", "๎");
  639. $str = str_replace($arr, "", $nick);
  640. if (empty($str)) {
  641. return $nick . '.';
  642. } else {
  643. return $nick;
  644. }
  645. }
  646. /**
  647. * 游戏币数量格式化
  648. * @param int $num
  649. * @return string
  650. */
  651. public function numFormat($num) {
  652. if (!$num || $num < 1000) return $num;
  653. if ($num < 1000000) {
  654. $c = array(1000, 'K');
  655. } elseif ($num < 1000000000) {
  656. $c = array(1000000, 'M');
  657. } else {
  658. $c = array(1000000000, 'B');
  659. }
  660. if ($num % $c[0] == 0) {
  661. $ret = number_format($num / $c[0], -3, ',', ',') . $c[1];
  662. } else {
  663. $tmp = floor($num / $c[0]);
  664. $n = floor(($num % $c[0]) * 10 / $c[0]);
  665. if ($n != 0) {
  666. $ret = sprintf("%s.%s%s", number_format($tmp, -3, ',', ','), $n, $c[1]);
  667. } else {
  668. $ret = number_format($tmp, -3, ',', ',') . $c[1];
  669. }
  670. }
  671. return $ret;
  672. }
  673. /**
  674. * 黑名单,默认是为支付第三方,兑换实物添加
  675. * MikeMei
  676. * @param int $type
  677. * 加type为1是为了防止以后有扩展
  678. */
  679. public function checkBlackIp($type = 1){
  680. $blackIpList = array('58.8.176.68', '124.120.191.139', '104.132.115.99'); //黑名单
  681. $myip = $this->getip();//PHP获取到的IP
  682. $longIp = ip2long($myip);
  683. if( $longIp == -1 || $longIp === FALSE ){ //获取IP错误
  684. return false;
  685. }
  686. $isblackip = false;
  687. foreach( $blackIpList as $ip ){
  688. if( sprintf("%u", $longIp) == sprintf("%u", ip2long($ip)) ){
  689. $isblackip = true;
  690. break;
  691. }
  692. }
  693. return $isblackip;
  694. }
  695. public static function recall_inner()
  696. {
  697. return array(
  698. 'uid',
  699. 'lasttime'
  700. );
  701. }
  702. /**
  703. * 压缩
  704. * $metas 被压缩的key, example: ovalue::table()
  705. * $data 被压缩的数组,example: array('tid'=>1002, 'sb'=>50)
  706. */
  707. public static function compress($metas, $data)
  708. {
  709. $temp = array();
  710. foreach($metas as $index => $name)
  711. {
  712. if (!isset($data[$name]))
  713. {
  714. continue;
  715. }
  716. if ($data[$name] === null)
  717. {
  718. $data[$name] = '';
  719. }
  720. $temp[$index] = $data[$name];
  721. }
  722. return $temp;
  723. }
  724. /**
  725. * 解压
  726. * $metas 被解压的key, example: ovalue::table()
  727. * $data 被解压的数组,example: array(0=>1002, 0=>50)
  728. */
  729. public static function decompress($metas, $data)
  730. {
  731. $temp = array();
  732. foreach($data as $index => $value)
  733. {
  734. if (isset($metas[$index]))
  735. {
  736. $temp[$metas[$index]] = $value;
  737. }
  738. }
  739. return $temp;
  740. }
  741. /**
  742. * 获取server的GameSid
  743. * 方法: redis拿到GameSid后
  744. $gid = 6619292;
  745. $hex = 0x0000FFFF;
  746. $a = $gid & $hex;
  747. echo $a;
  748. */
  749. public static function getServerGameSid($gamesid = 0)
  750. {
  751. if( !$gamesid ){
  752. return 0;
  753. }
  754. $gamesid = intval($gamesid);
  755. $hex = 0x0000FFFF;
  756. return $gamesid & $hex;
  757. }
  758. }