CWIS Developer Documentation
PopupWindow.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: PopupWindow.php
4 #
5 # Part of the ScoutLib application support library
6 # Copyright 2002-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/
8 #
9 
13 class PopupWindow {
14 
15  # ---- PUBLIC INTERFACE --------------------------------------------------
16 
25  function PopupWindow($PopupId, $DB, $UserId = NULL)
26  {
27  # save our window ID and database handle
28  $this->Id = intval($PopupId);
29  $this->DB = $DB;
30  $this->UserId = $UserId ? intval($UserId) : NULL;
31 
32  # set defaults
33  $this->Width = 400;
34  $this->Height = 200;
35  $this->ForceDisplay = FALSE;
36  $this->SeenCountThreshold = 5;
37  $this->SeenTimeThreshold = 60;
38  $this->CookieLifetimeInDays = 90;
39  }
40 
49  function Initialize($CountThreshold, $TimeThreshold)
50  {
51  $this->SeenCountThreshold = $CountThreshold;
52  $this->SeenTimeThreshold = $TimeThreshold;
53  $this->ShouldDisplay();
54  }
55 
61  function Width($NewWidth) { $this->Width = intval($NewWidth); }
62 
68  function Height($NewHeight) { $this->Height = intval($NewHeight); }
69 
74  function Id() { return $this->Id; }
75 
81  function WillDisplay() { return $this->ShouldDisplay(); }
82 
87  function AlwaysDisplay($Display) { $this->ForceDisplay = $Display; }
88 
93  function PrintHeaderCode()
94  {
95  # if we should display the window
96  if ($this->ShouldDisplay())
97  {
98  ?>
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');
108  });
109  </script>
110  <?PHP
111  }
112  }
113 
119  {
120  # if we should display the window
121  if ($this->ShouldDisplay())
122  {
123  # display code for beginning of content section
124  ?><div id="PopupWindowContent<?PHP print($this->Id());
125  ?>" style="display: none;"><span><?PHP
126  }
127  }
128 
134  {
135  # if we should display the window
136  if ($this->ShouldDisplay())
137  {
138  # display code for end of content section
139  ?></span></div><?PHP
140  }
141  }
142 
143 
144  # ---- PRIVATE INTERFACE -------------------------------------------------
145 
146  private $Id;
147  private $DB;
148  private $UserId;
149  private $Width;
150  private $Height;
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;
160 
168  private function ShouldDisplay()
169  {
170  # if user requested always display return TRUE to caller
171  if ($this->ForceDisplay) { return TRUE; }
172 
173  # if we have already determined status for this window
174  if (isset($this->DisplayStatus))
175  {
176  # return status to caller
177  return $this->DisplayStatus;
178  }
179 
180  # if cookie is available
181  if (isset($_COOKIE["ScoutPopupCount".$this->Id])
182  && isset($_COOKIE["ScoutPopupFirstSeen".$this->Id]))
183  {
184  # if cookie seen count is below threshold
185  $Count = $_COOKIE["ScoutPopupCount".$this->Id];
186  if ($Count < $this->SeenCountThreshold)
187  {
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)));
194  }
195  else
196  {
197  # if enough time has elapsed and we are not sure about displaying window
198  if ((time() - $_COOKIE["ScoutPopupFirstSeen".$this->Id])
199  >= $this->SeenTimeThreshold)
200  {
201  # if cookie seen count is at threshold
202  if ($Count == $this->SeenCountThreshold)
203  {
204  # display the window
205  $Display = TRUE;
206 
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)));
213  }
214  else
215  {
216  # do not display the window
217  $Display = FALSE;
218  }
219  }
220  }
221  }
222  else
223  {
224  # set cookie
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)));
229  }
230 
231  # if we know the user ID
232  if ($this->UserId !== NULL)
233  {
234  # if we have seen this user ID before
235  $Count = $this->SeenCountForUserId();
236  if ($Count !== NULL)
237  {
238  # if user ID seen count is below threshold
239  if ($Count < $this->SeenCountThreshold)
240  {
241  # increase user ID seen count
242  $Count = $this->SeenCountForUserId($Count + 1);
243  }
244  else
245  {
246  # if enough time has elapsed
247  if ($this->SecondsSinceUserIdFirstSeen()
248  >= $this->SeenTimeThreshold)
249  {
250  # if user ID seen count is at threshold
251  if ($Count == $this->SeenCountThreshold)
252  {
253  # display the window (if not previously disallowed)
254  if (!isset($Display)) { $Display = TRUE; }
255 
256  # increase user ID seen count
257  $Count = $this->SeenCountForUserId($Count + 1);
258  }
259  else
260  {
261  # do not display the window
262  $Display = FALSE;
263  }
264  }
265  }
266  }
267  else
268  {
269  # add user ID to database
270  $Count = $this->SeenCountForUserId(1);
271  }
272  }
273 
274  # if we have seen this IP address before
275  $Count = $this->SeenCountForIPAddress();
276  if ($Count !== NULL)
277  {
278  # if IP address seen count is below threshold
279  if ($Count < $this->SeenCountThreshold)
280  {
281  # increase IP address seen count
282  $Count = $this->SeenCountForIPAddress($Count + 1);
283  }
284  else
285  {
286  # if enough time has elapsed
287  if ($this->SecondsSinceIPAddressFirstSeen() >= $this->SeenTimeThreshold)
288  {
289  # if IP address seen count is at threshold
290  if ($Count == $this->SeenCountThreshold)
291  {
292  # display the window (if not previously disallowed)
293  if (!isset($Display)) { $Display = TRUE; }
294 
295  # increase IP address seen count
296  $Count = $this->SeenCountForIPAddress($Count + 1);
297  }
298  else
299  {
300  # do not display the window
301  $Display = FALSE;
302  }
303  }
304  }
305  }
306  else
307  {
308  # add IP address to database
309  $Count = $this->SeenCountForIPAddress(1);
310  }
311 
312  # if we are still not sure whether to display the window
313  if (!isset($Display))
314  {
315  # do not display the window
316  $Display = FALSE;
317  }
318 
319  # save window display status
320  $this->DisplayStatus = $Display;
321 
322  # return window display status to caller
323  return $Display;
324  }
325 
333  private function SeenCountForUserId($NewSeenCount = NULL)
334  {
335  # attempt to retrieve count from database
336  if (!isset($this->UserIdSeenCount))
337  {
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() )
343  {
344  $Tmp = $this->DB->FetchRow();
345  $this->UserIdSeenCount = $Tmp["SeenCount"];
346  $this->UserIdFirstSeen = $Tmp["FirstSeen"];
347  }
348  else
349  {
350  $this->UserIdSeenCount = NULL;
351  $this->UserIdFirstSeen = NULL;
352  }
353  }
354  $Count = $this->UserIdSeenCount;
355 
356  # if new count supplied
357  if ($NewSeenCount !== NULL)
358  {
359  # if count is already in database
360  if ($Count !== NULL)
361  {
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");
367  }
368  else
369  {
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(), "
374  .$NewSeenCount.")");
375  }
376 
377  # set current count to new count
378  $Count = $NewSeenCount;
379  }
380 
381  # return current count to caller
382  return $Count;
383  }
384 
385 
394  private function SeenCountForIPAddress($NewSeenCount = NULL)
395  {
396  # attempt to retrieve count from database
397  if (!isset($this->IPAddressSeenCount))
398  {
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() )
404  {
405  $Tmp = $this->DB->FetchRow();
406  $this->IPAddressSeenCount = $Tmp["SeenCount"];
407  $this->IPAddressFirstSeen = $Tmp["FirstSeen"];
408  }
409  else
410  {
411  $this->IPAddressSeenCount = NULL;
412  $this->IPAddressFirstSeen = NULL;
413  }
414  }
415  $Count = $this->IPAddressSeenCount;
416 
417  # if new count supplied
418  if ($NewSeenCount !== NULL)
419  {
420  # if count is already in database
421  if ($Count !== NULL)
422  {
423  # update count in database
424  $this->DB->Query("UPDATE PopupLog SET SeenCount = ".$NewSeenCount
425  ." WHERE SigOne = ".$this->UserIPSigOne()
426  ." AND SigTwo = ".$this->UserIPSigTwo());
427  }
428  else
429  {
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())");
437  }
438 
439  # set current count to new count
440  $Count = $NewSeenCount;
441  }
442 
443  # return current count to caller
444  return $Count;
445  }
446 
452  private function SecondsSinceUserIdFirstSeen()
453  {
454  if (!isset($this->UserIdFirstSeen)) { $this->SeenCountForUserId(); }
455  return time() - strtotime($this->UserIdFirstSeen);
456  }
457 
464  private function SecondsSinceIPAddressFirstSeen()
465  {
466  if (!isset($this->IPAddressFirstSeen)) { $this->SeenCountForIPAddress(); }
467  return time() - strtotime($this->IPAddressFirstSeen);
468  }
469 
476  private function UserIPSigOne()
477  {
478  $Bytes = explode(".", $_SERVER["REMOTE_ADDR"]);
479  return (count($Bytes) != 4) ? 0
480  : (intval($Bytes[0]) * 256) + intval($Bytes[1]);
481  }
482 
489  private function UserIPSigTwo()
490  {
491  $Bytes = explode(".", $_SERVER["REMOTE_ADDR"]);
492  return (count($Bytes) != 4) ? 0
493  : (intval($Bytes[2]) * 256) + intval($Bytes[3]);
494  }
495 
496 }
Lightboxed pop-up window with repeat prevention.
Definition: PopupWindow.php:13
PrintBeginContentCode()
Print the beginning code wrapper for the pop-up window content section in page body if appropriate...
AlwaysDisplay($Display)
Sets the flag that forces the pop-up window to display.
Definition: PopupWindow.php:87
Width($NewWidth)
Set the width of the pop-up window.
Definition: PopupWindow.php:61
PopupWindow($PopupId, $DB, $UserId=NULL)
Object constructor.
Definition: PopupWindow.php:25
PrintHeaderCode()
Print header code if appropriate.
Definition: PopupWindow.php:93
WillDisplay()
Report whether the pop-up window will display if code printing methods are called.
Definition: PopupWindow.php:81
Initialize($CountThreshold, $TimeThreshold)
Initialize pop-up window tracking.
Definition: PopupWindow.php:49
Height($NewHeight)
Set the height of the pop-up window.
Definition: PopupWindow.php:68
Id()
Get the integer ID of window.
Definition: PopupWindow.php:74
PrintEndContentCode()
Print the ending code wrapper for the pop-up window content section in page body if appropriate...