Typecho非插件实现评论显示系统浏览器及IP

Admin
2023-09-28 / 0 评论 / 11 阅读 / 正在检测是否收录...

Typecho非插件实现评论显示系统浏览器及IP

1.将如下代码粘贴在 functions.php 文件种;
/**
 * 浏览器和设备信息
 *
 * @param string $userAgent 用户代理
 * @return string[]
 */
function getBrowsersInfo ($userAgent) {

    $deviceInfo = [
        "system" => "",
        "systemVersion" => "",
        "browser" => "",
        "version" => "",
        "device" => "PC"
    ];


    $match = [
        // 浏览器 - 国外浏览器
        "Safari" => strstr($userAgent, 'Safari') != false ,
        "Chrome" => strstr($userAgent, 'Chrome') != false || strstr($userAgent, 'CriOS') != false ,
        "IE" => strstr($userAgent, 'MSIE') != false || strstr($userAgent, 'Trident') != false ,
        "Edge" => strstr($userAgent, 'Edge') != false || strstr($userAgent, 'Edg/') != false || strstr($userAgent, 'EdgA') != false || strstr($userAgent, 'EdgiOS') != false,
        "Firefox" => strstr($userAgent, 'Firefox') != false || strstr($userAgent, 'FxiOS') != false ,
        "Firefox Focus" => strstr($userAgent, 'Focus') != false,
        "Chromium" => strstr($userAgent,'Chromium') != false,
        "Opera" => strstr($userAgent,'Opera') != false || strstr($userAgent,'OPR') != false,
        "Vivaldi" => strstr($userAgent,'Vivaldi') != false,
        "Yandex" => strstr($userAgent,'YaBrowser') != false,
        "Arora" => strstr($userAgent,'Arora') != false,
        "Lunascape" => strstr($userAgent,'Lunascape') != false,
        "QupZilla" => strstr($userAgent,'QupZilla') != false,
        "Coc Coc" => strstr($userAgent,'coc_coc_browser') != false,
        "Kindle" => strstr($userAgent,'Kindle') != false || strstr($userAgent,'Silk/') != false,
        "Iceweasel" => strstr($userAgent,'Iceweasel') != false,
        "Konqueror" => strstr($userAgent,'Konqueror') != false,
        "Iceape" => strstr($userAgent,'Iceape') != false,
        "SeaMonkey" => strstr($userAgent,'SeaMonkey') != false,
        "Epiphany" => strstr($userAgent,'Epiphany') != false,
        // 浏览器 - 国内浏览器
        "360" => strstr($userAgent,'QihooBrowser') != false || strstr($userAgent,'QHBrowser') != false,
        "360EE" => strstr($userAgent,'360EE') != false,
        "360SE" => strstr($userAgent,'360SE') != false,
        "UC" => strstr($userAgent,'UCBrowser') != false || strstr($userAgent,' UBrowser') != false || strstr($userAgent,'UCWEB') != false,
        "QQBrowser" => strstr($userAgent,'QQBrowser') != false,
        "QQ" => strstr($userAgent,'QQ/') != false,
        "Baidu" => strstr($userAgent,'Baidu') != false || strstr($userAgent,'BIDUBrowser') != false || strstr($userAgent,'baidubrowser') != false || strstr($userAgent,'baiduboxapp') != false || strstr($userAgent,'BaiduHD') != false,
        "Maxthon" => strstr($userAgent,'Maxthon') != false,
        "Sogou" => strstr($userAgent,'MetaSr') != false || strstr($userAgent,'Sogou') != false,
        "Liebao" => strstr($userAgent,'LBBROWSER') != false || strstr($userAgent,'LieBaoFast') != false,
        "2345Explorer" => strstr($userAgent,'2345Explorer') != false || strstr($userAgent,'Mb2345Browser') != false || strstr($userAgent,'2345chrome') != false,
        "115Browser" => strstr($userAgent,'115Browser') != false,
        "TheWorld" => strstr($userAgent,'TheWorld') != false,
        "Quark" => strstr($userAgent,'Quark') != false,
        "Qiyu" => strstr($userAgent,'Qiyu') != false,
        // 浏览器 - 手机厂商
        "XiaoMi" => strstr($userAgent,'MiuiBrowser') != false,
        "Huawei" => strstr($userAgent,'HuaweiBrowser') != false || strstr($userAgent,'HUAWEI/') != false || strstr($userAgent,'HONOR') != false || strstr($userAgent,'HBPC/') != false,
        "Vivo" => strstr($userAgent,'VivoBrowser') != false,
        "OPPO" => strstr($userAgent,'HeyTapBrowser') != false,
        // 浏览器 - 客户端
        "Wechat" => strstr($userAgent,'MicroMessenger') != false,
        "WechatWork" => strstr($userAgent,'wxwork/') != false,
        "Taobao" => strstr($userAgent,'AliApp(TB') != false,
        "Alipay" => strstr($userAgent,'AliApp(AP') != false,
        "Weibo" => strstr($userAgent,'Weibo') != false,
        "Douban" => strstr($userAgent,'com.douban.frodo') != false,
        "Suning" => strstr($userAgent,'SNEBUY-APP') != false,
        "iQiYi" => strstr($userAgent,'IqiyiApp') != false,
        "DingTalk" => strstr($userAgent,'DingTalk') != false,
        "Douyin" => strstr($userAgent,'aweme') != false,
        // 系统或平台
        "Windows" => strstr($userAgent,'Windows') != false,
        "Linux" => strstr($userAgent,'Linux') != false || strstr($userAgent,'X11') != false,
        "Mac OS" => strstr($userAgent,'Macintosh') != false,
        "Android" => strstr($userAgent,'Android') != false || strstr($userAgent,'Adr') != false,
        "HarmonyOS" => strstr($userAgent,'HarmonyOS') != false,
        "Ubuntu" => strstr($userAgent,'Ubuntu') != false,
        "FreeBSD" => strstr($userAgent,'FreeBSD') != false,
        "Debian" => strstr($userAgent,'Debian') != false,
        "Windows Phone" => strstr($userAgent,'IEMobile') != false || strstr($userAgent,'Windows Phone') != false,
        "BlackBerry" => strstr($userAgent,'BlackBerry') != false || strstr($userAgent,'RIM') != false,
        "MeeGo" => strstr($userAgent,'MeeGo') != false,
        "Symbian" => strstr($userAgent,'Symbian') != false,
        "iOS" => strstr($userAgent,'like Mac OS X') != false,
        "Chrome OS" => strstr($userAgent,'CrOS') != false,
        "WebOS" => strstr($userAgent,'hpwOS') != false,
        // 设备
        "Mobile" => strstr($userAgent,'Mobi') != false || strstr($userAgent,'iPh') != false || strstr($userAgent,'480') != false,
        "Tablet" => strstr($userAgent,'Tablet') != false || strstr($userAgent,'Pad') != false || strstr($userAgent,'Nexus 7') != false,
    ];

    // 部分修正 | 因typecho评论数据只存储了ua的信息,所以不能完全进行修正尤其是360相关浏览器
    if ($match['Baidu'] && $match['Opera']) $match['Baidu'] = false;
    if ($match['iOS']) $match['Safari'] = true;

    // 基本信息
    $baseInfo = [
        "browser" => [
            'Safari', 'Chrome', 'Edge', 'IE', 'Firefox', 'Firefox Focus', 'Chromium',
            'Opera', 'Vivaldi', 'Yandex', 'Arora', 'Lunascape','QupZilla', 'Coc Coc',
            'Kindle', 'Iceweasel', 'Konqueror', 'Iceape','SeaMonkey', 'Epiphany', 'XiaoMi',
            'Vivo', 'OPPO', '360', '360SE','360EE', 'UC', 'QQBrowser', 'QQ', 'Huawei', 'Baidu',
            'Maxthon', 'Sogou', 'Liebao', '2345Explorer', '115Browser', 'TheWorld', 'Quark', 'Qiyu',
            'Wechat', 'WechatWork', 'Taobao', 'Alipay', 'Weibo', 'Douban', 'Suning', 'iQiYi', 'DingTalk', 'Douyin'
        ],
        "system" => [
            'Windows', 'Linux', 'Mac OS', 'Android', 'HarmonyOS', 'Ubuntu',
            'FreeBSD', 'Debian', 'iOS', 'Windows Phone', 'BlackBerry', 'MeeGo',
            'Symbian', 'Chrome OS', 'WebOS'
        ],
        "device" => ['Mobile', 'Tablet'],
    ];

    foreach ($baseInfo as $k => $v) {
        foreach ($v as $xv) {
            if ($match[$xv]) $deviceInfo[$k] = $xv;
        }
    }

    // 操作系统版本信息
    $windowsVersion = [
        '10' => "10",
        '6.4' => '10',
        '6.3' => '8.1',
        '6.2' => '8',
        '6.1' => '7',
        '6.0' => 'Vista',
        '5.2' => 'XP',
        '5.1' => 'XP',
        '5.0' => '2000',
    ];
    $wv = pregMatch("/^Mozilla\/\d.0 \(Windows NT ([\d.]+)[;)].*$/", $userAgent);
    $HarmonyOSVersion = [
        10 => "2",
        12 => "3"
    ];
    $systemVersion = [
        "Windows" => $windowsVersion[$wv] ?? $wv,
        "Android" => pregMatch("/^.*Android ([\d.]+);.*$/", $userAgent),
        "HarmonyOS" => $HarmonyOSVersion[pregMatch("/^Mozilla.*Android ([\d.]+)[;)].*$/", $userAgent)] ?? '',
        "iOS" => preg_replace("/_/", '.', pregMatch("/^.*OS ([\d_]+) like.*$/", $userAgent)),
        "Debian" =>  pregMatch("/^.*Debian\/([\d.]+).*$/", $userAgent),
        "Windows Phone" => pregMatch("/^.*Windows Phone( OS)? ([\d.]+);.*$/", $userAgent),
        "Mac OS" => preg_replace("/_/", '.',pregMatch("/^.*Mac OS X ([\d_]+).*$/", $userAgent)),
        "WebOS" => pregMatch("/^.*hpwOS\/([\d.]+);.*$/", $userAgent)
    ];

    if ($systemVersion[$deviceInfo['system']]) {
        $deviceInfo['systemVersion'] = $systemVersion[$deviceInfo['system']];
        if ($deviceInfo['systemVersion'] == $userAgent) $deviceInfo['systemVersion'] = '';
    }

//    if ($deviceInfo['system'] == 'Windows' && $_windowsVersion) $deviceInfo['systemVersion'] = $_windowsVersion;


    // 浏览器版本信息
    $browsers_360SE = [
        108 => '14.0',
        86 => '13.0',
        78 => '12.0',
        69 => '11.0',
        63 => '10.0',
        55 => '9.1',
        45 => '8.1',
        42 => '8.0',
        31 => '7.0',
        21 => '6.3',
    ];
    $browsers_360EE = [
        95 => '21',
        86 => '13.0',
        78 => '12.0',
        69 => '11.0',
        63 => '9.5',
        55 => '9.0',
        50 => '8.7',
        30 => '7.5',
    ];
    $browsers_liebao = [
         57 => '6.5',
        49 => '6.0',
        46 => '5.9',
        42 => '5.3',
        39 => '5.2',
        34 => '5.0',
        29 => '4.5',
        21 => '4.0'
    ];
    $browsers_2345 = [
        69 => '10.0',
        55 => '9.9',
        69 => '10.0',
        55 => '9.9',
        69 => '10.0',
        55 => '9.9'
    ];

    $chromeVersion = pregMatch('/^.*Chrome\/([\d]+).*$/', $userAgent);

    $browsersVersion = [
        "Safari" => pregMatch("/^.*Version\/([\d.]+).*$/", $userAgent),
        "Chrome" => pregMatch("/^.*Chrome\/([\d.]+).*$/", $userAgent) ?? pregMatch("/^.*CriOS\/([\d.]+).*$/", $userAgent),
        "IE" => pregMatch("/^.*MSIE ([\d.]+).*$/", $userAgent) ?? pregMatch("/^.*rv:([\d.]+).*$/", $userAgent),
        "Edge" => pregMatch("/^.*Edge\/([\d.]+).*$/", $userAgent) ?? pregMatch("/^.*Edg\/([\d.]+).*$/", $userAgent) ?? pregMatch("/^.*EdgA\/([\d.]+).*$/", $userAgent) ?? pregMatch("/^.*EdgiOS\/([\d.]+).*$/", $userAgent),
        "Firefox" => pregMatch("/^.*Firefox\/([\d.]+).*$/", $userAgent) ?? pregMatch("/^.*FxiOS\/([\d.]+).*$/", $userAgent),
        "Firefox Focus" => pregMatch("/^.*Focus\/([\d.]+).*$/", $userAgent),
        "Chromium" => pregMatch("/^.*Chromium\/([\d.]+).*$/", $userAgent),
        "Opera" => pregMatch("/^.*Opera\/([\d.]+).*$/", $userAgent) ?? pregMatch("/^.*OPR\/([\d.]+).*$/", $userAgent),
        "Vivaldi" => pregMatch("/^.*Vivaldi\/([\d.]+).*$/", $userAgent),
        "Yandex" => pregMatch("/^.*YaBrowser\/([\d.]+).*$/", $userAgent),
        "Brave" => pregMatch("/^.*Chrome\/([\d.]+).*$/", $userAgent),
        "Arora" => pregMatch("/^.*Arora\/([\d.]+).*$/", $userAgent),
        "Lunascape" => pregMatch("/^.*Lunascape[\/\s]([\d.]+).*$/", $userAgent),
        "QupZilla" => pregMatch("/^.*QupZilla[\/\s]([\d.]+).*$/", $userAgent),
        "Coc Coc" => pregMatch("/^.*coc_coc_browser\/([\d.]+).*$/", $userAgent),
        "Kindle" => pregMatch("/^.*Version\/([\d.]+).*$/", $userAgent),
        "Iceweasel" => pregMatch("/^.*Iceweasel\/([\d.]+).*$/", $userAgent),
        "Konqueror" => pregMatch("/^.*Konqueror\/([\d.]+).*$/", $userAgent),
        "Iceape" => pregMatch("/^.*Iceape\/([\d.]+).*$/", $userAgent),
        "SeaMonkey" => pregMatch("/^.*SeaMonkey\/([\d.]+).*$/", $userAgent),
        "Epiphany" => pregMatch("/^.*Epiphany\/([\d.]+).*$/", $userAgent),
        "360" => pregMatch("/^.*QihooBrowser(HD)?\/([\d.]+).*$/", $userAgent),
        "Maxthon" => pregMatch("/^.*Maxthon\/([\d.]+).*$/", $userAgent),
        "QQBrowser" => pregMatch("/^.*QQBrowser\/([\d.]+).*$/", $userAgent),
        "QQ" => pregMatch("/^.*QQ\/([\d.]+).*$/", $userAgent),
        "Baidu" => pregMatch("/^.*BIDUBrowser[\s\/]([\d.]+).*$/", $userAgent) ?? pregMatch("/^.*baiduboxapp\/([\d.]+).*$/", $userAgent),
        "UC" => pregMatch("/^.*UC?Browser\/([\d.]+).*$/", $userAgent),
        "Sogou" => pregMatch("/^.*SE ([\d.X]+).*$/", $userAgent) ?? pregMatch("/^.*SogouMobileBrowser\/([\d.]+).*$/", $userAgent),
        "115Browser" => pregMatch("/^.*115Browser\/([\d.]+).*$/", $userAgent),
        "TheWorld" => pregMatch("/^.*TheWorld ([\d.]+).*$/", $userAgent),
        "XiaoMi" => pregMatch("/^.*MiuiBrowser\/([\d.]+).*$/", $userAgent),
        "Vivo" => pregMatch("/^.*VivoBrowser\/([\d.]+).*$/", $userAgent),
        "OPPO" => pregMatch("/^.*HeyTapBrowser\/([\d.]+).*$/", $userAgent),
        "Quark" => pregMatch("/^.*Quark\/([\d.]+).*$/", $userAgent),
        "Qiyu" => pregMatch("/^.*Qiyu\/([\d.]+).*$/", $userAgent),
        "Wechat" => pregMatch("/^.*MicroMessenger\/([\d.]+).*$/", $userAgent),
        "WechatWork" => pregMatch("/^.*wxwork\/([\d.]+).*$/", $userAgent),
        "Taobao" => pregMatch("/^.*AliApp\(TB\/([\d.]+).*$/", $userAgent),
        "Alipay" => pregMatch("/^.*AliApp\(AP\/([\d.]+).*$/", $userAgent),
        "Weibo" => pregMatch("/^.*weibo__([\d.]+).*$/", $userAgent),
        "Douban" => pregMatch("/^.*com.douban.frodo\/([\d.]+).*$/", $userAgent),
        "Suning" => pregMatch("/^.*SNEBUY-APP([\d.]+).*$/", $userAgent),
        "iQiYi" => pregMatch("/^.*IqiyiVersion\/([\d.]+).*$/", $userAgent),
        "DingTalk" => pregMatch("/^.*DingTalk\/([\d.]+).*$/", $userAgent),
        "Douyin" => pregMatch("/^.*app_version\/([\d.]+).*$/", $userAgent),
        "Huawei" => pregMatch("/^.*Version\/([\d.]+).*$/", $userAgent) ??  pregMatch("/^.*HuaweiBrowser\/([\d.]+).*$/", $userAgent) ?? pregMatch("/^.*HBPC\/([\d.]+).*$/", $userAgent),
        "360SE" => $browsers_360SE[$chromeVersion] ?? '',
        "360EE" => $browsers_360EE[$chromeVersion] ?? '',
        "Liebao" => pregMatch("/^.*LieBaoFast\/([\d.]+).*$/", $userAgent) ?? $browsers_liebao[$chromeVersion],
        "2345Explorer" => $browsers_2345[$chromeVersion]  ?? pregMatch("/^.*2345Explorer\/([\d.]+).*$/", $userAgent) ?? pregMatch("/^.*Mb2345Browser\/([\d.]+).*$/", $userAgent),
    ];


    if ($browsersVersion[$deviceInfo['browser']]) {
        $deviceInfo["version"] = $browsersVersion[$deviceInfo['browser']];
        if ($deviceInfo["version"] == $userAgent) $deviceInfo['version'] = '';
    }

    // 修正浏览器版本信息
    $chrome = pregMatch('/\S+Browser/', $userAgent);
    if ($deviceInfo['browser'] == 'Chrome' && $chrome) {
        $deviceInfo['browser'] = $chrome;
        $deviceInfo['version'] = pregMatch('/^.*Browser\/([\d.]+).*$/', $userAgent);
    }

    return $deviceInfo;
}

