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
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
* @version $Id$
*
* @category Piwik
* @package Piwik
*/
/**
* Class to check if a newer version of Piwik is available
*
* @package Piwik
*/
class Piwik_UpdateCheck
{
const CHECK_INTERVAL = 28800; // every 8 hours
const UI_CLICK_CHECK_INTERVAL = 10; // every 10s when user clicks UI link
const LAST_TIME_CHECKED = 'UpdateCheck_LastTimeChecked';
const LATEST_VERSION = 'UpdateCheck_LatestVersion';
const SOCKET_TIMEOUT = 2;
/**
* Check for a newer version
*
* @param bool $force Force check
*/
public static function check($force = false, $interval = null)
{
if ($interval === null)
{
$interval = self::CHECK_INTERVAL;
}
$lastTimeChecked = Piwik_GetOption(self::LAST_TIME_CHECKED);
if($force
|| $lastTimeChecked === false
|| time() - $interval > $lastTimeChecked )
{
// set the time checked first, so that parallel Piwik requests don't all trigger the http requests
Piwik_SetOption(self::LAST_TIME_CHECKED, time(), $autoload = 1);
$parameters = array(
'piwik_version' => Piwik_Version::VERSION,
'php_version' => PHP_VERSION,
'url' => Piwik_Url::getCurrentUrlWithoutQueryString(),
'trigger' => Piwik_Common::getRequestVar('module','','string'),
'timezone' => Piwik_SitesManager_API::getInstance()->getDefaultTimezone(),
);
$url = Piwik_Config::getInstance()->General['api_service_url']
. '/1.0/getLatestVersion/'
. '?' . http_build_query($parameters, '', '&');
$timeout = self::SOCKET_TIMEOUT;
if(@Piwik_Config::getInstance()->Debug['allow_upgrades_to_beta'])
{
$url = 'http://builds.piwik.org/LATEST_BETA';
}
try {
$latestVersion = Piwik_Http::sendHttpRequest($url, $timeout);
if (!preg_match('~^[0-9][0-9a-zA-Z_.-]*$~D', $latestVersion))
{
$latestVersion = '';
}
} catch(Exception $e) {
// e.g., disable_functions = fsockopen; allow_url_open = Off
$latestVersion = '';
}
Piwik_SetOption(self::LATEST_VERSION, $latestVersion);
}
}
/**
* Returns version number of a newer Piwik release.
*
* @return string|false false if current version is the latest available,
* or the latest version number if a newest release is available
*/
public static function isNewestVersionAvailable()
{
$latestVersion = Piwik_GetOption(self::LATEST_VERSION);
if(!empty($latestVersion)
&& version_compare(Piwik_Version::VERSION, $latestVersion) == -1)
{
return $latestVersion;
}
return false;
}
}
|