From bef638fe11a9f87dd9f5b4e56cab9bd36b7c86f7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Noack?= <noack@data-quest.de>
Date: Thu, 3 Feb 2022 10:48:42 +0000
Subject: [PATCH] Resolve "TwilloConnector ignoriert Proxy Einstellungen"

---
 lib/classes/TwilloConnector.php               | 34 ++++++++++++++-----
 .../edu-sharing-auth-helper.php               |  6 ++++
 .../edu-sharing-helper-base.php               |  7 ++--
 .../edu-sharing-node-helper.php               | 11 +++++-
 4 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/lib/classes/TwilloConnector.php b/lib/classes/TwilloConnector.php
index e91d912e912..7a57583b131 100644
--- a/lib/classes/TwilloConnector.php
+++ b/lib/classes/TwilloConnector.php
@@ -11,6 +11,13 @@ class TwilloConnector
      */
     static protected $ticket = null;
 
+    static public $twillo_base_url = 'https://www.twillo.de/edu-sharing';
+
+    public static function getHttpProxy()
+    {
+        $stream_context_options = stream_context_get_options(get_default_http_stream_context(self::$twillo_base_url));
+        return isset($stream_context_options['http']['proxy']) ? Config::get()->HTTP_PROXY : '';
+    }
     /**
      * Returns the DFN-AAI-ID for the given user. This ID must be the content of a datafield with the
      * datafield_id in the global config OERCAMPUS_TWILLO_DFNAAIID_DATAFIELD. If either this config or
@@ -44,9 +51,11 @@ class TwilloConnector
     {
         $user_id || $user_id = User::findCurrent()->id;
         $base = new EduSharingHelperBase(
-            'https://www.twillo.de/edu-sharing',
+            self::$twillo_base_url,
             file_get_contents($GLOBALS['STUDIP_BASE_PATH']."/config/twillo-private.key"),
-            Config::get()->OERCAMPUS_TWILLO_APPID // 'data-quest-Test'
+            Config::get()->OERCAMPUS_TWILLO_APPID,
+            self::getHttpProxy()
+        // 'data-quest-Test'
         );
         $authHelper = new EduSharingAuthHelper($base);
         if (!static::$ticket) {
@@ -63,11 +72,12 @@ class TwilloConnector
             $header[] = "Accept: application/json";
 
             $cr = curl_init();
-            curl_setopt($cr, CURLOPT_URL, 'https://www.twillo.de/edu-sharing/rest/node/v1/nodes/-home-/-userhome-/children');
+            curl_setopt($cr, CURLOPT_URL, self::$twillo_base_url . '/rest/node/v1/nodes/-home-/-userhome-/children');
             curl_setopt($cr, CURLOPT_HTTPHEADER, $header);
             curl_setopt($cr, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($cr, CURLOPT_SSL_VERIFYPEER, false);
             curl_setopt($cr, CURLOPT_SSL_VERIFYHOST, false);
+            curl_setopt($cr, CURLOPT_PROXY, self::getHttpProxy());
             $body = curl_exec($cr);
             $error = curl_error($cr);
             if ($error) {
@@ -95,11 +105,13 @@ class TwilloConnector
 
                 $cr = curl_init();
                 curl_setopt($cr, CURLOPT_POST, 1);
-                curl_setopt($cr, CURLOPT_URL, 'https://www.twillo.de/edu-sharing/rest/node/v1/nodes/-home-/-userhome-/children?type=cm%3Afolder&renameIfExists=false&assocType=&versionComment=');
+                curl_setopt($cr, CURLOPT_URL, self::$twillo_base_url . '/rest/node/v1/nodes/-home-/-userhome-/children?type=cm%3Afolder&renameIfExists=false&assocType=&versionComment=');
                 curl_setopt($cr, CURLOPT_HTTPHEADER, $header);
                 curl_setopt($cr, CURLOPT_RETURNTRANSFER, 1);
                 curl_setopt($cr, CURLOPT_SSL_VERIFYPEER, false);
                 curl_setopt($cr, CURLOPT_SSL_VERIFYHOST, false);
+                curl_setopt($cr, CURLOPT_PROXY, self::getHttpProxy());
+
                 $postbody = json_encode([
                     'cm:edu_forcemetadataset' => ['true'],
                     'cm:edu_metadataset' => ['mds'],
@@ -121,11 +133,12 @@ class TwilloConnector
 
             $cr = curl_init();
             curl_setopt($cr, CURLOPT_POST, 1);
-            curl_setopt($cr, CURLOPT_URL, 'https://www.twillo.de/edu-sharing/rest/node/v1/nodes/-home-/'.$subfolder_id.'/children?type=ccm%3Aio&renameIfExists=true');
+            curl_setopt($cr, CURLOPT_URL, self::$twillo_base_url . '/rest/node/v1/nodes/-home-/'.$subfolder_id.'/children?type=ccm%3Aio&renameIfExists=true');
             curl_setopt($cr, CURLOPT_HTTPHEADER, $header);
             curl_setopt($cr, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($cr, CURLOPT_SSL_VERIFYPEER, false);
             curl_setopt($cr, CURLOPT_SSL_VERIFYHOST, false);
+            curl_setopt($cr, CURLOPT_PROXY, self::getHttpProxy());
             $postbody = json_encode([
                 'ccm:wwwurl' => [$material->getDownloadUrl()],
                 'ccm:linktype' => ["USER_GENERATED"],
@@ -148,11 +161,12 @@ class TwilloConnector
 
         $cr = curl_init();
         curl_setopt($cr, CURLOPT_POST, 1);
-        curl_setopt($cr, CURLOPT_URL, 'https://www.twillo.de/edu-sharing/rest/node/v1/nodes/-home-/'.$material['published_id_on_twillo'].'/metadata?versionComment=METADATA_UPDATE');
+        curl_setopt($cr, CURLOPT_URL, self::$twillo_base_url . '/rest/node/v1/nodes/-home-/'.$material['published_id_on_twillo'].'/metadata?versionComment=METADATA_UPDATE');
         curl_setopt($cr, CURLOPT_HTTPHEADER, $header);
         curl_setopt($cr, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($cr, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($cr, CURLOPT_SSL_VERIFYHOST, false);
+        curl_setopt($cr, CURLOPT_PROXY, self::getHttpProxy());
         $old_base = URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']);
         $permalink = URLHelper::getLink("dispatch.php/oer/market/details/".$material->getId());
         URLHelper::setBaseURL($old_base);
@@ -248,9 +262,10 @@ class TwilloConnector
         $user_id || $user_id = User::findCurrent()->id;
 
         $base = new EduSharingHelperBase(
-            'https://www.twillo.de/edu-sharing',
+            self::$twillo_base_url,
             file_get_contents($GLOBALS['STUDIP_BASE_PATH']."/config/twillo-private.key"),
-            Config::get()->OERCAMPUS_TWILLO_APPID // 'data-quest-Test'
+            Config::get()->OERCAMPUS_TWILLO_APPID,
+            self::getHttpProxy()// 'data-quest-Test'
         );
         $authHelper = new EduSharingAuthHelper($base);
         if (!static::$ticket) {
@@ -264,10 +279,11 @@ class TwilloConnector
 
         $cr = curl_init();
         curl_setopt($cr, CURLOPT_CUSTOMREQUEST, "DELETE");
-        curl_setopt($cr, CURLOPT_URL, 'https://www.twillo.de/edu-sharing/rest/node/v1/nodes/-home-/'.$oer_id);
+        curl_setopt($cr, CURLOPT_URL, self::$twillo_base_url . '/rest/node/v1/nodes/-home-/'.$oer_id);
         curl_setopt($cr, CURLOPT_HTTPHEADER, $header);
         curl_setopt($cr, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($cr, CURLOPT_SSL_VERIFYHOST, false);
+        curl_setopt($cr, CURLOPT_PROXY, self::getHttpProxy());
         curl_exec($cr);
         curl_close($cr);
     }
diff --git a/vendor/edu-sharing-plugin/edu-sharing-auth-helper.php b/vendor/edu-sharing-plugin/edu-sharing-auth-helper.php
index 071c0c6f561..ef9dec1f8f0 100644
--- a/vendor/edu-sharing-plugin/edu-sharing-auth-helper.php
+++ b/vendor/edu-sharing-plugin/edu-sharing-auth-helper.php
@@ -25,6 +25,9 @@ class EduSharingAuthHelper extends EduSharingHelperAbstract  {
             CURLOPT_CONNECTTIMEOUT => 5,
             CURLOPT_TIMEOUT => 5
         ]);
+        if ($this->base->http_proxy) {
+            curl_setopt($curl, CURLOPT_PROXY, $this->base->http_proxy);
+        }
         $data = json_decode(curl_exec($curl), true);
         curl_close($curl);
         if ( is_null( $data ) ) {
@@ -79,6 +82,9 @@ class EduSharingAuthHelper extends EduSharingHelperAbstract  {
             CURLOPT_POSTFIELDS,
             is_array($bodyparams) ? json_encode($bodyparams) : (string) $bodyparams
         );
+        if ($this->base->http_proxy) {
+            curl_setopt($curl, CURLOPT_PROXY, $this->base->http_proxy);
+        }
 
         $output = curl_exec($curl);
         $data = json_decode($output, true);
diff --git a/vendor/edu-sharing-plugin/edu-sharing-helper-base.php b/vendor/edu-sharing-plugin/edu-sharing-helper-base.php
index ac3e2f64e0a..4c6f6da53eb 100644
--- a/vendor/edu-sharing-plugin/edu-sharing-helper-base.php
+++ b/vendor/edu-sharing-plugin/edu-sharing-helper-base.php
@@ -5,6 +5,7 @@ class EduSharingHelperBase {
     public $privateKey;
     public $appId;
     public $language = 'de';
+    public $http_proxy = '';
 
     /**
      * @param string $baseUrl
@@ -17,7 +18,8 @@ class EduSharingHelperBase {
     public function __construct(
         string $baseUrl,
         string $privateKey,
-        string $appId
+        string $appId,
+        string $http_proxy = ''
     ) {
         if(!preg_match('/^([a-z]|[A-Z]|[0-9]|[-_])+$/', $appId)) {
             throw new Exception('The given app id contains invalid characters or symbols');
@@ -25,6 +27,7 @@ class EduSharingHelperBase {
         $this->baseUrl=$baseUrl;
         $this->privateKey=$privateKey;
         $this->appId=$appId;
+        $this->http_proxy = $http_proxy;
     }
 
     public function setLanguage(string $language) {
@@ -39,4 +42,4 @@ class EduSharingHelperBase {
         return $signature;
     }
 
-}
\ No newline at end of file
+}
diff --git a/vendor/edu-sharing-plugin/edu-sharing-node-helper.php b/vendor/edu-sharing-plugin/edu-sharing-node-helper.php
index 112d7d85d91..6c7a18ecead 100644
--- a/vendor/edu-sharing-plugin/edu-sharing-node-helper.php
+++ b/vendor/edu-sharing-plugin/edu-sharing-node-helper.php
@@ -69,6 +69,9 @@ class EduSharingNodeHelper extends EduSharingHelperAbstract  {
             CURLOPT_RETURNTRANSFER => 1,
             CURLOPT_HTTPHEADER => $headers
         ]);
+        if ($this->base->http_proxy) {
+            curl_setopt($curl, CURLOPT_PROXY, $this->base->http_proxy);
+        }
         $data = json_decode(curl_exec($curl), true);
         $err     = curl_errno( $curl );
         $info = curl_getinfo($curl);
@@ -127,6 +130,9 @@ class EduSharingNodeHelper extends EduSharingHelperAbstract  {
             CURLOPT_RETURNTRANSFER => 1,
             CURLOPT_HTTPHEADER => $headers
         ]);
+        if ($this->base->http_proxy) {
+            curl_setopt($curl, CURLOPT_PROXY, $this->base->http_proxy);
+        }
         $data = json_decode(curl_exec($curl), true);
         $err     = curl_errno( $curl );
         $info = curl_getinfo($curl);
@@ -160,6 +166,9 @@ class EduSharingNodeHelper extends EduSharingHelperAbstract  {
             CURLOPT_RETURNTRANSFER => 1,
             CURLOPT_HTTPHEADER => $headers
         ]);
+        if ($this->base->http_proxy) {
+            curl_setopt($curl, CURLOPT_PROXY, $this->base->http_proxy);
+        }
         $data = json_decode(curl_exec($curl), true);
         $err     = curl_errno( $curl );
         $info = curl_getinfo($curl);
@@ -172,4 +181,4 @@ class EduSharingNodeHelper extends EduSharingHelperAbstract  {
         }
 
     }
-}
\ No newline at end of file
+}
-- 
GitLab