Ejemplo n.º 1
0
 public function testPostException()
 {
     $client = $this->getMockBuilder('\\OCP\\Http\\Client\\IClient')->disableOriginalConstructor()->getMock();
     $this->clientService->expects($this->once())->method('newClient')->will($this->returnValue($client));
     $client->expects($this->once())->method('post')->with('https://owncloud.org', ['body' => ['Foo' => 'Bar'], 'connect_timeout' => 10])->will($this->throwException(new \Exception('Something failed')));
     $response = $this->httpHelperMock->post('https://owncloud.org', ['Foo' => 'Bar']);
     $expected = ['success' => false, 'result' => 'Something failed'];
     $this->assertSame($expected, $response);
 }
Ejemplo n.º 2
0
 /**
  * send http post request
  *
  * @param string $url
  * @param array $fields data send by the request
  * @return array
  * @deprecated Use \OCP\Http\Client\IClientService
  */
 public function post($url, array $fields)
 {
     $client = $this->clientService->newClient();
     try {
         $response = $client->post($url, ['body' => $fields, 'connect_timeout' => 10]);
     } catch (\Exception $e) {
         return ['success' => false, 'result' => $e->getMessage()];
     }
     return ['success' => true, 'result' => $response->getBody()];
 }
Ejemplo n.º 3
0
	/**
	 * Checks if the ownCloud server can connect to the internet using HTTPS and HTTP
	 * @return bool
	 */
	private function isInternetConnectionWorking() {
		if ($this->config->getSystemValue('has_internet_connection', true) === false) {
			return false;
		}

		try {
			$client = $this->clientService->newClient();
			$client->get('https://www.owncloud.org/');
			$client->get('http://www.owncloud.org/');
			return true;
		} catch (\Exception $e) {
			return false;
		}
	}
Ejemplo n.º 4
0
    public function testGetApplicationDownloadUrlSuccessful()
    {
        $this->config->expects($this->at(0))->method('getSystemValue')->with('appstoreenabled', true)->will($this->returnValue(true));
        $this->config->expects($this->at(1))->method('getSystemValue')->with('appstoreurl', 'https://api.owncloud.com/v1')->will($this->returnValue('https://api.owncloud.com/v1'));
        $response = $this->getMock('\\OCP\\Http\\Client\\IResponse');
        $response->expects($this->once())->method('getBody')->will($this->returnValue('<?xml version="1.0"?>
				<ocs>
				 <meta>
				  <status>ok</status>
				  <statuscode>100</statuscode>
				  <message></message>
				 </meta>
				 <data>
				  <content details="download">
				   <downloadlink>https://apps.owncloud.com/CONTENT/content-files/166052-files_trashbin.zip</downloadlink>
				   <mimetype>application/zip</mimetype>
				   <gpgfingerprint></gpgfingerprint>
				   <gpgsignature></gpgsignature>
				   <packagename></packagename>
				   <repository></repository>
				  </content>
				 </data>
				</ocs>
				'));
        $client = $this->getMock('\\OCP\\Http\\Client\\IClient');
        $client->expects($this->once())->method('get')->with('https://api.owncloud.com/v1/content/download/MyId/1', ['timeout' => 5, 'query' => ['version' => '8x1x0x7']])->will($this->returnValue($response));
        $this->clientService->expects($this->once())->method('newClient')->will($this->returnValue($client));
        $expected = ['downloadlink' => 'https://apps.owncloud.com/CONTENT/content-files/166052-files_trashbin.zip'];
        $this->assertSame($expected, $this->ocsClient->getApplicationDownload('MyId', [8, 1, 0, 7]));
    }
Ejemplo n.º 5
0
 /**
  * try http post first with https and then with http as a fallback
  *
  * @param string $remoteDomain
  * @param string $urlSuffix
  * @param array $fields post parameters
  * @return array
  * @throws \Exception
  */
 protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields)
 {
     $client = $this->httpClientService->newClient();
     $protocol = 'https://';
     $result = ['success' => false, 'result' => ''];
     $try = 0;
     while ($result['success'] === false && $try < 2) {
         $endpoint = $this->discoveryManager->getShareEndpoint($protocol . $remoteDomain);
         try {
             $response = $client->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, ['body' => $fields, 'timeout' => 10, 'connect_timeout' => 10]);
             $result['result'] = $response->getBody();
             $result['success'] = true;
             break;
         } catch (\Exception $e) {
             // if flat re-sharing is not supported by the remote server
             // we re-throw the exception and fall back to the old behaviour.
             // (flat re-shares has been introduced in ownCloud 9.1)
             if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
                 throw $e;
             }
             $try++;
             $protocol = 'http://';
         }
     }
     return $result;
 }
