get_charset_collate(); # add required package require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); # if not table create $tblname = "wpasl_log"; $tbltrak = $table_prefix.$tblname; if($wpdb->get_var( "show tables like '".$tbltrak."'") != $tbltrak) { $sql = " CREATE TABLE ".$tbltrak." ( `sno` int(11) NOT NULL AUTO_INCREMENT, `type` enum('admin','front') NOT NULL, `user_id` int(11) NOT NULL, `ip` varchar(20) NOT NULL, `user_agent` longtext NOT NULL, `platform` varchar(20) NOT NULL, `url` longtext NOT NULL, `datetime` datetime NOT NULL, PRIMARY KEY (`sno`) ) ".$charset_collate." "; dbDelta($sql); } # if not table create /*$tblname = "wpasl_blocktable"; $tbltrak = $table_prefix.$tblname; if($wpdb->get_var( "show tables like '".$tbltrak."'") != $tbltrak) { $sql = " CREATE TABLE ".$tbltrak." ( `sno` int(11) NOT NULL AUTO_INCREMENT, `ip` varchar(20) NOT NULL, `datetime` datetime NOT NULL, `blocked_by` int(11) NOT NULL, PRIMARY KEY (`sno`) ) ".$charset_collate; dbDelta($sql); }*/ # if not table create /*$tblname = "wpasl_alert"; $tbltrak = $table_prefix.$tblname; if($wpdb->get_var( "show tables like '".$tbltrak."'") != $tbltrak) { $sql = " CREATE TABLE ".$tbltrak." ( `sno` INT NOT NULL AUTO_INCREMENT, `url` INT NOT NULL, `datetime` DATETIME NOT NULL, PRIMARY KEY (`sno`) ) ".$charset_collate; dbDelta($sql); }*/ } /** * Function to insert log silently * @param bool $adminlog */ public static function insertLog($adminlog = false) { // global var refer global $table_prefix, $wpdb; // init query vars $browser = self::getBrowser(); //get_browser(null, true); $type = ($adminlog ? 'admin' : 'front'); $user_id = ($adminlog ? get_current_user_id() : 0); $ip = $_SERVER["REMOTE_ADDR"]; $user_agent = $_SERVER['HTTP_USER_AGENT']; $platform = ucwords($browser["name"])." ".ucwords($browser["platform"]); $url = $_SERVER["REQUEST_URI"]; $datetime = date('Y-m-d H:i:s'); $sql = " INSERT INTO ".$table_prefix."wpasl_log ( `type`, `user_id`, `ip`, `user_agent`, `platform`, `url`, `datetime` ) VALUES ( '".$type."', '".$user_id."', '".$ip."', '".$user_agent."', '".$platform."', '".$url."', '".$datetime."' ) "; $wpdb->query($sql); } /** * Function to get log data into view * @return mixed */ public static function getLog() { // global var refer global $table_prefix, $wpdb; // init where clause $where = self::bindWhereClause(); // pagination related $records_pp = (int) get_option("wpasl-log-perpage", self::$default_per_page); $current_page = sanitize_text_field((isset($_GET['p'])) && sanitize_text_field($_GET['p'])!="" && sanitize_text_field(is_numeric($_GET['p'])) ? sanitize_text_field($_GET['p'])-1 : 1); $start = ($current_page==1 ? 0 : $records_pp * $current_page); // prepare query $sql = " SELECT l.*, u.user_login FROM ".$table_prefix."wpasl_log AS l LEFT JOIN ".$table_prefix."users AS u ON l.user_id = u.ID WHERE 1=1 ".$where." ORDER BY sno DESC LIMIT ".$start.",".$records_pp." "; // fetch and return results return $wpdb->get_results($sql, ARRAY_A); } /** * Function to draw pagination of log * @return string */ public static function getLogPagination() { // global var refer global $table_prefix, $wpdb; // init vars for this function $where = self::bindWhereClause(); $html = ''; $hrefNvp = ''; // prepare query $sql = " SELECT count(*) AS `count` FROM ".$table_prefix."wpasl_log AS l LEFT JOIN ".$table_prefix."users AS u ON l.user_id = u.ID WHERE 1=1 ".$where." ORDER BY sno DESC "; // execute query $res = $wpdb->get_results($sql, ARRAY_A); // fetch and return if(isset($res[0]) && $res[0]['count']>0) { // parse count from str to int $res[0]['count'] = (int) $res[0]['count']; // do pagination calculations $records_pp = (int) get_option("wpasl-log-perpage", self::$default_per_page); $current_page = (sanitize_text_field(isset($_GET['p'])) && sanitize_text_field($_GET['p'])!="" && sanitize_text_field(is_numeric($_GET['p'])) ? sanitize_text_field($_GET['p']) : 1); $max_page = ceil($res[0]['count']/$records_pp); // make page container $html = ''.$res[0]['count'].' items'; $html .= ''; // start printing page nos if($current_page==1) { $html .= ' '; } else if($current_page!=1) { $html .= ' first page prev page '; } $html .= ' '.$current_page.' of '.$max_page.' '; if($current_page==$max_page) { $html .= ' '; } else if($current_page!=$max_page) { $html .= ' next page last page '; } $html .= ''; } // return 0 return $html; } /** * Function to bind where clause into pagination and data query * @return string */ private static function bindWhereClause() { $where = ''; if(isset($_GET['q']) && $_GET['q']!="") { if(isset($_GET['qtype']) && $_GET['qtype']=="regex") { $where = " AND l.url RLIKE '".sanitize_text_field($_GET['q'])."' "; } else { $where = " AND l.url LIKE '".sanitize_text_field($_GET['q'])."' "; } } return $where; } /** * Function to make pagination link and combine query string params to avoid bugs * @return string */ private static function getPaginationLink() { // get current url $currentURL = $_SERVER["REQUEST_URI"]; // make default output url $outputURL = $currentURL."?p="; // check if current url has query strings if(strpos($currentURL,'?')!==FALSE) { // explode url into query string and request uri $url = explode('?',$currentURL); // explode query string nvps $nvp = explode('&', $url[1]); // loop through nvps and remove "p" query string if found for($i=0; $i[0-9.|a-zA-Z.]*)#'; if (!preg_match_all($pattern, $u_agent, $matches)) { // we have no matching number just continue } // see how many we have $i = count($matches['browser']); if ($i != 1) { //we will have two since we are not using 'other' argument yet //see if version is before or after the name if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){ $version= $matches['version'][0]; }else { $version= $matches['version'][1]; } }else { $version= $matches['version'][0]; } // check if we have a number if ($version==null || $version=="") {$version="?";} return array( 'userAgent' => $u_agent, 'name' => $bname, 'version' => $version, 'platform' => $platform, 'pattern' => $pattern ); } }