Updates an existing curl as necessary
public static configureCurl ( resource $curl_handle ) : resource | ||
$curl_handle | resource | A curl_handle resource created by curl_init which should have several options set |
return | resource | curl_handle with updated options |
/** * Returns information with latest version from phpmyadmin.net * * @return object JSON decoded object with the data */ public function getLatestVersion() { if (!$GLOBALS['cfg']['VersionCheck']) { return null; } // wait 3s at most for server response, it's enough to get information // from a working server $connection_timeout = 3; $response = '{}'; // Get response text from phpmyadmin.net or from the session // Update cache every 6 hours if (isset($_SESSION['cache']['version_check']) && time() < $_SESSION['cache']['version_check']['timestamp'] + 3600 * 6) { $save = false; $response = $_SESSION['cache']['version_check']['response']; } else { $save = true; $file = 'https://www.phpmyadmin.net/home_page/version.json'; if (ini_get('allow_url_fopen')) { $context = array('http' => array('request_fulluri' => true, 'timeout' => $connection_timeout)); $context = Util::handleContext($context); if (!defined('TESTSUITE')) { session_write_close(); } $response = file_get_contents($file, false, stream_context_create($context)); } else { if (function_exists('curl_init')) { $curl_handle = curl_init($file); if ($curl_handle === false) { return null; } $curl_handle = Util::configureCurl($curl_handle); curl_setopt($curl_handle, CURLOPT_HEADER, false); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_handle, CURLOPT_TIMEOUT, $connection_timeout); if (!defined('TESTSUITE')) { session_write_close(); } $response = curl_exec($curl_handle); } } } /* Parse response */ $data = json_decode($response); /* Basic sanity checking */ if (!is_object($data) || empty($data->version) || empty($data->releases) || empty($data->date)) { return null; } if ($save) { if (!isset($_SESSION) && !defined('TESTSUITE')) { ini_set('session.use_only_cookies', 'false'); ini_set('session.use_cookies', 'false'); ini_set('session.use_trans_sid', 'false'); ini_set('session.cache_limiter', 'nocache'); session_start(); } $_SESSION['cache']['version_check'] = array('response' => $response, 'timestamp' => time()); } return $data; }
/** * Checks if given URL is 200 or 404, optionally returns data * * @param string $link the URL to check * @param boolean $get_body whether to retrieve body of document * * @return string|boolean test result or data */ public function checkHTTP($link, $get_body = false) { if (!function_exists('curl_init')) { return null; } $handle = curl_init($link); if ($handle === false) { return null; } Util::configureCurl($handle); curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($handle, CURLOPT_TIMEOUT, 5); curl_setopt($handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); if (!defined('TESTSUITE')) { session_write_close(); } $data = @curl_exec($handle); if (!defined('TESTSUITE')) { ini_set('session.use_only_cookies', '0'); ini_set('session.use_cookies', '0'); ini_set('session.use_trans_sid', '0'); ini_set('session.cache_limiter', 'nocache'); session_start(); } if ($data === false) { return null; } $http_status = curl_getinfo($handle, CURLINFO_HTTP_CODE); if ($http_status == 200) { return $get_body ? $data : true; } if ($http_status == 404) { return false; } return null; }
/** * Creates HTTP request using curl * * @param string $url Url to send the request * @param string $method HTTP request method (GET, POST, PUT, DELETE, etc) * @param bool $return_only_status If set to true, the method would only return response status * @param mixed $content Content to be sent with HTTP request * @param string $header Header to be set for the HTTP request * * @return mixed */ public static function httpRequestCurl($url, $method, $return_only_status = false, $content = null, $header = "") { $curl_handle = curl_init($url); if ($curl_handle === false) { return null; } $curl_handle = Util::configureCurl($curl_handle); if ($method != "GET") { curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, $method); } if ($header) { curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array($header)); curl_setopt($curl_handle, CURLOPT_HEADER, true); } if ($method == "POST") { curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $content); } curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, '2'); curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, '1'); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,true); curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($curl_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($curl_handle, CURLOPT_TIMEOUT, 10); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10); $response = @curl_exec($curl_handle); if ($response === false) { return null; } $http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE); return Util::httpRequestReturn($response, $http_status, $return_only_status); }