Ejemplo n.º 6
0
 /**
  * Check if the used  SSL lib is outdated. Older OpenSSL and NSS versions do
  * have multiple bugs which likely lead to problems in combination with
  * functionality required by ownCloud such as SNI.
  *
  * @link https://github.com/owncloud/core/issues/17446#issuecomment-122877546
  * @link https://bugzilla.redhat.com/show_bug.cgi?id=1241172
  * @return string
  */
 private function isUsedTlsLibOutdated()
 {
     $versionString = $this->getCurlVersion();
     if (isset($versionString['ssl_version'])) {
         $versionString = $versionString['ssl_version'];
     } else {
         return '';
     }
     $features = (string) $this->l10n->t('installing and updating apps via the app store or Federated Cloud Sharing');
     if (OC_Util::getEditionString() !== '') {
         $features = (string) $this->l10n->t('Federated Cloud Sharing');
     }
     // Check if at least OpenSSL after 1.01d or 1.0.2b
     if (strpos($versionString, 'OpenSSL/') === 0) {
         $majorVersion = substr($versionString, 8, 5);
         $patchRelease = substr($versionString, 13, 6);
         if ($majorVersion === '1.0.1' && ord($patchRelease) < ord('d') || $majorVersion === '1.0.2' && ord($patchRelease) < ord('b')) {
             return (string) $this->l10n->t('cURL is using an outdated %s version (%s). Please update your operating system or features such as %s will not work reliably.', ['OpenSSL', $versionString, $features]);
         }
     }
     // Check if NSS and perform heuristic check
     if (strpos($versionString, 'NSS/') === 0) {
         try {
             $firstClient = $this->clientService->newClient();
             $firstClient->get('https://www.owncloud.org/');
             $secondClient = $this->clientService->newClient();
             $secondClient->get('https://owncloud.org/');
         } catch (ClientException $e) {
             if ($e->getResponse()->getStatusCode() === 400) {
                 return (string) $this->l10n->t('cURL is using an outdated %s version (%s). Please update your operating system or features such as %s will not work reliably.', ['NSS', $versionString, $features]);
             }
         }
     }
     return '';
 }
Ejemplo n.º 7
0
 /**
  * Get the download url for an application from the OCS server
  * @param string $id
  * @param array $targetVersion The target ownCloud version
  * @return array|null an array of application data or null
  */
 public function getApplicationDownload($id, array $targetVersion)
 {
     if (!$this->isAppStoreEnabled()) {
         return null;
     }
     $url = $this->getAppStoreUrl() . '/content/download/' . urlencode($id) . '/1';
     $client = $this->httpClientService->newClient();
     try {
         $response = $client->get($url, ['timeout' => 5, 'query' => ['version' => implode('x', $targetVersion)]]);
     } catch (\Exception $e) {
         $this->logger->error(sprintf('Could not get application download URL: %s', $e->getMessage()), ['app' => 'core']);
         return null;
     }
     $data = $this->loadData($response->getBody(), 'application download URL');
     if ($data === null) {
         return null;
     }
     $tmp = $data->data->content;
     $app = [];
     if (isset($tmp->downloadlink)) {
         $app['downloadlink'] = (string) $tmp->downloadlink;
     } else {
         $app['downloadlink'] = '';
     }
     return $app;
 }
