updateController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_update_Controller extends FreshRSS_ActionController {
  4. private const LASTUPDATEFILE = 'last_update.txt';
  5. public static function isGit(): bool {
  6. return is_dir(FRESHRSS_PATH . '/.git/');
  7. }
  8. /**
  9. * Automatic change to the new name of edge branch since FreshRSS 1.18.0.
  10. * @throws Minz_Exception
  11. */
  12. public static function migrateToGitEdge(): bool {
  13. $errorMessage = 'Error during git checkout to edge branch. Please change branch manually!';
  14. if (!is_writable(FRESHRSS_PATH . '/.git/config')) {
  15. throw new Minz_Exception($errorMessage);
  16. }
  17. //Note `git branch --show-current` requires git 2.22+
  18. exec('git symbolic-ref --short HEAD', $output, $return);
  19. if ($return != 0) {
  20. throw new Minz_Exception($errorMessage);
  21. }
  22. $line = implode('', $output);
  23. if ($line !== 'master' && $line !== 'dev') {
  24. return true; // not on master or dev, nothing to do
  25. }
  26. Minz_Log::warning('Automatic migration to git edge branch');
  27. unset($output);
  28. exec('git checkout edge --guess -f', $output, $return);
  29. if ($return != 0) {
  30. throw new Minz_Exception($errorMessage);
  31. }
  32. unset($output);
  33. exec('git reset --hard FETCH_HEAD', $output, $return);
  34. if ($return != 0) {
  35. throw new Minz_Exception($errorMessage);
  36. }
  37. return true;
  38. }
  39. public static function getCurrentGitBranch(): string {
  40. $output = [];
  41. exec('git branch --show-current', $output, $return);
  42. if ($return === 0) {
  43. return 'git branch: ' . $output[0];
  44. } else {
  45. return 'git';
  46. }
  47. }
  48. public static function hasGitUpdate(): bool {
  49. $cwd = getcwd();
  50. if ($cwd === false) {
  51. Minz_Log::warning('getcwd() failed');
  52. return false;
  53. }
  54. chdir(FRESHRSS_PATH);
  55. $output = [];
  56. try {
  57. /** @throws ValueError */
  58. exec('git fetch --prune', $output, $return);
  59. if ($return == 0) {
  60. $output = [];
  61. exec('git status -sb --porcelain remote', $output, $return);
  62. } else {
  63. $line = implode('; ', $output);
  64. Minz_Log::warning('git fetch warning: ' . $line);
  65. }
  66. } catch (Throwable $e) {
  67. Minz_Log::warning('git fetch error: ' . $e->getMessage());
  68. }
  69. chdir($cwd);
  70. $line = implode('; ', $output);
  71. return $line == '' ||
  72. strpos($line, '[behind') !== false || strpos($line, '[ahead') !== false || strpos($line, '[gone') !== false;
  73. }
  74. /** @return string|true */
  75. public static function gitPull() {
  76. Minz_Log::notice(_t('admin.update.viaGit'));
  77. $cwd = getcwd();
  78. if ($cwd === false) {
  79. Minz_Log::warning('getcwd() failed');
  80. return 'getcwd() failed';
  81. }
  82. chdir(FRESHRSS_PATH);
  83. $output = [];
  84. $return = 1;
  85. try {
  86. exec('git fetch --prune', $output, $return);
  87. if ($return == 0) {
  88. $output = [];
  89. exec('git reset --hard FETCH_HEAD', $output, $return);
  90. }
  91. $output = [];
  92. self::migrateToGitEdge();
  93. } catch (Throwable $e) {
  94. Minz_Log::warning('Git error: ' . $e->getMessage());
  95. $output = $e->getMessage();
  96. $return = 1;
  97. }
  98. chdir($cwd);
  99. $line = is_array($output) ? implode('; ', $output) : $output;
  100. return $return == 0 ? true : 'Git error: ' . $line;
  101. }
  102. #[\Override]
  103. public function firstAction(): void {
  104. if (!FreshRSS_Auth::hasAccess('admin')) {
  105. Minz_Error::error(403);
  106. }
  107. include_once(LIB_PATH . '/lib_install.php');
  108. invalidateHttpCache();
  109. $this->view->is_release_channel_stable = $this->is_release_channel_stable(FRESHRSS_VERSION);
  110. $this->view->update_to_apply = false;
  111. $this->view->last_update_time = 'unknown';
  112. $timestamp = @filemtime(join_path(DATA_PATH, self::LASTUPDATEFILE));
  113. if ($timestamp !== false) {
  114. $this->view->last_update_time = timestamptodate($timestamp);
  115. }
  116. }
  117. public function indexAction(): void {
  118. FreshRSS_View::prependTitle(_t('admin.update.title') . ' · ');
  119. if (file_exists(UPDATE_FILENAME)) {
  120. // There is an update file to apply!
  121. $version = @file_get_contents(join_path(DATA_PATH, self::LASTUPDATEFILE));
  122. if ($version == '') {
  123. $version = 'unknown';
  124. }
  125. if (touch(FRESHRSS_PATH . '/index.html')) {
  126. $this->view->update_to_apply = true;
  127. $this->view->message = [
  128. 'status' => 'good',
  129. 'title' => _t('gen.short.ok'),
  130. 'body' => _t('feedback.update.can_apply', $version),
  131. ];
  132. } else {
  133. $this->view->message = [
  134. 'status' => 'bad',
  135. 'title' => _t('gen.short.damn'),
  136. 'body' => _t('feedback.update.file_is_nok', $version, FRESHRSS_PATH),
  137. ];
  138. }
  139. }
  140. }
  141. private function is_release_channel_stable(string $currentVersion): bool {
  142. return strpos($currentVersion, 'dev') === false &&
  143. strpos($currentVersion, 'edge') === false;
  144. }
  145. /* Check installation if there is a newer version.
  146. via Git, if available.
  147. Else via system configuration auto_update_url
  148. */
  149. public function checkAction(): void {
  150. FreshRSS_View::prependTitle(_t('admin.update.title') . ' · ');
  151. $this->view->_path('update/index.phtml');
  152. if (file_exists(UPDATE_FILENAME)) {
  153. // There is already an update file to apply: we don’t need to check
  154. // the webserver!
  155. // Or if already check during the last hour, do nothing.
  156. Minz_Request::forward(['c' => 'update'], true);
  157. return;
  158. }
  159. $script = '';
  160. if (self::isGit()) {
  161. if (self::hasGitUpdate()) {
  162. $version = self::getCurrentGitBranch();
  163. } else {
  164. $this->view->message = [
  165. 'status' => 'latest',
  166. 'body' => _t('feedback.update.none'),
  167. ];
  168. @touch(join_path(DATA_PATH, self::LASTUPDATEFILE));
  169. return;
  170. }
  171. } else {
  172. $auto_update_url = FreshRSS_Context::systemConf()->auto_update_url . '/?v=' . FRESHRSS_VERSION;
  173. Minz_Log::debug('HTTP GET ' . $auto_update_url);
  174. $curlResource = curl_init($auto_update_url);
  175. if ($curlResource === false) {
  176. Minz_Log::warning('curl_init() failed');
  177. $this->view->message = [
  178. 'status' => 'bad',
  179. 'title' => _t('gen.short.damn'),
  180. 'body' => _t('feedback.update.server_not_found', $auto_update_url)
  181. ];
  182. return;
  183. }
  184. curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true);
  185. curl_setopt($curlResource, CURLOPT_SSL_VERIFYPEER, true);
  186. curl_setopt($curlResource, CURLOPT_SSL_VERIFYHOST, 2);
  187. $result = curl_exec($curlResource);
  188. $curlGetinfo = curl_getinfo($curlResource, CURLINFO_HTTP_CODE);
  189. $curlError = curl_error($curlResource);
  190. curl_close($curlResource);
  191. if ($curlGetinfo !== 200) {
  192. Minz_Log::warning(
  193. 'Error during update (HTTP code ' . $curlGetinfo . '): ' . $curlError
  194. );
  195. $this->view->message = [
  196. 'status' => 'bad',
  197. 'body' => _t('feedback.update.server_not_found', $auto_update_url),
  198. ];
  199. return;
  200. }
  201. $res_array = explode("\n", (string)$result, 2);
  202. $status = $res_array[0];
  203. if (strpos($status, 'UPDATE') !== 0) {
  204. $this->view->message = [
  205. 'status' => 'latest',
  206. 'body' => _t('feedback.update.none'),
  207. ];
  208. @touch(join_path(DATA_PATH, self::LASTUPDATEFILE));
  209. return;
  210. }
  211. $script = $res_array[1];
  212. $version = explode(' ', $status, 2);
  213. $version = $version[1];
  214. Minz_Log::notice(_t('admin.update.copiedFromURL', $auto_update_url));
  215. }
  216. if (file_put_contents(UPDATE_FILENAME, $script) !== false) {
  217. @file_put_contents(join_path(DATA_PATH, self::LASTUPDATEFILE), $version);
  218. Minz_Request::forward(['c' => 'update'], true);
  219. } else {
  220. $this->view->message = [
  221. 'status' => 'bad',
  222. 'body' => _t('feedback.update.error', 'Cannot save the update script'),
  223. ];
  224. }
  225. }
  226. public function applyAction(): void {
  227. if (FreshRSS_Context::systemConf()->disable_update || !file_exists(UPDATE_FILENAME) || !touch(FRESHRSS_PATH . '/index.html')) {
  228. Minz_Request::forward(['c' => 'update'], true);
  229. }
  230. if (Minz_Request::paramBoolean('post_conf')) {
  231. if (self::isGit()) {
  232. $res = !self::hasGitUpdate();
  233. } else {
  234. require(UPDATE_FILENAME);
  235. // @phpstan-ignore function.notFound
  236. $res = do_post_update();
  237. }
  238. Minz_ExtensionManager::callHookVoid('post_update');
  239. if ($res === true) {
  240. @unlink(UPDATE_FILENAME);
  241. @file_put_contents(join_path(DATA_PATH, self::LASTUPDATEFILE), '');
  242. Minz_Log::notice(_t('feedback.update.finished'));
  243. Minz_Request::good(_t('feedback.update.finished'));
  244. } else {
  245. Minz_Log::error(_t('feedback.update.error', $res));
  246. Minz_Request::bad(_t('feedback.update.error', $res), [ 'c' => 'update', 'a' => 'index' ]);
  247. }
  248. } else {
  249. $res = false;
  250. if (self::isGit()) {
  251. $res = self::gitPull();
  252. } else {
  253. require(UPDATE_FILENAME);
  254. if (Minz_Request::isPost()) {
  255. // @phpstan-ignore function.notFound
  256. save_info_update();
  257. }
  258. // @phpstan-ignore function.notFound
  259. if (!need_info_update()) {
  260. // @phpstan-ignore function.notFound
  261. $res = apply_update();
  262. } else {
  263. return;
  264. }
  265. }
  266. if (function_exists('opcache_reset')) {
  267. opcache_reset();
  268. }
  269. if ($res === true) {
  270. Minz_Request::forward([
  271. 'c' => 'update',
  272. 'a' => 'apply',
  273. 'params' => ['post_conf' => '1'],
  274. ], true);
  275. } else {
  276. Minz_Log::error(_t('feedback.update.error', $res));
  277. Minz_Request::bad(_t('feedback.update.error', $res), [ 'c' => 'update', 'a' => 'index' ]);
  278. }
  279. }
  280. }
  281. /**
  282. * This action displays information about installation.
  283. */
  284. public function checkInstallAction(): void {
  285. FreshRSS_View::prependTitle(_t('admin.check_install.title') . ' · ');
  286. $this->view->status_php = check_install_php();
  287. $this->view->status_files = check_install_files();
  288. $this->view->status_database = check_install_database();
  289. }
  290. }