2014-09-11 17:21:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
*
|
2016-01-12 14:02:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2014-09-11 17:21:56 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-09-11 17:21:56 +00:00
|
|
|
namespace OC;
|
|
|
|
|
2015-03-16 10:28:23 +00:00
|
|
|
use OCP\Http\Client\IClientService;
|
2014-12-04 18:51:04 +00:00
|
|
|
use OCP\IConfig;
|
2014-11-27 17:19:14 +00:00
|
|
|
|
2015-03-16 10:28:23 +00:00
|
|
|
/**
|
|
|
|
* Class HTTPHelper
|
|
|
|
*
|
|
|
|
* @package OC
|
|
|
|
* @deprecated Use \OCP\Http\Client\IClientService
|
|
|
|
*/
|
2014-09-11 17:21:56 +00:00
|
|
|
class HTTPHelper {
|
|
|
|
const USER_AGENT = 'ownCloud Server Crawler';
|
|
|
|
|
2014-11-27 17:19:14 +00:00
|
|
|
/** @var \OCP\IConfig */
|
2014-09-11 17:21:56 +00:00
|
|
|
private $config;
|
2015-03-16 10:28:23 +00:00
|
|
|
/** @var IClientService */
|
|
|
|
private $clientService;
|
2014-12-04 18:51:04 +00:00
|
|
|
|
2014-09-11 17:21:56 +00:00
|
|
|
/**
|
2015-03-16 10:28:23 +00:00
|
|
|
* @param IConfig $config
|
|
|
|
* @param IClientService $clientService
|
2014-09-11 17:21:56 +00:00
|
|
|
*/
|
2015-03-16 10:28:23 +00:00
|
|
|
public function __construct(IConfig $config,
|
|
|
|
IClientService $clientService) {
|
2014-09-11 17:21:56 +00:00
|
|
|
$this->config = $config;
|
2015-03-16 10:28:23 +00:00
|
|
|
$this->clientService = $clientService;
|
2014-09-11 17:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get URL content
|
|
|
|
* @param string $url Url to get content
|
|
|
|
* @throws \Exception If the URL does not start with http:// or https://
|
|
|
|
* @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.
|
2015-03-16 10:28:23 +00:00
|
|
|
* @deprecated Use \OCP\Http\Client\IClientService
|
2014-09-11 17:21:56 +00:00
|
|
|
*/
|
|
|
|
public function getUrlContent($url) {
|
2015-03-16 10:28:23 +00:00
|
|
|
try {
|
|
|
|
$client = $this->clientService->newClient();
|
|
|
|
$response = $client->get($url);
|
|
|
|
return $response->getBody();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
2014-09-11 17:21:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the response headers of a HTTP URL without following redirects
|
|
|
|
* @param string $location Needs to be a HTTPS or HTTP URL
|
|
|
|
* @return array
|
2015-03-16 10:28:23 +00:00
|
|
|
* @deprecated Use \OCP\Http\Client\IClientService
|
2014-09-11 17:21:56 +00:00
|
|
|
*/
|
|
|
|
public function getHeaders($location) {
|
2015-03-16 10:28:23 +00:00
|
|
|
$client = $this->clientService->newClient();
|
|
|
|
$response = $client->get($location);
|
|
|
|
return $response->getHeaders();
|
2014-09-11 17:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the supplied URL begins with HTTPS:// or HTTP:// (case insensitive)
|
|
|
|
* @param string $url
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isHTTPURL($url) {
|
|
|
|
return stripos($url, 'https://') === 0 || stripos($url, 'http://') === 0;
|
|
|
|
}
|
|
|
|
|
2014-12-04 18:51:04 +00:00
|
|
|
/**
|
|
|
|
* send http post request
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param array $fields data send by the request
|
2015-03-16 10:28:23 +00:00
|
|
|
* @return array
|
|
|
|
* @deprecated Use \OCP\Http\Client\IClientService
|
2014-12-04 18:51:04 +00:00
|
|
|
*/
|
|
|
|
public function post($url, array $fields) {
|
2015-03-16 10:28:23 +00:00
|
|
|
$client = $this->clientService->newClient();
|
2014-12-04 18:51:04 +00:00
|
|
|
|
2015-03-16 10:28:23 +00:00
|
|
|
try {
|
2015-05-26 09:22:50 +00:00
|
|
|
$response = $client->post(
|
|
|
|
$url,
|
|
|
|
[
|
|
|
|
'body' => $fields,
|
|
|
|
'connect_timeout' => 10,
|
|
|
|
]
|
|
|
|
);
|
2015-03-16 10:28:23 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return ['success' => false, 'result' => $e->getMessage()];
|
2014-12-04 18:51:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 10:28:23 +00:00
|
|
|
return ['success' => true, 'result' => $response->getBody()];
|
2014-12-04 18:51:04 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 17:21:56 +00:00
|
|
|
}
|