Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateCheck.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. use Exception;
  11. use Piwik\Plugins\SitesManager\API;
  12. /**
  13. * Class to check if a newer version of Piwik is available
  14. *
  15. */
  16. class UpdateCheck
  17. {
  18. const CHECK_INTERVAL = 28800; // every 8 hours
  19. const UI_CLICK_CHECK_INTERVAL = 10; // every 10s when user clicks UI link
  20. const LAST_TIME_CHECKED = 'UpdateCheck_LastTimeChecked';
  21. const LATEST_VERSION = 'UpdateCheck_LatestVersion';
  22. const SOCKET_TIMEOUT = 2;
  23. private static function isAutoUpdateEnabled()
  24. {
  25. return (bool) Config::getInstance()->General['enable_auto_update'];
  26. }
  27. /**
  28. * Check for a newer version
  29. *
  30. * @param bool $force Force check
  31. * @param int $interval Interval used for update checks
  32. */
  33. public static function check($force = false, $interval = null)
  34. {
  35. if(!self::isAutoUpdateEnabled()) {
  36. return;
  37. }
  38. if ($interval === null) {
  39. $interval = self::CHECK_INTERVAL;
  40. }
  41. $lastTimeChecked = Option::get(self::LAST_TIME_CHECKED);
  42. if ($force
  43. || $lastTimeChecked === false
  44. || time() - $interval > $lastTimeChecked
  45. ) {
  46. // set the time checked first, so that parallel Piwik requests don't all trigger the http requests
  47. Option::set(self::LAST_TIME_CHECKED, time(), $autoLoad = 1);
  48. $parameters = array(
  49. 'piwik_version' => Version::VERSION,
  50. 'php_version' => PHP_VERSION,
  51. 'url' => Url::getCurrentUrlWithoutQueryString(),
  52. 'trigger' => Common::getRequestVar('module', '', 'string'),
  53. 'timezone' => API::getInstance()->getDefaultTimezone(),
  54. );
  55. $url = Config::getInstance()->General['api_service_url']
  56. . '/1.0/getLatestVersion/'
  57. . '?' . http_build_query($parameters, '', '&');
  58. $timeout = self::SOCKET_TIMEOUT;
  59. if (@Config::getInstance()->Debug['allow_upgrades_to_beta']) {
  60. $url = 'http://builds.piwik.org/LATEST_BETA';
  61. }
  62. try {
  63. $latestVersion = Http::sendHttpRequest($url, $timeout);
  64. if (!preg_match('~^[0-9][0-9a-zA-Z_.-]*$~D', $latestVersion)) {
  65. $latestVersion = '';
  66. }
  67. } catch (Exception $e) {
  68. // e.g., disable_functions = fsockopen; allow_url_open = Off
  69. $latestVersion = '';
  70. }
  71. Option::set(self::LATEST_VERSION, $latestVersion);
  72. }
  73. }
  74. /**
  75. * Returns the latest available version number. Does not perform a check whether a later version is available.
  76. *
  77. * @return false|string
  78. */
  79. public static function getLatestVersion()
  80. {
  81. return Option::get(self::LATEST_VERSION);
  82. }
  83. /**
  84. * Returns version number of a newer Piwik release.
  85. *
  86. * @return string|bool false if current version is the latest available,
  87. * or the latest version number if a newest release is available
  88. */
  89. public static function isNewestVersionAvailable()
  90. {
  91. $latestVersion = self::getLatestVersion();
  92. if (!empty($latestVersion)
  93. && version_compare(Version::VERSION, $latestVersion) == -1
  94. ) {
  95. return $latestVersion;
  96. }
  97. return false;
  98. }
  99. }