updateController.php 7.6 KB

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