updateController.php 7.8 KB

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