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 +--------------------------------------------------------------------+
13 * This class glues together the various parts of the extension
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Extension_Browser
{
22 * An URL for public extensions repository.
24 * Note: This default is now handled through setting/*.php.
28 const DEFAULT_EXTENSIONS_REPOSITORY
= 'https://civicrm.org/extdir/ver={ver}|cms={uf}';
31 * Relative path below remote repository URL for single extensions file.
33 const SINGLE_FILE_PATH
= '/single';
36 * The name of the single JSON extension cache file.
38 const CACHE_JSON_FILE
= 'extensions.json';
40 // timeout for when the connection or the server is slow
41 const CHECK_TIMEOUT
= 5;
44 * @param string $repoUrl
45 * URL of the remote repository.
46 * @param string $indexPath
47 * Relative path of the 'index' file within the repository.
48 * @param string $cacheDir
49 * Local path in which to cache files.
51 public function __construct($repoUrl, $indexPath, $cacheDir) {
52 $this->repoUrl
= $repoUrl;
53 $this->cacheDir
= $cacheDir;
54 $this->indexPath
= empty($indexPath) ? self
::SINGLE_FILE_PATH
: $indexPath;
55 if ($cacheDir && !file_exists($cacheDir) && is_dir(dirname($cacheDir)) && is_writable(dirname($cacheDir))) {
56 CRM_Utils_File
::createDir($cacheDir, FALSE);
61 * Determine whether the system policy allows downloading new extensions.
63 * This is reflection of *policy* and *intent*; it does not indicate whether
64 * the browser will actually *work*. For that, see checkRequirements().
68 public function isEnabled() {
69 return (FALSE !== $this->getRepositoryUrl());
75 public function getRepositoryUrl() {
76 return $this->repoUrl
;
80 * Refresh the cache of remotely-available extensions.
82 public function refresh() {
83 $file = $this->getTsPath();
84 if (file_exists($file)) {
90 * Determine whether downloading is supported.
93 * List of error messages; empty if OK.
95 public function checkRequirements() {
96 if (!$this->isEnabled()) {
102 if (!$this->cacheDir ||
!is_dir($this->cacheDir
) ||
!is_writable($this->cacheDir
)) {
103 $civicrmDestination = urlencode(CRM_Utils_System
::url('civicrm/admin/extensions', 'reset=1'));
104 $url = CRM_Utils_System
::url('civicrm/admin/setting/path', "reset=1&civicrmDestination=${civicrmDestination}");
106 'title' => ts('Directory Unwritable'),
107 '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/>',
109 1 => $this->cacheDir
,
120 * Get a list of all available extensions.
123 * ($key => CRM_Extension_Info)
125 public function getExtensions() {
126 if (!$this->isEnabled() ||
count($this->checkRequirements())) {
132 $remote = $this->_discoverRemote();
133 if (is_array($remote)) {
134 foreach ($remote as $dc => $e) {
143 * Get a description of a particular extension.
146 * Fully-qualified extension name.
148 * @return CRM_Extension_Info|NULL
150 public function getExtension($key) {
151 // TODO optimize performance -- we don't need to fetch/cache the entire repo
152 $exts = $this->getExtensions();
153 if (array_key_exists($key, $exts)) {
163 * @throws CRM_Extension_Exception_ParseException
165 private function _discoverRemote() {
166 $tsPath = $this->getTsPath();
169 if (file_exists($tsPath)) {
170 $timestamp = file_get_contents($tsPath);
173 // 3 minutes ago for now
174 $outdated = (int) $timestamp < (time() - 180) ?
TRUE : FALSE;
176 if (!$timestamp ||
$outdated) {
177 $remotes = json_decode($this->grabRemoteJson(), TRUE);
180 $remotes = json_decode($this->grabCachedJson(), TRUE);
183 $this->_remotesDiscovered
= [];
184 foreach ((array) $remotes as $id => $xml) {
185 $ext = CRM_Extension_Info
::loadFromString($xml);
186 $this->_remotesDiscovered
[] = $ext;
189 if (file_exists(dirname($tsPath))) {
190 file_put_contents($tsPath, (string) time());
193 return $this->_remotesDiscovered
;
197 * Loads the extensions data from the cache file. If it is empty
198 * or doesn't exist, try fetching from remote instead.
202 private function grabCachedJson() {
203 $filename = $this->cacheDir
. DIRECTORY_SEPARATOR
. self
::CACHE_JSON_FILE
. '.' . md5($this->getRepositoryUrl());
205 if (file_exists($filename)) {
206 $json = file_get_contents($filename);
209 $json = $this->grabRemoteJson();
215 * Connects to public server and grabs the list of publicly available
219 * @throws \CRM_Extension_Exception
221 private function grabRemoteJson() {
223 ini_set('default_socket_timeout', self
::CHECK_TIMEOUT
);
224 set_error_handler(array('CRM_Extension_Browser', 'downloadError'));
226 if (!ini_get('allow_url_fopen')) {
227 ini_set('allow_url_fopen', 1);
230 if (FALSE === $this->getRepositoryUrl()) {
231 // don't check if the user has configured civi not to check an external
232 // url for extensions. See CRM-10575.
236 $filename = $this->cacheDir
. DIRECTORY_SEPARATOR
. self
::CACHE_JSON_FILE
. '.' . md5($this->getRepositoryUrl());
237 $url = $this->getRepositoryUrl() . $this->indexPath
;
238 $status = CRM_Utils_HttpClient
::singleton()->fetch($url, $filename);
240 ini_restore('allow_url_fopen');
241 ini_restore('default_socket_timeout');
243 restore_error_handler();
245 if ($status !== CRM_Utils_HttpClient
::STATUS_OK
) {
246 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');
249 // Don't call grabCachedJson here, that would risk infinite recursion
250 return file_get_contents($filename);
256 private function getTsPath() {
257 return $this->cacheDir
. DIRECTORY_SEPARATOR
. 'timestamp.txt';
261 * A dummy function required for suppressing download errors.
263 * @param $errorNumber
264 * @param $errorString
266 public static function downloadError($errorNumber, $errorString) {