Handle images URL via LSurl

This commit is contained in:
Benjamin Renard 2020-05-02 17:49:15 +02:00
parent 8c2e5e604b
commit 50ea12a81c
5 changed files with 67 additions and 41 deletions

View file

@ -1,39 +0,0 @@
<?php
/*******************************************************************************
* Copyright (C) 2007 Easter-eggs
* http://ldapsaisie.labs.libre-entreprise.org
*
* Author: See AUTHORS file in top-level directory.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******************************************************************************/
require_once 'core.php';
if(LSsession :: initialize()) {
if (isset($_GET['i'])) {
$img_path=LStemplate :: getImagePath($_GET['i']);
if (is_file($img_path)) {
header('Content-type: '.mime_content_type($img_path));
header('Cache-Control: public');
header('Pragma: cache');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + 60*60*24*30)); // one month
readfile($img_path);
exit();
}
}
else {
die(_('Missing parameter'));
}
}

View file

@ -418,7 +418,7 @@ function LStemplate_smarty_tr($params) {
function LStemplate_smarty_img($params) { function LStemplate_smarty_img($params) {
extract($params); extract($params);
echo "image.php?i=$name"; echo "image/$name";
} }
function LStemplate_smarty_css($params) { function LStemplate_smarty_css($params) {

View file

@ -639,3 +639,52 @@ function LSdebugDefined() {
} }
return "unknown : ".(string)$callable; return "unknown : ".(string)$callable;
} }
/**
* Dump file content
*
* @param[in] $file_path string The file path to dump
* @param[in] $max_age integer The cache max_age value, as return in Cache-Control HTTP header
* (optional, default: 3600)
*
* @retval void
**/
function dumpFile($file_path, $max_age=3600) {
if (is_file($file_path)) {
header('Content-Type: '.mime_content_type($file_path));
$last_modified_time = filemtime($file_path);
$etag = md5_file($file_path);
header("Cache-Control: max-age=$max_age, must-revalidate");
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("Etag: $etag");
if (
(
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
)
||
(
isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag
)
) {
header("HTTP/1.1 304 Not Modified");
exit();
}
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit();
}
// File not found, Trigger error 404 (via LSurl if defined)
if (class_exists('LSurl')) {
LSurl :: error_404();
}
else {
header("HTTP/1.1 404 Not found");
exit();
}
}

View file

@ -218,7 +218,7 @@ var LSdefault = new Class({
}, },
imagePath: function(image) { imagePath: function(image) {
return 'image.php?i=' + image; return 'image/' + image;
}, },
getParams: function(name) { getParams: function(name) {

View file

@ -45,3 +45,19 @@ function handle_index($request) {
LSsession :: displayTemplate(); LSsession :: displayTemplate();
} }
LSurl :: add_handler('#^(index\.php)?$#', 'handle_index', true); LSurl :: add_handler('#^(index\.php)?$#', 'handle_index', true);
/*
* Handle image request
*
* @param[in] $request LSurlRequest The request
*
* @retval void
**/
function handle_image($request) {
$img_path = LStemplate :: getImagePath($request -> image);
if (is_file($img_path)) {
dumpFile($img_path);
}
LSurl :: error_404($request);
}
LSurl :: add_handler('#^image/(?P<image>[^/]+)$#', 'handle_image', false);