function phpAds_performAutoMaintenance()
{
    global $phpAds_config;
    // Make sure that the output is sent to the browser before
    // loading libraries and connecting to the db
    flush();
    // Include required files
    if (!defined('LIBLOCKS_INCLUDED')) {
        require phpAds_path . '/libraries/lib-locks.inc.php';
    }
    // Load config from the db
    phpAds_LoadDbConfig();
    $last_run = $phpAds_config['maintenance_timestamp'];
    // Make sure that negative values don't break the script
    if ($last_run > 0) {
        $last_run = strtotime(date('Y-m-d H:00:05', $last_run));
    }
    if (time() >= $last_run + 3600) {
        if ($lock = phpAds_maintenanceGetLock()) {
            require phpAds_path . "/libraries/lib-userlog.inc.php";
            require phpAds_path . "/maintenance/lib-maintenance.inc.php";
            // Got the advisory lock, we can proceed running maintenance
            phpAds_userlogSetUser(phpAds_userAutoMaintenance);
            // Finally run maintenance
            phpAds_performMaintenance();
            // Release lock
            phpAds_maintenanceReleaseLock($lock);
        }
    }
}
function phpAds_performMaintenance()
{
    global $phpAds_config;
    // Include required files
    if (!defined('LIBLOCKS_INCLUDED')) {
        require phpAds_path . '/libraries/lib-locks.inc.php';
    }
    // Aquire lock to ensure that maintenance runs only once
    if ($lock = phpAds_maintenanceGetLock()) {
        // Set time limit and ignore user abort
        if (!get_cfg_var('safe_mode')) {
            @set_time_limit(300);
            @ignore_user_abort(1);
        }
        // Include required files
        if (!defined('LIBMAIL_INCLUDED')) {
            require phpAds_path . "/libraries/lib-mail.inc.php";
        }
        if (!defined('LIBADMINSTATISTICS_INCLUDED')) {
            require phpAds_path . "/admin/lib-statistics.inc.php";
        }
        if (!defined('LIBADMINCONFIG_INCLUDED')) {
            require phpAds_path . "/admin/lib-config.inc.php";
        }
        // Load language strings
        @(include phpAds_path . '/language/english/default.lang.php');
        if ($phpAds_config['language'] != 'english' && file_exists(phpAds_path . '/language/' . $phpAds_config['language'] . '/default.lang.php')) {
            @(include phpAds_path . '/language/' . $phpAds_config['language'] . '/default.lang.php');
        }
        // Update the timestamp
        $res = phpAds_dbQuery("\n\t\t\tUPDATE\n\t\t\t\t" . $phpAds_config['tbl_config'] . "\n\t\t\tSET\n\t\t\t\tmaintenance_timestamp = '" . time() . "'\n\t\t");
        // Run different maintenance tasks on midnight or soon after if the last run was before midnight
        if ($phpAds_config['maintenance_timestamp'] < phpAds_LastMidnight) {
            include phpAds_path . "/maintenance/maintenance-reports.php";
            include phpAds_path . "/maintenance/maintenance-activation.php";
            include phpAds_path . "/maintenance/maintenance-autotargeting.php";
            include phpAds_path . "/maintenance/maintenance-geotargeting.php";
            include phpAds_path . "/maintenance/maintenance-cleantables.php";
            include phpAds_path . "/maintenance/maintenance-openadssync.php";
        }
        include phpAds_path . "/maintenance/maintenance-priority.php";
        // Rebuild cache
        if (!defined('LIBVIEWCACHE_INCLUDED')) {
            include phpAds_path . '/libraries/deliverycache/cache-' . $phpAds_config['delivery_caching'] . '.inc.php';
        }
        phpAds_cacheDelete();
        // Release lock
        phpAds_maintenanceReleaseLock($lock);
        return true;
    }
    return false;
}