/**
 * 返回符合正则的值
 *
 * @param string $reg 正则
 * @param string $sourceData 源数据
 * @return mixed|void
 */
function pregMatch($reg, $sourceData) {
    if (preg_match($reg, $sourceData, $mat)) return $mat[1];
}
2.找到输出评论表单文件 comments.php ,在需要显示的地方加入以下代码;
<span id="device_comment_<?php $comments->sequence(); ?>" >
     <?php $data = getBrowsersInfo($comments->agent); ?>
     <span><?php echo $data['system']; ?> </span><!-- 输出操作系统 -->
     <span><?php echo $data['systemVersion']; ?> </span> · <!-- 输出操作系统版本号 -->
     <span><?php echo $data['browser']; ?> </span> <!-- 输出浏览器名称 -->
     <span><?php echo $data['version']; ?> </span> <!-- 输出IP地址,为了用户安全,可以删除 -->
</span>
3.Joe主题自带的获取操作系统及浏览器版本不太准确,找到 Joe/core/function.php 文件,将1中的代码替换掉 第43~116行 的代码(如下);
/* 根据评论agent获取浏览器类型 */
function _getAgentBrowser($agent)
{
  if (preg_match('/MSIE\s([^\s|;]+)/i', $agent, $regs)) {
    $outputer = 'Internet Explore';
  } else if (preg_match('/FireFox\/([^\s]+)/i', $agent, $regs)) {
    $outputer = 'FireFox';
  } else if (preg_match('/Maxthon([\d]*)\/([^\s]+)/i', $agent, $regs)) {
    $outputer = 'MicroSoft Edge';
  } else if (preg_match('#360([a-zA-Z0-9.]+)#i', $agent, $regs)) {
    $outputer = '360 Fast Browser';
  } else if (preg_match('/Edge([\d]*)\/([^\s]+)/i', $agent, $regs)) {
    $outputer = 'MicroSoft Edge';
  } else if (preg_match('/UC/i', $agent)) {
    $outputer = 'UC Browser';
  } else if (preg_match('/QQ/i', $agent, $regs) || preg_match('/QQ Browser\/([^\s]+)/i', $agent, $regs)) {
    $outputer = 'QQ Browser';
  } else if (preg_match('/UBrowser/i', $agent, $regs)) {
    $outputer = 'UC Browser';
  } else if (preg_match('/Opera[\s|\/]([^\s]+)/i', $agent, $regs)) {
    $outputer = 'Opera';
  } else if (preg_match('/Chrome([\d]*)\/([^\s]+)/i', $agent, $regs)) {
    $outputer = 'Google Chrome';
  } else if (preg_match('/safari\/([^\s]+)/i', $agent, $regs)) {
    $outputer = 'Safari';
  } else {
    $outputer = 'Google Chrome';
  }
  echo $outputer;
}

