56 lines
No EOL
1.7 KiB
PHP
56 lines
No EOL
1.7 KiB
PHP
<?php
|
|
|
|
|
|
$initialized = file_exists('outchat.db');
|
|
|
|
try {
|
|
$database = new SQLite3('outchat.db');
|
|
} catch (Exception $exception) {
|
|
http_response_code(500);
|
|
echo $exception;
|
|
die(-1);
|
|
}
|
|
|
|
if (!$initialized) {
|
|
$database->exec(file_get_contents("outchat-ddl.sql"));
|
|
}
|
|
|
|
$action = $_GET['action'];
|
|
|
|
if ($action === 'getMessages') {
|
|
$timestamp = $database->escapeString(htmlspecialchars($_GET['timestamp']));
|
|
$timestamp = ($timestamp == 0) ? strtotime('-6 hours') : $timestamp;
|
|
|
|
$statement = $database->prepare('SELECT * FROM messages WHERE timestamp > :timestamp');
|
|
$statement->bindValue('timestamp', $timestamp);
|
|
$results = $statement->execute();
|
|
|
|
$messageArray = [];
|
|
while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
|
|
$row['datetime'] = date('d/m H:i', $row['timestamp']);
|
|
|
|
$image_search = preg_match('/(http|https):\/\/[^ ]+(\.gif|\.jpg|\.jpeg|\.png)/', $row['text'], $out);
|
|
|
|
if ($image_search > 0) {
|
|
$row['text_processed'] = str_replace($out[0], '<p><img src="' . $out[0] . '" /></p>', $row['text']);
|
|
} else {
|
|
$row['text_processed'] = $row['text'];
|
|
}
|
|
|
|
$messageArray[] = $row;
|
|
}
|
|
echo json_encode($messageArray);
|
|
}
|
|
|
|
if ($action === 'createMessage') {
|
|
$timestamp = time();
|
|
$name = $database->escapeString(htmlspecialchars($_POST['name']));
|
|
$text = $database->escapeString(htmlspecialchars($_POST['text']));
|
|
|
|
$statement = $database->prepare('INSERT INTO messages (name, text, timestamp) VALUES (:name, :text, :timestamp)');
|
|
$statement->bindValue(':name', $name, SQLITE3_TEXT);
|
|
$statement->bindValue(':text', $text, SQLITE3_TEXT);
|
|
$statement->bindValue(':timestamp', $timestamp, SQLITE3_INTEGER);
|
|
$statement->execute();
|
|
}
|
|
?>
|