updateController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. class FreshRSS_update_Controller extends Minz_ActionController {
  3. public static function isGit() {
  4. return is_dir(FRESHRSS_PATH . '/.git/');
  5. }
  6. public static function hasGitUpdate() {
  7. $cwd = getcwd();
  8. chdir(FRESHRSS_PATH);
  9. $output = array();
  10. try {
  11. exec('git fetch', $output, $return);
  12. if ($return == 0) {
  13. exec('git status -sb --porcelain remote', $output, $return);
  14. } else {
  15. $line = is_array($output) ? implode('; ', $output) : '' . $output;
  16. Minz_Log::warning('git fetch warning:' . $line);
  17. }
  18. } catch (Exception $e) {
  19. Minz_Log::warning('git fetch error:' . $e->getMessage());
  20. }
  21. chdir($cwd);
  22. $line = is_array($output) ? implode('; ', $output) : '' . $output;
  23. return strpos($line, '[behind') !== false;
  24. }
  25. public static function gitPull() {
  26. $cwd = getcwd();
  27. chdir(FRESHRSS_PATH);
  28. $output = array();
  29. $return = 1;
  30. try {
  31. exec('git pull --ff-only', $output, $return);
  32. } catch (Exception $e) {
  33. Minz_Log::warning('git pull error:' . $e->getMessage());
  34. }
  35. chdir($cwd);
  36. $line = is_array($output) ? implode('; ', $output) : '' . $output;
  37. return $return == 0 ? true : 'Git error: ' . $line;
  38. }
  39. public function firstAction() {
  40. if (!FreshRSS_Auth::hasAccess('admin')) {
  41. Minz_Error::error(403);
  42. }
  43. invalidateHttpCache();
  44. $this->view->update_to_apply = false;
  45. $this->view->last_update_time = 'unknown';
  46. $timestamp = @filemtime(join_path(DATA_PATH, 'last_update.txt'));
  47. if ($timestamp !== false) {
  48. $this->view->last_update_time = timestamptodate($timestamp);
  49. }
  50. }
  51. public function indexAction() {
  52. Minz_View::prependTitle(_t('admin.update.title') . ' · ');
  53. if (!is_writable(FRESHRSS_PATH)) {
  54. $this->view->message = array(
  55. 'status' => 'bad',
  56. 'title' => _t('gen.short.damn'),
  57. 'body' => _t('feedback.update.file_is_nok', FRESHRSS_PATH)
  58. );
  59. } elseif (file_exists(UPDATE_FILENAME)) {
  60. // There is an update file to apply!
  61. $version = @file_get_contents(join_path(DATA_PATH, 'last_update.txt'));
  62. if (empty($version)) {
  63. $version = 'unknown';
  64. }
  65. $this->view->update_to_apply = true;
  66. $this->view->message = array(
  67. 'status' => 'good',
  68. 'title' => _t('gen.short.ok'),
  69. 'body' => _t('feedback.update.can_apply', $version)
  70. );
  71. }
  72. }
  73. public function checkAction() {
  74. $this->view->change_view('update', 'index');
  75. if (file_exists(UPDATE_FILENAME)) {
  76. // There is already an update file to apply: we don't need to check
  77. // the webserver!
  78. // Or if already check during the last hour, do nothing.
  79. Minz_Request::forward(array('c' => 'update'), true);
  80. return;
  81. }
  82. $script = '';
  83. $version = '';
  84. if (self::isGit()) {
  85. if (self::hasGitUpdate()) {
  86. $version = 'git';
  87. } else {
  88. $this->view->message = array(
  89. 'status' => 'bad',
  90. 'title' => _t('gen.short.damn'),
  91. 'body' => _t('feedback.update.none')
  92. );
  93. @touch(join_path(DATA_PATH, 'last_update.txt'));
  94. return;
  95. }
  96. } else {
  97. $auto_update_url = FreshRSS_Context::$system_conf->auto_update_url . '?v=' . FRESHRSS_VERSION;
  98. Minz_Log::debug('HTTP GET ' . $auto_update_url);
  99. $c = curl_init($auto_update_url);
  100. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  101. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  102. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  103. $result = curl_exec($c);
  104. $c_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  105. $c_error = curl_error($c);
  106. curl_close($c);
  107. if ($c_status !== 200) {
  108. Minz_Log::warning(
  109. 'Error during update (HTTP code ' . $c_status . '): ' . $c_error
  110. );
  111. $this->view->message = array(
  112. 'status' => 'bad',
  113. 'title' => _t('gen.short.damn'),
  114. 'body' => _t('feedback.update.server_not_found', $auto_update_url)
  115. );
  116. return;
  117. }
  118. $res_array = explode("\n", $result, 2);
  119. $status = $res_array[0];
  120. if (strpos($status, 'UPDATE') !== 0) {
  121. $this->view->message = array(
  122. 'status' => 'bad',
  123. 'title' => _t('gen.short.damn'),
  124. 'body' => _t('feedback.update.none')
  125. );
  126. @touch(join_path(DATA_PATH, 'last_update.txt'));
  127. return;
  128. }
  129. $script = $res_array[1];
  130. $version = explode(' ', $status, 2);
  131. $version = $version[1];
  132. }
  133. if (file_put_contents(UPDATE_FILENAME, $script) !== false) {
  134. @file_put_contents(join_path(DATA_PATH, 'last_update.txt'), $version);
  135. Minz_Request::forward(array('c' => 'update'), true);
  136. } else {
  137. $this->view->message = array(
  138. 'status' => 'bad',
  139. 'title' => _t('gen.short.damn'),
  140. 'body' => _t('feedback.update.error', 'Cannot save the update script')
  141. );
  142. }
  143. }
  144. public function applyAction() {
  145. if (!file_exists(UPDATE_FILENAME) || !is_writable(FRESHRSS_PATH)) {
  146. Minz_Request::forward(array('c' => 'update'), true);
  147. }
  148. if (Minz_Request::param('post_conf', false)) {
  149. if (self::isGit()) {
  150. $res = !self::hasGitUpdate();
  151. } else {
  152. require(UPDATE_FILENAME);
  153. $res = do_post_update();
  154. }
  155. Minz_ExtensionManager::callHook('post_update');
  156. if ($res === true) {
  157. @unlink(UPDATE_FILENAME);
  158. @file_put_contents(join_path(DATA_PATH, 'last_update.txt'), '');
  159. Minz_Request::good(_t('feedback.update.finished'));
  160. } else {
  161. Minz_Request::bad(_t('feedback.update.error', $res),
  162. array('c' => 'update', 'a' => 'index'));
  163. }
  164. } else {
  165. $res = false;
  166. if (self::isGit()) {
  167. $res = self::gitPull();
  168. } else {
  169. if (Minz_Request::isPost()) {
  170. save_info_update();
  171. }
  172. if (!need_info_update()) {
  173. $res = apply_update();
  174. } else {
  175. return;
  176. }
  177. }
  178. if ($res === true) {
  179. Minz_Request::forward(array(
  180. 'c' => 'update',
  181. 'a' => 'apply',
  182. 'params' => array('post_conf' => true)
  183. ), true);
  184. } else {
  185. Minz_Request::bad(_t('feedback.update.error', $res),
  186. array('c' => 'update', 'a' => 'index'));
  187. }
  188. }
  189. }
  190. /**
  191. * This action displays information about installation.
  192. */
  193. public function checkInstallAction() {
  194. Minz_View::prependTitle(_t('admin.check_install.title') . ' · ');
  195. $this->view->status_php = check_install_php();
  196. $this->view->status_files = check_install_files();
  197. $this->view->status_database = check_install_database();
  198. }
  199. }