updateController.php 6.3 KB

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