Wordpress 记录登录日志

wordpress 记录登录日志


function get_client_ip() {  
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
        $ip = getenv("HTTP_CLIENT_IP");
    } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),  
"unknown")) {
        $ip = getenv("HTTP_X_FORWARDED_FOR"); 
    } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
        $ip = getenv("REMOTE_ADDR");
    } else if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR']  
&& strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))  {
        $ip = $_SERVER['REMOTE_ADDR']; 
    } else {
        $ip = "unknown";
    }
    return ($ip);  
}  

function hanada_user_login_message($user_login)
{
    global $user_ID;
    date_default_timezone_set(PRC);  // 纠正8小时时差
    $current_user = get_user_by( 'login', $user_login );//获取当前登录信息
    $login_date = date('Y-m-d H:i:s');//获取登录时间
    $login_ip = get_client_ip();//获取登录IP
    $login_addr_arr = json_decode(@file_get_contents('https://freeapi.ipip.net/'.$login_ip),true);//使用接口获得登录IP对应地理位置json数组并解码为php数组
    $login_addr = $login_addr_arr[0].$login_addr_arr[1].$login_addr_arr[2].$login_addr_arr[3].$login_addr_arr[4];//拼接地理位置
    $login_ua = $_SERVER['HTTP_USER_AGENT'];//获取UA
    $login_type = '密码';//标记登录类型为使用密码登录
    //将登录记录写入用户表
    if (empty(get_user_meta($current_user->ID,'login_num', true))) {
        update_user_meta($current_user->ID, 'login_num', 1);
        $login_message_arr = array(
            'login_num_1' => array( 'date' => '', 'ip' => '', 'addr' => '', 'ua' => '', 'type' => '' ),
            'login_num_2' => array( 'date' => '', 'ip' => '', 'addr' => '', 'ua' => '', 'type' => '' ),
            'login_num_3' => array( 'date' => '', 'ip' => '', 'addr' => '', 'ua' => '', 'type' => '' ),
            'login_num_4' => array( 'date' => '', 'ip' => '', 'addr' => '', 'ua' => '', 'type' => '' ),
            'login_num_5' => array( 'date' => '', 'ip' => '', 'addr' => '', 'ua' => '', 'type' => '' ),
            'login_num_6' => array( 'date' => '', 'ip' => '', 'addr' => '', 'ua' => '', 'type' => '' ),
            'login_num_7' => array( 'date' => '', 'ip' => '', 'addr' => '', 'ua' => '', 'type' => '' ),
            'login_num_8' => array( 'date' => '', 'ip' => '', 'addr' => '', 'ua' => '', 'type' => '' ),
            );
    } else if (get_user_meta($current_user->ID,'login_num', true) >= 8) {
        update_user_meta($current_user->ID, 'login_num', 1);
        $login_message_arr = get_user_meta($current_user->ID, 'login_message', true);
    }else {
        update_user_meta($current_user->ID, 'login_num', get_user_meta($current_user->ID,'login_num', true) + 1 );
        $login_message_arr = get_user_meta($current_user->ID, 'login_message', true);
    }
    $login_num = get_user_meta($current_user->ID,'login_num', true);
    $login_message_arr['login_num_'.$login_num]['date'] = $login_date;
    $login_message_arr['login_num_'.$login_num]['ip'] = $login_ip;
    $login_message_arr['login_num_'.$login_num]['addr'] = $login_addr;
    $login_message_arr['login_num_'.$login_num]['ua'] = $login_ua;
    $login_message_arr['login_num_'.$login_num]['type'] = $login_type;
    update_user_meta($current_user->ID, 'login_message', $login_message_arr);
}
add_action('wp_login','hanada_user_login_message');

