update.php 3.9 KB

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