updateController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. Minz_View::prependTitle(_t('update_system') . ' · ');
  12. $this->view->last_update_time = 'unknown';
  13. $timestamp = (int)@file_get_contents(DATA_PATH . '/last_update.txt');
  14. if (is_numeric($timestamp) && $timestamp > 0) {
  15. $this->view->last_update_time = timestamptodate($timestamp);
  16. }
  17. }
  18. public function indexAction() {
  19. if (file_exists(UPDATE_FILENAME) && !is_writable(FRESHRSS_PATH)) {
  20. $this->view->message = array(
  21. 'status' => 'bad',
  22. 'title' => _t('damn'),
  23. 'body' => _t('file_is_nok', FRESHRSS_PATH)
  24. );
  25. } elseif (file_exists(UPDATE_FILENAME)) {
  26. // There is an update file to apply!
  27. $this->view->message = array(
  28. 'status' => 'good',
  29. 'title' => _t('ok'),
  30. 'body' => _t('update_can_apply', _url('update', 'apply'))
  31. );
  32. }
  33. }
  34. public function checkAction() {
  35. $this->view->change_view('update', 'index');
  36. // Get the last update. If already check during the last hour, do nothing.
  37. $last_update = (int)@file_get_contents(DATA_PATH . '/last_update.txt');
  38. $check_last_hour = (time() - 3600) <= $last_update;
  39. if (file_exists(UPDATE_FILENAME) || $check_last_hour) {
  40. // There is already an update file to apply: we don't need to check
  41. // the webserver!
  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. curl_close($c);
  52. if ($c_status !== 200) {
  53. $this->view->message = array(
  54. 'status' => 'bad',
  55. 'title' => _t('damn'),
  56. 'body' => _t('update_server_not_found', FRESHRSS_UPDATE_WEBSITE)
  57. );
  58. return;
  59. }
  60. $res_array = explode("\n", $result, 2);
  61. $status = $res_array[0];
  62. if (strpos($status, 'UPDATE') !== 0) {
  63. $this->view->message = array(
  64. 'status' => 'bad',
  65. 'title' => _t('damn'),
  66. 'body' => _t('no_update')
  67. );
  68. return;
  69. }
  70. $script = $res_array[1];
  71. if (file_put_contents(UPDATE_FILENAME, $script) !== false) {
  72. Minz_Request::forward(array('c' => 'update'));
  73. } else {
  74. $this->view->message = array(
  75. 'status' => 'bad',
  76. 'title' => _t('damn'),
  77. 'body' => _t('update_problem', 'Cannot save the update script')
  78. );
  79. }
  80. }
  81. public function applyAction() {
  82. if (!file_exists(UPDATE_FILENAME) || !is_writable(FRESHRSS_PATH)) {
  83. Minz_Request::forward(array('c' => 'update'), true);
  84. }
  85. require(UPDATE_FILENAME);
  86. if (Minz_Request::isPost()) {
  87. save_info_update();
  88. }
  89. if (!need_info_update()) {
  90. $res = apply_update();
  91. if ($res === true) {
  92. @unlink(UPDATE_FILENAME);
  93. @file_put_contents(DATA_PATH . '/last_update.txt', time());
  94. Minz_Request::good(_t('update_finished'));
  95. } else {
  96. Minz_Request::bad(_t('update_problem', $res),
  97. array('c' => 'update', 'a' => 'index'));
  98. }
  99. }
  100. }
  101. }