Skip to content

Commit

Permalink
MDL-11845 removing curl emulation library, snoopy has better support …
Browse files Browse the repository at this point in the history
…for proxies and seems to be more reliable on different platforms; spelling correction cURL instead of Curl; adding all proxy admin settings; better debug messages for file downloading; SOCKS5 proxy support; merged from MOODLE_19_STABLE
  • Loading branch information
skodak committed Dec 30, 2007
1 parent e33fbf8 commit 5f8bdc1
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 2,741 deletions.
8 changes: 8 additions & 0 deletions admin/settings/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,16 @@
$temp = new admin_settingpage('http', get_string('http', 'admin'));
$temp->add(new admin_setting_configtext('framename', get_string('framename', 'admin'), get_string('configframename', 'admin'), '_top', PARAM_ALPHAEXT));
$temp->add(new admin_Setting_configcheckbox('slasharguments', get_string('slasharguments', 'admin'), get_string('configslasharguments', 'admin'), 1));
$temp->add(new admin_setting_heading('webproxy', get_string('webproxy', 'admin'), get_string('webproxyinfo', 'admin')));
$temp->add(new admin_setting_configtext('proxyhost', get_string('proxyhost', 'admin'), get_string('configproxyhost', 'admin'), '', PARAM_HOST));
$temp->add(new admin_setting_configtext('proxyport', get_string('proxyport', 'admin'), get_string('configproxyport', 'admin'), 0, PARAM_INT));
$options = array('HTTP'=>'HTTP');
if (defined('CURLPROXY_SOCKS5')) {
$options['SOCKS5'] = 'SOCKS5';
}
$temp->add(new admin_setting_configselect('proxytype', get_string('proxytype', 'admin'), get_string('configproxytype','admin'), 'HTTP', $options));
$temp->add(new admin_setting_configtext('proxyuser', get_string('proxyuser', 'admin'), get_string('configproxyuser', 'admin'), ''));
$temp->add(new admin_setting_configtext('proxypassword', get_string('proxypassword', 'admin'), get_string('configproxypassword', 'admin'), ''));
$ADMIN->add('server', $temp);

$ADMIN->add('server', new admin_externalpage('maintenancemode', get_string('sitemaintenancemode', 'admin'), "$CFG->wwwroot/$CFG->admin/maintenance.php"));
Expand Down
12 changes: 10 additions & 2 deletions lang/en_utf8/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,11 @@
$string['configpathtodu'] = 'Path to du. Probably something like /usr/bin/du. If you enter this, pages that display directory contents will run much faster for directories with a lot of files.';
$string['configperfdebug'] = 'If you turn this on, performance info will be printed in the footer of the standard theme';
$string['configprotectusernames'] = 'By default forget_password.php does not display any hints that would allow guessing of usernames or email addresses.';
$string['configproxyhost'] = 'If this <b>server</b> needs to use a proxy computer (eg a firewall) to access the Internet, then provide the proxy hostname and port here. Otherwise leave it blank.';
$string['configproxyhost'] = 'If this <b>server</b> needs to use a proxy computer (eg a firewall) to access the Internet, then provide the proxy hostname here. Otherwise leave it blank.';
$string['configproxypassword'] = 'Password needed to access internet through proxy if required, empty if none (PHP cURL extension required).';
$string['configproxyport'] = 'If this server needs to use a proxy computer, then provide the proxy port here.';
$string['configproxyuser'] = 'Username needed to access internet through proxy if required, empty if none (PHP cURL extension required).';
$string['configproxytype'] = 'Type of web proxy (PHP5 and cURL extension required for SOCKS5 support).';
$string['configquarantinedir'] = 'If you want clam AV to move infected files to a quarantine directory, enter it here. It must be writable by the webserver. If you leave this blank, or if you enter a directory that doesn\'t exist or isn\'t writable, infected files will be deleted. Do not include a trailing slash.';
$string['configcronclionly'] = 'If this is set, then the cron script can only be run from the commandline instead of via the web. This overrides the cron password setting below.';
$string['configcronremotepassword'] = 'This means that the cron.php script cannot be run from a web browser without supplying the password using the following form of URL:<pre>
Expand Down Expand Up @@ -244,7 +247,7 @@
$string['cronremotepassword'] = 'Cron password for remote access';
$string['cronwarning'] = 'The <a href=\"cron.php\">cron.php maintenance script</a> has not been run for at least 24 hours.';
$string['csvdelimiter'] = 'CSV delimiter';
$string['curlrecommended'] = 'Installing the optional Curl library is highly recommended in order to enable Moodle Networking functionality.';
$string['curlrecommended'] = 'Installing the optional cURL library is highly recommended in order to enable Moodle Networking functionality.';
$string['customcheck'] = 'Other Checks';
$string['datarootsecuritywarning'] = 'Your site configuration might not be secure. Please make sure that your dataroot directory ($a) is not directly accessible via web.';
$string['dbmigrate'] = 'Moodle Database Migration';
Expand Down Expand Up @@ -551,6 +554,9 @@
$string['protectusernames'] = 'Protect usernames';
$string['proxyhost'] = 'Proxy host';
$string['proxyport'] = 'Proxy port';
$string['proxypassword'] = 'Proxy password';
$string['proxyuser'] = 'Proxy username';
$string['proxytype'] = 'Proxy type';
$string['qtyperqpwillberemoved'] = 'During the upgrade, the RQP question type will be removed. You were not using this question type, so you should not experience any problems.';
$string['qtyperqpwillberemovedanyway'] = 'During the upgrade, the RQP question type will be removed. You have some RQP questions in your database, and these will stop working unless you reinstall the code from http://moodle.org/mod/data/view.php?d=13&amp;rid=797 before continuing with the upgrade.';
$string['quarantinedir'] = 'Quarantine directory';
Expand Down Expand Up @@ -701,6 +707,8 @@
$string['uuupdatetype'] = 'Existing user details';
$string['validateerror'] = 'This value was not valid:';
$string['warningcurrentsetting'] = 'Invalid current value: $a';
$string['webproxy'] = 'Web proxy';
$string['webproxyinfo'] = 'Fill in following options if your Moodle server can not access internet directly. Internet access is required for download of environment data, language packs, RSS feeds, timezones, etc.<br /><em>PHP cURL extension is highly recommended.</em>';
$string['xmlstrictheaders'] = 'XML strict headers';

