updateController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. $c = curl_init(FRESHRSS_UPDATE_WEBSITE);
  47. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  48. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  49. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  50. $result = curl_exec($c);
  51. $c_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  52. $c_error = curl_error($c);
  53. curl_close($c);
  54. if ($c_status !== 200) {
  55. Minz_Log::error(
  56. 'Error during update (HTTP code ' . $c_status . '): ' . $c_error
  57. );
  58. $this->view->message = array(
  59. 'status' => 'bad',
  60. 'title' => _t('gen.short.damn'),
  61. 'body' => _t('feedback.update.server_not_found', FRESHRSS_UPDATE_WEBSITE)
  62. );
  63. return;
  64. }
  65. $res_array = explode("\n", $result, 2);
  66. $status = $res_array[0];
  67. if (strpos($status, 'UPDATE') !== 0) {
  68. $this->view->message = array(
  69. 'status' => 'bad',
  70. 'title' => _t('gen.short.damn'),
  71. 'body' => _t('feedback.update.none')
  72. );
  73. @touch(join_path(DATA_PATH, 'last_update.txt'));
  74. return;
  75. }
  76. $script = $res_array[1];
  77. if (file_put_contents(UPDATE_FILENAME, $script) !== false) {
  78. $version = explode(' ', $status, 2);
  79. $version = $version[1];
  80. @file_put_contents(join_path(DATA_PATH, 'last_update.txt'), $version);
  81. Minz_Request::forward(array('c' => 'update'), true);
  82. } else {
  83. $this->view->message = array(
  84. 'status' => 'bad',
  85. 'title' => _t('gen.short.damn'),
  86. 'body' => _t('feedback.update.error', 'Cannot save the update script')
  87. );
  88. }
  89. }
  90. public function applyAction() {
  91. if (!file_exists(UPDATE_FILENAME) || !is_writable(FRESHRSS_PATH)) {
  92. Minz_Request::forward(array('c' => 'update'), true);
  93. }
  94. require(UPDATE_FILENAME);
  95. if (Minz_Request::param('post_conf', false)) {
  96. $res = do_post_update();
  97. Minz_ExtensionManager::callHook('post_update');
  98. if ($res === true) {
  99. @unlink(UPDATE_FILENAME);
  100. @file_put_contents(join_path(DATA_PATH, 'last_update.txt'), '');
  101. Minz_Request::good(_t('feedback.update.finished'));
  102. } else {
  103. Minz_Request::bad(_t('feedback.update.error', $res),
  104. array('c' => 'update', 'a' => 'index'));
  105. }
  106. }
  107. if (Minz_Request::isPost()) {
  108. save_info_update();
  109. }
  110. if (!need_info_update()) {
  111. $res = apply_update();
  112. if ($res === true) {
  113. Minz_Request::forward(array(
  114. 'c' => 'update',
  115. 'a' => 'apply',
  116. 'params' => array('post_conf' => true)
  117. ), true);
  118. } else {
  119. Minz_Request::bad(_t('feedback.update.error', $res),
  120. array('c' => 'update', 'a' => 'index'));
  121. }
  122. }
  123. }
  124. /**
  125. * This action displays information about installation.
  126. */
  127. public function checkInstallAction() {
  128. Minz_View::prependTitle(_t('admin.check_install.title') . ' · ');
  129. $this->view->status_php = check_install_php();
  130. $this->view->status_files = check_install_files();
  131. $this->view->status_database = check_install_database();
  132. }
  133. }