D
D
Dmitry Sidorenko2015-01-04 13:55:29
PHP
Dmitry Sidorenko, 2015-01-04 13:55:29

How to force htaccess to check for the presence of a file on the path that needs to be obtained (path) after parsing the mod rewrite url?

Sorry if the question sounds confusing, but after reading the explanation below you will understand what is at stake.
There is a file in the native .htaccess engine
######################
RewriteEngine on
DirectoryIndex index.php
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond % {REQUEST_FILENAME} !-d RewriteRule [%, a
-zA-Z0-9/-/+/()]+[.]{1}[php|html?] index.php
script that
# processes images
RewriteRule ximage/[a-zA-Z\_\-0-9\/]+[.]{1}[jpg|jpeg?] ximg.php
########## ############
The essence of it is that if, when contacting a hosting along the way:
{host}/ximage/1600/1600/upload/models/07871915a8107172b3b5dc15a6574ad3.jpg
it redirects the request to ximg.php.
And ximg.php resizes the image 1600 by 1600 (of course, the numbers can be any supported by the script)
The script itself, depending on the parameters, resizes or crops the image.
But here's the problem.
It always checks the cache, and if there is one, it returns it using PHP!
header('Content-type: ' . $mime);
if ( !is_file($_SERVER['DOCUMENT_ROOT'] . $output) ) {
$file = resize($input, $_SERVER['DOCUMENT_ROOT'] . $output, $h, $w, $ext);
} else {
readfile($_SERVER['DOCUMENT_ROOT'] . $output);
}
Which greatly increases the load on the server.
Is it possible to check for the existence of a file on .htaccess and make sure that it does not access the script if the file is on the server.
An alternative solution would be a header that forces the image to be cached for 2 days from the moment it was created. But I would like to use Apache (mod_rew) tools to make a decision about issuing a cached image.
Thank you for reading up to this point =)
For those who are interested, here is the script itself (I hope it will be useful to someone, but please note that it does not have protection against height brute force):
<?php
$goodExtArr = array(
'jpg' => 1,
'jpeg' => 1,
'png' => 1,
'gif' => 1
);
$goodSizes = [
200 => [
600 => 1,
],
400 => [
600 => 1,
500 => 1
],
600 => [
600 => 1,
500 => 1
],
800 => [
600 => 1,
500 => 1
],
1200 => [
600 => 1,
500 => 1
],
1600 => [
600 => 1,
500 => 1
],
];
$_SERVER['REQUEST_URI'] = str_replace('?10x15crop', '', $_SERVER['REQUEST_URI']);
$input = $_SERVER['REQUEST_URI'];
$input = explode('/', $input);
if ( $input[1] == 'ximage' ) {
$w = intval($input[2]);
$fname = $input[count($input) - 1];
unset($input[0]);
unset($input[1]);
unset($input[2]);
unset($input[3]);
$path = implode('/', $input);
if ( substr($path, 0, 7) != 'upload/' && strpos($path, '../') ) {
die('gg');
}
if ( !isset($goodSizes[$w]) ) {
die('bad size');
}
$ext = explode('.', $path);
$ext = $ext[count($ext)-1];
#$ext = explode('?', $ext);
#$ext = $ext[0];
if ( !isset($goodExtArr[$ext]) ) {
die('bad ext');
}
}
$output = '/upload/img_cache/' . $w. '_' . $h. '_' . md5($path) . $fname;
$input = $_SERVER['DOCUMENT_ROOT'] . '/' . $path;
if ( !is_file($_SERVER['DOCUMENT_ROOT'] . $output) ) {
resize($input, $_SERVER['DOCUMENT_ROOT'] . $output, $h, $w, $ext);
} else {
#$header('Location: ' . $output);
}
$date = date("U");
$date = 1418906326 + 3600*365 * 5;
$date = 1418906326 - 2000;
#echo $date;
#die;
$dt_tmp=getdate($date);
header("Expires: " . gmdate("D, d MYH:i:s",
$date-(86400*($dt_tmp["wday"]-8))) . " GMT");
header("Cache control: public");
#$a = file_get_contents($_SERVER['DOCUMENT_ROOT'] . $output);
header('Content-type: image/jpeg');
readfile($_SERVER['DOCUMENT_ROOT'] . $output);
#echo $a;
} else if ( $ext == 'png' ) {
header('Content-type: image/png');
readfile($_SERVER['DOCUMENT_ROOT'] . $output);
#echo $a;
} else if ( $ext == 'jpg' ) {
header('Content-type: image/gif');
readfile($_SERVER['DOCUMENT_ROOT'] . $output);
#echo $a;
}
function resize($input, $output, $height, $width, $ext) {
$ext = '.' . $ext;
if (file_exists($output)) {
unlink($output);
}
$size = getimagesize($input);
$w = $size[0];
if ($h < $height) {
$a = 1;
$newy = $h;
} else {
$a = $height / $h;
$newy = $height;
}
$newx = $a*$w;
if ($newx > $width) {
$a = $width / $newx;
$newx = $width;
$newy = $newy * $a;
}
if (($ext == ".jpg") || ($ext == ".jpeg") || ($ext == ".png") || ($ext ==".gif")) {
if (($ext == ".jpg") || ($ext == ".jpeg")) {
$source = imagecreatefromjpeg($input) or die('Cannot load original JPEG');
}
if ($ext ==".gif") {
$source = imagecreatefromgif($input) or die(' Cannot load original GIF');
}
if ($ext == ".png") {
$source = imagecreatefrompng($input) or die('Cannot load original PNG');
}
$target = imagecreatetruecolor($newx, $newy);
imagefill($target, 0,0, '0');
imagecopyresampled($target, $source, 0.0, 0.0, $newx, $newy, $size[0], $size[1] );
if (($ext == ".jpg") || ($ext == ".jpeg")) imagejpeg($target, $output, 100);
if ($ext ==".gif") imagegif($target, $output, 85);
if ($ext == ".png") imagepng($target, $output, 0);
imagedestroy($target);
imagedestroy($source);
} else {
copy($input, $output);
print_r($a);
echo '';
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2015-01-04
@sidorenkoda

I haven’t worked with .htaccess for a long time, but if you just check before the rule whether there is such a file or not, and the paths are the same or different?
Those. when the script cuts it really lies at:
{host}/ximage/1600/1600/upload/models/07871915a8107172b3b5dc15a6574ad3.jpg ?

# если картинка, то мод реврайт кидает на скрипт,
# обрабатывающий изображения
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ximage/[a-zA-Z\_\-0-9\/]+[.]{1}[jpg|jpeg?] ximg.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question