updateController.php 8.3 KB

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