From f5515de2eec7aa2e40d31ec8f33ea017ef0c448c Mon Sep 17 00:00:00 2001 From: Francesco Nardelli Date: Thu, 24 Sep 2015 15:54:06 +0200 Subject: [PATCH] first commit to fix ngpush facebook and twitter connection through proxy authentication --- classes/facebook.php | 14 ++++++++++ classes/ngpush_base.php | 14 ++++++++++ classes/ngpush_facebook_base.php | 14 ++++++++++ classes/ngpush_facebook_feed.php | 13 ++++++++++ classes/twitteroauth.php | 13 ++++++++++ modules/push/save_access_token.php | 41 +++++++++++++++++++++++++++++- 6 files changed, 108 insertions(+), 1 deletion(-) diff --git a/classes/facebook.php b/classes/facebook.php index 3e148fe..abd3895 100644 --- a/classes/facebook.php +++ b/classes/facebook.php @@ -548,6 +548,20 @@ protected function makeRequest($url, $params, $ch=null) { } curl_setopt_array($ch, $opts); + + $ini = eZINI::instance(); + $proxy = $ini->variable( 'ProxySettings', 'ProxyServer' ); + if ( $proxy ) + { + curl_setopt( $ch, CURLOPT_PROXY, $proxy ); + $userName = $ini->variable( 'ProxySettings', 'User' ); + $password = $ini->variable( 'ProxySettings', 'Password' ); + if ( $userName ) + { + curl_setopt( $ch, CURLOPT_PROXYUSERPWD, "$userName:$password" ); + } + } + $result = curl_exec($ch); if ($result === false) { $e = new FacebookApiException(array( diff --git a/classes/ngpush_base.php b/classes/ngpush_base.php index cf9bea9..df3ad28 100755 --- a/classes/ngpush_base.php +++ b/classes/ngpush_base.php @@ -27,6 +27,7 @@ public static function save_token( $SettingsBlock, $Token, $TokenSuffix = false $fileHandler->storeContents( $Token ); $storedToken = $fileHandler->fetchContents(); + if ( $storedToken !== false ) return true; @@ -73,6 +74,19 @@ public function curl_redir_exec($ch, $content = '') $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query'] ? '?' . $url['query'] : ''); curl_setopt($ch, CURLOPT_URL, $new_url); + $ini = eZINI::instance(); + $proxy = $ini->variable( 'ProxySettings', 'ProxyServer' ); + if ( $proxy ) + { + curl_setopt( $ch, CURLOPT_PROXY, $proxy ); + $userName = $ini->variable( 'ProxySettings', 'User' ); + $password = $ini->variable( 'ProxySettings', 'Password' ); + if ( $userName ) + { + curl_setopt( $ch, CURLOPT_PROXYUSERPWD, "$userName:$password" ); + } + } + return self::curl_redir_exec($ch, $data2); } else diff --git a/classes/ngpush_facebook_base.php b/classes/ngpush_facebook_base.php index 68f1bbd..9e84c7d 100644 --- a/classes/ngpush_facebook_base.php +++ b/classes/ngpush_facebook_base.php @@ -32,6 +32,20 @@ public function getPageToken($Token, $Id) $ch = curl_init($url); curl_setopt_array($ch, $options); + + $ini = eZINI::instance(); + $proxy = $ini->variable( 'ProxySettings', 'ProxyServer' ); + if ( $proxy ) + { + curl_setopt( $ch, CURLOPT_PROXY, $proxy ); + $userName = $ini->variable( 'ProxySettings', 'User' ); + $password = $ini->variable( 'ProxySettings', 'Password' ); + if ( $userName ) + { + curl_setopt( $ch, CURLOPT_PROXYUSERPWD, "$userName:$password" ); + } + } + $content = curl_exec($ch); $errno = curl_errno($ch); $errmsg = curl_error($ch); diff --git a/classes/ngpush_facebook_feed.php b/classes/ngpush_facebook_feed.php index 86f0bf8..b219b5f 100644 --- a/classes/ngpush_facebook_feed.php +++ b/classes/ngpush_facebook_feed.php @@ -39,6 +39,19 @@ public function push($Account, $Arguments) $ch = curl_init($url); curl_setopt_array($ch, $options); + $ini = eZINI::instance(); + $proxy = $ini->variable( 'ProxySettings', 'ProxyServer' ); + if ( $proxy ) + { + curl_setopt( $ch, CURLOPT_PROXY, $proxy ); + $userName = $ini->variable( 'ProxySettings', 'User' ); + $password = $ini->variable( 'ProxySettings', 'Password' ); + if ( $userName ) + { + curl_setopt( $ch, CURLOPT_PROXYUSERPWD, "$userName:$password" ); + } + } + $content = curl_exec($ch); $errno = curl_errno($ch); $errmsg = curl_error($ch); diff --git a/classes/twitteroauth.php b/classes/twitteroauth.php index 1603ed3..71e8d09 100644 --- a/classes/twitteroauth.php +++ b/classes/twitteroauth.php @@ -203,6 +203,19 @@ function http($url, $method, $postfields = NULL) { curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); curl_setopt($ci, CURLOPT_HEADER, FALSE); + $ini = eZINI::instance(); + $proxy = $ini->variable( 'ProxySettings', 'ProxyServer' ); + if ( $proxy ) + { + curl_setopt( $ci, CURLOPT_PROXY, $proxy ); + $userName = $ini->variable( 'ProxySettings', 'User' ); + $password = $ini->variable( 'ProxySettings', 'Password' ); + if ( $userName ) + { + curl_setopt( $ci, CURLOPT_PROXYUSERPWD, "$userName:$password" ); + } + } + switch ($method) { case 'POST': curl_setopt($ci, CURLOPT_POST, TRUE); diff --git a/modules/push/save_access_token.php b/modules/push/save_access_token.php index 7e01cd2..06556de 100644 --- a/modules/push/save_access_token.php +++ b/modules/push/save_access_token.php @@ -9,6 +9,8 @@ $saveStatus = false; $NGPushIni = eZINI::instance( 'ngpush.ini' ); +$ini = eZINI::instance(); +$proxy_server = $ini->variable( 'ProxySettings', 'ProxyServer' ); switch ($Params['Case']) { case 'twitter': @@ -44,7 +46,44 @@ . "client_id=" . $NGPushIni->variable( $settingsBlock, 'AppId') . "&redirect_uri=" . urlencode($redirectUrl) . "&client_secret=" . $NGPushIni->variable( $settingsBlock, 'AppSecret') . "&code=" . $accessToken[1]; - $response = file_get_contents($token_url); + + //proxy + if($proxy_server!=null){ + + //if exists remove http or https from url + $proxy_server = preg_replace('#^https?://#', '', $proxy_server); + + $userName = $ini->variable( 'ProxySettings', 'User' ); + $password = $ini->variable( 'ProxySettings', 'Password' ); + + //econde username and password + $auth = base64_encode("$userName:$password"); + + //both http and https + $opts = array( + 'http' => array ( + 'method'=>'GET', + 'proxy'=>$proxy_server, + 'request_fulluri' => true, + 'header'=> "Proxy-Authorization: Basic $auth" + + ), + 'https' => array ( + 'method'=>'GET', + 'proxy'=>$proxy_server, + 'request_fulluri' => true, + 'header'=> "Proxy-Authorization: Basic $auth" + ) + ); + + $ctx = stream_context_create($opts); + $response = file_get_contents($token_url,false,$ctx); + + //no proxy + }else{ + $response = file_get_contents($token_url); + } + $params = null; parse_str($response, $params);