updateController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. class FreshRSS_update_Controller extends FreshRSS_ActionController {
  3. 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 hasGitUpdate(): bool {
  38. $cwd = getcwd();
  39. chdir(FRESHRSS_PATH);
  40. $output = array();
  41. try {
  42. exec('git fetch --prune', $output, $return);
  43. if ($return == 0) {
  44. $output = [];
  45. exec('git status -sb --porcelain remote', $output, $return);
  46. } else {
  47. $line = implode('; ', $output);
  48. Minz_Log::warning('git fetch warning: ' . $line);
  49. }
  50. } catch (Exception $e) {
  51. Minz_Log::warning('git fetch error: ' . $e->getMessage());
  52. }
  53. chdir($cwd);
  54. $line = implode('; ', $output);
  55. return $line == '' ||
  56. strpos($line, '[behind') !== false || strpos($line, '[ahead') !== false || strpos($line, '[gone') !== false;
  57. }
  58. /** @return string|true */
  59. public static function gitPull() {
  60. $cwd = getcwd();
  61. chdir(FRESHRSS_PATH);
  62. $output = [];
  63. $return = 1;
  64. try {
  65. exec('git fetch --prune', $output, $return);
  66. if ($return == 0) {
  67. $output = [];
  68. exec('git reset --hard FETCH_HEAD', $output, $return);
  69. }
  70. $output = [];
  71. self::migrateToGitEdge();
  72. } catch (Exception $e) {
  73. Minz_Log::warning('Git error: ' . $e->getMessage());
  74. if (empty($output)) {
  75. $output = $e->getMessage();
  76. }
  77. $return = 1;
  78. }
  79. chdir($cwd);
  80. $line = is_array($output) ? implode('; ', $output) : $output;
  81. return $return == 0 ? true : 'Git error: ' . $line;
  82. }
  83. public function firstAction(): void {
  84. if (!FreshRSS_Auth::hasAccess('admin')) {
  85. Minz_Error::error(403);
  86. }
  87. include_once(LIB_PATH . '/lib_install.php');
  88. invalidateHttpCache();
  89. $this->view->update_to_apply = false;
  90. $this->view->last_update_time = 'unknown';
  91. $timestamp = @filemtime(join_path(DATA_PATH, self::LASTUPDATEFILE));
  92. if ($timestamp !== false) {
  93. $this->view->last_update_time = timestamptodate($timestamp);
  94. }
  95. }
  96. public function indexAction(): void {
  97. FreshRSS_View::prependTitle(_t('admin.update.title') . ' · ');
  98. if (file_exists(UPDATE_FILENAME)) {
  99. // There is an update file to apply!
  100. $version = @file_get_contents(join_path(DATA_PATH, self::LASTUPDATEFILE));
  101. if ($version == '') {
  102. $version = 'unknown';
  103. }
  104. if (touch(FRESHRSS_PATH . '/index.html')) {
  105. $this->view->update_to_apply = true;
  106. $this->view->message = array(
  107. 'status' => 'good',
  108. 'title' => _t('gen.short.ok'),
  109. 'body' => _t('feedback.update.can_apply', $version),
  110. );
  111. } else {
  112. $this->view->message = array(
  113. 'status' => 'bad',
  114. 'title' => _t('gen.short.damn'),
  115. 'body' => _t('feedback.update.file_is_nok', $version, FRESHRSS_PATH),
  116. );
  117. }
  118. }
  119. }
  120. public function checkAction(): void {
  121. $this->view->_path('update/index.phtml');
  122. if (file_exists(UPDATE_FILENAME)) {
  123. // There is already an update file to apply: we don’t need to check
  124. // the webserver!
  125. // Or if already check during the last hour, do nothing.
  126. Minz_Request::forward(array('c' => 'update'), true);
  127. return;
  128. }
  129. $script = '';
  130. $version = '';
  131. if (self::isGit()) {
  132. if (self::hasGitUpdate()) {
  133. $version = 'git';
  134. } else {
  135. $this->view->message = array(
  136. 'status' => 'latest',
  137. 'title' => _t('gen.short.damn'),
  138. 'body' => _t('feedback.update.none')
  139. );
  140. @touch(join_path(DATA_PATH, self::LASTUPDATEFILE));
  141. return;
  142. }
  143. } else {
  144. $auto_update_url = FreshRSS_Context::$system_conf->auto_update_url . '/?v=' . FRESHRSS_VERSION;
  145. Minz_Log::debug('HTTP GET ' . $auto_update_url);
  146. $c = curl_init($auto_update_url);
  147. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  148. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  149. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  150. $result = curl_exec($c);
  151. $c_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  152. $c_error = curl_error($c);
  153. curl_close($c);
  154. if ($c_status !== 200) {
  155. Minz_Log::warning(
  156. 'Error during update (HTTP code ' . $c_status . '): ' . $c_error
  157. );
  158. $this->view->message = array(
  159. 'status' => 'bad',
  160. 'title' => _t('gen.short.damn'),
  161. 'body' => _t('feedback.update.server_not_found', $auto_update_url)
  162. );
  163. return;
  164. }
  165. $res_array = explode("\n", $result, 2);
  166. $status = $res_array[0];
  167. if (strpos($status, 'UPDATE') !== 0) {
  168. $this->view->message = array(
  169. 'status' => 'latest',
  170. 'title' => _t('gen.short.damn'),
  171. 'body' => _t('feedback.update.none')
  172. );
  173. @touch(join_path(DATA_PATH, self::LASTUPDATEFILE));
  174. return;
  175. }
  176. $script = $res_array[1];
  177. $version = explode(' ', $status, 2);
  178. $version = $version[1];
  179. }
  180. if (file_put_contents(UPDATE_FILENAME, $script) !== false) {
  181. @file_put_contents(join_path(DATA_PATH, self::LASTUPDATEFILE), $version);
  182. Minz_Request::forward(array('c' => 'update'), true);
  183. } else {
  184. $this->view->message = array(
  185. 'status' => 'bad',
  186. 'title' => _t('gen.short.damn'),
  187. 'body' => _t('feedback.update.error', 'Cannot save the update script')
  188. );
  189. }
  190. }
  191. public function applyAction(): void {
  192. if (FreshRSS_Context::$system_conf->disable_update || !file_exists(UPDATE_FILENAME) || !touch(FRESHRSS_PATH . '/index.html')) {
  193. Minz_Request::forward(array('c' => 'update'), true);
  194. }
  195. if (Minz_Request::param('post_conf', false)) {
  196. if (self::isGit()) {
  197. $res = !self::hasGitUpdate();
  198. } else {
  199. require(UPDATE_FILENAME);
  200. // @phpstan-ignore-next-line
  201. $res = do_post_update();
  202. }
  203. Minz_ExtensionManager::callHook('post_update');
  204. if ($res === true) {
  205. @unlink(UPDATE_FILENAME);
  206. @file_put_contents(join_path(DATA_PATH, self::LASTUPDATEFILE), '');
  207. Minz_Request::good(_t('feedback.update.finished'));
  208. } else {
  209. Minz_Request::bad(_t('feedback.update.error', $res), [ 'c' => 'update', 'a' => 'index' ]);
  210. }
  211. } else {
  212. $res = false;
  213. if (self::isGit()) {
  214. $res = self::gitPull();
  215. } else {
  216. require(UPDATE_FILENAME);
  217. if (Minz_Request::isPost()) {
  218. // @phpstan-ignore-next-line
  219. save_info_update();
  220. }
  221. // @phpstan-ignore-next-line
  222. if (!need_info_update()) {
  223. // @phpstan-ignore-next-line
  224. $res = apply_update();
  225. } else {
  226. return;
  227. }
  228. }
  229. if (function_exists('opcache_reset')) {
  230. opcache_reset();
  231. }
  232. if ($res === true) {
  233. Minz_Request::forward(array(
  234. 'c' => 'update',
  235. 'a' => 'apply',
  236. 'params' => array('post_conf' => '1')
  237. ), true);
  238. } else {
  239. Minz_Request::bad(_t('feedback.update.error', $res), [ 'c' => 'update', 'a' => 'index' ]);
  240. }
  241. }
  242. }
  243. /**
  244. * This action displays information about installation.
  245. */
  246. public function checkInstallAction(): void {
  247. FreshRSS_View::prependTitle(_t('admin.check_install.title') . ' · ');
  248. $this->view->status_php = check_install_php();
  249. $this->view->status_files = check_install_files();
  250. $this->view->status_database = check_install_database();
  251. }
  252. }