Returns information with regards to handling the http request
public static handleContext ( array $context ) : array | ||
$context | array | Data about the context for which to http request is sent |
return | array | of updated context information |
/** * 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; }
/** * Creates HTTP request using file_get_contents * * @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 httpRequestFopen($url, $method, $return_only_status = false, $content = null, $header = "") { $context = array( 'http' => array( 'method' => $method, 'request_fulluri' => true, 'timeout' => 10, 'user_agent' => 'phpMyAdmin', 'header' => "Accept: */*", ) ); if ($header) { $context['http']['header'] .= "\n" . $header; } if ($method == "POST") { $context['http']['content'] = $content; } $context = Util::handleContext($context); $response = @file_get_contents( $url, false, stream_context_create($context) ); if (! isset($http_response_header)) { return null; } preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#", $http_response_header[0], $out ); $http_status = intval($out[1]); return Util::httpRequestReturn($response, $http_status, $return_only_status); }