function phpAds_logConversion($userid, $trackerid)
{
    global $HTTP_SERVER_VARS, $phpAds_config, $phpAds_geo;
    // Check if host is on list of hosts to ignore
    if ($host = phpads_logCheckHost()) {
        $log_country = $phpAds_config['geotracking_stats'] && $phpAds_geo && $phpAds_geo['country'] ? $phpAds_geo['country'] : '';
        $log_host = $phpAds_config['log_hostname'] ? $HTTP_SERVER_VARS['REMOTE_HOST'] : '';
        $log_host = $phpAds_config['log_iponly'] ? $HTTP_SERVER_VARS['REMOTE_ADDR'] : $log_host;
        phpAds_dbQuery("INSERT " . ($phpAds_config['insert_delayed'] ? 'DELAYED' : '') . " INTO " . $phpAds_config['tbl_adconversions'] . "(userid" . ",trackerid" . ",host" . ",country)" . " VALUES " . "('" . $userid . "'" . "," . $trackerid . ",'" . $log_host . "'" . ",'" . $log_country . "')");
        return phpAds_dbInsertID();
    }
}
         $activate = "0000-00-00";
     }
 } else {
     // No date was set, activate immediately
     $activate = "0000-00-00";
 }
 // Set campaign inactive if weight and target are both null and autotargeting is disabled
 if ($active == 't' && !($targetviews > 0 || $weight > 0 || $expire != '0000-00-00' && $views > 0)) {
     $active = 'f';
 }
 $new_campaign = $campaignid == 'null';
 $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;
                $basename = $row['zonename'];
            }
            $names = array();
            $res = phpAds_dbQuery("\n\t\t\t\tSELECT\n\t\t\t   \t\t*\n\t\t\t\tFROM\n\t\t\t\t\t" . $phpAds_config['tbl_zones'] . "\n\t\t\t") or phpAds_sqlDie();
            while ($name = phpAds_dbFetchArray($res)) {
                $names[] = $name['zonename'];
            }
            // Get unique name
            $i = 2;
            while (in_array($basename . ' (' . $i . ')', $names)) {
                $i++;
            }
            $row['zonename'] = $basename . ' (' . $i . ')';
            // Remove bannerid
            unset($row['zoneid']);
            $values = array();
            while (list($name, $value) = each($row)) {
                $values[] = $name . " = '" . addslashes($value) . "'";
            }
            $res = phpAds_dbQuery("\n\t\t   \t\tINSERT INTO\n\t\t   \t\t\t" . $phpAds_config['tbl_zones'] . "\n\t\t\t\tSET\n\t\t\t\t\t" . implode(", ", $values) . "\n\t   \t\t") or phpAds_sqlDie();
            $new_zoneid = phpAds_dbInsertID();
            Header("Location: " . $returnurl . "?affiliateid=" . $affiliateid . "&zoneid=" . $new_zoneid);
            exit;
        }
    }
}
// Prevent HTTP response splitting
if (!preg_match('/[\\r\\n]/', $returnurl)) {
    $url = stripslashes($returnurl);
    header("Location: " . $returnurl . "?affiliateid=" . $affiliateid . "&zoneid=" . $zoneid);
}
 } else {
     // INSERT
     $final['compiledlimitation'] = "true";
     $values_fields = "";
     $values = "";
     while (list($name, $value) = each($final)) {
         $values_fields .= "{$name}, ";
         $values .= "'{$value}', ";
     }
     // Cut trailing commas
     $values_fields = ereg_replace(", \$", "", $values_fields);
     $values = ereg_replace(", \$", "", $values);
     // Execute query
     $sql_query = "\n\t\t\tINSERT INTO\n\t\t\t\t" . $phpAds_config['tbl_banners'] . "\n\t\t\t\t({$values_fields})\n\t\t\tVALUES\n\t\t\t({$values})";
     $res = phpAds_dbQuery($sql_query) or phpAds_sqlDie();
     $bannerid = phpAds_dbInsertID();
 }
 // Recalculate priorities
 if (!isset($current) || $current['weight'] != $weight) {
     require "../libraries/lib-priority.inc.php";
     phpAds_PriorityCalculate();
 }
 // Rebuild cache
 if (!defined('LIBVIEWCACHE_INCLUDED')) {
     include phpAds_path . '/libraries/deliverycache/cache-' . $phpAds_config['delivery_caching'] . '.inc.php';
 }
 phpAds_cacheDelete();
 if ($edit_swf) {
     Header('Location: banner-swf.php?clientid=' . $clientid . '&campaignid=' . $campaignid . '&bannerid=' . $bannerid);
 } else {
     if (phpAds_isUser(phpAds_Client)) {
 }
 if (!isset($affiliateid) || $affiliateid == '') {
     $keys = array();
     $values = array();
     while (list($key, $value) = each($affiliate)) {
         $keys[] = $key;
         $values[] = $value;
     }
     $query = "INSERT INTO " . $phpAds_config['tbl_affiliates'] . " (";
     $query .= implode(", ", $keys);
     $query .= ") VALUES ('";
     $query .= implode("', '", $values);
     $query .= "')";
     // Insert
     phpAds_dbQuery($query) or phpAds_sqlDie();
     $affiliateid = phpAds_dbInsertID();
     // Go to next page
     if (isset($move) && $move == 't') {
         // Move loose zones to this affiliate
         $res = phpAds_dbQuery("\n\t\t\t\t\tUPDATE\n\t\t\t\t\t\t" . $phpAds_config['tbl_zones'] . "\n\t\t\t\t\tSET\n\t\t\t\t\t\taffiliateid = '" . $affiliateid . "'\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tISNULL(affiliateid) OR\n\t\t\t\t\t\taffiliateid = 0\n\t\t\t\t");
         header("Location: affiliate-zones.php?affiliateid=" . $affiliateid);
     } else {
         header("Location: zone-edit.php?affiliateid=" . $affiliateid);
     }
 } else {
     $pairs = array();
     while (list($key, $value) = each($affiliate)) {
         $pairs[] = " " . $key . "='" . $value . "'";
     }
     $query = "UPDATE " . $phpAds_config['tbl_affiliates'] . " SET ";
     $query .= trim(implode(",", $pairs)) . " ";
            }
            $names = array();
            $res = phpAds_dbQuery("SELECT *" . " FROM " . $phpAds_config['tbl_trackers']) or phpAds_sqlDie();
            while ($name = phpAds_dbFetchArray($res)) {
                $names[] = $name['trackername'];
            }
            // Get unique name
            $i = 2;
            while (in_array($basename . ' (' . $i . ')', $names)) {
                $i++;
            }
            $row['trackername'] = $basename . ' (' . $i . ')';
            // Remove tracker
            unset($row['trackerid']);
            $values = array();
            while (list($name, $value) = each($row)) {
                $values[] = $name . " = '" . addslashes($value) . "'";
            }
            $res = phpAds_dbQuery("\n\t\t   \t\tINSERT INTO\n\t\t   \t\t\t" . $phpAds_config['tbl_trackers'] . "\n\t\t\t\tSET\n\t\t\t\t\t" . implode(", ", $values) . "\n\t   \t\t") or phpAds_sqlDie();
            $new_trackerid = phpAds_dbInsertID();
            // Copy any linked campaigns
            $res = phpAds_dbQuery("SELECT" . " campaignid" . ",trackerid" . ",logstats" . ",clickwindow" . ",viewwindow" . " FROM " . $phpAds_config['tbl_campaigns_trackers'] . " WHERE trackerid=" . $trackerid) or phpAds_sqlDie();
            while ($row = phpAds_dbFetchArray($res)) {
                $res2 = phpAds_dbQuery(" INSERT INTO " . $phpAds_config['tbl_campaigns_trackers'] . " SET campaignid=" . $row['campaignid'] . ",trackerid=" . $new_trackerid . ",logstats='" . $row['logstats'] . "'" . ",clickwindow=" . $row['clickwindow'] . ",viewwindow=" . $row['viewwindow']) or phpAds_sqlDie();
            }
            Header("Location: " . $returnurl . "?clientid=" . $clientid . "&trackerid=" . $new_trackerid);
            exit;
        }
    }
}
Header("Location: " . $returnurl . "?clientid=" . $clientid . "&trackerid=" . $trackerid);
 if (count($errormessage) == 0) {
     if (!isset($agencyid) || $agencyid == '') {
         $keys = array();
         $values = array();
         while (list($key, $value) = each($agency)) {
             $keys[] = $key;
             $values[] = $value;
         }
         $query = "INSERT INTO " . $phpAds_config['tbl_agency'] . " (";
         $query .= implode(", ", $keys);
         $query .= ") VALUES ('";
         $query .= implode("', '", $values);
         $query .= "')";
         // Insert
         phpAds_dbQuery($query) or phpAds_sqlDie();
         $agencyid = phpAds_dbInsertID();
         // When adding an agency, copy the values in the config table to this new agency
         $query = "SELECT * FROM " . $phpAds_config['tbl_config'] . " WHERE agencyid=0";
         $res = phpAds_dbQuery($query) or phpAds_sqlDie();
         if ($row = phpAds_dbFetchArray($res)) {
             $row['agencyid'] = $agencyid;
             $keys = array_keys($row);
             $values = array_values($row);
             $query = "INSERT INTO " . $phpAds_config['tbl_config'] . "(" . implode(',', $keys) . ") VALUES ('" . implode("','", $values) . "')";
             phpAds_dbQuery($query) or phpAds_sqlDie();
         }
     } else {
         $pairs = array();
         while (list($key, $value) = each($agency)) {
             $pairs[] = " " . $key . "='" . $value . "'";
         }
 if (count($errormessage) == 0) {
     if (!isset($clientid) || $clientid == '') {
         $keys = array();
         $values = array();
         while (list($key, $value) = each($client)) {
             $keys[] = $key;
             $values[] = $value;
         }
         $query = "INSERT INTO " . $phpAds_config['tbl_clients'] . " (";
         $query .= implode(", ", $keys);
         $query .= ") VALUES ('";
         $query .= implode("', '", $values);
         $query .= "')";
         // Insert
         phpAds_dbQuery($query) or phpAds_sqlDie();
         $clientid = phpAds_dbInsertID();
         // Go to next page
         header("Location: campaign-edit.php?clientid=" . $clientid);
     } else {
         $pairs = array();
         while (list($key, $value) = each($client)) {
             $pairs[] = " " . $key . "='" . $value . "'";
         }
         $query = "UPDATE " . $phpAds_config['tbl_clients'] . " SET ";
         $query .= trim(implode(",", $pairs)) . " ";
         $query .= "WHERE clientid = " . $clientid;
         // Update
         phpAds_dbQuery($query) or phpAds_sqlDie();
         // Go to next page
         if (phpAds_isUser(phpAds_Client)) {
             // Set current session to new language
function phpAds_countConversions($begin_timestamp, $end_timestamp, $day, $hour)
{
    global $phpAds_config, $report;
    //Process conversions...
    $num_conversions = 0;
    $time = time();
    $report .= "\tCounting the verbose conversions between " . $begin_timestamp . " and " . $end_timestamp . "...\n";
    // Get all of the conversions for this hour...
    $conversion_query = "SELECT userid" . ",t_stamp" . ",trackerid" . ",host" . ",country" . " FROM " . $phpAds_config['tbl_adconversions'] . " WHERE userid!=''" . " AND t_stamp>=" . $begin_timestamp . " AND t_stamp<" . $end_timestamp . " ORDER BY trackerid";
    $res = phpAds_dbQuery($conversion_query) or $report .= "Could not perform SQL: " . $conversion_query . "\n";
    while ($row = phpAds_dbFetchArray($res)) {
        $userid = $row['userid'];
        $t_stamp = $row['t_stamp'];
        $trackerid = $row['trackerid'];
        $host = $row['host'];
        $country = $row['country'];
        $campaign_query = "SELECT *" . " FROM " . $phpAds_config['tbl_campaigns_trackers'] . " WHERE trackerid=" . $trackerid;
        $campaign_res = phpAds_dbQuery($campaign_query) or $report .= "Could not perform SQL: " . $campaign_query . "\n";
        while ($campaign_row = phpAds_dbFetchArray($campaign_res)) {
            $found = false;
            $action_query = "SELECT c.t_stamp AS t_stamp" . ",c.bannerid AS bannerid" . ",c.zoneid AS zoneid" . ",c.host AS host" . ",c.source AS source" . ",c.country AS country" . " FROM " . $phpAds_config['tbl_adclicks'] . " AS c" . "," . $phpAds_config['tbl_banners'] . " AS b" . " WHERE c.userid='" . $userid . "'" . " AND c.bannerid=b.bannerid" . " AND b.campaignid=" . $campaign_row['campaignid'] . " AND c.t_stamp>= DATE_SUB(" . $t_stamp . ", INTERVAL " . $campaign_row['clickwindow'] . " SECOND)" . " AND c.t_stamp<" . $t_stamp . " ORDER BY t_stamp DESC" . " LIMIT 1";
            /*
            $action_query =
            	"SELECT t_stamp".
            	",bannerid".
            	",zoneid".
            	",host".
            	",source".
            	",country".
            	" FROM ".$phpAds_config['tbl_adclicks'].
            	" WHERE userid='".$userid."'".
            	" AND t_stamp>= DATE_SUB(".$t_stamp.", INTERVAL ".$campaign_row['clickwindow']." SECOND)".
            	" AND t_stamp<".$t_stamp.
            	" ORDER BY t_stamp DESC".
            	" LIMIT 1"
            ;
            */
            $action_res = phpAds_dbQuery($action_query) or $report .= "Could not perform SQL: " . $action_query . "\n";
            if ($action_row = phpAds_dbFetchArray($action_res)) {
                $found = true;
                $action = 'click';
            } else {
                $action_query = "SELECT v.t_stamp AS t_stamp" . ",v.bannerid AS bannerid" . ",v.zoneid AS zoneid" . ",v.host AS host" . ",v.source AS source" . ",v.country AS country" . " FROM " . $phpAds_config['tbl_adviews'] . " AS v" . "," . $phpAds_config['tbl_banners'] . " AS b" . " WHERE v.userid='" . $userid . "'" . " AND v.bannerid=b.bannerid" . " AND b.campaignid=" . $campaign_row['campaignid'] . " AND v.t_stamp>= DATE_SUB(" . $t_stamp . ", INTERVAL " . $campaign_row['viewwindow'] . " SECOND)" . " AND v.t_stamp<" . $t_stamp . " ORDER BY t_stamp DESC" . " LIMIT 1";
                /*
                $action_query =
                	"SELECT t_stamp".
                	",bannerid".
                	",zoneid".
                	",host".
                	",source".
                	",country".
                	" FROM ".$phpAds_config['tbl_adviews'].
                	" WHERE userid='".$userid."'".
                	" AND t_stamp>= DATE_SUB(".$t_stamp.", INTERVAL ".$campaign_row['viewwindow']." SECOND)".
                	" AND t_stamp<".$t_stamp.
                	" ORDER BY t_stamp DESC".
                	" LIMIT 1"
                ;
                */
                $action_res = phpAds_dbQuery($action_query) or $report .= "Could not perform SQL: " . $action_query . "\n";
                if ($action_row = phpAds_dbFetchArray($action_res)) {
                    $found = true;
                    $action = 'view';
                }
            }
            if ($found) {
                $campaignid = $campaign_row['campaignid'];
                $action_t_stamp = $action_row['t_stamp'];
                $action_bannerid = $action_row['bannerid'];
                $action_zoneid = $action_row['zoneid'];
                $action_host = $action_row['host'];
                $action_source = $action_row['source'];
                $action_country = $action_row['country'];
                $cnv_logstats = $campaign_row['logstats'];
                $cnv_clickwindow = $campaign_row['clickwindow'];
                $cnv_viewwindow = $campaign_row['viewwindow'];
                // Found an item which passed the rules.
                // Now, log this item
                $log_query = "INSERT INTO " . $phpAds_config['tbl_conversionlog'] . " (campaignid" . ",trackerid" . ",userid" . ",t_stamp" . ",host" . ",country" . ",cnv_logstats" . ",cnv_clickwindow" . ",cnv_viewwindow" . ",action" . ",action_bannerid" . ",action_zoneid" . ",action_t_stamp" . ",action_host" . ",action_source" . ",action_country)" . " VALUES " . " (" . $campaignid . "," . $trackerid . ",'" . $userid . "'" . "," . $t_stamp . ",'" . $host . "'" . ",'" . $country . "'" . ",'" . $cnv_logstats . "'" . "," . $cnv_clickwindow . "," . $cnv_viewwindow . ",'" . $action . "'" . "," . $action_bannerid . "," . $action_zoneid . "," . $action_t_stamp . ",'" . $action_host . "'" . ",'" . $action_source . "'" . ",'" . $action_country . "')";
                phpAds_dbQuery($log_query) or $report .= "Could not perform SQL: " . $log_query . "\n";
                $conversionlogid = phpAds_dbInsertID();
                $conversion_update_query = "UPDATE " . $phpAds_config['tbl_adconversions'] . " SET conversionlogid=" . $conversionlogid . " WHERE userid='" . $userid . "'" . " AND t_stamp=" . $t_stamp;
                phpAds_dbQuery($conversion_query) or $report .= "Could not perform SQL: " . $conversion_query . "\n";
                $num_conversions++;
            }
        }
    }
    // Now, add up all of the conversions that we just logged and put them into adstats.
    //Process conversions...
    $num_conversions = 0;
    $conversion_query = "SELECT action_bannerid" . ",action_zoneid" . ",action_source" . ",count(*) as conversions" . " FROM " . $phpAds_config['tbl_conversionlog'] . " WHERE t_stamp>=" . $begin_timestamp . " AND t_stamp<" . $end_timestamp . " AND cnv_logstats='y'" . " GROUP BY action_bannerid,action_zoneid,action_source";
    $conversion_result = phpAds_dbQuery($conversion_query) or $report .= "Could not perform SQL: " . $conversion_query . "\n";
    while ($conversion_row = phpAds_dbFetchArray($conversion_result)) {
        $stat_query = "UPDATE " . $phpAds_config['tbl_adstats'] . " SET conversions=conversions+" . $conversion_row['conversions'] . " WHERE day='" . $day . "'" . " AND hour=" . $hour . " AND bannerid=" . $conversion_row['action_bannerid'] . " AND zoneid=" . $conversion_row['action_zoneid'];
        $stat_result = phpAds_dbQuery($stat_query) or $report .= " Could not perform SQL: " . $stat_query . "\n";
        if (phpAds_dbAffectedRows($stat_result) < 1) {
            $stat_query = "INSERT INTO " . $phpAds_config['tbl_adstats'] . " SET day='" . $day . "'" . ",hour=" . $hour . ",bannerid=" . $conversion_row['action_bannerid'] . ",zoneid=" . $conversion_row['action_zoneid'] . ",conversions=" . $conversion_row['conversions'];
            $stat_result = phpAds_dbQuery($stat_query) or $report .= " Could not perform SQL: " . $stat_query . "\n";
        }
        $num_conversions += $conversion_row['conversions'];
    }
    $report .= "\tCounted " . $num_conversions . " conversions in " . (time() - $time) . " seconds.\n\n";
}