CRM_Core_BAO_Setting::getItem() - Fix calls which use deprecated parameters
[civicrm-core.git] / CRM / Utils / HttpClient.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
e647e388
TO
29 * This class handles HTTP downloads
30 *
31 * FIXME: fetch() and get() report errors differently -- e.g.
32 * fetch() returns fatal and get() returns an error code. Should
33 * refactor both (or get a third-party HTTP library) but don't
34 * want to deal with that so late in the 4.3 dev cycle.
6a488035
TO
35 *
36 * @package CRM
e7112fa7 37 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
38 * $Id$
39 *
40 */
41class CRM_Utils_HttpClient {
42
43 const STATUS_OK = 'ok';
44 const STATUS_WRITE_ERROR = 'write-error';
45 const STATUS_DL_ERROR = 'dl-error';
46
3b6f287b
TO
47 /**
48 * @var CRM_Utils_HttpClient
49 */
50 protected static $singleton;
51
afcc9be0 52 /**
021cfda1 53 * @var int|NULL seconds; or NULL to use system default
afcc9be0 54 */
021cfda1 55 protected $connectionTimeout;
afcc9be0 56
5bc392e6
EM
57 /**
58 * @return CRM_Utils_HttpClient
59 */
3b6f287b
TO
60 public static function singleton() {
61 if (!self::$singleton) {
62 self::$singleton = new CRM_Utils_HttpClient();
63 }
64 return self::$singleton;
65 }
66
5bc392e6
EM
67 /**
68 * @param null $connectionTimeout
69 */
021cfda1
TO
70 public function __construct($connectionTimeout = NULL) {
71 $this->connectionTimeout = $connectionTimeout;
afcc9be0
TO
72 }
73
6a488035
TO
74 /**
75 * Download the remote zipfile.
76 *
77855840
TO
77 * @param string $remoteFile
78 * URL of a .zip file.
79 * @param string $localFile
80 * Path at which to store the .zip file.
6a488035
TO
81 * @return STATUS_OK|STATUS_WRITE_ERROR|STATUS_DL_ERROR
82 */
3b6f287b 83 public function fetch($remoteFile, $localFile) {
6a488035
TO
84 // Download extension zip file ...
85 if (!function_exists('curl_init')) {
86 CRM_Core_Error::fatal('Cannot install this extension - curl is not installed!');
87 }
e647e388
TO
88
89 list($ch, $caConfig) = $this->createCurl($remoteFile);
6a488035
TO
90 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
91 CRM_Core_Error::fatal('Cannot install this extension - does not support SSL');
92 }
93
6a488035
TO
94 $fp = @fopen($localFile, "w");
95 if (!$fp) {
96 CRM_Core_Session::setStatus(ts('Unable to write to %1.<br />Is the location writable?', array(1 => $localFile)), ts('Write Error'), 'error');
97 return self::STATUS_WRITE_ERROR;
98 }
99 curl_setopt($ch, CURLOPT_FILE, $fp);
100
101 curl_exec($ch);
102 if (curl_errno($ch)) {
103 CRM_Core_Session::setStatus(ts('Unable to download extension from %1. Error Message: %2',
104 array(1 => $remoteFile, 2 => curl_error($ch))), ts('Download Error'), 'error');
105 return self::STATUS_DL_ERROR;
106 }
107 else {
108 curl_close($ch);
109 }
110
111 fclose($fp);
112
113 return self::STATUS_OK;
114 }
afcc9be0
TO
115
116 /**
fe482240 117 * Send an HTTP GET for a remote resource.
afcc9be0 118 *
77855840
TO
119 * @param string $remoteFile
120 * URL of remote file.
a6c01b45
CW
121 * @return array
122 * array(0 => STATUS_OK|STATUS_DL_ERROR, 1 => string)
afcc9be0
TO
123 */
124 public function get($remoteFile) {
afcc9be0
TO
125 // Download extension zip file ...
126 if (!function_exists('curl_init')) {
dadcd1ac
FG
127 // CRM-13805
128 CRM_Core_Session::setStatus(
129 ts('As a result, actions like retrieving the CiviCRM news feed will fail. Talk to your server administrator or hosting company to rectify this.'),
130 ts('Curl is not installed')
131 );
afcc9be0
TO
132 return array(self::STATUS_DL_ERROR, NULL);
133 }
e647e388
TO
134
135 list($ch, $caConfig) = $this->createCurl($remoteFile);
136
afcc9be0
TO
137 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
138 //CRM_Core_Error::fatal('Cannot install this extension - does not support SSL');
139 return array(self::STATUS_DL_ERROR, NULL);
140 }
141
afcc9be0 142 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
afcc9be0
TO
143 $data = curl_exec($ch);
144 if (curl_errno($ch)) {
2b5a6dbf 145 return array(self::STATUS_DL_ERROR, $data);
afcc9be0
TO
146 }
147 else {
148 curl_close($ch);
149 }
150
151 return array(self::STATUS_OK, $data);
e647e388
TO
152 }
153
49626e3d 154 /**
fe482240 155 * Send an HTTP POST for a remote resource.
49626e3d 156 *
77855840
TO
157 * @param string $remoteFile
158 * URL of a .zip file.
c490a46a 159 * @param array $params
f4aaa82a 160 *
a6c01b45
CW
161 * @return array
162 * array(0 => STATUS_OK|STATUS_DL_ERROR, 1 => string)
49626e3d
CW
163 */
164 public function post($remoteFile, $params) {
165 // Download extension zip file ...
166 if (!function_exists('curl_init')) {
167 //CRM_Core_Error::fatal('Cannot install this extension - curl is not installed!');
168 return array(self::STATUS_DL_ERROR, NULL);
169 }
170
171 list($ch, $caConfig) = $this->createCurl($remoteFile);
172
173 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
174 //CRM_Core_Error::fatal('Cannot install this extension - does not support SSL');
175 return array(self::STATUS_DL_ERROR, NULL);
176 }
177
178 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
e7292422
TO
179 curl_setopt($ch, CURLOPT_POST, TRUE);
180 curl_setopt($ch, CURLOPT_POST, count($params));
181 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
49626e3d
CW
182 $data = curl_exec($ch);
183 if (curl_errno($ch)) {
2b5a6dbf 184 return array(self::STATUS_DL_ERROR, $data);
49626e3d
CW
185 }
186 else {
187 curl_close($ch);
188 }
189
190 return array(self::STATUS_OK, $data);
191 }
192
e647e388
TO
193 /**
194 * @param string $remoteFile
a6c01b45
CW
195 * @return array
196 * (0 => resource, 1 => CA_Config_Curl)
e647e388
TO
197 */
198 protected function createCurl($remoteFile) {
e647e388 199 $caConfig = CA_Config_Curl::probe(array(
d356cdeb 200 'verify_peer' => (bool) CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL'),
e647e388
TO
201 ));
202
203 $ch = curl_init();
204 curl_setopt($ch, CURLOPT_URL, $remoteFile);
205 curl_setopt($ch, CURLOPT_HEADER, FALSE);
206 curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
207 curl_setopt($ch, CURLOPT_VERBOSE, 0);
eb066397 208 if ($this->isRedirectSupported()) {
439b9688
LS
209 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
210 }
021cfda1
TO
211 if ($this->connectionTimeout !== NULL) {
212 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout);
213 }
e647e388
TO
214 if (preg_match('/^https:/', $remoteFile) && $caConfig->isEnableSSL()) {
215 curl_setopt_array($ch, $caConfig->toCurlOptions());
216 }
afcc9be0 217
e647e388 218 return array($ch, $caConfig);
afcc9be0
TO
219 }
220
5bc392e6
EM
221 /**
222 * @return bool
223 */
eb066397 224 public function isRedirectSupported() {
8c633404 225 return (ini_get('open_basedir') == '') && (ini_get('safe_mode') == 'Off' || ini_get('safe_mode') == '' || ini_get('safe_mode') === FALSE);
eb066397
TO
226 }
227
6a488035 228}