| Server IP : 89.39.208.18 / Your IP : 216.73.217.74 Web Server : LiteSpeed System : User : ( ) PHP Version : 7.4.33 Disable Function : eval,./,ln,passthru,dl,system,exec,popen,shell_exec,posix_getpwuid,posix_uname,ftp_exec,posix_uname,posix_getpwuid,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,get_current_user,getmyuid,getmygid,listen,chgrp,apache_note,apache_setenv,apache_child_terminate,closelog,debugger_off,debugge_on,ini_restore,netscript,escapeshellarg,cmd,backtick,virtual,show_sourc,show_source,pclose,pcntl_exec,datasec,old_offset,ctrl_dir,ini_alter,leak,listen,chgrp,apache_setenv,define_syslog_variables,root,diskfreespace,disk_free_space,disk_total_space,posix_kill,getmyuid,getmygid,apache_child_terminate,php_ini_scanned_files,ls,ps_aux,chown,getrusage,posixc,posame,chgrp,posix_setuid,posix_setsid,posix_setgid,apache_note,apache_setenv,x_getuid,e_ini_file,nfo,SQL,mysql_li,t_dbs,filegroup,highlight_file,ilegetcontents,get_dir,popen,popens,pfsockopen,dos_conv,apache_get_modules,crack_check,crack_closedict,rar_open,bzopen,bzread,bzwrite,shellcode,posix_isatty,posix_getservbyname,escapeshellarg,hypot,pg_host,pos,posix_access,inurl,posix_times,posix_mknod,pclose,ps_fill,symlink,id,escapeshellcmd,php_uname,phpinfo MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /home3/mahyarcake/www/wp-content/plugins/wp-statistics/includes/ |
Upload File : |
<?php
namespace WP_STATISTICS;
class Referred
{
/**
* Top Referring Transient name
*
* @var string
*/
public static $top_referring_transient = 'wps_top_referring';
/**
* Referral Url Details Option name
*
* @var string
*/
public static $referral_detail_opt = 'wp_statistics_referrals_detail';
/**
* Referrer Spam List
*
* @var string
*/
public static $referrer_spam_link = 'https://cdn.jsdelivr.net/gh/matomo-org/referrer-spam-list@master/spammers.txt';
/**
* Referred constructor.
*/
public function __construct()
{
# Remove Cache When Delete Visitor Table
add_action('wp_statistics_truncate_table', array($this, 'deleteCacheData'));
}
/**
* Get referer URL
*
* @return string
*/
public static function getRefererURL()
{
if (Helper::is_rest_request() && isset($_REQUEST['referred'])) {
return sanitize_url(wp_unslash($_REQUEST['referred']));
}
return (isset($_SERVER['HTTP_REFERER']) ? sanitize_url(wp_unslash($_SERVER['HTTP_REFERER'])) : '');
}
/**
* Return the referrer link for the current user.
*
* @return array|bool|string
*/
public static function get()
{
// Get Default
$referred = self::getRefererURL();
// Sanitize Referer Url
$referred = esc_url_raw(wp_strip_all_tags($referred));
// If Referer is empty, set only ''
if (empty($referred)) {
$referred = '';
}
$referred = Helper::FilterQueryStringUrl($referred, Helper::get_query_params_allow_list());
return apply_filters('wp_statistics_user_referer', $referred);
}
/**
* Get referrer link
*
* @param string $referrer
* @param string $title
* @param bool $is_blank
* @return string | void
*/
public static function get_referrer_link($referrer, $title = '', $is_blank = false)
{
// Sanitize Link
$html_referrer = self::html_sanitize_referrer($referrer);
// Check Url Protocol
if (!Helper::check_url_scheme($html_referrer)) {
$html_referrer = '//' . $html_referrer;
}
$html_referrer = esc_url($html_referrer);
// Parse Url
$base_url = @wp_parse_url($html_referrer);
// Get Page title
$title = (trim($title) == "" ? $html_referrer : $title);
// If referrer is the current site or empty, return empty string
if (empty($base_url['host']) || strpos($referrer, site_url()) !== false) {
return \WP_STATISTICS\Admin_Template::UnknownColumn();
}
// Get Html Link
return "<a href='{$html_referrer}' title='{$title}'" . ($is_blank === true ? ' target="_blank"' : '') . ">{$base_url['host']}</a>";
}
/**
* Sanitizes the referrer
*
* @param $referrer
* @param int $length
* @return string
*/
public static function html_sanitize_referrer($referrer, $length = -1)
{
$referrer = trim($referrer);
if ('data:' == strtolower(substr($referrer, 0, 5))) {
$referrer = 'http://127.0.0.1';
}
if ('javascript:' == strtolower(substr($referrer, 0, 11))) {
$referrer = 'http://127.0.0.1';
}
if ($length > 0) {
$referrer = substr($referrer, 0, $length);
}
return $referrer;
}
/**
* Get Number Referer Domain
*
* @param $url
* @param string $type [list|number]
* @param array $time_rang
* @param null $limit
* @return array
* @throws \Exception
*/
public static function get_referer_from_domain($url, $type = 'number', $time_rang = array(), $limit = null)
{
global $wpdb;
//Get Domain Name
$search_url = Helper::get_domain_name($url);
//Prepare SQL
$time_sql = '';
if (count($time_rang) > 0 and !empty($time_rang)) {
$time_sql = sprintf("AND `last_counter` BETWEEN '%s' AND '%s'", $time_rang[0], $time_rang[1]);
}
$sql = $wpdb->prepare("SELECT " . ($type == 'number' ? 'COUNT(*)' : '*') . " FROM `" . DB::table('visitor') . "` WHERE `referred` REGEXP \"^(https?://|www\\.)[\.A-Za-z0-9\-]+\\.[a-zA-Z]{2,4}\" AND referred <> '' AND LENGTH(referred) >=12 AND (`referred` LIKE %s OR `referred` LIKE %s OR `referred` LIKE %s OR `referred` LIKE %s) " . $time_sql . " ORDER BY `" . DB::table('visitor') . "`.`ID` DESC " . ($limit != null ? " LIMIT " . $limit : "") . "", 'https://www.' . $wpdb->esc_like($search_url) . '%', 'https://' . $wpdb->esc_like($search_url) . '%', 'http://www.' . $wpdb->esc_like($search_url) . '%', 'http://' . $wpdb->esc_like($search_url) . '%'); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
//Get Count
return ($type == 'number' ? $wpdb->get_var($sql) : Visitor::prepareData($wpdb->get_results($sql))); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
/**
* Downloads the referrer spam database
*
* @see https://github.com/matomo-org/referrer-spam-blacklist.
* @return string
*/
public static function download_referrer_spam()
{
if (Option::get('referrerspam') == false) {
return '';
}
// Download the file from MaxMind, this places it in a temporary location.
$response = wp_remote_get(self::$referrer_spam_link, array('timeout' => 60));
if (is_wp_error($response)) {
return false;
}
$referrerspamlist = wp_remote_retrieve_body($response);
if (is_wp_error($referrerspamlist)) {
return false;
}
if ($referrerspamlist != '' || Option::get('referrerspamlist') != '') {
Option::update('referrerspamlist', $referrerspamlist);
}
return true;
}
/**
* Get WebSite IP Server And Country Name
*
* @param $url string domain name e.g : wp-statistics.com
* @return array
* @throws \Exception
*/
public static function get_domain_server($url)
{
//Create Empty Object
$result = array('ip' => '', 'country' => '');
//Get Ip by Domain
if (function_exists('gethostbyname')) {
// Get Host Domain
$ip = gethostbyname($url);
// Check Validate IP
if (filter_var($ip, FILTER_VALIDATE_IP)) {
$result['ip'] = $ip;
$result['country'] = GeoIP::getCountry($ip);
}
}
return $result;
}
/**
* Get Top Referring Site
*
* @param int $number
* @return array
* @throws \Exception
*/
public static function getTop($number = 10)
{
global $wpdb;
//Get Top Referring
if (false === ($get_urls = get_transient(self::$top_referring_transient))) {
$sql = $wpdb->prepare("ORDER BY `number` DESC LIMIT %d", $number);
$result = $wpdb->get_results(self::generateReferSql($sql, '')); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
foreach ($result as $items) {
$get_urls[$items->domain] = self::get_referer_from_domain($items->domain);
}
// Put the results in a transient. Expire after 12 hours.
set_transient(self::$top_referring_transient, $get_urls, 20 * HOUR_IN_SECONDS);
}
// Return Data
return self::PrepareReferData($get_urls);
}
/**
* Prepare Refer Data
*
* @param $get_urls
* @return array
* @throws \Exception
*/
public static function PrepareReferData($get_urls)
{
//Prepare List
$list = array();
//Load country Code
$ISOCountryCode = Country::getList();
//Get Refer Site Detail
$refer_opt = get_option(self::$referral_detail_opt);
$referrer_list = (empty($refer_opt) ? array() : $refer_opt);
if (!$get_urls) {
return array();
}
// Check List
foreach ($get_urls as $domain => $number) {
//Get Site Link
$referrer_html = Referred::html_sanitize_referrer($domain);
//Get Site information if Not Exist
if (!array_key_exists($domain, $referrer_list)) {
$get_site_inf = Referred::get_domain_server($domain);
$get_site_title = Helper::get_site_title_by_url($domain);
$referrer_list[$domain] = array(
'ip' => $get_site_inf['ip'],
'country' => $get_site_inf['country'],
'title' => ($get_site_title === false ? '' : $get_site_title),
);
}
// Push to list
$list[] = array(
'domain' => $domain,
'title' => $referrer_list[$domain]['title'],
'ip' => ($referrer_list[$domain]['ip'] != "" ? $referrer_list[$domain]['ip'] : '-'),
'country' => ($referrer_list[$domain]['country'] != "" ? $ISOCountryCode[$referrer_list[$domain]['country']] : ''),
'flag' => ($referrer_list[$domain]['country'] != "" ? Country::flag($referrer_list[$domain]['country']) : ''),
'page_link' => Menus::admin_url('referrers', array('referrer' => $referrer_html)),
'number' => number_format_i18n($number)
);
}
//Save Referrer List Update
update_option(self::$referral_detail_opt, $referrer_list, 'no');
// Return Data
return $list;
}
/**
* Get Referred Site List
*
* @param array $args
* @return mixed
*/
public static function getList($args = array())
{
global $wpdb;
// Check Custom Date
$where = '';
if (isset($args['from']) and isset($args['to'])) {
$where = "AND `last_counter` BETWEEN '" . $args['from'] . "' AND '" . $args['to'] . "' ";
}
// Check Min Number
$having = '';
if (isset($args['min'])) {
$having = "HAVING `number` >" . $args['min'];
}
// Check Limit
$limit = '';
if (isset($args['limit'])) {
$limit = "LIMIT " . $args['limit'];
}
// Return List
return $wpdb->get_results(self::generateReferSql($having . " ORDER BY `number` DESC " . $limit, $where)); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
/**
* Generate Basic SQL Refer List
*
* @param string $where
* @param string $extra
* @return string
*/
public static function generateReferSql($extra = '', $where = '')
{
// Check Protocol Of domain
$domain_name = rtrim(preg_replace('/^https?:\/\//', '', get_site_url()), " / ");
foreach (array("http", "https", "ftp") as $protocol) {
foreach (array('', 'www.') as $w3) {
$where .= " AND `referred` NOT LIKE '{$protocol}://{$w3}{$domain_name}%' ";
}
}
// Return SQL
return "SELECT SUBSTRING_INDEX(REPLACE( REPLACE( referred, 'http://', '') , 'https://' , '') , '/', 1 ) as `domain`, count(referred) as `number` FROM " . DB::table('visitor') . " WHERE `referred` REGEXP \"^(https?://|www\\.)[\.A-Za-z0-9\-]+\\.[a-zA-Z]{2,4}\" AND referred <> '' AND LENGTH(referred) >=12 " . $where . " GROUP BY domain " . $extra;
}
/**
* Remove Complete Cache Data
* @param $table_name
*/
public function deleteCacheData($table_name)
{
if ($table_name == "visitor") {
delete_transient(self::$top_referring_transient);
delete_option(self::$referral_detail_opt);
}
}
}
new Referred();