ApplicationUtils.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Shaarli (application) utilities
  4. */
  5. class ApplicationUtils
  6. {
  7. private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli';
  8. private static $GIT_BRANCHES = array('master', 'stable');
  9. private static $VERSION_FILE = 'shaarli_version.php';
  10. private static $VERSION_START_TAG = '<?php /* ';
  11. private static $VERSION_END_TAG = ' */ ?>';
  12. /**
  13. * Gets the latest version code from the Git repository
  14. *
  15. * The code is read from the raw content of the version file on the Git server.
  16. *
  17. * @param string $url URL to reach to get the latest version.
  18. * @param int $timeout Timeout to check the URL (in seconds).
  19. *
  20. * @return mixed the version code from the repository if available, else 'false'
  21. */
  22. public static function getLatestGitVersionCode($url, $timeout=2)
  23. {
  24. list($headers, $data) = get_http_response($url, $timeout);
  25. if (strpos($headers[0], '200 OK') === false) {
  26. error_log('Failed to retrieve ' . $url);
  27. return false;
  28. }
  29. return str_replace(
  30. array(self::$VERSION_START_TAG, self::$VERSION_END_TAG, PHP_EOL),
  31. array('', '', ''),
  32. $data
  33. );
  34. }
  35. /**
  36. * Checks if a new Shaarli version has been published on the Git repository
  37. *
  38. * Updates checks are run periodically, according to the following criteria:
  39. * - the update checks are enabled (install, global config);
  40. * - the user is logged in (or this is an open instance);
  41. * - the last check is older than a given interval;
  42. * - the check is non-blocking if the HTTPS connection to Git fails;
  43. * - in case of failure, the update file's modification date is updated,
  44. * to avoid intempestive connection attempts.
  45. *
  46. * @param string $currentVersion the current version code
  47. * @param string $updateFile the file where to store the latest version code
  48. * @param int $checkInterval the minimum interval between update checks (in seconds
  49. * @param bool $enableCheck whether to check for new versions
  50. * @param bool $isLoggedIn whether the user is logged in
  51. * @param string $branch check update for the given branch
  52. *
  53. * @throws Exception an invalid branch has been set for update checks
  54. *
  55. * @return mixed the new version code if available and greater, else 'false'
  56. */
  57. public static function checkUpdate($currentVersion,
  58. $updateFile,
  59. $checkInterval,
  60. $enableCheck,
  61. $isLoggedIn,
  62. $branch='stable')
  63. {
  64. if (! $isLoggedIn) {
  65. // Do not check versions for visitors
  66. return false;
  67. }
  68. if (empty($enableCheck)) {
  69. // Do not check if the user doesn't want to
  70. return false;
  71. }
  72. if (is_file($updateFile) && (filemtime($updateFile) > time() - $checkInterval)) {
  73. // Shaarli has checked for updates recently - skip HTTP query
  74. $latestKnownVersion = file_get_contents($updateFile);
  75. if (version_compare($latestKnownVersion, $currentVersion) == 1) {
  76. return $latestKnownVersion;
  77. }
  78. return false;
  79. }
  80. if (! in_array($branch, self::$GIT_BRANCHES)) {
  81. throw new Exception(
  82. 'Invalid branch selected for updates: "' . $branch . '"'
  83. );
  84. }
  85. // Late Static Binding allows overriding within tests
  86. // See http://php.net/manual/en/language.oop5.late-static-bindings.php
  87. $latestVersion = static::getLatestGitVersionCode(
  88. self::$GIT_URL . '/' . $branch . '/' . self::$VERSION_FILE
  89. );
  90. if (! $latestVersion) {
  91. // Only update the file's modification date
  92. file_put_contents($updateFile, $currentVersion);
  93. return false;
  94. }
  95. // Update the file's content and modification date
  96. file_put_contents($updateFile, $latestVersion);
  97. if (version_compare($latestVersion, $currentVersion) == 1) {
  98. return $latestVersion;
  99. }
  100. return false;
  101. }
  102. /**
  103. * Checks the PHP version to ensure Shaarli can run
  104. *
  105. * @param string $minVersion minimum PHP required version
  106. * @param string $curVersion current PHP version (use PHP_VERSION)
  107. *
  108. * @throws Exception the PHP version is not supported
  109. */
  110. public static function checkPHPVersion($minVersion, $curVersion)
  111. {
  112. if (version_compare($curVersion, $minVersion) < 0) {
  113. throw new Exception(
  114. 'Your PHP version is obsolete!'
  115. .' Shaarli requires at least PHP '.$minVersion.', and thus cannot run.'
  116. .' Your PHP version has known security vulnerabilities and should be'
  117. .' updated as soon as possible.'
  118. );
  119. }
  120. }
  121. /**
  122. * Checks Shaarli has the proper access permissions to its resources
  123. *
  124. * @param ConfigManager $conf Configuration Manager instance.
  125. *
  126. * @return array A list of the detected configuration issues
  127. */
  128. public static function checkResourcePermissions($conf)
  129. {
  130. $errors = array();
  131. // Check script and template directories are readable
  132. foreach (array(
  133. 'application',
  134. 'inc',
  135. 'plugins',
  136. $conf->get('resource.raintpl_tpl'),
  137. ) as $path) {
  138. if (! is_readable(realpath($path))) {
  139. $errors[] = '"'.$path.'" directory is not readable';
  140. }
  141. }
  142. // Check cache and data directories are readable and writable
  143. foreach (array(
  144. $conf->get('resource.thumbnails_cache'),
  145. $conf->get('resource.data_dir'),
  146. $conf->get('resource.page_cache'),
  147. $conf->get('resource.raintpl_tmp'),
  148. ) as $path) {
  149. if (! is_readable(realpath($path))) {
  150. $errors[] = '"'.$path.'" directory is not readable';
  151. }
  152. if (! is_writable(realpath($path))) {
  153. $errors[] = '"'.$path.'" directory is not writable';
  154. }
  155. }
  156. // Check configuration files are readable and writable
  157. foreach (array(
  158. $conf->getConfigFileExt(),
  159. $conf->get('resource.datastore'),
  160. $conf->get('resource.ban_file'),
  161. $conf->get('resource.log'),
  162. $conf->get('resource.update_check'),
  163. ) as $path) {
  164. if (! is_file(realpath($path))) {
  165. # the file may not exist yet
  166. continue;
  167. }
  168. if (! is_readable(realpath($path))) {
  169. $errors[] = '"'.$path.'" file is not readable';
  170. }
  171. if (! is_writable(realpath($path))) {
  172. $errors[] = '"'.$path.'" file is not writable';
  173. }
  174. }
  175. return $errors;
  176. }
  177. }