sermon/api.php

43 lines
1.5 KiB
PHP
Raw Normal View History

2018-10-17 13:22:43 +02:00
<?php
$database = new SQLite3('outchat.db');
$action = $_GET['action'];
2018-10-17 13:37:53 +02:00
if ($action === 'getMessages') {
2018-10-17 13:22:43 +02:00
$timestamp = $database->escapeString(htmlspecialchars($_GET['timestamp']));
$timestamp = ($timestamp == 0) ? strtotime('-6 hours') : $timestamp;
2018-10-17 14:08:18 +02:00
$statement = $database->prepare('SELECT * FROM messages WHERE timestamp > :timestamp');
$statement->bindValue('timestamp', $timestamp);
$results = $statement->execute();
2018-10-17 13:22:43 +02:00
$messageArray = [];
2018-10-17 13:37:53 +02:00
while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
2018-10-17 13:22:43 +02:00
$row['datetime'] = date('d/m H:i', $row['timestamp']);
$image_search = preg_match('/(http|https):\/\/[^ ]+(\.gif|\.jpg|\.jpeg|\.png)/', $row['text'], $out);
2018-10-17 13:37:53 +02:00
if ($image_search > 0) {
$row['text_processed'] = str_replace($out[0], '<p><img src="' . $out[0] . '" /></p>', $row['text']);
2018-10-17 13:22:43 +02:00
} else {
$row['text_processed'] = $row['text'];
}
$messageArray[] = $row;
}
echo json_encode($messageArray);
}
2018-10-17 13:37:53 +02:00
if ($action === 'createMessage') {
2018-10-17 13:22:43 +02:00
$timestamp = time();
$name = $database->escapeString(htmlspecialchars($_POST['name']));
$text = $database->escapeString(htmlspecialchars($_POST['text']));
2018-10-17 14:08:18 +02:00
$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();
2018-10-17 13:22:43 +02:00
}
?>