News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

2.0.14 proxy.php - explain why 404 thrown for valid image

Started by Shambles, May 16, 2017, 05:54:46 AM

Previous topic - Next topic

Shambles

Ok so there's been a few threads complaining why proxy.php throws a 404 when an image actually exists.

I added to one such thread that I was also suffering.

If a developer could explain the following it'd be appreciated:

In proxy.php there are the following lines:

// Did we get an error when trying to fetch the image
$response = $this->checkRequest();
if (is_int($response)) {
// Throw a 404
header('HTTP/1.0 404 Not Found');
exit;
}


The existence of $response as an integer entity assumes the result of checkRequest() is a failing (404) response.

However, checkRequest() can return this:

// Attempt to cache the request if it doesn't exist
if (!$this->isCached($request)) {
return $this->cacheImage($request);
}


and cacheImage() can return this:

return file_put_contents($dest, json_encode(array(
'content_type' => $headers['content-type'],
'size' => $response['size'],
'time' => time(),
'body' => base64_encode($response['body']),
)));


I dumped the value of $response to a text file and saw all manner of numeric values, each ultimately causing the 404 to be thrown, until the image is cached. I believe the returned value is the number of bytes written?

What am I missing here?

Shambles

This change works in all circumstances, for me:

proxy.php

Code (Find) Select
return file_put_contents($dest, json_encode(array(
'content_type' => $headers['content-type'],
'size' => $response['size'],
'time' => time(),
'body' => base64_encode($response['body']),
)));


Code (Replace) Select
return (file_put_contents($dest, json_encode(array(
'content_type' => $headers['content-type'],
'size' => $response['size'],
'time' => time(),
'body' => base64_encode($response['body']),
)))) === false ? false : true;




tinoest

Quote from: Shambles on May 16, 2017, 06:44:41 AM
This change works in all circumstances, for me:

proxy.php

Code (Find) Select
return file_put_contents($dest, json_encode(array(
'content_type' => $headers['content-type'],
'size' => $response['size'],
'time' => time(),
'body' => base64_encode($response['body']),
)));


Code (Replace) Select
return (file_put_contents($dest, json_encode(array(
'content_type' => $headers['content-type'],
'size' => $response['size'],
'time' => time(),
'body' => base64_encode($response['body']),
)))) === false ? false : true;


Your change is abusing the fact that is_int decides that false isn't a integer, true also isn't treated as a integer.

http://php.net/manual/en/function.is-int.php

So if the file_put_contents was to fail I don't think it would be picked up correctly.

A better fix would be to check if the file_put_contents was false and to return a integer then. If not return a null or similar.

return (file_put_contents($dest, json_encode(array(
'content_type' => $headers['content-type'],
'size' => $response['size'],
'time' => time(),
'body' => base64_encode($response['body']),
)))) === false ? 1 : null;

Shambles

Quote from: tinoest
A better fix would be to check if the file_put_contents was false and to return a integer then.

Then change it.

ChicagolandAnswers

Hello.... I'm not a coder like many of you, so I'm a little wary of replacing code manually.... heck I don't even know where to look for the code. LOL  - Can you point me in the right direction or is there perhaps someone around here who could do it for me, so I don't screw my forum up.  I've been searching for answers for several hours the last two days, but simply don't have the time or knowledge at this point to figure out what to do.... but obviously, I need photos to show up correctly within posts on my forum.


Colin

A fix is being included in 2.0.15 which is being released soon. The changes are made in proxy.php
"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

Advertisement: