updateController.php 7.4 KB

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