2014-09-11 21:21:56 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-22 10:45:36 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
*
|
|
|
|
* @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 21:21:56 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-09-11 21:21:56 +04:00
|
|
|
namespace OC;
|
|
|
|
|
2015-03-16 13:28:23 +03:00
|
|
|
use OCP\Http\Client\IClientService;
|
2014-12-04 21:51:04 +03:00
|
|
|
use OCP\IConfig;
|
2014-11-27 20:19:14 +03:00
|
|
|
|
2015-03-16 13:28:23 +03:00
|
|
|
/**
|
|
|
|
* Class HTTPHelper
|
|
|
|
*
|
|
|
|
* @package OC
|
|
|
|
* @deprecated Use \OCP\Http\Client\IClientService
|
|
|
|
*/
|
2014-09-11 21:21:56 +04:00
|
|
|
class HTTPHelper {
|
|
|
|
const USER_AGENT = 'ownCloud Server Crawler';
|
|
|
|
|
2014-11-27 20:19:14 +03:00
|
|
|
/** @var \OCP\IConfig */
|
2014-09-11 21:21:56 +04:00
|
|
|
private $config;
|
2015-03-16 13:28:23 +03:00
|
|
|
/** @var IClientService */
|
|
|
|
private $clientService;
|
2014-12-04 21:51:04 +03:00
|
|
|
|
2014-09-11 21:21:56 +04:00
|
|
|
/**
|
2015-03-16 13:28:23 +03:00
|
|
|
* @param IConfig $config
|
|
|
|
* @param IClientService $clientService
|
2014-09-11 21:21:56 +04:00
|
|
|
*/
|
2015-03-16 13:28:23 +03:00
|
|
|
public function __construct(IConfig $config,
|
|
|
|
IClientService $clientService) {
|
2014-09-11 21:21:56 +04:00
|
|
|
$this->config = $config;
|
2015-03-16 13:28:23 +03:00
|
|
|
$this->clientService = $clientService;
|
2014-09-11 21:21:56 +04: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 13:28:23 +03:00
|
|
|
* @deprecated Use \OCP\Http\Client\IClientService
|
2014-09-11 21:21:56 +04:00
|
|
|
*/
|
|
|
|
public function getUrlContent($url) {
|
2015-03-16 13:28:23 +03:00
|
|
|
try {
|
|
|
|
$client = $this->clientService->newClient();
|
|
|
|
$response = $client->get($url);
|
|
|
|
return $response->getBody();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
2014-09-11 21:21:56 +04: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 13:28:23 +03:00
|
|
|
* @deprecated Use \OCP\Http\Client\IClientService
|
2014-09-11 21:21:56 +04:00
|
|
|
*/
|
|
|
|
public function getHeaders($location) {
|
2015-03-16 13:28:23 +03:00
|
|
|
$client = $this->clientService->newClient();
|
|
|
|
$response = $client->get($location);
|
|
|
|
return $response->getHeaders();
|
2014-09-11 21:21:56 +04: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 21:51:04 +03:00
|
|
|
/**
|
|
|
|
* send http post request
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param array $fields data send by the request
|
2015-03-16 13:28:23 +03:00
|
|
|
* @return array
|
|
|
|
* @deprecated Use \OCP\Http\Client\IClientService
|
2014-12-04 21:51:04 +03:00
|
|
|
*/
|
|
|
|
public function post($url, array $fields) {
|
2015-03-16 13:28:23 +03:00
|
|
|
$client = $this->clientService->newClient();
|
2014-12-04 21:51:04 +03:00
|
|
|
|
2015-03-16 13:28:23 +03:00
|
|
|
try {
|
2015-05-26 12:22:50 +03:00
|
|
|
$response = $client->post(
|
|
|
|
$url,
|
|
|
|
[
|
|
|
|
'body' => $fields,
|
|
|
|
'connect_timeout' => 10,
|
|
|
|
]
|
|
|
|
);
|
2015-03-16 13:28:23 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return ['success' => false, 'result' => $e->getMessage()];
|
2014-12-04 21:51:04 +03:00
|
|
|
}
|
|
|
|
|
2015-03-16 13:28:23 +03:00
|
|
|
return ['success' => true, 'result' => $response->getBody()];
|
2014-12-04 21:51:04 +03:00
|
|
|
}
|
|
|
|
|
2014-09-11 21:21:56 +04:00
|
|
|
}
|