3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This class glues together the various parts of the extension
33 * @copyright CiviCRM LLC (c) 2004-2015
37 class CRM_Extension_Browser
{
40 * An URL for public extensions repository.
42 const DEFAULT_EXTENSIONS_REPOSITORY
= 'https://civicrm.org/extdir/ver={ver}|cms={uf}';
45 * Relative path below remote repository URL for single extensions file.
47 const SINGLE_FILE_PATH
= '/single';
50 * The name of the single JSON extension cache file.
52 const CACHE_JSON_FILE
= 'extensions.json';
55 * @param string $repoUrl
56 * URL of the remote repository.
57 * @param string $indexPath
58 * Relative path of the 'index' file within the repository.
59 * @param string $cacheDir
60 * Local path in which to cache files.
62 public function __construct($repoUrl, $indexPath, $cacheDir) {
63 $this->repoUrl
= $repoUrl;
64 $this->cacheDir
= $cacheDir;
65 $this->indexPath
= empty($indexPath) ? self
::SINGLE_FILE_PATH
: $indexPath;
66 if ($cacheDir && !file_exists($cacheDir) && is_dir(dirname($cacheDir)) && is_writable(dirname($cacheDir))) {
67 CRM_Utils_File
::createDir($cacheDir, FALSE);
72 * Determine whether the system policy allows downloading new extensions.
74 * This is reflection of *policy* and *intent*; it does not indicate whether
75 * the browser will actually *work*. For that, see checkRequirements().
79 public function isEnabled() {
80 return (FALSE !== $this->getRepositoryUrl());
86 public function getRepositoryUrl() {
87 return $this->repoUrl
;
91 * Refresh the cache of remotely-available extensions.
93 public function refresh() {
94 $file = $this->getTsPath();
95 if (file_exists($file)) {
101 * Determine whether downloading is supported.
104 * List of error messages; empty if OK.
106 public function checkRequirements() {
107 if (!$this->isEnabled()) {
113 if (!$this->cacheDir ||
!is_dir($this->cacheDir
) ||
!is_writable($this->cacheDir
)) {
114 $civicrmDestination = urlencode(CRM_Utils_System
::url('civicrm/admin/extensions', 'reset=1'));
115 $url = CRM_Utils_System
::url('civicrm/admin/setting/path', "reset=1&civicrmDestination=${civicrmDestination}");
117 'title' => ts('Directory Unwritable'),
118 '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/>',
120 1 => $this->cacheDir
,
131 * Get a list of all available extensions.
134 * ($key => CRM_Extension_Info)
136 public function getExtensions() {
137 if (!$this->isEnabled() ||
count($this->checkRequirements())) {
143 $remote = $this->_discoverRemote();
144 if (is_array($remote)) {
145 foreach ($remote as $dc => $e) {
154 * Get a description of a particular extension.
157 * Fully-qualified extension name.
159 * @return CRM_Extension_Info|NULL
161 public function getExtension($key) {
162 // TODO optimize performance -- we don't need to fetch/cache the entire repo
163 $exts = $this->getExtensions();
164 if (array_key_exists($key, $exts)) {
168 // throw new CRM_Extension_Exception("Unknown remote extension: $key");
175 * @throws CRM_Extension_Exception_ParseException
177 private function _discoverRemote() {
178 $tsPath = $this->getTsPath();
181 if (file_exists($tsPath)) {
182 $timestamp = file_get_contents($tsPath);
185 // 3 minutes ago for now
186 $outdated = (int) $timestamp < (time() - 180) ?
TRUE : FALSE;
188 if (!$timestamp ||
$outdated) {
189 $remotes = json_decode($this->grabRemoteJson(), TRUE);
192 $remotes = json_decode($this->grabCachedJson(), TRUE);
195 $this->_remotesDiscovered
= array();
196 foreach ($remotes as $id => $xml) {
197 $ext = CRM_Extension_Info
::loadFromString($xml);
198 $this->_remotesDiscovered
[] = $ext;
201 if (file_exists(dirname($tsPath))) {
202 file_put_contents($tsPath, (string) time());
205 return $this->_remotesDiscovered
;
209 * Loads the extensions data from the cache file. If it is empty
210 * or doesn't exist, try fetching from remote instead.
214 private function grabCachedJson() {
215 $filename = $this->cacheDir
. DIRECTORY_SEPARATOR
. self
::CACHE_JSON_FILE
;
216 $json = file_get_contents($filename);
218 $json = $this->grabRemoteJson();
224 * Connects to public server and grabs the list of publicly available
229 private function grabRemoteJson() {
231 ini_set('default_socket_timeout', CRM_Utils_VersionCheck
::CHECK_TIMEOUT
);
232 set_error_handler(array('CRM_Extension_Browser', 'downloadError'));
234 if (!ini_get('allow_url_fopen')) {
235 ini_set('allow_url_fopen', 1);
238 if (FALSE === $this->getRepositoryUrl()) {
239 // don't check if the user has configured civi not to check an external
240 // url for extensions. See CRM-10575.
241 CRM_Core_Session
::setStatus(ts('Not checking remote URL for extensions since ext_repo_url is set to false.'), ts('Check Settings'), 'alert');
245 $filename = $this->cacheDir
. DIRECTORY_SEPARATOR
. self
::CACHE_JSON_FILE
;
246 $url = $this->getRepositoryUrl() . $this->indexPath
;
247 $status = CRM_Utils_HttpClient
::singleton()->fetch($url, $filename);
248 if ($status !== CRM_Utils_HttpClient
::STATUS_OK
) {
249 CRM_Core_Session
::setStatus(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>.<br />', array(1 => $this->getRepositoryUrl())), ts('Connection Error'), 'error');
252 // Don't call grabCachedJson here, that would risk infinite recursion
253 $json = file_get_contents($filename);
256 // CRM-13141 There may not be any compatible extensions available for the requested CiviCRM version + CMS. If so, $extdir is empty so just return a notification.
258 $config = CRM_Core_Config
::singleton();
259 CRM_Core_Session
::setStatus(ts('There are currently no extensions on the CiviCRM public extension directory which are compatible with version %2 (<a href="%1">requested extensions from here</a>). If you want to install an extension which is not marked as compatible, you may be able to <a href="%3">download and install extensions manually</a> (depending on access to your web server).<br />', array(
260 1 => $this->getRepositoryUrl(),
261 2 => CRM_Utils_System
::version(),
262 3 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Extensions',
263 )), ts('No Extensions Available for this Version'), 'info');
266 ini_restore('allow_url_fopen');
267 ini_restore('default_socket_timeout');
269 restore_error_handler();
277 private function getTsPath() {
278 return $this->cacheDir
. DIRECTORY_SEPARATOR
. 'timestamp.txt';
282 * A dummy function required for suppressing download errors.
284 * @param $errorNumber
285 * @param $errorString
287 public static function downloadError($errorNumber, $errorString) {