forked from artyuum/simple-note
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
231 lines (199 loc) · 8.59 KB
/
index.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/include/notes_config.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/include/functions.php';
sec_session_start();
if(token_check($mysqli) != true){
if(login_check($mysqli) != true) {
header('Location: ../login?error=2');
exit();
}
}
// Core (class)
class Notes {
private $mysqli;
private $user;
function __construct($mysqli, $user) {
$this->mysqli = $mysqli;
$this->user = $user;
// Create new table if not exists.
if($stmt=$this->mysqli->prepare('CREATE TABLE IF NOT EXISTS '.$this->user.' (ID INTEGER PRIMARY KEY AUTO_INCREMENT, title TEXT NOT NULL, content TEXT NOT NULL, created DATETIME NOT NULL);')){
$stmt->execute();
}
}
public function fetchNotes($id = null) {
if ($id != null) {
if($stmt=$this->mysqli->prepare('SELECT title,content FROM '.$this->user.' WHERE id = ?')){
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
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 {
if($stmt=$this->mysqli->prepare('SELECT * FROM '.$this->user.' ORDER BY created DESC')){
$stmt->execute();
$result = $stmt->get_result();
return $result;
}
}
}
public function create($title, $content) {
$datetime = date("Y-m-d H:i:s");
if($stmt=$this->mysqli->prepare('INSERT INTO '.$this->user.' (title, content, created) VALUES (?, ?, ?)')){
$stmt->bind_param('sss',$title, $content, $datetime);
$stmt->execute();
}
}
public function delete($id) {
if ($id == 'all') {
$stmt = $this->mysqli->query('DROP table '.$this->user.' FROM notes; VACUUM');
__construct();
} else {
if($stmt=$this->mysqli->prepare('DELETE FROM '.$this->user.' WHERE id = ?')){
$stmt->bind_param('i', $id);
$stmt->execute();
}
}
}
public function edit($id, $title, $content) {
if($stmt=$this->mysqli->prepare('UPDATE '.$this->user.' SET title = ?, content = ? WHERE id = ?')){
$stmt->bind_param('ssi',$title, $content, $id);
$stmt->execute();
}
}
}
// Init core (class)
$notes = new Notes($mysqli_notes, $_SESSION['username']);
// Actions
if (isset($_POST['new'])) {
$title = $_POST['title'];
$content = $_POST['content'];
$notes->create($title, $content);
}
if (isset($_POST['edit'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$notes->edit($id, $title, $content);
}
if (!empty($_GET['del'])) {
$id = $_GET['del'];
$notes->delete($id);
}
if (!empty($_GET['dl'])) {
$id = $_GET['dl'];
$notes->fetchNotes($id);
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Simple Note - <?php echo $_SESSION['username'];?></title>
<link rel="stylesheet" href="//bootswatch.com/flatly/bootstrap.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="notes.js"></script>
<style>
.container {
max-width: 680px;
}
textarea {
resize: vertical; /* allow only vertical stretch */
}
</style>
</head>
<body>
<div class="container">
<div class="page-header">
<h2> Send a new note </h2>
</div>
<form role="form" action="index" method="POST">
<div class="form-group">
<input class="form-control" type="text" placeholder="Title" name="title" required>
</div>
<div class="form-group">
<textarea class="form-control" rows="5" placeholder="What do you have in mind ?" name="content" autofocus required></textarea>
</div>
<div class="btn-group pull-right">
<button class="btn btn-danger" type="reset"><span class="glyphicon glyphicon-remove"></span> Clear </button>
<button class="btn btn-success" name="new" type="submit"><span class="glyphicon glyphicon-send"></span> Send </button>
</div>
</form>
</div>
<?php
if (!empty($notes->fetchNotes())):
$notes = $notes->fetchNotes();
?>
<div class="container" id="notes">
<div class="page-header">
<h2> Previously sent </h2>
</div>
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Time</th>
<th>Date</th>
<th class="pull-right">Actions<br></th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach ($notes as $row): ?>
<td>
<small><?= htmlspecialchars(substr($row['title'], 0, 15), ENT_QUOTES, 'UTF-8') ?></small>
</td>
<td><?= date('H:i', strtotime($row['created'])) ?></td>
<td><?= date('d/m/Y', strtotime($row['created'])) ?></td>
<td class="pull-right">
<div class="btn-group">
<a class="btn btn-default btn-xs" title="Edit this note" href="#" data-toggle="modal" data-target="#<?= $row['ID'] ?>"><span class="glyphicon glyphicon-edit"></span></a>
<a class="btn btn-danger btn-xs" title="Delete this note" onclick="confirm_Delete(<?= $row['ID'] ?>)" ><span class="glyphicon glyphicon-trash"></span></a>
<a class="btn btn-info btn-xs" title="Download this note" href="?dl=<?= $row['ID'] ?>" target="_blank"><span class="glyphicon glyphicon-download-alt"></span></a>
</div>
</td>
</tr>
<div class="modal fade" id="<?= $row['ID'] ?>" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit note</h4>
</div>
<div class="modal-body">
<form role="form" action="index.php" method="POST">
<div class="form-group">
<input class="form-control" type="text" placeholder="Title" name="title" value="<?= htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8') ?>">
</div>
<div class="form-group">
<textarea class="form-control" rows="5" placeholder="What do you have in mind ?" name="content" required><?= htmlspecialchars($row['content'], ENT_QUOTES, 'UTF-8') ?></textarea>
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="id" value="<?= $row['ID'] ?>">
<div class="btn-group pull-right">
<button class="btn btn-success" name="edit" type="submit"><span class="glyphicon glyphicon-floppy-disk"></span> Save </button>
</div>
</div>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</body>
</html>