CRM_Utils_HttpClient - Change static method (in preparation for using mock clients...
[civicrm-core.git] / CRM / Utils / HttpClient.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This class handles downloads of remotely-provided extensions
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36 class CRM_Utils_HttpClient {
37
38 const STATUS_OK = 'ok';
39 const STATUS_WRITE_ERROR = 'write-error';
40 const STATUS_DL_ERROR = 'dl-error';
41
42 /**
43 * @var CRM_Utils_HttpClient
44 */
45 protected static $singleton;
46
47 public static function singleton() {
48 if (!self::$singleton) {
49 self::$singleton = new CRM_Utils_HttpClient();
50 }
51 return self::$singleton;
52 }
53
54 /**
55 * Download the remote zipfile.
56 *
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
60 */
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)
65 ));
66
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 if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) {
72 CRM_Core_Error::fatal('Cannot install this extension - does not support SSL');
73 }
74
75 //setting the curl parameters.
76 $ch = curl_init();
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());
83 }
84
85 //follow redirects
86 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
87
88 $fp = @fopen($localFile, "w");
89 if (!$fp) {
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;
92 }
93 curl_setopt($ch, CURLOPT_FILE, $fp);
94
95 curl_exec($ch);
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;
100 }
101 else {
102 curl_close($ch);
103 }
104
105 fclose($fp);
106
107 return self::STATUS_OK;
108 }
109 }