source: trunk/Modules/Referrer/Referrer.php

Last change on this file was 900, checked in by chronos, 7 months ago
  • Fixed: More form input fields validation.
  • Modified: Code cleanup.
File size: 10.9 KB
Line 
1<?php
2
3class ModuleReferrer extends Module
4{
5 var $Excludes;
6
7 function __construct(System $System)
8 {
9 parent::__construct($System);
10 $this->Name = 'Referrer';
11 $this->Version = '1.0';
12 $this->Creator = 'Chronos';
13 $this->License = 'GNU/GPL';
14 $this->Description = 'Log visitor HTTP referrer URLs to database for later evaluation.';
15 $this->Dependencies = array();
16
17 $this->Excludes = array();
18 }
19
20 function DoStart(): void
21 {
22 $this->Excludes[] = Core::Cast($this->System)->Config['Web']['Host'];
23 $this->Log();
24 $this->System->RegisterPage(['referrer'], 'PageReferrer');
25 Core::Cast($this->System)->RegisterMenuItem(array(
26 'Title' => T('Promotion'),
27 'Hint' => 'Informace k propagaci tohoto projektu',
28 'Link' => $this->System->Link('/referrer/'),
29 'Permission' => LICENCE_ANONYMOUS,
30 'Icon' => '',
31 ));
32 }
33
34 function Log()
35 {
36 if (array_key_exists('HTTP_REFERER', $_SERVER))
37 {
38 $Referrer = addslashes($_SERVER['HTTP_REFERER']);
39 $HostName = substr($Referrer, strpos($Referrer, '/') + 2);
40 $HostName = substr($HostName, 0, strpos($HostName, '/'));
41 if (!in_array($HostName, $this->Excludes))
42 {
43 $IP = GetRemoteAddress();
44
45 // Check if client IP is not blaclisted as spam source. If yes then add new referrer as invisible
46 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `BlackList` WHERE `IP` = "'.$IP.'"');
47 $DbRow = $DbResult->fetch_row();
48 if ($DbRow[0] == 0)
49 {
50 $Visible = '1';
51 $Description = '';
52 } else
53 {
54 $Visible = '0';
55 $Description = 'Spam';
56 }
57
58 // Check if host name is already stored and need just to update hit counter
59 $DbResult = $this->System->Database->query('SELECT `Id` FROM `Referrer` WHERE `Web` = "'.$HostName.'"');
60 if ($DbResult->num_rows > 0)
61 {
62 $DbRow = $DbResult->fetch_assoc();
63 $this->System->Database->query('UPDATE `Referrer` SET `Hits` = `Hits` + 1, `DateLast` = NOW(), `LastURL` = "'.
64 addslashes($Referrer).'", `LastIP` = "'.$IP.'" WHERE `Id` = '.$DbRow['Id']);
65 } else
66 {
67 $this->System->Database->query('INSERT INTO `Referrer` (`Web`, `DateFirst`, `DateLast`, `LastURL`, `Hits`, `LastIP`, `Visible`, `Description`) '.
68 'VALUES ("'.$HostName.'", NOW(), NOW( ), "'.addslashes($Referrer).'", 1, "'.$IP.'", '.$Visible.', "'.$Description.'")');
69 }
70 }
71 }
72 }
73}
74
75class PageReferrer extends Page
76{
77 function ShowList()
78 {
79 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
80 $Banner = '<a href="https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/').'">'.
81 '<img src="https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/banners/wowpreklad_big.jpg').'" '.
82 'alt="wowpreklad" title="Otevřený projekt překládání celé hry World of Warcraft" '.
83 'class="banner" height="60" width="468" /></a>';
84
85 $BannerSmall = '<a href="https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/').'">'.
86 '<img src="https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/banners/wowpreklad_small.jpg').'" '.
87 'alt="wowpreklad" title="Otevřený projekt překládání celé hry World of Warcraft" '.
88 'class="banner" height="31" width="88" /></a>';
89
90 $Output = '<strong>'.T('Banners').':</strong><br />';
91
92 $Output .= $Banner.' &nbsp;&nbsp;<textarea rows="2" cols="30">'.htmlspecialchars($Banner).'</textarea><br />';
93 $Output .= $BannerSmall.' &nbsp;&nbsp;<textarea rows="2" cols="30">'.htmlspecialchars($BannerSmall).'</textarea><br />';
94
95 if ($User->Licence(LICENCE_ADMIN))
96 {
97 $MonthAge = 3;
98 $YesNo = array('Ne', 'Ano');
99 $Output .= '<br/><strong>'.T('Servers referring to us').':</strong> <br />'.
100 '<div style="font-size: 10px;">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.</div><br />';
101
102 if (!$User->Licence(LICENCE_ADMIN)) $Where = ' WHERE (`Visible`=1) AND (`Parent` IS NULL)';
103 else $Where = '';
104 $Query = 'SELECT *, (SELECT Web FROM `Referrer` AS T4 WHERE T4.Id = T3.Parent) AS ParentName '.
105 'FROM (SELECT *, '.
106 '(`Hits` + COALESCE((SELECT SUM(`Hits`) FROM '.
107 '`Referrer` AS `T1` WHERE `T1`.`Parent` = `T2`.`Id`), 0)) AS `TotalHits`, '.
108 'GREATEST(`DateLast`, COALESCE((SELECT MAX(`DateLast`) FROM '.
109 '`Referrer` AS `T1` WHERE `T1`.`Parent` = `T2`.`Id`), 0)) AS `MaxDateLast` FROM '.
110 '`Referrer` AS `T2` '.$Where.') AS `T3` '.
111 'WHERE (`T3`.`MaxDateLast` > (NOW() - INTERVAL '.$MonthAge.' MONTH))';
112
113 $DbResult = $this->Database->query('SELECT COUNT(*) FROM ('.$Query.') AS T');
114 $DbRow = $DbResult->fetch_row();
115 $PageList = GetPageList($DbRow[0]);
116
117 $Output .= $PageList['Output'].
118 '<table class="BaseTable">';
119
120 $TableColumns = array(
121 array('Name' => 'Web', 'Title' => T('Address')),
122 array('Name' => 'MaxDateLast', 'Title' => T('Last visit')),
123 array('Name' => 'TotalHits', 'Title' => T('Hits')),
124 );
125 if ($User->Licence(LICENCE_ADMIN))
126 {
127 $TableColumns[] = array('Name' => 'Visible', 'Title' => T('Visible'));
128 $TableColumns[] = array('Name' => 'Parent', 'Title' => T('Parent'));
129 $TableColumns[] = array('Name' => 'Description', 'Title' => T('Comment'));
130 $TableColumns[] = array('Name' => 'LastIP', 'Title' => T('Last IP address'));
131 $TableColumns[] = array('Name' => 'Action', 'Title' => T('Actions'));
132 }
133 $Order = GetOrderTableHeader($TableColumns, 'MaxDateLast', 1);
134 $Output .= $Order['Output'];
135
136 $Query .= $Order['SQL'].$PageList['SQLLimit'];
137
138 $DbResult = $this->Database->query($Query);
139 while ($Line = $DbResult->fetch_assoc())
140 {
141 $Output .= '<tr><td><a href="'.$Line['LastURL'].'">'.$Line['Web'].'</a></td>'.
142 '<td>'.HumanDate($Line['MaxDateLast']).'</td>'.
143 '<td>'.$Line['TotalHits'].'</td>';
144 if ($User->Licence(LICENCE_ADMIN))
145 {
146 $Output .=
147 '<td>'.$YesNo[$Line['Visible']].'</td>'.
148 '<td>'.$Line['ParentName'].'</td>'.
149 '<td>'.$Line['Description'].'</td>'.
150 '<td>'.$Line['LastIP'].'</td>'.
151 '<td><a href="?action=edit&amp;id='.$Line['Id'].'">'.T('Modify').'</a> '.
152 '<a href="?action=spam&amp;id='.$Line['Id'].'">'.T('Spam').'</a></td>';
153 }
154 $Output .= '</tr>';
155 }
156 $Output .= '</table>';
157 $Output .= $PageList['Output'];
158 }
159 return $Output;
160 }
161
162 function SelectParentItem($Selected, $Self)
163 {
164 $Output = '<select name="Parent">';
165 $Output .= '<option value=""';
166 if ($Selected == '')
167 $Output .= ' selected="selected"';
168 $Output .= '></option>';
169 $DbResult = $this->Database->select('Referrer', '`Id`, `Web`', '`Parent` IS NULL ORDER BY `Web`');
170 while ($Language = $DbResult->fetch_assoc())
171 if ($Language['Id'] != $Self)
172 {
173 $Output .= '<option value="'.$Language['Id'].'"';
174 if ($Selected == $Language['Id'])
175 $Output .= ' selected="selected"';
176 $Output .= '>'.$Language['Web'].'</option>';
177 }
178 $Output .= '</select>';
179 return $Output;
180 }
181
182 function Spam()
183 {
184 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
185 if ($User->Licence(LICENCE_ADMIN))
186 {
187 if (array_key_exists('id', $_GET))
188 {
189 $DbResult = $this->Database->select('Referrer', '*', 'Id='.$_GET['id']);
190 if ($DbResult->num_rows > 0)
191 {
192 $Item = $DbResult->fetch_assoc();
193 $this->Database->update('Referrer', 'Id='.$_GET['id'], array('Visible' => 0, 'Description' => 'Spam'));
194 $DbResult2 = $this->Database->select('BlackList', '*', 'IP="'.$Item['LastIP'].'"');
195 if ($DbResult2->num_rows == 0)
196 {
197 $this->Database->insert('BlackList', array('Time' => 'NOW()', 'IP' => $Item['LastIP']));
198 }
199 $Output = ShowMessage(T('Set as spam'), MESSAGE_CRITICAL);
200 $Output .= $this->ShowList();
201 } else $Output = ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
202 } else $Output = ShowMessage(T('Item not specified'), MESSAGE_CRITICAL);
203 } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
204 return $Output;
205 }
206
207 function Edit()
208 {
209 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
210 if ($User->Licence(LICENCE_ADMIN))
211 {
212 if (array_key_exists('id', $_GET))
213 {
214 $DbResult = $this->Database->select('Referrer', '*', 'Id='.$_GET['id']);
215 if ($DbResult->num_rows > 0)
216 {
217 $Item = $DbResult->fetch_assoc();
218 if ($Item['Visible'] == 1) $Visible = ' checked ';
219 else $Visible = '';
220 $Output = '<form action="?action=editsave&amp;id='.$_GET['id'].'" method="post"><table>'.
221 '<tr><td>'.T('Web').'</td><td>'.$Item['Web'].'</td></tr>'.
222 '<tr><td>'.T('Visible').'</td><td><input type="checkbox" name="Visible" '.$Visible.'/></td></tr>'.
223 '<tr><td>'.T('Description').'</td><td><input type="text" name="Description" value="'.$Item['Description'].'"/></td></tr>'.
224 '<tr><td>'.T('Parent item').'</td><td>'.$this->SelectParentItem($Item['Parent'], $Item['Id']).'</td></tr>'.
225 '<tr><td>'.T('Last IP address').'</td><td><input type="text" name="LastIP" value="'.$Item['LastIP'].'"/></td></tr>'.
226 '<tr><td colspan="2"><input type="submit" value="Uložit"/></td></tr></table></form>';
227 } else $Output = ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
228 } else $Output = ShowMessage(T('Item not specified'), MESSAGE_CRITICAL);
229 } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
230 return $Output;
231 }
232
233 function EditSave()
234 {
235 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
236 if ($User->Licence(LICENCE_ADMIN))
237 {
238 if ($_POST['Parent'] == '') $_POST['Parent'] = null;
239 $_POST['Visible'] = array_key_exists('Visible', $_POST);
240 $DbResult = $this->Database->update('Referrer', 'Id='.$_GET['id'], array(
241 'Visible' => $_POST['Visible'], 'LastIP' => $_POST['LastIP'],
242 'Parent' => $_POST['Parent'], 'Description' => $_POST['Description']));
243 $_SERVER['QUERY_STRING'] = '';
244 $Output = ShowMessage(T('Settings saved'));
245 $Output .= $this->ShowList();
246 } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
247 return $Output;
248 }
249
250 function Show(): string
251 {
252 $this->Title = T('Promotion');
253 if (array_key_exists('action', $_GET))
254 {
255 if ($_GET['action'] == 'edit') $Output = $this->Edit();
256 else if ($_GET['action'] == 'spam') $Output = $this->Spam();
257 else if ($_GET['action'] == 'editsave') $Output = $this->EditSave();
258 else $Output = $this->ShowList();
259 } else $Output = $this->ShowList();
260 return $Output;
261 }
262}
Note: See TracBrowser for help on using the repository browser.