| 1 |
<?php
|
| 2 |
// $Id: banner_file.php,v 1.20.2.3 2006/09/23 14:39:43 wulff Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* The file system cache handler for the banner module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
header("Content-Type: application/x-javascript; charset=utf-8");
|
| 10 |
|
| 11 |
$group = (int) $_GET['group'];
|
| 12 |
$count = (int) $_GET['count'];
|
| 13 |
$terms = (string) $_GET['terms'];
|
| 14 |
$path = (string) $_GET['path'];
|
| 15 |
$max = (int) $_GET['max'];
|
| 16 |
$refresh = 0;
|
| 17 |
$output = '';
|
| 18 |
|
| 19 |
for ($i = 1; $i <= $max; $i++) {
|
| 20 |
$cache_file = $path.'/.'.$i.'.banner.cache';
|
| 21 |
if (!$fd = @fopen($cache_file, "r+")) {
|
| 22 |
if ($i == $max) {
|
| 23 |
// we failed to open all cache files, try refreshing
|
| 24 |
$refresh = 1;
|
| 25 |
}
|
| 26 |
continue;
|
| 27 |
}
|
| 28 |
// determine whether to use a blocking lock or a non-blocking lock
|
| 29 |
if ($i < $max) {
|
| 30 |
if (!flock($fd, LOCK_EX|LOCK_NB)) {
|
| 31 |
@fclose($fd);
|
| 32 |
continue;
|
| 33 |
}
|
| 34 |
}
|
| 35 |
else { // last cache file, use a blocking-lock
|
| 36 |
if (!flock($fd, LOCK_EX)) {
|
| 37 |
// something is very wrong, try refreshing cache
|
| 38 |
@fclose($fd);
|
| 39 |
$refresh = 1;
|
| 40 |
break;
|
| 41 |
}
|
| 42 |
}
|
| 43 |
$contents = fread($fd, filesize($cache_file));
|
| 44 |
|
| 45 |
$struct = unserialize($contents);
|
| 46 |
$last_updated = $struct[2];
|
| 47 |
|
| 48 |
$tids = explode(',', $terms);
|
| 49 |
|
| 50 |
$ballot_and = array();
|
| 51 |
foreach ($tids as $tid) {
|
| 52 |
if (isset($struct[0][$tid][$group])) {
|
| 53 |
$ballot_and[] = $struct[0][$tid][$group];
|
| 54 |
}
|
| 55 |
}
|
| 56 |
$ballot_or = array();
|
| 57 |
foreach ($tids as $tid) {
|
| 58 |
if (isset($struct[0][$tid][$group])) {
|
| 59 |
foreach ($struct[0][$tid][$group] as $nid)
|
| 60 |
$ballot_or[] = $nid;
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
$ballot = array();
|
| 65 |
if (count($ballot_and) >= 2) {
|
| 66 |
$ballot = call_user_func_array('array_intersect', $ballot_and);
|
| 67 |
}
|
| 68 |
if (empty($ballot)) {
|
| 69 |
$ballot = array_unique($ballot_or);
|
| 70 |
}
|
| 71 |
if (empty($ballot)) {
|
| 72 |
foreach ($struct[0][0][$group] as $nid) {
|
| 73 |
$ballot[] = $nid;
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
$counter = 0;
|
| 78 |
while ($counter < $count && count($ballot)) {
|
| 79 |
// select a random banner
|
| 80 |
$last = count($ballot) - 1;
|
| 81 |
if ($last > 0) {
|
| 82 |
$random = mt_rand(0, $last);
|
| 83 |
}
|
| 84 |
else {
|
| 85 |
$random = 0;
|
| 86 |
}
|
| 87 |
$banner_id = $ballot[$random];
|
| 88 |
|
| 89 |
// get banner and remove it from the ballot
|
| 90 |
$this_banner = $struct[1][$banner_id];
|
| 91 |
array_splice($ballot, $random, 1);
|
| 92 |
|
| 93 |
// add a view
|
| 94 |
$struct[1][$banner_id]->views_this++;
|
| 95 |
$struct[1][$banner_id]->views++;
|
| 96 |
$struct[1][$banner_id]->views_day++;
|
| 97 |
$struct[1][$banner_id]->views_week++;
|
| 98 |
|
| 99 |
// if needed, disable this banner
|
| 100 |
if ($this_banner->views_max > 0 && ($this_banner->views + 1) >= $this_banner->views_max) {
|
| 101 |
include_once './includes/bootstrap.inc';
|
| 102 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
|
| 103 |
// reached maximum views, set status to "blocked"
|
| 104 |
db_query('UPDATE {banner} SET workflow = 5 WHERE nid = %d', $banner_id);
|
| 105 |
$refresh = 1;
|
| 106 |
}
|
| 107 |
else if ($this_banner->views_day_max > 0 && ($this_banner->views_day + 1) >= $this_banner->views_day_max) {
|
| 108 |
include_once './includes/bootstrap.inc';
|
| 109 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
|
| 110 |
// reached day's maximum views, set status to "day's limit reached"
|
| 111 |
db_query('UPDATE {banner} SET workflow = 2 WHERE nid = %d', $banner_id);
|
| 112 |
$refresh = 1;
|
| 113 |
}
|
| 114 |
else if ($this_banner->views_week_max > 0 && ($this_banner->views_week + 1) >= $this_banner->views_week_max) {
|
| 115 |
include_once './includes/bootstrap.inc';
|
| 116 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
|
| 117 |
// reached week's maximum views, set status to "week's limit reached"
|
| 118 |
db_query('UPDATE {banner} SET workflow = 3 WHERE nid = %d', $banner_id);
|
| 119 |
$refresh = 1;
|
| 120 |
}
|
| 121 |
|
| 122 |
$output .= $this_banner->html;
|
| 123 |
|
| 124 |
$counter++;
|
| 125 |
}
|
| 126 |
|
| 127 |
// once every minute update views in db
|
| 128 |
if ($last_updated < (time() - 60)) {
|
| 129 |
$struct[2] = time();
|
| 130 |
$refresh = 1;
|
| 131 |
}
|
| 132 |
|
| 133 |
// dump back in cache
|
| 134 |
$data = serialize($struct);
|
| 135 |
rewind($fd);
|
| 136 |
fwrite($fd, $data, strlen($data));
|
| 137 |
flock($fd, LOCK_UN);
|
| 138 |
fclose($fd);
|
| 139 |
break;
|
| 140 |
}
|
| 141 |
|
| 142 |
if ($refresh) {
|
| 143 |
include_once './includes/bootstrap.inc';
|
| 144 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
|
| 145 |
drupal_set_header("Content-Type: application/x-javascript; charset=utf-8");
|
| 146 |
_banner_refresh_cache();
|
| 147 |
}
|
| 148 |
|
| 149 |
print $output;
|