PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/updater/init.php

https://github.com/iarna/Tiny-Tiny-RSS
PHP | 390 lines | 216 code | 32 blank | 142 comment | 20 complexity | 6c537af45fe57d111e32b05124d00dae MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0, LGPL-3.0
  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. // Set PATH to run "which"
  22. putenv('PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"');
  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($force = false) {
  212. $step = 0;
  213. $stop = false;
  214. $params = array();
  215. while (!$stop) {
  216. $rc = $this->update_self_step($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("READ THE FOLLOWING BEFORE CONTINUING!");
  227. _debug("* It is suggested to backup your tt-rss directory first.");
  228. _debug("* Your database will not be modified.");
  229. _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.");
  230. _debug("Type 'yes' to continue.");
  231. $input = read_stdin();
  232. if ($input != 'yes' && $input != 'force')
  233. exit;
  234. $this->update_self_cli($input == 'force');
  235. }
  236. function get_prefs_js() {
  237. return file_get_contents(dirname(__FILE__) . "/updater.js");
  238. }
  239. function hook_prefs_tab($args) {
  240. if ($args != "prefPrefs") return;
  241. if (($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) && CHECK_FOR_NEW_VERSION) {
  242. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Update Tiny Tiny RSS')."\">";
  243. if ($_SESSION["pref_last_version_check"] + 86400 + rand(-1000, 1000) < time()) {
  244. $_SESSION["version_data"] = @check_for_update();
  245. $_SESSION["pref_last_version_check"] = time();
  246. }
  247. if (is_array($_SESSION["version_data"])) {
  248. $version = $_SESSION["version_data"]["version"];
  249. $version_id = $_SESSION["version_data"]["version_id"];
  250. print_notice(T_sprintf("New version of Tiny Tiny RSS is available (%s).", "<b>$version</b>"));
  251. $details = "http://tt-rss.org/redmine/versions/$version_id";
  252. print "<p><button onclick=\"window.open('$details')\" dojoType=\"dijit.form.Button\">".__("See the release notes")."</button>";
  253. print " <button dojoType=\"dijit.form.Button\" onclick=\"return updateSelf()\">".
  254. __('Update Tiny Tiny RSS')."</button></p>";
  255. } else {
  256. print_notice(__("Your Tiny Tiny RSS installation is up to date."));
  257. }
  258. print "</div>"; #pane
  259. }
  260. }
  261. function updateSelf() {
  262. print_warning(__("Do not close this dialog until updating is finished."));
  263. print "<form style='display : block' name='self_update_form' id='self_update_form'>";
  264. print "<style type='text/css'>
  265. li.notice { font-style : italic; color : red; }
  266. </style>";
  267. print "<ul class='selfUpdateList' id='self_update_log'>";
  268. print "<li class='notice'>" .__("It is suggested to backup your tt-rss directory first.") . "</li>";
  269. print "<li class='notice'>" . __("Your database will not be modified.") . "</li>";
  270. 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>";
  271. print "<li>" . __("Ready to update.") . "</li>";
  272. print "</ul>";
  273. print "<div class='dlgButtons'>";
  274. print "<button id=\"self_update_start_btn\" dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('updateSelfDlg').start()\" >".
  275. __("Start update")."</button>";
  276. print "<button id=\"self_update_stop_btn\" onclick=\"return dijit.byId('updateSelfDlg').close()\" dojoType=\"dijit.form.Button\">".
  277. __("Close this window")."</button>";
  278. print "</div>";
  279. print "</form>";
  280. }
  281. function performUpdate() {
  282. $step = (int) $_REQUEST["step"];
  283. $params = json_decode($_REQUEST["params"], true);
  284. $force = (bool) $_REQUEST["force"];
  285. if (($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) && CHECK_FOR_NEW_VERSION) {
  286. print json_encode($this->update_self_step($step, $params, $force));
  287. }
  288. }
  289. function api_version() {
  290. return 2;
  291. }
  292. }
  293. ?>