View source
<?php
function adserve_cache() {
static $functions = array();
$args = func_get_args();
$function = array_shift($args);
_debug_echo("adserve_cache function({$function})");
if (!isset($functions[$function])) {
$cache = adserve_variable('adcache');
$test = "ad_cache_{$cache}_{$function}";
if (!function_exists($test)) {
_debug_echo("Cache function '{$test}' does not exist.\n");
$test = "adserve_cache_{$function}";
}
$functions[$function] = $test;
}
if (function_exists($functions[$function])) {
_debug_echo("Invoking cache function '" . $functions[$function] . "'.");
return call_user_func_array($functions[$function], $args);
}
else {
_debug_echo("Cache function '" . $functions[$function] . "' does not exist.\n");
}
return array();
}
function adserve_invoke_hook() {
static $hooks = array();
$args = func_get_args();
$hook = array_shift($args);
$action = array_shift($args);
_debug_echo("adserve_invoke_hook hook({$hook}) action({$action})");
if (!isset($hooks[$hook])) {
$hooks[$hook] = adserve_cache('hook', $hook);
if (is_array($hooks[$hook]) && !empty($hooks[$hook]) && is_array($hooks[$hook]['file'])) {
foreach ($hooks[$hook]['file'] as $files) {
if (is_array($files)) {
foreach ($files as $file) {
$include_file = adserve_variable('root_dir') . '/' . $file;
if (file_exists($include_file) && is_file($include_file)) {
_debug_echo("Including file: '{$include_file}'.");
include_once $include_file;
}
else {
_debug_echo("Failed to include file: '{$include_file}'.");
}
}
}
}
}
}
$return = array();
if (is_array($hooks[$hook]) && !empty($hooks[$hook]) && is_array($hooks[$hook]['function'])) {
foreach ($hooks[$hook]['function'] as $weight => $functions) {
foreach ($functions as $function) {
if (function_exists($function)) {
_debug_echo("Invoking '{$function}'.");
$return[] = call_user_func_array($function, $args);
}
else {
_debug_echo("Function '{$function}' does not exist.\n");
}
}
}
}
else {
$function = "adserve_hook_{$hook}";
if (function_exists($function)) {
_debug_echo("Invoking '{$function}'.");
$return[] = call_user_func_array($function, $args);
}
else {
_debug_echo("Function '{$function}' does not exist.\n");
}
}
switch ($action) {
case 'intersect':
if (sizeof($return) == 1) {
return $return[0];
}
else {
return call_user_func_array('array_intersect', $return);
}
case 'merge':
if (sizeof($return) == 1) {
return $return[0];
}
else {
$merge = array();
foreach ($return as $array) {
$merge += $array;
}
return $merge;
}
case 'first':
foreach ($return as $item) {
if (is_array($item) && !empty($item)) {
return $item;
}
}
return array();
case 'append':
$append = '';
foreach ($return as $item) {
if (!is_array($item)) {
$append .= $item;
}
}
return $append;
default:
case 'raw':
default:
return $return;
}
}
function adserve_cache_open() {
adserve_bootstrap();
}
function adserve_cache_get_cache($data = NULL) {
static $cache = NULL;
if (is_null($cache)) {
$cache = module_invoke_all('ad_build_cache');
}
if ($data) {
if (isset($cache[$data])) {
return $cache[$data];
}
else {
return NULL;
}
}
return $cache;
}
function adserve_cache_hook($hook) {
static $cache = NULL;
if (is_null($cache)) {
$external = adserve_cache('get_cache');
$cache = adserve_cache('build_hooks', $external);
}
if (is_array($cache) && is_array($cache["hook_{$hook}"])) {
_debug_echo("Invoking hook '{$hook}'.");
return $cache["hook_{$hook}"];
}
_debug_echo("Did not find hook '{$hook}'.");
}
function adserve_cache_build_hooks($cache) {
$return = array();
if (is_array($cache)) {
foreach ($cache as $module => $hooks) {
foreach (array(
'hook_init',
'hook_filter',
'hook_weight',
'hook_select',
'hook_init_text',
'hook_exit_text',
'hook_increment_extra',
) as $hook) {
if (is_array($hooks[$hook])) {
$weight = isset($hooks[$hook]['weight']) ? (int) $hooks[$hook]['weight'] : 0;
$return[$hook]['file'][$weight][] = $hooks[$hook]['file'];
$return[$hook]['function'][$weight][] = $hooks[$hook]['function'];
}
}
}
}
return $return;
}
function adserve_cache_id($type, $id) {
switch ($type) {
case 'nids':
$result = db_query("SELECT aid FROM {ads} WHERE adstatus = 'active' AND aid IN(%d)", $id);
break;
case 'tids':
$result = db_query("SELECT a.aid FROM {ads} a INNER JOIN {term_node} n ON a.aid = n.nid WHERE a.adstatus = 'active' AND n.tid IN(%d)", $id);
break;
case 'default':
$result = db_query("SELECT a.aid FROM {ads} a LEFT JOIN {term_node} n ON a.aid = n.nid WHERE a.adstatus = 'active' AND n.tid IS NULL");
break;
default:
_debug_echo("Unsupported type '{$type}'.");
}
$ids = array();
while ($ad = db_fetch_object($result)) {
$ids[$ad->aid] = $ad->aid;
}
$number = sizeof($ids);
return $ids;
}
function adserve_hook_filter($ids, $hostid) {
return $ids;
}
function adserve_cache_display_ad($id) {
static $modules = array();
$ad = node_load($id);
if (!isset($modules[$ad->adtype])) {
$modules[$ad->adtype] = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s'", "ad_{$detail->adtype}"));
}
_debug_echo("Ad type '{$ad->adtype}', loading module '" . $modules[$ad->adtype] . "'");
return module_invoke("ad_{$ad->adtype}", 'display_ad', $ad);
}
function adserve_cache_validate($aids, $displayed, $hostid) {
$valid = array();
foreach ($aids as $aid) {
if (!in_array($aid, $displayed)) {
$valid[] = $aid;
}
}
return $valid;
}
function adserve_cache_increment($action, $aid) {
$hostid = adserve_variable('hostid');
_debug_echo("adserve_increment action({$action}) aid({$aid}) hostid({$hostid})");
adserve_bootstrap();
$extra = adserve_invoke_hook('increment_extra', 'merge', $action, $aid);
if (is_array($extra) && !empty($extra)) {
$extra = implode('|,|', $extra);
}
adserve_variable('extra', $extra);
_debug_echo("adserve_increment extra({$extra})");
db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND extra = '%s' AND hostid = '%s'", $aid, $action, date('YmdH'), adserve_variable('group'), $extra, $hostid);
if (!db_affected_rows()) {
db_query("INSERT INTO {ad_statistics} (aid, date, action, adgroup, extra, hostid, count) VALUES(%d, %d, '%s', '%s', '%s', '%s', 1)", $aid, date('YmdH'), $action, adserve_variable('group'), $extra, $hostid);
if (!db_affected_rows()) {
db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND extra = '%s' AND hostid = '%s'", $aid, $action, date('YmdH'), adserve_variable('group'), $extra, $hostid);
}
}
if ($action == 'view') {
$ad = db_fetch_object(db_query('SELECT maxviews, activated FROM {ads} WHERE aid = %d', $aid));
if ($ad->maxviews) {
$views = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, date('YmdH', $ad->activated)));
if ($views >= $ad->maxviews) {
db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $aid);
ad_statistics_increment('autoexpired', $aid);
ad_statistics_increment('expired', $aid);
}
}
}
}
function adserve_hook_select($ids, $quantity = 1, $hostid = '') {
$select = 0;
if (is_array($ids)) {
$ads = $ids;
foreach ($ids as $key => $value) {
$available = sizeof($ads);
$select++;
_debug_echo("Randomly selecting ad {$select} of {$quantity}.");
$id = 0;
if ($id == 0) {
$id = $available > 1 ? $ads[mt_rand(0, $available - 1)] : $ads[0];
_debug_echo("Randomly selected ID: {$id}.");
$selected[] = $id;
$ads = adserve_cache('validate', $ads, array(
$id,
), $hostid);
}
if ($quantity == $select || !count($ads)) {
break;
}
}
}
if ($select < $quantity) {
_debug_echo('No more advertisements available.');
}
return $selected;
}
function adserve_cache_get_ad_ids() {
static $displayed_count = 0;
_debug_echo('Entering default adserve_display.');
adserve_cache('open');
$hostid = adserve_variable('hostid') ? adserve_variable('hostid') : 'none';
_debug_echo("Hostid: '{$hostid}'.");
$init = adserve_invoke_hook('init', 'first', $hostid);
if (is_array($init) && !empty($init)) {
_debug_echo('Initialized externally.');
$quantity = $init['quantity'];
$id = $init['id'];
$aids = explode(',', $id);
$type = $init['type'];
}
else {
$quantity = adserve_variable('quantity');
if ($ids = adserve_cache('id', 'host', NULL, $hostid)) {
$id = implode(',', $ids);
$type = 'host';
}
else {
if ($id = adserve_variable('nids')) {
$type = 'nids';
adserve_variable('group', "n{$id}");
}
else {
if ($id = adserve_variable('tids')) {
$type = 'tids';
adserve_variable('group', "t{$id}");
}
else {
$id = 0;
$type = 'default';
adserve_variable('group', "{$id}");
}
}
}
_debug_echo("Searching {$type}: {$id}");
$aids = adserve_cache('id', $type, $id, $hostid);
}
$number_of_ads = sizeof($aids);
_debug_echo("Total ads: '{$number_of_ads}'.");
$displayed = adserve_variable("{$type}-displayed");
if (!is_array($displayed)) {
$displayed = array();
}
_debug_echo('Already displayed: ' . sizeof($displayed));
$aids = adserve_cache('validate', $aids, $displayed, $hostid);
$number_of_ads = sizeof($aids);
_debug_echo("Validated ads: '{$number_of_ads}'.");
$aids = adserve_invoke_hook('filter', 'intersect', $aids, $hostid);
$number_of_ads = sizeof($aids);
_debug_echo("Filtered ads: '{$number_of_ads}'.");
$aids = adserve_invoke_hook('weight', 'first', $aids, $hostid);
$number_of_ads = sizeof($aids);
_debug_echo("Weighted ads: '{$number_of_ads}'.");
$aids = adserve_invoke_hook('select', 'first', $aids, $quantity, $hostid);
$number_of_ads = sizeof($aids);
_debug_echo("Selected ads: '{$number_of_ads}'.");
adserve_variable("{$type}-displayed", array_merge($aids, $displayed));
return $aids;
}
function adserve_cache_display($ids) {
$output = '';
$ads = 0;
foreach ($ids as $id) {
$ad = adserve_cache('display_ad', $id);
_debug_echo('ad: ' . htmlentities($ad));
if ($output) {
$group = adserve_variable('group');
$output .= "<div class=\"advertisement-space\" id=\"space-{$group}-{$ads}\"></div>";
}
$output .= $ad;
if (adserve_variable('ad_display') == 'raw') {
$output .= ad_display_image($ad);
}
else {
adserve_cache('increment', 'view', $id);
}
$ads++;
}
if (empty($ids)) {
adserve_variable('error', TRUE);
$output = 'No active ads were found in ' . adserve_variable('group');
adserve_cache('increment', 'count', NULL);
}
adserve_cache('close');
$hostid = adserve_variable('hostid');
$group = adserve_variable('group');
$extra = adserve_variable('extra');
if ($url = htmlentities(adserve_variable('url'))) {
$replace = "/{$group}/{$extra}/{$hostid}?u={$url}";
}
else {
$replace = "/{$group}/{$extra}/{$hostid}";
}
$output = preg_replace('&/@HOSTID___&', $replace, $output);
if (adserve_variable('error')) {
$output = "<!-- {$output} -->";
}
$init_text = adserve_invoke_hook('init_text', 'append');
$exit_text = adserve_invoke_hook('exit_text', 'append');
$output = $init_text . $output . $exit_text;
switch (adserve_variable('ad_display')) {
case 'javascript':
default:
$output = str_replace(array(
"\r",
"\n",
"<",
">",
"&",
), array(
'\\r',
'\\n',
'\\x3c',
'\\x3e',
'\\x26',
), addslashes($output));
if (!adserve_variable('debug')) {
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Content-Type: application/x-javascript; charset=utf-8');
}
print "document.write('{$output}');";
exit(0);
case 'iframe':
case 'jquery':
if (!adserve_variable('debug')) {
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
}
else {
_debug_echo('Output: ' . htmlentities($output));
}
print "{$output}";
exit(0);
case 'raw':
_debug_echo('Output: ' . htmlentities($output));
chdir(adserve_variable('ad_dir'));
return $output;
}
_debug_echo('Output: ' . htmlentities($output));
return $output;
}