updateController.php 2.7 KB

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