Use GuzzleHttp\Client instead of custom code see BT#13047

pull/2487/head
jmontoyaa 8 years ago
parent bb0daf34a2
commit 1bd8c02102
  1. 20
      main/admin/index.php
  2. 120
      main/inc/ajax/admin.ajax.php
  3. 36
      main/inc/ajax/link.ajax.php
  4. 44
      main/inc/lib/link.lib.php

@ -481,7 +481,13 @@ if (api_is_platform_admin()) {
/* Chamilo.org */ /* 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']['label'] = 'Chamilo.org';
$blocks['chamilo']['class'] = 'block-admin-chamilo'; $blocks['chamilo']['class'] = 'block-admin-chamilo';
@ -505,12 +511,18 @@ if (api_is_platform_admin()) {
$blocks['chamilo']['search_form'] = null; $blocks['chamilo']['search_form'] = null;
// Version check // 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']['label'] = get_lang('VersionCheck');
$blocks['version_check']['extra'] = '<div class="admin-block-version"></div>'; $blocks['version_check']['extra'] = '<div class="admin-block-version"></div>';
$blocks['version_check']['search_form'] = null; $blocks['version_check']['search_form'] = null;
$blocks['version_check']['items'] = null; $blocks['version_check']['items'] = '<div class="block-admin-version_check"></div>';
$blocks['version_check']['class'] = 'block-admin-version_check'; $blocks['version_check']['class'] = '';
// Check Hook Event for Admin Block Object // Check Hook Event for Admin Block Object
if (!empty($hook)) { if (!empty($hook)) {

@ -112,16 +112,37 @@ function version_check()
*/ */
function check_system_version() function check_system_version()
{ {
// Check if curl is available.
if (!in_array('curl', get_loaded_extensions())) {
return '<span style="color:red">'.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').'</span>';
}
$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 // the chamilo version of your installation
$system_version = trim(api_get_configuration_value('system_version')); $system_version = trim(api_get_configuration_value('system_version'));
if (ini_get('allow_url_fopen') == 1) { if ($urlValidated) {
// The number of courses // The number of courses
$number_of_courses = Statistics::countCourses(); $number_of_courses = Statistics::countCourses();
// The number of users // The number of users
$number_of_users = Statistics::countUsers(); $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 // The number of sessions
$number_of_sessions = Statistics::countSessions(); $number_of_sessions = Statistics::countSessions();
@ -152,90 +173,31 @@ function check_system_version()
// or in the installed config file. The default value is 'chamilo' // or in the installed config file. The default value is 'chamilo'
'packager' => $packager, 'packager' => $packager,
); );
$version = null; $version = null;
// version.php has been updated to include the version in an HTTP header $client = new GuzzleHttp\Client();
// called "X-Chamilo-Version", so that we don't have to worry about $url .= '?';
// issues with the content not being returned by fread for some reason foreach ($data as $k => $v) {
$res = _http_request('version.chamilo.org', 80, '/version.php', $data, 5, null, true); $url .= urlencode($k).'='.urlencode($v).'&';
$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]);
}
} }
if (substr($res, 0, 5) != 'Error') { $res = $client->request('GET', $url, $options);
if (empty($version)) { if ($res->getStatusCode() == '200') {
$version_info = $res; $versionData = $res->getHeader('X-Chamilo-Version');
} else { if (isset($versionData[0])) {
$version_info = $version; $version = trim($versionData[0]);
} }
}
if (version_compare($system_version, $version_info, '<')) { if (version_compare($system_version, $version, '<')) {
$output = '<span style="color:red">'.get_lang('YourVersionNotUpToDate').'<br /> $output = '<span style="color:red">'.get_lang('YourVersionNotUpToDate').'<br />
'.get_lang('LatestVersionIs').' <b>Chamilo '.$version_info.'</b>. <br /> '.get_lang('LatestVersionIs').' <b>Chamilo '.$version.'</b>. <br />
'.get_lang('YourVersionIs').' <b>Chamilo '.$system_version.'</b>. <br />'.str_replace('http://www.chamilo.org', '<a href="http://www.chamilo.org">http://www.chamilo.org</a>', get_lang('PleaseVisitOurWebsite')).'</span>'; '.get_lang('YourVersionIs').' <b>Chamilo '.$system_version.'</b>. <br />'.str_replace('http://www.chamilo.org', '<a href="http://www.chamilo.org">http://www.chamilo.org</a>', get_lang('PleaseVisitOurWebsite')).'</span>';
} else {
$output = '<span style="color:green">'.get_lang('VersionUpToDate').': Chamilo '.$version_info.'</span>';
}
} else { } else {
$output = '<span style="color:red">'.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').'</span>'; $output = '<span style="color:green">'.get_lang('VersionUpToDate').': Chamilo '.$version.'</span>';
} }
} else {
$output = '<span style="color:red">'.get_lang('AllowurlfopenIsSetToOff').'</span>';
}
return $output;
}
/** 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).'&';
} }
$crlf = "\r\n"; return '<span style="color:red">'.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').'</span>';
$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);
} }

@ -15,41 +15,7 @@ switch ($action) {
case 'check_url': case 'check_url':
if (api_is_allowed_to_edit(null, true)) { if (api_is_allowed_to_edit(null, true)) {
$url = $_REQUEST['url']; $url = $_REQUEST['url'];
// Check if curl is available. $result = \Link::checkUrl($url);
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);
if ($result) { if ($result) {
echo Display::return_icon( echo Display::return_icon(

@ -1945,4 +1945,48 @@ class Link extends Model
{ {
return self::moveLinkDisplayOrder($id, 'DESC'); 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;
}
} }

Loading…
Cancel
Save