-
Notifications
You must be signed in to change notification settings - Fork 0
/
articles.php
42 lines (38 loc) · 1.1 KB
/
articles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
header('Access-Control-Allow-Origin: *');
require 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
session_start();
if(!isset($_SESSION["user"]) || $_SESSION["user"]["type"] != "admin") {
http_response_code(401);
exit;
}
if (empty($_GET['id'])) {
$stmt = "INSERT INTO articles (title, text) VALUES (?,?)";
$vars=array($_POST['title'], $_POST['text']);
} else {
$stmt = "UPDATE articles SET title=?, text=?, date=? WHERE id=?";
$vars=array($_POST['title'], $_POST['text'], $_POST['date'], $_GET['id']);
}
$q = $conn->prepare($stmt);
$q->execute($vars);
} else if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
session_start();
if(!isset($_SESSION["user"]) || $_SESSION["user"]["type"] != "admin") {
http_response_code(401);
exit;
}
$stmt = "DELETE FROM articles WHERE id=?";
$q = $conn->prepare($stmt);
$q->execute(array($_GET['id']));
} else {
$stmt = "SELECT *
FROM articles
ORDER BY date DESC
LIMIT 10";
$q = $conn->prepare($stmt);
$q->execute();
$info = $q->setFetchMode(PDO::FETCH_ASSOC);
$info = $q->fetchAll();
echo json_encode($info);
}