Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UpdateCheck.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f381ec6c65ef951d78b45f40746b7de8a7ce5cb6 (plain)
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
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */
namespace Piwik;

use Exception;

use Piwik\Plugins\SitesManager\API;

/**
 * Class to check if a newer version of Piwik is available
 *
 * @package Piwik
 */
class 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
     * @param int $interval  Interval used for update checks
     */
    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' => Version::VERSION,
                'php_version'   => PHP_VERSION,
                'url'           => Url::getCurrentUrlWithoutQueryString(),
                'trigger'       => Common::getRequestVar('module', '', 'string'),
                'timezone'      => API::getInstance()->getDefaultTimezone(),
            );

            $url = Config::getInstance()->General['api_service_url']
                . '/1.0/getLatestVersion/'
                . '?' . http_build_query($parameters, '', '&');
            $timeout = self::SOCKET_TIMEOUT;

            if (@Config::getInstance()->Debug['allow_upgrades_to_beta']) {
                $url = 'http://builds.piwik.org/LATEST_BETA';
            }

            try {
                $latestVersion = 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|bool  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(Version::VERSION, $latestVersion) == -1
        ) {
            return $latestVersion;
        }
        return false;
    }
}