diff options
Diffstat (limited to 'lib/private/httphelper.php')
-rw-r--r-- | lib/private/httphelper.php | 164 |
1 files changed, 34 insertions, 130 deletions
diff --git a/lib/private/httphelper.php b/lib/private/httphelper.php index 6bb8e1d3ec0..16b824e927f 100644 --- a/lib/private/httphelper.php +++ b/lib/private/httphelper.php @@ -1,6 +1,6 @@ <?php /** - * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com> + * Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com> * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. @@ -8,41 +8,31 @@ namespace OC; +use OCP\Http\Client\IClientService; use OCP\IConfig; -use OCP\ICertificateManager; +/** + * Class HTTPHelper + * + * @package OC + * @deprecated Use \OCP\Http\Client\IClientService + */ class HTTPHelper { const USER_AGENT = 'ownCloud Server Crawler'; /** @var \OCP\IConfig */ private $config; - - /** @var \OC\Security\CertificateManager */ - private $certificateManager; + /** @var IClientService */ + private $clientService; /** - * @param \OCP\IConfig $config + * @param IConfig $config + * @param IClientService $clientService */ - public function __construct(IConfig $config, ICertificateManager $certificateManager) { + public function __construct(IConfig $config, + IClientService $clientService) { $this->config = $config; - $this->certificateManager = $certificateManager; - } - - /** - * Returns the default context array - * @return array - */ - public function getDefaultContextArray() { - return array( - 'http' => array( - 'header' => 'User-Agent: ' . self::USER_AGENT . "\r\n", - 'timeout' => 10, - 'follow_location' => false, // Do not follow the location since we can't limit the protocol - ), - 'ssl' => array( - 'disable_compression' => true - ) - ); + $this->clientService = $clientService; } /** @@ -52,87 +42,28 @@ class HTTPHelper { * @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_contents is used. + * @deprecated Use \OCP\Http\Client\IClientService */ public function getUrlContent($url) { - if (!$this->isHTTPURL($url)) { - throw new \Exception('$url must start with https:// or http://', 1); - } - - $proxy = $this->config->getSystemValue('proxy', null); - $proxyUserPwd = $this->config->getSystemValue('proxyuserpwd', null); - $curl = curl_init(); - $max_redirects = 10; - - curl_setopt($curl, CURLOPT_HEADER, 0); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); - curl_setopt($curl, CURLOPT_URL, $url); - curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); - curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); - - curl_setopt($curl, CURLOPT_USERAGENT, self::USER_AGENT); - if ($proxy !== null) { - curl_setopt($curl, CURLOPT_PROXY, $proxy); - } - if ($proxyUserPwd !== null) { - curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyUserPwd); - } - - if (ini_get('open_basedir') === '') { - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($curl, CURLOPT_MAXREDIRS, $max_redirects); - $data = curl_exec($curl); - } else { - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); - $mr = $max_redirects; - if ($mr > 0) { - $newURL = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); - $rCurl = curl_copy_handle($curl); - curl_setopt($rCurl, CURLOPT_HEADER, true); - curl_setopt($rCurl, CURLOPT_NOBODY, true); - curl_setopt($rCurl, CURLOPT_FORBID_REUSE, false); - curl_setopt($rCurl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($rCurl, CURLOPT_USERAGENT, self::USER_AGENT); - do { - curl_setopt($rCurl, CURLOPT_URL, $newURL); - $header = curl_exec($rCurl); - if (curl_errno($rCurl)) { - $code = 0; - } else { - $code = curl_getinfo($rCurl, CURLINFO_HTTP_CODE); - if ($code == 301 || $code == 302) { - preg_match('/Location:(.*?)\n/', $header, $matches); - $newURL = trim(array_pop($matches)); - } else { - $code = 0; - } - } - } while ($code && --$mr); - curl_close($rCurl); - if ($mr > 0) { - curl_setopt($curl, CURLOPT_URL, $newURL); - } - } - - if ($mr == 0 && $max_redirects > 0) { - $data = false; - } else { - $data = curl_exec($curl); - } + try { + $client = $this->clientService->newClient(); + $response = $client->get($url); + return $response->getBody(); + } catch (\Exception $e) { + return false; } - curl_close($curl); - - return $data; } /** * Returns the response headers of a HTTP URL without following redirects * @param string $location Needs to be a HTTPS or HTTP URL * @return array + * @deprecated Use \OCP\Http\Client\IClientService */ public function getHeaders($location) { - stream_context_set_default($this->getDefaultContextArray()); - return get_headers($location, 1); + $client = $this->clientService->newClient(); + $response = $client->get($location); + return $response->getHeaders(); } /** @@ -145,50 +76,23 @@ class HTTPHelper { } /** - * create string of parameters for post request - * - * @param array $parameters - * @return string - */ - private function assemblePostParameters(array $parameters) { - $parameterString = ''; - foreach ($parameters as $key => $value) { - $parameterString .= $key . '=' . urlencode($value) . '&'; - } - - return rtrim($parameterString, '&'); - } - - /** * send http post request * * @param string $url * @param array $fields data send by the request - * @return bool + * @return array + * @deprecated Use \OCP\Http\Client\IClientService */ public function post($url, array $fields) { + $client = $this->clientService->newClient(); - $fieldsString = $this->assemblePostParameters($fields); - - $certBundle = $this->certificateManager->getCertificateBundle(); - - $ch = curl_init(); - - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_POST, count($fields)); - curl_setopt($ch, CURLOPT_POSTFIELDS, (string)$fieldsString); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); - if (is_readable($certBundle)) { - curl_setopt($ch, CURLOPT_CAINFO, $certBundle); + try { + $response = $client->post($url, ['body' => $fields]); + } catch (\Exception $e) { + return ['success' => false, 'result' => $e->getMessage()]; } - $result = curl_exec($ch); - $success = $result ? true : false; - - curl_close($ch); - - return array('success' => $success, 'result' => $result); + return ['success' => true, 'result' => $response->getBody()]; } } |