PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/updater/init.php

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