function phpAds_checkForUpdates($already_seen = 0, $send_sw_data = true) { global $phpAds_config, $phpAds_updatesServer; global $xmlrpcerruser; // Create client object $client = new xmlrpc_client($phpAds_updatesServer['script'], $phpAds_updatesServer['host'], $phpAds_updatesServer['port']); $params = array(new xmlrpcval($GLOBALS['phpAds_productname'], "string"), new xmlrpcval($phpAds_config['config_version'], "string"), new xmlrpcval($already_seen, "string"), new xmlrpcval($phpAds_config['updates_dev_builds'] ? 'dev' : '', "string"), new xmlrpcval($phpAds_config['instance_id'], "string")); if ($send_sw_data) { // Prepare software data $params[] = phpAds_xmlrpcEncode(array('os_type' => php_uname('s'), 'os_version' => php_uname('r'), 'webserver_type' => isset($_SERVER['SERVER_SOFTWARE']) ? preg_replace('#^(.*?)/.*$#', '$1', $_SERVER['SERVER_SOFTWARE']) : '', 'webserver_version' => isset($_SERVER['SERVER_SOFTWARE']) ? preg_replace('#^.*?/(.*?)(?: .*)?$#', '$1', $_SERVER['SERVER_SOFTWARE']) : '', 'db_type' => $GLOBALS['phpAds_dbmsname'], 'db_version' => phpAds_dbResult(phpAds_dbQuery("SELECT VERSION()"), 0, 0), 'php_version' => phpversion(), 'php_sapi' => ucfirst(php_sapi_name()), 'php_extensions' => get_loaded_extensions(), 'php_register_globals' => (bool) ini_get('register_globals'), 'php_magic_quotes_gpc' => (bool) ini_get('magic_quotes_gpc'), 'php_safe_mode' => (bool) ini_get('safe_mode'), 'php_open_basedir' => (bool) strlen(ini_get('open_basedir')), 'php_upload_tmp_readable' => (bool) is_readable(ini_get('upload_tmp_dir') . DIRECTORY_SEPARATOR))); } // Create XML-RPC request message $msg = new xmlrpcmsg("Openads.Sync", $params); // Send XML-RPC request message if ($response = $client->send($msg, 10)) { // XML-RPC server found, now checking for errors if (!$response->faultCode()) { $ret = array(0, phpAds_xmlrpcDecode($response->value())); // Prepare cache $cache = $ret[1]; } else { $ret = array($response->faultCode(), $response->faultString()); // Prepare cache $cache = false; } // Save to cache phpAds_dbQuery("\n\t\t\tUPDATE\n\t\t\t\t" . $phpAds_config['tbl_config'] . "\n\t\t\tSET\n\t\t\t\tupdates_cache = '" . addslashes(serialize($cache)) . "',\n\t\t\t\tupdates_timestamp = " . time() . "\n\t\t"); return $ret; } return array(-1, 'No response from the server'); }
function phpAds_maintenanceGetLock() { $lock = array('type' => 'db', 'id' => addslashes('pan.' . $GLOBALS['phpAds_config']['instance_id'])); if (phpAds_dbResult(phpAds_dbQuery("SELECT GET_LOCK('{$lock['id']}', 0)"), 0, 0)) { return $lock; } return false; }
$query = "\n\t\tREPLACE INTO\n\t\t\t" . $phpAds_config['tbl_clients'] . "\n\t\t (clientid,\n\t\t\tclientname,\n\t\t\tparent,\n\t\t\tviews,\n\t\t\tclicks,\n\t\t\texpire,\n\t\t\tactivate,\n\t\t\tactive,\n\t\t\tweight,\n\t\t\ttarget)\n\t\tVALUES\n\t\t\t('{$campaignid}',\n\t\t\t'{$clientname}',\n\t\t\t'{$clientid}',\n\t\t\t'{$views}',\n\t\t\t'{$clicks}',\n\t\t\t'{$expire}',\n\t\t\t'{$activate}',\n\t\t\t'{$active}',\n\t\t\t'{$weight}',\n\t\t\t'{$targetviews}')"; $res = phpAds_dbQuery($query) or phpAds_sqlDie(); // Get ID of campaign if ($campaignid == "null") { $campaignid = phpAds_dbInsertID(); } // Auto-target campaign if adviews purchased and expiration set if ($active == 't' && $expire != '0000-00-00' && $views > 0) { include phpAds_path . '/libraries/lib-autotargeting.inc.php'; // Get served adviews for today if ($phpAds_config['compact_stats']) { $res = phpAds_dbQuery("SELECT SUM(s.views) FROM " . $phpAds_config['tbl_adstats'] . " s INNER JOIN " . $phpAds_config['tbl_banners'] . " b ON (b.bannerid = s.bannerid) WHERE day = '" . date('Y-m-d') . "' AND b.clientid = '" . $campaignid . "'") or phpAds_sqlDie(); $already_served = phpAds_dbResult($res, 0, 0); } else { $res = phpAds_dbQuery("SELECT COUNT(*) FROM " . $phpAds_config['tbl_adviews'] . " s INNER JOIN " . $phpAds_config['tbl_banners'] . " b ON (b.bannerid = s.bannerid) WHERE t_stamp >= " . date('Ymd') . "000000 AND b.clientid = '" . $campaignid . "'") or phpAds_sqlDie(); $already_served = phpAds_dbResult($res, 0, 0); } $targetviews = phpAds_AutoTargetingGetTarget(phpAds_AutoTargetingPrepareProfile(), $views, mktime(0, 0, 0, $expireMonth, $expireDay, $expireYear), isset($phpAds_config['autotarget_factor']) ? $phpAds_config['autotarget_factor'] : -1, $already_served); if (is_array($targetviews)) { list($targetviews, ) = $targetviews; } phpAds_dbQuery("\n\t\t\tUPDATE " . $phpAds_config['tbl_clients'] . "\n\t\t\tSET\n\t\t\t\ttarget = " . $targetviews . "\n\t\t\tWHERE\n\t\t\t\tclientid = " . $campaignid); } if (isset($move) && $move == 't') { // We are moving a client to a campaign // Update banners $res = phpAds_dbQuery("\n\t\t\tUPDATE\n\t\t\t\t" . $phpAds_config['tbl_banners'] . "\n\t\t\tSET\n\t\t\t\tclientid='" . $campaignid . "'\n\t\t\tWHERE\n\t\t\t\tclientid='" . $clientid . "'\n\t\t\t") or phpAds_sqlDie(); // Force priority recalculation $new_campaing = false; } // Update targetstats
function phpAds_TargetStatsSaveViews() { global $phpAds_config; $campaigns = array(); // Get total views $res = phpAds_dbQuery("SELECT SUM(views) AS sum_views" . ",day" . " FROM " . $phpAds_config['tbl_adstats'] . " WHERE day=DATE_SUB(NOW(), INTERVAL 1 DAY)" . " GROUP BY day"); $sum_views = phpAds_dbResult($res, 0, 'sum_views'); $day = phpAds_dbResult($res, 0, 'day'); $totalviews = 0; $res = phpAds_dbQuery("SELECT campaignid" . " FROM " . $phpAds_config['tbl_targetstats'] . " WHERE day='" . $day . "'" . " AND campaignid>0"); while ($row = phpAds_dbFetchArray($res)) { $query = "SELECT SUM(views) AS sum_views" . " FROM " . $phpAds_config['tbl_adstats'] . " AS v" . "," . $phpAds_config['tbl_banners'] . " AS b" . " WHERE v.day='" . $day . "'" . " AND b.bannerid = v.bannerid" . " AND b.campaignid = " . $row['campaignid']; $views = (int) phpAds_dbResult(phpAds_dbQuery($query), 0, 'sum_views'); $totalviews += $views; $campaigns[$row['campaignid']] = $views; } $campaigns[0] = $sum_views - $totalviews; while (list($campaignid, $views) = each($campaigns)) { if ($campaignid) { phpAds_dbQuery("UPDATE " . $phpAds_config['tbl_targetstats'] . " SET views=" . $views . " WHERE campaignid=" . $campaignid . " AND day='" . $day . "'"); } else { phpAds_dbQuery("INSERT INTO " . $phpAds_config['tbl_targetstats'] . " (day, campaignid, target, views)" . " VALUES ('" . $day . "'," . $campaignid . ",0," . $views . ")"); } } }
$source = addslashes($cookie['source']); } if (isset($cookie['dest'])) { $dest = addslashes($cookie['dest']); } } else { $bannerid = 'DEFAULT'; } } // Open a connection to the database if (phpAds_dbConnect()) { if ($bannerid != "DEFAULT") { // Get target URL and ClientID $res = phpAds_dbQuery("\n\t\t\tSELECT\n\t\t\t\turl, clientid\n\t\t\tFROM\n\t\t\t\t" . $phpAds_config['tbl_banners'] . "\n\t\t\tWHERE\n\t\t\t\tbannerid = '{$bannerid}'\n\t\t") or die; $url = phpAds_dbResult($res, 0, 0); $clientid = phpAds_dbResult($res, 0, 1); // If destination is a parameter don't use // url from database if (isset($dest) && $dest != '') { // Prevent HTTP header injection, suggested by Tem if (preg_match('#^https?://#', $dest) && !preg_match('/[\\r\\n]/', $dest)) { $url = stripslashes($dest); } } // If zoneid is not set, log it as a regular banner if (!isset($zoneid)) { $zoneid = 0; } if (!isset($source)) { $source = ''; }
// total number of clients $res_clients = phpAds_dbQuery("SELECT count(*) AS count FROM " . $phpAds_config['tbl_clients'] . " WHERE parent = 0") or phpAds_sqlDie(); $res_campaigns = phpAds_dbQuery("SELECT count(*) AS count FROM " . $phpAds_config['tbl_clients'] . " WHERE parent > 0") or phpAds_sqlDie(); $res_active_campaigns = phpAds_dbQuery("SELECT count(*) AS count FROM " . $phpAds_config['tbl_clients'] . " WHERE parent > 0 AND active='t'"); $res_total_banners = phpAds_dbQuery("SELECT count(*) AS count FROM " . $phpAds_config['tbl_banners']); $res_active_banners = phpAds_dbQuery("SELECT count(*) AS count FROM " . $phpAds_config['tbl_banners'] . " as b, " . $phpAds_config['tbl_clients'] . " as c WHERE b.clientid=c.clientid AND c.active='t' AND b.active='t'"); echo "<br><br><br><br>"; echo "<table width='100%' border='0' align='center' cellspacing='0' cellpadding='0'>"; echo "<tr><td height='25' colspan='3'> <b>" . $strOverall . "</b></td></tr>"; echo "<tr height='1'><td colspan='4' bgcolor='#888888'><img src='images/break.gif' height='1' width='100%'></td></tr>"; echo "<tr><td height='25'> " . $strTotalBanners . ": <b>" . phpAds_dbResult($res_total_banners, 0, "count") . "</b></td>"; echo "<td height='25'>" . $strTotalCampaigns . ": <b>" . phpAds_dbResult($res_campaigns, 0, "count") . "</b></td>"; echo "<td height='25'>" . $strTotalClients . ": <b>" . phpAds_dbResult($res_clients, 0, "count") . "</b></td></tr>"; echo "<tr height='1'><td colspan='4' bgcolor='#888888'><img src='images/break-el.gif' height='1' width='100%'></td></tr>"; echo "<tr><td height='25'> " . $strActiveBanners . ": <b>" . phpAds_dbResult($res_active_banners, 0, "count") . "</b></td>"; echo "<td height='25'>" . $strActiveCampaigns . ": <b>" . phpAds_dbResult($res_active_campaigns, 0, "count") . "</b></td>"; echo "<td height='25'> </td></tr>"; echo "<tr height='1'><td colspan='3' bgcolor='#888888'><img src='images/break.gif' height='1' width='100%'></td></tr>"; echo "</table>"; echo "<br><br>"; /*********************************************************/ /* Store preferences */ /*********************************************************/ $Session['prefs']['client-index.php']['hideinactive'] = $hideinactive; $Session['prefs']['client-index.php']['listorder'] = $listorder; $Session['prefs']['client-index.php']['orderdirection'] = $orderdirection; $Session['prefs']['client-index.php']['nodes'] = implode(",", $node_array); phpAds_SessionDataStore(); /*********************************************************/ /* HTML framework */ /*********************************************************/
function phpAds_sqlDie() { global $phpAds_dbmsname, $phpAds_version_readable, $phpAds_version, $phpAds_productname; global $phpAds_last_query; $error = phpAds_dbError(); $corrupt = false; if ($phpAds_dbmsname == 'MySQL') { $errornumber = phpAds_dbErrorNo(); if ($errornumber == 1027 || $errornumber == 1039) { $corrupt = true; } if ($errornumber == 1016 || $errornumber == 1030) { // Probably corrupted table, do additional check eregi("[0-9]+", $error, $matches); if ($matches[0] == 126 || $matches[0] == 127 || $matches[0] == 132 || $matches[0] == 134 || $matches[0] == 135 || $matches[0] == 136 || $matches[0] == 141 || $matches[0] == 144 || $matches[0] == 145) { $corrupt = true; } } } if ($corrupt) { $title = $GLOBALS['strErrorDBSerious']; $message = $GLOBALS['strErrorDBNoDataSerious']; if (phpAds_isLoggedIn() && phpAds_isUser(phpAds_Admin)) { $message .= " (" . $error . ").<br><br>" . $GLOBALS['strErrorDBCorrupt']; } else { $message .= ".<br>" . $GLOBALS['strErrorDBContact']; } } else { $title = $GLOBALS['strErrorDBPlain']; $message = $GLOBALS['strErrorDBNoDataPlain']; if (phpAds_isLoggedIn() && phpAds_isUser(phpAds_Admin)) { $message .= $GLOBALS['strErrorDBSubmitBug']; $last_query = $phpAds_last_query; $message .= "<br><br><table cellpadding='0' cellspacing='0' border='0'>"; $message .= "<tr><td valign='top' nowrap><b>Version:</b> </td><td>" . $phpAds_productname . " " . $phpAds_version_readable . " (" . $phpAds_version . ")</td></tr>"; $message .= "<tr><td> </td><td>PHP " . phpversion() . " / " . $phpAds_dbmsname . " " . phpAds_dbResult(phpAds_dbQuery('SELECT VERSION()'), 0, 0) . "</td></tr>"; $message .= "<tr><td valign='top' nowrap><b>Page:</b></td><td>" . $_SERVER['PHP_SELF'] . "</td></tr>"; $message .= "<tr><td valign='top' nowrap><b>Error:</b></td><td>" . $error . "</td></tr>"; $message .= "<tr><td valign='top' nowrap><b>Query:</b></td><td>" . $last_query . "</td></tr>"; $message .= "</table>"; } } phpAds_Die($title, $message); }
function phpAds_upgradeTargetStats() { global $phpAds_config; if (!isset($phpAds_config['config_version']) || $phpAds_config['config_version'] < 200.13) { $res = phpAds_dbQuery("\n\t\t\tSELECT\n\t\t\t\ttimestamp,\n\t\t\t\tdetails\n\t\t\tFROM\n\t\t\t\t" . $phpAds_config['tbl_userlog'] . "\n\t\t\tWHERE\n\t\t\t\taction = 11\n\t\t\tORDER BY\n\t\t\t\ttimestamp\n\t\t\t"); while ($row = phpAds_dbFetchArray($res)) { while (ereg('\\[id([0-9]+)\\]: ([0-9]+)', $row['details'], $match)) { $day = date('Y-m-d', $row['timestamp']); if (!isset($start)) { $start = $row['timestamp']; } $autotargets[$day][$match[1]]['target'] = $match[2]; $row['details'] = str_replace($match[0], '', $row['details']); } } if (!isset($start)) { // No autotargeting logs, exit return; } $t_stamp = mktime(0, 0, 0, date('m', $start), date('d', $start), date('Y', $start)); $t_stamp_now = mktime(0, 0, 0, date('m'), date('d'), date('Y')); while ($t_stamp < $t_stamp_now) { $day = date('Ymd', $t_stamp); $begin = $day . '000000'; $end = $day . '235959'; $campaigns = array(); if (isset($autotargets[$day])) { while (list($campaignid, ) = each($autotargets[$day])) { $campaigns[] = $campaignid; // We need to figure out how to upgrade to batch statistics... if ($phpAds_config['compact_stats']) { $res_views = phpAds_dbQuery("\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\tSUM(views) AS sum_views\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_adstats'] . " AS v,\n\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_banners'] . " AS b\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tv.day = " . $day . " AND\n\t\t\t\t\t\t\t\tb.bannerid = v.bannerid AND\n\t\t\t\t\t\t\t\tb.clientid = " . $campaignid . "\n\t\t\t\t\t\t\t"); } else { $res_views = phpAds_dbQuery("\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\tCOUNT(*) AS sum_views\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_adviews'] . " AS v,\n\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_banners'] . " AS b\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tv.t_stamp >= " . $begin . " AND\n\t\t\t\t\t\t\t\tv.t_stamp <= " . $end . " AND\n\t\t\t\t\t\t\t\tb.bannerid = v.bannerid AND\n\t\t\t\t\t\t\t\tb.campaignid = " . $campaignid . "\n\t\t\t\t\t\t\t"); } if ($views = phpAds_dbResult($res_views, 0, 0)) { $autotargets[$day][$campaignid]['views'] = $views; } } } if (count($campaigns)) { // We need to figure out how to upgrade to batch statistics... if ($phpAds_config['compact_stats']) { $res_views = phpAds_dbQuery("\n\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\tSUM(views) AS sum_views\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t" . $phpAds_config['tbl_adstats'] . " AS v,\n\t\t\t\t\t\t\t" . $phpAds_config['tbl_banners'] . " AS b\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tv.day = " . $day . " AND\n\t\t\t\t\t\t\tb.bannerid = v.bannerid AND\n\t\t\t\t\t\t\tb.campaignid NOT IN (" . join(', ', $campaigns) . ")\n\t\t\t\t\t\t"); } else { $res_views = phpAds_dbQuery("\n\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\tCOUNT(*) AS sum_views\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t" . $phpAds_config['tbl_adviews'] . " AS v,\n\t\t\t\t\t\t\t" . $phpAds_config['tbl_banners'] . " AS b\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tv.t_stamp >= " . $begin . " AND\n\t\t\t\t\t\t\tv.t_stamp <= " . $end . " AND\n\t\t\t\t\t\t\tb.bannerid = v.bannerid AND\n\t\t\t\t\t\t\tb.campaignid NOT IN (" . join(', ', $campaigns) . ")\n\t\t\t\t\t\t"); } } else { // We need to figure out how to upgrade to batch statistics if ($phpAds_config['compact_stats']) { $res_views = phpAds_dbQuery("\n\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\tSUM(views) AS sum_views\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t" . $phpAds_config['tbl_adstats'] . " AS v\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tv.day = " . $day . "\n\t\t\t\t\t\t"); } else { $res_views = phpAds_dbQuery("\n\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\tCOUNT(*) AS sum_views\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t" . $phpAds_config['tbl_adviews'] . " AS v\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tv.t_stamp >= " . $begin . " AND\n\t\t\t\t\t\t\tv.t_stamp <= " . $end . "\n\t\t\t\t\t\t"); } } $views = phpAds_dbResult($res_views, 0, 0); $autotargets[$day][0]['views'] = $views ? $views : 0; $t_stamp = phpAds_makeTimestamp($t_stamp, 60 * 60 * 24); } for (reset($autotargets); $day = key($autotargets); next($autotargets)) { reset($autotargets[$day]); while (list($campaignid, $value) = each($autotargets[$day])) { phpAds_dbQuery("\n\t\t\t\t\tINSERT INTO\n\t\t\t\t\t\t" . $phpAds_config['tbl_targetstats'] . " (\n\t\t\t\t\t\t\tday,\n\t\t\t\t\t\t\tcampaignid,\n\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\tviews\n\t\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t\t'" . $day . "',\n\t\t\t\t\t\t\t" . $campaignid . ",\n\t\t\t\t\t\t\t" . (isset($value['target']) ? (int) $value['target'] : 0) . ",\n\t\t\t\t\t\t\t" . (isset($value['views']) ? (int) $value['views'] : 0) . "\n\t\t\t\t\t\t)\n\t\t\t\t\t"); } } } }
/* Main code */ /*********************************************************/ echo "<br>"; // Extra campaign info $res = phpAds_dbQuery("SELECT COUNT(*) AS count, SUM(target) AS sum_target FROM " . $phpAds_config['tbl_clients'] . " WHERE parent > 0 AND target > 0"); $campaigns_count = phpAds_dbResult($res, 0, 'count'); $campaigns_target = phpAds_dbResult($res, 0, 'sum_target'); $res = phpAds_dbQuery("SELECT COUNT(*) AS campaigns FROM " . $phpAds_config['tbl_clients'] . " WHERE parent > 0 AND weight > 0"); $campaigns_weight = phpAds_dbResult($res, 0, 'campaigns'); // Get the number of days running if ($phpAds_config['compact_stats']) { $res = phpAds_dbQuery("SELECT UNIX_TIMESTAMP(MIN(day)) AS days_running FROM " . $phpAds_config['tbl_adstats']); $days_running = phpAds_dbResult($res, 0, 'days_running'); } else { $res = phpAds_dbQuery("SELECT UNIX_TIMESTAMP(MIN(t_stamp)) AS days_running FROM " . $phpAds_config['tbl_adviews']); $days_running = phpAds_dbResult($res, 0, 'days_running'); } if ($days_running > 0) { $days_running = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - $days_running; $days_running = round($days_running / (60 * 60 * 24)) - 1; } else { $days_running = 0; } if ($days_running >= 2) { echo str_replace("{days}", $days_running, $strPriorityDaysRunning); if ($days_running >= 8) { echo $strPriorityBasedLastWeek; } if ($days_running >= 2 && $days_running < 8) { echo $strPriorityBasedLastDays; }
function phpAds_TargetStatsSaveViews() { global $phpAds_config; $campaigns = array(); $day = date('Ymd', phpAds_makeTimestamp(mktime(0, 0, 0, date('m'), date('d'), date('Y')), -(60 * 60 * 24))); $begin = $day . '000000'; $end = $day . '235959'; // Get total views if ($phpAds_config['compact_stats']) { $query = "\n\t\t\tSELECT\n\t\t\t\tSUM(views) AS sum_views\n\t\t\tFROM\n\t\t\t\t" . $phpAds_config['tbl_adstats'] . "\n\t\t\tWHERE\n\t\t\t\tday = " . $day . "\n\t\t"; } else { $query = "\n\t\t\tSELECT\n\t\t\t\tCOUNT(*) AS sum_views\n\t\t\tFROM\n\t\t\t\t" . $phpAds_config['tbl_adviews'] . "\n\t\t\tWHERE\n\t\t\t\tt_stamp >= " . $begin . " AND\n\t\t\t\tt_stamp <= " . $end . "\n\t\t"; } $sum_views = phpAds_dbResult(phpAds_dbQuery($query), 0, 'sum_views'); $totalviews = 0; $res = phpAds_dbQuery("\n\t\tSELECT\n\t\t\tclientid\n\t\tFROM\n\t\t\t" . $phpAds_config['tbl_targetstats'] . "\n\t\tWHERE\n\t\t\tday = " . $day . " AND\n\t\t\tclientid > 0\n\t\t"); while ($row = phpAds_dbFetchArray($res)) { if ($phpAds_config['compact_stats']) { $query = "\n\t\t\t\tSELECT\n\t\t\t\t\tSUM(views) AS sum_views\n\t\t\t\tFROM\n\t\t\t\t\t" . $phpAds_config['tbl_adstats'] . " AS v,\n\t\t\t\t\t" . $phpAds_config['tbl_banners'] . " AS b\n\t\t\t\tWHERE\n\t\t\t\t\tv.day = " . $day . " AND\n\t\t\t\t\tb.bannerid = v.bannerid AND\n\t\t\t\t\tb.clientid = " . $row['clientid'] . "\n\t\t\t"; } else { $query = "\n\t\t\t\tSELECT\n\t\t\t\t\tCOUNT(*) AS sum_views\n\t\t\t\tFROM\n\t\t\t\t\t" . $phpAds_config['tbl_adviews'] . " AS v,\n\t\t\t\t\t" . $phpAds_config['tbl_banners'] . " AS b\n\t\t\t\tWHERE\n\t\t\t\t\tv.t_stamp >= " . $begin . " AND\n\t\t\t\t\tv.t_stamp <= " . $end . " AND\n\t\t\t\t\tb.bannerid = v.bannerid AND\n\t\t\t\t\tb.clientid = " . $row['clientid'] . "\n\t\t\t"; } $views = (int) phpAds_dbResult(phpAds_dbQuery($query), 0, 'sum_views'); $totalviews += $views; $campaigns[$row['clientid']] = $views; } $campaigns[0] = $sum_views - $totalviews; while (list($campaignid, $views) = each($campaigns)) { if ($campaignid) { phpAds_dbQuery("\n\t\t\t\tUPDATE \n\t\t\t\t\t" . $phpAds_config['tbl_targetstats'] . "\n\t\t\t\tSET\n\t\t\t\t\tviews = " . $views . "\n\t\t\t\tWHERE\n\t\t\t\t\tclientid = " . $campaignid . " AND\n\t\t\t\t\tday = " . $day . "\n\t\t\t\t"); } else { phpAds_dbQuery("\n\t\t\t\tINSERT INTO " . $phpAds_config['tbl_targetstats'] . "\n\t\t\t\t\t(day, clientid, target, views)\n\t\t\t\tVALUES\n\t\t\t\t\t(" . $day . ", " . $campaignid . ", 0, " . $views . ")\n\t\t\t\t"); } } }
$source = addslashes($cookie['source']); } if (isset($cookie['dest'])) { $dest = addslashes($cookie['dest']); } } else { $bannerid = 'DEFAULT'; } } // Open a connection to the database if (phpAds_dbConnect()) { if ($bannerid != "DEFAULT") { // Get target URL and CampaignID $res = phpAds_dbQuery("\n\t\t\tSELECT\n\t\t\t\turl, campaignid\n\t\t\tFROM\n\t\t\t\t" . $phpAds_config['tbl_banners'] . "\n\t\t\tWHERE\n\t\t\t\tbannerid = '{$bannerid}'\n\t\t") or die; $url = phpAds_dbResult($res, 0, 0); $campaignid = phpAds_dbResult($res, 0, 1); // If destination is a parameter don't use // url from database if (isset($dest) && $dest != '') { $url = stripslashes($dest); } // If zoneid is not set, log it as a regular banner if (!isset($zoneid) || strlen($zoneid) == 0) { $zoneid = 0; } $source = phpAds_deriveSource($source); // Log clicks if (!phpAds_isClickBlocked($bannerid)) { if ($phpAds_config['log_adclicks']) { phpAds_logClick($userid, $bannerid, $zoneid, $source); }
function phpAds_PriorityPredictProfile($campaigns, $banners) { global $phpAds_config; global $debug, $debuglog; $real_profile = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); $profile_correction_executed = false; // Get the number of days running $res = phpAds_dbQuery("SELECT UNIX_TIMESTAMP(MIN(day)) AS days_running FROM " . $phpAds_config['tbl_adstats'] . " WHERE day > 0 AND hour > 0 ORDER BY day LIMIT 1"); $days_running = phpAds_dbResult($res, 0, 'days_running'); if ($days_running > 0) { $now = phpAds_CurrentDay; $days_running = $now - $days_running + (date('I', $days_running) - date('I', $now)) * 60; $days_running = round($days_running / (60 * 60 * 24)) - 1; } if ($days_running < 0) { $days_running = 0; } // BEGIN REPORTING $debuglog .= "-----------------------------------------------------\n"; $debuglog .= "Number of days running: {$days_running}\n"; // END REPORTING if ($days_running >= 8) { // determine the history if ($days_running > 13) { $use_days = $days_running - 7; } else { $use_days = 6; } // get total impressions last {$use_days} days $impressions_this_week = phpAds_PriorityGetImpressions($use_days, 0); // get total impressions last {$use_days} days last week $impressions_last_week = phpAds_PriorityGetImpressions($use_days, 7); // BEGIN REPORTING $debuglog .= "Using data from data from this week and last week\n"; $debuglog .= "Days fetched: {$use_days}\n"; $debuglog .= "Impressions up to this week: " . $impressions_this_week . "\n"; $debuglog .= "Impressions up to last week: " . $impressions_last_week . "\n"; // END REPORTING if ($impressions_last_week > 0) { // determine trend $trend = $impressions_this_week / $impressions_last_week; if ($days_running > 9) { // get profile using a normal distribution $profile = phpAds_PriorityGetGaussianProfile($days_running); // BEGIN REPORTING $debuglog .= "Using gaussian profile prediction\n"; // END REPORTING } else { // get profile seven days ago $profile = phpAds_PriorityGetHourlyProfile(1, 6); } // apply trend for ($i = 0; $i < count($profile); $i++) { $profile[$i] = (int) round($profile[$i] * $trend); } // BEGIN REPORTING $debuglog .= sprintf("Trend: %.4f\n", $trend); // END REPORTING } else { // no stats for last week, fall back to looking only at yesterday $days_running = 1; // BEGIN REPORTING $debuglog .= "No stats up to last week: days running set to 1\n"; // END REPORTING } // BEGIN REPORTING $debuglog .= "-----------------------------------------------------\n\n\n"; // END REPORTING } if ($days_running >= 2 && $days_running < 8) { // BEGIN REPORTING $debuglog .= "Using data from data from the last couple of days\n"; $debuglog .= "-----------------------------------------------------\n\n\n"; // END REPORTING // get last couple of days $profile = phpAds_PriorityGetHourlyProfile($days_running, 0); // average for ($i = 0; $i < count($profile); $i++) { $profile[$i] = (int) round($profile[$i] / $days_running); } } if ($days_running == 1) { // BEGIN REPORTING $debuglog .= "Using data from data from yesterday\n"; $debuglog .= "-----------------------------------------------------\n\n\n"; // END REPORTING // get yesterday $profile = phpAds_PriorityGetHourlyProfile($days_running, 0); } if (!$days_running) { // BEGIN REPORTING $debuglog .= "There's no data to predict a profile\n"; $debuglog .= "-----------------------------------------------------\n\n\n"; // END REPORTING } // BEGIN REPORTING if (isset($profile)) { $debuglog .= "PREDICTED PROFILE\n"; $debuglog .= "-----------------------------------------------------\n"; $debuglog .= phpAds_PriorityPrintProfile($profile); $debuglog .= "\n\n\n"; } // END REPORTING $begin = date('Ymd', phpAds_CurrentDay); $query = "\n\t\tSELECT hour, SUM(views) AS sum_views\n\t\tFROM " . $phpAds_config['tbl_adstats'] . "\n\t\tWHERE day = " . $begin . "\n\t\tAND hour < " . phpAds_CurrentHour . "\n\t\tGROUP BY hour\n\t"; $res = phpAds_dbQuery($query); $real_up_till_now = 0; while ($row = phpAds_dbFetchArray($res)) { $real_profile[$row['hour']] = $row['sum_views']; $real_up_till_now += $row['sum_views']; } // BEGIN REPORTING $debuglog .= "REAL VALUES UP TILL " . phpAds_CurrentHour . ":00 \n"; $debuglog .= "-----------------------------------------------------\n"; $debuglog .= phpAds_PriorityPrintProfile($real_profile); $debuglog .= "\n\n\n"; // END REPORTING // Calculate total predicted profile $total_profile = 0; for ($i = 0; $i < 24; $i++) { $total_profile += isset($profile[$i]) ? $profile[$i] : 0; } // Adjust profile with real data if ($total_profile > 0) { if (phpAds_CurrentHour > 0) { $predicted_today = 0; for ($i = 0; $i < 24; $i++) { $predicted_today += isset($profile[$i]) ? $profile[$i] : 0; } $predicted_up_till_now = 0; for ($i = 0; $i < phpAds_CurrentHour; $i++) { $predicted_up_till_now += isset($profile[$i]) ? $profile[$i] : 0; } $predicted_left_today = $predicted_today - $predicted_up_till_now; // BEGIN REPORTING $debuglog .= "Predicted impressions today: {$predicted_today} \n"; $debuglog .= "Predicted impression up till now: {$predicted_up_till_now} \n"; $debuglog .= "Predicted impressions left today: {$predicted_left_today} \n"; $debuglog .= "-----------------------------------------------------\n"; // END REPORTING // Adjust prediction for today if ($predicted_up_till_now > 0) { $importance = (sin(M_PI * (sin(M_PI * pow(phpAds_CurrentHour / 24, 0.9) - M_PI / 2) + 1) / 2 - M_PI / 2) + 1) / 2; $deviance_old = ($real_up_till_now / $predicted_up_till_now - 1) * $importance + 1; $profile_correction_done = false; while (!$profile_correction_done) { for ($i = phpAds_CurrentHour; $i > 0; $i--) { $deviance = phpAds_PriorityGetDeviance($i, $profile, $real_profile); if ($deviance > 2.25) { // BEGIN REPORTING $debuglog .= sprintf("Got deviance %.4f at %02d:00\n", $deviance, $i); // END REPORTING $k = $i > 1 ? $i - 1 : $i; while ($k && phpAds_PriorityGetDeviance($k, $profile, $real_profile) > $deviance) { // BEGIN REPORTING $debuglog .= sprintf("Got greater deviance (%.4f) at %02d:00\n", $deviance, $k); // END REPORTING $k--; } $deviance = (phpAds_PriorityGetDeviance($k, $profile, $real_profile) + phpAds_PriorityGetDeviance($k == phpAds_CurrentHour ? $k : $k + 1, $profile, $real_profile)) / 2; for ($j = 0; $j < $k; $j++) { $profile[$j] = ($profile[$j] ? $profile[$j] : 1) * $deviance; $profile_correction_executed = true; } break; } if ($i == 1) { $profile_correction_done = true; } } } if ($profile_correction_executed) { for ($i = 0; $i < 24; $i++) { $profile[$i] = round($profile[$i]); } $predicted_today = 0; for ($i = 0; $i < 24; $i++) { $predicted_today += $profile[$i]; } $predicted_up_till_now = 0; for ($i = 0; $i < phpAds_CurrentHour; $i++) { $predicted_up_till_now += $profile[$i]; } $predicted_left_today = $predicted_today - $predicted_up_till_now; } $deviance = phpAds_PriorityGetDeviance(phpAds_CurrentHour, $profile, $real_profile); // BEGIN REPORTING $debuglog .= "Importance factor: " . sprintf('%.4f', phpAds_PriorityGetImportance(phpAds_CurrentHour)) . " \n"; $debuglog .= "Deviance: " . sprintf('%.4f (%.4f before correction)', $deviance, $deviance_old) . " \n"; $debuglog .= "-----------------------------------------------------\n"; if ($profile_correction_executed) { $debuglog .= "Predicted impressions today after correction: {$predicted_today} \n"; $debuglog .= "Predicted impression up till now after correction: {$predicted_up_till_now} \n"; $debuglog .= "Predicted impressions left today after correction: {$predicted_left_today} \n"; $debuglog .= "\n\nNEW PREDICTED PROFILE\n"; $debuglog .= "-----------------------------------------------------\n"; $debuglog .= phpAds_PriorityPrintProfile($profile); $debuglog .= "\n\n\n"; } // END REPORTING $real_left_today = round($predicted_left_today * $deviance); } else { $real_left_today = $predicted_today; } // Create new profile based on new prediction // and real data $real_today = $real_left_today + $real_up_till_now; if ($predicted_left_today > 0) { $adjustment = $real_left_today / $predicted_left_today; } else { $adjustment = 1; } // BEGIN REPORTING $debuglog .= "Real impressions up till now: {$real_up_till_now} \n"; // END REPORTING if ($predicted_up_till_now) { // BEGIN REPORTING $debuglog .= sprintf("Adjustment: %.4f\n", $adjustment); $debuglog .= "Adjusted predicted impressions today: {$real_today}\n"; $debuglog .= "Adjusted predicted impressions left today: {$real_left_today}\n"; $debuglog .= "-----------------------------------------------------\n"; // END REPORTING } if ($adjustment > 0) { for ($i = 0; $i < 24; $i++) { if ($i < phpAds_CurrentHour) { if (!$profile_correction_executed) { $profile[$i] = (int) $real_profile[$i]; } } else { $profile[$i] = (int) round($profile[$i] * $adjustment); } } } elseif (!$profile_correction_executed) { for ($i = 0; $i < phpAds_CurrentHour; $i++) { $profile[$i] = (int) $real_profile[$i]; } } } } else { if ($real_up_till_now > 0 && phpAds_CurrentHour > 0) { $predicted_today = $real_up_till_now / phpAds_CurrentHour * 24; $predicted_left_today = $predicted_today - $real_up_till_now; $hours_left_today = 24 - phpAds_CurrentHour; for ($i = 0; $i < 24; $i++) { if ($i < phpAds_CurrentHour) { $profile[$i] = isset($real_profile[$i]) ? $real_profile[$i] : 0; } else { $profile[$i] = (int) round($predicted_left_today / $hours_left_today); } } } else { // No data available // Now it is time to make something up :) for ($i = 0; $i < 24; $i++) { if ($i < phpAds_CurrentHour) { $profile[$i] = isset($real_profile[$i]) ? $real_profile[$i] : 0; } else { $profile[$i] = isset($total_target) ? (int) round($total_target / 24) : 0; } } } } // Calculate total predicted profile $total_profile = 0; for ($i = 0; $i < 24; $i++) { $total_profile += $profile[$i]; } // Calculate total impressions target $total_target = 0; for (reset($campaigns); $c = key($campaigns); next($campaigns)) { $total_target += $campaigns[$c]['target']; } if ($total_profile == 0) { // No data available, profile is completely zero // create a profile to match campaign weights only $total_weight = phpAds_PriorityTotalWeight($campaigns, $banners); for ($i = 0; $i < 24; $i++) { $profile[$i] = (int) $total_weight; } $profile_correction_executed = false; } // BEGIN REPORTING $debuglog .= "\n\n\nADJUSTED PROFILE\n"; $debuglog .= "-----------------------------------------------------\n"; $debuglog .= phpAds_PriorityPrintProfile($profile); $debuglog .= "\n\n\n"; // END REPORTING return array($profile, $profile_correction_executed); }
echo "</td>\n"; echo "<td></td>\n"; echo "\t\t\t\t</tr>\n"; echo "\t\t\t\t</table>\n"; // total number of agencies $res_agencies = phpAds_dbQuery("SELECT count(*) AS count FROM " . $phpAds_config['tbl_agency']) or phpAds_sqlDie(); echo "\t\t\t\t<br><br><br><br>\n"; echo "\t\t\t\t<table width='100%' border='0' align='center' cellspacing='0' cellpadding='0'>\n"; echo "\t\t\t\t<tr>\n"; echo "\t\t\t\t\t<td height='25' colspan='3'> <b>" . $strOverall . "</b></td>\n"; echo "\t\t\t\t</tr>\n"; echo "\t\t\t\t<tr height='1'>\n"; echo "\t\t\t\t\t<td colspan='4' bgcolor='#888888'><img src='images/break.gif' height='1' width='100%'></td>\n"; echo "\t\t\t\t</tr>\n"; echo "\t\t\t\t<tr>\n"; echo "\t\t\t\t\t<td height='25'> " . $strTotalAgencies . ": <b>" . phpAds_dbResult($res_agencies, 0, "count") . "</b></td>\n"; echo "\t\t\t\t\t<td height='25' colspan='2'></td>\n"; echo "\t\t\t\t</tr>\n"; echo "\t\t\t\t<tr height='1'>\n"; echo "\t\t\t\t\t<td colspan='3' bgcolor='#888888'><img src='images/break.gif' height='1' width='100%'></td>\n"; echo "\t\t\t\t</tr>\n"; echo "\t\t\t\t</table>\n"; echo "\t\t\t\t<br><br>\n"; /*********************************************************/ /* Store preferences */ /*********************************************************/ $Session['prefs']['agency-index.php']['hideinactive'] = $hideinactive; $Session['prefs']['agency-index.php']['listorder'] = $listorder; $Session['prefs']['agency-index.php']['orderdirection'] = $orderdirection; $Session['prefs']['agency-index.php']['nodes'] = implode(",", $node_array); phpAds_SessionDataStore();