3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This class handles HTTP downloads
15 * FIXME: fetch() and get() report errors differently -- e.g.
16 * fetch() returns fatal and get() returns an error code. Should
17 * refactor both (or get a third-party HTTP library) but don't
18 * want to deal with that so late in the 4.3 dev cycle.
21 * @copyright CiviCRM LLC https://civicrm.org/licensing
23 class CRM_Utils_HttpClient
{
25 const STATUS_OK
= 'ok';
26 const STATUS_WRITE_ERROR
= 'write-error';
27 const STATUS_DL_ERROR
= 'dl-error';
30 * @var CRM_Utils_HttpClient
32 protected static $singleton;
36 * seconds; or NULL to use system default
38 protected $connectionTimeout;
41 * @return CRM_Utils_HttpClient
43 public static function singleton() {
44 if (!self
::$singleton) {
45 self
::$singleton = new CRM_Utils_HttpClient();
47 return self
::$singleton;
51 * @param int|null $connectionTimeout
52 * seconds; or NULL to use system default
54 public function __construct($connectionTimeout = NULL) {
55 $this->connectionTimeout
= $connectionTimeout;
59 * Download the remote zipfile.
61 * @param string $remoteFile
63 * @param string $localFile
64 * Path at which to store the .zip file.
65 * @return STATUS_OK|STATUS_WRITE_ERROR|STATUS_DL_ERROR
67 * @throws CRM_Core_Exception
69 public function fetch($remoteFile, $localFile) {
70 // Download extension zip file ...
71 if (!function_exists('curl_init')) {
72 throw new CRM_Core_Exception('Cannot install this extension - curl is not installed!');
75 list($ch, $caConfig) = $this->createCurl($remoteFile);
76 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
77 throw new CRM_Core_Exception('Cannot install this extension - does not support SSL');
80 $fp = @fopen
($localFile, "w");
82 return self
::STATUS_WRITE_ERROR
;
84 curl_setopt($ch, CURLOPT_FILE
, $fp);
87 if (curl_errno($ch)) {
88 return self
::STATUS_DL_ERROR
;
96 return self
::STATUS_OK
;
100 * Send an HTTP GET for a remote resource.
102 * @param string $remoteFile
103 * URL of remote file.
105 * array(0 => STATUS_OK|STATUS_DL_ERROR, 1 => string)
107 public function get($remoteFile) {
108 // Download extension zip file ...
109 if (!function_exists('curl_init')) {
111 CRM_Core_Session
::setStatus(
112 ts('As a result, actions like retrieving the CiviCRM news feed will fail. Talk to your server administrator or hosting company to rectify this.'),
113 ts('Curl is not installed')
115 return [self
::STATUS_DL_ERROR
, NULL];
118 list($ch, $caConfig) = $this->createCurl($remoteFile);
120 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
121 return [self
::STATUS_DL_ERROR
, NULL];
124 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
125 $data = curl_exec($ch);
126 if (curl_errno($ch)) {
127 return [self
::STATUS_DL_ERROR
, $data];
133 return [self
::STATUS_OK
, $data];
137 * Send an HTTP POST for a remote resource.
139 * @param string $remoteFile
140 * URL of a .zip file.
141 * @param array $params
144 * array(0 => STATUS_OK|STATUS_DL_ERROR, 1 => string)
146 public function post($remoteFile, $params) {
147 // Download extension zip file ...
148 if (!function_exists('curl_init')) {
149 return [self
::STATUS_DL_ERROR
, NULL];
152 list($ch, $caConfig) = $this->createCurl($remoteFile);
154 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
155 return [self
::STATUS_DL_ERROR
, NULL];
158 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
159 curl_setopt($ch, CURLOPT_POST
, TRUE);
160 curl_setopt($ch, CURLOPT_POST
, count($params));
161 curl_setopt($ch, CURLOPT_POSTFIELDS
, $params);
162 $data = curl_exec($ch);
163 if (curl_errno($ch)) {
164 return [self
::STATUS_DL_ERROR
, $data];
170 return [self
::STATUS_OK
, $data];
174 * @param string $remoteFile
176 * (0 => resource, 1 => CA_Config_Curl)
178 protected function createCurl($remoteFile) {
179 $caConfig = CA_Config_Curl
::probe([
180 'verify_peer' => (bool) Civi
::settings()->get('verifySSL'),
184 curl_setopt($ch, CURLOPT_URL
, $remoteFile);
185 curl_setopt($ch, CURLOPT_HEADER
, FALSE);
186 curl_setopt($ch, CURLOPT_ENCODING
, 'gzip');
187 curl_setopt($ch, CURLOPT_VERBOSE
, 0);
188 if ($this->isRedirectSupported()) {
189 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, TRUE);
191 if ($this->connectionTimeout
!== NULL) {
192 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, $this->connectionTimeout
);
194 if (preg_match('/^https:/', $remoteFile) && $caConfig->isEnableSSL()) {
195 curl_setopt_array($ch, $caConfig->toCurlOptions());
198 return [$ch, $caConfig];
204 public function isRedirectSupported() {
205 return (ini_get('open_basedir') == '');