new way to generate database fromseparate schema.sql
This commit is contained in:
		@@ -23,8 +23,8 @@
 | 
			
		||||
//       1         2         3         4         5         6         7         8
 | 
			
		||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
 | 
			
		||||
require_once("usertable.php");
 | 
			
		||||
try {
 | 
			
		||||
  require_once("opendb.php");
 | 
			
		||||
  $user = $db->real_escape_string($_REQUEST['user']);
 | 
			
		||||
  $q = $db->query("select * from user where name='$user';");
 | 
			
		||||
  if ($q->num_rows==0) {
 | 
			
		||||
 
 | 
			
		||||
@@ -28,11 +28,12 @@
 | 
			
		||||
*/
 | 
			
		||||
//       1         2         3         4         5         6         7         8
 | 
			
		||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
require_once("messagetable.php");
 | 
			
		||||
try {
 | 
			
		||||
  require_once("opendb.php");
 | 
			
		||||
  $start = $db->real_escape_string($_REQUEST['start']);
 | 
			
		||||
  $q = $db->query("select id, UNIX_TIMESTAMP(time) as time, user, msg from message where id>$start;");
 | 
			
		||||
  echo json_encode($q->fetch_all(MYSQLI_ASSOC));
 | 
			
		||||
  if ($q) echo json_encode($q->fetch_all(MYSQLI_ASSOC));
 | 
			
		||||
  else echo json_encode(null);
 | 
			
		||||
} catch (Exception $e) {
 | 
			
		||||
  echo json_encode(null);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,7 @@
 | 
			
		||||
//       1         2         3         4         5         6         7         8
 | 
			
		||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
try {
 | 
			
		||||
  require_once("usertable.php");
 | 
			
		||||
  require_once("opendb.php");
 | 
			
		||||
  $user = $db->real_escape_string($_REQUEST['user']);
 | 
			
		||||
  $pubkey = $db->real_escape_string($_REQUEST['pubkey']);
 | 
			
		||||
  if ($user=="safechat") error("username safechat is reserved for server");
 | 
			
		||||
 
 | 
			
		||||
@@ -1,32 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*! @file
 | 
			
		||||
 | 
			
		||||
    @id $Id$
 | 
			
		||||
 | 
			
		||||
    @see @ref messagetable for the database schema
 | 
			
		||||
 | 
			
		||||
    @page database Database
 | 
			
		||||
 | 
			
		||||
    @section messagetable Message Table
 | 
			
		||||
 | 
			
		||||
    <table>
 | 
			
		||||
    <caption>Table: message</caption>
 | 
			
		||||
    <tr><th>Colum Name</th><th>SQL Type</th><th>Description</th></tr>
 | 
			
		||||
    <tr><td>id</td><td>int, primary key</td><td>Incrementing message id starting at 1.</td></tr>
 | 
			
		||||
    <tr><td>time</td><td>timestamp</td><td>Time when message has been stored in the database.</td></tr>
 | 
			
		||||
    <tr><td>user</td><td>varchar(50), references \ref usertable "user (name)"</td><td>The sender's user name (pseudonym).</td></tr>
 | 
			
		||||
    <tr><td>msg</td><td>longtext</td><td>The encryped and armored message text.</td></tr>
 | 
			
		||||
    </table>
 | 
			
		||||
*/
 | 
			
		||||
//       1         2         3         4         5         6         7         8
 | 
			
		||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
 | 
			
		||||
require_once("opendb.php");
 | 
			
		||||
try {
 | 
			
		||||
  $db->query(file_get_contents("messagetable.sql"));
 | 
			
		||||
  $db->query('set global max_allowed_packet=1000000000');
 | 
			
		||||
  $db->query('set global net_buffer_length=1000000');
 | 
			
		||||
} catch (Exception $e) {
 | 
			
		||||
  error('database error on server');
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
@@ -1,20 +0,0 @@
 | 
			
		||||
    create table if not exists
 | 
			
		||||
      'message' (
 | 
			
		||||
        'id'
 | 
			
		||||
          int primary key not null auto_increment
 | 
			
		||||
          comment "id of the message, it is used in the client to check if a message has already been downloaded or not",
 | 
			
		||||
        'time'
 | 
			
		||||
          timestamp default current_timestamp
 | 
			
		||||
          comment "time when the message has been stored on the server",
 | 
			
		||||
        'user'
 | 
			
		||||
          varchar(50) not null
 | 
			
		||||
          comment "name of the user that sent the message",
 | 
			
		||||
        'msg'
 | 
			
		||||
          longtext not null
 | 
			
		||||
          comment "message content, must be armored gnupg encrypted format",
 | 
			
		||||
        foreign key ('user')
 | 
			
		||||
          references 'user'('name')
 | 
			
		||||
          on delete cascade
 | 
			
		||||
          on update cascade
 | 
			
		||||
      ) character set utf8 engine=innodb
 | 
			
		||||
        comment="table to hold all messages for later download by the receiver";
 | 
			
		||||
							
								
								
									
										48
									
								
								html/opendb.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								html/opendb.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*! @file
 | 
			
		||||
 | 
			
		||||
    @id $Id$
 | 
			
		||||
 | 
			
		||||
    @see @ref usertable for the database schema
 | 
			
		||||
 | 
			
		||||
    @page database Database
 | 
			
		||||
 | 
			
		||||
    @tableofcontents
 | 
			
		||||
 | 
			
		||||
    @section usertable User Table
 | 
			
		||||
 | 
			
		||||
    <table>
 | 
			
		||||
    <caption>Table: user</caption>
 | 
			
		||||
    <tr><th>Colum Name</th><th>SQL Type</th><th>Description</th></tr>
 | 
			
		||||
    <tr><td>name</td><td>varchar(50)</td><td>The user's name (pseudonym).</td></tr>
 | 
			
		||||
    <tr><td>pubkey</td><td>text</td><td>The user's public key.</td></tr>
 | 
			
		||||
    </table>
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
//       1         2         3         4         5         6         7         8
 | 
			
		||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
 | 
			
		||||
mysqli_report(MYSQLI_REPORT_STRICT);
 | 
			
		||||
require_once("functions.php");
 | 
			
		||||
try {
 | 
			
		||||
  if (!isset($pgp)) {
 | 
			
		||||
    $pgp = gnupg_init();
 | 
			
		||||
    if (!$pgp) error("pgp on server failed");
 | 
			
		||||
  }
 | 
			
		||||
} catch (Exception $e) {
 | 
			
		||||
  error('cannot start pgp on server');
 | 
			
		||||
}
 | 
			
		||||
try {
 | 
			
		||||
  if (!isset($db)) {
 | 
			
		||||
    $db = new mysqli("mysql", "root", $_SERVER["MYSQL_ENV_MYSQL_ROOT_PASSWORD"]);
 | 
			
		||||
    if (!$db) error("database connection failed on server");
 | 
			
		||||
    $db->query("create database if not exists safechat;");
 | 
			
		||||
    $db->select_db("safechat");
 | 
			
		||||
    if (!$db) error("cannot create database for safechat");
 | 
			
		||||
    $db->query(file_get_contents("schema.sql"));
 | 
			
		||||
    if (!$db) error("cannot create database tables");
 | 
			
		||||
  }
 | 
			
		||||
} catch (Exception $e) {
 | 
			
		||||
  error('database error on server');
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
@@ -29,11 +29,11 @@ try {
 | 
			
		||||
  require_once("usertable.php");
 | 
			
		||||
  $user = $db->real_escape_string($_REQUEST['user']);
 | 
			
		||||
  $q = $db->query("select pubkey from user where name='$user';");
 | 
			
		||||
  if ($q->num_rows!=1 && $user=="safechat") {
 | 
			
		||||
    require_once("optionstable.php");
 | 
			
		||||
    createSafechatUser();
 | 
			
		||||
    $q = $db->query("select pubkey from user where name='$user';");
 | 
			
		||||
  }
 | 
			
		||||
  /* if ($q->num_rows!=1 && $user=="safechat") { */
 | 
			
		||||
  /*   require_once("optionstable.php"); */
 | 
			
		||||
  /*   createSafechatUser(); */
 | 
			
		||||
  /*   $q = $db->query("select pubkey from user where name='$user';"); */
 | 
			
		||||
  /* } */
 | 
			
		||||
  if ($q->num_rows==1) {
 | 
			
		||||
    echo json_encode($q->fetch_row()[0]);
 | 
			
		||||
  } else {
 | 
			
		||||
 
 | 
			
		||||
@@ -56,7 +56,7 @@ function error(data, stay) {
 | 
			
		||||
                $("#status").html(data);
 | 
			
		||||
                console.log("error: "+data);
 | 
			
		||||
            } else {
 | 
			
		||||
                $("#status").html('error');
 | 
			
		||||
                $("#status").html('unknown error: '+JSON.stringify(data));
 | 
			
		||||
                console.log("error: "+JSON.stringify(data));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
@@ -686,13 +686,12 @@ function start() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// On Load, Call @ref start
 | 
			
		||||
$(
 | 
			
		||||
    window.onbeforeunload = function() {
 | 
			
		||||
        return "Are you sure you want to navigate away?";
 | 
			
		||||
    }
 | 
			
		||||
    window.onunload = function () { // you probably don't want to leave now...
 | 
			
		||||
        alert('You are trying to leave.');
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    start();
 | 
			
		||||
);
 | 
			
		||||
$(window.onbeforeunload = function() {
 | 
			
		||||
    return "Are you sure you want to navigate away?";
 | 
			
		||||
});
 | 
			
		||||
$(window.onunload = function() { // you probably don't want to leave now...
 | 
			
		||||
    alert('You are trying to leave.');
 | 
			
		||||
    return false;
 | 
			
		||||
});
 | 
			
		||||
$(start);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										30
									
								
								html/schema.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								html/schema.sql
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
CREATE TABLE IF NOT EXISTS `user` (
 | 
			
		||||
  `name` varchar(50) NOT NULL UNIQUE COMMENT 'unique name of the user',
 | 
			
		||||
  `pubkey` text NOT NULL COMMENT 'armored gnupg public key of the user',
 | 
			
		||||
  PRIMARY KEY (`name`)
 | 
			
		||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='list of all registered users and their public keys';
 | 
			
		||||
create table if not exists `message` (
 | 
			
		||||
        `id`
 | 
			
		||||
          int not null auto_increment
 | 
			
		||||
          comment "id of the message, it is used in the client to check if a message has already been downloaded or not",
 | 
			
		||||
        `time`
 | 
			
		||||
          timestamp default current_timestamp
 | 
			
		||||
          comment "time when the message has been stored on the server",
 | 
			
		||||
        `user`
 | 
			
		||||
          varchar(50) not null
 | 
			
		||||
          comment "name of the user that sent the message",
 | 
			
		||||
        `msg`
 | 
			
		||||
          longtext not null
 | 
			
		||||
          comment "message content, must be armored gnupg encrypted format",
 | 
			
		||||
        primary key (id),
 | 
			
		||||
        foreign key (user)
 | 
			
		||||
          references user(name)
 | 
			
		||||
          on delete cascade
 | 
			
		||||
          on update cascade
 | 
			
		||||
      ) character set utf8 engine=innodb
 | 
			
		||||
        comment="table to hold all messages for later download by the receiver";
 | 
			
		||||
create table if not exists options (
 | 
			
		||||
       name varchar(50) not null unique comment "option name",
 | 
			
		||||
       value text not null comment "option value",
 | 
			
		||||
       primary key (name)
 | 
			
		||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 comment="table for system settings";
 | 
			
		||||
@@ -26,7 +26,7 @@
 | 
			
		||||
//       1         2         3         4         5         6         7         8
 | 
			
		||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
try {
 | 
			
		||||
  require_once("usertable.php");
 | 
			
		||||
  require_once("opendb.php");
 | 
			
		||||
  $user = $db->real_escape_string($_REQUEST['user']);
 | 
			
		||||
  $msg = $db->real_escape_string($_REQUEST['msg']);
 | 
			
		||||
  if (strlen($_REQUEST['msg'])>100000) error("message is too long");
 | 
			
		||||
@@ -34,7 +34,6 @@ try {
 | 
			
		||||
  if (!$q || $q->num_rows!=1) error("user not found on server");
 | 
			
		||||
  $pubkey = gnupg_import($pgp, $q->fetch_row()[0]);
 | 
			
		||||
  if (!$pubkey) error("wrong identity");
 | 
			
		||||
  require_once("messagetable.php");
 | 
			
		||||
  $q = $db->query("insert into message (user, msg) values ('$user', '$msg');");
 | 
			
		||||
  if (!$q) {
 | 
			
		||||
    error_log("Error storing message: ".$db->error);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,31 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*! @file
 | 
			
		||||
 | 
			
		||||
    @id $Id$
 | 
			
		||||
 | 
			
		||||
    @see @ref usertable for the database schema
 | 
			
		||||
 | 
			
		||||
    @page database Database
 | 
			
		||||
 | 
			
		||||
    @tableofcontents
 | 
			
		||||
 | 
			
		||||
    @section usertable User Table
 | 
			
		||||
 | 
			
		||||
    <table>
 | 
			
		||||
    <caption>Table: user</caption>
 | 
			
		||||
    <tr><th>Colum Name</th><th>SQL Type</th><th>Description</th></tr>
 | 
			
		||||
    <tr><td>name</td><td>varchar(50), primary key</td><td>The user's name (pseudonym).</td></tr>
 | 
			
		||||
    <tr><td>pubkey</td><td>text</td><td>The user's public key.</td></tr>
 | 
			
		||||
    </table>
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
//       1         2         3         4         5         6         7         8
 | 
			
		||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
 | 
			
		||||
require_once("opendb.php");
 | 
			
		||||
try {
 | 
			
		||||
  $db->query(file_get_contents("usertable.sql"));
 | 
			
		||||
} catch (Exception $e) {
 | 
			
		||||
  error('database error on server');
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
@@ -1,5 +0,0 @@
 | 
			
		||||
CREATE TABLE IF NOT EXISTS `user` (
 | 
			
		||||
  `name` varchar(50) NOT NULL COMMENT 'unique name of the user',
 | 
			
		||||
  `pubkey` text NOT NULL COMMENT 'armored gnupg public key of the user',
 | 
			
		||||
  PRIMARY KEY (`name`)
 | 
			
		||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='list of all registered users and their public keys';
 | 
			
		||||
		Reference in New Issue
	
	Block a user