updateController.php 6.1 KB

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