Ejemplo n.º 8
0
 public function testRemoteWithInvalidRemote()
 {
     $client = $this->getMockBuilder('\\OCP\\Http\\Client\\IClient')->disableOriginalConstructor()->getMock();
     $response = $this->getMockBuilder('\\OCP\\Http\\Client\\IResponse')->disableOriginalConstructor()->getMock();
     $client->method('get')->will($this->onConsecutiveCalls($response, $response));
     $response->expects($this->exactly(2))->method('getBody')->will($this->returnValue('Certainly not a JSON string'));
     $this->clientService->expects($this->exactly(2))->method('newClient')->will($this->returnValue($client));
     $this->assertEquals(new DataResponse(false), $this->getExternalShareController()->testRemote('owncloud.org'));
 }
Ejemplo n.º 9
0
 /**
  * Test whether the specified remote is accessible
  *
  * @param string $remote
  * @return bool
  */
 protected function testUrl($remote)
 {
     try {
         $client = $this->clientService->newClient();
         $response = json_decode($client->get($remote, ['timeout' => 3, 'connect_timeout' => 3])->getBody());
         return !empty($response->version) && version_compare($response->version, '7.0.0', '>=');
     } catch (\Exception $e) {
         return false;
     }
 }
Ejemplo n.º 10
0
 /**
  * @param string $path
  * @param string $target
  */
 protected function uploadFile($path, $target)
 {
     $this->init();
     // invalidate
     $target = $this->cleanPath($target);
     $this->statCache->remove($target);
     $source = fopen($path, 'r');
     $this->httpClientService->newClient()->put($this->createBaseUri() . $this->encodePath($target), ['body' => $source, 'auth' => [$this->user, $this->password]]);
     $this->removeCachedFile($target);
 }
Ejemplo n.º 11
0
 /**
  * check if URL point to a ownCloud server
  *
  * @param string $url
  * @return bool
  */
 public function isOwnCloudServer($url)
 {
     $isValidOwnCloud = false;
     $client = $this->httpClientService->newClient();
     $result = $client->get($url . '/status.php', ['timeout' => 3, 'connect_timeout' => 3]);
     if ($result->getStatusCode() === Http::STATUS_OK) {
         $isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody());
     }
     return $isValidOwnCloud;
 }
Ejemplo n.º 12
0
 /**
  * @codeCoverageIgnore
  * @param string $url
  * @return bool|resource|string
  */
 protected function getUrlContent($url)
 {
     try {
         $client = $this->clientService->newClient();
         $response = $client->get($url);
         return $response->getBody();
     } catch (\Exception $e) {
         return false;
     }
 }
