updateController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class FreshRSS_update_Controller extends Minz_ActionController {
  3. public function firstAction() {
  4. $current_user = Minz_Session::param('currentUser', '');
  5. if (!$this->view->loginOk && Minz_Configuration::isAdmin($current_user)) {
  6. Minz_Error::error(
  7. 403,
  8. array('error' => array(_t('access_denied')))
  9. );
  10. }
  11. invalidateHttpCache();
  12. Minz_View::prependTitle(_t('update_system') . ' · ');
  13. $this->view->update_to_apply = false;
  14. $this->view->last_update_time = 'unknown';
  15. $this->view->check_last_hour = false;
  16. $timestamp = (int)@file_get_contents(DATA_PATH . '/last_update.txt');
  17. if (is_numeric($timestamp) && $timestamp > 0) {
  18. $this->view->last_update_time = timestamptodate($timestamp);
  19. $this->view->check_last_hour = (time() - 3600) <= $timestamp;
  20. }
  21. }
  22. public function indexAction() {
  23. if (file_exists(UPDATE_FILENAME) && !is_writable(FRESHRSS_PATH)) {
  24. $this->view->message = array(
  25. 'status' => 'bad',
  26. 'title' => _t('damn'),
  27. 'body' => _t('file_is_nok', FRESHRSS_PATH)
  28. );
  29. } elseif (file_exists(UPDATE_FILENAME)) {
  30. // There is an update file to apply!
  31. $this->view->update_to_apply = true;
  32. $this->view->message = array(
  33. 'status' => 'good',
  34. 'title' => _t('ok'),
  35. 'body' => _t('update_can_apply')
  36. );
  37. }
  38. }
  39. public function checkAction() {
  40. $this->view->change_view('update', 'index');
  41. if (file_exists(UPDATE_FILENAME) || $this->view->check_last_hour) {
  42. // There is already an update file to apply: we don't need to check
  43. // the webserver!
  44. // Or if already check during the last hour, do nothing.
  45. Minz_Request::forward(array('c' => 'update'));
  46. return;
  47. }
  48. $c = curl_init(FRESHRSS_UPDATE_WEBSITE);
  49. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  50. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  51. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  52. $result = curl_exec($c);
  53. $c_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  54. $c_error = curl_error($c);
  55. curl_close($c);
  56. if ($c_status !== 200) {
  57. Minz_Log::error(
  58. 'Error during update (HTTP code ' . $c_status . '): ' . $c_error
  59. );
  60. $this->view->message = array(
  61. 'status' => 'bad',
  62. 'title' => _t('damn'),
  63. 'body' => _t('update_server_not_found', FRESHRSS_UPDATE_WEBSITE)
  64. );
  65. return;
  66. }
  67. $res_array = explode("\n", $result, 2);
  68. $status = $res_array[0];
  69. if (strpos($status, 'UPDATE') !== 0) {
  70. $this->view->message = array(
  71. 'status' => 'bad',
  72. 'title' => _t('damn'),
  73. 'body' => _t('no_update')
  74. );
  75. @file_put_contents(DATA_PATH . '/last_update.txt', time());
  76. return;
  77. }
  78. $script = $res_array[1];
  79. if (file_put_contents(UPDATE_FILENAME, $script) !== false) {
  80. Minz_Request::forward(array('c' => 'update'));
  81. } else {
  82. $this->view->message = array(
  83. 'status' => 'bad',
  84. 'title' => _t('damn'),
  85. 'body' => _t('update_problem', '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::isPost()) {
  95. save_info_update();
  96. }
  97. if (!need_info_update()) {
  98. $res = apply_update();
  99. if ($res === true) {
  100. @unlink(UPDATE_FILENAME);
  101. @file_put_contents(DATA_PATH . '/last_update.txt', time());
  102. Minz_Request::good(_t('update_finished'));
  103. } else {
  104. Minz_Request::bad(_t('update_problem', $res),
  105. array('c' => 'update', 'a' => 'index'));
  106. }
  107. }
  108. }
  109. }