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@0: // 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@0: adserve_bootstrap(0);
pierre@0:
pierre@0: adserve_debug();
pierre@0:
pierre@0: adserve_variable('error', FALSE);
pierre@0: $output = NULL;
pierre@0: if (adserve_variable('adcache') != 'none') {
pierre@0: /**
pierre@0: * Ad caches are defined through external modules. Ad caches are composed
pierre@0: * of a module 'ad_cache_TYPE.module' and an include file
pierre@0: * 'ad_cache_TYPE.inc' that live in the 'cache/TYPE' subdirectory where
pierre@0: * 'TYPE' is replaced with the type of cache. For example, the included
pierre@0: * file cache lives in 'cache/file'.
pierre@0: *
pierre@0: * The ad_cache_TYPE.inc file must have a function named ad_cache_TYPE()
pierre@0: * which is used to display ads. It can optionally include a function
pierre@0: * titled ad_cache_TYPE_variables used to extract any necessary
pierre@0: * variables from the global $_GET array (this can also be used to override
pierre@0: * values that would normally be set from $_GET). Any functions used
pierre@0: * by this code without bootstrapping Drupal should also be in this file.
pierre@0: *
pierre@0: * The ad_cache_TYPE.module file should define the drupal _help() hook
pierre@0: * so the module can be enabled. It should also define the _adcacheapi()
pierre@0: * hook allowing for configuration and processing. Any functions used by
pierre@0: * this code after bootstrapping Drupal should also be in this module.
pierre@0: *
pierre@0: * Refer to cache/file/* for an implementation example.
pierre@0: */
pierre@0: $function = 'ad_cache_'. adserve_variable('adcache');
pierre@0: $output = adserve_invoke_file($function);
pierre@0:
pierre@0: }
pierre@0:
pierre@0: // If there's no output, we assume either there's no cache enabled, or the
pierre@0: // cache failed.
pierre@0: // TODO: Log failures with the watchdog.
pierre@0: if ($output == NULL) {
pierre@0: if (adserve_variable('debug')) {
pierre@0: echo "No cache enabled.
\n";
pierre@0: }
pierre@0:
pierre@0: adserve_bootstrap();
pierre@0:
pierre@0: if (adserve_variable('nids')) {
pierre@0: $id = adserve_variable('nids');
pierre@0: $type = 'nids';
pierre@0: adserve_variable('group', "n$id");
pierre@0:
pierre@0: // Retrieve all active advertisements from the provided nid list.
pierre@0: $sql = "SELECT aid FROM {ads} WHERE adstatus = 'active' AND aid IN (%s)";
pierre@0: $result = db_query($sql, $id);
pierre@0:
pierre@0: if (adserve_variable('debug')) {
pierre@0: echo "Searching for ad from nid list: $id.
\n";
pierre@0: echo "Query: \"$sql;\"
\n";
pierre@0: }
pierre@0: }
pierre@0: else if (adserve_variable('tids')) {
pierre@0: $id = adserve_variable('tids');
pierre@0: $type = 'tids';
pierre@0: adserve_variable('group', "t$id");
pierre@0:
pierre@0: // Retrieve all active advertisements from the provided tid list.
pierre@0: $sql = "SELECT a.aid FROM {ads} a INNER JOIN {term_node} n ON a.aid = n.nid WHERE a.adstatus = 'active' AND n.tid IN (%s)";
pierre@0: $result = db_query($sql, $id);
pierre@0:
pierre@0: if (adserve_variable('debug')) {
pierre@0: echo "Searching for ad from tid list: $id.
\n";
pierre@0: echo "Query: \"$sql;\"
\n";
pierre@0: }
pierre@0: }
pierre@0: else {
pierre@0: $id = 0;
pierre@0: $type = 'default';
pierre@0: adserve_variable('group', "$id");
pierre@0:
pierre@0: // Randomly determine which ad to display from those that do not have
pierre@0: // any tid assigned to them.
pierre@0: $sql = "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";
pierre@0: $result = db_query($sql);
pierre@0:
pierre@0: if (adserve_variable('debug')) {
pierre@0: echo "Searching for ads with no tids.
\n";
pierre@0: echo "Query: \"$sql;\"
\n";
pierre@0: }
pierre@0: }
pierre@0:
pierre@0: // Build list of all available ads to choose from.
pierre@0: $available = array();
pierre@0: while ($ad = db_fetch_object($result)) {
pierre@0: $available[$ad->aid] = $ad->aid;
pierre@0: }
pierre@0: if (adserve_variable('debug')) {
pierre@0: echo 'Available ads: ';
pierre@0: if (sizeof($ads)) {
pierre@0: echo implode(', ', $available) ."
";
pierre@0: }
pierre@0: else {
pierre@0: echo 'none
';
pierre@0: }
pierre@0: }
pierre@0:
pierre@0: // Randomly select from available advertisements.
pierre@0: $selected = adserve_select_ad($available, adserve_variable('quantity'));
pierre@0:
pierre@0: $output = '';
pierre@0: $ads = 0;
pierre@0: $details = array();
pierre@0: $ids = array();
pierre@0: // Include appropriate module for displaying selected ad.
pierre@0: foreach ($selected as $aid) {
pierre@0: $ids[$aid] = $aid;
pierre@0: $ads++;
pierre@0: $detail = $details[$aid] = node_load($aid);
pierre@0: if (!isset($modules[$detail->adtype])) {
pierre@0: $modules[$detail->adtype] = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s'", 'ad_'. $detail->adtype));
pierre@0: }
pierre@0: if (adserve_variable('debug')) {
pierre@0: echo 'ad:
';
pierre@0: print_r($detail);
pierre@0: echo '
';
pierre@0: echo "Loading module '". $modules[$detail->adtype] ."'.
\n";
pierre@0: }
pierre@0: include_once $modules[$detail->adtype];
pierre@0:
pierre@0: if ($output) {
pierre@0: // Add a div between ads that themers can use to arrange ads when
pierre@0: // displaying more than one at a time.
pierre@0: $displayed_count++;
pierre@0: $output .= "";
pierre@0: }
pierre@0: $output .= module_invoke("ad_$detail->adtype", 'display_ad', $detail);
pierre@0:
pierre@0: // Update the ad's impressions counter.
pierre@0: if (adserve_variable('ad_display') == 'raw') {
pierre@0: $output .= ad_display_image($detail);
pierre@0: }
pierre@0: else {
pierre@0: adserve_increment($detail);
pierre@0: }
pierre@0: }
pierre@0: adserve_variable("$type-ids", $ids);
pierre@0: if (empty($ads)) {
pierre@0: adserve_variable('error', TRUE);
pierre@0: $output = 'No active ads were found in the '. (empty($nids) ? 'tids' : 'nids') ." '$id'.";
pierre@0: adserve_increment(NULL, 'count');
pierre@0: }
pierre@0: if (adserve_variable('debug')) {
pierre@0: echo "Ads displayed: $ads
";
pierre@0: }
pierre@0: }
pierre@0:
pierre@0: $hostid = adserve_variable('hostid');
pierre@0: $group = adserve_variable('group');
pierre@0: $replace = "/$group";
pierre@0: if (!empty($hostid)) {
pierre@0: $replace .= "/$hostid";
pierre@0: }
pierre@0: if ($url = htmlentities(adserve_variable('url'))) {
pierre@0: $replace .= "?u=$url";
pierre@0: }
pierre@0:
pierre@0: $output = preg_replace('&/@HOSTID___&', $replace, $output);
pierre@0: if (adserve_variable('error')) {
pierre@0: $output = "";
pierre@0: }
pierre@0:
pierre@0: /**
pierre@0: * Modules can add custom code to be displayed before or after ads are
pierre@0: * displayed. For example, you many want to add a tagline, "Powered by
pierre@0: * Drupal". To do so, define 'adserve_exit_text' within your module's
pierre@0: * adapi hook.
pierre@0: *
pierre@0: * Code sample for adserve_exit_text example:
pierre@0: *
pierre@0: * sample_adapi($op, $ad) {
pierre@0: * case 'adserve_exit_text':
pierre@0: * return array(
pierre@0: * 'sample' => array(
pierre@0: * 'text' => t('Powered by Drupal'),
pierre@0: * )
pierre@0: * );
pierre@0: * }
pierre@0: *
pierre@0: * As another example use case, you could also use the _init_text and
pierre@0: * _exit_text hooks to wrap all advertisements in a custom div.
pierre@0: */
pierre@0: $init = TRUE;
pierre@0: foreach (array('adserve_init_text', 'adserve_exit_text') as $hook) {
pierre@0: $result = adserve_invoke_hook($hook);
pierre@0: if (is_array($result)) {
pierre@0: $append = '';
pierre@0: foreach ($result as $text) {
pierre@0: if ($text['text']) {
pierre@0: $append .= $text['text'];
pierre@0: }
pierre@0: }
pierre@0: if ($init) {
pierre@0: $output = $append . $output;
pierre@0: }
pierre@0: else {
pierre@0: $output .= $append;
pierre@0: }
pierre@0: }
pierre@0: $init = FALSE;
pierre@0: }
pierre@0:
pierre@0: switch (adserve_variable('ad_display')) {
pierre@0: case 'iframe':
pierre@0: case 'jquery':
pierre@0: if (!adserve_variable('debug')) {
pierre@0: // Tell the web browser not to cache this frame so the ad refreshes
pierre@0: // each time the page is viewed.
pierre@0:
pierre@0: // Expires in the past:
pierre@0: header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
pierre@0: // Last load:
pierre@0: header('Last-Modified: '. gmdate('D, d M Y H:i:s') .' GMT');
pierre@0: // HTTP 1.1:
pierre@0: header('Cache-Control: no-store, no-cache, must-revalidate');
pierre@0: header('Cache-Control: post-check=0, pre-check=0', FALSE);
pierre@0: // HTTP 1.0:
pierre@0: header('Pragma: no-cache');
pierre@0: }
pierre@0: print "$output";
pierre@0: exit(0);
pierre@0: case 'javascript':
pierre@0: default:
pierre@0: $output = str_replace(array("\r", "\n", "<", ">", "&"),
pierre@0: array('\r', '\n', '\x3c', '\x3e', '\x26'),
pierre@0: addslashes($output));
pierre@0: if (!adserve_variable('debug')) {
pierre@0: // Tell the web browser not to cache this script so the ad refreshes
pierre@0: // each time the page is viewed.
pierre@0: // Expires in the past:
pierre@0: header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
pierre@0: // Last load:
pierre@0: header('Last-Modified: '. gmdate('D, d M Y H:i:s') .' GMT');
pierre@0: // HTTP 1.1:
pierre@0: header('Cache-Control: no-store, no-cache, must-revalidate');
pierre@0: header('Cache-Control: post-check=0, pre-check=0', FALSE);
pierre@0: // HTTP 1.0:
pierre@0: header('Pragma: no-cache');
pierre@0: // Output is a JavaScript:
pierre@0: header('Content-Type: application/x-javascript; charset=utf-8');
pierre@0: }
pierre@0: print "document.write('$output');";
pierre@0: exit(0);
pierre@0: case 'raw':
pierre@0: chdir(adserve_variable('ad_dir'));
pierre@0: return $output;
pierre@0: }
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@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@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@0: /**
pierre@0: * Invoke adserve hooks, defined in adapi with adserve_HOOK.
pierre@0: */
pierre@0: function adserve_invoke_hook($hook, $a1 = NULL, $a2 = NULL) {
pierre@0: if (adserve_variable('adcache') != 'none') {
pierre@0: $cache = adserve_variable('adcache');
pierre@0: _debug_echo("Invoking adserve hook '$hook' in $cache cache.");
pierre@0: // Get information from cache.
pierre@0: return adserve_invoke_file("ad_cache_{$cache}_$hook", $a1, $a2);
pierre@0: }
pierre@0: else {
pierre@0: _debug_echo("Invoking adserve hook '$hook'.");
pierre@0: // Get information from Drupal variable table.
pierre@0: $actions = variable_get($hook, '');
pierre@0: $return = array();
pierre@0: if (!empty($actions)) {
pierre@0: $actions = unserialize($actions);
pierre@0: foreach ($actions as $name => $action) {
pierre@0: if ($action['function']) {
pierre@0: $function = $action['function'];
pierre@0: if (!function_exists($function)) {
pierre@0: if ($action['path']) {
pierre@0: _debug_echo("Including file '". $action['path'] ."'.");
pierre@0: include_once($action['path']);
pierre@0: }
pierre@0: }
pierre@0: if (function_exists($function)) {
pierre@0: _debug_echo("Invoking function '$function'.");
pierre@0: $return[] = $function($a1, $a2);
pierre@0: }
pierre@0: else if (adserve_variable('debug')) {
pierre@0: echo "Function '$function' does not exist.
\n";
pierre@0: }
pierre@0: }
pierre@0: else {
pierre@0: $return[] = $action;
pierre@0: }
pierre@0: }
pierre@0: }
pierre@0: return $return;
pierre@0: }
pierre@0: // Retreive hook definition from cache if using, or from variable_get
pierre@0: // return hook action.
pierre@0: }
pierre@0:
pierre@0: function _debug_echo($text) {
pierre@0: if (adserve_variable('debug')) {
pierre@0: echo "$text
\n";
pierre@0: }
pierre@0: }
pierre@0:
pierre@0: /**
pierre@0: * Remove one or more ids from array.
pierre@0: * TODO: Optimize. Perhaps something like array_flip, unset, array_flip.
pierre@0: * @param $ids An array of ID's, ie array(5, 8, 9, 11).
pierre@0: * @param $remove An ID or an array of ID's to be removed from $ids.
pierre@0: */
pierre@0: function adserve_select_reindex($ids, $remove) {
pierre@0: $new = array();
pierre@0: // Walk through array of IDs and decide what to keep.
pierre@0: foreach ($ids as $id) {
pierre@0: // If $remove is an array, walk through array to decide fate of ID.
pierre@0: if (is_array($remove)) {
pierre@0: $keep = TRUE;
pierre@0: foreach ($remove as $rem) {
pierre@0: // Loop until we find one that matches or reach end of array.
pierre@0: if ($id == $rem) {
pierre@0: $keep = FALSE;
pierre@0: break;
pierre@0: }
pierre@0: }
pierre@0: if ($keep) {
pierre@0: $new[] = $id;
pierre@0: }
pierre@0: }
pierre@0: else {
pierre@0: if ($id != $remove) {
pierre@0: $new[] = $id;
pierre@0: }
pierre@0: }
pierre@0: }
pierre@0: return $new;
pierre@0: }
pierre@0:
pierre@0: /**
pierre@0: * Disabled: will be re-implemented with new adserve hooks introduced for
pierre@0: * geotargeting.
pierre@0: function adserve_invoke_weight($ads, $quantity = 1, $invalid = array()) {
pierre@0: $parent = adserve_variable('ad_dir') .'/weight';
pierre@0: if (is_dir($parent) && $handle = opendir($parent)) {
pierre@0: while ($dir = readdir($handle)) {
pierre@0: if (is_dir("$parent/$dir") && !in_array($dir, array('.', '..', 'CVS'))) {
pierre@0: $include = "$parent/$dir/ad_weight_$dir.inc";
pierre@0: if (file_exists($include)) {
pierre@0: require_once($include);
pierre@0: $function = "ad_weight_{$dir}_select_ad";
pierre@0: if (function_exists($function)) {
pierre@0: $return = $function($ads, $quantity, $invalid);
pierre@0: // First come, first serve. We found an ad_weight function that
pierre@0: // returned something, so we'll take it.
pierre@0: if ($return) {
pierre@0: return $return;
pierre@0: }
pierre@0: }
pierre@0: }
pierre@0: }
pierre@0: }
pierre@0: }
pierre@0: }
pierre@0: */
pierre@0:
pierre@0: /**
pierre@0: * Simple default function to randomly select an ad. Provides a hook to allow
pierre@0: * the definition of external display methods.
pierre@0: * @param An array of valid ad IDs, ie array(5, 8, 9, 11).
pierre@0: * @param Optional, how many unique ads to select.
pierre@0: * @param Optional, an array of invalid IDs.
pierre@0: */
pierre@0: function adserve_select_ad($ads, $quantity = 1, $invalid = array()) {
pierre@0: //adserve_invoke_weight($ads, $quantity, $invalid);
pierre@0:
pierre@0: $ids = array();
pierre@0: $id = 0;
pierre@0: $total = sizeof($ads);
pierre@0: _debug_echo("Selecting $quantity ad(s) from $total total ad(s).");
pierre@0: if (is_array($ads)) {
pierre@0: $ads = adserve_select_reindex($ads, $invalid);
pierre@0: $total = sizeof($ads);
pierre@0: for ($i = 0; $i < $quantity; $i++) {
pierre@0: _debug_echo('Randomly selecting ad: '. ($i + 1) ." of $quantity.");
pierre@0: $id = 0;
pierre@0: // Randomly select a unique banner to display. We subtract 1 as arrays
pierre@0: // start at 0.
pierre@0: $return = adserve_invoke_hook('adserve_select', $ads, $invalid);
pierre@0: if (is_array($return) && !empty($return)) {
pierre@0: foreach ($return as $id) {
pierre@0: // First come first serve.
pierre@0: if ((int)$id) break;
pierre@0: }
pierre@0: }
pierre@0: if ($id >= 0 && sizeof($ads)) {
pierre@0: if ($id == 0) {
pierre@0: _debug_echo("Default ID selection in adserve.inc.");
pierre@0: $id = $total > 1 ? $ads[mt_rand(0, $total - 1)] : $ads[0];
pierre@0: _debug_echo("Randomly selected ID: $id.");
pierre@0: }
pierre@0: if ($id > 0) {
pierre@0: $ids[] = $id;
pierre@0: }
pierre@0: }
pierre@0: else {
pierre@0: // There are no more valid advertisements left to display.
pierre@0: break;
pierre@0: }
pierre@0: $invalid[] = $id;
pierre@0: $ads = adserve_select_reindex($ads, $id);
pierre@0: $total = sizeof($ads);
pierre@0: // We're out of ads to display.
pierre@0: if ($total <= 0) {
pierre@0: break;
pierre@0: }
pierre@0: }
pierre@0: }
pierre@0: return $ids;
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@0: if (adserve_variable('debug')) {
pierre@0: echo "Drupal bootstrap '". $bootstrap ."'.
\n";
pierre@0: }
pierre@0:
pierre@0: drupal_bootstrap($bootstrap);
pierre@0: }
pierre@0:
pierre@0: /**
pierre@0: * Increment ad counters. Increment in cache if enabled.
pierre@0: */
pierre@0: function adserve_increment($ad, $action = 'view') {
pierre@0: $cache = adserve_variable('adcache');
pierre@0: if (adserve_variable('debug')) {
pierre@0: echo "adserve_increment action($action) cache($cache)
\n";
pierre@0: }
pierre@0: if (is_object($ad) && isset($ad->aid)) {
pierre@0: $aid = $ad->aid;
pierre@0: }
pierre@0: else {
pierre@0: $aid = 0;
pierre@0: }
pierre@0: if ($cache != 'none') {
pierre@0: $rc = adserve_invoke_file("ad_cache_{$cache}_increment", $action, $aid);
pierre@0: if ($rc) return;
pierre@0: }
pierre@0: adserve_bootstrap();
pierre@0: // Update impressions statistics.
pierre@0: db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND hostid = '%s'", $aid, $action, date('YmdH'), adserve_variable('group'), adserve_variable('hostid'));
pierre@0: // If column doesn't already exist, we need to add it.
pierre@0: if (!db_affected_rows()) {
pierre@0: db_query("INSERT INTO {ad_statistics} (aid, date, action, adgroup, hostid, count) VALUES(%d, %d, '%s', '%s', '%s', 1)", $aid, date('YmdH'), $action, adserve_variable('hostid'), adserve_variable('hostid'));
pierre@0: // If another process already added this row our INSERT will fail, if
pierre@0: // so we still need to increment it so we don't loose an impression.
pierre@0: if (!db_affected_rows()) {
pierre@0: db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND hostid = '%s'", $aid, $action, date('YmdH'), adserve_variable('group'), adserve_variable('hostid'));
pierre@0: }
pierre@0: }
pierre@0:
pierre@0: if ($action == 'view') {
pierre@0: // See if we need to perform additional queries.
pierre@0: if (isset($ad->maxviews) && $ad->maxviews > 0) {
pierre@0: $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)));
pierre@0: if ($views >= $ad->maxviews) {
pierre@0: db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $aid);
pierre@0: ad_statistics_increment($aid, 'autoexpired');
pierre@0: ad_statistics_increment($aid, 'expired');
pierre@0: }
pierre@0: }
pierre@0: }
pierre@0: // TODO: Do we need to do this here? Can it happen when a new click is
pierre@0: // registered?
pierre@0: if (isset($ad->maxclicks) && $ad->maxclicks > 0) {
pierre@0: $clicks = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $aid, date('YmdH', $ad->activated)));
pierre@0: if ($clicks >= $ad->maxclicks) {
pierre@0: db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $aid);
pierre@0: ad_statistics_increment($aid, 'autoexpired');
pierre@0: ad_statistics_increment($aid, 'expired');
pierre@0: }
pierre@0: }
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@0: $files = array("$ad_dir/serve.php", "$ad_dir/ad.module");
pierre@0: if (adserve_variable('debug') > 2) {
pierre@0: $files = array_merge($files, array("$ad_dir/ad.install"));
pierre@0: }
pierre@0: if (adserve_variable('debug') > 3) {
pierre@0: $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"));
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: