CWIS Developer Documentation
PopupWindow.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: Scout--PopupWindow.php
4 # Copyright 2009 Edward Almasy and Internet Scout
5 # http://scout.wisc.edu
6 #
7 
11 class PopupWindow {
12 
13  # ---- PUBLIC INTERFACE --------------------------------------------------
14 
21  function PopupWindow($PopupId, $DB, $UserId = NULL)
22  {
23  # save our window ID and database handle
24  $this->Id = intval($PopupId);
25  $this->DB = $DB;
26  $this->UserId = $UserId ? intval($UserId) : NULL;
27 
28  # set defaults
29  $this->Width = 400;
30  $this->Height = 200;
31  $this->ForceDisplay = FALSE;
32  $this->SeenCountThreshold = 5;
33  $this->SeenTimeThreshold = 60;
34  $this->CookieLifetimeInDays = 90;
35  }
36 
42  function Initialize($CountThreshold, $TimeThreshold)
43  {
44  $this->SeenCountThreshold = $CountThreshold;
45  $this->SeenTimeThreshold = $TimeThreshold;
46  $this->ShouldDisplay();
47  }
48 
52  function Width($NewWidth) { $this->Width = intval($NewWidth); }
56  function Height($NewHeight) { $this->Height = intval($NewHeight); }
57 
62  function Id() { return $this->Id; }
63 
68  function WillDisplay() { return $this->ShouldDisplay(); }
69 
74  function AlwaysDisplay($Display) { $this->ForceDisplay = $Display; }
75 
79  function PrintHeaderCode()
80  {
81  # if we should display the window
82  if ($this->ShouldDisplay())
83  {
84  ?>
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');
94  });
95  </script>
96  <?PHP
97  }
98  }
99 
104  {
105  # if we should display the window
106  if ($this->ShouldDisplay())
107  {
108  # display code for beginning of content section
109  ?><div id="PopupWindowContent<?PHP print($this->Id());
110  ?>" style="display: none;"><span><?PHP
111  }
112  }
113 
118  {
119  # if we should display the window
120  if ($this->ShouldDisplay())
121  {
122  # display code for end of content section
123  ?></span></div><?PHP
124  }
125  }
126 
127 
128  # ---- PRIVATE INTERFACE -------------------------------------------------
129 
130  private $Id;
131  private $DB;
132  private $UserId;
133  private $Width;
134  private $Height;
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;
144 
145  # determine whether pop-up window should be displayed
146  private function ShouldDisplay()
147  {
148  # if user requested always display return TRUE to caller
149  if ($this->ForceDisplay) { return TRUE; }
150 
151  # if we have already determined status for this window
152  if (isset($this->DisplayStatus))
153  {
154  # return status to caller
155  return $this->DisplayStatus;
156  }
157 
158  # if cookie is available
159  if (isset($_COOKIE["ScoutPopupCount".$this->Id])
160  && isset($_COOKIE["ScoutPopupFirstSeen".$this->Id]))
161  {
162  # if cookie seen count is below threshold
163  $Count = $_COOKIE["ScoutPopupCount".$this->Id];
164  if ($Count < $this->SeenCountThreshold)
165  {
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)));
172  }
173  else
174  {
175  # if enough time has elapsed and we are not sure about displaying window
176  if ((time() - $_COOKIE["ScoutPopupFirstSeen".$this->Id])
177  >= $this->SeenTimeThreshold)
178  {
179  # if cookie seen count is at threshold
180  if ($Count == $this->SeenCountThreshold)
181  {
182  # display the window
183  $Display = TRUE;
184 
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)));
191  }
192  else
193  {
194  # do not display the window
195  $Display = FALSE;
196  }
197  }
198  }
199  }
200  else
201  {
202  # set cookie
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)));
207  }
208 
209  # if we know the user ID
210  if ($this->UserId !== NULL)
211  {
212  # if we have seen this user ID before
213  $Count = $this->SeenCountForUserId();
214  if ($Count !== NULL)
215  {
216  # if user ID seen count is below threshold
217  if ($Count < $this->SeenCountThreshold)
218  {
219  # increase user ID seen count
220  $Count = $this->SeenCountForUserId($Count + 1);
221  }
222  else
223  {
224  # if enough time has elapsed
225  if ($this->SecondsSinceUserIdFirstSeen()
226  >= $this->SeenTimeThreshold)
227  {
228  # if user ID seen count is at threshold
229  if ($Count == $this->SeenCountThreshold)
230  {
231  # display the window (if not previously disallowed)
232  if (!isset($Display)) { $Display = TRUE; }
233 
234  # increase user ID seen count
235  $Count = $this->SeenCountForUserId($Count + 1);
236  }
237  else
238  {
239  # do not display the window
240  $Display = FALSE;
241  }
242  }
243  }
244  }
245  else
246  {
247  # add user ID to database
248  $Count = $this->SeenCountForUserId(1);
249  }
250  }
251 
252  # if we have seen this IP address before
253  $Count = $this->SeenCountForIPAddress();
254  if ($Count !== NULL)
255  {
256  # if IP address seen count is below threshold
257  if ($Count < $this->SeenCountThreshold)
258  {
259  # increase IP address seen count
260  $Count = $this->SeenCountForIPAddress($Count + 1);
261  }
262  else
263  {
264  # if enough time has elapsed
265  if ($this->SecondsSinceIPAddressFirstSeen() >= $this->SeenTimeThreshold)
266  {
267  # if IP address seen count is at threshold
268  if ($Count == $this->SeenCountThreshold)
269  {
270  # display the window (if not previously disallowed)
271  if (!isset($Display)) { $Display = TRUE; }
272 
273  # increase IP address seen count
274  $Count = $this->SeenCountForIPAddress($Count + 1);
275  }
276  else
277  {
278  # do not display the window
279  $Display = FALSE;
280  }
281  }
282  }
283  }
284  else
285  {
286  # add IP address to database
287  $Count = $this->SeenCountForIPAddress(1);
288  }
289 
290  # if we are still not sure whether to display the window
291  if (!isset($Display))
292  {
293  # do not display the window
294  $Display = FALSE;
295  }
296 
297  # save window display status
298  $this->DisplayStatus = $Display;
299 
300  # return window display status to caller
301  return $Display;
302  }
303 
304  # get/set seen count for current user ID (returns NULL if not set)
305  private function SeenCountForUserId($NewSeenCount = NULL)
306  {
307  # attempt to retrieve count from database
308  if (!isset($this->UserIdSeenCount))
309  {
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() )
315  {
316  $Tmp = $this->DB->FetchRow();
317  $this->UserIdSeenCount = $Tmp["SeenCount"];
318  $this->UserIdFirstSeen = $Tmp["FirstSeen"];
319  }
320  else
321  {
322  $this->UserIdSeenCount = NULL;
323  $this->UserIdFirstSeen = NULL;
324  }
325  }
326  $Count = $this->UserIdSeenCount;
327 
328  # if new count supplied
329  if ($NewSeenCount !== NULL)
330  {
331  # if count is already in database
332  if ($Count !== NULL)
333  {
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");
339  }
340  else
341  {
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(), "
346  .$NewSeenCount.")");
347  }
348 
349  # set current count to new count
350  $Count = $NewSeenCount;
351  }
352 
353  # return current count to caller
354  return $Count;
355  }
356 
357  # get/set seen count for IP address of current user (returns NULL if not set)
358  private function SeenCountForIPAddress($NewSeenCount = NULL)
359  {
360  # attempt to retrieve count from database
361  if (!isset($this->IPAddressSeenCount))
362  {
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() )
368  {
369  $Tmp = $this->DB->FetchRow();
370  $this->IPAddressSeenCount = $Tmp["SeenCount"];
371  $this->IPAddressFirstSeen = $Tmp["FirstSeen"];
372  }
373  else
374  {
375  $this->IPAddressSeenCount = NULL;
376  $this->IPAddressFirstSeen = NULL;
377  }
378  }
379  $Count = $this->IPAddressSeenCount;
380 
381  # if new count supplied
382  if ($NewSeenCount !== NULL)
383  {
384  # if count is already in database
385  if ($Count !== NULL)
386  {
387  # update count in database
388  $this->DB->Query("UPDATE PopupLog SET SeenCount = ".$NewSeenCount
389  ." WHERE SigOne = ".$this->UserIPSigOne()
390  ." AND SigTwo = ".$this->UserIPSigTwo());
391  }
392  else
393  {
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())");
401  }
402 
403  # set current count to new count
404  $Count = $NewSeenCount;
405  }
406 
407  # return current count to caller
408  return $Count;
409  }
410 
411  # get seconds since current user ID first seen
412  private function SecondsSinceUserIdFirstSeen()
413  {
414  if (!isset($this->UserIdFirstSeen)) { $this->SeenCountForUserId(); }
415  return time() - strtotime($this->UserIdFirstSeen);
416  }
417 
418  # get seconds since IP address of current user first seen
419  private function SecondsSinceIPAddressFirstSeen()
420  {
421  if (!isset($this->IPAddressFirstSeen)) { $this->SeenCountForIPAddress(); }
422  return time() - strtotime($this->IPAddressFirstSeen);
423  }
424 
425  # return signature values for IP address of current user
426  private function UserIPSigOne()
427  {
428  $Bytes = explode(".", $_SERVER["REMOTE_ADDR"]);
429  return (count($Bytes) != 4) ? 0
430  : (intval($Bytes[0]) * 256) + intval($Bytes[1]);
431  }
432  private function UserIPSigTwo()
433  {
434  $Bytes = explode(".", $_SERVER["REMOTE_ADDR"]);
435  return (count($Bytes) != 4) ? 0
436  : (intval($Bytes[2]) * 256) + intval($Bytes[3]);
437  }
438 }
439 
440 
441 ?>