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 * The version of the current (local) installation
50 public $localVersion = NULL;
53 * The major version (branch name) of the local version
57 public $localMajorVersion;
60 * Info about available versions
64 public $versionInfo = array();
71 protected $stats = array();
83 public function __construct() {
85 $config = CRM_Core_Config
::singleton();
87 $localFile = $civicrm_root . DIRECTORY_SEPARATOR
. self
::LOCALFILE_NAME
;
88 $this->cacheFile
= $config->uploadDir
. self
::CACHEFILE_NAME
;
90 if (file_exists($localFile)) {
91 require_once $localFile;
93 if (function_exists('civicrmVersion')) {
94 $info = civicrmVersion();
95 $this->localVersion
= trim($info['version']);
96 $this->localMajorVersion
= $this->getMajorVersion($this->localVersion
);
98 // Populate $versionInfo
99 if (Civi
::settings()->get('versionCheck')) {
100 // Use cached data if available and not stale
101 if (!$this->readCacheFile()) {
102 // Collect stats for pingback
103 $this->getSiteStats();
105 // Get the latest version and send site info
109 // Sort version info in ascending order for easier comparisons
110 ksort($this->versionInfo
, SORT_NUMERIC
);
115 * Finds the release info for a minor version.
116 * @param string $version
119 public function getReleaseInfo($version) {
120 $majorVersion = $this->getMajorVersion($version);
121 if (isset($this->versionInfo
[$majorVersion])) {
122 foreach ($this->versionInfo
[$majorVersion]['releases'] as $info) {
123 if ($info['version'] == $version) {
132 * @param $minorVersion
135 public function getMajorVersion($minorVersion) {
136 if (!$minorVersion) {
139 list($a, $b) = explode('.', $minorVersion);
145 * Get the latest version number if it's newer than the local one
148 * Returns version number of the latest release if it is greater than the local version,
149 * along with the type of upgrade (regular/security) needed and the status of the major
152 public function isNewerVersionAvailable() {
159 if ($this->versionInfo
&& $this->localVersion
) {
160 if (isset($this->versionInfo
[$this->localMajorVersion
])) {
161 switch (CRM_Utils_Array
::value('status', $this->versionInfo
[$this->localMajorVersion
])) {
165 // look for latest version in this major version
166 $releases = $this->checkBranchForNewVersion($this->versionInfo
[$this->localMajorVersion
]);
167 if ($releases['newest']) {
168 $return['version'] = $releases['newest'];
170 // check for intervening security releases
171 $return['upgrade'] = ($releases['security']) ?
'security' : 'regular';
177 // look for latest version ever
178 foreach ($this->versionInfo
as $majorVersionNumber => $majorVersion) {
179 if ($majorVersionNumber < $this->localMajorVersion ||
$majorVersion['status'] == 'testing') {
182 $releases = $this->checkBranchForNewVersion($this->versionInfo
[$majorVersionNumber]);
184 if ($releases['newest']) {
185 $return['version'] = $releases['newest'];
187 // check for intervening security releases
188 $return['upgrade'] = ($releases['security'] ||
$return['upgrade'] == 'security') ?
'security' : 'regular';
192 $return['status'] = $this->versionInfo
[$this->localMajorVersion
]['status'];
195 // Figure if the version is really old or really new
198 foreach ($this->versionInfo
as $majorVersionNumber => $majorVersion) {
199 $wayOld = ($this->localMajorVersion
< $majorVersionNumber);
203 $releases = $this->checkBranchForNewVersion($majorVersion);
206 'version' => $releases['newest'],
207 'upgrade' => 'security',
218 * @param $majorVersion
219 * @return null|string
221 private function checkBranchForNewVersion($majorVersion) {
222 $newerVersion = array(
226 if (!empty($majorVersion['releases'])) {
227 foreach ($majorVersion['releases'] as $release) {
228 if (version_compare($this->localVersion
, $release['version']) < 0) {
229 $newerVersion['newest'] = $release['version'];
230 if (CRM_Utils_Array
::value('security', $release)) {
231 $newerVersion['security'] = $release['version'];
236 return $newerVersion;
240 * Collect info about the site to be sent as pingback data.
242 private function getSiteStats() {
243 $config = CRM_Core_Config
::singleton();
244 $siteKey = md5(defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY
: '');
246 // Calorie-free pingback for alphas
247 $this->stats
= array('version' => $this->localVersion
);
249 // Non-alpha versions get the full treatment
250 if ($this->localVersion
&& !strpos($this->localVersion
, 'alpha')) {
251 $this->stats +
= array(
252 'hash' => md5($siteKey . $config->userFrameworkBaseURL
),
253 'uf' => $config->userFramework
,
254 'lang' => $config->lcMessages
,
255 'co' => $config->defaultContactCountry
,
256 'ufv' => $config->userSystem
->getVersion(),
257 'PHP' => phpversion(),
258 'MySQL' => CRM_CORE_DAO
::singleValueQuery('SELECT VERSION()'),
259 'communityMessagesUrl' => Civi
::settings()->get('communityMessagesUrl'),
261 $this->getPayProcStats();
262 $this->getEntityStats();
263 $this->getExtensionStats();
268 * Get active payment processor types.
270 private function getPayProcStats() {
271 $dao = new CRM_Financial_DAO_PaymentProcessor();
276 // Get title and id for all processor types
277 $ppTypeNames = CRM_Core_PseudoConstant
::paymentProcessorType();
279 while ($dao->fetch()) {
280 $ppTypes[] = $ppTypeNames[$dao->payment_processor_type_id
];
282 // add the .-separated list of the processor types
283 $this->stats
['PPTypes'] = implode(',', array_unique($ppTypes));
287 * Fetch counts from entity tables.
288 * Add info to the 'entities' array
290 private function getEntityStats() {
292 'CRM_Activity_DAO_Activity' => 'is_test = 0',
293 'CRM_Case_DAO_Case' => 'is_deleted = 0',
294 'CRM_Contact_DAO_Contact' => 'is_deleted = 0',
295 'CRM_Contact_DAO_Relationship' => NULL,
296 'CRM_Campaign_DAO_Campaign' => NULL,
297 'CRM_Contribute_DAO_Contribution' => 'is_test = 0',
298 'CRM_Contribute_DAO_ContributionPage' => 'is_active = 1',
299 'CRM_Contribute_DAO_ContributionProduct' => NULL,
300 'CRM_Contribute_DAO_Widget' => 'is_active = 1',
301 'CRM_Core_DAO_Discount' => NULL,
302 'CRM_Price_DAO_PriceSetEntity' => NULL,
303 'CRM_Core_DAO_UFGroup' => 'is_active = 1',
304 'CRM_Event_DAO_Event' => 'is_active = 1',
305 'CRM_Event_DAO_Participant' => 'is_test = 0',
306 'CRM_Friend_DAO_Friend' => 'is_active = 1',
307 'CRM_Grant_DAO_Grant' => NULL,
308 'CRM_Mailing_DAO_Mailing' => 'is_completed = 1',
309 'CRM_Member_DAO_Membership' => 'is_test = 0',
310 'CRM_Member_DAO_MembershipBlock' => 'is_active = 1',
311 'CRM_Pledge_DAO_Pledge' => 'is_test = 0',
312 'CRM_Pledge_DAO_PledgeBlock' => NULL,
314 foreach ($tables as $daoName => $where) {
315 $dao = new $daoName();
317 $dao->whereAdd($where);
319 $short_name = substr($daoName, strrpos($daoName, '_') +
1);
320 $this->stats
['entities'][] = array(
321 'name' => $short_name,
322 'size' => $dao->count(),
328 * Fetch stats about enabled components/extensions
329 * Add info to the 'extensions' array
331 private function getExtensionStats() {
333 $config = CRM_Core_Config
::singleton();
334 foreach ($config->enableComponents
as $comp) {
335 $this->stats
['extensions'][] = array(
336 'name' => 'org.civicrm.component.' . strtolower($comp),
338 'version' => $this->stats
['version'],
341 // Contrib extensions
342 $mapper = CRM_Extension_System
::singleton()->getMapper();
343 $dao = new CRM_Core_DAO_Extension();
345 while ($dao->fetch()) {
346 $info = $mapper->keyToInfo($dao->full_name
);
347 $this->stats
['extensions'][] = array(
348 'name' => $dao->full_name
,
349 'enabled' => $dao->is_active
,
350 'version' => isset($info->version
) ?
$info->version
: NULL,
356 * Send the request to civicrm.org
357 * Set timeout and suppress errors
358 * Store results in the cache file
360 private function pingBack() {
361 ini_set('default_socket_timeout', self
::CHECK_TIMEOUT
);
365 'header' => 'Content-type: application/x-www-form-urlencoded',
366 'content' => http_build_query($this->stats
),
369 $ctx = stream_context_create($params);
370 $rawJson = @file_get_contents
(self
::PINGBACK_URL
, FALSE, $ctx);
371 $versionInfo = $rawJson ?
json_decode($rawJson, TRUE) : NULL;
372 // If we couldn't fetch or parse the data $versionInfo will be NULL
373 // Otherwise it will be an array and we'll cache it.
374 // Note the array may be empty e.g. in the case of a pre-alpha with no releases
375 if ($versionInfo !== NULL) {
376 $this->writeCacheFile($rawJson);
377 $this->versionInfo
= $versionInfo;
379 ini_restore('default_socket_timeout');
385 private function readCacheFile() {
386 $expiryTime = time() - self
::CACHEFILE_EXPIRE
;
388 // if there's a cachefile and it's not stale, use it
389 if (file_exists($this->cacheFile
) && (filemtime($this->cacheFile
) > $expiryTime)) {
390 $this->versionInfo
= (array) json_decode(file_get_contents($this->cacheFile
), TRUE);
397 * Save version info to file.
398 * @param string $contents
400 private function writeCacheFile($contents) {
401 $fp = @fopen
($this->cacheFile
, 'w');
403 if (CRM_Core_Permission
::check('administer CiviCRM')) {
404 CRM_Core_Session
::setStatus(
405 ts('Unable to write file') . ": $this->cacheFile<br />" . ts('Please check your system file permissions.'),
406 ts('File Error'), 'error');
410 fwrite($fp, $contents);