function clrs_get_user_agent($useragent)
{
    $stitle = $sver = null;
    if(preg_match('/iPad/i',$useragent)){
        $stitle = 'iPad';
        if(preg_match('/CPU\ OS\ ([.<em>0-9a-zA-Z]+)/i',$useragent,$regmatch)) $sver = 'iOS '.str_replace('</em>','.',$regmatch[1]);
    }elseif(preg_match('/iPod/i',$useragent)){
        $stitle = 'iPod';
        if(preg_match('/iPhone\ OS\ ([.<em>0-9a-zA-Z]+)/i',$useragent,$regmatch)) $sver = 'iOS '.str_replace('</em>','.',$regmatch[1]);
    }elseif(preg_match('/iPhone/i',$useragent)&&!preg_match('/Windows Phone/i',$useragent)){
        $stitle = 'iPhone';
        if(preg_match('/iPhone\ OS\ ([.<em>0-9a-zA-Z]+)/i',$useragent,$regmatch)) $sver = 'iOS '.str_replace('</em>', '.', $regmatch[1]);
    }elseif(preg_match('/Windows Phone OS 7/i',$useragent)||preg_match('/ZuneWP7/i',$useragent)||preg_match('/WP7/i',$useragent)){
        $stitle = 'Windows Phone';
        $sver = '7';
    }elseif(preg_match('/Windows Phone OS 8.1/i',$useragent)||preg_match('/Windows Phone 8.1/i',$useragent)||preg_match('/WP8.1/i',$useragent)){
        $stitle = 'Windows Phone';
        $sver = '8.1';
    }elseif(preg_match('/Windows Phone OS 8/i',$useragent)||preg_match('/Windows Phone 8/i',$useragent)||preg_match('/WP8/i',$useragent)){
        $stitle = 'Windows Phone';
        $sver = '8';
    }elseif(preg_match('/Windows Phone 10/i',$useragent)||preg_match('/WP10/i',$useragent)){
        $stitle = 'Windows Phone';
        $sver = '10';
    }elseif(preg_match('/wp-windowsphone/i',$useragent)){
        $stitle = 'Windows Phone';
    }elseif(preg_match('/Android/i',$useragent)){
        $stitle = 'Android';
        if(preg_match('/Android[\ |\/]?([.0-9a-zA-Z]+)/i',$useragent,$regmatch)) $sver = $regmatch[1];
    }elseif(preg_match('/[^A-Za-z]Arch/i',$useragent)){
        $stitle = 'Arch Linux';
    }elseif(preg_match('/CentOS/i',$useragent)){
        $stitle = 'CentOS';
        if(preg_match('/.el([.0-9a-zA-Z]+).centos/i',$useragent,$regmatch)) $sver = $regmatch[1];
    }elseif(preg_match('/Debian/i',$useragent)){
        $stitle = 'Debian GNU/Linux';
    }elseif(preg_match('/Edubuntu/i',$useragent)){
        $stitle = 'Edubuntu';
        if(preg_match('/Edubuntu<a href="[.0-9a-zA-Z]+">\/|\ </a>/i',$useragent,$regmatch)) $sver = $regmatch[1];
    }elseif(preg_match('/FreeBSD/i',$useragent)){
        $stitle = 'FreeBSD';
    }elseif(preg_match('/Kubuntu/i',$useragent)){
        $stitle = 'Kubuntu';
        if(preg_match('/Kubuntu<a href="[.0-9a-zA-Z]+">\/|\ </a>/i',$useragent,$regmatch)) $sver = $regmatch[1];
    }elseif(preg_match('/Linux\ Mint/i',$useragent)){
        $stitle = 'Linux Mint';
        if(preg_match('/Linux\ Mint\/([.0-9a-zA-Z]+)/i',$useragent,$regmatch)) $sver = $regmatch[1];
    }elseif(preg_match('/Lubuntu/i',$useragent)){
        $stitle = 'Lubuntu';
        if (preg_match('/Lubuntu<a href="[.0-9a-zA-Z]+">\/|\ </a>/i',$useragent,$regmatch)) $sver = $regmatch[1];
    }elseif(preg_match('/Mac/i',$useragent)||preg_match('/Darwin/i',$useragent)){
        $stitle = 'Mac';
        if(preg_match('/Mac OS X/i',$useragent)||preg_match('/Mac OSX/i',$useragent)){
            if(preg_match('/Mac OS X/i',$useragent)){
                $sver = substr($useragent,strpos(strtolower($useragent),strtolower('OS X'))+4);
            }else{
                $sver = substr($useragent,strpos(strtolower($useragent),strtolower('OSX'))+3);
            }
            $sver = substr($sver,0,strpos($sver,')'));
            if(strpos($sver,';') > -1) $sver = substr($sver,0,strpos($sver,';'));
            $sver = str_replace('<em>','.',$sver);
            if($wpua_show_version==='simple'&&preg_match('/([0-9]+.[0-9]+)/i',$sver,$regmatch)) $sver = $regmatch[1];
            $sver = (empty($sver))?'OS X':"OS X $sver";
        }elseif(preg_match('/Darwin/i',$useragent)){
            $sver = 'OS Darwin';
        }else{
            $stitle = 'Macintosh';
        }
    }elseif(preg_match('/NetBSD/i',$useragent)){
        $stitle = 'NetBSD';
    }elseif(preg_match('/Nova/i',$useragent)){
        $stitle = 'Nova';
        if(preg_match('/Nova<a href="[.0-9a-zA-Z]+">\/|\ </a>/i',$useragent,$regmatch)) $sver = $regmatch[1];
    }elseif(preg_match('/OpenBSD/i',$useragent)){
        $stitle = 'OpenBSD';
    }elseif(preg_match('/Red\ Hat/i',$useragent)||preg_match('/RedHat/i',$useragent)){
        $stitle = 'Red Hat';
        if(preg_match('/.el([._0-9a-zA-Z]+)/i',$useragent,$regmatch)){
            $stitle .= ' Enterprise Linux';
            $sver = str_replace('</em>','.',$regmatch[1]);
        }
    }elseif(preg_match('/Xubuntu/i',$useragent)){
        $stitle = 'Xubuntu';
        if(preg_match('/Xubuntu<a href="[.0-9a-zA-Z]+">\/|\ </a>/i',$useragent,$regmatch)) $sver = $regmatch[1];
    }elseif(preg_match('/Ubuntu/i',$useragent)){
        $stitle = 'Ubuntu';
        if(preg_match('/Ubuntu<a href="[.0-9]+[.0-9a-zA-Z]+">\/|\ </a>/i',$useragent,$regmatch)) $sver = $regmatch[1];
    }elseif(preg_match('/Unix/i',$useragent)){
        $stitle = 'Unix';
    }elseif(preg_match('/Linux/i',$useragent)){
        $stitle = 'Linux';
    }elseif(preg_match('/Windows/i',$useragent)||preg_match('/WinNT/i',$useragent)||preg_match('/Win32/i',$useragent)){
        $stitle = 'Windows';
        if(preg_match('/Windows NT 10.0/i',$useragent)||preg_match('/Windows NT 6.4/i',$useragent)){
            $sver = '10';
        }elseif(preg_match('/Windows NT 6.3/i',$useragent)){
            $sver = '8.1';
        }elseif(preg_match('/Windows NT 6.2/i',$useragent)){
            $sver = '8';
        }elseif(preg_match('/Windows NT 6.1/i',$useragent)){
            $sver = '7';
        }elseif(preg_match('/Windows NT 6.0/i',$useragent)){
            $sver = 'Vista';
        }elseif(preg_match('/Windows NT 5.2 x64/i',$useragent)){
            $sver = 'XP';
        }elseif(preg_match('/Windows NT 5.2/i',$useragent)){
            $sver = 'Server 2003';
        }elseif(preg_match('/Windows NT 5.1/i',$useragent)||preg_match('/Windows XP/i',$useragent)){
            $sver = 'XP';
        }elseif(preg_match('/Windows NT 5.01/i',$useragent)){
            $sver = '2000, Service Pack 1 (SP1)';
        }elseif(preg_match('/Windows NT 5.0/i',$useragent)||preg_match('/Windows NT5/i',$useragent)||preg_match('/Windows 2000/i',$useragent)){
            $sver = '2000';
        }elseif(preg_match('/Windows NT 4.0/i',$useragent)||preg_match('/WinNT4.0/i',$useragent)){
            $sver = 'NT 4.0';
        }elseif (preg_match('/Windows NT 3.51/i',$useragent)||preg_match('/WinNT3.51/i',$useragent)){
            $sver = 'NT 3.11';
        }elseif(preg_match('/Windows NT/i',$useragent)||preg_match('/WinNT/i',$useragent)){
            $sver = 'NT';
        }elseif(preg_match('/Windows 3.11/i',$useragent)||preg_match('/Win3.11/i',$useragent)||preg_match('/Win16/i',$useragent)){
            $sver = '3.11';
        }elseif(preg_match('/Windows 3.1/i',$useragent)){
            $sver = '3.1';
        }elseif(preg_match('/Windows 98; Win 9x 4.90/i',$useragent)||preg_match('/Win 9x 4.90/i',$useragent)||preg_match('/Windows ME/i',$useragent)){
            $sver = 'Millennium Edition (Windows Me)';
        }elseif(preg_match('/Win98/i',$useragent)){
            $sver = '98 SE';
        }elseif(preg_match('/Windows 98/i',$useragent)||preg_match('/Windows\ 4.10/i',$useragent)){
            $sver = '98';
        }elseif(preg_match('/Windows 95/i',$useragent)||preg_match('/Win95/i',$useragent)){
            $sver = '95';
        }elseif(preg_match('/Windows CE/i',$useragent)){
            $sver = 'CE';
        }elseif(preg_match('/WM5/i',$useragent)){
            $sver = 'Mobile 5';
        }elseif(preg_match('/WindowsMobile/i',$useragent)){
            $sver = 'Mobile';
        }else{
        }
    }
    $btitle = null;
    if(preg_match('/BonEcho/i',$useragent)){
        $btitle = 'BonEcho';
    }elseif(preg_match('/chromeframe/i',$useragent)){
        $btitle = 'Google Chrome Frame';
    }elseif(preg_match('/ChromePlus/i',$useragent)){
        $btitle = 'ChromePlus';
    }elseif(preg_match('/Chromium/i',$useragent)){
        $btitle = 'Chromium';
    }elseif(preg_match('/CrMo/i',$useragent)){
        $btitle = 'Chrome Mobile';
    }elseif(preg_match('/CriOS/i',$useragent)){
        $btitle = 'Chrome';
    }elseif(preg_match('/Galaxy/i',$useragent)&&!preg_match('/Chrome/i',$useragent)){
        $btitle = 'Galaxy';
    }elseif(preg_match('/GoBrowser/i',$useragent)){
        $btitle = 'GO Browser';
    }elseif(preg_match('/Google\ Wireless\ Transcoder/i',$useragent)){
        $btitle = 'Google Wireless Transcoder';
    }elseif(preg_match('/GreenBrowser/i',$useragent)){
        $btitle = 'GreenBrowser';
    }elseif(preg_match('/GSA/i',$useragent)&&preg_match('/Mobile/i',$useragent)){
        $btitle = 'Google Search App';
    }elseif(preg_match('/Lorentz/i',$useragent)){
        $btitle = 'Lorentz';
    }elseif(preg_match('/MiniBrowser/i',$useragent)){
        $btitle = 'MiniBrowser';
    }elseif(preg_match('/Minimo/i',$useragent)){
        $btitle = 'Minimo';
    }elseif(preg_match('/MiuiBrowser/i',$useragent)){
        $btitle = 'MIUI Browser';
    }elseif(preg_match('/Mosaic/i',$useragent)){
        $btitle = 'Mosaic';
    }elseif(preg_match('/MozillaDeveloperPreview/i',$useragent)){
        $btitle = 'Mozilla Developer Preview';
    }elseif(preg_match('/MQQBrowser/i',$useragent)||preg_match('/QQBrowser/i',$useragent)){
        $btitle = 'QQbrowser';
    }elseif(preg_match('/MultiZilla/i',$useragent)){
        $btitle = 'MultiZilla';
    }elseif(preg_match('/NokiaBrowser/i',$useragent)){
        $btitle = 'Nokia Browser';
    }elseif(preg_match('/OneBrowser/i',$useragent)){
        $btitle = 'OneBrowser';
    }elseif(preg_match('/Opera Mini/i',$useragent)){
        $btitle = 'Opera Mini';
    }elseif(preg_match('/Opera Mobi/i',$useragent)){
        $btitle = 'Opera Mobile';
    }elseif(preg_match('#Opera.([a-zA-Z0-9.]+)#i',$useragent,$matches)){
        $btitle = 'Opera';
        if(preg_match('/Edition ([\ .<em>0-9a-zA-Z]+)/i',$useragent,$regmatch)){
            $btitle .= ' '.$regmatch[1];
        }elseif(preg_match('/Opera ([\ ._0-9a-zA-Z]+)/i',$useragent,$regmatch)){
            $btitle .= ' '.$regmatch[1];
        }
    }elseif(preg_match('/SeaMonkey/i',$useragent)){
        $btitle = 'SeaMonkey';
    }elseif(preg_match('/Series60/i',$useragent)&&!preg_match('/Symbian/i',$useragent)){
        $btitle = 'Nokia Series60';
    }elseif(preg_match('/S60/i',$useragent)&&!preg_match('/Symbian/i',$useragent)){
        $btitle = 'Nokia S60';
    }elseif(preg_match('#SE 2([a-zA-Z0-9.]+)#i',$useragent,$matches)&&preg_match('/MetaSr/i',$useragent)){
        $btitle = 'Sogou Explorer';
    }elseif(preg_match('/Shiretoko/i',$useragent)){
        $btitle = 'Shiretoko';
    }elseif(preg_match('/SlimBrowser/i',$useragent)){
        $btitle = 'SlimBrowser';
    }elseif(preg_match('#SAMSUNG-(S.H-[a-zA-Z0-9</em>/.]+)#i',$useragent)){
        $btitle = "Samsung";
    }elseif(preg_match('/Songbird/i',$useragent)){
        $btitle = 'Songbird';
    }elseif(preg_match('/TheWorld/i',$useragent)){
        $btitle = 'TheWorld Browser';
    }elseif(preg_match('/Thunderbird/i',$useragent)){
        $btitle = 'Thunderbird';
    }elseif(preg_match('#TencentTraveler ([a-zA-Z0-9.]+)#i',$useragent)){
        $btitle = 'TT Explorer';
    }elseif(preg_match('/uBrowser/i',$useragent)&&!preg_match('/Chrome/i',$useragent)){
        $btitle = 'uBrowser';
    }elseif((preg_match('/Ubuntu\;\ Mobile/i',$useragent)||preg_match('/Ubuntu\;\ Tablet/i',$useragent)&&preg_match('/WebKit/i',$useragent))){
        $btitle = 'Ubuntu Web Browser';
    }elseif(preg_match('#UBrowser([a-zA-Z0-9.]+)#i',$useragent,$matches)){
        $btitle = 'UC Browser';
    }elseif(preg_match('#UCBrowser([a-zA-Z0-9.]+)#i',$useragent,$matches)){
        $btitle = 'UC Browser';
    }elseif(preg_match('#UC\ Browser([a-zA-Z0-9.]+)#i',$useragent,$matches)){
        $btitle = 'UC Browser';
    }elseif(preg_match('#UCWEB([a-zA-Z0-9.]+)#i',$useragent,$matches)){
        $btitle = 'UC Browser';
    }elseif(preg_match('/UltraBrowser/i',$useragent)){
        $btitle = 'UltraBrowser';
    }elseif(preg_match('/UP.Browser/i',$useragent)){
        $btitle = 'Openwave Mobile Browser';
    }elseif(preg_match('/UP.Link/i',$useragent)){
        $btitle = 'Openwave Mobile Browser';
    }elseif(preg_match('/AppleWebkit/i',$useragent,$matches)&&preg_match('/Android/i',$useragent)&&!preg_match('/Chrome/i',$useragent)){
        $btitle = 'Android Webkit';
    }elseif(preg_match('/WebExplorer/i',$useragent)){
        $btitle = 'Web Explorer';
    }elseif(preg_match('/Chrome/i',$useragent)&&preg_match('/Mobile/i',$useragent)&&(preg_match('/Version/i',$useragent)||preg_match('/wv/i',$useragent,$matches))){
        $btitle = 'WebView';
    }elseif(preg_match('/wp-android/i',$useragent)){
        $btitle = 'Wordpress App';
    }elseif(preg_match('/wp-blackberry/i',$useragent)){
        $btitle = 'wp-blackberry';
    }elseif(preg_match('/wp-iphone/i',$useragent)){
        $btitle = 'Wordpress App';
    }elseif(preg_match('/wp-nokia/i',$useragent)){
        $btitle = 'wp-nokia';
    }elseif(preg_match('/wp-webos/i',$useragent)){
        $btitle = 'wp-webos';
    }elseif(preg_match('/wp-windowsphone/i',$useragent)){
        $btitle = 'wp-windowsphone';
    }elseif(preg_match('/YaBrowser/i',$useragent)){
        $btitle = 'Yandex Browser';
    }elseif(preg_match('#Edge/([a-zA-Z0-9.]+)#i',$useragent,$matches)&&preg_match('/Chrome/i',$useragent)&&preg_match('/Safari/i',$useragent)){
        $btitle = 'Microsoft Edge';
    }elseif(preg_match('#Chrome/([a-zA-Z0-9.]+)#i',$useragent,$matches)){
        $btitle = 'Google Chrome';
    }elseif(preg_match('/Safari/i',$useragent)&&!preg_match('/Nokia/i',$useragent)){
        $btitle = 'Safari';
    }elseif(preg_match('/Nokia/i',$useragent)){
        $btitle = 'Nokia Web Browser';
    }elseif(preg_match('#(Firefox|Phoenix|Firebird|BonEcho|GranParadiso|Minefield|Iceweasel)/([a-zA-Z0-9.]+)#i',$useragent,$matches)){
        $btitle = 'Firefox';
    }elseif(preg_match('#360([a-zA-Z0-9.]+)#i',$useragent,$matches)){
        $btitle = '360Safe Explorer';
    }elseif(preg_match('/baidubrowser/i',$useragent)){
        $btitle = 'Baidu Browser';
    }elseif(preg_match('/\ Spark/i',$useragent)){
        $btitle = 'Baidu Spark';
    }elseif(preg_match('/MSIE/i',$useragent)||preg_match('/Trident/i',$useragent)){
        $btitle = 'Internet Explorer';
    }
    if(!$stitle){
        $stitle = 'Unknown System';
    }
    if(!$btitle){
        $btitle = 'Unknown Browser';
    }
    return $sver ?  $stitle.' '.$sver.' '.$btitle :  $stitle.' '.$btitle;
}

