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']. ''; $TableColumns = array( array('Name' => 'Web', 'Title' => T('Address')), array('Name' => 'MaxDateLast', 'Title' => T('Last visit')), array('Name' => 'TotalHits', 'Title' => T('Hits')), ); if ($User->Licence(LICENCE_ADMIN)) { $TableColumns[] = array('Name' => 'Visible', 'Title' => T('Visible')); $TableColumns[] = array('Name' => 'Parent', 'Title' => T('Parent')); $TableColumns[] = array('Name' => 'Description', 'Title' => T('Comment')); $TableColumns[] = array('Name' => 'LastIP', 'Title' => T('Last IP address')); $TableColumns[] = array('Name' => 'Action', 'Title' => T('Actions')); } $Order = GetOrderTableHeader($TableColumns, 'MaxDateLast', 1); $Output .= $Order['Output']; $Query .= $Order['SQL'].$PageList['SQLLimit']; $DbResult = $this->Database->query($Query); while ($Line = $DbResult->fetch_assoc()) { $Output .= ''. ''. ''; if ($User->Licence(LICENCE_ADMIN)) { $Output .= ''. ''. ''. ''. ''; } $Output .= ''; } $Output .= '
'.$Line['Web'].''.HumanDate($Line['MaxDateLast']).''.$Line['TotalHits'].''.$YesNo[$Line['Visible']].''.$Line['ParentName'].''.$Line['Description'].''.$Line['LastIP'].''.T('Modify').' '. ''.T('Spam').'
'; $Output .= $PageList['Output']; } return $Output; } function SelectParentItem($Selected, $Self) { $Output = ''; return $Output; } function Spam() { $User = ModuleUser::Cast($this->System->GetModule('User'))->User; if ($User->Licence(LICENCE_ADMIN)) { if (array_key_exists('id', $_GET)) { $DbResult = $this->Database->select('Referrer', '*', 'Id='.$_GET['id']); if ($DbResult->num_rows > 0) { $Item = $DbResult->fetch_assoc(); $this->Database->update('Referrer', 'Id='.$_GET['id'], array('Visible' => 0, 'Description' => 'Spam')); $DbResult2 = $this->Database->select('BlackList', '*', 'IP="'.$Item['LastIP'].'"'); if ($DbResult2->num_rows == 0) { $this->Database->insert('BlackList', array('Time' => 'NOW()', 'IP' => $Item['LastIP'])); } $Output = ShowMessage(T('Set as spam'), MESSAGE_CRITICAL); $Output .= $this->ShowList(); } else $Output = ShowMessage(T('Item not found'), MESSAGE_CRITICAL); } else $Output = ShowMessage(T('Item not specified'), MESSAGE_CRITICAL); } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL); return $Output; } function Edit() { $User = ModuleUser::Cast($this->System->GetModule('User'))->User; if ($User->Licence(LICENCE_ADMIN)) { if (array_key_exists('id', $_GET)) { $DbResult = $this->Database->select('Referrer', '*', 'Id='.$_GET['id']); if ($DbResult->num_rows > 0) { $Item = $DbResult->fetch_assoc(); if ($Item['Visible'] == 1) $Visible = ' checked '; else $Visible = ''; $Output = '
'. ''. ''. ''. ''. ''. '
'.T('Web').''.$Item['Web'].'
'.T('Visible').'
'.T('Description').'
'.T('Parent item').''.$this->SelectParentItem($Item['Parent'], $Item['Id']).'
'.T('Last IP address').'
'; } else $Output = ShowMessage(T('Item not found'), MESSAGE_CRITICAL); } else $Output = ShowMessage(T('Item not specified'), MESSAGE_CRITICAL); } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL); return $Output; } function EditSave() { $User = ModuleUser::Cast($this->System->GetModule('User'))->User; if ($User->Licence(LICENCE_ADMIN)) { if ($_POST['Parent'] == '') $_POST['Parent'] = null; $_POST['Visible'] = array_key_exists('Visible', $_POST); $DbResult = $this->Database->update('Referrer', 'Id='.$_GET['id'], array( 'Visible' => $_POST['Visible'], 'LastIP' => $_POST['LastIP'], 'Parent' => $_POST['Parent'], 'Description' => $_POST['Description'])); $_SERVER['QUERY_STRING'] = ''; $Output = ShowMessage(T('Settings saved')); $Output .= $this->ShowList(); } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL); return $Output; } function Show(): string { $this->Title = T('Promotion'); if (array_key_exists('action', $_GET)) { if ($_GET['action'] == 'edit') $Output = $this->Edit(); else if ($_GET['action'] == 'spam') $Output = $this->Spam(); else if ($_GET['action'] == 'editsave') $Output = $this->EditSave(); else $Output = $this->ShowList(); } else $Output = $this->ShowList(); return $Output; } }