Przeglądaj źródła

修正IP地址范围问题、支持获取CloudFlare等真实用户IP

magicblack 4 lat temu
rodzic
commit
c750324508

+ 130 - 10
application/common.php

@@ -914,6 +914,57 @@ function mac_get_tag($title,$content){
     return false;
 }
 
+function mac_get_client_ip()
+{
+    static $final;
+    if (!is_null($final)) {
+        return $final;
+    }
+    $ips = array();
+    if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
+        $ips[] = $_SERVER['HTTP_CF_CONNECTING_IP'];
+    }
+    if (!empty($_SERVER['HTTP_ALI_CDN_REAL_IP'])) {
+        $ips[] = $_SERVER['HTTP_ALI_CDN_REAL_IP'];
+    }
+    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
+        $ips[] = $_SERVER['HTTP_CLIENT_IP'];
+    }
+    if (!empty($_SERVER['HTTP_PROXY_USER'])) {
+        $ips[] = $_SERVER['HTTP_PROXY_USER'];
+    }
+    $real_ip = getenv('HTTP_X_REAL_IP');
+    if (!empty($real_ip)) {
+        $ips[] = $real_ip;
+    }
+    if (!empty($_SERVER['REMOTE_ADDR'])) {
+        $ips[] = $_SERVER['REMOTE_ADDR'];
+    }
+    // 选第一个最合法的,或最后一个正常的IP
+    foreach ($ips as $ip) {
+        $long = ip2long($ip);
+        $long && $final = $ip;
+        // 排除不正确的或私有IP
+        if ($long > 0 && $ip_long < 0xFFFFFFFF) {
+            $final = long2ip($long);
+            break;
+        }
+    }
+    empty($final) && $final = '0.0.0.0';
+    return $final;
+}
+
+function mac_get_ip_long($ip_addr = '')
+{
+    $ip_addr = !empty($ip_addr) ? $ip_addr : mac_get_client_ip();
+    $ip_long = sprintf('%u',ip2long($ip_addr));
+    // 排除不正确的或私有IP
+    if ($ip_long < 0 || $ip_long >= 0xFFFFFFFF) {
+        $ip_long = 0;
+    }
+    return $ip_long;
+}
+
 function mac_get_uniqid_code($code_prefix='')
 {
     $code_prefix = strtoupper($code_prefix);
@@ -970,18 +1021,54 @@ function mac_unescape($str)
 /*特殊字段的值转换*/
 function mac_get_mid_code($data)
 {
-    $arr = [1=>'vod',2=>'art',3=>'topic',4=>'comment',5=>'gbook',6=>'user',7=>'label',8=>'actor',9=>'role',10=>'plot',11=>'website'];
+    $arr = [
+        1  => 'vod',
+        2  => 'art',
+        3  => 'topic',
+        4  => 'comment',
+        5  => 'gbook',
+        6  => 'user',
+        7  => 'label',
+        8  => 'actor',
+        9  => 'role',
+        10 => 'plot',
+        11 => 'website',
+    ];
     return $arr[$data];
 }
 function mac_get_mid_text($data)
 {
-    $arr = [1=>lang('vod'),2=>lang('art'),3=>lang('topic'),4=>lang('comment'),5=>lang('gbook'),6=>lang('user'),7=>lang('label'),8=>lang('actor'),9=>lang('role'),10=>lang('plot'),11=>lang('website')];
+    $arr = [
+        1  => lang('vod'),
+        2  => lang('art'),
+        3  => lang('topic'),
+        4  => lang('comment'),
+        5  => lang('gbook'),
+        6  => lang('user'),
+        7  => lang('label'),
+        8  => lang('actor'),
+        9  => lang('role'),
+        10 => lang('plot'),
+        11 => lang('website'),
+    ];
     return $arr[$data];
 }
 function mac_get_mid($controller)
 {
     $controller=strtolower($controller);
-    $arr = ['vod'=>1,'art'=>2,'topic'=>3,'comment'=>4,'gbook'=>5,'user'=>6,'label'=>7,'actor'=>8,'role'=>9,'plot'=>10,'website'=>11];
+    $arr = [
+        'vod'     => 1,
+        'art'     => 2,
+        'topic'   => 3,
+        'comment' => 4,
+        'gbook'   => 5,
+        'user'    => 6,
+        'label'   => 7,
+        'actor'   => 8,
+        'role'    => 9,
+        'plot'    => 10,
+        'website' => 11,
+    ];
     return $arr[$controller];
 }
 function mac_get_aid($controller,$action='')
@@ -1010,44 +1097,77 @@ function mac_get_aid($controller,$action='')
 
 function mac_get_user_status_text($data)
 {
-    $arr = [0=>lang('disable'),1=>lang('enable')];
+    $arr = [
+        0 => lang('disable'),
+        1 => lang('enable'),
+    ];
     return $arr[$data];
 }
 function mac_get_user_flag_text($data)
 {
-    $arr = [0=>lang('counting_points'),1=>lang('counting_times'),2=>lang('counting_ips')];
+    $arr = [
+        0 => lang('counting_points'),
+        1 => lang('counting_times'),
+        2 => lang('counting_ips'),
+    ];
     return $arr[$data];
 }
 
 function mac_get_ulog_type_text($data)
 {
-    $arr = [1=>lang('browse'),2=>lang('collect'),3=>lang('want_see'),4=>lang('play'),5=>lang('down')];
+    $arr = [
+        1 => lang('browse'),
+        2 => lang('collect'),
+        3 => lang('want_see'),
+        4 => lang('play'),
+        5 => lang('down'),
+    ];
     return $arr[$data];
 }
 
 function mac_get_plog_type_text($data)
 {
-    $arr = [1=>lang('integral_recharge'),2=>lang('registration_promotion'),3=>lang('visit_promotion'),4=>lang('one_level_distribution'),5=>lang('two_level_distribution'),6=>lang('three_level_distribution'),7=>lang('points_upgrade'),8=>lang('integral_consumption'),9=>lang('integral_withdrawal')];
+    $arr = [
+        1 => lang('integral_recharge'),
+        2 => lang('registration_promotion'),
+        3 => lang('visit_promotion'),
+        4 => lang('one_level_distribution'),
+        5 => lang('two_level_distribution'),
+        6 => lang('three_level_distribution'),
+        7 => lang('points_upgrade'),
+        8 => lang('integral_consumption'),
+        9 => lang('integral_withdrawal'),
+    ];
     return $arr[$data];
 }
 
 function mac_get_card_sale_status_text($data)
 {
-    $arr = [0=>lang('not_sale'),1=>lang('sold')];
+    $arr = [
+        0 => lang('not_sale'),
+        1 => lang('sold'),
+    ];
     return $arr[$data];
 }
 
 function mac_get_card_use_status_text($data)
 {
-    $arr = [0=>lang('not_used'),1=>lang('used')];
+    $arr = [
+        0 => lang('not_used'),
+        1 => lang('used'),
+    ];
     return $arr[$data];
 }
 
 function mac_get_order_status_text($data)
 {
-    $arr = [0=>lang('not_paid'),1=>lang('paid')];
+    $arr = [
+        0 => lang('not_paid'),
+        1 => lang('paid'),
+    ];
     return $arr[$data];
 }
+
 function mac_get_user_portrait($user_id)
 {
     $res = MAC_PATH . 'static/images/touxiang.png';

+ 1 - 1
application/common/extend/pay/Weixin.php

@@ -17,7 +17,7 @@ class Weixin {
         $data['fee_type'] =  'CNY';//标价币种
         $data['out_trade_no'] = $order['order_code'];//商户订单号
         $data['total_fee'] = $total_fee*100;//金额,单位分
-        $data['spbill_create_ip'] =  request()->ip();//终端IP
+        $data['spbill_create_ip'] =  mac_get_client_ip();//终端IP
         $data['notify_url'] =  $GLOBALS['http_type'] . $_SERVER['HTTP_HOST'] . '/index.php/payment/notify/pay_type/weixin';
         $data['trade_type'] =  'NATIVE';//交易类型 JSAPI,NATIVE,APP
         $data['product_id'] = '1';//商品ID

+ 3 - 7
application/common/model/Admin.php

@@ -130,11 +130,7 @@ class Admin extends Base {
             return ['code'=>1003,'msg'=>lang('access_or_pass_err')];
         }
         $random = md5(rand(10000000,99999999));
-        $ip = sprintf('%u',ip2long(request()->ip()));
-        if($ip>2147483647){
-            $ip=0;
-        }
-        $update['admin_login_ip'] = $ip;
+        $update['admin_login_ip'] = mac_get_ip_long();
         $update['admin_login_time'] = time();
         $update['admin_login_num'] = $row['admin_login_num'] + 1;
         $update['admin_random'] = $random;
@@ -151,7 +147,7 @@ class Admin extends Base {
 
         //cookie('admin_id',$row['admin_id']);
         //cookie('admin_name',$row['admin_name']);
-        //cookie('admin_check',md5($random .'-'. $row['admin_name'] .'-'.$row['admin_id'] .'-'.request()->ip() ) );
+        //cookie('admin_check',md5($random .'-'. $row['admin_name'] .'-'.$row['admin_id'] .'-'.mac_get_client_ip() ) );
 
         return ['code'=>1,'msg'=>lang('model/admin/login_ok')];
     }
@@ -200,7 +196,7 @@ class Admin extends Base {
         }
         $info = $info->toArray();
 
-        $login_check = md5($info['admin_random'] .'-'. $info['admin_name'] .'-'.$info['admin_id'] .'-'.request()->ip() ) ;
+        $login_check = md5($info['admin_random'] .'-'. $info['admin_name'] .'-'.$info['admin_id'] .'-'.mac_get_client_ip() ) ;
         if($login_check != $admin_check){
             return ['code'=>1003,'msg'=>lang('model/admin/not_login')];
         }

+ 1 - 1
application/common/model/Collect.php

@@ -2241,7 +2241,7 @@ class Collect extends Base {
                 $v['comment_down'] = intval($v['comment_down']);
                 $v['comment_mid'] = intval($v['comment_mid']);
                 if(!empty($v['comment_ip']) && !is_numeric($v['comment_ip'])){
-                    $v['comment_ip'] = ip2long($v['comment_ip']);
+                    $v['comment_ip'] = mac_get_ip_long($v['comment_ip']);
                 }
 
                 if($config['updown_start']>0 && $config['updown_end']){

+ 3 - 19
application/common/model/User.php

@@ -161,13 +161,7 @@ class User extends Base
             }
         }
 
-        $ip = sprintf('%u',ip2long(request()->ip()));
-        if($ip>2147483647){
-            $ip=0;
-        }
-
-
-
+        $ip = mac_get_ip_long();
         if( $GLOBALS['config']['user']['reg_num'] > 0){
             $where2=[];
             $where2['user_reg_ip'] = ['eq', $ip];
@@ -178,8 +172,6 @@ class User extends Base
             }
         }
 
-
-
         $fields = [];
         $fields['user_name'] = $data['user_name'];
         $fields['user_pwd'] = md5($data['user_pwd']);
@@ -372,12 +364,8 @@ class User extends Base
         }
 
         $random = md5(rand(10000000, 99999999));
-        $ip = sprintf('%u',ip2long(request()->ip()));
-        if($ip>2147483647){
-            $ip=0;
-        }
         $update['user_random'] = $random;
-        $update['user_login_ip'] = $ip;
+        $update['user_login_ip'] = mac_get_ip_long();
         $update['user_login_time'] = time();
         $update['user_login_num'] = $row['user_login_num'] + 1;
         $update['user_last_login_time'] = $row['user_login_time'];
@@ -848,11 +836,7 @@ class User extends Base
             return ['code' => 101, 'msg' =>lang('model/user/id_err')];
         }
 
-        $ip = sprintf('%u', ip2long(request()->ip()));
-        if ($ip > 2147483647) {
-            $ip = 0;
-        }
-
+        $ip = mac_get_ip_long();
         $max_cc = $GLOBALS['config']['user']['invite_visit_num'];
         if(empty($max_cc)){
             $max_cc=1;

+ 1 - 5
application/common/model/Website.php

@@ -587,11 +587,7 @@ class Website extends Base {
 
     public function visit($param)
     {
-        $ip = sprintf('%u', ip2long(request()->ip()));
-        if ($ip > 2147483647) {
-            $ip = 0;
-        }
-
+        $ip = mac_get_ip_long();
         $max_cc = $GLOBALS['config']['website']['refer_visit_num'];
         if(empty($max_cc)){
             $max_cc=1;

+ 8 - 12
application/index/controller/Comment.php

@@ -26,15 +26,15 @@ class Comment extends Base
 
         return $this->label_fetch('comment/index');
     }
-	
-	public function ajax() {
-		$param = mac_param_url();
+    
+    public function ajax() {
+        $param = mac_param_url();
         $this->assign('param',$param);
         $this->assign('comment',$GLOBALS['config']['comment']);
         return $this->label_fetch('comment/ajax',0,'json');
-	}
+    }
 
-	public function saveData() {
+    public function saveData() {
         $param = input();
 
         if($GLOBALS['config']['comment']['verify'] == 1){
@@ -86,13 +86,9 @@ class Comment extends Base
             $param['comment_status'] = 0;
         }
 
-        $ip = sprintf('%u',ip2long(request()->ip()));
-        if($ip>2147483647){
-            $ip=0;
-        }
-        $param['comment_ip'] = $ip;
+        $param['comment_ip'] = mac_get_ip_long();
 
-		$res = model('Comment')->saveData($param);
+        $res = model('Comment')->saveData($param);
         if($res['code']>1){
             return $res;
         }
@@ -106,7 +102,7 @@ class Comment extends Base
             }
             return $res;
         }
-	}
+    }
 
     public function report()
     {

+ 1 - 5
application/index/controller/Gbook.php

@@ -91,11 +91,7 @@ class Gbook extends Base
             $param['gbook_status'] = 0;
         }
 
-        $ip = sprintf('%u',ip2long(request()->ip()));
-        if($ip>2147483647){
-            $ip=0;
-        }
-        $param['gbook_ip'] = $ip;
+        $param['gbook_ip'] = mac_get_ip_long();
 
         $res = model('Gbook')->saveData($param);