function hanade_shotcode_login_message()
{
    //未登录时返回空值
    if (!is_user_logged_in()) {
        return null;
    }
    //获取登录记录信息
    $current_user = wp_get_current_user();
    $user_meta = get_user_meta($current_user->ID);
    //echo '<pre>';
    //print_r($user_meta);
    $login_num = get_user_meta($current_user->ID,'login_num', true);
    $login_message_arr = get_user_meta($current_user->ID,'login_message', true);
    
    // echo '<pre>';
    // print_r($login_num);
    // print_r($login_message_arr);
    // die;
    //正则替换隐藏IP地址后两位
    $login_ip_reg = '~(\d+).(\d+).(\d+).(\d+)~';
    //设置北京时间
    date_default_timezone_set("PRC");
    //获取当前cookie
    $cookie = wp_parse_auth_cookie( '', 'logged_in' );
    //获取当前账号sessions
    $sessions = wp_get_all_sessions();
    //格式化输出登录记录信息
    $login_message_table = '
<table class="login_message_table"><thead><tr>
            <th>时间</th>
            <th>IP地址</th>
            <th>地理位置</th>
            <th>登录设备</th>
            <th>登录方式</th>
        </tr></thead><tbody class="row-hover">';
    for( $i=$login_num; $i>0; $i-- ) {
        if(!empty($login_message_arr['login_num_'.$i]['date'])) {
            $login_message_table .= '
        <tr>
            <td class="login_message_date">'.$login_message_arr['login_num_'.$i]['date'].'</td>
            <td class="login_message_op">'.preg_replace($login_ip_reg, "$1.$2.*.*", $login_message_arr['login_num_'.$i]['ip']).'</td>
            <td class="login_message_addr">'.$login_message_arr['login_num_'.$i]['addr'].'</td>
            <td class="login_message_ua" title="'.$login_message_arr['login_num_'.$i]['ua'].'">'.clrs_get_user_agent($login_message_arr['login_num_'.$i]['ua']).'</td>
            <td class="login_message_type">'.$login_message_arr['login_num_'.$i]['type'];
            if ( strtotime($login_message_arr['login_num_'.$i]['date']) == $cookie['expiration'] - 1209600 || strtotime($login_message_arr['login_num_'.$i]['date']) == $cookie['expiration'] - 172800) {
                $login_message_table = $login_message_table.'<span class="current_session">当前登录</span></td></tr>';
            } else {
                $has_seesion = false;
                foreach ($sessions as $value) {
                    if( strtotime($login_message_arr['login_num_'.$i]['date']) == $value['login'] ){
                        $login_message_table = $login_message_table.'<span class="other_session">尚未登出</span></td></tr>';
                        $has_seesion = true;
                        break;
                    }
                }
                if( $has_seesion == false ) {
                    $login_message_table = $login_message_table.'<span class="loggedout_session">已经登出</span></td></tr>';
                }
            }
        }
    }
    for( $i=8; $i>$login_num; $i-- ) {
        if(!empty($login_message_arr['login_num_'.$i]['date'])){
                $login_message_table = $login_message_table.'
        <tr>
            <td class="login_message_date">'.$login_message_arr['login_num_'.$i]['date'].'</td>
            <td class="login_message_ip">'.preg_replace($login_ip_reg, "$1.$2.*.*", $login_message_arr['login_num_'.$i]['ip']).'</td>
            <td class="login_message_addr">'.$login_message_arr['login_num_'.$i]['addr'].'</td>
            <td class="login_message_ua" title="'.$login_message_arr['login_num_'.$i]['ua'].'">'.clrs_get_user_agent($login_message_arr['login_num_'.$i]['ua']).'</td>
            <td class="login_message_type">'.$login_message_arr['login_num_'.$i]['type'];
            if( strtotime($login_message_arr['login_num_'.$i]['date']) === $cookie['expiration'] - 1209600 || strtotime($login_message_arr['login_num_'.$i]['date'] ) == $cookie['expiration'] - 172800){
                $login_message_table = $login_message_table.'<span class="current_session">当前登录</span></td></tr>';
            }else{
                $has_seesion = false;
                foreach($sessions as $value){
                    if( strtotime($login_message_arr['login_num_'.$i]['date']) == $value['login'] ){
                        $login_message_table = $login_message_table.'<span class="other_session">尚未登出</span></td></tr>';
                        $has_seesion = true;
                        break;
                    }
                }
                if( $has_seesion == false ) $login_message_table = $login_message_table.'<span class="loggedout_session">已经登出</span></td></tr>';
            }
        }
    }
    $login_message_table = $login_message_table.'</tbody></table>';
    // $login_message_table .= '<p>111</p>';
    return $login_message_table;
}
add_shortcode('login_message', 'hanade_shotcode_login_message');

