Name = 'Referrer';
$this->Version = '1.0';
$this->Creator = 'Chronos';
$this->License = 'GNU/GPL';
$this->Description = 'Log visitor HTTP referrer URLs to database for later evaluation.';
$this->Dependencies = array();
$this->Excludes = array();
}
function DoStart(): void
{
$this->Excludes[] = Core::Cast($this->System)->Config['Web']['Host'];
$this->Log();
$this->System->RegisterPage(['referrer'], 'PageReferrer');
Core::Cast($this->System)->RegisterMenuItem(array(
'Title' => T('Promotion'),
'Hint' => 'Informace k propagaci tohoto projektu',
'Link' => $this->System->Link('/referrer/'),
'Permission' => LICENCE_ANONYMOUS,
'Icon' => '',
));
}
function Log()
{
if (array_key_exists('HTTP_REFERER', $_SERVER))
{
$Referrer = addslashes($_SERVER['HTTP_REFERER']);
$HostName = substr($Referrer, strpos($Referrer, '/') + 2);
$HostName = substr($HostName, 0, strpos($HostName, '/'));
if (!in_array($HostName, $this->Excludes))
{
$IP = GetRemoteAddress();
// Check if client IP is not blaclisted as spam source. If yes then add new referrer as invisible
$DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `BlackList` WHERE `IP` = "'.$IP.'"');
$DbRow = $DbResult->fetch_row();
if ($DbRow[0] == 0)
{
$Visible = '1';
$Description = '';
} else
{
$Visible = '0';
$Description = 'Spam';
}
// Check if host name is already stored and need just to update hit counter
$DbResult = $this->System->Database->query('SELECT `Id` FROM `Referrer` WHERE `Web` = "'.$HostName.'"');
if ($DbResult->num_rows > 0)
{
$DbRow = $DbResult->fetch_assoc();
$this->System->Database->query('UPDATE `Referrer` SET `Hits` = `Hits` + 1, `DateLast` = NOW(), `LastURL` = "'.
addslashes($Referrer).'", `LastIP` = "'.$IP.'" WHERE `Id` = '.$DbRow['Id']);
} else
{
$this->System->Database->query('INSERT INTO `Referrer` (`Web`, `DateFirst`, `DateLast`, `LastURL`, `Hits`, `LastIP`, `Visible`, `Description`) '.
'VALUES ("'.$HostName.'", NOW(), NOW( ), "'.addslashes($Referrer).'", 1, "'.$IP.'", '.$Visible.', "'.$Description.'")');
}
}
}
}
}
class PageReferrer extends Page
{
function ShowList()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
$Banner = ''.
'';
$BannerSmall = ''.
'';
$Output = ''.T('Banners').': ';
$Output .= $Banner.' ';
$Output .= $BannerSmall.' ';
if ($User->Licence(LICENCE_ADMIN))
{
$MonthAge = 3;
$YesNo = array('Ne', 'Ano');
$Output .= ' '.T('Servers referring to us').': '.
'
Seznam je automaticky aktualizován a zobrazeny jsou servery, ze kterých přišli uživatelé během posledních třech měsíců řazený sestupně dle nejnovějších.
';
if (!$User->Licence(LICENCE_ADMIN)) $Where = ' WHERE (`Visible`=1) AND (`Parent` IS NULL)';
else $Where = '';
$Query = 'SELECT *, (SELECT Web FROM `Referrer` AS T4 WHERE T4.Id = T3.Parent) AS ParentName '.
'FROM (SELECT *, '.
'(`Hits` + COALESCE((SELECT SUM(`Hits`) FROM '.
'`Referrer` AS `T1` WHERE `T1`.`Parent` = `T2`.`Id`), 0)) AS `TotalHits`, '.
'GREATEST(`DateLast`, COALESCE((SELECT MAX(`DateLast`) FROM '.
'`Referrer` AS `T1` WHERE `T1`.`Parent` = `T2`.`Id`), 0)) AS `MaxDateLast` FROM '.
'`Referrer` AS `T2` '.$Where.') AS `T3` '.
'WHERE (`T3`.`MaxDateLast` > (NOW() - INTERVAL '.$MonthAge.' MONTH))';
$DbResult = $this->Database->query('SELECT COUNT(*) FROM ('.$Query.') AS T');
$DbRow = $DbResult->fetch_row();
$PageList = GetPageList($DbRow[0]);
$Output .= $PageList['Output'].
'