?>
2 changes: 1 addition & 1 deletion lang/en_utf8/mnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
$string['postrequired'] = 'The delete function requires a POST request.';
$string['hostdeleted'] = 'Ok - host deleted';
$string['reenableserver'] = 'No - select this option to re-enable this server.';
$string['nocurl'] = 'PHP Curl library is not installed';
$string['nocurl'] = 'PHP cURL library is not installed';
$string['publish'] = 'Publish';
$string['subscribe'] = 'Subscribe';
$string['failedaclwrite'] = 'Failed to write to the MNET access control list for user \'$a\'.';
Expand Down
103 changes: 95 additions & 8 deletions lib/filelib.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
<?php //$Id$

require_once($CFG->libdir.'/libcurlemu/libcurlemu.inc.php'); // might be moved to setup.php later

define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7'); //unique string constant

/**
* Fetches content of file from Internet (using proxy if defined).
*
* Fetches content of file from Internet (using proxy if defined). Uses cURL extension if present.
* @param string $url file url
* @return mixed false if request failed or content of the file as string if ok.
*/
function download_file_content($url) {
global $CFG;

if (!extension_loaded('curl')) {
require_once($CFG->libdir.'/snoopy/Snoopy.class.inc');
$snoopy = new Snoopy();
$snoopy->proxy_host = $CFG->proxyhost;
$snoopy->proxy_port = $CFG->proxyport;
if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
// this will probably fail, but let's try it anyway
$snoopy->proxy_user = $CFG->proxyuser;
$snoopy->proxy_password = $CFG->proxypassword;
}
if ($snoopy->fetch($url)) {
if (strpos($snoopy->response_code, '200') === false) {
debugging("Snoopy request for \"$url\" failed, http response code: ".$snoopy->response_code, DEBUG_ALL);
return false;
} else {
return $snoopy->results;
}
} else {
debugging("Snoopy request for \"$url\" failed with: ".$snoopy->error, DEBUG_ALL);
return false;
}
}

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
if (!empty($CFG->proxyhost)) {
// SOCKS supported in PHP5 only
if (!empty($CFG->proxytype) and ($CFG->proxytype == 'SOCKS5')) {
if (defined('CURLPROXY_SOCKS5')) {
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
} else {
debugging("SOCKS5 proxies are supported only in PHP5 when cURL extension loaded.", DEBUG_ALL);
curl_close($ch);
return false;
}
}

// don't CONNECT for non-https connections
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);

Expand All @@ -24,15 +56,70 @@ function download_file_content($url) {
} else {
curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost.':'.$CFG->proxyport);
}
if(!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {

if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
if (defined('CURLOPT_PROXYAUTH')) {
// any proxy authentication if PHP 5.1
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
}
}
}
$result = curl_exec($ch);

if (curl_errno($ch)) {
$curlerror = "CURL request for \"$url\" failed with: ". curl_error($ch);
debugging($curlerror, DEBUG_DEVELOPER);
debugging("CURL request for \"$url\" failed with: ".curl_error($ch), DEBUG_ALL);
$result = false;

} else {
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 200) {
$codes = array(
100=>'Continue',
101=>'Switching Protocols',
200=>'OK',
201=>'Created',
202=>'Accepted',
203=>'Non-Authoritative Information',
204=>'No Content',
205=>'Reset Content',
206=>'Partial Content',
300=>'Multiple Choices',
301=>'Moved Permanently',
302=>'Found',
303=>'See Other',
304=>'Not Modified',
305=>'Use Proxy',
306=>'(Unused)',
307=>'Temporary Redirect',
400=>'Bad Request',
401=>'Unauthorized',
402=>'Payment Required',
403=>'Forbidden',
404=>'Not Found',
405=>'Method Not Allowed',
406=>'Not Acceptable',
407=>'Proxy Authentication Required',
408=>'Request Timeout',
409=>'Conflict',
410=>'Gone',
411=>'Length Required',
412=>'Precondition Failed',
413=>'Request Entity Too Large',
414=>'Request-URI Too Long',
415=>'Unsupported Media Type',
416=>'Requested Range Not Satisfiable',
417=>'Expectation Failed',
500=>'Internal Server Error',
501=>'Not Implemented',
502=>'Bad Gateway',
503=>'Service Unavailable',
504=>'Gateway Timeout',
505=>'HTTP Version Not Supported');

debugging("CURL request for \"$url\" failed, http response code: ".$code.' '.$codes[$code], DEBUG_ALL);
$result = false;
}
}

curl_close($ch);
Expand Down Expand Up @@ -347,7 +434,7 @@ function send_file($path, $filename, $lifetime=86400 , $filter=0, $pathisstring=

// if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup
if (check_browser_version('MSIE')) {
$filename = urlencode($filename);
$filename = urlencode($filename);
}

if ($forcedownload) {
Expand Down
Loading

0 comments on commit 5f8bdc1

Please sign in to comment.