updateController.php 7.4 KB

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