3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This class handles HTTP downloads
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.
37 * @copyright CiviCRM LLC (c) 2004-2014
41 class CRM_Utils_HttpClient
{
43 const STATUS_OK
= 'ok';
44 const STATUS_WRITE_ERROR
= 'write-error';
45 const STATUS_DL_ERROR
= 'dl-error';
48 * @var CRM_Utils_HttpClient
50 protected static $singleton;
53 * @var int|NULL seconds; or NULL to use system default
55 protected $connectionTimeout;
58 * @return CRM_Utils_HttpClient
60 public static function singleton() {
61 if (!self
::$singleton) {
62 self
::$singleton = new CRM_Utils_HttpClient();
64 return self
::$singleton;
68 * @param null $connectionTimeout
70 public function __construct($connectionTimeout = NULL) {
71 $this->connectionTimeout
= $connectionTimeout;
75 * Download the remote zipfile.
77 * @param string $remoteFile URL of a .zip file
78 * @param string $localFile path at which to store the .zip file
79 * @return STATUS_OK|STATUS_WRITE_ERROR|STATUS_DL_ERROR
81 public function fetch($remoteFile, $localFile) {
82 // Download extension zip file ...
83 if (!function_exists('curl_init')) {
84 CRM_Core_Error
::fatal('Cannot install this extension - curl is not installed!');
87 list($ch, $caConfig) = $this->createCurl($remoteFile);
88 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
89 CRM_Core_Error
::fatal('Cannot install this extension - does not support SSL');
92 $fp = @fopen
($localFile, "w");
94 CRM_Core_Session
::setStatus(ts('Unable to write to %1.<br />Is the location writable?', array(1 => $localFile)), ts('Write Error'), 'error');
95 return self
::STATUS_WRITE_ERROR
;
97 curl_setopt($ch, CURLOPT_FILE
, $fp);
100 if (curl_errno($ch)) {
101 CRM_Core_Session
::setStatus(ts('Unable to download extension from %1. Error Message: %2',
102 array(1 => $remoteFile, 2 => curl_error($ch))), ts('Download Error'), 'error');
103 return self
::STATUS_DL_ERROR
;
111 return self
::STATUS_OK
;
115 * Send an HTTP GET for a remote resource
117 * @param string $remoteFile URL of remote file
118 * @return array array(0 => STATUS_OK|STATUS_DL_ERROR, 1 => string)
120 public function get($remoteFile) {
121 // Download extension zip file ...
122 if (!function_exists('curl_init')) {
124 CRM_Core_Session
::setStatus(
125 ts('As a result, actions like retrieving the CiviCRM news feed will fail. Talk to your server administrator or hosting company to rectify this.'),
126 ts('Curl is not installed')
128 return array(self
::STATUS_DL_ERROR
, NULL);
131 list($ch, $caConfig) = $this->createCurl($remoteFile);
133 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
134 //CRM_Core_Error::fatal('Cannot install this extension - does not support SSL');
135 return array(self
::STATUS_DL_ERROR
, NULL);
138 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
139 $data = curl_exec($ch);
140 if (curl_errno($ch)) {
141 return array(self
::STATUS_DL_ERROR
, $data);
147 return array(self
::STATUS_OK
, $data);
151 * Send an HTTP POST for a remote resource
153 * @param string $remoteFile URL of a .zip file
154 * @param array $params
156 * @return array array(0 => STATUS_OK|STATUS_DL_ERROR, 1 => string)
158 public function post($remoteFile, $params) {
159 // Download extension zip file ...
160 if (!function_exists('curl_init')) {
161 //CRM_Core_Error::fatal('Cannot install this extension - curl is not installed!');
162 return array(self
::STATUS_DL_ERROR
, NULL);
165 list($ch, $caConfig) = $this->createCurl($remoteFile);
167 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
168 //CRM_Core_Error::fatal('Cannot install this extension - does not support SSL');
169 return array(self
::STATUS_DL_ERROR
, NULL);
172 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
173 curl_setopt($ch, CURLOPT_POST
, true);
174 curl_setopt($ch, CURLOPT_POST
,count($params));
175 curl_setopt($ch, CURLOPT_POSTFIELDS
,$params);
176 $data = curl_exec($ch);
177 if (curl_errno($ch)) {
178 return array(self
::STATUS_DL_ERROR
, $data);
184 return array(self
::STATUS_OK
, $data);
188 * @param string $remoteFile
189 * @return array (0 => resource, 1 => CA_Config_Curl)
191 protected function createCurl($remoteFile) {
192 require_once 'CA/Config/Curl.php';
193 $caConfig = CA_Config_Curl
::probe(array(
194 'verify_peer' => (bool) CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL', NULL, TRUE)
198 curl_setopt($ch, CURLOPT_URL
, $remoteFile);
199 curl_setopt($ch, CURLOPT_HEADER
, FALSE);
200 curl_setopt($ch, CURLOPT_ENCODING
, 'gzip');
201 curl_setopt($ch, CURLOPT_VERBOSE
, 0);
202 if ($this->isRedirectSupported()) {
203 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, TRUE);
205 if ($this->connectionTimeout
!== NULL) {
206 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, $this->connectionTimeout
);
208 if (preg_match('/^https:/', $remoteFile) && $caConfig->isEnableSSL()) {
209 curl_setopt_array($ch, $caConfig->toCurlOptions());
212 return array($ch, $caConfig);
218 public function isRedirectSupported() {
219 return (ini_get('open_basedir') == '') && (ini_get('safe_mode') == 'Off' ||
ini_get('safe_mode') == '' ||
ini_get('safe_mode') === FALSE);