Merge pull request #12683 from civicrm/5.5
[civicrm-core.git] / CRM / Extension / Downloader.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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/**
29 * This class handles downloads of remotely-provided extensions
30 *
31 * @package CRM
8c9251b3 32 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
33 */
34class CRM_Extension_Downloader {
35 /**
36 * @var CRM_Extension_Container_Basic the place where downloaded extensions are ultimately stored
37 */
38 public $container;
39
40 /**
41 * @var string local path to a temporary data directory
42 */
43 public $tmpDir;
44
45 /**
fd31fa4c 46 * @param CRM_Extension_Manager $manager
f41911fd
TO
47 * @param string $containerDir
48 * The place to store downloaded & extracted extensions.
6a488035
TO
49 * @param string $tmpDir
50 */
51 public function __construct(CRM_Extension_Manager $manager, $containerDir, $tmpDir) {
52 $this->manager = $manager;
53 $this->containerDir = $containerDir;
54 $this->tmpDir = $tmpDir;
55 }
56
57 /**
fe482240 58 * Determine whether downloading is supported.
6a488035 59 *
a6c01b45
CW
60 * @return array
61 * list of error messages; empty if OK
6a488035
TO
62 */
63 public function checkRequirements() {
64 $errors = array();
65
22e263ad 66 if (!$this->containerDir || !is_dir($this->containerDir) || !is_writable($this->containerDir)) {
6a488035
TO
67 $civicrmDestination = urlencode(CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1'));
68 $url = CRM_Utils_System::url('civicrm/admin/setting/path', "reset=1&civicrmDestination=${civicrmDestination}");
69 $errors[] = array(
70 'title' => ts('Directory Unwritable'),
6a488035
TO
71 'message' => ts("Your extensions directory is not set or is not writable. Click <a href='%1'>here</a> to set the extensions directory.",
72 array(
6a488035
TO
73 1 => $url,
74 )
21dfd5f5 75 ),
6a488035
TO
76 );
77 }
78
79 if (!class_exists('ZipArchive')) {
80 $errors[] = array(
81 'title' => ts('ZIP Support Required'),
82 'message' => ts('You will not be able to install extensions at this time because your installation of PHP does not support ZIP archives. Please ask your system administrator to install the standard PHP-ZIP extension.'),
83 );
84 }
85
353ffa53 86 if (empty($errors) && !CRM_Utils_HttpClient::singleton()->isRedirectSupported()) {
eb066397
TO
87 CRM_Core_Session::setStatus(ts('WARNING: The downloader may be unable to download files which require HTTP redirection. This may be a configuration issue with PHP\'s open_basedir or safe_mode.'));
88 CRM_Core_Error::debug_log_message('WARNING: The downloader may be unable to download files which require HTTP redirection. This may be a configuration issue with PHP\'s open_basedir or safe_mode.');
89 }
90
6a488035
TO
91 return $errors;
92 }
93
94 /**
fe482240 95 * Install or upgrade an extension from a remote URL.
6a488035 96 *
f41911fd
TO
97 * @param string $key
98 * The name of the extension being installed.
99 * @param string $downloadUrl
100 * URL of a .zip file.
a6c01b45
CW
101 * @return bool
102 * TRUE for success
6a488035
TO
103 * @throws CRM_Extension_Exception
104 */
105 public function download($key, $downloadUrl) {
106 $filename = $this->tmpDir . DIRECTORY_SEPARATOR . $key . '.zip';
107 $destDir = $this->containerDir . DIRECTORY_SEPARATOR . $key;
108
109 if (!$downloadUrl) {
110 CRM_Core_Error::fatal('Cannot install this extension - downloadUrl is not set!');
111 }
112
353ffa53 113 if (!$this->fetch($downloadUrl, $filename)) {
6a488035
TO
114 return FALSE;
115 }
116
117 $extractedZipPath = $this->extractFiles($key, $filename);
353ffa53 118 if (!$extractedZipPath) {
6a488035
TO
119 return FALSE;
120 }
121
353ffa53 122 if (!$this->validateFiles($key, $extractedZipPath)) {
6a488035
TO
123 return FALSE;
124 }
125
126 $this->manager->replace($extractedZipPath);
127
128 return TRUE;
129 }
130
131 /**
132 * Download the remote zipfile.
133 *
f41911fd
TO
134 * @param string $remoteFile
135 * URL of a .zip file.
136 * @param string $localFile
137 * Path at which to store the .zip file.
ae5ffbb7 138 * @return bool
a6c01b45 139 * Whether the download was successful.
6a488035
TO
140 */
141 public function fetch($remoteFile, $localFile) {
3b6f287b 142 $result = CRM_Utils_HttpClient::singleton()->fetch($remoteFile, $localFile);
6a488035
TO
143 switch ($result) {
144 case CRM_Utils_HttpClient::STATUS_OK:
145 return TRUE;
b3a4b879 146
6a488035
TO
147 default:
148 return FALSE;
149 }
150 }
151
152 /**
fe482240 153 * Extract an extension from a zip file.
6a488035 154 *
f41911fd
TO
155 * @param string $key
156 * The name of the extension being installed; this usually matches the basedir in the .zip.
157 * @param string $zipFile
158 * The local path to a .zip file.
72b3a70c
CW
159 * @return string|FALSE
160 * zip file path
6a488035
TO
161 */
162 public function extractFiles($key, $zipFile) {
163 $config = CRM_Core_Config::singleton();
164
165 $zip = new ZipArchive();
166 $res = $zip->open($zipFile);
167 if ($res === TRUE) {
168 $zipSubDir = CRM_Utils_Zip::guessBasedir($zip, $key);
169 if ($zipSubDir === FALSE) {
170 CRM_Core_Session::setStatus(ts('Unable to extract the extension: bad directory structure'), '', 'error');
171 return FALSE;
172 }
173 $extractedZipPath = $this->tmpDir . DIRECTORY_SEPARATOR . $zipSubDir;
174 if (is_dir($extractedZipPath)) {
175 if (!CRM_Utils_File::cleanDir($extractedZipPath, TRUE, FALSE)) {
176 CRM_Core_Session::setStatus(ts('Unable to extract the extension: %1 cannot be cleared', array(1 => $extractedZipPath)), ts('Installation Error'), 'error');
177 return FALSE;
178 }
179 }
180 if (!$zip->extractTo($this->tmpDir)) {
181 CRM_Core_Session::setStatus(ts('Unable to extract the extension to %1.', array(1 => $this->tmpDir)), ts('Installation Error'), 'error');
182 return FALSE;
183 }
184 $zip->close();
185 }
186 else {
187 CRM_Core_Session::setStatus(ts('Unable to extract the extension.'), '', 'error');
188 return FALSE;
189 }
190
191 return $extractedZipPath;
192 }
193
194 /**
195 * Validate that $extractedZipPath contains valid for extension $key
196 *
da6b46f4
EM
197 * @param $key
198 * @param $extractedZipPath
199 *
6a488035
TO
200 * @return bool
201 */
00be9182 202 public function validateFiles($key, $extractedZipPath) {
6a488035
TO
203 $filename = $extractedZipPath . DIRECTORY_SEPARATOR . CRM_Extension_Info::FILENAME;
204 if (!is_readable($filename)) {
205 CRM_Core_Session::setStatus(ts('Failed reading data from %1 during installation', array(1 => $filename)), ts('Installation Error'), 'error');
206 return FALSE;
207 }
208
209 try {
210 $newInfo = CRM_Extension_Info::loadFromFile($filename);
0db6c3e1
TO
211 }
212 catch (Exception $e) {
6a488035
TO
213 CRM_Core_Session::setStatus(ts('Failed reading data from %1 during installation', array(1 => $filename)), ts('Installation Error'), 'error');
214 return FALSE;
215 }
216
217 if ($newInfo->key != $key) {
218 CRM_Core_Error::fatal('Cannot install - there are differences between extdir XML file and archive XML file!');
219 }
220
221 return TRUE;
222 }
223
224}