3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 downloads of remotely-provided extensions
32 * @copyright CiviCRM LLC (c) 2004-2013
36 class CRM_Utils_HttpClient
{
38 const STATUS_OK
= 'ok';
39 const STATUS_WRITE_ERROR
= 'write-error';
40 const STATUS_DL_ERROR
= 'dl-error';
43 * @var CRM_Utils_HttpClient
45 protected static $singleton;
47 public static function singleton() {
48 if (!self
::$singleton) {
49 self
::$singleton = new CRM_Utils_HttpClient();
51 return self
::$singleton;
55 * Download the remote zipfile.
57 * @param string $remoteFile URL of a .zip file
58 * @param string $localFile path at which to store the .zip file
59 * @return STATUS_OK|STATUS_WRITE_ERROR|STATUS_DL_ERROR
61 public function fetch($remoteFile, $localFile) {
62 require_once 'CA/Config/Curl.php';
63 $caConfig = CA_Config_Curl
::probe(array(
64 'verify_peer' => (bool) CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL', NULL, TRUE)
67 // Download extension zip file ...
68 if (!function_exists('curl_init')) {
69 CRM_Core_Error
::fatal('Cannot install this extension - curl is not installed!');
71 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
72 CRM_Core_Error
::fatal('Cannot install this extension - does not support SSL');
75 //setting the curl parameters.
77 curl_setopt($ch, CURLOPT_URL
, $remoteFile);
78 curl_setopt($ch, CURLOPT_HEADER
, FALSE);
79 curl_setopt($ch, CURLOPT_ENCODING
, 'gzip');
80 curl_setopt($ch, CURLOPT_VERBOSE
, 0);
81 if (preg_match('/^https:/', $remoteFile)) {
82 curl_setopt_array($ch, $caConfig->toCurlOptions());
86 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, TRUE);
88 $fp = @fopen
($localFile, "w");
90 CRM_Core_Session
::setStatus(ts('Unable to write to %1.<br />Is the location writable?', array(1 => $localFile)), ts('Write Error'), 'error');
91 return self
::STATUS_WRITE_ERROR
;
93 curl_setopt($ch, CURLOPT_FILE
, $fp);
96 if (curl_errno($ch)) {
97 CRM_Core_Session
::setStatus(ts('Unable to download extension from %1. Error Message: %2',
98 array(1 => $remoteFile, 2 => curl_error($ch))), ts('Download Error'), 'error');
99 return self
::STATUS_DL_ERROR
;
107 return self
::STATUS_OK
;