update.php 3.9 KB

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