diff --git a/createdb.php b/createdb.php index edf2d69..bea77dc 100755 --- a/createdb.php +++ b/createdb.php @@ -32,6 +32,7 @@

database creator

This page helps to create a database (in sqlite3 format) based on genomes uploaded by the user.

The final database can be downloaded for local analyzis, or it can be explored on the site.

+

Alternatively you can upload a Synteruptor database directly if you have generated a database yourself.

PDO::ERRMODE_EXCEPTION)); + $dbh = new PDO("sqlite:$dbpath", '', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } catch(PDOException $ex) { die_msg('Unable to connect to database.', $ex->getMessage()); @@ -85,6 +91,22 @@ function get_db_connection($db) { return $dbh; } +function check_db($dbh) { + $tables = ["breaks_all", "breaks_ranking"]; + foreach ($tables as $table) { + if (!has_table($dbh, $table)) { + return false; + } + $query = "SELECT * FROM $table LIMIT 1"; + $data = get_db_data($dbh, $query); + if (count($data) != 1) { + error_log("Table $table has not data?"); + return false; + } + } + return true; +} + function get_db_data($dbh, $query, $vals = array(), $key = '') { try { $result = $dbh->prepare($query); diff --git a/upload_db.php b/upload_db.php new file mode 100755 index 0000000..da96bca --- /dev/null +++ b/upload_db.php @@ -0,0 +1,75 @@ + + + + + + + + + <?php site_name(); ?> database creator + + + + + + + + + + +
+
+

database upload

+

This page helps to upload database (in sqlite3 format) to this website.

+'; + echo '

Restrictions

'; + echo '"; + echo "For bigger databases you should contact us directly, see the contact page."; + echo "
"; + + echo '
'; + echo "

Database file upload

"; + + # Check id + if (!check_id($id)) { + echo "Invalid id ($id)
"; + echo "
"; + echo "
"; + exit; + } + + # Get the database file + $new_db = scan_sqlite(); + if ($new_db) { + echo "Uploaded the database file to $new_db"; + } else { + echo '
'; + echo " "; + echo ''; + } + echo "
"; +} else { + echo "
"; +} +?> + + +
+ + diff --git a/upload_db_add.php b/upload_db_add.php new file mode 100755 index 0000000..0fde827 --- /dev/null +++ b/upload_db_add.php @@ -0,0 +1,84 @@ + + + +Start a new upload"; + exit; +} + +if (!isset($_FILES["new_db"])) { + $errormsg .= "
  • Max allowed size: " . ini_get('post_max_size') . " or " . ini_get('upload_max_filesize') . "
  • "; + $nerrors++; +} else { + if ($_FILES["new_db"]["error"] != UPLOAD_ERR_OK) { + $errormsg .= "
  • Upload error. [".$error."] on file '".$name."'
  • "; + $nerrors++; + } else { + $tmp_name = $_FILES["new_db"]["tmp_name"]; + if (!$tmp_name) return; + $name = $_FILES["new_db"]["name"]; + + // Check extension + if (!preg_match("/\.sqlite?$/", $name)) { + $errormsg .= "
  • Wrong file type for $name (only .sqlite allowed)
  • "; + $nerrors++; + } else if (filesize($tmp_name) == 0) { + $errormsg .= "
  • File is empty
  • "; + $nerrors++; + } else { + # Check there is data in the database + try { + $dbh = get_db_connection($tmp_name); + if (!check_db($dbh)) { + throw new DbException("Content of the db doesn't look right"); + } + } catch(Exception $e) { + $errormsg .= "
  • Exception: ".$e->getMessage()."
  • "; + $nerrors++; + } + + if ($nerrors == 0) { + # Just in case, to avoid collisions + $num = 1; + $new_id = $id; + $new_db_path = $final_db_path; + while(file_exists($new_db_path)) { + $new_id = $id . "_" . $num; + $new_db_path = str_replace("$id.sqlite", "$new_id.sqlite", $final_db_path); + $num++; + if ($num > 10) { + $errormsg .= "
  • ID collision detected with $new_id in $new_db_path.
  • "; + $nerrors++; + break; + } + } + if ($nerrors == 0) { + if ( move_uploaded_file($tmp_name, $new_db_path) ) { + $uploaded_array[] .= "Uploaded file '".$name."'.
    \n"; + } else { + $errormsg .= "
  • Could not move uploaded file '".$tmp_name."' to '".$name."'
  • "; + $nerrors++; + } + } + } + } + } +} + +if ($nerrors == 0) { + header("Location: $builder?id=$new_id"); +} else { + echo "Errors, please check:\n"; + echo "Go back"; +} +?> diff --git a/upload_db_lib.php b/upload_db_lib.php new file mode 100755 index 0000000..8a35ac2 --- /dev/null +++ b/upload_db_lib.php @@ -0,0 +1,55 @@ + + + + diff --git a/upload_db_start.php b/upload_db_start.php new file mode 100755 index 0000000..c4a20fc --- /dev/null +++ b/upload_db_start.php @@ -0,0 +1,28 @@ + + + +