From 1bd8c02102c0011ce0e88e5484b05f66b08ccbc9 Mon Sep 17 00:00:00 2001 From: jmontoyaa Date: Wed, 12 Jul 2017 09:19:04 +0200 Subject: [PATCH] Use GuzzleHttp\Client instead of custom code see BT#13047 --- main/admin/index.php | 20 ++++-- main/inc/ajax/admin.ajax.php | 120 ++++++++++++----------------------- main/inc/ajax/link.ajax.php | 36 +---------- main/inc/lib/link.lib.php | 44 +++++++++++++ 4 files changed, 102 insertions(+), 118 deletions(-) diff --git a/main/admin/index.php b/main/admin/index.php index a30beed2b4..d337049253 100644 --- a/main/admin/index.php +++ b/main/admin/index.php @@ -481,7 +481,13 @@ if (api_is_platform_admin()) { /* Chamilo.org */ - $blocks['chamilo']['icon'] = Display::return_icon('platform.png', 'Chamilo.org', array(), ICON_SIZE_MEDIUM, false); + $blocks['chamilo']['icon'] = Display::return_icon( + 'platform.png', + 'Chamilo.org', + array(), + ICON_SIZE_MEDIUM, + false + ); $blocks['chamilo']['label'] = 'Chamilo.org'; $blocks['chamilo']['class'] = 'block-admin-chamilo'; @@ -505,12 +511,18 @@ if (api_is_platform_admin()) { $blocks['chamilo']['search_form'] = null; // Version check - $blocks['version_check']['icon'] = Display::return_icon('platform.png', 'Chamilo.org', array(), ICON_SIZE_MEDIUM, false); + $blocks['version_check']['icon'] = Display::return_icon( + 'platform.png', + 'Chamilo.org', + array(), + ICON_SIZE_MEDIUM, + false + ); $blocks['version_check']['label'] = get_lang('VersionCheck'); $blocks['version_check']['extra'] = '
'; $blocks['version_check']['search_form'] = null; - $blocks['version_check']['items'] = null; - $blocks['version_check']['class'] = 'block-admin-version_check'; + $blocks['version_check']['items'] = '
'; + $blocks['version_check']['class'] = ''; // Check Hook Event for Admin Block Object if (!empty($hook)) { diff --git a/main/inc/ajax/admin.ajax.php b/main/inc/ajax/admin.ajax.php index 6bb6507526..c07cd325d8 100755 --- a/main/inc/ajax/admin.ajax.php +++ b/main/inc/ajax/admin.ajax.php @@ -112,16 +112,37 @@ function version_check() */ function check_system_version() { + // Check if curl is available. + if (!in_array('curl', get_loaded_extensions())) { + return ''.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').''; + } + + $url = 'https://version.chamilo.org'; + $options = [ + 'verify' => false + ]; + + $client = new GuzzleHttp\Client(); + $res = $client->request('GET', $url, $options); + $urlValidated = false; + if ($res->getStatusCode() == '200' || $res->getStatusCode() == '301') { + $urlValidated = true; + } // the chamilo version of your installation $system_version = trim(api_get_configuration_value('system_version')); - if (ini_get('allow_url_fopen') == 1) { + if ($urlValidated) { // The number of courses $number_of_courses = Statistics::countCourses(); // The number of users $number_of_users = Statistics::countUsers(); - $number_of_active_users = Statistics::countUsers(null, null, null, true); + $number_of_active_users = Statistics::countUsers( + null, + null, + null, + true + ); // The number of sessions $number_of_sessions = Statistics::countSessions(); @@ -152,90 +173,31 @@ function check_system_version() // or in the installed config file. The default value is 'chamilo' 'packager' => $packager, ); + $version = null; - // version.php has been updated to include the version in an HTTP header - // called "X-Chamilo-Version", so that we don't have to worry about - // issues with the content not being returned by fread for some reason - $res = _http_request('version.chamilo.org', 80, '/version.php', $data, 5, null, true); - $lines = preg_split('/\r\n/', $res); - foreach ($lines as $line) { - $elements = preg_split('/:/', $line); - // extract the X-Chamilo-Version header from the version.php response - if (strcmp(trim($elements[0]), 'X-Chamilo-Version') === 0) { - $version = trim($elements[1]); - } + $client = new GuzzleHttp\Client(); + $url .= '?'; + foreach ($data as $k => $v) { + $url .= urlencode($k).'='.urlencode($v).'&'; } - if (substr($res, 0, 5) != 'Error') { - if (empty($version)) { - $version_info = $res; - } else { - $version_info = $version; + $res = $client->request('GET', $url, $options); + if ($res->getStatusCode() == '200') { + $versionData = $res->getHeader('X-Chamilo-Version'); + if (isset($versionData[0])) { + $version = trim($versionData[0]); } + } - if (version_compare($system_version, $version_info, '<')) { - $output = ''.get_lang('YourVersionNotUpToDate').'
- '.get_lang('LatestVersionIs').' Chamilo '.$version_info.'.
- '.get_lang('YourVersionIs').' Chamilo '.$system_version.'.
'.str_replace('http://www.chamilo.org', 'http://www.chamilo.org', get_lang('PleaseVisitOurWebsite')).'
'; - } else { - $output = ''.get_lang('VersionUpToDate').': Chamilo '.$version_info.''; - } + if (version_compare($system_version, $version, '<')) { + $output = ''.get_lang('YourVersionNotUpToDate').'
+ '.get_lang('LatestVersionIs').' Chamilo '.$version.'.
+ '.get_lang('YourVersionIs').' Chamilo '.$system_version.'.
'.str_replace('http://www.chamilo.org', 'http://www.chamilo.org', get_lang('PleaseVisitOurWebsite')).'
'; } else { - $output = ''.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').''; + $output = ''.get_lang('VersionUpToDate').': Chamilo '.$version.''; } - } else { - $output = ''.get_lang('AllowurlfopenIsSetToOff').''; - } - return $output; -} -/** - * Function to make an HTTP request through fsockopen (specialised for GET) - * Derived from Jeremy Saintot: http://www.php.net/manual/en/function.fsockopen.php#101872 - * @param string $ip IP or hostname - * @param int $port Target port - * @param string $uri URI (defaults to '/') - * @param array $getdata GET data - * @param int $timeout Timeout - * @param bool $req_hdr Include HTTP Request headers? - * @param bool $res_hdr Include HTTP Response headers? - * @return string - */ -function _http_request($ip, $port = 80, $uri = '/', $getdata = array(), $timeout = 5, $req_hdr = false, $res_hdr = false) -{ - $verb = 'GET'; - $ret = ''; - $getdata_str = count($getdata) ? '?' : ''; - - foreach ($getdata as $k => $v) { - $getdata_str .= urlencode($k).'='.urlencode($v).'&'; + return $output; } - $crlf = "\r\n"; - $req = $verb.' '.$uri.$getdata_str.' HTTP/1.1'.$crlf; - $req .= 'Host: '.$ip.$crlf; - $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12'.$crlf; - $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'.$crlf; - $req .= 'Accept-Language: en-us,en;q=0.5'.$crlf; - $req .= 'Accept-Encoding: deflate'.$crlf; - $req .= 'Accept-Charset: utf-8;q=0.7,*;q=0.7'.$crlf; - - $req .= $crlf; - - if ($req_hdr) { - $ret .= $req; - } - if (($fp = @fsockopen($ip, $port, $errno, $errstr, $timeout)) == false) { - return "Error $errno: $errstr\n"; - } - - stream_set_timeout($fp, $timeout); - $r = fwrite($fp, $req); - $line = @fread($fp, 512); - $ret .= $line; - fclose($fp); - - if (!$res_hdr) { - $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4); - } - return trim($ret); + return ''.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').''; } diff --git a/main/inc/ajax/link.ajax.php b/main/inc/ajax/link.ajax.php index 4f26b97794..a5cd71fd12 100755 --- a/main/inc/ajax/link.ajax.php +++ b/main/inc/ajax/link.ajax.php @@ -15,41 +15,7 @@ switch ($action) { case 'check_url': if (api_is_allowed_to_edit(null, true)) { $url = $_REQUEST['url']; - // Check if curl is available. - if (!in_array('curl', get_loaded_extensions())) { - echo ''; - exit; - } - - // set URL and other appropriate options - $defaults = array( - CURLOPT_URL => $url, - CURLOPT_FOLLOWLOCATION => true, // follow redirects accept youtube.com - CURLOPT_HEADER => 0, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_TIMEOUT => 4 - ); - - $proxySettings = api_get_configuration_value('proxy_settings'); - - if (!empty($proxySettings) && - isset($proxySettings['curl_setopt_array']) - ) { - $defaults[CURLOPT_PROXY] = $proxySettings['curl_setopt_array']['CURLOPT_PROXY']; - $defaults[CURLOPT_PROXYPORT] = $proxySettings['curl_setopt_array']['CURLOPT_PROXYPORT']; - } - - // Create a new cURL resource - $ch = curl_init(); - curl_setopt_array($ch, $defaults); - - // grab URL and pass it to the browser - ob_start(); - $result = curl_exec($ch); - ob_get_clean(); - - // close cURL resource, and free up system resources - curl_close($ch); + $result = \Link::checkUrl($url); if ($result) { echo Display::return_icon( diff --git a/main/inc/lib/link.lib.php b/main/inc/lib/link.lib.php index 732cc9a2c2..e2e25c2c3b 100755 --- a/main/inc/lib/link.lib.php +++ b/main/inc/lib/link.lib.php @@ -1945,4 +1945,48 @@ class Link extends Model { return self::moveLinkDisplayOrder($id, 'DESC'); } + + /** + * @param string $url + * @return bool + */ + public static function checkUrl($url) + { + // Check if curl is available. + if (!in_array('curl', get_loaded_extensions())) { + return false; + } + + // set URL and other appropriate options + $defaults = array( + CURLOPT_URL => $url, + CURLOPT_FOLLOWLOCATION => true, // follow redirects accept youtube.com + CURLOPT_HEADER => 0, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 4 + ); + + $proxySettings = api_get_configuration_value('proxy_settings'); + + if (!empty($proxySettings) && + isset($proxySettings['curl_setopt_array']) + ) { + $defaults[CURLOPT_PROXY] = $proxySettings['curl_setopt_array']['CURLOPT_PROXY']; + $defaults[CURLOPT_PROXYPORT] = $proxySettings['curl_setopt_array']['CURLOPT_PROXYPORT']; + } + + // Create a new cURL resource + $ch = curl_init(); + curl_setopt_array($ch, $defaults); + + // grab URL and pass it to the browser + ob_start(); + $result = curl_exec($ch); + ob_get_clean(); + + // close cURL resource, and free up system resources + curl_close($ch); + + return $result; + } }