Merge pull request #468 from mlutfy/fix-43-case-ts
[civicrm-core.git] / CRM / Utils / VersionCheck.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id: $
33 *
34 */
35class CRM_Utils_VersionCheck {
36 CONST
37 LATEST_VERSION_AT = 'http://latest.civicrm.org/stable.php',
38 // timeout for when the connection or the server is slow
39 CHECK_TIMEOUT = 5,
40 // relative to $civicrm_root
41 LOCALFILE_NAME = 'civicrm-version.php',
42 // relative to $config->uploadDir
43 CACHEFILE_NAME = 'latest-version-cache.txt',
44 // cachefile expiry time (in seconds) - a week
45 CACHEFILE_EXPIRE = 604800;
46
47 /**
48 * We only need one instance of this object, so we use the
49 * singleton pattern and cache the instance in this variable
50 *
51 * @var object
52 * @static
53 */
54 static private $_singleton = NULL;
55
56 /**
57 * The version of the current (local) installation
58 *
59 * @var string
60 */
61 public $localVersion = NULL;
62
63 /**
64 * The latest version of CiviCRM
65 *
66 * @var string
67 */
68 public $latestVersion = NULL;
69
70 /**
71 * Pingback params
72 *
73 * @var string
74 */
75 protected $stats = array();
76
77 /**
78 * Class constructor
79 *
80 * @access private
81 */
82 function __construct() {
83 global $civicrm_root;
84 $config = CRM_Core_Config::singleton();
85
86 $localfile = $civicrm_root . DIRECTORY_SEPARATOR . self::LOCALFILE_NAME;
87 $cachefile = $config->uploadDir . self::CACHEFILE_NAME;
88
89 if (file_exists($localfile)) {
90 require_once ($localfile);
91 if (function_exists('civicrmVersion')) {
92 $info = civicrmVersion();
8edbc7e6 93 $this->localVersion = trim($info['version']);
6a488035
TO
94 }
95 }
96 if ($config->versionCheck) {
97 $expiryTime = time() - self::CACHEFILE_EXPIRE;
98
99 // if there's a cachefile and it's not stale use it to
100 // read the latestVersion, else read it from the Internet
101 if (file_exists($cachefile) && (filemtime($cachefile) > $expiryTime)) {
8edbc7e6 102 $this->latestVersion = trim(file_get_contents($cachefile));
6a488035
TO
103 }
104 else {
105 $siteKey = md5(defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY : '');
106
107 $this->stats = array(
108 'hash' => md5($siteKey . $config->userFrameworkBaseURL),
109 'version' => $this->localVersion,
110 'uf' => $config->userFramework,
111 'lang' => $config->lcMessages,
112 'co' => $config->defaultContactCountry,
113 'ufv' => $config->userFrameworkVersion,
114 'PHP' => phpversion(),
115 'MySQL' => CRM_CORE_DAO::singleValueQuery('SELECT VERSION()'),
116 );
117
118 // Add usage stats
119 $this->payProcStats();
120 $this->entityStats();
121 $this->extensionStats();
122
123 // Get the latest version and send site info
124 $this->pingBack();
125
126 // Update cache file
127 if ($this->latestVersion) {
128 $fp = @fopen($cachefile, 'w');
129 if (!$fp) {
130 if (CRM_Core_Permission::check('administer CiviCRM')) {
131 CRM_Core_Session::setStatus(
c7692a87 132 ts('Unable to write file') . ":$cachefile<br />" . ts('Please check your system file permissions.'),
6a488035
TO
133 ts('File Error'), 'error');
134 }
135 return;
136 }
137 fwrite($fp, $this->latestVersion);
138 fclose($fp);
139 }
140 }
141 }
142 }
143
144 /**
145 * Static instance provider
146 *
147 * Method providing static instance of CRM_Utils_VersionCheck,
148 * as in Singleton pattern
149 *
150 * @return CRM_Utils_VersionCheck
151 */
152 static function &singleton() {
153 if (!isset(self::$_singleton)) {
154 self::$_singleton = new CRM_Utils_VersionCheck();
155 }
156 return self::$_singleton;
157 }
158
159 /**
160 * Get the latest version number if it's newer than the local one
161 *
162 * @return string|null
163 * Returns the newer version's number or null if the versions are equal
164 */
165 public function newerVersion() {
166 if ($this->latestVersion) {
167 $local = array_pad(explode('.', $this->localVersion), 3, 0);
168 $latest = array_pad(explode('.', $this->latestVersion), 3, 0);
169
170 for ($i = 0; $i < 3; $i++) {
8edbc7e6
CW
171 $loc = (int) $local[$i];
172 $lat = (int) $latest[$i];
173 if ($loc > $lat) {
6a488035
TO
174 return NULL;
175 }
8edbc7e6 176 elseif ($loc < $lat) {
6a488035
TO
177 return $this->latestVersion;
178 }
179 }
180 }
181 return NULL;
182 }
183
184 /**
185 * Alert the site admin of new versions of CiviCRM
186 * Show the message once a day
187 */
188 public function versionAlert() {
189 if (CRM_Core_Permission::check('administer CiviCRM') && $this->newerVersion()) {
190 $session = CRM_Core_Session::singleton();
191 if ($session->timer('version_alert', 24 * 60 * 60)) {
192 $msg = ts('A newer version of CiviCRM is available: %1', array(1 => $this->latestVersion))
193 . '<br />' . ts('<a href="%1">Download Now</a>', array(1 => 'http://civicrm.org/download'));
194 $session->setStatus($msg, ts('Update Available'));
195 }
196 }
197 }
198
199 /**
200 * Get active payment processor types
201 */
202 private function payProcStats() {
203 $dao = new CRM_Financial_DAO_PaymentProcessor;
204 $dao->is_active = 1;
205 $dao->find();
206 $ppTypes = array();
207
208 // Get title and id for all processor types
209 $ppTypeNames = CRM_Core_PseudoConstant::paymentProcessorType();
210
211 while ($dao->fetch()) {
212 $ppTypes[] = $ppTypeNames[$dao->payment_processor_type_id];
213 }
214 // add the .-separated list of the processor types
215 $this->stats['PPTypes'] = implode(',', array_unique($ppTypes));
216
217 }
218
219 /**
220 * Fetch counts from entity tables
221 * Add info to the 'entities' array
222 */
223 private function entityStats() {
224 $tables = array(
225 'CRM_Activity_DAO_Activity' => 'is_test = 0',
226 'CRM_Case_DAO_Case' => 'is_deleted = 0',
227 'CRM_Contact_DAO_Contact' => 'is_deleted = 0',
228 'CRM_Contact_DAO_Relationship' => NULL,
229 'CRM_Campaign_DAO_Campaign' => NULL,
230 'CRM_Contribute_DAO_Contribution' => 'is_test = 0',
231 'CRM_Contribute_DAO_ContributionPage' => 'is_active = 1',
232 'CRM_Contribute_DAO_ContributionProduct' => NULL,
233 'CRM_Contribute_DAO_Widget' => 'is_active = 1',
234 'CRM_Core_DAO_Discount' => NULL,
235 'CRM_Price_DAO_SetEntity' => NULL,
236 'CRM_Core_DAO_UFGroup' => 'is_active = 1',
237 'CRM_Event_DAO_Event' => 'is_active = 1',
238 'CRM_Event_DAO_Participant' => 'is_test = 0',
239 'CRM_Friend_DAO_Friend' => 'is_active = 1',
240 'CRM_Grant_DAO_Grant' => NULL,
241 'CRM_Mailing_DAO_Mailing' => 'is_completed = 1',
242 'CRM_Member_DAO_Membership' => 'is_test = 0',
243 'CRM_Member_DAO_MembershipBlock' => 'is_active = 1',
244 'CRM_Pledge_DAO_Pledge' => 'is_test = 0',
245 'CRM_Pledge_DAO_PledgeBlock' => NULL,
246 );
247 foreach ($tables as $daoName => $where) {
248 $dao = new $daoName;
249 if ($where) {
250 $dao->whereAdd($where);
251 }
252 $short_name = substr($daoName, strrpos($daoName, '_') + 1);
253 $this->stats['entities'][] = array(
254 'name' => $short_name,
255 'size' => $dao->count(),
256 );
257 }
258 }
259
260 /**
261 * Fetch stats about enabled components/extensions
262 * Add info to the 'extensions' array
263 */
264 private function extensionStats() {
265 // Core components
266 $config = CRM_Core_Config::singleton();
267 foreach ($config->enableComponents as $comp) {
268 $this->stats['extensions'][] = array(
269 'name' => 'org.civicrm.component.' . strtolower($comp),
270 'enabled' => 1,
271 'version' => $this->stats['version'],
272 );
273 }
274 // Contrib extensions
275 $mapper = CRM_Extension_System::singleton()->getMapper();
276 $dao = new CRM_Core_DAO_Extension();
277 $dao->find();
278 while ($dao->fetch()) {
279 $info = $mapper->keyToInfo($dao->full_name);
280 $this->stats['extensions'][] = array(
281 'name' => $dao->full_name,
282 'enabled' => $dao->is_active,
283 'version' => isset($info->version) ? $info->version : NULL,
284 );
285 }
286 }
287
288 /**
289 * Send the request to civicrm.org
290 * Set timeout and suppress errors
291 */
292 private function pingBack() {
293 ini_set('default_socket_timeout', self::CHECK_TIMEOUT);
294 $params = array(
295 'http' => array(
296 'method' => 'POST',
297 'header' => 'Content-type: application/x-www-form-urlencoded',
298 'content' => http_build_query($this->stats),
299 ),
300 );
301 $ctx = stream_context_create($params);
302 $this->latestVersion = @file_get_contents(self::LATEST_VERSION_AT, FALSE, $ctx);
303 if (!preg_match('/^\d+\.\d+\.\d+$/', $this->latestVersion)) {
304 $this->latestVersion = NULL;
305 }
306 ini_restore('default_socket_timeout');
307 }
308
309}