You are here

function adserve_variable in Advertisement 6.2

Same name and namespace in other branches
  1. 5.2 adserve.inc \adserve_variable()
  2. 5 adserve.inc \adserve_variable()
  3. 6.3 adserve.inc \adserve_variable()
  4. 6 adserve.inc \adserve_variable()
  5. 7 adserve.inc \adserve_variable()

Retrieve variables from $_GET array or from passed in $value array.

22 calls to adserve_variable()
adserve_ad in ./adserve.inc
The main adserve logic.
adserve_cache in ./adcache.inc
Wrapper for calling adserve_cache functions.
adserve_cache_display in ./adcache.inc
Default function for displaying advertisements. This is not generally replaced by ad cache modules.
adserve_cache_get_ad_ids in ./adcache.inc
Default wrapper function for displaying advertisements. This generally is not replaced by ad caches modules.
adserve_cache_increment in ./adcache.inc
Increment action directly in the database.

... See full list

File

./adserve.inc, line 61
Configuration.

Code

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['debug_key'] = isset($value['debug_key']) ? $value['debug_key'] : '';
      $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;
    }
    else {
      $values = $_GET;
    }

    // Don't trust the GET parameter for 'debug' unless the debugging key
    // matches.
    if (!(defined('AD_DEBUG_KEY') && $values['debug_key'] == AD_DEBUG_KEY && isset($values['debug']))) {
      $values['debug'] = '';
    }
    unset($value);

    // 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']) : '';

    // Display url
    $variables->url = isset($values['u']) ? $values['u'] : '';
    if (!$variables->url) {
      $variables->url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
    }

    // Display alias
    $variables->alias = isset($values['l']) ? $values['l'] : '';

    // 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}: '" . htmlspecialchars($val) . "'<br />\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 "Variable {$var} was overridden by value in settings.php.<br />\n";
        }
      }
      $overridden = TRUE;
    }
  }
  if (!isset($cache_loaded[$variables->adcache])) {

    // Retrieve variables defined by cache plugin, if enabled.
    if (!empty($variables->adcache) && $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}'.<br />\n";
          }
          require_once $include;
        }
        else {
          if ($variables->debug) {
            echo "Failed to find cache include file '{$include}'.<br />\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:<br />\n";
    echo '<pre>';
    foreach ($variables as $var => $val) {
      echo "  {$var}(" . htmlspecialchars($val) . ")<br />\n";
    }
    echo '</pre>';
  }
  if (isset($variables->{$variable})) {
    return $variables->{$variable};
  }
  else {
    return NULL;
  }
}