From 4564898c288ab4cd221eeba91ab728331f12dba5 Mon Sep 17 00:00:00 2001 From: thomas Date: Mon, 12 Nov 2012 15:37:44 +0100 Subject: [PATCH 1/4] Use curl to get remote file content --- lib/ocsclient.php | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/ocsclient.php b/lib/ocsclient.php index b6b5ad8f0a..283f95d585 100644 --- a/lib/ocsclient.php +++ b/lib/ocsclient.php @@ -55,19 +55,29 @@ class OC_OCSClient{ * This function calls an OCS server and returns the response. It also sets a sane timeout */ private static function getOCSresponse($url) { - // set a sensible timeout of 10 sec to stay responsive even if the server is down. - $ctx = stream_context_create( - array( - 'http' => array( - 'timeout' => 10 - ) - ) - ); - $data=@file_get_contents($url, 0, $ctx); + $data = self::fileGetContentCurl($url); return($data); } - + /** + * @Brief Get file content via curl. + * @return string of the response + * This function get the content of a page via curl. + */ + + private static function fileGetContentCurl($url){ + $curl = curl_init(); + + curl_setopt($curl, CURLOPT_HEADER, 0); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_URL, $url); + + $data = curl_exec($curl); + curl_close($data); + + return $data; + } + /** * @brief Get all the categories from the OCS server * @returns array with category ids From 847467ab001fadc666770a0d47e909744935aa16 Mon Sep 17 00:00:00 2001 From: thomas Date: Mon, 12 Nov 2012 16:29:22 +0100 Subject: [PATCH 2/4] Add connection time out option --- lib/ocsclient.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ocsclient.php b/lib/ocsclient.php index 283f95d585..795ce30190 100644 --- a/lib/ocsclient.php +++ b/lib/ocsclient.php @@ -70,6 +70,7 @@ class OC_OCSClient{ curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curl, CURLOPT_URL, $url); $data = curl_exec($curl); From 40dd5ae61c8c62cfcda13bd8f8a3b67ff3c980e0 Mon Sep 17 00:00:00 2001 From: thomas Date: Wed, 14 Nov 2012 23:14:04 +0100 Subject: [PATCH 3/4] change and transfert getUrlContent --- lib/ocsclient.php | 22 +--------------------- lib/util.php | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/lib/ocsclient.php b/lib/ocsclient.php index 795ce30190..e730b159af 100644 --- a/lib/ocsclient.php +++ b/lib/ocsclient.php @@ -55,31 +55,11 @@ class OC_OCSClient{ * This function calls an OCS server and returns the response. It also sets a sane timeout */ private static function getOCSresponse($url) { - $data = self::fileGetContentCurl($url); + $data = \OC_Util::getUrlContent($url); return($data); } /** - * @Brief Get file content via curl. - * @return string of the response - * This function get the content of a page via curl. - */ - - private static function fileGetContentCurl($url){ - $curl = curl_init(); - - curl_setopt($curl, CURLOPT_HEADER, 0); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); - curl_setopt($curl, CURLOPT_URL, $url); - - $data = curl_exec($curl); - curl_close($data); - - return $data; - } - - /** * @brief Get all the categories from the OCS server * @returns array with category ids * @note returns NULL if config value appstoreenabled is set to false diff --git a/lib/util.php b/lib/util.php index 40b44bf9d6..497b787922 100755 --- a/lib/util.php +++ b/lib/util.php @@ -642,4 +642,43 @@ class OC_Util { return false; } + + /** + * @Brief Get file content via curl. + * @param string $url Url to get content + * @return string of the response + * This function get the content of a page via curl, if curl is enabled. + * If not, file_get_element is used. + */ + + public static function getUrlContent($url){ + + if (function_exists('curl_init')) { + + $curl = curl_init(); + + curl_setopt($curl, CURLOPT_HEADER, 0); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($curl, CURLOPT_URL, $url); + + $data = curl_exec($curl); + curl_close($data); + + } else { + + $ctx = stream_context_create( + array( + 'http' => array( + 'timeout' => 10 + ) + ) + ); + $data=@file_get_contents($url, 0, $ctx); + + } + + return($data); + } + } From d2047a00cf0fa7383a6dd1834d3d3500f58b1931 Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Nov 2012 20:46:17 +0100 Subject: [PATCH 4/4] Remove parentheses in return, modify description, and fix a mistake --- lib/util.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/util.php b/lib/util.php index 497b787922..6a8fab0da2 100755 --- a/lib/util.php +++ b/lib/util.php @@ -646,7 +646,7 @@ class OC_Util { /** * @Brief Get file content via curl. * @param string $url Url to get content - * @return string of the response + * @return string of the response or false on error * This function get the content of a page via curl, if curl is enabled. * If not, file_get_element is used. */ @@ -663,7 +663,7 @@ class OC_Util { curl_setopt($curl, CURLOPT_URL, $url); $data = curl_exec($curl); - curl_close($data); + curl_close($curl); } else { @@ -678,7 +678,7 @@ class OC_Util { } - return($data); + return $data; } }