updateController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. class FreshRSS_update_Controller extends Minz_ActionController {
  3. public function firstAction() {
  4. if (!FreshRSS_Auth::hasAccess('admin')) {
  5. Minz_Error::error(403);
  6. }
  7. invalidateHttpCache();
  8. $this->view->update_to_apply = false;
  9. $this->view->last_update_time = 'unknown';
  10. $this->view->check_last_hour = false;
  11. $timestamp = @filemtime(join_path(DATA_PATH, 'last_update.txt'));
  12. if ($timestamp !== false) {
  13. $this->view->last_update_time = timestamptodate($timestamp);
  14. $this->view->check_last_hour = (time() - 3600) <= $timestamp;
  15. }
  16. }
  17. public function indexAction() {
  18. Minz_View::prependTitle(_t('admin.update.title') . ' · ');
  19. if (file_exists(UPDATE_FILENAME) && !is_writable(FRESHRSS_PATH)) {
  20. $this->view->message = array(
  21. 'status' => 'bad',
  22. 'title' => _t('gen.short.damn'),
  23. 'body' => _t('feedback.update.file_is_nok', FRESHRSS_PATH)
  24. );
  25. } elseif (file_exists(UPDATE_FILENAME)) {
  26. // There is an update file to apply!
  27. $version = file_get_contents(join_path(DATA_PATH, 'last_update.txt'));
  28. $this->view->update_to_apply = true;
  29. $this->view->message = array(
  30. 'status' => 'good',
  31. 'title' => _t('gen.short.ok'),
  32. 'body' => _t('feedback.update.can_apply', $version)
  33. );
  34. }
  35. }
  36. public function checkAction() {
  37. $this->view->change_view('update', 'index');
  38. if (file_exists(UPDATE_FILENAME) || $this->view->check_last_hour) {
  39. // There is already an update file to apply: we don't need to check
  40. // the webserver!
  41. // Or if already check during the last hour, do nothing.
  42. Minz_Request::forward(array('c' => 'update'));
  43. return;
  44. }
  45. $c = curl_init(FRESHRSS_UPDATE_WEBSITE);
  46. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  47. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  48. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  49. $result = curl_exec($c);
  50. $c_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  51. $c_error = curl_error($c);
  52. curl_close($c);
  53. if ($c_status !== 200) {
  54. Minz_Log::error(
  55. 'Error during update (HTTP code ' . $c_status . '): ' . $c_error
  56. );
  57. $this->view->message = array(
  58. 'status' => 'bad',
  59. 'title' => _t('gen.short.damn'),
  60. 'body' => _t('feedback.update.server_not_found', FRESHRSS_UPDATE_WEBSITE)
  61. );
  62. return;
  63. }
  64. $res_array = explode("\n", $result, 2);
  65. $status = $res_array[0];
  66. if (strpos($status, 'UPDATE') !== 0) {
  67. $this->view->message = array(
  68. 'status' => 'bad',
  69. 'title' => _t('gen.short.damn'),
  70. 'body' => _t('feedback.update.none')
  71. );
  72. @touch(join_path(DATA_PATH, 'last_update.txt'));
  73. return;
  74. }
  75. $script = $res_array[1];
  76. if (file_put_contents(UPDATE_FILENAME, $script) !== false) {
  77. $version = explode(' ', $status, 2);
  78. $version = $version[1];
  79. @file_put_contents(join_path(DATA_PATH, 'last_update.txt'), $version);
  80. Minz_Request::forward(array('c' => 'update'));
  81. } else {
  82. $this->view->message = array(
  83. 'status' => 'bad',
  84. 'title' => _t('gen.short.damn'),
  85. 'body' => _t('feedback.update.error', 'Cannot save the update script')
  86. );
  87. }
  88. }
  89. public function applyAction() {
  90. if (!file_exists(UPDATE_FILENAME) || !is_writable(FRESHRSS_PATH)) {
  91. Minz_Request::forward(array('c' => 'update'), true);
  92. }
  93. require(UPDATE_FILENAME);
  94. if (Minz_Request::param('post_conf', false)) {
  95. $res = do_post_update();
  96. if ($res === true) {
  97. @unlink(UPDATE_FILENAME);
  98. @file_put_contents(join_path(DATA_PATH, 'last_update.txt'), '');
  99. Minz_Request::good(_t('feedback.update.finished'));
  100. } else {
  101. Minz_Request::bad(_t('feedback.update.error', $res),
  102. array('c' => 'update', 'a' => 'index'));
  103. }
  104. }
  105. if (Minz_Request::isPost()) {
  106. save_info_update();
  107. }
  108. if (!need_info_update()) {
  109. $res = apply_update();
  110. if ($res === true) {
  111. Minz_Request::forward(array(
  112. 'c' => 'update',
  113. 'a' => 'apply',
  114. 'params' => array('post_conf' => true)
  115. ), true);
  116. } else {
  117. Minz_Request::bad(_t('feedback.update.error', $res),
  118. array('c' => 'update', 'a' => 'index'));
  119. }
  120. }
  121. }
  122. /**
  123. * This action displays information about installation.
  124. */
  125. public function checkInstallAction() {
  126. Minz_View::prependTitle(_t('admin.check_install.title') . ' · ');
  127. $this->view->status_php = check_install_php();
  128. $this->view->status_files = check_install_files();
  129. $this->view->status_database = check_install_database();
  130. }
  131. }