Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-38509 Unify curl class usage in the add-on installer tool

Both classes using cURL features now access it via the core curl wrapper
class. Credit goes to Dan Poltawski for spotting the previous discrepancy
during the integration review.
  • Loading branch information...
commit 3e6a8aeb7ca46e26fca7fae9ff3df3765997baad 1 parent c9c4d0b
@mudrd8mz mudrd8mz authored
View
83 admin/tool/installaddon/classes/installer.php
@@ -349,80 +349,49 @@ public function handle_remote_request(tool_installaddon_renderer $output, $reque
*/
public function download_file($source, $target) {
global $CFG;
+ require_once($CFG->libdir.'/filelib.php');
- $newlines = array("\r", "\n");
- $source = str_replace($newlines, '', $source);
- if (!preg_match('|^https?://|i', $source)) {
- throw new tool_installaddon_installer_exception('err_download_transport_protocol', $source);
- }
- if (!$ch = curl_init($source)) {
- throw new tool_installaddon_installer_exception('err_curl_init', $source);
+ $targetfile = fopen($target, 'w');
+
+ if (!$targetfile) {
+ throw new tool_installaddon_installer_exception('err_download_write_file', $target);
}
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // verify the peer's certificate
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // check the existence of a common name and also verify that it matches the hostname provided
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the transfer as a string
- curl_setopt($ch, CURLOPT_HEADER, false); // don't include the header in the output
- curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); // nah, moodle.org is never unavailable! :-p
- curl_setopt($ch, CURLOPT_URL, $source);
+ $options = array(
+ 'file' => $targetfile,
+ 'timeout' => 300,
+ 'followlocation' => true,
+ 'maxredirs' => 3,
+ 'ssl_verifypeer' => true,
+ 'ssl_verifyhost' => 2,
+ );
$cacertfile = $CFG->dataroot.'/moodleorgca.crt';
if (is_readable($cacertfile)) {
// Do not use CA certs provided by the operating system. Instead,
// use this CA cert to verify the ZIP provider.
- curl_setopt($ch, CURLOPT_CAINFO, $cacertfile);
+ $options['cainfo'] = $cacertfile;
}
- if (!empty($CFG->proxyhost) and !is_proxybypass($source)) {
- if (!empty($CFG->proxytype)) {
- if (strtoupper($CFG->proxytype) === 'SOCKS5') {
- curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
- } else {
- curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
- curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
- }
- }
+ $curl = new curl(array('proxy' => true));
- if (empty($CFG->proxyport)) {
- curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost);
- } else {
- curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost.':'.$CFG->proxyport);
- }
+ $result = $curl->download_one($source, null, $options);
- if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
- curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
- curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
- }
- }
-
- $targetfile = fopen($target, 'w');
-
- if (!$targetfile) {
- throw new tool_installaddon_installer_exception('err_download_write_file', $target);
- }
-
- curl_setopt($ch, CURLOPT_FILE, $targetfile);
-
- $result = curl_exec($ch);
-
- // try to detect encoding problems
- if ((curl_errno($ch) == 23 or curl_errno($ch) == 61) and defined('CURLOPT_ENCODING')) {
- curl_setopt($ch, CURLOPT_ENCODING, 'none');
- $result = curl_exec($ch);
- }
+ $curlinfo = $curl->get_info();
fclose($targetfile);
- $curlerrno = curl_errno($ch);
- $curlerror = curl_error($ch);
- $curlinfo = curl_getinfo($ch);
+ if ($result !== true) {
+ throw new tool_installaddon_installer_exception('err_curl_exec', array(
+ 'url' => $source, 'errorno' => $curl->get_errno(), 'error' => $result));
- if ($result === false or $curlerrno) {
- throw new tool_installaddon_installer_exception('err_curl_exec', array('url' => $source, 'errorno' => $curlerrno, 'error' => $curlerror));
+ } else if (empty($curlinfo['http_code']) or $curlinfo['http_code'] != 200) {
+ throw new tool_installaddon_installer_exception('err_curl_http_code', array(
+ 'url' => $source, 'http_code' => $curlinfo['http_code']));
- } else if (is_array($curlinfo) and (empty($curlinfo['http_code']) or $curlinfo['http_code'] != 200)) {
- throw new tool_installaddon_installer_exception('err_curl_code', array('url' => $source, 'httpcode' => $curlinfo['http_code']));
+ } else if (isset($curlinfo['ssl_verify_result']) and $curlinfo['ssl_verify_result'] != 0) {
+ throw new tool_installaddon_installer_exception('err_curl_ssl_verify', array(
+ 'url' => $source, 'ssl_verify_result' => $curlinfo['ssl_verify_result']));
}
}
View
23 admin/tool/installaddon/classes/pluginfo_client.php
@@ -80,21 +80,28 @@ protected function call_service($component, $version) {
require_once($CFG->libdir.'/filelib.php');
$curl = new curl(array('proxy' => true));
+
$response = $curl->get(
$this->service_request_url(),
$this->service_request_params($component, $version),
$this->service_request_options());
+
$curlerrno = $curl->get_errno();
- if (!empty($curlerrno)) {
- throw new tool_installaddon_pluginfo_exception('err_response_curl', 'cURL error '.$curlerrno.': '.$curl->error);
- }
$curlinfo = $curl->get_info();
-/* if ($curlinfo['http_code'] == 404) {
- throw new tool_installaddon_pluginfo_exception('err_response_http_code_404');
-}*/
- if ($curlinfo['http_code'] != 200) {
- throw new tool_installaddon_pluginfo_exception('err_response_http_code', $curlinfo['http_code']);
+
+ if (!empty($curlerrno)) {
+ throw new tool_installaddon_pluginfo_exception('err_curl_exec', array(
+ 'url' => $curlinfo['url'], 'errno' => $curlerrno, 'error' => $curl->error));
+
+ } else if ($curlinfo['http_code'] != 200) {
+ throw new tool_installaddon_pluginfo_exception('err_curl_http_code', array(
+ 'url' => $curlinfo['url'], 'http_code' => $curlinfo['http_code']));
+
+ } else if (isset($curlinfo['ssl_verify_result']) and $curlinfo['ssl_verify_result'] != 0) {
+ throw new tool_installaddon_pluginfo_exception('err_curl_ssl_verify', array(
+ 'url' => $curlinfo['url'], 'ssl_verify_result' => $curlinfo['ssl_verify_result']));
}
+
return $response;
}
Please sign in to comment.
Something went wrong with that request. Please try again.