init.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. $log = array();
  24. if (!is_array($params)) $params = array();
  25. $stop = false;
  26. if (!chdir($work_dir)) {
  27. array_push($log, "Unable to change to work directory: $work_dir");
  28. $stop = true;
  29. }
  30. if (!$stop) {
  31. switch ($step) {
  32. case 0:
  33. array_push($log, "Work directory: $work_dir");
  34. if (!is_writable($work_dir) || !is_writable("$parent_dir")) {
  35. $user = posix_getpwuid(posix_geteuid());
  36. $user = $user["name"];
  37. array_push($log, "Both tt-rss and parent directories should be writable as current user ($user).");
  38. $stop = true; break;
  39. }
  40. if (!file_exists("$work_dir/config.php") || !file_exists("$work_dir/include/sanity_check.php")) {
  41. array_push($log, "Work directory $work_dir doesn't look like tt-rss installation.");
  42. $stop = true; break;
  43. }
  44. if (!is_writable(sys_get_temp_dir())) {
  45. array_push($log, "System temporary directory should be writable as current user.");
  46. $stop = true; break;
  47. }
  48. // bah, also humbug
  49. putenv("PATH=" . getenv("PATH") . PATH_SEPARATOR . "/bin" .
  50. PATH_SEPARATOR . "/usr/bin");
  51. array_push($log, "Checking for tar...");
  52. $system_rc = 0;
  53. system("which tar >/dev/null", $system_rc);
  54. if ($system_rc != 0) {
  55. array_push($log, "Could not run tar executable (RC=$system_rc).");
  56. $stop = true; break;
  57. }
  58. array_push($log, "Checking for gunzip...");
  59. $system_rc = 0;
  60. system("which gunzip >/dev/null", $system_rc);
  61. if ($system_rc != 0) {
  62. array_push($log, "Could not run gunzip executable (RC=$system_rc).");
  63. $stop = true; break;
  64. }
  65. array_push($log, "Checking for latest version...");
  66. $version_info = json_decode(fetch_file_contents("http://tt-rss.org/version.php"),
  67. true);
  68. if (!is_array($version_info)) {
  69. array_push($log, "Unable to fetch version information.");
  70. $stop = true; break;
  71. }
  72. $target_version = $version_info["version"];
  73. $target_dir = "$parent_dir/Tiny-Tiny-RSS-$target_version";
  74. array_push($log, "Target version: $target_version");
  75. $params["target_version"] = $target_version;
  76. if (version_compare(VERSION, $target_version) != -1 && !$force) {
  77. array_push($log, "Your Tiny Tiny RSS installation is up to date.");
  78. $stop = true; break;
  79. }
  80. if (file_exists($target_dir)) {
  81. array_push($log, "Target directory $target_dir already exists.");
  82. $stop = true; break;
  83. }
  84. break;
  85. case 1:
  86. $target_version = $params["target_version"];
  87. /* array_push($log, "Downloading checksums...");
  88. $md5sum_data = fetch_file_contents("http://tt-rss.org/download/md5sum.txt");
  89. if (!$md5sum_data) {
  90. array_push($log, "Could not download checksums.");
  91. $stop = true; break;
  92. }
  93. $md5sum_data = explode("\n", $md5sum_data);
  94. foreach ($md5sum_data as $line) {
  95. $pair = explode(" ", $line);
  96. if ($pair[1] == "tt-rss-$target_version.tar.gz") {
  97. $target_md5sum = $pair[0];
  98. break;
  99. }
  100. }
  101. if (!$target_md5sum) {
  102. array_push($log, "Unable to locate checksum for target version.");
  103. $stop = true; break;
  104. }
  105. $params["target_md5sum"] = $target_md5sum; */
  106. array_push($log, "Proceeding to download...");
  107. break;
  108. case 2:
  109. $target_version = $params["target_version"];
  110. // $target_md5sum = $params["target_md5sum"];
  111. array_push($log, "Downloading distribution tarball...");
  112. $tarball_url = "https://github.com/gothfox/Tiny-Tiny-RSS/archive/$target_version.tar.gz";
  113. $data = fetch_file_contents($tarball_url);
  114. if (!$data) {
  115. array_push($log, "Could not download distribution tarball ($tarball_url).");
  116. $stop = true; break;
  117. }
  118. /* array_push($log, "Verifying tarball checksum...");
  119. $test_md5sum = md5($data);
  120. if ($test_md5sum != $target_md5sum) {
  121. array_push($log, "Downloaded checksum doesn't match (got $test_md5sum, expected $target_md5sum).");
  122. $stop = true; break;
  123. } */
  124. $tmp_file = tempnam(sys_get_temp_dir(), 'tt-rss');
  125. array_push($log, "Saving download to $tmp_file");
  126. if (!file_put_contents($tmp_file, $data)) {
  127. array_push($log, "Unable to save download.");
  128. $stop = true; break;
  129. }
  130. $params["tmp_file"] = $tmp_file;
  131. break;
  132. case 3:
  133. $tmp_file = $params["tmp_file"];
  134. $target_version = $params["target_version"];
  135. if (!chdir($parent_dir)) {
  136. array_push($log, "Unable to change into parent directory.");
  137. $stop = true; break;
  138. }
  139. array_push($log, "Extracting tarball...");
  140. system("tar zxf $tmp_file", $system_rc);
  141. if ($system_rc != 0) {
  142. array_push($log, "Error while extracting tarball (RC=$system_rc).");
  143. $stop = true; break;
  144. }
  145. $target_dir = "$parent_dir/Tiny-Tiny-RSS-$target_version";
  146. if (!is_dir($target_dir)) {
  147. array_push($log, "Target directory ($target_dir) not found.");
  148. $stop = true; break;
  149. }
  150. $old_dir = tmpdirname($parent_dir, "tt-rss-old");
  151. array_push($log, "Renaming tt-rss directory to ".basename($old_dir));
  152. if (!rename($work_dir, $old_dir)) {
  153. array_push($log, "Unable to rename tt-rss directory.");
  154. $stop = true; break;
  155. }
  156. array_push($log, "Renaming target directory...");
  157. if (!rename($target_dir, $work_dir)) {
  158. array_push($log, "Unable to rename target directory.");
  159. $stop = true; break;
  160. }
  161. if (!chdir($work_dir)) {
  162. array_push($log, "Unable to change to work directory: $work_dir");
  163. $stop = true; break;
  164. }
  165. array_push($log, "Copying config.php...");
  166. if (!copy("$old_dir/config.php", "$work_dir/config.php")) {
  167. array_push($log, "Unable to copy config.php to $work_dir.");
  168. $stop = true; break;
  169. }
  170. array_push($log, "Cleaning up...");
  171. unlink($tmp_file);
  172. array_push($log, "Fixing permissions...");
  173. $directories = array(
  174. CACHE_DIR,
  175. CACHE_DIR . "/export",
  176. CACHE_DIR . "/images",
  177. CACHE_DIR . "/js",
  178. CACHE_DIR . "/simplepie",
  179. ICONS_DIR,
  180. LOCK_DIRECTORY);
  181. foreach ($directories as $dir) {
  182. array_push($log, "-> $dir");
  183. chmod($dir, 0777);
  184. }
  185. if (ICONS_DIR == "feed-icons") {
  186. array_push($log, "Migrating feed icons...");
  187. $icons = glob("$old_dir/feed-icons/*.ico");
  188. $icons_copied = 0;
  189. foreach ($icons as $icon) {
  190. $icon = basename($icon);
  191. if (copy("$old_dir/feed-icons/$icon", "$work_dir/feed-icons/$icon")) {
  192. ++$icons_copied;
  193. }
  194. }
  195. array_push($log, "Done; $icons_copied files copied");
  196. } else {
  197. array_push($log, "Not migrating feed icons, ICONS_DIR modified.");
  198. }
  199. array_push($log, "Upgrade completed.");
  200. array_push($log, "Your old tt-rss directory is saved at $old_dir. ".
  201. "Please migrate locally modified files (if any) and remove it.");
  202. array_push($log, "You might need to re-enter current directory in shell to see new files.");
  203. $stop = true;
  204. break;
  205. default:
  206. $stop = true;
  207. }
  208. }
  209. return array("step" => $step, "stop" => $stop, "params" => $params, "log" => $log);
  210. }
  211. function update_self_cli($link, $force = false) {
  212. $step = 0;
  213. $stop = false;
  214. $params = array();
  215. while (!$stop) {
  216. $rc = $this->update_self_step($link, $step, $params, $force);
  217. $params = $rc['params'];
  218. $stop = $rc['stop'];
  219. foreach ($rc['log'] as $line) {
  220. _debug($line);
  221. }
  222. ++$step;
  223. }
  224. }
  225. function update_self($args) {
  226. _debug("Warning: self-updating is experimental. Use at your own risk.");
  227. _debug("Please backup your tt-rss directory before continuing. Your database will not be modified.");
  228. _debug("Type 'yes' to continue.");
  229. $input = read_stdin();
  230. if ($input != 'yes' && $input != 'force')
  231. exit;
  232. $this->update_self_cli($link, $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($this->link);
  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 "<form style='display : block' name='self_update_form' id='self_update_form'>";
  261. print "<div class='error'>".__("Do not close this dialog until updating is finished. Backup your tt-rss directory before continuing.")."</div>";
  262. print "<ul class='selfUpdateList' id='self_update_log'>";
  263. print "<li>" . __("Ready to update.") . "</li>";
  264. print "</ul>";
  265. print "<div class='dlgButtons'>";
  266. print "<button id=\"self_update_start_btn\" dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('updateSelfDlg').start()\" >".
  267. __("Start update")."</button>";
  268. print "<button id=\"self_update_stop_btn\" onclick=\"return dijit.byId('updateSelfDlg').close()\" dojoType=\"dijit.form.Button\">".
  269. __("Close this window")."</button>";
  270. print "</div>";
  271. print "</form>";
  272. }
  273. function performUpdate() {
  274. $step = (int) $_REQUEST["step"];
  275. $params = json_decode($_REQUEST["params"], true);
  276. $force = (bool) $_REQUEST["force"];
  277. if (($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) && CHECK_FOR_NEW_VERSION) {
  278. print json_encode($this->update_self_step($this->link, $step, $params, $force));
  279. }
  280. }
  281. }
  282. ?>