function insert_last_login( $login ) {
	global $user_id;
	date_default_timezone_set(PRC);
	$user = get_userdatabylogin( $login );
	//current_time( 'mysql' )
	update_user_meta( $user->ID, 'last_login', date('Y-m-d H:i:s') );
}
add_action( 'wp_login', 'insert_last_login' );
// 添加一个新栏目“上次登录” 
function add_last_login_column( $columns ) {
	$columns['last_login'] = '上次登录';
	return $columns;
}
add_filter( 'manage_users_columns', 'add_last_login_column' );
// 显示登录时间到新增栏目 
function add_last_login_column_value( $value, $column_name, $user_id ) {
	$user = get_userdata( $user_id );
	if ( 'last_login' == $column_name && $user->last_login ) {
        $value = get_user_meta( $user->ID, 'last_login', true);
    } else {
        $value = '从未登录';
    }
	return $value;
}
add_action( 'manage_users_custom_column', 'add_last_login_column_value', 10, 3 );

function jerry_display_dashboard() {
    $service_email = get_service_email();
    $default_service_email = get_option('deafult_service_email');
    $html = '
    <form action="#" method="post" enctype="multipart/form-data" name="op_form" id="op_form">
      <div class="wrap">
        <h2>当前站点选项</h2>
        <table>
           <tbody>
               <tr>
                   <td>default service email</td>
               </tr>
               <tr>
                   <td>
                        <input name="deafult_service_email" value="'.$deafult_service_email.'" id="deafult_service_email" style="width: 300px;">
                   </td>
               </tr>
               <tr>
                   <td>service email</td>
               </tr>
               <tr>
                   <td>
                        <input name="site_service_email" value="'.$service_email.'" id="site_service_email" style="width: 300px;">
                   </td>
               </tr>
           </tbody>
        </table>
        <div class="submit">
            <input type="submit" name="jerry_input_save" calue="save">
        </div>
      </div>
    </form>';
    echo $html;
    
    //未登录时返回空值
    if (!is_user_logged_in()) {
        return null;
    }
    //获取登录记录信息
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    if (1 === $user_id) {
        echo do_shortcode("[login_message]");
    }
}

function jerry_admin_init() {
    if ( isset($_POST['jerry_input_save']) ) {
        $options = get_option('site_service_email');
        $site_top_host = getTopHost();
        $options[$site_top_host] = trim($_POST['site_service_email']);
        update_option('site_service_email', $options);

        $deafult_service_email = trim($_POST['deafult_service_email']);
        update_option('deafult_service_email', $deafult_service_email);
    }
    add_theme_page("登录日志", "登录日志", "edit_themes", basename(__FILE__), 'jerry_display_dashboard');
}
add_action('admin_menu', "jerry_admin_init");
posted @ 2021-09-01 09:44  绝技小嗨皮  阅读(241)  评论(0编辑  收藏  举报
Title