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

Added proxy options for PSR-7 Service #16539

Merged
merged 2 commits into from
May 20, 2024
Merged
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
29 changes: 29 additions & 0 deletions core/src/Revolution/Processors/System/Dashboard/Widget/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function loadFeed($url)
$this->modx->cacheManager->writeTree($cachePath);
}

$proxy_config = $this->buildProxyOptions();
if (!empty($proxy_config)) {
$feed->set_curl_options($proxy_config);
}

$feed->set_cache_location($cachePath);
$feed->set_useragent($this->modx->getVersionData()['full_version']);
$feed->set_feed_url($url);
Expand Down Expand Up @@ -115,4 +120,28 @@ public function getFileChunk($tpl, array $placeholders = [])

return $output;
}

/**
* Build configuration for the SimplePie client based on configuration settings.
*
* @return array The proxy configuration.
*/
private function buildProxyOptions()
{
$config = [];
$proxyHost = $this->modx->getOption('proxy_host', null, '');
if (!empty($proxyHost)) {
$config['CURLOPT_PROXY'] = $proxyHost;
$proxyPort = $this->modx->getOption('proxy_port', null, '');
if (!empty($proxyPort)) {
$config['CURLOPT_PROXY'] .= ':' . $proxyPort;
}
$proxyUsername = $this->modx->getOption('proxy_username', null, '');
if (!empty($proxyUsername)) {
$proxyPassword = $this->modx->getOption('proxy_password', null, '');
$config['CURLOPT_PROXYUSERPWD'] = $proxyUsername . ':' . $proxyPassword;
}
}
return $config;
}
}
27 changes: 26 additions & 1 deletion core/src/Revolution/modX.php
Original file line number Diff line number Diff line change
Expand Up @@ -2700,7 +2700,8 @@ protected function _initHttpClient()
// Http Client is created as a factory, so that repeat calls get a fresh client. This is done to make sure
// mutable clients (perhaps they allow setting options after instantiation) do not cause side-effects elsewhere
$this->services->add(ClientInterface::class, $this->services->factory(function() {
return new Client();
$opts = $this->buildHttpClientOptions();
return new Client($opts);
}));
}
if (!$this->services->has(ServerRequestFactoryInterface::class)) {
Expand All @@ -2720,6 +2721,30 @@ protected function _initHttpClient()
}
}

/**
* Build options for the HTTP client based on configuration settings.
*
* @return array The HTTP client options.
*/
private function buildHttpClientOptions() {
$opts = [];
$proxyHost = $this->getOption('proxy_host', null, '');
if (!empty($proxyHost)) {
$proxy_str = $proxyHost;
$proxyPort = $this->getOption('proxy_port', null, '');
if (!empty($proxyPort)) {
$proxy_str .= ':' . $proxyPort;
}
$proxyUsername = $this->getOption('proxy_username', null, '');
if (!empty($proxyUsername)) {
$proxyPassword = $this->getOption('proxy_password', null, '');
$proxy_str = $proxyUsername . ':' . $proxyPassword . '@' . $proxy_str;
}
$opts['proxy'] = $proxy_str;
}
return $opts;
}

/**
* Populates the map of events and registered plugins for each.
*
Expand Down
Loading