3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
33 class CRM_Utils_VersionCheck
{
35 CACHEFILE_NAME
= 'version-msgs-cache.json',
36 // After which length of time we expire the cached version info (3 days).
37 CACHEFILE_EXPIRE
= 259200;
40 * The version of the current (local) installation
44 public $localVersion = NULL;
47 * Info about available versions
51 public $versionInfo = array();
56 public $isInfoAvailable;
61 public $cronJob = array();
66 public $pingbackUrl = 'https://latest.civicrm.org/stable.php?format=summary';
73 protected $stats = array();
85 public function __construct() {
86 $this->localVersion
= CRM_Utils_System
::version();
87 $this->cacheFile
= CRM_Core_Config
::singleton()->uploadDir
. self
::CACHEFILE_NAME
;
91 * Self-populates version info
95 public function initialize() {
98 // Populate remote $versionInfo from cache file
99 $this->isInfoAvailable
= $this->readCacheFile();
101 // Fallback if scheduled job is enabled but has failed to run.
102 $expiryTime = time() - self
::CACHEFILE_EXPIRE
;
103 if (!empty($this->cronJob
['is_active']) &&
104 (!$this->isInfoAvailable ||
filemtime($this->cacheFile
) < $expiryTime)
106 // First try updating the files modification time, for 2 reasons:
107 // - if the file is not writeable, this saves the trouble of pinging back
108 // - if the remote server is down, this will prevent an immediate retry
109 if (touch($this->cacheFile
) === FALSE) {
110 throw new Exception('File not writable');
121 protected function setVersionInfo($info) {
122 $this->versionInfo
= $info;
130 * Ex: 'info', 'notice', 'warning', 'critical'.
132 public function getVersionMessages() {
133 return $this->isInfoAvailable ?
$this->versionInfo
: NULL;
137 * Called by version_check cron job
139 public function fetch() {
140 $this->getSiteStats();
145 * Collect info about the site to be sent as pingback data.
147 private function getSiteStats() {
148 $config = CRM_Core_Config
::singleton();
149 $siteKey = md5(defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY
: '');
151 // Calorie-free pingback for alphas
152 $this->stats
= array('version' => $this->localVersion
);
154 // Non-alpha versions get the full treatment
155 if ($this->localVersion
&& !strpos($this->localVersion
, 'alpha')) {
156 $this->stats +
= array(
157 'hash' => md5($siteKey . $config->userFrameworkBaseURL
),
158 'uf' => $config->userFramework
,
159 'lang' => $config->lcMessages
,
160 'co' => $config->defaultContactCountry
,
161 'ufv' => $config->userSystem
->getVersion(),
162 'PHP' => phpversion(),
163 'MySQL' => CRM_CORE_DAO
::singleValueQuery('SELECT VERSION()'),
164 'communityMessagesUrl' => Civi
::settings()->get('communityMessagesUrl'),
166 $this->getDomainStats();
167 $this->getPayProcStats();
168 $this->getEntityStats();
169 $this->getExtensionStats();
174 * Get active payment processor types.
176 private function getPayProcStats() {
177 $dao = new CRM_Financial_DAO_PaymentProcessor();
182 // Get title for all processor types
183 // FIXME: This should probably be getName, but it has always returned translated label so we stick with that for now as it would affect stats
184 while ($dao->fetch()) {
185 $ppTypes[] = CRM_Core_PseudoConstant
::getLabel('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', $dao->payment_processor_type_id
);
187 // add the .-separated list of the processor types
188 $this->stats
['PPTypes'] = implode(',', array_unique($ppTypes));
192 * Fetch counts from entity tables.
193 * Add info to the 'entities' array
195 private function getEntityStats() {
197 'CRM_Activity_DAO_Activity' => 'is_test = 0',
198 'CRM_Case_DAO_Case' => 'is_deleted = 0',
199 'CRM_Contact_DAO_Contact' => 'is_deleted = 0',
200 'CRM_Contact_DAO_Relationship' => NULL,
201 'CRM_Campaign_DAO_Campaign' => NULL,
202 'CRM_Contribute_DAO_Contribution' => 'is_test = 0',
203 'CRM_Contribute_DAO_ContributionPage' => 'is_active = 1',
204 'CRM_Contribute_DAO_ContributionProduct' => NULL,
205 'CRM_Contribute_DAO_Widget' => 'is_active = 1',
206 'CRM_Core_DAO_Discount' => NULL,
207 'CRM_Price_DAO_PriceSetEntity' => NULL,
208 'CRM_Core_DAO_UFGroup' => 'is_active = 1',
209 'CRM_Event_DAO_Event' => 'is_active = 1',
210 'CRM_Event_DAO_Participant' => 'is_test = 0',
211 'CRM_Friend_DAO_Friend' => 'is_active = 1',
212 'CRM_Grant_DAO_Grant' => NULL,
213 'CRM_Mailing_DAO_Mailing' => 'is_completed = 1',
214 'CRM_Member_DAO_Membership' => 'is_test = 0',
215 'CRM_Member_DAO_MembershipBlock' => 'is_active = 1',
216 'CRM_Pledge_DAO_Pledge' => 'is_test = 0',
217 'CRM_Pledge_DAO_PledgeBlock' => NULL,
218 'CRM_Mailing_Event_DAO_Delivered' => NULL,
220 foreach ($tables as $daoName => $where) {
221 $dao = new $daoName();
223 $dao->whereAdd($where);
225 $short_name = substr($daoName, strrpos($daoName, '_') +
1);
226 $this->stats
['entities'][] = array(
227 'name' => $short_name,
228 'size' => $dao->count(),
234 * Fetch stats about enabled components/extensions
235 * Add info to the 'extensions' array
237 private function getExtensionStats() {
239 $config = CRM_Core_Config
::singleton();
240 foreach ($config->enableComponents
as $comp) {
241 $this->stats
['extensions'][] = array(
242 'name' => 'org.civicrm.component.' . strtolower($comp),
244 'version' => $this->stats
['version'],
247 // Contrib extensions
248 $mapper = CRM_Extension_System
::singleton()->getMapper();
249 $dao = new CRM_Core_DAO_Extension();
251 while ($dao->fetch()) {
252 $info = $mapper->keyToInfo($dao->full_name
);
253 $this->stats
['extensions'][] = array(
254 'name' => $dao->full_name
,
255 'enabled' => $dao->is_active
,
256 'version' => isset($info->version
) ?
$info->version
: NULL,
262 * Fetch stats about domain and add to 'stats' array.
264 private function getDomainStats() {
265 // Start with default value NULL, then check to see if there's a better
267 $this->stats
['domain_isoCode'] = NULL;
269 'id' => CRM_Core_Config
::domainID(),
271 $domain_result = civicrm_api3('domain', 'getsingle', $params);
272 if (!empty($domain_result['contact_id'])) {
273 $address_params = array(
274 'contact_id' => $domain_result['contact_id'],
278 $address_result = civicrm_api3('address', 'get', $address_params);
279 if ($address_result['count'] == 1 && !empty($address_result['values'][0]['country_id'])) {
280 $country_params = array(
281 'id' => $address_result['values'][0]['country_id'],
283 $country_result = civicrm_api3('country', 'getsingle', $country_params);
284 if (!empty($country_result['iso_code'])) {
285 $this->stats
['domain_isoCode'] = $country_result['iso_code'];
292 * Send the request to civicrm.org
293 * Store results in the cache file
295 private function pingBack() {
299 'header' => 'Content-type: application/x-www-form-urlencoded',
300 'content' => http_build_query($this->stats
),
303 $ctx = stream_context_create($params);
304 $rawJson = file_get_contents($this->pingbackUrl
, FALSE, $ctx);
305 $versionInfo = $rawJson ?
json_decode($rawJson, TRUE) : NULL;
306 // If we couldn't fetch or parse the data $versionInfo will be NULL
307 // Otherwise it will be an array and we'll cache it.
308 // Note the array may be empty e.g. in the case of a pre-alpha with no releases
309 $this->isInfoAvailable
= $versionInfo !== NULL;
310 if ($this->isInfoAvailable
) {
311 $this->writeCacheFile($rawJson);
312 $this->setVersionInfo($versionInfo);
319 private function readCacheFile() {
320 if (file_exists($this->cacheFile
)) {
321 $this->setVersionInfo(json_decode(file_get_contents($this->cacheFile
), TRUE));
328 * Save version info to file.
329 * @param string $contents
332 private function writeCacheFile($contents) {
333 if (file_put_contents($this->cacheFile
, $contents) === FALSE) {
334 throw new Exception('File not writable');
339 * Removes cached version info.
341 public function flushCache() {
342 if (file_exists($this->cacheFile
)) {
343 unlink($this->cacheFile
);
348 * Lookup version_check scheduled job
350 private function getJob() {
351 $jobs = civicrm_api3('Job', 'get', array(
353 'api_action' => "version_check",
354 'api_entity' => "job",
356 $this->cronJob
= CRM_Utils_Array
::value(0, $jobs['values'], array());