Merge pull request #5479 from eileenmcnaughton/4.6
[civicrm-core.git] / CRM / Extension / Browser.php
index fa46980699b0e8346c18fc3e393e5168743f10e7..b3929d49e5fa2365ace36dc932b5da1149272b9f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -23,7 +23,7 @@
  | 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
 class CRM_Extension_Browser {
 
   /**
-   * An URL for public extensions repository
+   * An URL for public extensions repository.
    */
   const DEFAULT_EXTENSIONS_REPOSITORY = 'https://civicrm.org/extdir/ver={ver}|cms={uf}';
 
   /**
-   * @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 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.
    */
   public function __construct($repoUrl, $indexPath, $cacheDir) {
     $this->repoUrl = $repoUrl;
     $this->cacheDir = $cacheDir;
     $this->indexPath = $indexPath;
-    if ($cacheDir && !file_exists($cacheDir) && is_dir(dirname($cacheDir)) && is_writeable(dirname($cacheDir))) {
+    if ($cacheDir && !file_exists($cacheDir) && is_dir(dirname($cacheDir)) && is_writable(dirname($cacheDir))) {
       CRM_Utils_File::createDir($cacheDir, FALSE);
     }
   }
@@ -74,6 +77,9 @@ class CRM_Extension_Browser {
     return $this->repoUrl;
   }
 
+  /**
+   * Refresh the cache of remotely-available extensions.
+   */
   public function refresh() {
     $file = $this->getTsPath();
     if (file_exists($file)) {
@@ -82,9 +88,10 @@ class CRM_Extension_Browser {
   }
 
   /**
-   * Determine whether downloading is supported
+   * Determine whether downloading is supported.
    *
-   * @return array list of error messages; empty if OK
+   * @return array
+   *   List of error messages; empty if OK.
    */
   public function checkRequirements() {
     if (!$this->isEnabled()) {
@@ -93,7 +100,7 @@ class CRM_Extension_Browser {
 
     $errors = array();
 
-    if (!$this->cacheDir || !is_dir($this->cacheDir) || !is_writeable($this->cacheDir)) {
+    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(
@@ -103,7 +110,7 @@ class CRM_Extension_Browser {
             1 => $this->cacheDir,
             2 => $url,
           )
-        )
+        ),
       );
     }
 
@@ -111,9 +118,10 @@ class CRM_Extension_Browser {
   }
 
   /**
-   * Get a list of all available extensions
+   * Get a list of all available extensions.
    *
-   * @return array ($key => CRM_Extension_Info)
+   * @return array
+   *   ($key => CRM_Extension_Info)
    */
   public function getExtensions() {
     if (!$this->isEnabled() || count($this->checkRequirements())) {
@@ -133,9 +141,10 @@ class CRM_Extension_Browser {
   }
 
   /**
-   * Get a description of a particular extension
+   * Get a description of a particular extension.
    *
-   * @param $key
+   * @param string $key
+   *   Fully-qualified extension name.
    *
    * @return CRM_Extension_Info|NULL
    */
@@ -144,7 +153,8 @@ class CRM_Extension_Browser {
     $exts = $this->getExtensions();
     if (array_key_exists($key, $exts)) {
       return $exts[$key];
-    } else {
+    }
+    else {
       // throw new CRM_Extension_Exception("Unknown remote extension: $key");
       return NULL;
     }
@@ -155,7 +165,7 @@ class CRM_Extension_Browser {
    * @throws CRM_Extension_Exception_ParseException
    */
   private function _discoverRemote() {
-    $tsPath    = $this->getTsPath();
+    $tsPath = $this->getTsPath();
     $timestamp = FALSE;
 
     if (file_exists($tsPath)) {
@@ -194,9 +204,9 @@ class CRM_Extension_Browser {
    * @return array
    */
   private function grabCachedKeyList() {
-    $result     = array();
+    $result = array();
     $cachedPath = $this->cacheDir . DIRECTORY_SEPARATOR;
-    $files      = scandir($cachedPath);
+    $files = scandir($cachedPath);
     foreach ($files as $dc => $fname) {
       if (substr($fname, -4) == '.xml') {
         $result[] = array('key' => substr($fname, 0, -4));
@@ -209,9 +219,9 @@ class CRM_Extension_Browser {
    * Connects to public server and grabs the list of publically available
    * extensions.
    *
-   * @access public
    *
-   * @return Array list of extension names
+   * @return array
+   *   list of extension names
    */
   private function grabRemoteKeyList() {
 
@@ -222,7 +232,7 @@ class CRM_Extension_Browser {
       ini_set('allow_url_fopen', 1);
     }
 
-    if(FALSE === $this->getRepositoryUrl()) {
+    if (FALSE === $this->getRepositoryUrl()) {
       // don't check if the user has configured civi not to check an external
       // url for extensions. See CRM-10575.
       CRM_Core_Session::setStatus(ts('Not checking remote URL for extensions since ext_repo_url is set to false.'), ts('Check Settings'), 'alert');
@@ -233,7 +243,8 @@ class CRM_Extension_Browser {
     list ($status, $extdir) = CRM_Utils_HttpClient::singleton()->get($this->getRepositoryUrl() . $this->indexPath);
     if ($extdir === FALSE || $status !== CRM_Utils_HttpClient::STATUS_OK) {
       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');
-    } else {
+    }
+    else {
       $lines = explode("\n", $extdir);
 
       foreach ($lines as $ln) {
@@ -251,7 +262,11 @@ class CRM_Extension_Browser {
     // 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.
     if (empty($exts)) {
       $config = CRM_Core_Config::singleton();
-      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(1 => $this->getRepositoryUrl(), 2 => $config->civiVersion, 3 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Extensions')), ts('No Extensions Available for this Version'), 'info');
+      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(
+        1 => $this->getRepositoryUrl(),
+        2 => $config->civiVersion,
+        3 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Extensions',
+      )), ts('No Extensions Available for this Version'), 'info');
     }
 
     ini_restore('allow_url_fopen');
@@ -266,16 +281,17 @@ class CRM_Extension_Browser {
    * Given the key, retrieves the info XML from a remote server
    * and stores locally, returning the contents.
    *
-   * @access public
+   * @param string $key
+   *   Extension key.
+   * @param bool $cached
+   *   Whether to use cached data.
    *
-   * @param string $key extension key
-   * @param boolean $cached whether to use cached data
-   *
-   * @return contents of info.xml, or null if info.xml cannot be retrieved or parsed
+   * @return string
+   *   Contents of info.xml, or null if info.xml cannot be retrieved or parsed.
    */
   private function grabRemoteInfoFile($key, $cached = FALSE) {
     $filename = $this->cacheDir . DIRECTORY_SEPARATOR . $key . '.xml';
-    $url      = $this->getRepositoryUrl() . '/' . $key . '.xml';
+    $url = $this->getRepositoryUrl() . '/' . $key . '.xml';
 
     if (!$cached || !file_exists($filename)) {
       $fetchStatus = CRM_Utils_HttpClient::singleton()->fetch($url, $filename);
@@ -305,14 +321,16 @@ class CRM_Extension_Browser {
    * @return string
    */
   private function getTsPath() {
-      return $this->cacheDir . DIRECTORY_SEPARATOR . 'timestamp.txt';
+    return $this->cacheDir . DIRECTORY_SEPARATOR . 'timestamp.txt';
   }
 
   /**
-   * A dummy function required for suppressing download errors
+   * A dummy function required for suppressing download errors.
+   *
+   * @param $errorNumber
+   * @param $errorString
    */
   public static function downloadError($errorNumber, $errorString) {
-    return;
   }
 
 }