db-updater.php 4.6 KB

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