<?php
/*
* Copyright 2008 Blandware (http://www.blandware.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This file is used to send resource to browser.
*
* @package AtleapLite
* @author Roman Puchkovskiy
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
*/
require_once 'server_options.php';
/**
* Including common entry file header
*/
require 'include/entry_common.php';
require_once 'include/common.php';
$ok = true;
$fileName = $_GET['fileName'];
$notFound = false;
if (empty($fileName)) {
$ok = false;
$notFound = true;
}
if ($ok) {
configurateDataObject($dsn);
$cacheManager = new CacheManager();
$dao =& $cacheManager->getResource($fileName);
if (!$dao) {
// cache miss
$dao =& getDao('resource');
$dao->findByFileName($fileName);
if (empty($dao->id)) {
$ok = false;
$notFound = true;
} else {
if ($dao->size <= CACHED_RESOURCE_MAX_SIZE) {
$cacheManager->putResource($dao);
}
}
}
}
if ($ok) {
$ok = $dao->isActive();
if (!$ok) {
$notFound = true;
}
}
if (!$ok) {
if ($notFound) {
header("HTTP/1.0 404 Not Found");
}
} else {
$lastModified = strtotime($dao->modification_date);
if ($_SERVER['REQUEST_METHOD'] == "GET" && $lastModified != -1) {
if (processBrowserCache($lastModified)) {
exit;
}
}
$mimeType = detectMimeType($dao->file_name);
$download = $dao->isDownload() || $mimeType == null;
if ($download) {
header("Content-Disposition: attachment; filename=\"{$dao->file_name}\"");
header("Content-Type: application/octet-stream");
} else {
header("Content-Disposition: inline; filename=\"{$dao->file_name}\"");
header("Content-Type: $mimeType");
}
header("Content-Transfer-Encoding: binary");
header("Content-Length: {$dao->size}");
$data = unescapeBlobFromSelect($dao->data);
echo $data;
}
/**
* Implements client cache logic.
*
* @param long $lastModified last modified time-stamp
* @return true if resource is cached and nothing needs to be output
*/
function processBrowserCache($lastModified) {
//ETag generation
$username = getCurrentUser();
$eTag = "W/\"$username-$lastModified\"";
header("ETag: $eTag");
$conditionSatisfied = false;
$maxAge = getGlobalProperty('resourceCacheMaxAge');
if ($maxAge < 0) {
header("Cache-Control: no-cache,no-store,max-age=0");
header("Pragma: no-cache");
} else {
$cacheType = "public";
header("Cache-Control: $cacheType,max-age=$maxAge");
header("Pragma: ");
$headerValue = $_SERVER['HTTP_IF_NONE_MATCH'];
if (get_magic_quotes_gpc() == 1) {
$headerValue = stripslashes($headerValue);
}
if ($headerValue != null) {
if ($headerValue != "*") {
$tokens = explode(',', $headerValue);
foreach ($tokens as $currentToken) {
if (trim($currentToken) == $eTag) {
$conditionSatisfied = true;
}
}
} else {
$conditionSatisfied = true;
}
if ($conditionSatisfied) {
header("HTTP/1.0 304 Not Modified");
return true;
}
}
}
return $conditionSatisfied;
}
?>