init.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. class Updater extends Plugin {
  3. private $link;
  4. private $host;
  5. function about() {
  6. return array(1.0,
  7. "Updates tt-rss installation to latest version.",
  8. "fox",
  9. true);
  10. }
  11. function init($host) {
  12. $this->link = $host->get_link();
  13. $this->host = $host;
  14. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  15. $host->add_command("update-self",
  16. "update tt-rss installation to latest version",
  17. $this);
  18. }
  19. function update_self_step($link, $step, $params, $force = false) {
  20. // __FILE__ is in plugins/updater so we need to go one level up
  21. $work_dir = dirname(dirname(dirname(__FILE__)));
  22. $parent_dir = dirname($work_dir);
  23. if (!chdir($work_dir)) {
  24. array_push($log, "Unable to change to work directory: $work_dir");
  25. $stop = true; break;
  26. }
  27. $stop = false;
  28. $log = array();
  29. if (!is_array($params)) $params = array();
  30. switch ($step) {
  31. case 0:
  32. array_push($log, "Work directory: $work_dir");
  33. if (!is_writable($work_dir) && !is_writable("$parent_dir")) {
  34. $user = posix_getpwuid(posix_geteuid());
  35. $user = $user["name"];
  36. array_push($log, "Both tt-rss and parent directories should be writable as current user ($user).");
  37. $stop = true; break;
  38. }
  39. if (!file_exists("$work_dir/config.php") || !file_exists("$work_dir/include/sanity_check.php")) {
  40. array_push($log, "Work directory $work_dir doesn't look like tt-rss installation.");
  41. $stop = true; break;
  42. }
  43. if (!is_writable(sys_get_temp_dir())) {
  44. array_push($log, "System temporary directory should be writable as current user.");
  45. $stop = true; break;
  46. }
  47. array_push($log, "Checking for tar...");
  48. $system_rc = 0;
  49. system("which tar >/dev/null", $system_rc);
  50. if ($system_rc != 0) {
  51. array_push($log, "Could not run tar executable (RC=$system_rc).");
  52. $stop = true; break;
  53. }
  54. array_push($log, "Checking for gunzip...");
  55. $system_rc = 0;
  56. system("which gunzip >/dev/null", $system_rc);
  57. if ($system_rc != 0) {
  58. array_push($log, "Could not run gunzip executable (RC=$system_rc).");
  59. $stop = true; break;
  60. }
  61. array_push($log, "Checking for latest version...");
  62. $version_info = json_decode(fetch_file_contents("http://tt-rss.org/version.php"),
  63. true);
  64. if (!is_array($version_info)) {
  65. array_push($log, "Unable to fetch version information.");
  66. $stop = true; break;
  67. }
  68. $target_version = $version_info["version"];
  69. $target_dir = "$parent_dir/tt-rss-$target_version";
  70. array_push($log, "Target version: $target_version");
  71. $params["target_version"] = $target_version;
  72. if (version_compare(VERSION, $target_version) != -1 && !$force) {
  73. array_push($log, "Your Tiny Tiny RSS installation is up to date.");
  74. $stop = true; break;
  75. }
  76. if (file_exists($target_dir)) {
  77. array_push($log, "Target directory $target_dir already exists.");
  78. $stop = true; break;
  79. }
  80. break;
  81. case 1:
  82. $target_version = $params["target_version"];
  83. array_push($log, "Downloading checksums...");
  84. $md5sum_data = fetch_file_contents("http://tt-rss.org/download/md5sum.txt");
  85. if (!$md5sum_data) {
  86. array_push($log, "Could not download checksums.");
  87. $stop = true; break;
  88. }
  89. $md5sum_data = explode("\n", $md5sum_data);
  90. foreach ($md5sum_data as $line) {
  91. $pair = explode(" ", $line);
  92. if ($pair[1] == "tt-rss-$target_version.tar.gz") {
  93. $target_md5sum = $pair[0];
  94. break;
  95. }
  96. }
  97. if (!$target_md5sum) {
  98. array_push($log, "Unable to locate checksum for target version.");
  99. $stop = true; break;
  100. }
  101. $params["target_md5sum"] = $target_md5sum;
  102. break;
  103. case 2:
  104. $target_version = $params["target_version"];
  105. $target_md5sum = $params["target_md5sum"];
  106. array_push($log, "Downloading distribution tarball...");
  107. $tarball_url = "http://tt-rss.org/download/tt-rss-$target_version.tar.gz";
  108. $data = fetch_file_contents($tarball_url);
  109. if (!$data) {
  110. array_push($log, "Could not download distribution tarball ($tarball_url).");
  111. $stop = true; break;
  112. }
  113. array_push($log, "Verifying tarball checksum...");
  114. $test_md5sum = md5($data);
  115. if ($test_md5sum != $target_md5sum) {
  116. array_push($log, "Downloaded checksum doesn't match (got $test_md5sum, expected $target_md5sum).");
  117. $stop = true; break;
  118. }
  119. $tmp_file = tempnam(sys_get_temp_dir(), 'tt-rss');
  120. array_push($log, "Saving download to $tmp_file");
  121. if (!file_put_contents($tmp_file, $data)) {
  122. array_push($log, "Unable to save download.");
  123. $stop = true; break;
  124. }
  125. $params["tmp_file"] = $tmp_file;
  126. break;
  127. case 3:
  128. $tmp_file = $params["tmp_file"];
  129. $target_version = $params["target_version"];
  130. if (!chdir($parent_dir)) {
  131. array_push($log, "Unable to change into parent directory.");
  132. $stop = true; break;
  133. }
  134. $old_dir = tmpdirname($parent_dir, "tt-rss-old");
  135. array_push($log, "Renaming tt-rss directory to ".basename($old_dir));
  136. if (!rename($work_dir, $old_dir)) {
  137. array_push($log, "Unable to rename tt-rss directory.");
  138. $stop = true; break;
  139. }
  140. array_push($log, "Extracting tarball...");
  141. system("tar zxf $tmp_file", $system_rc);
  142. if ($system_rc != 0) {
  143. array_push($log, "Error while extracting tarball (RC=$system_rc).");
  144. $stop = true; break;
  145. }
  146. $target_dir = "$parent_dir/tt-rss-$target_version";
  147. array_push($log, "Renaming target directory...");
  148. if (!rename($target_dir, $work_dir)) {
  149. array_push($log, "Unable to rename target directory.");
  150. $stop = true; break;
  151. }
  152. if (!chdir($work_dir)) {
  153. array_push($log, "Unable to change to work directory: $work_dir");
  154. $stop = true; break;
  155. }
  156. array_push($log, "Copying config.php...");
  157. if (!copy("$old_dir/config.php", "$work_dir/config.php")) {
  158. array_push($log, "Unable to copy config.php to $work_dir.");
  159. $stop = true; break;
  160. }
  161. array_push($log, "Cleaning up...");
  162. unlink($tmp_file);
  163. array_push($log, "Fixing permissions...");
  164. $directories = array(
  165. CACHE_DIR,
  166. CACHE_DIR . "/export",
  167. CACHE_DIR . "/images",
  168. CACHE_DIR . "/simplepie",
  169. ICONS_DIR,
  170. LOCK_DIRECTORY);
  171. foreach ($directories as $dir) {
  172. array_push($log, "-> $dir");
  173. chmod($dir, 0777);
  174. }
  175. array_push($log, "Upgrade completed.");
  176. array_push($log, "Your old tt-rss directory is saved at $old_dir. ".
  177. "Please migrate locally modified files (if any) and remove it.");
  178. array_push($log, "You might need to re-enter current directory in shell to see new files.");
  179. $stop = true;
  180. break;
  181. default:
  182. $stop = true;
  183. }
  184. return array("step" => $step, "stop" => $stop, "params" => $params, "log" => $log);
  185. }
  186. function update_self_cli($link, $force = false) {
  187. $step = 0;
  188. $stop = false;
  189. $params = array();
  190. while (!$stop) {
  191. $rc = $this->update_self_step($link, $step, $params, $force);
  192. $params = $rc['params'];
  193. $stop = $rc['stop'];
  194. foreach ($rc['log'] as $line) {
  195. _debug($line);
  196. }
  197. ++$step;
  198. }
  199. }
  200. function update_self($args) {
  201. _debug("Warning: self-updating is experimental. Use at your own risk.");
  202. _debug("Please backup your tt-rss directory before continuing. Your database will not be modified.");
  203. _debug("Type 'yes' to continue.");
  204. if (read_stdin() != 'yes')
  205. exit;
  206. $this->update_self_cli($link, in_array("-force", $args));
  207. }
  208. function get_prefs_js() {
  209. return file_get_contents(dirname(__FILE__) . "/updater.js");
  210. }
  211. function hook_prefs_tab($args) {
  212. if ($args != "prefPrefs") return;
  213. if (($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) && CHECK_FOR_NEW_VERSION) {
  214. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Update Tiny Tiny RSS')."\">";
  215. if ($_SESSION["pref_last_version_check"] + 86400 + rand(-1000, 1000) < time()) {
  216. $_SESSION["version_data"] = @check_for_update($this->link);
  217. $_SESSION["pref_last_version_check"] = time();
  218. }
  219. if (is_array($_SESSION["version_data"])) {
  220. $version = $_SESSION["version_data"]["version"];
  221. print_notice(T_sprintf("New version of Tiny Tiny RSS is available (%s).", "<b>$version</b>"));
  222. print "<p><button dojoType=\"dijit.form.Button\" onclick=\"return updateSelf()\">".
  223. __('Update Tiny Tiny RSS')."</button></p>";
  224. } else {
  225. print_notice(__("Your Tiny Tiny RSS installation is up to date."));
  226. }
  227. print "</div>"; #pane
  228. }
  229. function updateSelf() {
  230. print "<form style='display : block' name='self_update_form' id='self_update_form'>";
  231. print "<div class='error'>".__("Do not close this dialog until updating is finished. Backup your tt-rss directory before continuing.")."</div>";
  232. print "<ul class='selfUpdateList' id='self_update_log'>";
  233. print "<li>" . __("Ready to update.") . "</li>";
  234. print "</ul>";
  235. print "<div class='dlgButtons'>";
  236. print "<button id=\"self_update_start_btn\" dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('updateSelfDlg').start()\" >".
  237. __("Start update")."</button>";
  238. print "<button id=\"self_update_stop_btn\" onclick=\"return dijit.byId('updateSelfDlg').close()\" dojoType=\"dijit.form.Button\">".
  239. __("Close this window")."</button>";
  240. print "</div>";
  241. print "</form>";
  242. }
  243. function performUpdate() {
  244. $step = (int) $_REQUEST["step"];
  245. $params = json_decode($_REQUEST["params"], true);
  246. $force = (bool) $_REQUEST["force"];
  247. if (($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) && CHECK_FOR_NEW_VERSION) {
  248. print json_encode($this->update_self_step($this->link, $step, $params, $force));
  249. }
  250. }
  251. }
  252. }
  253. ?>