updateController.php 6.4 KB

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