updateController.php 4.4 KB

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