ApplicationUtils.php 8.2 KB

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