phpAds_dbQuery("\n\t\t\tUPDATE\n\t\t\t\t" . $phpAds_config['tbl_config'] . "\n\t\t\tSET\n\t\t\t\tautotarget_factor = " . $phpAds_config['autotarget_factor'] . "\n\t\t\tWHERE\n\t\t\t\tconfigid = 0\n\t\t\t");
    } elseif (!phpAds_AutoTargetingProfileSum($profile)) {
        // Disable if a null profile was supplied
        $phpAds_config['autotarget_factor'] = -1;
        $report .= "skipped: supplied profile is null\n\n";
    } else {
        $report .= "skipped: already set\n\n";
    }
    $report .= "--------------------------------------------------\n";
    $report .= "Smoothing factor:               " . sprintf('%.2f', $phpAds_config['autotarget_factor']) . "\n";
    $report .= "Today dow:                      " . phpAds_DowToday . "\n";
    $report .= "Today profile value:            " . $profile[phpAds_DowToday] . "\n";
    if ($phpAds_config['autotarget_factor'] != -1) {
        // Targets should not be fully satisfied if using plain autotargeting
        // Smoothing the view profile for later use
        $profile = phpAds_AutoTargetingSmoothProfile($profile, $phpAds_config['autotarget_factor']);
        $report .= "Today smoothed profile value:   " . $profile[phpAds_DowToday] . "\n";
    }
    $report .= "--------------------------------------------------\n\n";
    while ($row = phpAds_dbFetchArray($res)) {
        $target = phpAds_AutoTargetingGetTarget($profile, $row['views'], $row['expire'], $phpAds_config['autotarget_factor']);
        if (is_array($target)) {
            list($target, $debuglog) = $target;
        } else {
            $debuglog = 'no debug info available';
        }
        phpAds_dbQuery("\n\t\t\tUPDATE\n\t\t\t\t" . $phpAds_config['tbl_clients'] . "\n\t\t\tSET\n\t\t\t\ttarget = " . $target . "\n\t\t\tWHERE\n\t\t\t\tclientid = " . $row['clientid'] . "\n\t\t");
        $report .= "\n<b>{$row['clientname']} [id{$row['clientid']}]:</b> {$target} {$debuglog}\n\n";
    }
}
if ($report != '' && $phpAds_config['userlog_priority']) {
function phpAds_AutoTargetingCaclulateFactor($profile, &$debuglog)
{
    global $phpAds_config;
    $debuglog .= "\n";
    // Use standard routine if the profile is empty
    if (!phpAds_AutoTargetingProfileSum($profile)) {
        $debuglog .= "Supplied profile is null, using default algorithm\n";
        return -1;
    }
    $target_profile = array(0, 0, 0, 0, 0, 0, 0);
    $autotarget_campaigns = 0;
    // Fetch all targeted campaigns and all which need autotargeting
    $res = phpAds_dbQuery("SELECT campaignid" . ",views" . ",UNIX_TIMESTAMP(expire) AS expire" . ",target" . " FROM " . $phpAds_config['tbl_campaigns'] . " WHERE active" . " AND weight = 0" . " AND ((expire > NOW() AND views > 0) OR target > 0)");
    $debuglog .= "Targeted campaigns: " . phpAds_dbNumRows($res) . "\n";
    while ($row = phpAds_dbFetchArray($res)) {
        $debuglog .= "\nCAMPAIGN " . $row['campaignid'] . " \n";
        $debuglog .= "--------------------------------------------------\n";
        if ($row['expire']) {
            // Expire date set, get remaining days
            $days = ($row['expire'] - phpAds_LastMidnight) / (double) (60 * 60 * 24);
            $debuglog .= "Days remaining: {$days}\n";
            if ($days <= 0) {
                continue;
            }
            if ($row['views']) {
                // Bought views set, use standard autotargeting
                $daily_target = ceil($row['views'] / $days);
                $autotarget_campaigns++;
                $debuglog .= "Campaign type: Autotargeting\n";
                $debuglog .= "Remaining AdViews: " . $row['views'] . "\n";
                $debuglog .= "Daily target: " . $daily_target . "\n";
            } elseif ($row['target']) {
                // Use target field
                $daily_target = $row['target'];
                $debuglog .= "Campaign type: Expiring high-pri\n";
                $debuglog .= "Daily target: " . $daily_target . "\n";
            } else {
                // Skip campaign
                continue;
            }
        } else {
            // Expire date not set, target field is needed
            if (!$row['target']) {
                // Skip campaign
                continue;
            }
            $daily_target = $row['target'];
            $debuglog .= "Daily target: " . $daily_target . "\n";
            if ($row['views']) {
                $debuglog .= "Campaign type: AdView based high-pri\n";
                // Bought views set, get remaining days using the current target
                $days = ceil($row['views'] / $daily_target);
                $debuglog .= "Remaining AdViews: " . $row['views'] . "\n";
            } else {
                // Set target for the whole week
                $days = 7;
                $debuglog .= "Campaign type: Default high-pri\n";
            }
            $debuglog .= "Days remaining: " . $days . "\n";
        }
        // Add this campaign to the targets profile
        for ($i = 0; $i < $days && $i < 7; $i++) {
            $target_profile[$i] += $daily_target;
        }
    }
    $debuglog .= "\nAutotargeted campaigns: " . $autotarget_campaigns . "\n";
    $debuglog .= "--------------------------------------------------\n";
    if (!$autotarget_campaigns) {
        // No autotargeting is needed, we can use default autotargeting safely
        $debuglog .= "No autotargeting needed, using default algorithm\n";
        return -1;
    }
    // Get the daily target
    $daily_target = phpAds_AutoTargetingProfileSum($target_profile);
    // Set default routine as best match
    $best = array('factor' => array(-1), 'views' => phpAds_AutoTargetingActualViews($profile, $target_profile));
    // Try factors - don't know why, but on my setup $x <= 1 skips $x == 1...
    for ($x = 0; $x <= 1.01; $x += 0.05) {
        // Smooth profile using the current factor and get the view sum
        $smooth_profile = phpAds_AutoTargetingSmoothProfile($profile, $x);
        $views = phpAds_AutoTargetingProfileSum($smooth_profile);
        // Skip if the profile is empty
        if (!$views) {
            continue;
        }
        // Fill the whieighted profile
        $new_profile = array();
        for ($i = 0; $i < 7; $i++) {
            $new_profile[$i] = round($daily_target / $views * $smooth_profile[$i]);
        }
        // Get actual views that could be reached using this profile
        $act_views = phpAds_AutoTargetingActualViews($profile, $new_profile);
        if ($act_views > $best['views']) {
            // Best factor found til now
            $best['factor'] = array($x);
            $best['views'] = $act_views;
        } elseif ($act_views == $best['views']) {
            // Add x to the best factors found til now
            $best['factor'][] = $x;
        }
    }
    if (is_array($best['factor'])) {
        // Get average factor
        $factor = 0;
        while (list(, $v) = each($best['factor'])) {
            $factor += $v;
        }
        $best['factor'] = round($factor * 100 / count($best['factor'])) / 100;
    }
    $debuglog .= "\n";
    return $best['factor'];
}