4
0

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