Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #10: Added proxy settings for Curl and soap requests #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions classes/rest/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,29 @@ public function rest_call($verb, $resourcepath, requestoptions $requestoptions,
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
// Add moodle proxy curlopts.
if (!empty($CFG->proxyhost)) {
if (empty($CFG->proxyport)) {
$proxyhost = $CFG->proxyhost;
} else {
$proxyhost = $CFG->proxyhost.':'.$CFG->proxyport;
}
if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
$proxyauth = $CFG->proxyuser.':'.$CFG->proxypassword;
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
}
if (!empty($CFG->proxytype)) {
if ($CFG->proxytype == 'SOCKS5') {
$proxytype = CURLPROXY_SOCKS5;
} else {
$proxytype = CURLPROXY_HTTP;
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
}
curl_setopt($ch, CURLOPT_PROXYTYPE, $proxytype);
}
curl_setopt($ch, CURLOPT_PROXY, $proxyhost);
}
$jsonstr = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->logger->info('response', ['httpcode' => $httpcode]);
Expand Down
37 changes: 37 additions & 0 deletions classes/soap/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class api extends generated\SASDefaultAdapter implements api_session, api_attend
* @param stdClass $config - custom config passed in on construct.
*/
public function __construct(array $options = array(), $wsdl = null, stdClass $config = null) {
global $CFG;

$this->setup($config);

Expand All @@ -94,6 +95,18 @@ public function __construct(array $options = array(), $wsdl = null, stdClass $co
$options['trace'] = 1;
}

// Add moodle proxy soap options.
if (!empty($CFG->proxyhost)) {
$options['proxy_host'] = $CFG->proxyhost;
if (!empty($CFG->proxyport)) {
$options['proxy_port'] = $CFG->proxyport;
}
if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
$options['proxy_login'] = $CFG->proxyuser;
$options['proxy_password'] = $CFG->proxypassword;
}
}

$serviceok = $this->test_service_reachable($options['location']);
if (!$serviceok) {
$this->usable = false;
Expand Down Expand Up @@ -161,6 +174,7 @@ public function is_usable() {
* @return bool
*/
protected function test_service_reachable($serviceuri) {
global $CFG;
$ch = curl_init();
$this->logger->info('Testing service availability: '.$serviceuri);
curl_setopt($ch, CURLOPT_URL, $serviceuri);
Expand All @@ -169,6 +183,29 @@ protected function test_service_reachable($serviceuri) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Add moodle proxy curlopts.
if (!empty($CFG->proxyhost)) {
if (empty($CFG->proxyport)) {
$proxyhost = $CFG->proxyhost;
} else {
$proxyhost = $CFG->proxyhost.':'.$CFG->proxyport;
}
if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
$proxyauth = $CFG->proxyuser.':'.$CFG->proxypassword;
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
}
if (!empty($CFG->proxytype)) {
if ($CFG->proxytype == 'SOCKS5') {
$proxytype = CURLPROXY_SOCKS5;
} else {
$proxytype = CURLPROXY_HTTP;
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
}
curl_setopt($ch, CURLOPT_PROXYTYPE, $proxytype);
}
curl_setopt($ch, CURLOPT_PROXY, $proxyhost);
}
$body = curl_exec($ch);
if (stripos($body, '<soap:Envelope') === false) {
// Body does not contain expected text, service is bad.
Expand Down