forked from civicrm/civicrm-core
/
Browser.php
339 lines (299 loc) · 10 KB
/
Browser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* This class glues together the various parts of the extension
* system.
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Extension_Browser {
/**
* An URL for public extensions repository.
*
* Note: This default is now handled through setting/*.php.
*
* @deprecated
*/
const DEFAULT_EXTENSIONS_REPOSITORY = 'https://civicrm.org/extdir/ver={ver}|cms={uf}';
/**
* Relative path below remote repository URL for single extensions file.
*/
const SINGLE_FILE_PATH = '/single';
/**
* The name of the single JSON extension cache file.
*/
const CACHE_JSON_FILE = 'extensions.json';
// timeout for when the connection or the server is slow
const CHECK_TIMEOUT = 5;
/**
* @var CRM_Utils_HttpClient
*/
protected $client;
/**
* @var CRM_Utils_Cache_Interface
*/
protected $cache;
/**
* Create default instance.
*
* @return CRM_Extension_Browser
*/
public static function create() {
return new CRM_Extension_Browser(
Civi::cache('extension_browser'),
CRM_Utils_HttpClient::singleton()
);
}
/**
* @param string $repoUrl
* URL of the remote repository.
* @param string $indexPath
* Relative path of the 'index' file within the repository.
* @param string $cacheDir
* Local path in which to cache files.
* @param CRM_Utils_Cache_Interface $cache
* @param CRM_Utils_HttpClient $client
*/
public function __construct($cache, $client, $repoUrl, $indexPath) {
$this->repoUrl = $repoUrl;
// $this->cacheDir = $cacheDir;
$this->cache = $cache;
$this->client = $client;
$this->indexPath = empty($indexPath) ? self::SINGLE_FILE_PATH : $indexPath;
// if ($cacheDir && !file_exists($cacheDir) && is_dir(dirname($cacheDir)) && is_writable(dirname($cacheDir))) {
// CRM_Utils_File::createDir($cacheDir, FALSE);
// }
}
/**
* Determine whether the system policy allows downloading new extensions.
*
* This is reflection of *policy* and *intent*; it does not indicate whether
* the browser will actually *work*. For that, see checkRequirements().
*
* @return bool
*/
public function isEnabled() {
return (FALSE !== $this->getRepositoryUrl());
}
/**
* @return string
*/
public function getRepositoryUrl() {
return $this->repoUrl;
}
/**
* Refresh the cache of remotely-available extensions.
*/
public function refresh() {
$file = $this->getTsPath();
if (file_exists($file)) {
unlink($file);
}
}
/**
* Determine whether downloading is supported.
*
* @return array
* List of error messages; empty if OK.
*/
// public function checkRequirements() {
// if (!$this->isEnabled()) {
// return array();
// }
//
// $errors = array();
//
// if (!$this->cacheDir || !is_dir($this->cacheDir) || !is_writable($this->cacheDir)) {
// $civicrmDestination = urlencode(CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1'));
// $url = CRM_Utils_System::url('civicrm/admin/setting/path', "reset=1&civicrmDestination=${civicrmDestination}");
// $errors[] = array(
// 'title' => ts('Directory Unwritable'),
// 'message' => ts('Your extensions cache directory (%1) is not web server writable. Please go to the <a href="%2">path setting page</a> and correct it.<br/>',
// array(
// 1 => $this->cacheDir,
// 2 => $url,
// )
// ),
// );
// }
//
// return $errors;
// }
/**
* Get a list of all available extensions.
*
* @return array
* ($key => CRM_Extension_Info)
*/
public function getExtensions() {
if (!$this->isEnabled() || count($this->checkRequirements())) {
return array();
}
$exts = array();
$remote = $this->_discoverRemote();
if (is_array($remote)) {
foreach ($remote as $dc => $e) {
$exts[$e->key] = $e;
}
}
return $exts;
}
/**
* Get a description of a particular extension.
*
* @param string $key
* Fully-qualified extension name.
*
* @return CRM_Extension_Info|NULL
*/
public function getExtension($key) {
// TODO optimize performance -- we don't need to fetch/cache the entire repo
$exts = $this->getExtensions();
if (array_key_exists($key, $exts)) {
return $exts[$key];
}
else {
return NULL;
}
}
/**
* @return array
* @throws CRM_Extension_Exception_ParseException
*/
private function _discoverRemote() {
$tsPath = $this->getTsPath();
$timestamp = FALSE;
if (file_exists($tsPath)) {
$timestamp = file_get_contents($tsPath);
}
// 3 minutes ago for now
$outdated = (int) $timestamp < (time() - 180) ? TRUE : FALSE;
if (!$timestamp || $outdated) {
$remotes = json_decode($this->grabRemoteJson(), TRUE);
}
else {
$remotes = json_decode($this->grabCachedJson(), TRUE);
}
$this->_remotesDiscovered = array();
foreach ((array) $remotes as $id => $xml) {
$ext = CRM_Extension_Info::loadFromString($xml);
$this->_remotesDiscovered[] = $ext;
}
if (file_exists(dirname($tsPath))) {
file_put_contents($tsPath, (string) time());
}
return $this->_remotesDiscovered;
}
/**
* Loads the extensions data from the cache file. If it is empty
* or doesn't exist, try fetching from remote instead.
*
* @return string
*/
// private function grabCachedJson() {
// $filename = $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_JSON_FILE . '.' . md5($this->getRepositoryUrl());
// $json = NULL;
// if (file_exists($filename)) {
// $json = file_get_contents($filename);
// }
// if (empty($json)) {
// $json = $this->grabRemoteJson();
// }
// return $json;
// }
public function grabExtCache() {
$isChanged = FALSE;
$extension = $this->cache->get('Extension_Browser');
if (empty($extension) || !is_array($extension)) {
$extension = array(
'expires' => 0, // ASAP
'ttl' => self::DEFAULT_RETRY,
'retry' => self::DEFAULT_RETRY,
);
$isChanged = TRUE;
}
if ($extension['expires'] <= CRM_Utils_Time::getTimeRaw()) {
$newExtension = $this->grabRemoteJson();
$extension = $newExtension;
$extension['expires'] = CRM_Utils_Time::getTimeRaw() + $extension['ttl'];
else {
// keep the old extensions for now, try again later
$extension['expires'] = CRM_Utils_Time::getTimeRaw() + $extension['retry'];
}
$isChanged = TRUE;
}
if ($isChanged) {
$this->cache->set('Extension_Browser', $extension);
}
return $extension;
}
/**
* Connects to public server and grabs the list of publicly available
* extensions.
*
* @return string
* @throws \CRM_Extension_Exception
*/
private function grabRemoteJson() {
ini_set('default_socket_timeout', self::CHECK_TIMEOUT);
set_error_handler(array('CRM_Extension_Browser', 'downloadError'));
if (!ini_get('allow_url_fopen')) {
ini_set('allow_url_fopen', 1);
}
if (FALSE === $this->getRepositoryUrl()) {
// don't check if the user has configured civi not to check an external
// url for extensions. See CRM-10575.
return array();
}
$filename = $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_JSON_FILE . '.' . md5($this->getRepositoryUrl());
$url = $this->getRepositoryUrl() . $this->indexPath;
$status = CRM_Utils_HttpClient::singleton()->fetch($url, $filename);
ini_restore('allow_url_fopen');
ini_restore('default_socket_timeout');
restore_error_handler();
if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
throw new CRM_Extension_Exception(ts('The CiviCRM public extensions directory at %1 could not be contacted - please check your webserver can make external HTTP requests or contact CiviCRM team on <a href="http://forum.civicrm.org/">CiviCRM forum</a>.', array(1 => $this->getRepositoryUrl())), 'connection_error');
}
// Don't call grabCachedJson here, that would risk infinite recursion
return file_get_contents($filename);
}
/**
* @return string
*/
private function getTsPath() {
return $this->cacheDir . DIRECTORY_SEPARATOR . 'timestamp.txt';
}
/**
* A dummy function required for suppressing download errors.
*
* @param $errorNumber
* @param $errorString
*/
public static function downloadError($errorNumber, $errorString) {
}
}