Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library fails if the URL of image is redirected (i.e. http to https) #51

Closed
DL7BJ opened this issue Nov 24, 2018 · 3 comments
Closed

Library fails if the URL of image is redirected (i.e. http to https) #51

DL7BJ opened this issue Nov 24, 2018 · 3 comments

Comments

@DL7BJ
Copy link

DL7BJ commented Nov 24, 2018

Could it be, that the function @file_get_contents has a problem with some redirected links?

That's a problem in phpBB, if a link to an image is redirected from http to https. The server answers with a 301 or 302, i.e. permanently redirected and after this the image data are transfered.

In this example https://www.stummiforum.de/viewtopic.php?f=14&t=161634&start=25#p1899025 the image link is redirected to https if you call it with http. This server strangely answers first with "Please enable Javascript".

The result of @file_get_contents are the first answer of the server, which isn't the image. So the library tries to find a image size in this data which results in an error:

"It was not possible to determine the dimensions of the image. Please verify that the URL you entered is correct."

I think, there is a javascript for redirect and @file_get_contents get this script result and not an image.

Can this problem be confirmed?

@DL7BJ
Copy link
Author

DL7BJ commented Nov 25, 2018

An addition:

It seems that @file_get_contents can't follow redirections if the redirection is a meta or JavaScript redirection. This debug code opens a new window with the image, the browser executes the JavaScript.

public function getImage($filename, $offset, $length, $forceLength = true)
        {
                if (empty($this->data))
                {
                        $this->data = @file_get_contents($filename, null, null, $offset, $length);
                        // Check data if there is a site redirect using Javascript
                        if(strpos($this->data, "enable JavaScript") == TRUE)
                        {
                                error_log("The site " . $filename . "  uses JavaScript redirection!");
                                echo "<script>window.open('$filename', '_blank'); window.focus();</script>";
                        }
                }

                // Force length to expected one. Return false if data length
                // is smaller than expected length 
                if ($forceLength === true)
                {
                        return (strlen($this->data) < $length) ? false : substr($this->data, $offset, $length) ;
                }
                return empty($this->data) ? false : $this->data;
        }    

I haven't the PHP experience to solve this problem, because the JavaScript must be executed to get the real url of image. There are no fixed string within the JavaScript code (like window.location), the url generation is dynamic.

So I think the library is not useful if the image link redirects using meta or JavaScript redirection.

@marc1706
Copy link
Owner

marc1706 commented Dec 7, 2019

To be honest, this library was never meant as a library that can and should handle all the possible pitfalls of redirects. You should make sure that data passed to this library is a valid image path and/or handle the possibility of this not being the case. Similar issues should also apply to the PHP native getimagesize function which IMHO uses the same core functionality that I am using with file_get_contents.

@DL7BJ
Copy link
Author

DL7BJ commented May 4, 2021

I am not the developer of this forum software. I was only a user there and have had this problem as much others. I think a possible fix must be done by the forum software developers. So I close this here.

Thanks!

@DL7BJ DL7BJ closed this as completed May 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants