.
*
* By default, adserve configuration happens dynamically as ads are served.
* However, it is possible to override dynamic settings with static defaults.
* Refer to the documentation/ADSERVE_CONFIGURATION.txt for details on adding
* adserve overrides to settings.php.
*
* Note that the path to Drupal's root directory can not be overriden in
* settings.php as adserve needs this path to find settings.php in the first
* place. To hard code the path to Drupal's root directory, uncomment the
* following define statement, and set the correct path. This is not generally
* required. On a Unix server this path will be something like '/path/to/web'.
* On a Windows server this path will be something like 'D:\path\to\web'.
*/
//define('DRUPAL_ROOT', '/var/www/html');
/**
* The main adserve logic.
*/
function adserve_ad($options = array()) {
static $displayed_count = 0;
// if no $options are passed in, assume we're using JavaScript
if (!empty($options)) {
adserve_variable('variable_load', $options);
}
else {
adserve_variable('variable_load');
}
// include Drupal's settings.php
adserve_bootstrap(0);
// if debug enabled, dump current state
adserve_debug();
// start with 'error' set to false
adserve_variable('error', FALSE);
// invoke cache function (file already included in adserve_variable)
$ids = adserve_cache('get_ad_ids');
// display the advertisement(s)
return adserve_cache('display', $ids);
}
/**
* Retrieve variables from $_GET array or from passed in $value array.
*/
function adserve_variable($variable, $value = NULL) {
global $conf;
static $variables = NULL, $overridden = NULL, $cache_loaded = array();
// Declare variables if not already declared.
if ($variables === NULL) {
$variables = new stdClass();
}
// Update the value, if set.
if (isset($value)) {
$variables->$variable = $value;
}
if (!isset($variables->loaded) || $variable == 'variable_load') {
if ($variable == 'variable_load' && isset($value)) {
$values['debug'] = isset($value['debug']) ? $value['debug'] : '';
$values['c'] = isset($value['adcache']) ? $value['adcache'] : '';
$values['n'] = isset($value['nids']) ? $value['nids'] : '';
$values['t'] = isset($value['tids']) ? $value['tids'] : '';
$values['k'] = isset($value['hostid']) ? $value['hostid'] : '';
$values['q'] = isset($value['quantity']) ? $value['quantity'] : 1;
$values['m'] = isset($value['ad_display']) ? $value['ad_display'] : 0;
unset($value);
}
else {
$values = $_GET;
}
// Don't use getcwd as path may involve symbolic links
$variables->ad_dir = dirname($_SERVER['SCRIPT_FILENAME']);
// 'debug' is an integer.
$variables->debug = isset($values['debug']) ? (int)$values['debug'] : 0;
// Cache types are comprised of only letters.
$variables->adcache = isset($values['c']) ? preg_replace('/[^a-zA-Z]/', '', $values['c']) : 'none';
// Nids is an integer or a ",".
$variables->nids = isset($values['n']) ? preg_replace('/[^0-9,]/', '', $values['n']) : '';
// Tids is an integer or a ",".
$variables->tids = isset($values['t']) ? preg_replace('/[^0-9,]/', '', $values['t']) : '';
// Hostid is an md5() which is comprised of numbers and letters a-f.
$variables->hostid = isset($values['k']) ? preg_replace('/[^0-9a-f]/', '', $values['k']) : '';
// Click url
$variables->url = isset($values['u']) ? $values['u'] : '';
if (!$variables->url) {
$variables->url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
}
// Quantity is an integer.
$variables->quantity = isset($values['q']) ? (int)$values['q'] : 0;
// Ad ID is an integer.
$variables->aid = isset($values['a']) ? (int)$values['a'] : 0;
// Method is compriese of only letters.
$variables->ad_display = isset($values['m']) ? preg_replace('/[^a-zA-Z]/', '', $values['m']) : 'javascript';
// Set defaults.
$variables->quantity = $variables->quantity ? $variables->quantity : 1;
if ($variables->debug) {
foreach ($variables as $variable => $val) {
echo "$variable: '$val'
\n";
}
if ($variables->debug == 1) exit;
}
$variables->loaded = TRUE;
// Override the value, if set during initialization.
if (isset($value)) {
$variables->$variable = $value;
}
}
if (!$overridden) {
if (isset($conf)) {
foreach ($conf as $var => $val) {
$variables->$var = $val;
if ($variables->debug) {
echo "Override $var: '$val'
\n";
}
}
$overridden = TRUE;
}
}
if (!isset($cache_loaded[$variables->adcache])) {
// Retrieve variables defined by cache plugin, if enabled.
if ($variables->adcache != 'none') {
$includes = array($variables->ad_dir ."/cache/$variables->adcache/ad_cache_$variables->adcache.inc", $variables->ad_dir ."/../ad_$variables->adcache/ad_cache_$variables->adcache.inc");
foreach ($includes as $include) {
if (file_exists($include)) {
if ($variables->debug) {
echo "Attempting to include cache include file '$include'.
\n";
}
require_once($include);
}
else if ($variables->debug) {
echo "Failed to find cache include file '$include'.
\n";
}
$function = 'ad_cache_'. $variables->adcache .'_variables';
if (function_exists($function)) {
$external_variables = $function();
foreach ($external_variables as $key => $val) {
if (!isset($variables->$key)) {
$variables->$key = $val;
}
}
}
}
}
$cache_loaded[$variables->adcache] = TRUE;
}
if ($variable == 'variable_dump') {
echo "Dumping \$variables:
\n";
echo '
'; foreach ($variables as $var => $val) { echo " $var($val)'; } if (isset($variables->$variable)) { return $variables->$variable; } else { return NULL; } } /** * Invoke a function in the specified file. */ function adserve_invoke_file($function, $arg1 = NULL, $arg2 = NULL) { $output = ''; if (function_exists($function)) { $output = $function($arg1, $arg2); } else if (adserve_variable('debug')) { echo "Function '$function' does not exist.
\n"; } echo '
'; print_r($errcontext); echo ''; } } } /** * Dump debug message to screen; set custom error handler. */ function _debug_echo($text) { static $error_handler = FALSE; static $time = 0; if (adserve_variable('debug')) { if ($time < time()) { $time = time(); echo '--> Time mark: '. date('H:i:s', $time) ."