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/ 16 # ---- PUBLIC INTERFACE -------------------------------------------------- 28 # save our window ID and database handle 29 $this->
Id = intval($PopupId);
31 $this->UserId = $UserId ? intval($UserId) : NULL;
36 $this->ForceDisplay = FALSE;
37 $this->SeenCountThreshold = 5;
38 $this->SeenTimeThreshold = 60;
39 $this->CookieLifetimeInDays = 90;
50 public function Initialize($CountThreshold, $TimeThreshold)
52 $this->SeenCountThreshold = $CountThreshold;
53 $this->SeenTimeThreshold = $TimeThreshold;
54 $this->ShouldDisplay();
62 public function Width($NewWidth)
64 $this->
Width = intval($NewWidth);
74 $this->
Height = intval($NewHeight);
93 return $this->ShouldDisplay();
102 $this->ForceDisplay = $Display;
111 # if we should display the window 112 if ($this->ShouldDisplay())
115 <style type=
"text/css">@
import 'include/thickbox.css';</style>
116 <script type=
"text/javascript" src=
"include/SPT--jQuery.js"></script>
117 <script type=
"text/javascript" src=
"include/thickbox-compressed.js"></script>
118 <script type=
"text/javascript">
119 $(document).ready(
function(){
120 tb_show(
'',
'#TB_inline?inlineId=PopupWindowContent<?PHP 121 print($this->Id()); ?>&width=<?PHP 122 print($this->Width); ?>&height=<?PHP 123 print($this->Height); ?>&modal=true',
'null');
136 # if we should display the window 137 if ($this->ShouldDisplay())
139 # display code for beginning of content section 140 ?><div
id=
"PopupWindowContent<?PHP print($this->Id()); 141 ?>" style=
"display: none;"><span><?
PHP 151 # if we should display the window 152 if ($this->ShouldDisplay())
154 # display code for end of content section 160 # ---- PRIVATE INTERFACE ------------------------------------------------- 167 private $ForceDisplay;
168 private $SeenCountThreshold;
169 private $SeenTimeThreshold;
170 private $CookieLifetimeInDays;
171 private $DisplayStatus; # local to ShouldDisplay()
172 private $UserIdSeenCount; # local to SeenCountForUserId()
173 private $UserIdFirstSeen;
174 private $IPAddressSeenCount; # local to SeenCountForIPAddress()
175 private $IPAddressFirstSeen;
184 private function ShouldDisplay()
186 # if user requested always display return TRUE to caller 187 if ($this->ForceDisplay) {
return TRUE; }
189 # if we have already determined status for this window 190 if (isset($this->DisplayStatus))
192 # return status to caller 193 return $this->DisplayStatus;
196 # if cookie is available 197 if (isset($_COOKIE[
"ScoutPopupCount".$this->
Id])
198 && isset($_COOKIE[
"ScoutPopupFirstSeen".$this->Id]))
200 # if cookie seen count is below threshold 201 $Count = $_COOKIE[
"ScoutPopupCount".$this->Id];
202 if ($Count < $this->SeenCountThreshold)
204 # increase cookie seen count 205 setcookie(
"ScoutPopupCount".$this->Id, ($Count + 1),
206 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
207 setcookie(
"ScoutPopupFirstSeen".$this->Id,
208 $_COOKIE[
"ScoutPopupFirstSeen".$this->Id],
209 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
213 # if enough time has elapsed and we are not sure about displaying window 214 if ((time() - $_COOKIE[
"ScoutPopupFirstSeen".$this->Id])
215 >= $this->SeenTimeThreshold)
217 # if cookie seen count is at threshold 218 if ($Count == $this->SeenCountThreshold)
223 # increase cookie seen count 224 setcookie(
"ScoutPopupCount".$this->Id, ($Count + 1),
225 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
226 setcookie(
"ScoutPopupFirstSeen".$this->Id,
227 $_COOKIE[
"ScoutPopupFirstSeen".$this->Id],
228 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
232 # do not display the window 241 setcookie(
"ScoutPopupFirstSeen".$this->Id, time(),
242 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
243 setcookie(
"ScoutPopupCount".$this->Id, 1,
244 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
247 # if we know the user ID 248 if ($this->UserId !== NULL)
250 # if we have seen this user ID before 251 $Count = $this->SeenCountForUserId();
254 # if user ID seen count is below threshold 255 if ($Count < $this->SeenCountThreshold)
257 # increase user ID seen count 258 $Count = $this->SeenCountForUserId($Count + 1);
262 # if enough time has elapsed 263 if ($this->SecondsSinceUserIdFirstSeen()
264 >= $this->SeenTimeThreshold)
266 # if user ID seen count is at threshold 267 if ($Count == $this->SeenCountThreshold)
269 # display the window (if not previously disallowed) 270 if (!isset($Display)) { $Display = TRUE; }
272 # increase user ID seen count 273 $Count = $this->SeenCountForUserId($Count + 1);
277 # do not display the window 285 # add user ID to database 286 $Count = $this->SeenCountForUserId(1);
290 # if we have seen this IP address before 291 $Count = $this->SeenCountForIPAddress();
294 # if IP address seen count is below threshold 295 if ($Count < $this->SeenCountThreshold)
297 # increase IP address seen count 298 $Count = $this->SeenCountForIPAddress($Count + 1);
302 # if enough time has elapsed 303 if ($this->SecondsSinceIPAddressFirstSeen() >= $this->SeenTimeThreshold)
305 # if IP address seen count is at threshold 306 if ($Count == $this->SeenCountThreshold)
308 # display the window (if not previously disallowed) 309 if (!isset($Display)) { $Display = TRUE; }
311 # increase IP address seen count 312 $Count = $this->SeenCountForIPAddress($Count + 1);
316 # do not display the window 324 # add IP address to database 325 $Count = $this->SeenCountForIPAddress(1);
328 # if we are still not sure whether to display the window 329 if (!isset($Display))
331 # do not display the window 335 # save window display status 336 $this->DisplayStatus = $Display;
338 # return window display status to caller 349 private function SeenCountForUserId($NewSeenCount = NULL)
351 # attempt to retrieve count from database 352 if (!isset($this->UserIdSeenCount))
354 $this->DB->Query(
"SELECT SeenCount, FirstSeen FROM PopupLog" 355 .
" WHERE PopupId = ".$this->
Id 356 .
" AND SigOne = ".$this->UserId
357 .
" AND SigTwo <= 0");
358 if( $this->DB->NumRowsSelected() )
360 $Tmp = $this->DB->FetchRow();
361 $this->UserIdSeenCount = $Tmp[
"SeenCount"];
362 $this->UserIdFirstSeen = $Tmp[
"FirstSeen"];
366 $this->UserIdSeenCount = NULL;
367 $this->UserIdFirstSeen = NULL;
370 $Count = $this->UserIdSeenCount;
372 # if new count supplied 373 if ($NewSeenCount !== NULL)
375 # if count is already in database 378 # update count in database 379 $this->DB->Query(
"UPDATE PopupLog SET SeenCount = ".$NewSeenCount
380 .
" WHERE PopupId = ".intval($this->
Id)
381 .
" AND SigOne = ".$this->UserId
382 .
" AND SigTwo <= 0");
386 # add count to database 387 $this->DB->Query(
"INSERT INTO PopupLog" 388 .
" (PopupId, SigOne, SigTwo, FirstSeen, SeenCount) VALUES " 389 .
" (".$this->
Id.
", ".$this->UserId.
", -1, NOW(), " 393 # set current count to new count 394 $Count = $NewSeenCount;
397 # return current count to caller 410 private function SeenCountForIPAddress($NewSeenCount = NULL)
412 # attempt to retrieve count from database 413 if (!isset($this->IPAddressSeenCount))
415 $this->DB->Query(
"SELECT SeenCount, FirstSeen FROM PopupLog" 416 .
" WHERE PopupId = ".$this->
Id 417 .
" AND SigOne = ".$this->UserIPSigOne()
418 .
" AND SigTwo = ".$this->UserIPSigTwo());
419 if( $this->DB->NumRowsSelected() )
421 $Tmp = $this->DB->FetchRow();
422 $this->IPAddressSeenCount = $Tmp[
"SeenCount"];
423 $this->IPAddressFirstSeen = $Tmp[
"FirstSeen"];
427 $this->IPAddressSeenCount = NULL;
428 $this->IPAddressFirstSeen = NULL;
431 $Count = $this->IPAddressSeenCount;
433 # if new count supplied 434 if ($NewSeenCount !== NULL)
436 # if count is already in database 439 # update count in database 440 $this->DB->Query(
"UPDATE PopupLog SET SeenCount = ".$NewSeenCount
441 .
" WHERE SigOne = ".$this->UserIPSigOne()
442 .
" AND SigTwo = ".$this->UserIPSigTwo());
446 # add count to database 447 $this->DB->Query(
"INSERT INTO PopupLog" 448 .
" (PopupId, SigOne, SigTwo, SeenCount, FirstSeen)" 449 .
" VALUES (".$this->
Id.
", " 450 .$this->UserIPSigOne().
", " 451 .$this->UserIPSigTwo().
", " 452 .$NewSeenCount.
", NOW())");
455 # set current count to new count 456 $Count = $NewSeenCount;
459 # return current count to caller 468 private function SecondsSinceUserIdFirstSeen()
470 if (!isset($this->UserIdFirstSeen)) { $this->SeenCountForUserId(); }
471 return time() - strtotime($this->UserIdFirstSeen);
480 private function SecondsSinceIPAddressFirstSeen()
482 if (!isset($this->IPAddressFirstSeen)) { $this->SeenCountForIPAddress(); }
483 return time() - strtotime($this->IPAddressFirstSeen);
492 private function UserIPSigOne()
494 $Bytes = explode(
".", $_SERVER[
"REMOTE_ADDR"]);
495 return (count($Bytes) != 4) ? 0
496 : (intval($Bytes[0]) * 256) + intval($Bytes[1]);
505 private function UserIPSigTwo()
507 $Bytes = explode(
".", $_SERVER[
"REMOTE_ADDR"]);
508 return (count($Bytes) != 4) ? 0
509 : (intval($Bytes[2]) * 256) + intval($Bytes[3]);