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)
\n"; pierre@0: } pierre@0: echo '
'; 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: return $output; pierre@0: } pierre@0: pierre@1: /* pierre@1: * When debugging, strip away distracting header errors. Dump all other errors. pierre@0: */ pierre@1: function _debug_error_handler($errno, $errstr, $errfile = NULL, $errline = 0, $errcontext = NULL) { pierre@1: if (!preg_match('/Cannot modify header information/', $errstr) && pierre@1: !preg_match('/Cannot send session cache limiter/', $errstr)) { pierre@1: echo "PHP: errno($errno): $errstr "; pierre@1: if ($errfile && $errline) { pierre@1: echo "; Line $errline in [$errfile]"; pierre@1: } pierre@1: echo "
\n"; pierre@1: if (!empty($errcontext) && adserve_variable('debug') >= 5) { pierre@1: echo 'Error context:
';
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) ."
\n"; pierre@1: _debug_memory(); pierre@1: } pierre@1: if (!$error_handler) { pierre@1: set_error_handler('_debug_error_handler'); pierre@1: $error_handler = TRUE; pierre@1: } pierre@0: echo "$text
\n"; pierre@0: } pierre@0: } pierre@0: pierre@1: function _debug_memory() { pierre@1: $memory = ''; pierre@1: if (adserve_variable('debug') && function_exists('memory_get_usage')) { pierre@1: $memory = number_format(round(memory_get_usage() / 1024, 3), 3); pierre@1: echo "Memory usage: $memory K
\n"; pierre@0: } pierre@0: } pierre@0: pierre@0: /** pierre@0: * Include Drupal's bootstrap.inc. pierre@0: */ pierre@0: function adserve_include_drupal() { pierre@0: // For optimal performance set DRUPAL_ROOT at the top of this file. pierre@0: if (defined('DRUPAL_ROOT')) { pierre@0: if (is_dir(DRUPAL_ROOT) && file_exists(DRUPAL_ROOT .'/includes/bootstrap.inc')) { pierre@0: chdir(DRUPAL_ROOT); pierre@0: adserve_variable('root_dir', DRUPAL_ROOT); pierre@0: } pierre@0: else { pierre@0: echo 'Invalid DRUPAL_ROOT ('. DRUPAL_ROOT .') defined in adserve.inc'; pierre@0: } pierre@0: } pierre@0: else { pierre@0: $path = explode('/', adserve_variable('ad_dir')); pierre@0: while (!empty($path)) { pierre@0: // Search for top level Drupal directory to perform bootstrap. pierre@0: chdir(implode('/', $path)); pierre@0: if (file_exists('./includes/bootstrap.inc')) { pierre@0: adserve_variable('root_dir', getcwd()); pierre@0: break; pierre@0: } pierre@0: array_pop($path); pierre@0: } pierre@0: } pierre@0: require_once adserve_variable('root_dir') .'/includes/bootstrap.inc'; pierre@0: } pierre@0: pierre@0: /** pierre@0: * Include the necessary files and call the Drupal bootstrap. pierre@0: */ pierre@0: function adserve_bootstrap($bootstrap = NULL) { pierre@0: adserve_include_drupal(); pierre@0: pierre@0: // If no specific bootstrap is specified, do a full bootstrap. pierre@0: if (!isset($bootstrap)) { pierre@0: $bootstrap = DRUPAL_BOOTSTRAP_FULL; pierre@0: } pierre@0: pierre@1: echo _debug_echo("Drupal bootstrap '$bootstrap'."); pierre@0: pierre@0: drupal_bootstrap($bootstrap); pierre@1: echo _debug_echo("Drupal bootstrap complete."); pierre@0: } pierre@0: pierre@0: /** pierre@0: * Display additional debug information. pierre@0: */ pierre@0: function adserve_debug() { pierre@0: if (adserve_variable('debug')) { pierre@0: echo "Root drupal directory detected as '". adserve_variable('root_dir') ."'.
\n
\n"; pierre@0: pierre@0: $ad_dir = adserve_variable('ad_dir'); pierre@1: $files = array("$ad_dir/serve.php", "$ad_dir/adserve.inc", "$ad_dir/adcache.inc", "$ad_dir/ad.module"); pierre@1: if (adserve_variable('debug') >= 2) { pierre@0: $files = array_merge($files, array("$ad_dir/ad.install")); pierre@0: } pierre@1: if (adserve_variable('debug') >= 3) { pierre@1: $files = array_merge($files, array("$ad_dir/image/ad_image.module", "$ad_dir/image/ad_image.install", "$ad_dir/text/ad_text.module", "$ad_dir/text/ad_text.install", "$ad_dir/embed/ad_embed.module", "$ad_dir/report/ad_report.module", "$ad_dir/notify/ad_notify.module", "$ad_dir/notify/ad_notify.install", "$ad_dir/cache/file/ad_cache_file.inc", "$ad_dir/cache/file/ad_cache_file.module", "$ad_dir/permission/ad_permission.module", "$ad_dir/weight/probability/ad_weight_probability.module", "$ad_dir/weight/probability/ad_weight_probability.inc")); pierre@0: } pierre@0: foreach ($files as $file) { pierre@0: if (!file_exists($file)) { pierre@0: echo "Error: '$file' does not exist!
\n"; pierre@0: } pierre@0: else if (!is_readable($file)) { pierre@0: echo "Error: '$file' is not readable!
\n"; pierre@0: } pierre@0: else { pierre@0: $fd = fopen($file, 'r'); pierre@0: while (!feof($fd)) { pierre@0: $line = fgets($fd); pierre@0: if (substr($line, 0, 5) == ""; pierre@0: break; pierre@0: } pierre@0: } pierre@0: } pierre@0: } pierre@0: echo "
\n"; pierre@0: } pierre@0: } pierre@0: