3 # FILE: Scout--PopupWindow.php
4 # Copyright 2009 Edward Almasy and Internet Scout
5 # http://scout.wisc.edu
13 # ---- PUBLIC INTERFACE --------------------------------------------------
23 # save our window ID and database handle
24 $this->
Id = intval($PopupId);
26 $this->UserId = $UserId ? intval($UserId) : NULL;
31 $this->ForceDisplay = FALSE;
32 $this->SeenCountThreshold = 5;
33 $this->SeenTimeThreshold = 60;
34 $this->CookieLifetimeInDays = 90;
44 $this->SeenCountThreshold = $CountThreshold;
45 $this->SeenTimeThreshold = $TimeThreshold;
46 $this->ShouldDisplay();
52 function Width($NewWidth) { $this->
Width = intval($NewWidth); }
56 function Height($NewHeight) { $this->
Height = intval($NewHeight); }
62 function Id() {
return $this->Id; }
81 # if we should display the window
82 if ($this->ShouldDisplay())
85 <style type=
"text/css">@
import 'include/thickbox.css';</style>
86 <script type=
"text/javascript" src=
"include/SPT--jQuery.js"></script>
87 <script type=
"text/javascript" src=
"include/thickbox-compressed.js"></script>
88 <script type=
"text/javascript">
89 $(document).ready(
function(){
90 tb_show(
'',
'#TB_inline?inlineId=PopupWindowContent<?PHP
91 print($this->Id()); ?>&width=<?PHP
92 print($this->Width); ?>&height=<?PHP
93 print($this->Height); ?>&modal=true',
'null');
105 # if we should display the window
106 if ($this->ShouldDisplay())
108 # display code for beginning of content section
109 ?><div
id=
"PopupWindowContent<?PHP print($this->Id());
110 ?>" style=
"display: none;"><span><?
PHP
119 # if we should display the window
120 if ($this->ShouldDisplay())
122 # display code for end of content section
128 # ---- PRIVATE INTERFACE -------------------------------------------------
135 private $ForceDisplay;
136 private $SeenCountThreshold;
137 private $SeenTimeThreshold;
138 private $CookieLifetimeInDays;
139 private $DisplayStatus; # local to ShouldDisplay()
140 private $UserIdSeenCount;
# local to SeenCountForUserId()
141 private $UserIdFirstSeen;
142 private $IPAddressSeenCount; # local to SeenCountForIPAddress()
143 private $IPAddressFirstSeen;
145 # determine whether pop-up window should be displayed
146 private function ShouldDisplay()
148 # if user requested always display return TRUE to caller
149 if ($this->ForceDisplay) {
return TRUE; }
151 # if we have already determined status for this window
152 if (isset($this->DisplayStatus))
154 # return status to caller
155 return $this->DisplayStatus;
158 # if cookie is available
159 if (isset($_COOKIE[
"ScoutPopupCount".$this->
Id])
160 && isset($_COOKIE[
"ScoutPopupFirstSeen".$this->Id]))
162 # if cookie seen count is below threshold
163 $Count = $_COOKIE[
"ScoutPopupCount".$this->Id];
164 if ($Count < $this->SeenCountThreshold)
166 # increase cookie seen count
167 setcookie(
"ScoutPopupCount".$this->Id, ($Count + 1),
168 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
169 setcookie(
"ScoutPopupFirstSeen".$this->Id,
170 $_COOKIE[
"ScoutPopupFirstSeen".$this->Id],
171 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
175 # if enough time has elapsed and we are not sure about displaying window
176 if ((time() - $_COOKIE[
"ScoutPopupFirstSeen".$this->Id])
177 >= $this->SeenTimeThreshold)
179 # if cookie seen count is at threshold
180 if ($Count == $this->SeenCountThreshold)
185 # increase cookie seen count
186 setcookie(
"ScoutPopupCount".$this->Id, ($Count + 1),
187 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
188 setcookie(
"ScoutPopupFirstSeen".$this->Id,
189 $_COOKIE[
"ScoutPopupFirstSeen".$this->Id],
190 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
194 # do not display the window
203 setcookie(
"ScoutPopupFirstSeen".$this->Id, time(),
204 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
205 setcookie(
"ScoutPopupCount".$this->Id, 1,
206 (time() + (60*60*24 * $this->CookieLifetimeInDays)));
209 # if we know the user ID
210 if ($this->UserId !== NULL)
212 # if we have seen this user ID before
213 $Count = $this->SeenCountForUserId();
216 # if user ID seen count is below threshold
217 if ($Count < $this->SeenCountThreshold)
219 # increase user ID seen count
220 $Count = $this->SeenCountForUserId($Count + 1);
224 # if enough time has elapsed
225 if ($this->SecondsSinceUserIdFirstSeen()
226 >= $this->SeenTimeThreshold)
228 # if user ID seen count is at threshold
229 if ($Count == $this->SeenCountThreshold)
231 # display the window (if not previously disallowed)
232 if (!isset($Display)) { $Display = TRUE; }
234 # increase user ID seen count
235 $Count = $this->SeenCountForUserId($Count + 1);
239 # do not display the window
247 # add user ID to database
248 $Count = $this->SeenCountForUserId(1);
252 # if we have seen this IP address before
253 $Count = $this->SeenCountForIPAddress();
256 # if IP address seen count is below threshold
257 if ($Count < $this->SeenCountThreshold)
259 # increase IP address seen count
260 $Count = $this->SeenCountForIPAddress($Count + 1);
264 # if enough time has elapsed
265 if ($this->SecondsSinceIPAddressFirstSeen() >= $this->SeenTimeThreshold)
267 # if IP address seen count is at threshold
268 if ($Count == $this->SeenCountThreshold)
270 # display the window (if not previously disallowed)
271 if (!isset($Display)) { $Display = TRUE; }
273 # increase IP address seen count
274 $Count = $this->SeenCountForIPAddress($Count + 1);
278 # do not display the window
286 # add IP address to database
287 $Count = $this->SeenCountForIPAddress(1);
290 # if we are still not sure whether to display the window
291 if (!isset($Display))
293 # do not display the window
297 # save window display status
298 $this->DisplayStatus = $Display;
300 # return window display status to caller
304 # get/set seen count for current user ID (returns NULL if not set)
305 private function SeenCountForUserId($NewSeenCount = NULL)
307 # attempt to retrieve count from database
308 if (!isset($this->UserIdSeenCount))
310 $this->DB->Query(
"SELECT SeenCount, FirstSeen FROM PopupLog"
311 .
" WHERE PopupId = ".$this->Id
312 .
" AND SigOne = ".$this->UserId
313 .
" AND SigTwo <= 0");
314 if( $this->DB->NumRowsSelected() )
316 $Tmp = $this->DB->FetchRow();
317 $this->UserIdSeenCount = $Tmp[
"SeenCount"];
318 $this->UserIdFirstSeen = $Tmp[
"FirstSeen"];
322 $this->UserIdSeenCount = NULL;
323 $this->UserIdFirstSeen = NULL;
326 $Count = $this->UserIdSeenCount;
328 # if new count supplied
329 if ($NewSeenCount !== NULL)
331 # if count is already in database
334 # update count in database
335 $this->DB->Query(
"UPDATE PopupLog SET SeenCount = ".$NewSeenCount
336 .
" WHERE PopupId = ".intval($this->Id)
337 .
" AND SigOne = ".$this->UserId
338 .
" AND SigTwo <= 0");
342 # add count to database
343 $this->DB->Query(
"INSERT INTO PopupLog"
344 .
" (PopupId, SigOne, SigTwo, FirstSeen, SeenCount) VALUES "
345 .
" (".$this->Id.
", ".$this->UserId.
", -1, NOW(), "
349 # set current count to new count
350 $Count = $NewSeenCount;
353 # return current count to caller
357 # get/set seen count for IP address of current user (returns NULL if not set)
358 private function SeenCountForIPAddress($NewSeenCount = NULL)
360 # attempt to retrieve count from database
361 if (!isset($this->IPAddressSeenCount))
363 $this->DB->Query(
"SELECT SeenCount, FirstSeen FROM PopupLog"
364 .
" WHERE PopupId = ".$this->Id
365 .
" AND SigOne = ".$this->UserIPSigOne()
366 .
" AND SigTwo = ".$this->UserIPSigTwo());
367 if( $this->DB->NumRowsSelected() )
369 $Tmp = $this->DB->FetchRow();
370 $this->IPAddressSeenCount = $Tmp[
"SeenCount"];
371 $this->IPAddressFirstSeen = $Tmp[
"FirstSeen"];
375 $this->IPAddressSeenCount = NULL;
376 $this->IPAddressFirstSeen = NULL;
379 $Count = $this->IPAddressSeenCount;
381 # if new count supplied
382 if ($NewSeenCount !== NULL)
384 # if count is already in database
387 # update count in database
388 $this->DB->Query(
"UPDATE PopupLog SET SeenCount = ".$NewSeenCount
389 .
" WHERE SigOne = ".$this->UserIPSigOne()
390 .
" AND SigTwo = ".$this->UserIPSigTwo());
394 # add count to database
395 $this->DB->Query(
"INSERT INTO PopupLog"
396 .
" (PopupId, SigOne, SigTwo, SeenCount, FirstSeen)"
397 .
" VALUES (".$this->Id.
", "
398 .$this->UserIPSigOne().
", "
399 .$this->UserIPSigTwo().
", "
400 .$NewSeenCount.
", NOW())");
403 # set current count to new count
404 $Count = $NewSeenCount;
407 # return current count to caller
411 # get seconds since current user ID first seen
412 private function SecondsSinceUserIdFirstSeen()
414 if (!isset($this->UserIdFirstSeen)) { $this->SeenCountForUserId(); }
415 return time() - strtotime($this->UserIdFirstSeen);
418 # get seconds since IP address of current user first seen
419 private function SecondsSinceIPAddressFirstSeen()
421 if (!isset($this->IPAddressFirstSeen)) { $this->SeenCountForIPAddress(); }
422 return time() - strtotime($this->IPAddressFirstSeen);
425 # return signature values for IP address of current user
426 private function UserIPSigOne()
428 $Bytes = explode(
".", $_SERVER[
"REMOTE_ADDR"]);
429 return (count($Bytes) != 4) ? 0
430 : (intval($Bytes[0]) * 256) + intval($Bytes[1]);
432 private function UserIPSigTwo()
434 $Bytes = explode(
".", $_SERVER[
"REMOTE_ADDR"]);
435 return (count($Bytes) != 4) ? 0
436 : (intval($Bytes[2]) * 256) + intval($Bytes[3]);