3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 use GuzzleHttp\Exception\GuzzleException
;
15 * This class glues together the various parts of the extension
19 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 class CRM_Extension_Browser
{
24 * An URL for public extensions repository.
26 * Note: This default is now handled through setting/*.php.
30 const DEFAULT_EXTENSIONS_REPOSITORY
= 'https://civicrm.org/extdir/ver={ver}|cms={uf}';
33 * Relative path below remote repository URL for single extensions file.
35 const SINGLE_FILE_PATH
= '/single';
38 * Timeout for when the connection or the server is slow
40 const CHECK_TIMEOUT
= 5;
43 * @var GuzzleHttp\Client
45 protected $guzzleClient;
48 * @return \GuzzleHttp\Client
50 public function getGuzzleClient(): \GuzzleHttp\Client
{
51 return $this->guzzleClient ??
new \GuzzleHttp\
Client();
55 * @param \GuzzleHttp\Client $guzzleClient
57 public function setGuzzleClient(\GuzzleHttp\Client
$guzzleClient) {
58 $this->guzzleClient
= $guzzleClient;
62 * @param string $repoUrl
63 * URL of the remote repository.
64 * @param string $indexPath
65 * Relative path of the 'index' file within the repository.
67 public function __construct($repoUrl, $indexPath) {
68 $this->repoUrl
= $repoUrl;
69 $this->indexPath
= empty($indexPath) ? self
::SINGLE_FILE_PATH
: $indexPath;
73 * Determine whether the system policy allows downloading new extensions.
75 * This is reflection of *policy* and *intent*; it does not indicate whether
76 * the browser will actually *work*. For that, see checkRequirements().
80 public function isEnabled() {
81 return (FALSE !== $this->getRepositoryUrl());
87 public function getRepositoryUrl() {
88 return $this->repoUrl
;
92 * Refresh the cache of remotely-available extensions.
94 public function refresh() {
95 \Civi
::cache('extension_browser')->flush();
99 * Determine whether downloading is supported.
102 * List of error messages; empty if OK.
104 public function checkRequirements() {
105 if (!$this->isEnabled()) {
109 // We used to check for the cache filesystem permissions, but it is now stored in DB
110 // If no new requirements have come up, consider removing this function after CiviCRM 5.60.
111 // The tests may need to be updated as well (tests/phpunit/CRM/Extension/BrowserTest.php).
117 * Get a list of all available extensions.
119 * @return CRM_Extension_Info[]
120 * ($key => CRM_Extension_Info)
122 public function getExtensions() {
123 if (!$this->isEnabled() ||
count($this->checkRequirements())) {
128 $remote = $this->_discoverRemote();
130 if (is_array($remote)) {
131 foreach ($remote as $dc => $e) {
140 * Get a description of a particular extension.
143 * Fully-qualified extension name.
145 * @return CRM_Extension_Info|NULL
147 public function getExtension($key) {
148 // TODO optimize performance -- we don't need to fetch/cache the entire repo
149 $exts = $this->getExtensions();
150 if (array_key_exists($key, $exts)) {
159 * @return CRM_Extension_Info[]
160 * @throws CRM_Extension_Exception_ParseException
162 private function _discoverRemote() {
163 $remotes = json_decode($this->grabCachedJson(), TRUE);
164 $this->_remotesDiscovered
= [];
166 foreach ((array) $remotes as $id => $xml) {
167 $ext = CRM_Extension_Info
::loadFromString($xml);
168 $this->_remotesDiscovered
[] = $ext;
171 return $this->_remotesDiscovered
;
175 * Loads the extensions data from the cache file. If it is empty
176 * or doesn't exist, try fetching from remote instead.
180 private function grabCachedJson() {
181 $cacheKey = $this->getCacheKey();
182 $json = \Civi
::cache('extension_browser')->get($cacheKey);
183 if ($json === NULL) {
184 $json = $this->grabRemoteJson();
190 * Connects to public server and grabs the list of publicly available
194 * @throws \CRM_Extension_Exception
196 private function grabRemoteJson() {
197 set_error_handler(array('CRM_Extension_Browser', 'downloadError'));
199 if (FALSE === $this->getRepositoryUrl()) {
200 // don't check if the user has configured civi not to check an external
201 // url for extensions. See CRM-10575.
205 $url = $this->getRepositoryUrl() . $this->indexPath
;
206 $client = $this->getGuzzleClient();
208 $response = $client->request('GET', $url, [
209 'timeout' => \Civi
::settings()->get('http_timeout'),
212 catch (GuzzleException
$e) {
213 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', [1 => $this->getRepositoryUrl()]), 'connection_error');
215 restore_error_handler();
217 if ($response->getStatusCode() !== 200) {
218 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', [1 => $this->getRepositoryUrl()]), 'connection_error');
221 $json = $response->getBody()->getContents();
222 $cacheKey = $this->getCacheKey();
223 \Civi
::cache('extension_browser')->set($cacheKey, $json);
228 * Returns a cache key based on the repository URL, which can be updated
229 * by admins in civicrm.settings.php or passed as a command-line option to cv.
231 private function getCacheKey() {
232 return 'extdir_' . md5($this->getRepositoryUrl());
236 * A dummy function required for suppressing download errors.
238 * @param $errorNumber
239 * @param $errorString
241 public static function downloadError($errorNumber, $errorString) {