Exemplo n.º 1
0
function au_url($url = '', $redirect = false)
{
    global $aulis;
    // If we have a request starting with ?, it's for the main file
    if (au_string_starts_with($url, '?')) {
        $url = $aulis['absolute_path'] . $aulis['file'] . $url;
    } elseif (au_string_starts_with($url, 'au://')) {
        $exploded = explode('au://', $url);
        // Recursion is awesome!
        $url = au_url($exploded[1]);
    } elseif (au_string_starts_with($url, 'http://') or au_string_starts_with($url, 'ftp://') or au_string_starts_with($url, 'https://') or au_string_starts_with($url, 'mailto:')) {
        $url = $url;
    } else {
        $url = $aulis['absolute_path'] . $url;
    }
    // If we are requested to redirect us to the given URL, we have to do that of course.
    if ($redirect) {
        return header("Location:" . $url);
    } else {
        return $url;
    }
}
function au_query($original_sql, $force_no_cache = false, $force_no_count = false)
{
    global $aulis, $setting;
    // We like counting
    if (!$force_no_count) {
        $aulis['db_query_count']++;
    }
    // Make sure we have the right database prefix.
    $search = array("FROM ", "INTO ", "UPDATE ", "JOIN ");
    $replace = array("FROM " . $aulis['db_prefix'], "INTO " . $aulis["db_prefix"], "UPDATE " . $aulis["db_prefix"], "JOIN " . $aulis["db_prefix"]);
    $sql = str_replace($search, $replace, $original_sql);
    // Are we in debug mode? ONLY ALPHA :: NOTE: THIS WILL SEND THE HEADERS AWAY
    if (DEBUG_SHOW_QUERIES) {
        echo "<div class='notice bg1 cwhite'>" . $sql . "</div>";
    }
    // If query caching is disabled, we just need to execute the query
    if ($force_no_cache or @$setting['enable_query_caching'] == 0) {
        return $aulis["db"]->query($sql);
    }
    // If this is not a select query, it will change something, therefore the cache needs to be cleaned
    if (!au_string_starts_with($sql, "SELECT")) {
        au_force_clean_cache();
    }
    // Only select queries can be cached
    if (!au_string_starts_with($sql, "SELECT")) {
        return $aulis["db"]->query($sql);
    }
    // We need the queries hash
    $hash = md5($sql);
    $cache_file = au_get_path_from_root('cache/queries/' . $hash . '.cache');
    $cache_folder = au_get_path_from_root('cache/queries');
    $cache_time = $setting['query_caching_time'];
    // If we are not writable, we have to run the query without cache
    if (!is_writable($cache_folder)) {
        return $aulis["db"]->query($sql);
    }
    // We need to see if there are any queries like these done within the query_cache_time
    if (file_exists($cache_file)) {
        // Our file exists... let's get its creation time
        $cache_file_time = filemtime($cache_file);
        // Is the file still valid?
        if (time() - $cache_file_time < $cache_time and $aulis['db_query_count']--) {
            return unserialize(file_get_contents($cache_file));
        } else {
            if (unlink($cache_file)) {
                return au_query($original_sql, false, true);
            }
        }
    } else {
        // We need to execute the query, cache it and return the cached object
        $execute = $aulis['db']->query($sql);
        // If the rowCount is 0, we can just create an empty cached query
        if ($execute->rowCount() == 0) {
            $cache_query = new au_class_cached_query();
        } else {
            // Fetching the objects in order to cache them
            $objects = array();
            while ($object = $execute->fetchObject()) {
                $objects[] = $object;
            }
            // Create the cached query
            $cache_query = new au_class_cached_query($objects, $execute->rowCount());
        }
        // Cache the whole thing, if we cannot do that, we need to fallback
        if (!file_put_contents($cache_file, serialize($cache_query))) {
            return au_query($original_sql, true);
        }
        return $cache_query;
    }
}