Removing changes for CRM-17033
[civicrm-core.git] / CRM / Utils / VersionCheck.php
index d1e8fa12e16dce46fc9be9ed63121a18991c4cd0..530d9fd2381e7029bb3413ccda21a5d9a811c42f 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.6                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id: $
  *
  */
@@ -95,7 +95,7 @@ class CRM_Utils_VersionCheck {
   protected $cacheFile;
 
   /**
-   * Class constructor
+   * Class constructor.
    */
   public function __construct() {
     global $civicrm_root;
@@ -130,7 +130,7 @@ class CRM_Utils_VersionCheck {
   }
 
   /**
-   * Static instance provider
+   * Static instance provider.
    *
    * Method providing static instance of CRM_Utils_VersionCheck,
    * as in Singleton pattern
@@ -145,7 +145,7 @@ class CRM_Utils_VersionCheck {
   }
 
   /**
-   * Finds the release info for a minor version
+   * Finds the release info for a minor version.
    * @param string $version
    * @return array|null
    */
@@ -173,50 +173,78 @@ class CRM_Utils_VersionCheck {
     return "$a.$b";
   }
 
-  /**
-   * @return bool
-   */
-  public function isSecurityUpdateAvailable() {
-    $thisVersion = $this->getReleaseInfo($this->localVersion);
-    $localVersionDate = CRM_Utils_Array::value('date', $thisVersion, 0);
-    foreach ($this->versionInfo as $majorVersion) {
-      foreach ($majorVersion['releases'] as $release) {
-        if (!empty($release['security']) && $release['date'] > $localVersionDate
-          && version_compare($this->localVersion, $release['version']) < 0
-        ) {
-          if (!$this->ignoreDate || $this->ignoreDate < $release['date']) {
-            return TRUE;
-          }
-        }
-      }
-    }
-  }
 
   /**
    * Get the latest version number if it's newer than the local one
    *
-   * @return string|null
-   *   Returns version number of the latest release if it is greater than the local version
+   * @return array
+   *   Returns version number of the latest release if it is greater than the local version,
+   *   along with the type of upgrade (regular/security) needed and the status of the major
+   *   version
    */
   public function isNewerVersionAvailable() {
-    $newerVersion = NULL;
+    $return = array(
+      'version' => NULL,
+      'upgrade' => NULL,
+      'status' => NULL,
+    );
+
     if ($this->versionInfo && $this->localVersion) {
-      foreach ($this->versionInfo as $majorVersionNumber => $majorVersion) {
-        $release = $this->checkBranchForNewVersion($majorVersion);
-        if ($release) {
-          // If we have a release with the same majorVersion as local, return it
-          if ($majorVersionNumber == $this->localMajorVersion) {
-            return $release;
-          }
-          // Search outside the local majorVersion (excluding non-stable)
-          elseif ($majorVersion['status'] != 'testing') {
-            // We found a new release but don't return yet, keep searching newer majorVersions
-            $newerVersion = $release;
-          }
+      if (isset($this->versionInfo[$this->localMajorVersion])) {
+        switch (CRM_Utils_Array::value('status', $this->versionInfo[$this->localMajorVersion])) {
+          case 'stable':
+          case 'lts':
+          case 'testing':
+            // look for latest version in this major version
+            $releases = $this->checkBranchForNewVersion($this->versionInfo[$this->localMajorVersion]);
+            if ($releases['newest']) {
+              $return['version'] = $releases['newest'];
+
+              // check for intervening security releases
+              $return['upgrade'] = ($releases['security']) ? 'security' : 'regular';
+            }
+            break;
+
+          case 'eol':
+          default:
+            // look for latest version ever
+            foreach ($this->versionInfo as $majorVersionNumber => $majorVersion) {
+              if ($majorVersionNumber < $this->localMajorVersion || $majorVersion['status'] == 'testing') {
+                continue;
+              }
+              $releases = $this->checkBranchForNewVersion($this->versionInfo[$majorVersionNumber]);
+
+              if ($releases['newest']) {
+                $return['version'] = $releases['newest'];
+
+                // check for intervening security releases
+                $return['upgrade'] = ($releases['security'] || $return['upgrade'] == 'security') ? 'security' : 'regular';
+              }
+            }
+        }
+        $return['status'] = $this->versionInfo[$this->localMajorVersion]['status'];
+      }
+      else {
+        // Figure if the version is really old or really new
+        $wayOld = TRUE;
+
+        foreach ($this->versionInfo as $majorVersionNumber => $majorVersion) {
+          $wayOld = ($this->localMajorVersion < $majorVersionNumber);
+        }
+
+        if ($wayOld) {
+          $releases = $this->checkBranchForNewVersion($majorVersion);
+
+          $return = array(
+            'version' => $releases['newest'],
+            'upgrade' => 'security',
+            'status' => 'eol',
+          );
         }
       }
     }
-    return $newerVersion;
+
+    return $return;
   }
 
   /**
@@ -224,12 +252,18 @@ class CRM_Utils_VersionCheck {
    * @return null|string
    */
   private function checkBranchForNewVersion($majorVersion) {
-    $newerVersion = NULL;
+    $newerVersion = array(
+      'newest' => NULL,
+      'security' => NULL,
+    );
     if (!empty($majorVersion['releases'])) {
       foreach ($majorVersion['releases'] as $release) {
         if (version_compare($this->localVersion, $release['version']) < 0) {
           if (!$this->ignoreDate || $this->ignoreDate < $release['date']) {
-            $newerVersion = $release['version'];
+            $newerVersion['newest'] = $release['version'];
+            if (CRM_Utils_Array::value('security', $release)) {
+              $newerVersion['security'] = $release['version'];
+            }
           }
         }
       }
@@ -238,47 +272,7 @@ class CRM_Utils_VersionCheck {
   }
 
   /**
-   * Alert the site admin of new versions of CiviCRM
-   * Show the message once a day
-   */
-  public function versionAlert() {
-    $versionAlertSetting = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'versionAlert', NULL, 1);
-    $securityAlertSetting = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityUpdateAlert', NULL, 3);
-    $settingsUrl = CRM_Utils_System::url('civicrm/admin/setting/misc', 'reset=1', FALSE, NULL, FALSE, FALSE, TRUE);
-    if (CRM_Core_Permission::check('administer CiviCRM') && $securityAlertSetting > 1 && $this->isSecurityUpdateAvailable()) {
-      $session = CRM_Core_Session::singleton();
-      if ($session->timer('version_alert', 24 * 60 * 60)) {
-        $msg = ts('This version of CiviCRM requires a security update.') .
-          '<ul>
-            <li><a href="https://civicrm.org/advisory">' . ts('Read advisory') . '</a></li>
-            <li><a href="https://civicrm.org/download">' . ts('Download now') . '</a></li>
-            <li><a class="crm-setVersionCheckIgnoreDate" href="' . $settingsUrl . '">' . ts('Suppress this message') . '</a></li>
-          </ul>';
-        $session->setStatus($msg, ts('Security Alert'), 'alert');
-        CRM_Core_Resources::singleton()
-          ->addScriptFile('civicrm', 'templates/CRM/Admin/Form/Setting/versionCheckOptions.js');
-      }
-    }
-    elseif (CRM_Core_Permission::check('administer CiviCRM') && $versionAlertSetting > 1) {
-      $newerVersion = $this->isNewerVersionAvailable();
-      if ($newerVersion) {
-        $session = CRM_Core_Session::singleton();
-        if ($session->timer('version_alert', 24 * 60 * 60)) {
-          $msg = ts('A newer version of CiviCRM is available: %1', array(1 => $newerVersion)) .
-            '<ul>
-              <li><a href="https://civicrm.org/download">' . ts('Download now') . '</a></li>
-              <li><a class="crm-setVersionCheckIgnoreDate" href="' . $settingsUrl . '">' . ts('Suppress this message') . '</a></li>
-            </ul>';
-          $session->setStatus($msg, ts('Update Available'), 'info');
-          CRM_Core_Resources::singleton()
-            ->addScriptFile('civicrm', 'templates/CRM/Admin/Form/Setting/versionCheckOptions.js');
-        }
-      }
-    }
-  }
-
-  /**
-   * Collect info about the site to be sent as pingback data
+   * Collect info about the site to be sent as pingback data.
    */
   private function getSiteStats() {
     $config = CRM_Core_Config::singleton();
@@ -306,7 +300,7 @@ class CRM_Utils_VersionCheck {
   }
 
   /**
-   * Get active payment processor types
+   * Get active payment processor types.
    */
   private function getPayProcStats() {
     $dao = new CRM_Financial_DAO_PaymentProcessor();
@@ -325,7 +319,7 @@ class CRM_Utils_VersionCheck {
   }
 
   /**
-   * Fetch counts from entity tables
+   * Fetch counts from entity tables.
    * Add info to the 'entities' array
    */
   private function getEntityStats() {
@@ -435,7 +429,7 @@ class CRM_Utils_VersionCheck {
   }
 
   /**
-   * Save version info to file
+   * Save version info to file.
    * @param string $contents
    */
   private function writeCacheFile($contents) {