· 7 years ago · Mar 13, 2018, 09:44 AM
1diff --git a/src/Plugin/Network/TwitterAuth.php b/src/Plugin/Network/TwitterAuth.php
2index 11d2141..d0e7398 100644
3--- a/src/Plugin/Network/TwitterAuth.php
4+++ b/src/Plugin/Network/TwitterAuth.php
5@@ -9,6 +9,7 @@ use Drupal\social_api\SocialApiException;
6 use Drupal\social_auth\Plugin\Network\SocialAuthNetwork;
7 use Abraham\TwitterOAuth\TwitterOAuth;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9+use Drupal\Core\Site\Settings;
10
11 /**
12 * Defines Social Auth Twitter Network Plugin.
13@@ -43,7 +44,8 @@ class TwitterAuth extends SocialAuthNetwork implements TwitterAuthInterface {
14 $plugin_id,
15 $plugin_definition,
16 $container->get('entity_type.manager'),
17- $container->get('config.factory')
18+ $container->get('config.factory'),
19+ $container->get('settings')
20 );
21 }
22
23@@ -62,11 +64,21 @@ class TwitterAuth extends SocialAuthNetwork implements TwitterAuthInterface {
24 * The entity type manager.
25 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
26 * The configuration factory.
27+ * @param \Drupal\Core\Site\Settings $settings
28+ * The settings factory.
29 */
30- public function __construct(MetadataBubblingUrlGenerator $url_generator, array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
31+ public function __construct(MetadataBubblingUrlGenerator $url_generator,
32+ array $configuration,
33+ $plugin_id,
34+ $plugin_definition,
35+ EntityTypeManagerInterface $entity_type_manager,
36+ ConfigFactoryInterface $config_factory,
37+ Settings $settings
38+ ) {
39 parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $config_factory);
40
41 $this->urlGenerator = $url_generator;
42+ $this->siteSettings = $settings;
43 }
44
45 /**
46@@ -84,6 +96,10 @@ class TwitterAuth extends SocialAuthNetwork implements TwitterAuthInterface {
47 // Creates a and sets data to TwitterOAuth object.
48 $client = new TwitterOAuth($settings->getConsumerKey(), $settings->getConsumerSecret());
49
50+ if ($proxy = $this->getProxy()) {
51+ $client->setProxy($proxy);
52+ }
53+
54 return $client;
55 }
56
57@@ -101,8 +117,37 @@ class TwitterAuth extends SocialAuthNetwork implements TwitterAuthInterface {
58 /* @var \Drupal\social_auth_twitter\Settings\TwitterAuthSettings $settings */
59 $settings = $this->settings;
60
61- return new TwitterOAuth($settings->getConsumerKey(), $settings->getConsumerSecret(),
62+ $client = new TwitterOAuth($settings->getConsumerKey(), $settings->getConsumerSecret(),
63 $oauth_token, $oauth_token_secret);
64+
65+ if ($proxy = $this->getProxy()) {
66+ $client->setProxy($proxy);
67+ }
68+
69+ return $client;
70+ }
71+
72+ /**
73+ * Parse proxy settings.
74+ *
75+ * @return array
76+ * The proxy settings or NULL if not set.
77+ */
78+ private function getProxy() {
79+ $proxy = NULL;
80+ $proxyUrl = $this->siteSettings->get('http_client_config')['proxy']['https'] ?? NULL;
81+ if ($proxyUrl) {
82+ $proxy_settings = parse_url($proxyUrl);
83+
84+ if ($proxy_settings) {
85+ $proxy = [
86+ 'CURLOPT_PROXY' => $proxy_settings['host'],
87+ 'CURLOPT_PROXYUSERPWD' => "{$proxy_settings['user']}:{$proxy_settings['pass']}",
88+ 'CURLOPT_PROXYPORT' => $proxy_settings['port'],
89+ ];
90+ }
91+ }
92+ return $proxy;
93 }
94
95 }