updateController.php 6.4 KB

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