Exemple #1
0
function output_cache_file($url)
{
    // no point streaming a local URI
    if (is_url_absolute($url) && get_opendb_config_var('http.stream_external_images', 'enable') !== FALSE && is_uri_domain_in_list($url, get_opendb_config_var('http.stream_external_images', 'domain_list'))) {
        $snoopy = new OpenDbSnoopy();
        $dataBuffer =& $snoopy->fetchURI($url, FALSE);
        if ($dataBuffer !== FALSE) {
            if (is_array($snoopy->headers)) {
                for ($i = 0; $i < count($snoopy->headers); $i++) {
                    header($snoopy->headers[$i]);
                }
            }
            echo $dataBuffer;
            flush();
        } else {
            opendb_redirect($url);
        }
        unset($snoopy);
    } else {
        if (($file = get_item_input_file_upload_url($url)) !== FALSE) {
            // file upload - that is not cached
            opendb_redirect($file);
        } else {
            opendb_redirect($url);
        }
    }
}
Exemple #2
0
function file_cache_insert_file($url, $location, $content_type, $content, $cache_type = 'HTTP', $overwrite = FALSE)
{
    $cache_config_r = file_cache_get_config($cache_type);
    if (is_array($cache_config_r) && $cache_config_r['enable'] !== FALSE) {
        $directory = $cache_config_r['directory'];
        if ($directory === FALSE) {
            return FALSE;
        }
        // we need to know whether we are overwriting existing URL or not
        $file_cache_r = fetch_url_file_cache_r($url, $cache_type, INCLUDE_EXPIRED);
        if (is_array($file_cache_r)) {
            if (!file_cache_get_cache_file($file_cache_r)) {
                opendb_logger(OPENDB_LOG_INFO, __FILE__, __FUNCTION__, 'Cache file record exists, but there is no cache file - will recreate', $file_cache_r);
                $file_cache_r['cache_file'] = NULL;
                // delete thumbnail if it exists
                if (!($thumbnail_file = file_cache_get_cache_file_thumbnail($file_cache_r))) {
                    delete_file($thumbnail_file);
                }
                $file_cache_r['cache_file_thumb'] = NULL;
            } else {
                if (!$overwrite) {
                    opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, 'File cache record exists and cannot be overwritten', $file_cache_r);
                    return FALSE;
                }
            }
        } else {
            $file_cache_r['cache_type'] = $cache_type;
        }
        if ($content == NULL && is_url_absolute($url)) {
            $httpClient = new OpenDbSnoopy();
            $content = $httpClient->fetchURI($url, FALSE);
            if ($content !== FALSE) {
                $location = $httpClient->getLocation();
                $content_type = $httpClient->getContentType();
                unset($httpClient);
            } else {
                unset($httpClient);
                // http client logs error
                return FALSE;
            }
        }
        $file_cache_r['url'] = ifempty($url, $file_cache_r['url']);
        $file_cache_r['location'] = ifempty($location, $file_cache_r['location']);
        $file_cache_r['content_type'] = validate_content_type(ifempty($content_type, $file_cache_r['content_type']));
        $thumbnail_support = FALSE;
        if (strlen($file_cache_r['content_type']) > 0) {
            $file_type_r = fetch_file_type_r($file_cache_r['content_type']);
            $file_cache_r['extension'] = $file_type_r['extension'];
            $thumbnail_support = $file_type_r['thumbnail_support_ind'] == 'Y';
        } else {
            $extension = get_file_ext($file_cache_r['url']);
            if (strlen($extension) > 0 && ($ext_content_type = fetch_file_type_for_extension($extension)) !== FALSE) {
                $content_type = $ext_content_type;
            } else {
                $content_type = validate_content_type($content_type);
            }
            $file_type_r = fetch_file_type_r($content_type);
            if (!is_array($file_type_r)) {
                opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, 'Content type not supported', array($url, $location, $content_type, $cache_type, $overwrite));
                return FALSE;
            }
            $file_cache_r['content_type'] = $content_type;
            $file_cache_r['extension'] = $file_type_r['extension'];
            $thumbnail_support = $file_type_r['thumbnail_support_ind'] == 'Y';
        }
        $file_cache_r['content_length'] = 0;
        if (($uploadFile = get_item_input_file_upload_url($url)) !== FALSE) {
            $file_cache_r['content_length'] = @filesize($uploadFile);
            $file_cache_r['cache_file'] = basename($uploadFile);
            $file_cache_r['upload_file_ind'] = 'Y';
        } else {
            if (strlen($content) > 0) {
                $file_cache_r['content_length'] = strlen($content);
                $file_cache_r['upload_file_ind'] = 'N';
            }
        }
        if ($file_cache_r['content_length'] > 0) {
            if (!is_numeric($file_cache_r['sequence_number'])) {
                $file_cache_r['sequence_number'] = insert_file_cache($cache_type, $file_cache_r['upload_file_ind'], $file_cache_r['url'], $file_cache_r['location'], $file_cache_r['content_type']);
                if ($file_cache_r['sequence_number'] === FALSE) {
                    return FALSE;
                }
            }
            if ($file_cache_r['cache_file'] == NULL) {
                $file_cache_r['cache_file'] = filecache_generate_cache_filename($file_cache_r);
            }
            if ($content != NULL) {
                $directory = file_cache_get_cache_type_directory($file_cache_r['cache_type']);
                $cacheFile = $directory . '/' . $file_cache_r['cache_file'];
                if (!file_put_contents($cacheFile, $content) !== FALSE) {
                    opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, 'Cache file not written', array($cacheFile));
                    return FALSE;
                }
            }
            if ($thumbnail_support) {
                if (strlen($file_cache_r['cache_file_thumb']) == 0) {
                    $file_cache_r['cache_file_thumb'] = filecache_generate_cache_filename($file_cache_r, TRUE);
                }
            }
            if ($file_cache_r['upload_file_ind'] != 'Y') {
                $expire_date = is_numeric($cache_config_r['lifetime']) ? "NOW()+ INTERVAL " . $cache_config_r['lifetime'] . " SECOND" : NULL;
            } else {
                $expire_date = NULL;
            }
            // do not expire uploaded file records.
            if (!update_file_cache($file_cache_r['sequence_number'], $file_cache_r['content_length'], $expire_date, $file_cache_r['cache_file'], $file_cache_r['cache_file_thumb'])) {
                return FALSE;
            }
            if ($thumbnail_support) {
                file_cache_save_thumbnail_file($file_cache_r, $errors);
            }
            return TRUE;
        } else {
            opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, 'File content length is zero', array($url, $location, $content_type, $cache_type, $overwrite));
            return FALSE;
        }
    } else {
        if (!is_array($cache_config_r)) {
            opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, 'No file cache configuration', array($url, $location, $content_type, $cache_type, $overwrite));
            return FALSE;
        } else {
            // file cache is disabled
            return TRUE;
        }
    }
}