Advertisement:

Author Topic: 2.0.14 proxy.php - explain why 404 thrown for valid image  (Read 2293 times)

Offline Shambles

  • SMF Hero
  • ******
  • Posts: 5,097
  • Gender: Male
    • i30 Owners Club
2.0.14 proxy.php - explain why 404 thrown for valid image
« on: May 16, 2017, 05:54:46 AM »
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:

Code: [Select]
// 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:

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

and cacheImage() can return this:

Code: [Select]
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?

Offline Shambles

  • SMF Hero
  • ******
  • Posts: 5,097
  • Gender: Male
    • i30 Owners Club
Re: 2.0.14 proxy.php - explain why 404 thrown for valid image
« Reply #1 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;

Offline Yariksat

  • Semi-Newbie
  • *
  • Posts: 38
Re: 2.0.14 proxy.php - explain why 404 thrown for valid image
« Reply #2 on: May 16, 2017, 07:02:41 AM »
This edit works for me

Offline rapalero

  • Semi-Newbie
  • *
  • Posts: 82
Re: 2.0.14 proxy.php - explain why 404 thrown for valid image
« Reply #3 on: May 26, 2017, 08:16:08 PM »
Works. Thanks.  ;D

Offline NekoSensei

  • Semi-Newbie
  • *
  • Posts: 60
  • Gender: Male
  • SMF User
    • Le Pays Du Manga et des Cartoons oubliés
Re: 2.0.14 proxy.php - explain why 404 thrown for valid image
« Reply #4 on: May 30, 2017, 11:16:00 AM »
No more error, thanks !

Offline tinoest

  • Development Contributor
  • Jr. Member
  • *
  • Posts: 246
Re: 2.0.14 proxy.php - explain why 404 thrown for valid image
« Reply #5 on: May 30, 2017, 03:51:29 PM »
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.

Code: [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 ? 1 : null;

Offline Shambles

  • SMF Hero
  • ******
  • Posts: 5,097
  • Gender: Male
    • i30 Owners Club
Re: 2.0.14 proxy.php - explain why 404 thrown for valid image
« Reply #6 on: May 30, 2017, 06:58:27 PM »
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.

Offline ChicagolandAnswers

  • Newbie
  • *
  • Posts: 4
Re: 2.0.14 proxy.php - explain why 404 thrown for valid image
« Reply #7 on: June 22, 2017, 01:40:46 AM »
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.


Offline Colin

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 7,867
  • Gender: Male
  • SMF Developer
    • colinschoen on GitHub
Re: 2.0.14 proxy.php - explain why 404 thrown for valid image
« Reply #8 on: June 22, 2017, 11:23:36 AM »
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