/* 根据评论agent获取设备类型 */
function _getAgentOS($agent)
{
  $os = "Linux";
  if (preg_match('/win/i', $agent)) {
    if (preg_match('/nt 6.0/i', $agent)) {
      $os = 'Windows Vista';
    } else if (preg_match('/nt 6.1/i', $agent)) {
      $os = 'Windows 7';
    } else if (preg_match('/nt 6.2/i', $agent)) {
      $os = 'Windows 8';
    } else if (preg_match('/nt 6.3/i', $agent)) {
      $os = 'Windows 8.1';
    } else if (preg_match('/nt 5.1/i', $agent)) {
      $os = 'Windows XP';
    } else if (preg_match('/nt 10.0/i', $agent)) {
      $os = 'Windows 10';
    } else {
      $os = 'Windows X64';
    }
  } else if (preg_match('/android/i', $agent)) {
    if (preg_match('/android 9/i', $agent)) {
      $os = 'Android Pie';
    } else if (preg_match('/android 8/i', $agent)) {
      $os = 'Android Oreo';
    } else {
      $os = 'Android';
    }
  } else if (preg_match('/ubuntu/i', $agent)) {
    $os = 'Ubuntu';
  } else if (preg_match('/linux/i', $agent)) {
    $os = 'Linux';
  } else if (preg_match('/iPhone/i', $agent)) {
    $os = 'iPhone';
  } else if (preg_match('/mac/i', $agent)) {
    $os = 'MacOS';
  } else if (preg_match('/fusion/i', $agent)) {
    $os = 'Android';
  } else {
    $os = 'Linux';
  }
  echo $os;
}
4.然后打开 Joe/public/comment.php 文件第116行,替换成如下下代码;
            <div class="agent">
            <span id="device_comment_<?php $comments->sequence(); ?>" >
     <?php $data = getBrowsersInfo($comments->agent); ?>
            <span><?php echo $data['system']; ?> </span><!-- 输出操作系统 -->
            <span><?php echo $data['systemVersion']; ?> </span> · <!-- 输出操作系统版本号 -->
            <span><?php echo $data['browser']; ?> </span> <!-- 输出浏览器名称 -->
            <span><?php echo $data['version']; ?> </span> <!-- 输出IP地址,为了用户安全,可以删除 -->
           </span>
            </div>
0

评论 (0)

取消