Ejemplo n.º 13
0
 public function testIsBuggyNss200()
 {
     $this->checkSetupController->expects($this->once())->method('getCurlVersion')->will($this->returnValue(['ssl_version' => 'NSS/1.0.2b']));
     $client = $this->getMockBuilder('\\OCP\\Http\\Client\\IClient')->disableOriginalConstructor()->getMock();
     $exception = $this->getMockBuilder('\\GuzzleHttp\\Exception\\ClientException')->disableOriginalConstructor()->getMock();
     $response = $this->getMockBuilder('\\GuzzleHttp\\Message\\ResponseInterface')->disableOriginalConstructor()->getMock();
     $response->expects($this->once())->method('getStatusCode')->will($this->returnValue(200));
     $exception->expects($this->once())->method('getResponse')->will($this->returnValue($response));
     $client->expects($this->at(0))->method('get')->with('https://www.owncloud.org/', [])->will($this->throwException($exception));
     $this->clientService->expects($this->once())->method('newClient')->will($this->returnValue($client));
     $this->assertSame('', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
 }
 public function testCheck()
 {
     $this->config->expects($this->at(0))->method('getSystemValue')->with('has_internet_connection', true)->will($this->returnValue(true));
     $this->config->expects($this->at(1))->method('getSystemValue')->with('memcache.local', null)->will($this->returnValue('SomeProvider'));
     $client = $this->getMockBuilder('\\OCP\\Http\\Client\\IClient')->disableOriginalConstructor()->getMock();
     $client->expects($this->at(0))->method('get')->with('https://www.owncloud.org/', []);
     $client->expects($this->at(1))->method('get')->with('http://www.owncloud.org/', [])->will($this->throwException(new \Exception()));
     $this->clientService->expects($this->once())->method('newClient')->will($this->returnValue($client));
     $this->util->expects($this->once())->method('isHtaccessWorking')->will($this->returnValue(true));
     $this->urlGenerator->expects($this->once())->method('linkToDocs')->with('admin-performance')->willReturn('http://doc.owncloud.org/server/go.php?to=admin-performance');
     $expected = new DataResponse(['serverHasInternetConnection' => false, 'dataDirectoryProtected' => true, 'isMemcacheConfigured' => true, 'memcacheDocs' => 'http://doc.owncloud.org/server/go.php?to=admin-performance']);
     $this->assertEquals($expected, $this->checkSetupController->check());
 }
Ejemplo n.º 15
0
 /**
  * Check if the used  SSL lib is outdated. Older OpenSSL and NSS versions do
  * have multiple bugs which likely lead to problems in combination with
  * functionality required by ownCloud such as SNI.
  *
  * @link https://github.com/owncloud/core/issues/17446#issuecomment-122877546
  * @link https://bugzilla.redhat.com/show_bug.cgi?id=1241172
  * @return string
  */
 private function isUsedTlsLibOutdated()
 {
     // Appstore is disabled by default in EE
     $appStoreDefault = false;
     if (\OC_Util::getEditionString() === '') {
         $appStoreDefault = true;
     }
     // Don't run check when:
     // 1. Server has `has_internet_connection` set to false
     // 2. AppStore AND S2S is disabled
     if (!$this->config->getSystemValue('has_internet_connection', true)) {
         return '';
     }
     if (!$this->config->getSystemValue('appstoreenabled', $appStoreDefault) && $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'no' && $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') === 'no') {
         return '';
     }
     $versionString = $this->getCurlVersion();
     if (isset($versionString['ssl_version'])) {
         $versionString = $versionString['ssl_version'];
     } else {
         return '';
     }
     $features = (string) $this->l10n->t('installing and updating apps via the app store or Federated Cloud Sharing');
     if (!$this->config->getSystemValue('appstoreenabled', $appStoreDefault)) {
         $features = (string) $this->l10n->t('Federated Cloud Sharing');
     }
     // Check if at least OpenSSL after 1.01d or 1.0.2b
     if (strpos($versionString, 'OpenSSL/') === 0) {
         $majorVersion = substr($versionString, 8, 5);
         $patchRelease = substr($versionString, 13, 6);
         if ($majorVersion === '1.0.1' && ord($patchRelease) < ord('d') || $majorVersion === '1.0.2' && ord($patchRelease) < ord('b')) {
             return (string) $this->l10n->t('cURL is using an outdated %s version (%s). Please update your operating system or features such as %s will not work reliably.', ['OpenSSL', $versionString, $features]);
         }
     }
     // Check if NSS and perform heuristic check
     if (strpos($versionString, 'NSS/') === 0) {
         try {
             $firstClient = $this->clientService->newClient();
             $firstClient->get('https://www.owncloud.org/');
             $secondClient = $this->clientService->newClient();
             $secondClient->get('https://owncloud.org/');
         } catch (ClientException $e) {
             if ($e->getResponse()->getStatusCode() === 400) {
                 return (string) $this->l10n->t('cURL is using an outdated %s version (%s). Please update your operating system or features such as %s will not work reliably.', ['NSS', $versionString, $features]);
             }
         }
     }
     return '';
 }
Ejemplo n.º 16
0
 /**
  * check if URL point to a ownCloud server
  *
  * @param string $url
  * @return bool
  */
 public function isOwnCloudServer($url)
 {
     $isValidOwnCloud = false;
     $client = $this->httpClientService->newClient();
     try {
         $result = $client->get($url . '/status.php', ['timeout' => 3, 'connect_timeout' => 3]);
         if ($result->getStatusCode() === Http::STATUS_OK) {
             $isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody());
         }
     } catch (\Exception $e) {
         $this->logger->error($e->getMessage(), ['app' => 'federation']);
         return false;
     }
     return $isValidOwnCloud;
 }
