Merge pull request #15734 from seamuslee001/remove_activity_option_join_custom_search
[civicrm-core.git] / CRM / Utils / HttpClient.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This class handles HTTP downloads
14 *
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.
19 *
20 * @package CRM
21 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 */
23 class CRM_Utils_HttpClient {
24
25 const STATUS_OK = 'ok';
26 const STATUS_WRITE_ERROR = 'write-error';
27 const STATUS_DL_ERROR = 'dl-error';
28
29 /**
30 * @var CRM_Utils_HttpClient
31 */
32 protected static $singleton;
33
34 /**
35 * @var int|null
36 * seconds; or NULL to use system default
37 */
38 protected $connectionTimeout;
39
40 /**
41 * @return CRM_Utils_HttpClient
42 */
43 public static function singleton() {
44 if (!self::$singleton) {
45 self::$singleton = new CRM_Utils_HttpClient();
46 }
47 return self::$singleton;
48 }
49
50 /**
51 * @param null $connectionTimeout
52 */
53 public function __construct($connectionTimeout = NULL) {
54 $this->connectionTimeout = $connectionTimeout;
55 }
56
57 /**
58 * Download the remote zipfile.
59 *
60 * @param string $remoteFile
61 * URL of a .zip file.
62 * @param string $localFile
63 * Path at which to store the .zip file.
64 * @return STATUS_OK|STATUS_WRITE_ERROR|STATUS_DL_ERROR
65 */
66 public function fetch($remoteFile, $localFile) {
67 // Download extension zip file ...
68 if (!function_exists('curl_init')) {
69 CRM_Core_Error::fatal('Cannot install this extension - curl is not installed!');
70 }
71
72 list($ch, $caConfig) = $this->createCurl($remoteFile);
73 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
74 CRM_Core_Error::fatal('Cannot install this extension - does not support SSL');
75 }
76
77 $fp = @fopen($localFile, "w");
78 if (!$fp) {
79 return self::STATUS_WRITE_ERROR;
80 }
81 curl_setopt($ch, CURLOPT_FILE, $fp);
82
83 curl_exec($ch);
84 if (curl_errno($ch)) {
85 return self::STATUS_DL_ERROR;
86 }
87 else {
88 curl_close($ch);
89 }
90
91 fclose($fp);
92
93 return self::STATUS_OK;
94 }
95
96 /**
97 * Send an HTTP GET for a remote resource.
98 *
99 * @param string $remoteFile
100 * URL of remote file.
101 * @return array
102 * array(0 => STATUS_OK|STATUS_DL_ERROR, 1 => string)
103 */
104 public function get($remoteFile) {
105 // Download extension zip file ...
106 if (!function_exists('curl_init')) {
107 // CRM-13805
108 CRM_Core_Session::setStatus(
109 ts('As a result, actions like retrieving the CiviCRM news feed will fail. Talk to your server administrator or hosting company to rectify this.'),
110 ts('Curl is not installed')
111 );
112 return [self::STATUS_DL_ERROR, NULL];
113 }
114
115 list($ch, $caConfig) = $this->createCurl($remoteFile);
116
117 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
118 // CRM_Core_Error::fatal('Cannot install this extension - does not support SSL');
119 return [self::STATUS_DL_ERROR, NULL];
120 }
121
122 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
123 $data = curl_exec($ch);
124 if (curl_errno($ch)) {
125 return [self::STATUS_DL_ERROR, $data];
126 }
127 else {
128 curl_close($ch);
129 }
130
131 return [self::STATUS_OK, $data];
132 }
133
134 /**
135 * Send an HTTP POST for a remote resource.
136 *
137 * @param string $remoteFile
138 * URL of a .zip file.
139 * @param array $params
140 *
141 * @return array
142 * array(0 => STATUS_OK|STATUS_DL_ERROR, 1 => string)
143 */
144 public function post($remoteFile, $params) {
145 // Download extension zip file ...
146 if (!function_exists('curl_init')) {
147 //CRM_Core_Error::fatal('Cannot install this extension - curl is not installed!');
148 return [self::STATUS_DL_ERROR, NULL];
149 }
150
151 list($ch, $caConfig) = $this->createCurl($remoteFile);
152
153 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
154 // CRM_Core_Error::fatal('Cannot install this extension - does not support SSL');
155 return [self::STATUS_DL_ERROR, NULL];
156 }
157
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];
165 }
166 else {
167 curl_close($ch);
168 }
169
170 return [self::STATUS_OK, $data];
171 }
172
173 /**
174 * @param string $remoteFile
175 * @return array
176 * (0 => resource, 1 => CA_Config_Curl)
177 */
178 protected function createCurl($remoteFile) {
179 $caConfig = CA_Config_Curl::probe([
180 'verify_peer' => (bool) Civi::settings()->get('verifySSL'),
181 ]);
182
183 $ch = curl_init();
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);
190 }
191 if ($this->connectionTimeout !== NULL) {
192 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout);
193 }
194 if (preg_match('/^https:/', $remoteFile) && $caConfig->isEnableSSL()) {
195 curl_setopt_array($ch, $caConfig->toCurlOptions());
196 }
197
198 return [$ch, $caConfig];
199 }
200
201 /**
202 * @return bool
203 */
204 public function isRedirectSupported() {
205 return (ini_get('open_basedir') == '') && (ini_get('safe_mode') == 'Off' || ini_get('safe_mode') == '' || ini_get('safe_mode') === FALSE);
206 }
207
208 }