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