View source
<?php
function adserve_ad($options = array()) {
static $displayed_count = 0;
if (!empty($options)) {
adserve_variable('variable_load', $options);
}
else {
adserve_variable('variable_load');
}
adserve_bootstrap(0);
adserve_debug();
adserve_variable('error', FALSE);
$ids = adserve_cache('get_ad_ids');
adserve_cache('display', $ids);
}
function adserve_variable($variable, $value = NULL) {
global $conf;
static $variables = NULL, $overridden = NULL, $cache_loaded = array();
if (isset($value)) {
$variables->{$variable} = $value;
}
$loaded = FALSE;
if (!isset($variables->loaded) || $variable == 'variable_load') {
if ($variable == 'variable_load' && isset($value)) {
$values['debug'] = $value['debug'];
$values['c'] = $value['adcache'];
$values['n'] = $value['nids'];
$values['t'] = $value['tids'];
$values['k'] = $value['hostid'];
$values['q'] = $value['quantity'];
$values['m'] = $value['ad_display'];
unset($value);
}
else {
$values = $_GET;
}
$variables->ad_dir = dirname($_SERVER['SCRIPT_FILENAME']);
$variables->debug = isset($values['debug']) ? (int) $values['debug'] : 0;
$variables->adcache = isset($values['c']) ? preg_replace('/[^a-zA-Z]/', '', $values['c']) : 'none';
$variables->nids = isset($values['n']) ? preg_replace('/[^0-9,]/', '', $values['n']) : '';
$variables->tids = isset($values['t']) ? preg_replace('/[^0-9,]/', '', $values['t']) : '';
$variables->hostid = isset($values['k']) ? preg_replace('/[^0-9a-f]/', '', $values['k']) : '';
$variables->url = isset($values['u']) ? $values['u'] : '';
if (!$variables->url) {
$variables->url = $_SERVER['HTTP_REFERER'];
}
$variables->quantity = isset($values['q']) ? (int) $values['q'] : 0;
$variables->aid = isset($values['a']) ? (int) $values['a'] : 0;
$variables->ad_display = isset($values['m']) ? preg_replace('/[^a-zA-Z]/', '', $values['m']) : 'javascript';
$variables->quantity = $variables->quantity ? $variables->quantity : 1;
if ($variables->debug) {
foreach ($variables as $variable => $val) {
echo "{$variable}: '{$val}'<br />\n";
}
if ($variables->debug == 1) {
exit;
}
}
$variables->loaded = TRUE;
if (isset($value)) {
$variables->{$variable} = $value;
}
$loaded = TRUE;
}
if (!$overridden) {
if (isset($conf)) {
foreach ($conf as $var => $val) {
$variables->{$var} = $val;
if ($variables->debug) {
echo "Override {$var}: '{$val}'<br />\n";
}
}
$overridden = TRUE;
}
}
if (!isset($cache_loaded[$variables->adcache])) {
if ($variables->adcache != 'none') {
$include = $variables->ad_dir . "/cache/{$variables->adcache}/ad_cache_{$variables->adcache}.inc";
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}({$val})<br />\n";
}
echo '</pre>';
}
if (isset($variables->{$variable})) {
return $variables->{$variable};
}
else {
return NULL;
}
}
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.<br />\n";
}
}
return $output;
}
function _debug_error_handler($errno, $errstr, $errfile = NULL, $errline = 0, $errcontext = NULL) {
if (!preg_match('/Cannot modify header information/', $errstr) && !preg_match('/Cannot send session cache limiter/', $errstr)) {
echo "PHP: errno({$errno}): {$errstr} ";
if ($errfile && $errline) {
echo "; Line {$errline} in [{$errfile}]";
}
echo "<br />\n";
if (!empty($errcontext) && adserve_variable('debug') >= 5) {
echo 'Error context:<pre>';
print_r($errcontext);
echo '</pre>';
}
}
}
function _debug_echo($text) {
static $error_handler = FALSE;
if (adserve_variable('debug')) {
if (!$error_handler) {
set_error_handler('_debug_error_handler');
$error_handler = TRUE;
}
echo "{$text}<br />\n";
}
}
function adserve_include_drupal() {
if (defined('DRUPAL_ROOT')) {
if (is_dir(DRUPAL_ROOT) && file_exists(DRUPAL_ROOT . '/includes/bootstrap.inc')) {
chdir(DRUPAL_ROOT);
}
else {
echo 'Invalid DRUPAL_ROOT (' . DRUPAL_ROOT . ') defined in adserve.inc';
}
}
else {
$path = explode('/', adserve_variable('ad_dir'));
while (!empty($path)) {
chdir(implode('/', $path));
if (file_exists('./includes/bootstrap.inc')) {
break;
}
array_pop($path);
}
}
adserve_variable('root_dir', getcwd());
require_once adserve_variable('root_dir') . '/includes/bootstrap.inc';
}
function adserve_bootstrap($bootstrap = NULL) {
adserve_include_drupal();
if (!isset($bootstrap)) {
$bootstrap = DRUPAL_BOOTSTRAP_FULL;
}
_debug_echo("Drupal bootstrap '" . $bootstrap . "'.");
drupal_bootstrap($bootstrap);
}
function adserve_debug() {
if (adserve_variable('debug')) {
echo "Root drupal directory detected as '" . adserve_variable('root_dir') . "'.<br />\n<br />\n";
$ad_dir = adserve_variable('ad_dir');
$files = array(
"{$ad_dir}/serve.php",
"{$ad_dir}/adserve.inc",
"{$ad_dir}/adcache.inc",
"{$ad_dir}/ad.module",
);
if (adserve_variable('debug') >= 3) {
$files = array_merge($files, array(
"{$ad_dir}/ad.install",
));
}
if (adserve_variable('debug') >= 4) {
$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",
));
}
foreach ($files as $file) {
if (!file_exists($file)) {
echo "Error: '{$file}' does not exist!<br />\n";
}
else {
if (!is_readable($file)) {
echo "Error: '{$file}' is not readable!<br />\n";
}
else {
$fd = fopen($file, 'r');
while (!feof($fd)) {
$line = fgets($fd);
if (substr($line, 0, 5) == "<?php") {
continue;
}
else {
echo "{$file}: {$line}<br />";
break;
}
}
}
}
}
echo "<br />\n";
}
}