ApplicationUtils.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. throw new Exception(
  137. 'Your PHP version is obsolete!'
  138. .' Shaarli requires at least PHP '.$minVersion.', and thus cannot run.'
  139. .' Your PHP version has known security vulnerabilities and should be'
  140. .' updated as soon as possible.'
  141. );
  142. }
  143. }
  144. /**
  145. * Checks Shaarli has the proper access permissions to its resources
  146. *
  147. * @param ConfigManager $conf Configuration Manager instance.
  148. *
  149. * @return array A list of the detected configuration issues
  150. */
  151. public static function checkResourcePermissions($conf)
  152. {
  153. $errors = array();
  154. $rainTplDir = rtrim($conf->get('resource.raintpl_tpl'), '/');
  155. // Check script and template directories are readable
  156. foreach (array(
  157. 'application',
  158. 'inc',
  159. 'plugins',
  160. $rainTplDir,
  161. $rainTplDir.'/'.$conf->get('resource.theme'),
  162. ) as $path) {
  163. if (! is_readable(realpath($path))) {
  164. $errors[] = '"'.$path.'" directory is not readable';
  165. }
  166. }
  167. // Check cache and data directories are readable and writable
  168. foreach (array(
  169. $conf->get('resource.thumbnails_cache'),
  170. $conf->get('resource.data_dir'),
  171. $conf->get('resource.page_cache'),
  172. $conf->get('resource.raintpl_tmp'),
  173. ) as $path) {
  174. if (! is_readable(realpath($path))) {
  175. $errors[] = '"'.$path.'" directory is not readable';
  176. }
  177. if (! is_writable(realpath($path))) {
  178. $errors[] = '"'.$path.'" directory is not writable';
  179. }
  180. }
  181. // Check configuration files are readable and writable
  182. foreach (array(
  183. $conf->getConfigFileExt(),
  184. $conf->get('resource.datastore'),
  185. $conf->get('resource.ban_file'),
  186. $conf->get('resource.log'),
  187. $conf->get('resource.update_check'),
  188. ) as $path) {
  189. if (! is_file(realpath($path))) {
  190. # the file may not exist yet
  191. continue;
  192. }
  193. if (! is_readable(realpath($path))) {
  194. $errors[] = '"'.$path.'" file is not readable';
  195. }
  196. if (! is_writable(realpath($path))) {
  197. $errors[] = '"'.$path.'" file is not writable';
  198. }
  199. }
  200. return $errors;
  201. }
  202. }