Admin Toolbox','manage_options','admin-toolbox','atb_admin');
}
}
add_action('admin_menu','atb_adminMenu');
function atb_admin() {
global $wpdb;
global $atb_version;
include_once('atb_admin.php');
wp_enqueue_style('atb_style',plugins_url('assets/atb_min.css?v=0'.$atb_version,__FILE__));
}
add_shortcode('atb_admin','atb_admin');
function atb_activate($upgrade) {
global $wpdb;
global $atb_version;
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
update_option('atb_db_version',$atb_version,'no');
$sql="
CREATE TABLE {$wpdb->prefix}atb_options
(option_id INT NOT NULL AUTO_INCREMENT
,option_name VARCHAR(191) NULL
,option_value VARCHAR(500)
,PRIMARY KEY (option_id));";
dbDelta($sql);
$sql="
CREATE TABLE {$wpdb->prefix}atb_pagehits
(hit_id INT NOT NULL AUTO_INCREMENT
,date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
,ip VARCHAR(191) NULL
,userid INT
,url VARCHAR(500)
,referrer VARCHAR(500)
,subsq INT DEFAULT NULL
,PRIMARY KEY (hit_id)
,KEY date (date)
,KEY ip (ip));";
dbDelta($sql);
if(function_exists('atb_pro_ping'))atb_pro_ping(2);
?>
Admin Toolbox plugin is active, but is not yet configured. Visit the configuration page to complete setup.",'Admin Toolbox');?>
get_results("SELECT 1 FROM {$wpdb->prefix}atb_options HAVING COUNT(*)>1;",OBJECT);
if(!$get_config) add_action('admin_notices','atb_admin_notice');
}
add_action('admin_init','atb_checkConfig');
function atb_add_action_links($links) {
$settings_url=get_admin_url(null,'admin.php?page=admin-toolbox');
$support_url='https://richardlerma.com/plugins/';
$links[]='Support';
array_push($links,'Settings');
return $links;
}
add_filter('plugin_action_links_'.plugin_basename(__FILE__),'atb_add_action_links');
function atb_uninstall() {
global $wpdb;
$get_config=$wpdb->get_results("SELECT 1 FROM {$wpdb->prefix}atb_options WHERE option_name='page_hit_keep' AND option_value>0;",OBJECT);
if(!$get_config) {
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}atb_options;");
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}atb_pagehits;");
delete_option('atb_db_version');
}
}
register_uninstall_hook(__FILE__,'atb_uninstall');
function atb_admin_impact() {global $wpdb;include_once('atb_admin_impact.php');}
add_action('admin_init','atb_admin_impact');
function atb_login_impact() {global $wpdb;include_once('atb_login_impact.php');}
add_action('login_enqueue_scripts','atb_login_impact');
function atb_frontend_impact() {
global $wpdb;
include_once('atb_frontend_impact.php');
}
if(!is_admin()) add_action('init','atb_frontend_impact');
function atb_cui($userid=0) {
global $current_user;
include_once(ABSPATH .'wp-includes/pluggable.php'); // If called prior to pluggable loaded natively
if(current_user_can('manage_options') && $userid>0) return $userid;
$current_user=wp_get_current_user();
$userid=$current_user->ID;
return $userid;
}
function atb_cur($user_id=0) {
$user_role='';
include_once(ABSPATH .'wp-includes/pluggable.php'); // If called prior to pluggable loaded natively
if(is_user_logged_in()) {
$user_id=atb_cui($user_id);
$user_info=get_userdata($user_id);
$user_role=$user_info->roles;
}
return $user_role;
}
function atb_r ($q,$t=NULL) {
include_once(ABSPATH .'wp-includes/pluggable.php'); // If called prior to pluggable loaded natively
require_once(ABSPATH .'wp-includes/wp-db.php');
global $wpdb;
if(!$wpdb) $wpdb=new wpdb(DB_USER,DB_PASSWORD,DB_NAME,DB_HOST);
$prf=$wpdb->prefix;
$s=str_replace(' wp_',' '.$prf,$q);
$s=str_replace($prf.str_replace('wp_','',$prf),$prf,$s);
if(strpos($s,'DELETE')!==false || strpos($s,'INSERT')!==false) $r=$wpdb->query($s); else $r=$wpdb->get_results($s,OBJECT);
if($t) {echo $wpdb->last_error." ";echo $s;}
if($r) return $r;
}
function atb_is_path($pages) {
$page_array=explode(',',$pages);
$current_page=strtolower($_SERVER['REQUEST_URI']);
foreach($page_array as $page) {
if(strpos($current_page,strtolower($page))!==false) return true;
}
return false;
}
// PageHit Action
function atb_hit_page() {
if(atb_is_path('toolbox&phv=1,ajax')!==false) return;
global $current_user;
$userid=0;
$ip=sanitize_text_field($_SERVER['REMOTE_ADDR']);
$url=sanitize_text_field(substr($_SERVER['REQUEST_URI'],0,250));
if(!get_transient("atb_flag_$ip:".atb_cui())) if(is_user_logged_in()) $userid=atb_cui();
$page=get_transient("atb_$ip:$userid");
if($url!=$page) {
$exclude_pages=atb_r("SELECT option_name,option_value FROM wp_atb_options WHERE option_name LIKE 'page_hit_page%';");
if($exclude_pages) {
foreach($exclude_pages as $row):
if($row->option_name=='page_hit_page_exclude') $page_hit_page_exclude=$row->option_value;
if($row->option_name=='page_hit_page_frag') $page_hit_page_frag=explode(" ",$row->option_value);
endforeach;
if($page_hit_page_exclude>0 && !empty($page_hit_page_frag)) {
$current_page=strtolower($_SERVER['REQUEST_URI']);
foreach($page_hit_page_frag as $frag) if(!empty($frag)) {if(strpos($current_page,strtolower($frag))!==false) return;}
}
}
if(isset($_SERVER['HTTP_REFERER'])) $referrer=sanitize_text_field(substr($_SERVER['HTTP_REFERER'],0,250)); else $referrer='';
set_transient("atb_$ip:$userid",$url,3600);
atb_r("INSERT INTO wp_atb_pagehits(ip,userid,url,referrer) VALUES ('$ip','$userid','$url','$referrer');");
} else atb_r("UPDATE wp_atb_pagehits SET subsq=IFNULL(subsq,0)+1 WHERE ip='$ip' AND userid='$userid' AND url='$url' ORDER BY hit_id DESC LIMIT 1;");
}
// Max Image Size Err Msg
function atb_media_size_msg() { ?>
0) return true;
if(atb_is_path('/wp-admin') && !atb_is_path('/wp-admin/plugins.php')) {
$user_roles=atb_cur();
if(empty($user_roles)) $user_roles=array();
$get_limit=atb_r("SELECT option_name,option_value FROM wp_atb_options WHERE option_name LIKE 'limit_img_size%';");
$limit_img_size=0;
$limit_img_size_kb=0;
if($get_limit):
foreach($get_limit as $row):
if($row->option_name=='limit_img_size') $limit_img_size=$row->option_value;
if($row->option_name=='limit_img_size_role') $limit_img_size_role=explode(",",$row->option_value);
if($row->option_name=='limit_img_size_role_exclude') $limit_img_size_role_exclude=$row->option_value;
if($row->option_name=='limit_img_size_kb') $limit_img_size_kb=$row->option_value;
endforeach;
endif;
if($limit_img_size==1 && $limit_img_size_kb>0) {
if(($limit_img_size_role_exclude==0 && (count(array_intersect($limit_img_size_role,$user_roles))>0 || in_array('*All',$limit_img_size_role,true)))
|| ($limit_img_size_role_exclude==1 && (count(array_intersect($limit_img_size_role,$user_roles))==0 || in_array('*All',$limit_img_size_role,true)))) {
set_transient('limit_img_size_kb_'.atb_cui(),$limit_img_size_kb,1100);
return true;
}
}
}
return false;
}
function atb_media_limit() {
atb_check_media_limit();
add_action('admin_head','atb_media_size_msg');
$kb=get_transient('limit_img_size_kb_'.atb_cui())*1000;
return $kb;
}
if(atb_check_media_limit()) add_filter('upload_size_limit','atb_media_limit');
// Remove other plugins' login features if logged in
if(!atb_is_path('/wp-json')) if(atb_cui()>0) remove_all_actions('login_init');
// Configure Login Page
function atb_login_init() {
if(atb_is_path('action=logout')) return;
if(atb_is_path('action=lostpassword') || atb_is_path('2fa=1')) {
if(is_user_logged_in()) {
remove_all_actions('login_init'); // Remove other plugins' features
add_action('login_init','atb_prompt_token');
}
} else add_action('login_init','atb_login_redirect');
}
add_action('wp_loaded','atb_login_init',0); // Renamed Login Page (AIOWPS)
add_action('wp_loaded','atb_login_init',999); // Standard wp-login.php
// Add Token on Login
function atb_create_token($login='') {
$newcode=uniqid();
if(!empty($login)) {$user=get_user_by('login',$login); $userid=$user->ID;} else $userid=atb_cui();
set_transient('atb_token_'.$userid,strtoupper(substr($newcode,8,5)),3600);
$ip=sanitize_text_field($_SERVER['REMOTE_ADDR']);
set_transient("atb_flag_$ip:".$userid,1,0);
}
add_action('wp_login','atb_create_token');
// Email Functions
function atb_mail_content_type() {return 'text/html';}
function atb_mail_from($email) {return get_bloginfo('admin_email');}
function atb_mail_name($name) {return get_bloginfo('name');}
function atb_html_mail($to,$subject,$message) {
add_filter('wp_mail_content_type','atb_mail_content_type');
add_filter('wp_mail_from','atb_mail_from');
add_filter('wp_mail_from_name','atb_mail_name');
if(empty($to)) $to=get_bloginfo('admin_email');
wp_mail($to,$subject,$message);
remove_filter('wp_mail_content_type','atb_mail_content_type');
}
function atb_email_token($target) {
$ip=sanitize_text_field($_SERVER['REMOTE_ADDR']);
$user_info=get_userdata(atb_cui());
$title=get_bloginfo('name');
$to=$user_info->user_email;
if(!get_transient('atb_target_'.atb_cui())) set_transient('atb_target_'.atb_cui(),$target,3600);
if(atb_is_path('rsd=2')) atb_create_token(); // regen token
if(atb_is_path('rsd=1')) set_transient("atb_flag_$ip:".atb_cui(),1,0); // resend token
if(get_transient("atb_flag_$ip:".atb_cui())==1) { // if token not yet sent
if(strpos($to,'@')!==false) { // check for valid email
if(!get_transient('atb_token_'.atb_cui())) atb_create_token();
require_once(ABSPATH.WPINC.'/pluggable.php');
$message=get_transient('atb_token_'.atb_cui())." is your $title verification code.";
if(function_exists('atb_text')) $sent=atb_text($user_info->ID,$message);
if(!$sent) {
$subject=$title.' Verification Code';
$name=$user_info->first_name;
$intro="Dear ".$name."
";
atb_html_mail($to,$subject,$intro.$message);
$to=substr($to,0,3)."81818".substr($to,-6);
set_transient('atb_prompt_mobile_'.atb_cui(),1,3600);
} else $to=substr($sent,0,3)."8181818".substr($sent,-2);
set_transient("atb_flag_$ip:".atb_cui(),2,0);
set_transient('atb_sent_to_'.atb_cui(),$to,3600);
} else delete_transient("atb_flag_$ip:".atb_cui());
}
wp_redirect(wp_login_url()."?action=lostpassword&2fa=1#token"); exit;
}
function atb_dual_auth($ip_exclude) {
if(strlen(trim($_SERVER['REMOTE_ADDR']))>6) $ip=trim($_SERVER['REMOTE_ADDR']); else $ip='na';
if(get_transient("atb_flag_$ip:".atb_cui())!==false && atb_is_path('/wp-admin')) {
if(function_exists('atb_check_abuse')) {
$abuse_score=0; $abuse_score=atb_check_abuse($ip);
if($abuse_score>5) {
atb_report_abuse($ip,'15','Attempted to use stolen credentials to access CMS.');
wp_redirect(wp_login_url()."?action=lostpassword&2fa=1#token"); exit;
}
}
$prev_ip=atb_r("SELECT group_concat(DISTINCT ip) as ip FROM wp_atb_pagehits WHERE userid=".atb_cui()." AND date>NOW()-INTERVAL 8 DAY;");
if($prev_ip) foreach($prev_ip as $prev) $ip_exclude.=','.$prev->ip; else $ip_exclude='';
if(atb_is_path('ajax')) $uri='/wp-admin/'; else $uri=$_SERVER['REQUEST_URI'];
if(empty($ip_exclude) || (!empty($ip_exclude) && strpos($ip_exclude,$ip)===false)) atb_email_token($_SERVER['HTTP_HOST'].$uri); else delete_transient("atb_flag_$ip:".atb_cui());
}
}
// Show Token Entry Form
function atb_prompt_token() {
if(atb_is_path('2fa=1')) {
if(!empty($_POST['token'])) $token=sanitize_text_field($_POST['token']); else $token=false;
$ip=sanitize_text_field($_SERVER['REMOTE_ADDR']);
if(!get_transient("atb_flag_$ip:".atb_cui())) atb_login_redirect();
if(($token && (strtolower(get_transient('atb_token_'.atb_cui()))==strtolower($token)))) {
$target=get_transient('atb_target_'.atb_cui());
if(get_transient('atb_prompt_mobile_'.atb_cui())!==false) if(strpos($target,'?')===false) $target.="?atb_mob=1"; else $target.="&atb_mob=1";
delete_transient('atb_prompt_mobile_'.atb_cui());
delete_transient('atb_target_'.atb_cui());
delete_transient("atb_flag_$ip:".atb_cui());
delete_transient('atb_token_'.atb_cui());
delete_transient('atb_sent_to_'.atb_cui());
wp_redirect(atb_login_redirect($target)); exit;
} else {
if($token) $error_msg="
ERROR: Invalid Code
"; else $error_msg=''; ?>
Two Factor Authentication
General
if($base!=$site) { // Fix AIOWPS Redirect bug
global $wpdb;
$get_config=$wpdb->get_results("SELECT 1 FROM {$wpdb->prefix}atb_options WHERE option_name='match_redirect' AND option_value>0;",OBJECT);
if(!$get_config) return;
if(atb_is_path('wp-admin')) wp_redirect(admin_url().$_SERVER['REQUEST_URI']); else wp_redirect(site_url().$_SERVER['REQUEST_URI']);
exit;
}
}
add_action('wp_loaded','atb_match_redirect',10);
//Redirect to HTTPS
function atb_https_redirect() {
if(empty($_SERVER['HTTPS'])) {
global $wpdb;
$get_config=$wpdb->get_results("SELECT 1 FROM {$wpdb->prefix}atb_options WHERE option_name='https_redirect' AND option_value>0",OBJECT);
if(!$get_config) return;
header("Location: https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
exit;
}
}
add_action('wp_loaded','atb_https_redirect',10);
// Compress PageHits
function atb_compress_hits() {
$r=atb_r("DELETE pg
FROM wp_atb_pagehits pg
JOIN (
SELECT MAX(hit_id) as hit_id
FROM wp_atb_pagehits
WHERE userid<1
AND date < NOW()-INTERVAL 90 DAY
AND date > NOW()-INTERVAL 111 DAY
GROUP BY LEFT(date,13),ip,userid,url,referrer
HAVING COUNT(ip)>1
ORDER BY hit_id ASC
LIMIT 10000
)mx ON mx.hit_id=pg.hit_id;");
return intval($r);
}
// PageHit Summary
function atb_view_summary($atb_version_type) {
if(get_transient('limit_img_size_kb_'.atb_cui())!==false) delete_transient('limit_img_size_kb_'.atb_cui());
$current_mth=current_time('Y-m-01');
$last_gen_mth=get_option('atb_mt_max');
if(!$last_gen_mth) $last_gen_mth=get_transient('atb_mt_max');
if(!$last_gen_mth) $last_gen_mth=$current_mth;
$mth=get_option('atb_mt');
if(!$mth) $mth=get_transient('atb_mt'); // Find Historical Month Stats
function atb_month_stat($params) {
$query="
SELECT DATE_FORMAT(CONCAT(month,'-01'),'%b %y') as month
,month as month_id,visitors
,ROUND(visitors/CASE WHEN month=DATE_FORMAT(NOW(),'%Y-%m') THEN DAY(NOW()) ELSE 30 END,0) as day_avg
FROM (
SELECT LEFT(date,7) as month,COUNT(DISTINCT ip) as visitors
FROM wp_atb_pagehits
WHERE date>NOW()-INTERVAL 400 DAY $params
GROUP BY LEFT(date,7)
)a
ORDER BY DATE_FORMAT(CONCAT(month,'-01'),'%y-%m');";
$mth=atb_r($query);
return $mth;
}
// Create Historical Month Stats
if(!$mth) {
$mth=atb_month_stat("AND date<'$current_mth'");
update_option('atb_mt',$mth,false);
update_option('atb_mt_max',$current_mth,false);
}
// Get Month Stats between last Gen Date and Month Begin
if(!$mth || $current_mth>$last_gen_mth) {
$mth_new=atb_month_stat("AND date>='$last_gen_mth' AND date<'$current_mth'");
if($mth && $mth_new) {
$mth=array_merge($mth,$mth_new); // Append Historical with Current
update_option('atb_mt',$mth,false);
update_option('atb_mt_max',$current_mth,false);
delete_transient('atb_mt');
delete_transient('atb_mt_max');
}
}
// Get Month Stats greater than Month Begin
$mth_new=atb_month_stat("AND date>='$current_mth'");
if(!$mth)$mth=$mth_new;
elseif($mth_new)$mth=array_merge($mth,$mth_new); // Append Historical with Today
$current_day=current_time('Y-m-d');
$last_gen_day=get_option('atb_dy_max');
if(!$last_gen_day) $last_gen_day=get_transient('atb_dy_max');
if(!$last_gen_day) $last_gen_day=$current_day;
$day=get_option('atb_dy');
if(!$day) $day=get_transient('atb_dy'); // Find Historical Day Stats
function atb_day_stat($params) {
$query="
SELECT DATE_FORMAT(date,'%a %D') as day
,date as date_id
,DATE_FORMAT(CONCAT(date,'-01'),'%b %y') as month,visitors
,visitors
FROM (
SELECT LEFT(date,10) as date,COUNT(DISTINCT ip) as visitors
FROM wp_atb_pagehits
WHERE date>NOW()-INTERVAL 400 DAY $params
GROUP BY LEFT(date,10)
)a
ORDER BY date;";
$day=atb_r($query);
return $day;
}
// Create Historical Day Stats
if(!$day) {
$day=atb_day_stat("AND date<'$current_day'");
update_option('atb_dy',$day,false);
update_option('atb_dy_max',$current_day,false);
}
// Get Day Stats between last Gen Date and Today
if(!$day || $current_day>$last_gen_day) {
$day_new=atb_day_stat("AND date>='$last_gen_day' AND date<'$current_day' ");
if($day && $day_new){ // Append Historical with Current
$day=array_merge($day,$day_new);
update_option('atb_dy',$day,false);
update_option('atb_dy_max',$current_day,false);
delete_transient('atb_dy');
delete_transient('atb_dy_max');
}
}
// Get Day Stats Today
$day_new=atb_day_stat("AND date>='$current_day'");
if(!$day) $day=$day_new;
elseif($day_new)$day=array_merge($day,$day_new); // Append Historical with Today
$max_visit_avg=0;
$max_visit_tot=0;
if($mth) foreach($mth as $mth_item) if($mth_item->day_avg>$max_visit_avg) {$max_visit_avg=$mth_item->day_avg; $max_visit_tot=$mth_item->visitors;} ?>