function phpAds_decrementCampaigns($day, $hour)
{
    global $phpAds_config, $report;
    //Next, Subtract the number of views for a particular banner...
    $report .= "\tDecrementing High Priority Campaigns...\n";
    $time = time();
    $num_views = 0;
    $num_clicks = 0;
    $num_conversions = 0;
    // Get campaign information
    $campaign_query = "SELECT" . " campaignid" . ",clientid" . ",campaignname" . ",active" . ",views" . ",clicks" . ",conversions" . ",UNIX_TIMESTAMP(expire) AS expire_st" . ",UNIX_TIMESTAMP(activate) AS activate_st" . ",UNIX_TIMESTAMP(NOW()) AS current_st" . " FROM " . $phpAds_config['tbl_campaigns'];
    $campaign_result = phpAds_dbQuery($campaign_query) or $report .= "Could not perform SQL: " . $campaign_query . "\n";
    while ($campaign_row = phpAds_dbFetchArray($campaign_result)) {
        $views = $campaign_row['views'];
        $clicks = $campaign_row['clicks'];
        $conversions = $campaign_row['conversions'];
        $active = $campaign_row['active'];
        if ($views > 0 || $clicks > 0 || $conversions > 0) {
            $count_query = "SELECT" . " SUM(views) AS sum_views" . ",SUM(clicks) AS sum_clicks" . ",SUM(conversions) AS sum_conversions" . " FROM " . $phpAds_config['tbl_adstats'] . "," . $phpAds_config['tbl_banners'] . " WHERE " . $phpAds_config['tbl_banners'] . ".bannerid=" . $phpAds_config['tbl_adstats'] . ".bannerid" . " AND " . $phpAds_config['tbl_banners'] . ".campaignid=" . $campaign_row['campaignid'] . " AND day='" . $day . "'" . " AND hour=" . $hour;
            $count_result = phpAds_dbQuery($count_query) or $report .= "Could not perform SQL: " . $count_query . "\n";
            if ($count_row = phpAds_dbFetchArray($count_result)) {
                if ($views > 0) {
                    $views -= $count_row['sum_views'];
                    if ($views < 1) {
                        $views = 0;
                        $active = 'f';
                    }
                    $num_views += $count_row['sum_views'];
                }
                if ($clicks > 0) {
                    $clicks -= $count_row['sum_clicks'];
                    if ($clicks < 1) {
                        $clicks = 0;
                        $active = 'f';
                    }
                    $num_clicks += $count_row['sum_clicks'];
                }
                if ($conversions > 0) {
                    $conversions -= $count_row['sum_conversions'];
                    if ($conversions < 1) {
                        $conversions = 0;
                        $active = 'f';
                    }
                    $num_conversions += $count_row['sum_conversions'];
                }
            }
        }
        // Check time status...
        if ($campaign_row['current_st'] < $campaign_row['activate_st'] || $campaign_row['current_st'] > $campaign_row['expire_st'] && $campaign_row['expire_st'] != 0) {
            $active = 'f';
        }
        // Check to see if we need to log a change in activation status...
        if ($campaign_row['active'] != $active) {
            $report .= "Sending an email to the owner of campaign " . $campaign_row['campaignid'] . "\n";
            if ($active == 'f') {
                //Send deactivation emails...
                if (!defined('LIBWARNING_INCLUDED')) {
                    require phpAds_path . '/libraries/lib-warnings.inc.php';
                }
                if (!defined('LIBMAIL_INCLUDED')) {
                    require phpAds_path . '/libraries/lib-mail.inc.php';
                }
                phpAds_deactivateMail($campaign_row);
            }
        }
        //Update campaign
        if ($views != $campaign_row['views'] || $clicks != $campaign_row['clicks'] || $conversions != $campaign_row['conversions'] || $active != $campaign_row['active']) {
            $update_query = "UPDATE " . $phpAds_config['tbl_campaigns'] . " SET views=" . $views . ",clicks=" . $clicks . ",conversions=" . $conversions . ",active='" . $active . "'" . " WHERE campaignid=" . $campaign_row['campaignid'];
            phpAds_dbQuery($update_query) or $report .= "Could not perform SQL: " . $update_query . "\n";
            $report .= "\tChanging campaign " . $campaign_row['campaignid'] . ":\n";
            $report .= "\t\tViews:  from " . $campaign_row['views'] . " to " . $views . "\n";
            $report .= "\t\tClicks:  from " . $campaign_row['clicks'] . " to " . $clicks . "\n";
            $report .= "\t\tConversions:  from " . $campaign_row['conversions'] . " to " . $conversions . "\n";
            $report .= "\t\tActive Status:  from " . $campaign_row['active'] . " to " . $active . "\n\n";
        }
    }
    $report .= "\tDecremented a total of " . $num_views . " views, " . $num_clicks . " clicks, and " . $num_conversions . " conversions in " . (time() - $time) . " seconds.\n\n\n";
}
function phpAds_logExpire($clientid, $type = 0)
{
    global $phpAds_config;
    // Get campaign information
    $campaignresult = phpAds_dbQuery("SELECT *, UNIX_TIMESTAMP(expire) AS expire_st, UNIX_TIMESTAMP(activate) AS activate_st FROM " . $phpAds_config['tbl_clients'] . " WHERE clientid = '" . $clientid . "'");
    if ($campaign = phpAds_dbFetchArray($campaignresult)) {
        // Decrement views
        if ($type == phpAds_Views && $campaign['views'] > 0) {
            phpAds_dbQuery("UPDATE " . $phpAds_config['tbl_clients'] . " SET views = views - 1 WHERE clientid = '" . $clientid . "'");
            $campaign['views']--;
            // Mail warning - preset is reached
            if ($campaign['views'] == $phpAds_config['warn_limit'] && ($phpAds_config['warn_admin'] || $phpAds_config['warn_client'])) {
                // Include warning library
                if (!defined('LIBWARNING_INCLUDED')) {
                    require phpAds_path . '/libraries/lib-warnings.inc.php';
                }
                if (!defined('LIBMAIL_INCLUDED')) {
                    require phpAds_path . '/libraries/lib-mail.inc.php';
                }
                if (!defined('LIBUSERLOG_INCLUDED')) {
                    require phpAds_path . '/libraries/lib-userlog.inc.php';
                }
                phpAds_userlogSetUser(phpAds_userDeliveryEngine);
                phpAds_warningMail($campaign);
            }
        }
        // Decrement clicks
        if ($type == phpAds_Clicks && $campaign['clicks'] > 0) {
            phpAds_dbQuery("UPDATE " . $phpAds_config['tbl_clients'] . " SET clicks = clicks - 1 WHERE clientid='" . $clientid . "'");
            $campaign['clicks']--;
        }
        // Check activation status
        $active = "t";
        if ($campaign["clicks"] == 0 || $campaign["views"] == 0 || time() < $campaign["activate_st"] || time() > $campaign["expire_st"] && $campaign["expire_st"] != 0) {
            $active = "f";
        }
        if ($campaign["active"] != $active) {
            if (!defined('LIBUSERLOG_INCLUDED')) {
                require phpAds_path . '/libraries/lib-userlog.inc.php';
            }
            // Log deactivation
            phpAds_userlogSetUser(phpAds_userDeliveryEngine);
            phpAds_userlogAdd(phpAds_actionDeactiveCampaign, $campaign['clientid']);
            // Deactivate campaign
            phpAds_dbQuery("UPDATE " . $phpAds_config['tbl_clients'] . " SET active='" . $active . "' WHERE clientid='" . $clientid . "'");
            // Send deactivation warning
            if ($active == 'f') {
                // Rebuild priorities
                if (!defined('LIBPRIORITY_INCLUDED')) {
                    require phpAds_path . '/libraries/lib-priority.inc.php';
                }
                phpAds_PriorityCalculate();
                // Recalculate cache
                if (!defined('LIBVIEWCACHE_INCLUDED')) {
                    include phpAds_path . '/libraries/deliverycache/cache-' . $phpAds_config['delivery_caching'] . '.inc.php';
                }
                phpAds_cacheDelete();
                // Include warning library
                if (!defined('LIBWARNING_INCLUDED')) {
                    require phpAds_path . '/libraries/lib-warnings.inc.php';
                }
                if (!defined('LIBMAIL_INCLUDED')) {
                    require phpAds_path . '/libraries/lib-mail.inc.php';
                }
                phpAds_deactivateMail($campaign);
            }
        }
    }
}
    }
    // Send Query
    $res_campaigns = phpAds_dbQuery("\n\t\tSELECT\n\t\t\tclientid,\n\t\t\tclientname,\n\t\t\tparent,\n\t\t\tviews,\n\t\t\tclicks,\n\t\t\texpire,\n\t\t\tUNIX_TIMESTAMP(expire) as expire_st,\n\t\t\tactivate,\n\t\t\tUNIX_TIMESTAMP(activate) as activate_st,\n\t\t\tactive\n\t\tFROM\n\t\t\t" . $phpAds_config['tbl_clients'] . "\n\t\tWHERE\n\t\t\tparent = " . $client['clientid'] . "\n\t\t") or die($GLOBALS['strLogErrorClients']);
    while ($campaign = phpAds_dbFetchArray($res_campaigns)) {
        $active = "t";
        if ($campaign["clicks"] == 0 || $campaign["views"] == 0) {
            $active = "f";
        }
        if (time() < $campaign["activate_st"]) {
            $active = "f";
        }
        if (time() > $campaign["expire_st"] && $campaign["expire_st"] != 0) {
            $active = "f";
        }
        if ($campaign["active"] != $active) {
            if ($active == "t") {
                phpAds_userlogAdd(phpAds_actionActiveCampaign, $campaign['clientid']);
            } else {
                phpAds_userlogAdd(phpAds_actionDeactiveCampaign, $campaign['clientid']);
                phpAds_deactivateMail($campaign);
            }
            phpAds_dbQuery("UPDATE " . $phpAds_config['tbl_clients'] . " SET active='{$active}' WHERE clientid=" . $campaign['clientid']);
        }
        if ($active == "t" && ($phpAds_config['warn_admin'] || $phpAds_config['warn_client'])) {
            $days_left = round(($campaign["expire_st"] - phpAds_LastMidnight) / (60 * 60 * 24));
            if ($days_left == $phpAds_config['warn_limit_days']) {
                phpAds_warningMail($campaign, $campaign["expire_st"]);
            }
        }
    }
}