4
0

updateController.php 9.8 KB

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