-
-
Notifications
You must be signed in to change notification settings - Fork 831
/
Copy pathVersionCheck.php
475 lines (432 loc) · 14.6 KB
/
VersionCheck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Utils_VersionCheck {
const
CACHEFILE_NAME = 'version-info-cache.json',
// After which length of time we expire the cached version info (7+ days).
CACHEFILE_EXPIRE = 605000;
/**
* The version of the current (local) installation
*
* @var string
*/
public $localVersion = NULL;
/**
* The major version (branch name) of the local version
*
* @var string
*/
public $localMajorVersion;
/**
* Info about available versions
*
* @var array
*/
public $versionInfo = array();
/**
* @var bool
*/
public $isInfoAvailable;
/**
* @var array
*/
public $cronJob = array();
/**
* @var string
*/
public $pingbackUrl = 'https://latest.civicrm.org/stable.php?format=json';
/**
* Pingback params
*
* @var array
*/
protected $stats = array();
/**
* Path to cache file
*
* @var string
*/
public $cacheFile;
/**
* Class constructor.
*/
public function __construct() {
$this->localVersion = CRM_Utils_System::version();
$this->localMajorVersion = $this->getMajorVersion($this->localVersion);
$this->cacheFile = CRM_Core_Config::singleton()->uploadDir . self::CACHEFILE_NAME;
}
/**
* Self-populates version info
*
* @throws \Exception
*/
public function initialize() {
$this->getJob();
// Populate remote $versionInfo from cache file
$this->isInfoAvailable = $this->readCacheFile();
// Fallback if scheduled job is enabled but has failed to run.
$expiryTime = time() - self::CACHEFILE_EXPIRE;
if (!empty($this->cronJob['is_active']) &&
(!$this->isInfoAvailable || filemtime($this->cacheFile) < $expiryTime)
) {
// First try updating the files modification time, for 2 reasons:
// - if the file is not writeable, this saves the trouble of pinging back
// - if the remote server is down, this will prevent an immediate retry
if (touch($this->cacheFile) === FALSE) {
throw new Exception('File not writable');
}
$this->fetch();
}
}
/**
* Sets $versionInfo
*
* @param $info
*/
public function setVersionInfo($info) {
$this->versionInfo = (array) $info;
// Sort version info in ascending order for easier comparisons
ksort($this->versionInfo, SORT_NUMERIC);
}
/**
* Finds the release info for a minor version.
* @param string $version
* @return array|null
*/
public function getReleaseInfo($version) {
$majorVersion = $this->getMajorVersion($version);
if (isset($this->versionInfo[$majorVersion])) {
foreach ($this->versionInfo[$majorVersion]['releases'] as $info) {
if ($info['version'] == $version) {
return $info;
}
}
}
return NULL;
}
/**
* @param $minorVersion
* @return string
*/
public function getMajorVersion($minorVersion) {
if (!$minorVersion) {
return NULL;
}
list($a, $b) = explode('.', $minorVersion);
return "$a.$b";
}
/**
* Get the latest version number if it's newer than the local one
*
* @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() {
$return = array(
'version' => NULL,
'upgrade' => NULL,
'status' => NULL,
);
if ($this->versionInfo && $this->localVersion) {
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 $return;
}
/**
* Called by version_check cron job
*/
public function fetch() {
$this->getSiteStats();
$this->pingBack();
}
/**
* @param $majorVersion
* @return null|string
*/
private function checkBranchForNewVersion($majorVersion) {
$newerVersion = array(
'newest' => NULL,
'security' => NULL,
);
if (!empty($majorVersion['releases'])) {
foreach ($majorVersion['releases'] as $release) {
if (version_compare($this->localVersion, $release['version']) < 0) {
$newerVersion['newest'] = $release['version'];
if (CRM_Utils_Array::value('security', $release)) {
$newerVersion['security'] = $release['version'];
}
}
}
}
return $newerVersion;
}
/**
* Collect info about the site to be sent as pingback data.
*/
private function getSiteStats() {
$config = CRM_Core_Config::singleton();
$siteKey = md5(defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY : '');
// Calorie-free pingback for alphas
$this->stats = array('version' => $this->localVersion);
// Non-alpha versions get the full treatment
if ($this->localVersion && !strpos($this->localVersion, 'alpha')) {
$this->stats += array(
'hash' => md5($siteKey . $config->userFrameworkBaseURL),
'uf' => $config->userFramework,
'lang' => $config->lcMessages,
'co' => $config->defaultContactCountry,
'ufv' => $config->userSystem->getVersion(),
'PHP' => phpversion(),
'MySQL' => CRM_CORE_DAO::singleValueQuery('SELECT VERSION()'),
'communityMessagesUrl' => Civi::settings()->get('communityMessagesUrl'),
);
$this->getDomainStats();
$this->getPayProcStats();
$this->getEntityStats();
$this->getExtensionStats();
}
}
/**
* Get active payment processor types.
*/
private function getPayProcStats() {
$dao = new CRM_Financial_DAO_PaymentProcessor();
$dao->is_active = 1;
$dao->find();
$ppTypes = array();
// Get title and id for all processor types
$ppTypeNames = CRM_Core_PseudoConstant::paymentProcessorType();
while ($dao->fetch()) {
$ppTypes[] = $ppTypeNames[$dao->payment_processor_type_id];
}
// add the .-separated list of the processor types
$this->stats['PPTypes'] = implode(',', array_unique($ppTypes));
}
/**
* Fetch counts from entity tables.
* Add info to the 'entities' array
*/
private function getEntityStats() {
$tables = array(
'CRM_Activity_DAO_Activity' => 'is_test = 0',
'CRM_Case_DAO_Case' => 'is_deleted = 0',
'CRM_Contact_DAO_Contact' => 'is_deleted = 0',
'CRM_Contact_DAO_Relationship' => NULL,
'CRM_Campaign_DAO_Campaign' => NULL,
'CRM_Contribute_DAO_Contribution' => 'is_test = 0',
'CRM_Contribute_DAO_ContributionPage' => 'is_active = 1',
'CRM_Contribute_DAO_ContributionProduct' => NULL,
'CRM_Contribute_DAO_Widget' => 'is_active = 1',
'CRM_Core_DAO_Discount' => NULL,
'CRM_Price_DAO_PriceSetEntity' => NULL,
'CRM_Core_DAO_UFGroup' => 'is_active = 1',
'CRM_Event_DAO_Event' => 'is_active = 1',
'CRM_Event_DAO_Participant' => 'is_test = 0',
'CRM_Friend_DAO_Friend' => 'is_active = 1',
'CRM_Grant_DAO_Grant' => NULL,
'CRM_Mailing_DAO_Mailing' => 'is_completed = 1',
'CRM_Member_DAO_Membership' => 'is_test = 0',
'CRM_Member_DAO_MembershipBlock' => 'is_active = 1',
'CRM_Pledge_DAO_Pledge' => 'is_test = 0',
'CRM_Pledge_DAO_PledgeBlock' => NULL,
'CRM_Mailing_Event_DAO_Delivered' => NULL,
);
foreach ($tables as $daoName => $where) {
$dao = new $daoName();
if ($where) {
$dao->whereAdd($where);
}
$short_name = substr($daoName, strrpos($daoName, '_') + 1);
$this->stats['entities'][] = array(
'name' => $short_name,
'size' => $dao->count(),
);
}
}
/**
* Fetch stats about enabled components/extensions
* Add info to the 'extensions' array
*/
private function getExtensionStats() {
// Core components
$config = CRM_Core_Config::singleton();
foreach ($config->enableComponents as $comp) {
$this->stats['extensions'][] = array(
'name' => 'org.civicrm.component.' . strtolower($comp),
'enabled' => 1,
'version' => $this->stats['version'],
);
}
// Contrib extensions
$mapper = CRM_Extension_System::singleton()->getMapper();
$dao = new CRM_Core_DAO_Extension();
$dao->find();
while ($dao->fetch()) {
$info = $mapper->keyToInfo($dao->full_name);
$this->stats['extensions'][] = array(
'name' => $dao->full_name,
'enabled' => $dao->is_active,
'version' => isset($info->version) ? $info->version : NULL,
);
}
}
/**
* Fetch stats about domain and add to 'stats' array.
*/
private function getDomainStats() {
// Start with default value NULL, then check to see if there's a better
// value to be had.
$this->stats['domain_isoCode'] = NULL;
$params = array(
'id' => CRM_Core_Config::domainID(),
);
$domain_result = civicrm_api3('domain', 'getsingle', $params);
if (!empty($domain_result['contact_id'])) {
$address_params = array(
'contact_id' => $domain_result['contact_id'],
'is_primary' => 1,
'sequential' => 1,
);
$address_result = civicrm_api3('address', 'get', $address_params);
if ($address_result['count'] == 1 && !empty($address_result['values'][0]['country_id'])) {
$country_params = array(
'id' => $address_result['values'][0]['country_id'],
);
$country_result = civicrm_api3('country', 'getsingle', $country_params);
if (!empty($country_result['iso_code'])) {
$this->stats['domain_isoCode'] = $country_result['iso_code'];
}
}
}
}
/**
* Send the request to civicrm.org
* Store results in the cache file
*/
private function pingBack() {
$params = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($this->stats),
),
);
$ctx = stream_context_create($params);
$rawJson = file_get_contents($this->pingbackUrl, 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.
// Note the array may be empty e.g. in the case of a pre-alpha with no releases
$this->isInfoAvailable = $versionInfo !== NULL;
if ($this->isInfoAvailable) {
$this->writeCacheFile($rawJson);
$this->setVersionInfo($versionInfo);
}
}
/**
* @return bool
*/
private function readCacheFile() {
if (file_exists($this->cacheFile)) {
$this->setVersionInfo(json_decode(file_get_contents($this->cacheFile), TRUE));
return TRUE;
}
return FALSE;
}
/**
* Save version info to file.
* @param string $contents
* @throws \Exception
*/
private function writeCacheFile($contents) {
if (file_put_contents($this->cacheFile, $contents) === FALSE) {
throw new Exception('File not writable');
}
}
/**
* Lookup version_check scheduled job
*/
private function getJob() {
$jobs = civicrm_api3('Job', 'get', array(
'sequential' => 1,
'api_action' => "version_check",
'api_entity' => "job",
));
$this->cronJob = CRM_Utils_Array::value(0, $jobs['values'], array());
}
}