ApplicationUtils.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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($currentVersion,
  83. $updateFile,
  84. $checkInterval,
  85. $enableCheck,
  86. $isLoggedIn,
  87. $branch='stable')
  88. {
  89. // Do not check versions for visitors
  90. // Do not check if the user doesn't want to
  91. // Do not check with dev version
  92. if (! $isLoggedIn || empty($enableCheck) || $currentVersion === 'dev') {
  93. return false;
  94. }
  95. if (is_file($updateFile) && (filemtime($updateFile) > time() - $checkInterval)) {
  96. // Shaarli has checked for updates recently - skip HTTP query
  97. $latestKnownVersion = file_get_contents($updateFile);
  98. if (version_compare($latestKnownVersion, $currentVersion) == 1) {
  99. return $latestKnownVersion;
  100. }
  101. return false;
  102. }
  103. if (! in_array($branch, self::$GIT_BRANCHES)) {
  104. throw new Exception(
  105. 'Invalid branch selected for updates: "' . $branch . '"'
  106. );
  107. }
  108. // Late Static Binding allows overriding within tests
  109. // See http://php.net/manual/en/language.oop5.late-static-bindings.php
  110. $latestVersion = static::getVersion(
  111. self::$GIT_URL . '/' . $branch . '/' . self::$VERSION_FILE
  112. );
  113. if (! $latestVersion) {
  114. // Only update the file's modification date
  115. file_put_contents($updateFile, $currentVersion);
  116. return false;
  117. }
  118. // Update the file's content and modification date
  119. file_put_contents($updateFile, $latestVersion);
  120. if (version_compare($latestVersion, $currentVersion) == 1) {
  121. return $latestVersion;
  122. }
  123. return false;
  124. }
  125. /**
  126. * Checks the PHP version to ensure Shaarli can run
  127. *
  128. * @param string $minVersion minimum PHP required version
  129. * @param string $curVersion current PHP version (use PHP_VERSION)
  130. *
  131. * @throws Exception the PHP version is not supported
  132. */
  133. public static function checkPHPVersion($minVersion, $curVersion)
  134. {
  135. if (version_compare($curVersion, $minVersion) < 0) {
  136. $msg = t(
  137. 'Your PHP version is obsolete!'
  138. . ' Shaarli requires at least PHP %s, and thus cannot run.'
  139. . ' Your PHP version has known security vulnerabilities and should be'
  140. . ' updated as soon as possible.'
  141. );
  142. throw new Exception(sprintf($msg, $minVersion));
  143. }
  144. }
  145. /**
  146. * Checks Shaarli has the proper access permissions to its resources
  147. *
  148. * @param ConfigManager $conf Configuration Manager instance.
  149. *
  150. * @return array A list of the detected configuration issues
  151. */
  152. public static function checkResourcePermissions($conf)
  153. {
  154. $errors = array();
  155. $rainTplDir = rtrim($conf->get('resource.raintpl_tpl'), '/');
  156. // Check script and template directories are readable
  157. foreach (array(
  158. 'application',
  159. 'inc',
  160. 'plugins',
  161. $rainTplDir,
  162. $rainTplDir.'/'.$conf->get('resource.theme'),
  163. ) as $path) {
  164. if (! is_readable(realpath($path))) {
  165. $errors[] = '"'.$path.'" '. t('directory is not readable');
  166. }
  167. }
  168. // Check cache and data directories are readable and writable
  169. foreach (array(
  170. $conf->get('resource.thumbnails_cache'),
  171. $conf->get('resource.data_dir'),
  172. $conf->get('resource.page_cache'),
  173. $conf->get('resource.raintpl_tmp'),
  174. ) as $path) {
  175. if (! is_readable(realpath($path))) {
  176. $errors[] = '"'.$path.'" '. t('directory is not readable');
  177. }
  178. if (! is_writable(realpath($path))) {
  179. $errors[] = '"'.$path.'" '. t('directory is not writable');
  180. }
  181. }
  182. // Check configuration files are readable and writable
  183. foreach (array(
  184. $conf->getConfigFileExt(),
  185. $conf->get('resource.datastore'),
  186. $conf->get('resource.ban_file'),
  187. $conf->get('resource.log'),
  188. $conf->get('resource.update_check'),
  189. ) as $path) {
  190. if (! is_file(realpath($path))) {
  191. # the file may not exist yet
  192. continue;
  193. }
  194. if (! is_readable(realpath($path))) {
  195. $errors[] = '"'.$path.'" '. t('file is not readable');
  196. }
  197. if (! is_writable(realpath($path))) {
  198. $errors[] = '"'.$path.'" '. t('file is not writable');
  199. }
  200. }
  201. return $errors;
  202. }
  203. /**
  204. * Returns a salted hash representing the current Shaarli version.
  205. *
  206. * Useful for assets browser cache.
  207. *
  208. * @param string $currentVersion of Shaarli
  209. * @param string $salt User personal salt, also used for the authentication
  210. *
  211. * @return string version hash
  212. */
  213. public static function getVersionHash($currentVersion, $salt)
  214. {
  215. return hash_hmac('sha256', $currentVersion, $salt);
  216. }
  217. }