X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FExtension%2FBrowser.php;h=42fe8fa574772944e13b65e3305dc4aa49fd5927;hb=b4167b7ca6942d0995383555d0b8ae98e0af3cf8;hp=89994cb00ebee2c52960bd74e7986feb65cdb208;hpb=8fcd163fd85f5b4b9d719386d5c9addca1f09f06;p=civicrm-core.git diff --git a/CRM/Extension/Browser.php b/CRM/Extension/Browser.php index 89994cb00e..42fe8fa574 100644 --- a/CRM/Extension/Browser.php +++ b/CRM/Extension/Browser.php @@ -40,6 +40,25 @@ class CRM_Extension_Browser { // timeout for when the connection or the server is slow const CHECK_TIMEOUT = 5; + /** + * @var GuzzleHttp\Client + */ + protected $guzzleClient; + + /** + * @return \GuzzleHttp\Client + */ + public function getGuzzleClient(): \GuzzleHttp\Client { + return $this->guzzleClient ?? new \GuzzleHttp\Client(); + } + + /** + * @param \GuzzleHttp\Client $guzzleClient + */ + public function setGuzzleClient(\GuzzleHttp\Client $guzzleClient) { + $this->guzzleClient = $guzzleClient; + } + /** * @param string $repoUrl * URL of the remote repository. @@ -94,10 +113,10 @@ class CRM_Extension_Browser { */ public function checkRequirements() { if (!$this->isEnabled()) { - return array(); + return []; } - $errors = array(); + $errors = []; if (!$this->cacheDir || !is_dir($this->cacheDir) || !is_writable($this->cacheDir)) { $civicrmDestination = urlencode(CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1')); @@ -119,15 +138,15 @@ class CRM_Extension_Browser { /** * Get a list of all available extensions. * - * @return array + * @return CRM_Extension_Info[] * ($key => CRM_Extension_Info) */ public function getExtensions() { if (!$this->isEnabled() || count($this->checkRequirements())) { - return array(); + return []; } - $exts = array(); + $exts = []; $remote = $this->_discoverRemote(); if (is_array($remote)) { @@ -159,7 +178,7 @@ class CRM_Extension_Browser { } /** - * @return array + * @return CRM_Extension_Info[] * @throws CRM_Extension_Exception_ParseException */ private function _discoverRemote() { @@ -180,7 +199,7 @@ class CRM_Extension_Browser { $remotes = json_decode($this->grabCachedJson(), TRUE); } - $this->_remotesDiscovered = array(); + $this->_remotesDiscovered = []; foreach ((array) $remotes as $id => $xml) { $ext = CRM_Extension_Info::loadFromString($xml); $this->_remotesDiscovered[] = $ext; @@ -219,31 +238,23 @@ class CRM_Extension_Browser { * @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(); + return ''; } $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'); + $client = $this->getGuzzleClient(); + $response = $client->request('GET', $url, ['sink' => $filename, 'timeout' => \Civi::settings()->get('http_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 CiviCRM forum.', array(1 => $this->getRepositoryUrl())), 'connection_error'); + if ($response->getStatusCode() !== 200) { + 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'); } // Don't call grabCachedJson here, that would risk infinite recursion