Ejemplo n.º 17
0
 /**
  * @return \OC_OCS_Result
  */
 public function sendReport()
 {
     $report = $this->getReport();
     $client = $this->clientService->newClient();
     $this->config->setAppValue('popularitycontestclient', 'last_sent', time());
     $this->config->setAppValue('popularitycontestclient', 'last_report', json_encode($report));
     try {
         $response = $client->post(self::SURVEY_SERVER_URL . 'ocs/v2.php/apps/popularitycontestserver/api/v1/survey', ['timeout' => 5, 'query' => ['data' => json_encode($report)]]);
     } catch (\Exception $e) {
         return new \OC_OCS_Result($report, Http::STATUS_INTERNAL_SERVER_ERROR);
     }
     if ($response->getStatusCode() === Http::STATUS_OK) {
         return new \OC_OCS_Result($report, 100);
     }
     return new \OC_OCS_Result($report, Http::STATUS_INTERNAL_SERVER_ERROR);
 }
Ejemplo n.º 18
0
 /**
  * try http post first with https and then with http as a fallback
  *
  * @param string $url
  * @param array $fields post parameters
  * @return array
  */
 private function tryHttpPost($url, array $fields)
 {
     $client = $this->httpClientService->newClient();
     $protocol = 'https://';
     $result = ['success' => false, 'result' => ''];
     $try = 0;
     while ($result['success'] === false && $try < 2) {
         try {
             $response = $client->post($protocol . $url, ['body' => $fields]);
             $result['result'] = $response->getBody();
             $result['success'] = true;
             break;
         } catch (\Exception $e) {
             $try++;
             $protocol = 'http://';
         }
     }
     return $result;
 }
Ejemplo n.º 19
0
 /**
  * @param string $url
  * @return bool
  */
 private function testRemoteUrl($url)
 {
     $cache = $this->memcacheFactory->create('files_sharing_remote_url');
     if ($cache->hasKey($url)) {
         return (bool) $cache->get($url);
     }
     $client = $this->httpClient->newClient();
     try {
         $result = $client->get($url, ['timeout' => 10, 'connect_timeout' => 10])->getBody();
         $data = json_decode($result);
         $returnValue = is_object($data) && !empty($data->version);
     } catch (ConnectException $e) {
         $returnValue = false;
     } catch (ClientException $e) {
         $returnValue = false;
     }
     $cache->set($url, $returnValue);
     return $returnValue;
 }
Ejemplo n.º 20
0
 /**
  * try http post first with https and then with http as a fallback
  *
  * @param string $remoteDomain
  * @param string $urlSuffix
  * @param array $fields post parameters
  * @return array
  */
 private function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields)
 {
     $client = $this->httpClientService->newClient();
     $protocol = 'https://';
     $result = ['success' => false, 'result' => ''];
     $try = 0;
     while ($result['success'] === false && $try < 2) {
         $endpoint = $this->discoveryManager->getShareEndpoint($protocol . $remoteDomain);
         try {
             $response = $client->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, ['body' => $fields]);
             $result['result'] = $response->getBody();
             $result['success'] = true;
             break;
         } catch (\Exception $e) {
             $try++;
             $protocol = 'http://';
         }
     }
     return $result;
 }
Ejemplo n.º 21
0
 /**
  * @param ICacheFactory $cacheFactory
  * @param IClientService $clientService
  */
 public function __construct(ICacheFactory $cacheFactory, IClientService $clientService)
 {
     $this->cache = $cacheFactory->create('ocs-discovery');
     $this->client = $clientService->newClient();
 }