From 3a39a8b5f5d7569d7e8e8b45887f43f1dd9533ac Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 20 Dec 2014 14:47:04 -0500 Subject: [PATCH] CRM-13672 - Add option to suppress current update alerts --- CRM/Admin/Form/Setting/Miscellaneous.php | 1 + CRM/Utils/VersionCheck.php | 86 ++++++++++++------- settings/Core.setting.php | 16 ++++ .../CRM/Admin/Form/Setting/Miscellaneous.tpl | 5 ++ .../Admin/Form/Setting/versionCheckOptions.js | 24 ++++++ 5 files changed, 99 insertions(+), 33 deletions(-) create mode 100644 templates/CRM/Admin/Form/Setting/versionCheckOptions.js diff --git a/CRM/Admin/Form/Setting/Miscellaneous.php b/CRM/Admin/Form/Setting/Miscellaneous.php index f5b03e0d74..d6123cd3b0 100644 --- a/CRM/Admin/Form/Setting/Miscellaneous.php +++ b/CRM/Admin/Form/Setting/Miscellaneous.php @@ -45,6 +45,7 @@ class CRM_Admin_Form_Setting_Miscellaneous extends CRM_Admin_Form_Setting { 'versionAlert' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityUpdateAlert' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'versionCheck' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'versionCheckIgnoreDate' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'empoweredBy' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'maxFileSize' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'doNotAttachPDFReceipt' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, diff --git a/CRM/Utils/VersionCheck.php b/CRM/Utils/VersionCheck.php index 973246a823..e8cdf969a7 100644 --- a/CRM/Utils/VersionCheck.php +++ b/CRM/Utils/VersionCheck.php @@ -67,6 +67,13 @@ class CRM_Utils_VersionCheck { */ public $localMajorVersion; + /** + * User setting to skip updates prior to a certain date + * + * @var string + */ + public $ignoreDate; + /** * Info about available versions * @@ -108,6 +115,7 @@ class CRM_Utils_VersionCheck { $this->localVersion = trim($info['version']); $this->localMajorVersion = $this->getMajorVersion($this->localVersion); } + // Populate $versionInfo if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'versionCheck', NULL, 1)) { // Use cached data if available and not stale if (!$this->readCacheFile()) { @@ -117,26 +125,11 @@ class CRM_Utils_VersionCheck { // Get the latest version and send site info $this->pingBack(); } - } - // Make sure version info is in ascending order for easier comparisons - ksort($this->versionInfo, SORT_NUMERIC); - } + $this->ignoreDate = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'versionCheckIgnoreDate'); - /** - * Magic property accessor - * @param $variable - * @return mixed - */ - function __get($variable) { - switch ($variable) { - case "localVersionStatus": - if ($this->localVersion && $this->versionInfo) { - $versionInfo = CRM_Utils_Array::value($this->localMajorVersion, $this->versionInfo); - return CRM_Utils_Array::value('status', $versionInfo); - } - return NULL; + // Sort version info in ascending order for easier comparisons + ksort($this->versionInfo, SORT_NUMERIC); } - return NULL; } /** @@ -183,13 +176,18 @@ 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) { - return TRUE; + if (!$this->ignoreDate || $this->ignoreDate < $release['date']) { + return TRUE; + } } } } @@ -199,24 +197,39 @@ class CRM_Utils_VersionCheck { * Get the latest version number if it's newer than the local one * * @return string|null - * Returns the newer version's number, or null if the versions are equal + * Returns version number of the latest release if it is greater than the local version */ public function isNewerVersionAvailable() { $newerVersion = NULL; if ($this->versionInfo && $this->localVersion) { - // If using an alpha or beta, find the absolute latest available - $latest = end($this->versionInfo); - // Otherwise find the latest stable version available - if ($this->localVersionStatus != 'testing') { - foreach ($this->versionInfo as $majorVersion) { - if ($majorVersion['status'] == 'stable') { - $latest = $majorVersion; + 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 ($latest && !empty($latest['releases'])) { - foreach ($latest['releases'] as $release) { - if (version_compare($this->localVersion, $release['version']) < 0) { + } + return $newerVersion; + } + + /** + * @param $majorVersion + * @return null|string + */ + private function checkBranchForNewVersion($majorVersion) { + $newerVersion = 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']; } } @@ -232,6 +245,7 @@ class CRM_Utils_VersionCheck { 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)) { @@ -239,8 +253,10 @@ class CRM_Utils_VersionCheck { ''; - $session->setStatus($msg, ts('Security Alert')); + $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) { @@ -248,9 +264,13 @@ class CRM_Utils_VersionCheck { 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)) - . '
' . ts('Download now') . ''; + $msg = ts('A newer version of CiviCRM is available: %1', array(1 => $newerVersion)) . + ''; $session->setStatus($msg, ts('Update Available'), 'info'); + CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Admin/Form/Setting/versionCheckOptions.js'); } } } diff --git a/settings/Core.setting.php b/settings/Core.setting.php index ffd41021b3..66b697e6e0 100644 --- a/settings/Core.setting.php +++ b/settings/Core.setting.php @@ -407,6 +407,22 @@ return array ( 'description' => "", 'help_text' => null, ), + 'versionCheckIgnoreDate' => array( + 'group_name' => 'CiviCRM Preferences', + 'group' => 'core', + 'name' => 'versionCheckIgnoreDate', + 'type' => 'String', + 'quick_form_type' => 'Element', + 'html_type' => 'text', + 'html_attributes' => array('placeholder' => 'YYYY-MM-DD'), + 'default' => '', + 'add' => '4.6', + 'title' => 'Ignore Updates Prior to', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => "", + 'help_text' => null, + ), 'securityAlert' => array( 'group_name' => 'CiviCRM Preferences', 'group' => 'core', diff --git a/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl b/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl index 7b3f84a9db..8c7fb3012f 100644 --- a/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl +++ b/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl @@ -77,6 +77,11 @@ {$form.versionCheck.html}

{ts}When enabled, statistics about your CiviCRM installation are reported anonymously to the CiviCRM team to assist in prioritizing ongoing development efforts. The following information is gathered: CiviCRM version, versions of PHP, MySQL and framework (Drupal/Joomla/standalone), and default language. Counts (but no actual data) of the following record types are reported: contacts, activities, cases, relationships, contributions, contribution pages, contribution products, contribution widgets, discounts, price sets, profiles, events, participants, tell-a-friend pages, grants, mailings, memberships, membership blocks, pledges, pledge blocks and active payment processor types.{/ts}

+ + {$form.versionCheckIgnoreDate.label} + {$form.versionCheckIgnoreDate.html|crmReplace:type:date}
+

{ts}If you wish to stop receiving alerts about a current update (for example, if you have applied a security patch manually), set this to today's date.{/ts}

+ {$form.securityUpdateAlert.label} {$form.securityUpdateAlert.html}
diff --git a/templates/CRM/Admin/Form/Setting/versionCheckOptions.js b/templates/CRM/Admin/Form/Setting/versionCheckOptions.js new file mode 100644 index 0000000000..c6de62a11a --- /dev/null +++ b/templates/CRM/Admin/Form/Setting/versionCheckOptions.js @@ -0,0 +1,24 @@ +// https://civicrm.org/licensing +CRM.$(function($) { + 'use strict'; + $(document) + .off('.crmVersionCheckOptions') + .on('click.crmVersionCheckOptions', 'a.crm-setVersionCheckIgnoreDate', function(e) { + var isSecurity = !($(this).closest('.ui-notify-message').hasClass('info')); + var msg = '

' + ts('This will suppress notifications about all currently available updates.') + ' '; + if (isSecurity) { + msg += ts('Notifications will resume when a new security advisory is published.') + + '

' + + ts('Warning: Do this only if you have already taken alternate steps to ensure your site is secure.'); + } else { + msg += ts('Notifications will resume when a new release is published.'); + } + msg += '

'; + CRM.confirm({message: msg, title: $(this).text()}) + .on('crmConfirm:yes', function() { + CRM.api3('setting', 'create', {versionCheckIgnoreDate: new Date().toISOString().slice(0,10)}, true); + CRM.closeAlertByChild('a.crm-setVersionCheckIgnoreDate'); + }); + e.preventDefault(); + }); +}); -- 2.25.1