4
0

updateController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  40. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  41. $result = curl_exec($c);
  42. $c_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  43. curl_close($c);
  44. if ($c_status !== 200) {
  45. $this->view->message = array(
  46. 'status' => 'bad',
  47. 'title' => _t('damn'),
  48. 'body' => _t('update_server_not_found', FRESHRSS_UPDATE_WEBSITE)
  49. );
  50. return;
  51. }
  52. $res_array = explode("\n", $result, 2);
  53. $status = $res_array[0];
  54. if (strpos($status, 'UPDATE') !== 0) {
  55. $this->view->message = array(
  56. 'status' => 'bad',
  57. 'title' => _t('damn'),
  58. 'body' => _t('no_update')
  59. );
  60. return;
  61. }
  62. $script = $res_array[1];
  63. if (file_put_contents(UPDATE_FILENAME, $script) !== false) {
  64. $this->view->message = array(
  65. 'status' => 'good',
  66. 'title' => _t('ok'),
  67. 'body' => _t('update_can_apply', _url('update', 'apply'))
  68. );
  69. } else {
  70. $this->view->message = array(
  71. 'status' => 'bad',
  72. 'title' => _t('damn'),
  73. 'body' => _t('update_problem', 'Cannot save the update script')
  74. );
  75. }
  76. }
  77. public function applyAction() {
  78. if (!file_exists(UPDATE_FILENAME)) {
  79. Minz_Request::forward(array('c' => 'update'), true);
  80. }
  81. require(UPDATE_FILENAME);
  82. if (Minz_Request::isPost()) {
  83. save_info_update();
  84. }
  85. if (!need_info_update()) {
  86. $res = apply_update();
  87. if ($res === true) {
  88. @unlink(UPDATE_FILENAME);
  89. // TODO: record last update
  90. Minz_Session::_param('notification', array(
  91. 'type' => 'good',
  92. 'content' => Minz_Translate::t('update_finished')
  93. ));
  94. Minz_Request::forward(array(), true);
  95. } else {
  96. Minz_Session::_param('notification', array(
  97. 'type' => 'bad',
  98. 'content' => Minz_Translate::t('update_problem', $res)
  99. ));
  100. Minz_Request::forward(array('c' => 'update'), true);
  101. }
  102. }
  103. }
  104. }