sermon/api.php
2018-10-17 13:22:57 +02:00

43 lines
No EOL
1.3 KiB
PHP

<?php
$database = new SQLite3('outchat.db');
$action = $_GET['action'];
if($action === 'getMessages'){
$timestamp = $database->escapeString(htmlspecialchars($_GET['timestamp']));
$timestamp = ($timestamp == 0) ? strtotime('-6 hours') : $timestamp;
$results = $database->query('SELECT * FROM messages WHERE timestamp > '.$timestamp);
$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']));
/*
$payload = file_get_contents('php://input');
$data = json_decode($payload);
var_dump($data);
*/
$database->query('INSERT INTO messages (name, text, timestamp) VALUES ("'.$name.'", "'.$text.'", "'.$timestamp.'")');
}
?>