| 1 |
<?php
|
| 2 |
// $Id: $
|
| 3 |
|
| 4 |
$debug = (int)$_GET['debug'];
|
| 5 |
$group = $_GET['group'];
|
| 6 |
$up = (int)$_GET['up'];
|
| 7 |
$cache = $_GET['cache'];
|
| 8 |
$hostid = $_GET['k'];
|
| 9 |
|
| 10 |
// Set defaults.
|
| 11 |
if (!$group) {
|
| 12 |
$group = 'default';
|
| 13 |
}
|
| 14 |
if (!$cache) {
|
| 15 |
$cache = 'database';
|
| 16 |
}
|
| 17 |
|
| 18 |
if ($debug) {
|
| 19 |
echo "Group: '$group'<br />";
|
| 20 |
echo "Updirs: '$up'<br />";
|
| 21 |
echo "Cache: '$cache'<br />";
|
| 22 |
echo "Host: '$hostid'<br />";
|
| 23 |
exit;
|
| 24 |
}
|
| 25 |
|
| 26 |
header("Content-Type: application/x-javascript; charset=utf-8");
|
| 27 |
|
| 28 |
// Move process to top level Drupal directory so we can perform a bootstrap.
|
| 29 |
for ($i = 1; $i <= $up; $i++) {
|
| 30 |
chdir('..');
|
| 31 |
}
|
| 32 |
|
| 33 |
switch ($cache) {
|
| 34 |
case 'database':
|
| 35 |
// Include just enough to make queries and to include other modules.
|
| 36 |
include_once './includes/bootstrap.inc';
|
| 37 |
include_once './includes/module.inc';
|
| 38 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
|
| 39 |
|
| 40 |
// Query to randomly determine which ad to display.
|
| 41 |
$ad = db_fetch_object(db_query("SELECT a.aid, a.adtype, a.redirect FROM {ads} a INNER JOIN {ad_groups} g ON a.gid = g.gid WHERE a.adstatus = 'active' AND g.name = '%s' ORDER BY RAND() LIMIT 1", $group));
|
| 42 |
if (empty($ad)) {
|
| 43 |
echo "No ad found.";
|
| 44 |
return;
|
| 45 |
}
|
| 46 |
|
| 47 |
// Include appropriate module for displaying selected ad.
|
| 48 |
$module = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s'", "ad_$ad->adtype"));
|
| 49 |
include_once "$module";
|
| 50 |
|
| 51 |
print "document.write('". module_invoke("ad_$ad->adtype", 'display_ad', $ad) ."')";
|
| 52 |
|
| 53 |
// Update view statistics.
|
| 54 |
db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE aid = %d AND action = 'view' AND date = %d AND hostid = '%s'", $ad->aid, date('YmdH'), $hostid);
|
| 55 |
// If column doesn't already exist, we need to add it.
|
| 56 |
if (!db_affected_rows()) {
|
| 57 |
db_query("INSERT INTO {ad_statistics} (aid, date, action, hostid, count) VALUES(%d, %d, 'view', '%s', 1)", $ad->aid, date('YmdH'), $hostid);
|
| 58 |
// If another process already added this row our INSERT will fail, if so
|
| 59 |
// we still need to increment it so we don't loose a view.
|
| 60 |
if (!db_affected_rows()) {
|
| 61 |
db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE aid = %d AND action = 'view' AND date = %d AND hostid = '%s'", $ad->aid, date('YmdH'), $hostid);
|
| 62 |
}
|
| 63 |
}
|
| 64 |
break;
|
| 65 |
|
| 66 |
case 'file':
|
| 67 |
/**
|
| 68 |
* TODO: Implement file-based caching. Database caching is easier, so
|
| 69 |
* we'll start there.
|
| 70 |
*/
|
| 71 |
print "document.write('The file cache is not implemented yet.')";
|
| 72 |
|
| 73 |
default:
|
| 74 |
/**
|
| 75 |
* TODO: The goal here is to provide alternative caching mechanisms without
|
| 76 |
* having to patch this file. The trick is in locating the required file
|
| 77 |
* that we will need to include and call into. Perhaps we will need to
|
| 78 |
* create hard rules (like 'the file needs to be in the top level directory,
|
| 79 |
* and named 'ad_cache_TYPE' where TYPE is replaced with the cache type.
|
| 80 |
* We can then simply call it and pass in all necessary arguments.
|
| 81 |
* Something like:
|
| 82 |
* $name = "ad_cache_$cache";
|
| 83 |
* include_once "$name.php";
|
| 84 |
* $name($group);
|
| 85 |
*/
|
| 86 |
print "document.write('External cache types are not implemented yet.')";
|
| 87 |
}
|
| 88 |
|
| 89 |
?>
|