duonghien201089 / timthumb

Automatically exported from code.google.com/p/timthumb
0 stars 0 forks source link

getLocalImagePath returns empty string if $_SERVER['DOCUMENT_ROOT'] incorrect #242

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. $_SERVER['DOCUMENT_ROOT'] is set to "/htdocs"
2. timthumb is part of site with doc root NOT "/htdocs"

What is the expected output? What do you see instead?

Given the above conditions, getLocalImagePath returns an empty string, because 
realpath cannot resolve the incorrect $_SERVER['DOCUMENT_ROOT'] to a filesystem 
location. The error that results is: "Could not find the internal image you 
specified." even though file exists and can be loaded directly.

What version of the product are you using? On what operating system?

version 2 on Ubuntu

Please provide any additional information below.

in the calcDocRoot function, $docRoot gets set, but because the 'DOCUMENT_ROOT' 
$_SERVER var is incorrect, the realpath does not resolve -- perhaps resolve the 
docroot relative to the realpath of the timthumb script file (versus its url)?

Original issue reported on code.google.com by stewart....@gmail.com on 19 Aug 2011 at 3:17

GoogleCodeExporter commented 8 years ago
temporary fix is to set $_SERVER['DOCUMENT_ROOT'] to the correct doc root -- 
however the bug still applies re misleading error message being output

Original comment by stewart....@gmail.com on 19 Aug 2011 at 3:18

GoogleCodeExporter commented 8 years ago
looking in the Apache config and also PHP.ini it seems that the document root 
is not set and defaults to "/htdocs" -- becuase I'm using mass virtual hosting 
with a VirtualDocumentRoot.

See here for the same kind of problem: http://stackoverflow.com/q/390276

Original comment by stewart....@gmail.com on 19 Aug 2011 at 3:28

GoogleCodeExporter commented 8 years ago
I guess the solution might be to set the $_SERVER['DOCUMENT_ROOT'] to an empty 
string or FALSE so that the alternate method of finding the docroot is used...

Original comment by stewart....@gmail.com on 19 Aug 2011 at 3:29

GoogleCodeExporter commented 8 years ago
Nope.

Turns out to fix I had to set it as such:

$_SERVER['DOCUMENT_ROOT'] = '/var/www/' . $_SERVER['SERVER_NAME'] . '/html';

because in my Apache config I have:

VirtualDocumentRoot /var/www/%0/html

Original comment by stewart....@gmail.com on 19 Aug 2011 at 3:35

GoogleCodeExporter commented 8 years ago
I have a similar problem: http://code.google.com/p/timthumb/issues/detail?id=240

Original comment by tosz...@gmail.com on 20 Aug 2011 at 11:02

GoogleCodeExporter commented 8 years ago
Yeah I still had the same problem even with that fix, so I added the following 
to the top of my timthumb.php (I develop in Windows and my staging server is 
Linux):

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    $_SERVER['DOCUMENT_ROOT'] = 'C:\www\\' . $_SERVER['SERVER_NAME'] . '\html';
} else {
    $_SERVER['DOCUMENT_ROOT'] = '/var/www/' . $_SERVER['SERVER_NAME'] . '/html';
}

Perhaps an optional variable where users could set their document root would be 
handy (assuming it's not set correctly or my case where you're using a mass 
virtual hosting Apache config).

Original comment by stewart....@gmail.com on 31 Aug 2011 at 9:19

GoogleCodeExporter commented 8 years ago
The script now allows a custom settings file to be loaded. Just create a file 
called timthumb-config.php and add your settings to it. As such I would 
override the document_root there.

Original comment by BinaryMoon on 1 Sep 2011 at 12:18

GoogleCodeExporter commented 8 years ago
Excellent! :) Thanks!

Original comment by stewart....@gmail.com on 1 Sep 2011 at 2:45

GoogleCodeExporter commented 8 years ago
Sometimes it's just a simple fix: Replace line 879 with the following:

$img = $_SERVER['DOCUMENT_ROOT'] . "/$src"; 
if (file_exists($img)) { return $img; } else { return false; }

That fixed it for me...

Original comment by der.kunstler on 16 Nov 2011 at 5:33

GoogleCodeExporter commented 8 years ago
nice fix there der. Worked for me to

Original comment by sjp...@gmail.com on 6 Dec 2011 at 9:10

GoogleCodeExporter commented 8 years ago
Unfortunately there's still a piece to fix, what do you think happens here if 
DOCUMENT_ROOT is  '/' ?

// account for Windows directory structure
if (strstr($_SERVER['SCRIPT_FILENAME'],':')) {
    $sub_directories = explode('\\', str_replace($this->docRoot, '', $_SERVER['SCRIPT_FILENAME']));
} else {
     $sub_directories = explode('/', str_replace($this->docRoot, '', $_SERVER['SCRIPT_FILENAME']));
}

Yeah, it will remove slashes from the the whole path! So insted of getting 
images/full/cupcakes.jpg you'll have imagesfull/cupcakes.jpg which will 
obviously fail.
Only the first occurence should be replaced.

Original comment by a.sav...@webgreen.it on 18 Oct 2012 at 9:06

GoogleCodeExporter commented 8 years ago
There is a quickfix for this issue.
Somewere inside the class constructor (around/close to line 225?), you will 
find this piece of code:

if(preg_match('/https?:\/\/(?:www\.)?' . $this->myHost . '(?:$|\/)/i', 
$this->src)){
    $this->src = preg_replace('/https?:\/\/(?:www\.)?' . $this->myHost . '/i', '', $this->src);
}

Comment these three lines and the script will assume the image as an external 
resource, just make sure you give it the full address and your php.ini allows 
external resources and everything works just great.

if you intend to give it the partial url and the script can't seem to find the 
image, goto line 810? inside calcDocRoot func, and after:

$docRoot = @$_SERVER['DOCUMENT_ROOT'];

insert the used document root (under linux will be something similar to):

$docRoot = '/home/{user_name}/public_html/{index_dir}/';

where
user_name = your user name in the system (maybe account name inside an online 
server); and
index_dir = directory where your index file is located (might not be needed if 
located in public_html)

Original comment by fabioe...@gmail.com on 5 Dec 2013 at 11:50