3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
33 class CRM_Utils_VersionCheck
{
35 PINGBACK_URL
= 'http://latest.civicrm.org/stable.php?format=json',
36 // timeout for when the connection or the server is slow
38 // relative to $civicrm_root
39 LOCALFILE_NAME
= 'civicrm-version.php',
40 // relative to $config->uploadDir
41 CACHEFILE_NAME
= 'version-info-cache.json',
42 // cachefile expiry time (in seconds) - one day
43 CACHEFILE_EXPIRE
= 86400;
46 * We only need one instance of this object, so we use the
47 * singleton pattern and cache the instance in this variable
51 static private $_singleton = NULL;
54 * The version of the current (local) installation
58 public $localVersion = NULL;
61 * The major version (branch name) of the local version
65 public $localMajorVersion;
68 * User setting to skip updates prior to a certain date
75 * Info about available versions
79 public $versionInfo = array();
86 protected $stats = array();
98 public function __construct() {
100 $config = CRM_Core_Config
::singleton();
102 $localFile = $civicrm_root . DIRECTORY_SEPARATOR
. self
::LOCALFILE_NAME
;
103 $this->cacheFile
= $config->uploadDir
. self
::CACHEFILE_NAME
;
105 if (file_exists($localFile)) {
106 require_once $localFile;
108 if (function_exists('civicrmVersion')) {
109 $info = civicrmVersion();
110 $this->localVersion
= trim($info['version']);
111 $this->localMajorVersion
= $this->getMajorVersion($this->localVersion
);
113 // Populate $versionInfo
114 if (CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'versionCheck', NULL, 1)) {
115 // Use cached data if available and not stale
116 if (!$this->readCacheFile()) {
117 // Collect stats for pingback
118 $this->getSiteStats();
120 // Get the latest version and send site info
123 $this->ignoreDate
= CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'versionCheckIgnoreDate');
125 // Sort version info in ascending order for easier comparisons
126 ksort($this->versionInfo
, SORT_NUMERIC
);
131 * Static instance provider.
133 * Method providing static instance of CRM_Utils_VersionCheck,
134 * as in Singleton pattern
136 * @return CRM_Utils_VersionCheck
138 public static function &singleton() {
139 if (!isset(self
::$_singleton)) {
140 self
::$_singleton = new CRM_Utils_VersionCheck();
142 return self
::$_singleton;
146 * Finds the release info for a minor version.
147 * @param string $version
150 public function getReleaseInfo($version) {
151 $majorVersion = $this->getMajorVersion($version);
152 if (isset($this->versionInfo
[$majorVersion])) {
153 foreach ($this->versionInfo
[$majorVersion]['releases'] as $info) {
154 if ($info['version'] == $version) {
163 * @param $minorVersion
166 public function getMajorVersion($minorVersion) {
167 if (!$minorVersion) {
170 list($a, $b) = explode('.', $minorVersion);
176 * Get the latest version number if it's newer than the local one
179 * Returns version number of the latest release if it is greater than the local version,
180 * along with the type of upgrade (regular/security) needed and the status of the major
183 public function isNewerVersionAvailable() {
190 if ($this->versionInfo
&& $this->localVersion
) {
191 if (isset($this->versionInfo
[$this->localMajorVersion
])) {
192 switch (CRM_Utils_Array
::value('status', $this->versionInfo
[$this->localMajorVersion
])) {
196 // look for latest version in this major version
197 $releases = $this->checkBranchForNewVersion($this->versionInfo
[$this->localMajorVersion
]);
198 if ($releases['newest']) {
199 $return['version'] = $releases['newest'];
201 // check for intervening security releases
202 $return['upgrade'] = ($releases['security']) ?
'security' : 'regular';
208 // look for latest version ever
209 foreach ($this->versionInfo
as $majorVersionNumber => $majorVersion) {
210 if ($majorVersionNumber < $this->localMajorVersion ||
$majorVersion['status'] == 'testing') {
213 $releases = $this->checkBranchForNewVersion($this->versionInfo
[$majorVersionNumber]);
215 if ($releases['newest']) {
216 $return['version'] = $releases['newest'];
218 // check for intervening security releases
219 $return['upgrade'] = ($releases['security'] ||
$return['upgrade'] == 'security') ?
'security' : 'regular';
223 $return['status'] = $this->versionInfo
[$this->localMajorVersion
]['status'];
226 // Figure if the version is really old or really new
229 foreach ($this->versionInfo
as $majorVersionNumber => $majorVersion) {
230 $wayOld = ($this->localMajorVersion
< $majorVersionNumber);
234 $releases = $this->checkBranchForNewVersion($majorVersion);
237 'version' => $releases['newest'],
238 'upgrade' => 'security',
249 * @param $majorVersion
250 * @return null|string
252 private function checkBranchForNewVersion($majorVersion) {
253 $newerVersion = array(
257 if (!empty($majorVersion['releases'])) {
258 foreach ($majorVersion['releases'] as $release) {
259 if (version_compare($this->localVersion
, $release['version']) < 0) {
260 if (!$this->ignoreDate ||
$this->ignoreDate
< $release['date']) {
261 $newerVersion['newest'] = $release['version'];
262 if (CRM_Utils_Array
::value('security', $release)) {
263 $newerVersion['security'] = $release['version'];
269 return $newerVersion;
273 * Collect info about the site to be sent as pingback data.
275 private function getSiteStats() {
276 $config = CRM_Core_Config
::singleton();
277 $siteKey = md5(defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY
: '');
279 // Calorie-free pingback for alphas
280 $this->stats
= array('version' => $this->localVersion
);
282 // Non-alpha versions get the full treatment
283 if ($this->localVersion
&& !strpos($this->localVersion
, 'alpha')) {
284 $this->stats +
= array(
285 'hash' => md5($siteKey . $config->userFrameworkBaseURL
),
286 'uf' => $config->userFramework
,
287 'lang' => $config->lcMessages
,
288 'co' => $config->defaultContactCountry
,
289 'ufv' => $config->userSystem
->getVersion(),
290 'PHP' => phpversion(),
291 'MySQL' => CRM_CORE_DAO
::singleValueQuery('SELECT VERSION()'),
292 'communityMessagesUrl' => Civi
::settings()->get('communityMessagesUrl'),
294 $this->getPayProcStats();
295 $this->getEntityStats();
296 $this->getExtensionStats();
301 * Get active payment processor types.
303 private function getPayProcStats() {
304 $dao = new CRM_Financial_DAO_PaymentProcessor();
309 // Get title and id for all processor types
310 $ppTypeNames = CRM_Core_PseudoConstant
::paymentProcessorType();
312 while ($dao->fetch()) {
313 $ppTypes[] = $ppTypeNames[$dao->payment_processor_type_id
];
315 // add the .-separated list of the processor types
316 $this->stats
['PPTypes'] = implode(',', array_unique($ppTypes));
320 * Fetch counts from entity tables.
321 * Add info to the 'entities' array
323 private function getEntityStats() {
325 'CRM_Activity_DAO_Activity' => 'is_test = 0',
326 'CRM_Case_DAO_Case' => 'is_deleted = 0',
327 'CRM_Contact_DAO_Contact' => 'is_deleted = 0',
328 'CRM_Contact_DAO_Relationship' => NULL,
329 'CRM_Campaign_DAO_Campaign' => NULL,
330 'CRM_Contribute_DAO_Contribution' => 'is_test = 0',
331 'CRM_Contribute_DAO_ContributionPage' => 'is_active = 1',
332 'CRM_Contribute_DAO_ContributionProduct' => NULL,
333 'CRM_Contribute_DAO_Widget' => 'is_active = 1',
334 'CRM_Core_DAO_Discount' => NULL,
335 'CRM_Price_DAO_PriceSetEntity' => NULL,
336 'CRM_Core_DAO_UFGroup' => 'is_active = 1',
337 'CRM_Event_DAO_Event' => 'is_active = 1',
338 'CRM_Event_DAO_Participant' => 'is_test = 0',
339 'CRM_Friend_DAO_Friend' => 'is_active = 1',
340 'CRM_Grant_DAO_Grant' => NULL,
341 'CRM_Mailing_DAO_Mailing' => 'is_completed = 1',
342 'CRM_Member_DAO_Membership' => 'is_test = 0',
343 'CRM_Member_DAO_MembershipBlock' => 'is_active = 1',
344 'CRM_Pledge_DAO_Pledge' => 'is_test = 0',
345 'CRM_Pledge_DAO_PledgeBlock' => NULL,
347 foreach ($tables as $daoName => $where) {
348 $dao = new $daoName();
350 $dao->whereAdd($where);
352 $short_name = substr($daoName, strrpos($daoName, '_') +
1);
353 $this->stats
['entities'][] = array(
354 'name' => $short_name,
355 'size' => $dao->count(),
361 * Fetch stats about enabled components/extensions
362 * Add info to the 'extensions' array
364 private function getExtensionStats() {
366 $config = CRM_Core_Config
::singleton();
367 foreach ($config->enableComponents
as $comp) {
368 $this->stats
['extensions'][] = array(
369 'name' => 'org.civicrm.component.' . strtolower($comp),
371 'version' => $this->stats
['version'],
374 // Contrib extensions
375 $mapper = CRM_Extension_System
::singleton()->getMapper();
376 $dao = new CRM_Core_DAO_Extension();
378 while ($dao->fetch()) {
379 $info = $mapper->keyToInfo($dao->full_name
);
380 $this->stats
['extensions'][] = array(
381 'name' => $dao->full_name
,
382 'enabled' => $dao->is_active
,
383 'version' => isset($info->version
) ?
$info->version
: NULL,
389 * Send the request to civicrm.org
390 * Set timeout and suppress errors
391 * Store results in the cache file
393 private function pingBack() {
394 ini_set('default_socket_timeout', self
::CHECK_TIMEOUT
);
398 'header' => 'Content-type: application/x-www-form-urlencoded',
399 'content' => http_build_query($this->stats
),
402 $ctx = stream_context_create($params);
403 $rawJson = @file_get_contents
(self
::PINGBACK_URL
, FALSE, $ctx);
404 $versionInfo = $rawJson ?
json_decode($rawJson, TRUE) : NULL;
405 // If we couldn't fetch or parse the data $versionInfo will be NULL
406 // Otherwise it will be an array and we'll cache it.
407 // Note the array may be empty e.g. in the case of a pre-alpha with no releases
408 if ($versionInfo !== NULL) {
409 $this->writeCacheFile($rawJson);
410 $this->versionInfo
= $versionInfo;
412 ini_restore('default_socket_timeout');
418 private function readCacheFile() {
419 $expiryTime = time() - self
::CACHEFILE_EXPIRE
;
421 // if there's a cachefile and it's not stale, use it
422 if (file_exists($this->cacheFile
) && (filemtime($this->cacheFile
) > $expiryTime)) {
423 $this->versionInfo
= (array) json_decode(file_get_contents($this->cacheFile
), TRUE);
430 * Save version info to file.
431 * @param string $contents
433 private function writeCacheFile($contents) {
434 $fp = @fopen
($this->cacheFile
, 'w');
436 if (CRM_Core_Permission
::check('administer CiviCRM')) {
437 CRM_Core_Session
::setStatus(
438 ts('Unable to write file') . ": $this->cacheFile<br />" . ts('Please check your system file permissions.'),
439 ts('File Error'), 'error');
443 fwrite($fp, $contents);