db-updater.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. require_once "functions.php";
  3. require_once "sessions.php";
  4. require_once "sanity_check.php";
  5. require_once "config.php";
  6. require_once "db.php";
  7. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  8. init_connection($link);
  9. login_sequence($link);
  10. $owner_uid = $_SESSION["uid"];
  11. if (!SINGLE_USER_MODE && $_SESSION["access_level"] < 10) {
  12. $_SESSION["login_error_msg"] = __("Your access level is insufficient to run this script.");
  13. render_login_form($link);
  14. exit;
  15. }
  16. ?>
  17. <html>
  18. <head>
  19. <title>Database Updater</title>
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  21. <link rel="stylesheet" type="text/css" href="utility.css">
  22. </head>
  23. <body>
  24. <script type='text/javascript'>
  25. function confirmOP() {
  26. return confirm(__("Update the database?"));
  27. }
  28. </script>
  29. <div class="floatingLogo"><img src="images/logo_wide.png"></div>
  30. <h1><?php echo __("Database Updater") ?></h1>
  31. <?php
  32. function getline($fp, $delim) {
  33. $result = "";
  34. while(!feof($fp)) {
  35. $tmp = fgetc($fp);
  36. if($tmp == $delim) {
  37. return $result;
  38. }
  39. $result .= $tmp;
  40. }
  41. return $result;
  42. }
  43. $op = $_POST["op"];
  44. $result = db_query($link, "SELECT schema_version FROM ttrss_version");
  45. $version = db_fetch_result($result, 0, "schema_version");
  46. $update_files = glob("schema/versions/".DB_TYPE."/*sql");
  47. $update_versions = array();
  48. foreach ($update_files as $f) {
  49. $m = array();
  50. preg_match_all("/schema\/versions\/".DB_TYPE."\/(\d*)\.sql/", $f, $m,
  51. PREG_PATTERN_ORDER);
  52. if ($m[1][0]) {
  53. $update_versions[$m[1][0]] = $f;
  54. }
  55. }
  56. ksort($update_versions, SORT_NUMERIC);
  57. $latest_version = max(array_keys($update_versions));
  58. if ($version == $latest_version) {
  59. if ($version != SCHEMA_VERSION) {
  60. print_error(__("Could not update database"));
  61. print "<p>" .
  62. __("Could not find necessary schema file, need version:") .
  63. " " . SCHEMA_VERSION . __(", found: ") . $latest_version . "</p>";
  64. } else {
  65. print_notice(__("Tiny Tiny RSS database is up to date."));
  66. print "<form method=\"GET\" action=\"tt-rss.php\">
  67. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  68. </form>";
  69. }
  70. } else if ($version <= $latest_version && !$op) {
  71. print_warning(__("Please backup your database before proceeding."));
  72. print "<p>" . T_sprintf("Your Tiny Tiny RSS database needs update to the latest version (<b>%d</b> to <b>%d</b>).", $version, $latest_version) . "</p>";
  73. /* print "<p>Available incremental updates:";
  74. foreach (array_keys($update_versions) as $v) {
  75. if ($v > $version) {
  76. print " <a href='$update_versions[$v]'>$v</a>";
  77. }
  78. } */
  79. print "</p>";
  80. print "<form method='POST'>
  81. <input type='hidden' name='op' value='do'>
  82. <input type='submit' onclick='return confirmOP()' value='".__("Perform updates")."'>
  83. </form>";
  84. } else if ($op == "do") {
  85. print "<p>".__("Performing updates...")."</p>";
  86. $num_updates = 0;
  87. foreach (array_keys($update_versions) as $v) {
  88. if ($v == $version + 1) {
  89. print "<p>".T_sprintf("Updating to version %d...", $v)."</p>";
  90. $fp = fopen($update_versions[$v], "r");
  91. if ($fp) {
  92. while (!feof($fp)) {
  93. $query = trim(getline($fp, ";"));
  94. if ($query != "") {
  95. print "<p class='query'>$query</p>";
  96. db_query($link, $query);
  97. }
  98. }
  99. }
  100. fclose($fp);
  101. print "<p>".__("Checking version... ");
  102. $result = db_query($link, "SELECT schema_version FROM ttrss_version");
  103. $version = db_fetch_result($result, 0, "schema_version");
  104. if ($version == $v) {
  105. print __("OK!");
  106. } else {
  107. print "<b>".__("ERROR!")."</b>";
  108. return;
  109. }
  110. $num_updates++;
  111. }
  112. }
  113. print "<p>".T_sprintf("Finished. Performed <b>%d</b> update(s) up to schema
  114. version <b>%d</b>.", $num_updates, $version)."</p>";
  115. print "<form method=\"GET\" action=\"backend.php\">
  116. <input type=\"hidden\" name=\"op\" value=\"logout\">
  117. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  118. </form>";
  119. } else if ($version >= $latest_version) {
  120. print_error(__("Your database schema is from a newer version of Tiny Tiny RSS."));
  121. print "<p>" . T_sprintf("Found schema version: <b>%d</b>, required: <b>%d</b>.", $version, $latest_version) . "</p>";
  122. print "<p>" . __("Schema upgrade impossible. Please update Tiny Tiny RSS files to the newer version and continue.") . "</p>";
  123. print "<form method=\"GET\" action=\"backend.php\">
  124. <input type=\"hidden\" name=\"op\" value=\"logout\">
  125. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  126. </form>";
  127. }
  128. ?>
  129. </body>
  130. </html>