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

fetch_remote_file is dated and needs updating #4696

Open
RevertIT opened this issue May 4, 2023 · 0 comments
Open

fetch_remote_file is dated and needs updating #4696

RevertIT opened this issue May 4, 2023 · 0 comments

Comments

@RevertIT
Copy link
Contributor

RevertIT commented May 4, 2023

As I was trying to build a new plugin I was stopped by the native fetch function due to the code logic being dated. Many things need improvement in the code and today I've hit many issues with the function itself so I had to make a custom implementation.

  1. There is a wrong/useless part that prevents fetching data from sites with custom host:port values like https://somesite:8443/api/data.json
(!in_array($url_components['port'], array(80, 8080, 443)))
  1. I couldn't set a method if API requests PUT or DELETE. There needs to be something like the 4th param with @param $method = "GET"
// Set the method
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
    
// Set the data if it's a PUT or POST request
if ($method == 'PUT' || $method == 'POST')
{
   curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
}
  1. Authorization token? Custom headers... 5th optional @param $headers = array().
// Set the headers
if (!empty($headers))
{
   curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}

And set equivalent for fsocks headers in this part

if(!empty($post_body))
		{
			$headers[] = "POST {$url_components['path']} HTTP/1.0";
			$headers[] = "Content-Length: ".strlen($post_body);
			$headers[] = "Content-Type: application/x-www-form-urlencoded";
		}
		else
		{
			$headers[] = "GET {$url_components['path']} HTTP/1.0";
		}

Also, this part is redundant as well and should be removed if someone set https and port 80, there must be a reason for that.

		if($url_components['scheme'] == 'https')
		{
			$scheme = 'ssl://';
			if($url_components['port'] == 80)
			{
				$url_components['port'] = 443;
			}
		}

This function overall is very powerful, but very dated and there is no reason not to update it. As it hooks in many plugins, we can jus add optional parameters on the function's end and meet today's standards.

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

1 participant