updateController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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'; // TODO
  13. }
  14. public function indexAction() {
  15. if (file_exists(UPDATE_FILENAME) && !is_writable(FRESHRSS_PATH)) {
  16. $this->view->message = array(
  17. 'status' => 'bad',
  18. 'title' => _t('damn'),
  19. 'body' => _t('file_is_nok', FRESHRSS_PATH)
  20. );
  21. } elseif (file_exists(UPDATE_FILENAME)) {
  22. // There is an update file to apply!
  23. $this->view->message = array(
  24. 'status' => 'good',
  25. 'title' => _t('ok'),
  26. 'body' => _t('update_can_apply', _url('update', 'apply'))
  27. );
  28. }
  29. }
  30. public function checkAction() {
  31. $this->view->change_view('update', 'index');
  32. if (file_exists(UPDATE_FILENAME)) {
  33. // There is already an update file to apply: we don't need to check
  34. // the webserver!
  35. Minz_Request::forward(array('c' => 'update'));
  36. return;
  37. }
  38. $c = curl_init(FRESHRSS_UPDATE_WEBSITE);
  39. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  40. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  41. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  42. $result = curl_exec($c);
  43. $c_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  44. curl_close($c);
  45. if ($c_status !== 200) {
  46. $this->view->message = array(
  47. 'status' => 'bad',
  48. 'title' => _t('damn'),
  49. 'body' => _t('update_server_not_found', FRESHRSS_UPDATE_WEBSITE)
  50. );
  51. return;
  52. }
  53. $res_array = explode("\n", $result, 2);
  54. $status = $res_array[0];
  55. if (strpos($status, 'UPDATE') !== 0) {
  56. $this->view->message = array(
  57. 'status' => 'bad',
  58. 'title' => _t('damn'),
  59. 'body' => _t('no_update')
  60. );
  61. return;
  62. }
  63. $script = $res_array[1];
  64. if (file_put_contents(UPDATE_FILENAME, $script) !== false) {
  65. Minz_Request::forward(array('c' => 'update'));
  66. } else {
  67. $this->view->message = array(
  68. 'status' => 'bad',
  69. 'title' => _t('damn'),
  70. 'body' => _t('update_problem', 'Cannot save the update script')
  71. );
  72. }
  73. }
  74. public function applyAction() {
  75. if (!file_exists(UPDATE_FILENAME) || !is_writable(FRESHRSS_PATH)) {
  76. Minz_Request::forward(array('c' => 'update'), true);
  77. }
  78. require(UPDATE_FILENAME);
  79. if (Minz_Request::isPost()) {
  80. save_info_update();
  81. }
  82. if (!need_info_update()) {
  83. $res = apply_update();
  84. if ($res === true) {
  85. @unlink(UPDATE_FILENAME);
  86. // TODO: record last update_finished
  87. Minz_Request::good(_t('update_finished'));
  88. } else {
  89. Minz_Request::bad(_t('update_problem', $res),
  90. array('c' => 'update', 'a' => 'index'));
  91. }
  92. }
  93. }
  94. }