3 # FILE: PopupWindow.php
5 # Part of the ScoutLib application support library
6 # Copyright 2002-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/
15 # ---- PUBLIC INTERFACE --------------------------------------------------
27 # save our window ID and database handle
28 $this->
Id = intval($PopupId);
30 $this->UserId = $UserId ? intval($UserId) : NULL;
35 $this->ForceDisplay = FALSE;
36 $this->SeenCountThreshold = 5;
37 $this->SeenTimeThreshold = 60;
38 $this->CookieLifetimeInDays = 90;
51 $this->SeenCountThreshold = $CountThreshold;
52 $this->SeenTimeThreshold = $TimeThreshold;
53 $this->ShouldDisplay();
61 function Width($NewWidth) { $this->
Width = intval($NewWidth); }
68 function Height($NewHeight) { $this->
Height = intval($NewHeight); }
74 function Id() {
return $this->Id; }
95 # if we should display the window
96 if ($this->ShouldDisplay())
99 <style type=
"text/css">@
import 'include/thickbox.css';</style>
100 <script type=
"text/javascript" src=
"include/SPT--jQuery.js"></script>
101 <script type=
"text/javascript" src=
"include/thickbox-compressed.js"></script>
102 <script type=
"text/javascript">
103 $(document).ready(
function(){
104 tb_show(
'',
'#TB_inline?inlineId=PopupWindowContent<?PHP
105 print($this->Id()); ?>&width=<?PHP
106 print($this->Width); ?>&height=<?PHP
107 print($this->Height); ?>&modal=true',
'null');
120 # if we should display the window
121 if ($this->ShouldDisplay())
123 # display code for beginning of content section
124 ?><div
id=
"PopupWindowContent<?PHP print($this->Id());
125 ?>" style=
"display: none;"><span><?
PHP
135 # if we should display the window
136 if ($this->ShouldDisplay())
138 # display code for end of content section
144 # ---- PRIVATE INTERFACE -------------------------------------------------
151 private $ForceDisplay;
152 private $SeenCountThreshold;
153 private $SeenTimeThreshold;
154 private $CookieLifetimeInDays;
155 private $DisplayStatus; # local to ShouldDisplay()
156 private $UserIdSeenCount;
# local to SeenCountForUserId()
157 private $UserIdFirstSeen;
158 private $IPAddressSeenCount; # local to SeenCountForIPAddress()
159 private $IPAddressFirstSeen;
168 private function ShouldDisplay()
170 # if user requested always display return TRUE to caller
171 if ($this->ForceDisplay) {
return TRUE; }
173 # if we have already determined status for this window
174 if (isset($this->DisplayStatus))
176 # return status to caller
177 return $this->DisplayStatus;
180 # if cookie is available
181 if (isset($_COOKIE[
"ScoutPopupCount".$this->
Id])
182 && isset($_COOKIE[
"ScoutPopupFirstSeen".$this->Id]))
184 # if cookie seen count is below threshold
185 $Count = $_COOKIE[
"ScoutPopupCount".$this->Id];
186 if ($Count < $this->SeenCountThreshold)
188 # increase cookie seen count
189 setcookie(
"ScoutPopupCount".$this->Id, ($Count + 1),
190 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
191 setcookie(
"ScoutPopupFirstSeen".$this->Id,
192 $_COOKIE[
"ScoutPopupFirstSeen".$this->Id],
193 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
197 # if enough time has elapsed and we are not sure about displaying window
198 if ((time() - $_COOKIE[
"ScoutPopupFirstSeen".$this->Id])
199 >= $this->SeenTimeThreshold)
201 # if cookie seen count is at threshold
202 if ($Count == $this->SeenCountThreshold)
207 # increase cookie seen count
208 setcookie(
"ScoutPopupCount".$this->Id, ($Count + 1),
209 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
210 setcookie(
"ScoutPopupFirstSeen".$this->Id,
211 $_COOKIE[
"ScoutPopupFirstSeen".$this->Id],
212 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
216 # do not display the window
225 setcookie(
"ScoutPopupFirstSeen".$this->Id, time(),
226 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
227 setcookie(
"ScoutPopupCount".$this->Id, 1,
228 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
231 # if we know the user ID
232 if ($this->UserId !== NULL)
234 # if we have seen this user ID before
235 $Count = $this->SeenCountForUserId();
238 # if user ID seen count is below threshold
239 if ($Count < $this->SeenCountThreshold)
241 # increase user ID seen count
242 $Count = $this->SeenCountForUserId($Count + 1);
246 # if enough time has elapsed
247 if ($this->SecondsSinceUserIdFirstSeen()
248 >= $this->SeenTimeThreshold)
250 # if user ID seen count is at threshold
251 if ($Count == $this->SeenCountThreshold)
253 # display the window (if not previously disallowed)
254 if (!isset($Display)) { $Display = TRUE; }
256 # increase user ID seen count
257 $Count = $this->SeenCountForUserId($Count + 1);
261 # do not display the window
269 # add user ID to database
270 $Count = $this->SeenCountForUserId(1);
274 # if we have seen this IP address before
275 $Count = $this->SeenCountForIPAddress();
278 # if IP address seen count is below threshold
279 if ($Count < $this->SeenCountThreshold)
281 # increase IP address seen count
282 $Count = $this->SeenCountForIPAddress($Count + 1);
286 # if enough time has elapsed
287 if ($this->SecondsSinceIPAddressFirstSeen() >= $this->SeenTimeThreshold)
289 # if IP address seen count is at threshold
290 if ($Count == $this->SeenCountThreshold)
292 # display the window (if not previously disallowed)
293 if (!isset($Display)) { $Display = TRUE; }
295 # increase IP address seen count
296 $Count = $this->SeenCountForIPAddress($Count + 1);
300 # do not display the window
308 # add IP address to database
309 $Count = $this->SeenCountForIPAddress(1);
312 # if we are still not sure whether to display the window
313 if (!isset($Display))
315 # do not display the window
319 # save window display status
320 $this->DisplayStatus = $Display;
322 # return window display status to caller
333 private function SeenCountForUserId($NewSeenCount = NULL)
335 # attempt to retrieve count from database
336 if (!isset($this->UserIdSeenCount))
338 $this->DB->Query(
"SELECT SeenCount, FirstSeen FROM PopupLog"
339 .
" WHERE PopupId = ".$this->Id
340 .
" AND SigOne = ".$this->UserId
341 .
" AND SigTwo <= 0");
342 if( $this->DB->NumRowsSelected() )
344 $Tmp = $this->DB->FetchRow();
345 $this->UserIdSeenCount = $Tmp[
"SeenCount"];
346 $this->UserIdFirstSeen = $Tmp[
"FirstSeen"];
350 $this->UserIdSeenCount = NULL;
351 $this->UserIdFirstSeen = NULL;
354 $Count = $this->UserIdSeenCount;
356 # if new count supplied
357 if ($NewSeenCount !== NULL)
359 # if count is already in database
362 # update count in database
363 $this->DB->Query(
"UPDATE PopupLog SET SeenCount = ".$NewSeenCount
364 .
" WHERE PopupId = ".intval($this->Id)
365 .
" AND SigOne = ".$this->UserId
366 .
" AND SigTwo <= 0");
370 # add count to database
371 $this->DB->Query(
"INSERT INTO PopupLog"
372 .
" (PopupId, SigOne, SigTwo, FirstSeen, SeenCount) VALUES "
373 .
" (".$this->Id.
", ".$this->UserId.
", -1, NOW(), "
377 # set current count to new count
378 $Count = $NewSeenCount;
381 # return current count to caller
394 private function SeenCountForIPAddress($NewSeenCount = NULL)
396 # attempt to retrieve count from database
397 if (!isset($this->IPAddressSeenCount))
399 $this->DB->Query(
"SELECT SeenCount, FirstSeen FROM PopupLog"
400 .
" WHERE PopupId = ".$this->Id
401 .
" AND SigOne = ".$this->UserIPSigOne()
402 .
" AND SigTwo = ".$this->UserIPSigTwo());
403 if( $this->DB->NumRowsSelected() )
405 $Tmp = $this->DB->FetchRow();
406 $this->IPAddressSeenCount = $Tmp[
"SeenCount"];
407 $this->IPAddressFirstSeen = $Tmp[
"FirstSeen"];
411 $this->IPAddressSeenCount = NULL;
412 $this->IPAddressFirstSeen = NULL;
415 $Count = $this->IPAddressSeenCount;
417 # if new count supplied
418 if ($NewSeenCount !== NULL)
420 # if count is already in database
423 # update count in database
424 $this->DB->Query(
"UPDATE PopupLog SET SeenCount = ".$NewSeenCount
425 .
" WHERE SigOne = ".$this->UserIPSigOne()
426 .
" AND SigTwo = ".$this->UserIPSigTwo());
430 # add count to database
431 $this->DB->Query(
"INSERT INTO PopupLog"
432 .
" (PopupId, SigOne, SigTwo, SeenCount, FirstSeen)"
433 .
" VALUES (".$this->Id.
", "
434 .$this->UserIPSigOne().
", "
435 .$this->UserIPSigTwo().
", "
436 .$NewSeenCount.
", NOW())");
439 # set current count to new count
440 $Count = $NewSeenCount;
443 # return current count to caller
452 private function SecondsSinceUserIdFirstSeen()
454 if (!isset($this->UserIdFirstSeen)) { $this->SeenCountForUserId(); }
455 return time() - strtotime($this->UserIdFirstSeen);
464 private function SecondsSinceIPAddressFirstSeen()
466 if (!isset($this->IPAddressFirstSeen)) { $this->SeenCountForIPAddress(); }
467 return time() - strtotime($this->IPAddressFirstSeen);
476 private function UserIPSigOne()
478 $Bytes = explode(
".", $_SERVER[
"REMOTE_ADDR"]);
479 return (count($Bytes) != 4) ? 0
480 : (intval($Bytes[0]) * 256) + intval($Bytes[1]);
489 private function UserIPSigTwo()
491 $Bytes = explode(
".", $_SERVER[
"REMOTE_ADDR"]);
492 return (count($Bytes) != 4) ? 0
493 : (intval($Bytes[2]) * 256) + intval($Bytes[3]);