From 999128a92b3afc4770f635408cfb46b8e57790e2 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 4 Dec 2015 21:24:28 -0500 Subject: [PATCH] CRM-17637 - Add version_check api job --- CRM/Utils/VersionCheck.php | 62 ++++++++++++-------------------------- api/v3/Job.php | 10 ++++++ 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/CRM/Utils/VersionCheck.php b/CRM/Utils/VersionCheck.php index 0b013bf638..d7a0bcd9da 100644 --- a/CRM/Utils/VersionCheck.php +++ b/CRM/Utils/VersionCheck.php @@ -33,14 +33,10 @@ class CRM_Utils_VersionCheck { const PINGBACK_URL = 'http://latest.civicrm.org/stable.php?format=json', - // timeout for when the connection or the server is slow - CHECK_TIMEOUT = 5, // relative to $civicrm_root - LOCALFILE_NAME = 'civicrm-version.php', + LOCALFILE_NAME = '[civicrm.root]/civicrm-version.php', // relative to $config->uploadDir - CACHEFILE_NAME = 'version-info-cache.json', - // cachefile expiry time (in seconds) - one day - CACHEFILE_EXPIRE = 86400; + CACHEFILE_NAME = '[civicrm.files]/persist/version-info-cache.json'; /** * The version of the current (local) installation @@ -81,12 +77,8 @@ class CRM_Utils_VersionCheck { * Class constructor. */ public function __construct() { - global $civicrm_root; - $config = CRM_Core_Config::singleton(); - - $localFile = $civicrm_root . DIRECTORY_SEPARATOR . self::LOCALFILE_NAME; - $this->cacheFile = $config->uploadDir . self::CACHEFILE_NAME; - + // Populate local version + $localFile = Civi::paths()->getPath(self::LOCALFILE_NAME); if (file_exists($localFile)) { require_once $localFile; } @@ -95,20 +87,10 @@ class CRM_Utils_VersionCheck { $this->localVersion = trim($info['version']); $this->localMajorVersion = $this->getMajorVersion($this->localVersion); } - // Populate $versionInfo - if (Civi::settings()->get('versionCheck')) { - // Use cached data if available and not stale - if (!$this->readCacheFile()) { - // Collect stats for pingback - $this->getSiteStats(); - - // Get the latest version and send site info - $this->pingBack(); - } - // Sort version info in ascending order for easier comparisons - ksort($this->versionInfo, SORT_NUMERIC); - } + // Populate remote $versionInfo from cache file + $this->cacheFile = Civi::paths()->getPath(self::CACHEFILE_NAME); + $this->readCacheFile(); } /** @@ -214,6 +196,14 @@ class CRM_Utils_VersionCheck { return $return; } + /** + * Called by version_check cron job + */ + public function fetch() { + $this->getSiteStats(); + $this->pingBack(); + } + /** * @param $majorVersion * @return null|string @@ -354,11 +344,9 @@ class CRM_Utils_VersionCheck { /** * Send the request to civicrm.org - * Set timeout and suppress errors * Store results in the cache file */ private function pingBack() { - ini_set('default_socket_timeout', self::CHECK_TIMEOUT); $params = array( 'http' => array( 'method' => 'POST', @@ -367,7 +355,7 @@ class CRM_Utils_VersionCheck { ), ); $ctx = stream_context_create($params); - $rawJson = @file_get_contents(self::PINGBACK_URL, FALSE, $ctx); + $rawJson = file_get_contents(self::PINGBACK_URL, FALSE, $ctx); $versionInfo = $rawJson ? json_decode($rawJson, TRUE) : NULL; // If we couldn't fetch or parse the data $versionInfo will be NULL // Otherwise it will be an array and we'll cache it. @@ -376,18 +364,16 @@ class CRM_Utils_VersionCheck { $this->writeCacheFile($rawJson); $this->versionInfo = $versionInfo; } - ini_restore('default_socket_timeout'); } /** * @return bool */ private function readCacheFile() { - $expiryTime = time() - self::CACHEFILE_EXPIRE; - - // if there's a cachefile and it's not stale, use it - if (file_exists($this->cacheFile) && (filemtime($this->cacheFile) > $expiryTime)) { + if (file_exists($this->cacheFile)) { $this->versionInfo = (array) json_decode(file_get_contents($this->cacheFile), TRUE); + // Sort version info in ascending order for easier comparisons + ksort($this->versionInfo, SORT_NUMERIC); return TRUE; } return FALSE; @@ -398,15 +384,7 @@ class CRM_Utils_VersionCheck { * @param string $contents */ private function writeCacheFile($contents) { - $fp = @fopen($this->cacheFile, 'w'); - if (!$fp) { - if (CRM_Core_Permission::check('administer CiviCRM')) { - CRM_Core_Session::setStatus( - ts('Unable to write file') . ": $this->cacheFile
" . ts('Please check your system file permissions.'), - ts('File Error'), 'error'); - } - return; - } + $fp = fopen($this->cacheFile, 'w'); fwrite($fp, $contents); fclose($fp); } diff --git a/api/v3/Job.php b/api/v3/Job.php index 8bc0de5216..10eb30aadc 100644 --- a/api/v3/Job.php +++ b/api/v3/Job.php @@ -639,3 +639,13 @@ function civicrm_api3_job_group_rebuild($params) { return civicrm_api3_create_success(); } + +/** + * Check for CiviCRM software updates. + * + * Anonymous site statistics are sent back to civicrm.org during this check. + */ +function civicrm_api3_job_version_check() { + $vc = new CRM_Utils_VersionCheck(); + $vc->fetch(); +} -- 2.25.1