Commit | Line | Data |
---|---|---|
6a488035 TO |
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 | * Download the remote zipfile. | |
44 | * | |
45 | * @param string $remoteFile URL of a .zip file | |
46 | * @param string $localFile path at which to store the .zip file | |
47 | * @return STATUS_OK|STATUS_WRITE_ERROR|STATUS_DL_ERROR | |
48 | */ | |
49 | public static function fetch($remoteFile, $localFile) { | |
50 | require_once 'CA/Config/Curl.php'; | |
51 | $caConfig = CA_Config_Curl::probe(array( | |
52 | 'verify_peer' => (bool) CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL', NULL, TRUE) | |
53 | )); | |
54 | ||
55 | // Download extension zip file ... | |
56 | if (!function_exists('curl_init')) { | |
57 | CRM_Core_Error::fatal('Cannot install this extension - curl is not installed!'); | |
58 | } | |
59 | if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) { | |
60 | CRM_Core_Error::fatal('Cannot install this extension - does not support SSL'); | |
61 | } | |
62 | ||
63 | //setting the curl parameters. | |
64 | $ch = curl_init(); | |
65 | curl_setopt($ch, CURLOPT_URL, $remoteFile); | |
66 | curl_setopt($ch, CURLOPT_HEADER, FALSE); | |
67 | curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); | |
68 | curl_setopt($ch, CURLOPT_VERBOSE, 0); | |
69 | if (preg_match('/^https:/', $remoteFile)) { | |
70 | curl_setopt_array($ch, $caConfig->toCurlOptions()); | |
71 | } | |
72 | ||
73 | //follow redirects | |
74 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); | |
75 | ||
76 | $fp = @fopen($localFile, "w"); | |
77 | if (!$fp) { | |
78 | CRM_Core_Session::setStatus(ts('Unable to write to %1.<br />Is the location writable?', array(1 => $localFile)), ts('Write Error'), 'error'); | |
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 | CRM_Core_Session::setStatus(ts('Unable to download extension from %1. Error Message: %2', | |
86 | array(1 => $remoteFile, 2 => curl_error($ch))), ts('Download Error'), 'error'); | |
87 | return self::STATUS_DL_ERROR; | |
88 | } | |
89 | else { | |
90 | curl_close($ch); | |
91 | } | |
92 | ||
93 | fclose($fp); | |
94 | ||
95 | return self::STATUS_OK; | |
96 | } | |
97 | } |