init.php 11 KB

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