forked from artyuum/simple-note
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.php
100 lines (90 loc) · 2.95 KB
/
notes.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
// Core (class)
class Notes {
private $pdo;
function __construct($db) {
$this->pdo = new PDO('sqlite:'.$db);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->pdo->exec('CREATE TABLE IF NOT EXISTS notes (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
created DATETIME NOT NULL
);');
}
public function fetchNotes($id = null) {
if ($id != null) {
$stmt = $this->pdo->prepare('SELECT title, content FROM notes WHERE id = :ID');
$stmt->bindParam(':ID', $id);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
$title = $row['title'];
header("Content-type: text/plain; charset=utf-8");
header("Content-Disposition: attachment; filename=$title.txt");
echo $row['content'];
return;
}
} else {
$stmt = $this->pdo->query('SELECT * FROM notes ORDER BY created DESC');
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
}
public function create($title, $content) {
$datetime = date("Y-m-d H:i:s");
$stmt = $this->pdo->prepare('INSERT INTO notes (title, content, created) VALUES (:title, :content, :created)');
$stmt->bindParam(':title', $title);
$stmt->bindParam(':content', $content);
$stmt->bindParam(':created', $datetime);
$stmt->execute();
}
public function delete($id) {
if ($id == 'all') {
$stmt = $this->pdo->query('DELETE FROM notes; VACUUM');
} else {
$stmt = $this->pdo->prepare('DELETE FROM notes WHERE id = :ID');
$stmt->bindParam(':ID', $id);
$stmt->execute();
}
}
public function edit($id, $title, $content) {
$stmt = $this->pdo->prepare('UPDATE notes SET title = :title, content = :content WHERE id = :ID');
$stmt->bindParam(':ID', $id);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':content', $content);
$stmt->execute();
}
}
// Init core (class)
$notes = new Notes($Config["Database"]);
// Actions
if (isset($_POST['new'])) {
$title = $_POST['title'];
$content = $_POST['content'];
$notes->create($title, $content);
header('Location: .');
exit();
}
if (isset($_POST['edit'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$notes->edit($id, $title, $content);
header('Location: .');
exit();
}
if (!empty($_GET['del'])) {
$id = $_GET['del'];
$notes->delete($id);
header('Location: .');
exit();
}
if (!empty($_GET['dl'])) {
$id = $_GET['dl'];
$notes->fetchNotes($id);
exit();
}
require_once("template.inc.php");
?>