updateController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 -v', $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. strpos($line, '[behind') !== false || strpos($line, '[ahead') !== false || strpos($line, '[gone') !== false;
  82. }
  83. /** @return string|true */
  84. public static function gitPull() {
  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 strpos($currentVersion, 'dev') === false &&
  152. strpos($currentVersion, 'edge') === false;
  153. }
  154. /* Check installation if there is a newer version.
  155. via Git, if available.
  156. Else via system configuration auto_update_url
  157. */
  158. public function checkAction(): void {
  159. FreshRSS_View::prependTitle(_t('admin.update.title') . ' · ');
  160. $this->view->_path('update/index.phtml');
  161. if (file_exists(UPDATE_FILENAME)) {
  162. // There is already an update file to apply: we don’t need to check
  163. // the webserver!
  164. // Or if already check during the last hour, do nothing.
  165. Minz_Request::forward(['c' => 'update'], true);
  166. return;
  167. }
  168. $script = '';
  169. if (self::isGit()) {
  170. if (self::hasGitUpdate()) {
  171. $version = self::getCurrentGitBranch();
  172. } else {
  173. $this->view->message = [
  174. 'status' => 'latest',
  175. 'body' => _t('feedback.update.none'),
  176. ];
  177. @touch(join_path(DATA_PATH, self::LASTUPDATEFILE));
  178. return;
  179. }
  180. } else {
  181. $auto_update_url = FreshRSS_Context::systemConf()->auto_update_url . '/?v=' . FRESHRSS_VERSION;
  182. Minz_Log::debug('HTTP GET ' . $auto_update_url);
  183. $curlResource = curl_init($auto_update_url);
  184. if ($curlResource === false) {
  185. Minz_Log::warning('curl_init() failed');
  186. $this->view->message = [
  187. 'status' => 'bad',
  188. 'title' => _t('gen.short.damn'),
  189. 'body' => _t('feedback.update.server_not_found', $auto_update_url)
  190. ];
  191. return;
  192. }
  193. curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true);
  194. curl_setopt($curlResource, CURLOPT_SSL_VERIFYPEER, true);
  195. curl_setopt($curlResource, CURLOPT_SSL_VERIFYHOST, 2);
  196. $result = curl_exec($curlResource);
  197. $curlGetinfo = curl_getinfo($curlResource, CURLINFO_HTTP_CODE);
  198. $curlError = curl_error($curlResource);
  199. curl_close($curlResource);
  200. if ($curlGetinfo !== 200) {
  201. Minz_Log::warning(
  202. 'Error during update (HTTP code ' . $curlGetinfo . '): ' . $curlError
  203. );
  204. $this->view->message = [
  205. 'status' => 'bad',
  206. 'body' => _t('feedback.update.server_not_found', $auto_update_url),
  207. ];
  208. return;
  209. }
  210. $res_array = explode("\n", (string)$result, 2);
  211. $status = $res_array[0];
  212. if (strpos($status, 'UPDATE') !== 0) {
  213. $this->view->message = [
  214. 'status' => 'latest',
  215. 'body' => _t('feedback.update.none'),
  216. ];
  217. @touch(join_path(DATA_PATH, self::LASTUPDATEFILE));
  218. return;
  219. }
  220. $script = $res_array[1];
  221. $version = explode(' ', $status, 2);
  222. $version = $version[1];
  223. Minz_Log::notice(_t('admin.update.copiedFromURL', $auto_update_url));
  224. }
  225. if (file_put_contents(UPDATE_FILENAME, $script) !== false) {
  226. @file_put_contents(join_path(DATA_PATH, self::LASTUPDATEFILE), $version);
  227. Minz_Request::forward(['c' => 'update'], true);
  228. } else {
  229. $this->view->message = [
  230. 'status' => 'bad',
  231. 'body' => _t('feedback.update.error', 'Cannot save the update script'),
  232. ];
  233. }
  234. }
  235. public function applyAction(): void {
  236. if (FreshRSS_Context::systemConf()->disable_update || !file_exists(UPDATE_FILENAME) || !touch(FRESHRSS_PATH . '/index.html')) {
  237. Minz_Request::forward(['c' => 'update'], true);
  238. }
  239. if (Minz_Request::paramBoolean('post_conf')) {
  240. if (self::isGit()) {
  241. $res = !self::hasGitUpdate();
  242. } else {
  243. require(UPDATE_FILENAME);
  244. // @phpstan-ignore function.notFound
  245. $res = do_post_update();
  246. }
  247. Minz_ExtensionManager::callHookVoid('post_update');
  248. if ($res === true) {
  249. @unlink(UPDATE_FILENAME);
  250. @file_put_contents(join_path(DATA_PATH, self::LASTUPDATEFILE), '');
  251. Minz_Log::notice(_t('feedback.update.finished'));
  252. Minz_Request::good(_t('feedback.update.finished'));
  253. } else {
  254. Minz_Log::error(_t('feedback.update.error', $res));
  255. Minz_Request::bad(_t('feedback.update.error', $res), [ 'c' => 'update', 'a' => 'index' ]);
  256. }
  257. } else {
  258. $res = false;
  259. if (self::isGit()) {
  260. $res = self::gitPull();
  261. } else {
  262. require(UPDATE_FILENAME);
  263. if (Minz_Request::isPost()) {
  264. // @phpstan-ignore function.notFound
  265. save_info_update();
  266. }
  267. // @phpstan-ignore function.notFound
  268. if (!need_info_update()) {
  269. // @phpstan-ignore function.notFound
  270. $res = apply_update();
  271. } else {
  272. return;
  273. }
  274. }
  275. if (function_exists('opcache_reset')) {
  276. opcache_reset();
  277. }
  278. if ($res === true) {
  279. Minz_Request::forward([
  280. 'c' => 'update',
  281. 'a' => 'apply',
  282. 'params' => ['post_conf' => '1'],
  283. ], true);
  284. } else {
  285. Minz_Log::error(_t('feedback.update.error', $res));
  286. Minz_Request::bad(_t('feedback.update.error', $res), [ 'c' => 'update', 'a' => 'index' ]);
  287. }
  288. }
  289. }
  290. /**
  291. * This action displays information about installation.
  292. */
  293. public function checkInstallAction(): void {
  294. FreshRSS_View::prependTitle(_t('admin.check_install.title') . ' · ');
  295. $this->view->status_php = check_install_php();
  296. $this->view->status_files = check_install_files();
  297. $this->view->status_database = check_install_database();
  298. }
  299. }