updateController.php 9.1 KB

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