-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.php
344 lines (296 loc) · 11.8 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* Pasthis - Stupid Simple Pastebin
*
* Copyright (C) 2014 - 2020 Julien (jvoisin) Voisin - dustri.org
* Copyright (C) 2014 - 2020 Antoine Tenart <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
final class Pasthis {
public $title;
private $contents = array();
private $db;
function __construct($title = 'Pasthis - Simple pastebin') {
$this->title = $title;
$dsn = 'sqlite:' . dirname(__FILE__) .'/pasthis.db';
try {
$this->db = new PDO($dsn);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $e) {
die('Unable to open database: ' . $e->getMessage());
}
$this->db->exec('pragma auto_vacuum = 1');
$this->db->exec(
"CREATE TABLE if not exists pastes (
id PRIMARY KEY,
deletion_date TIMESTAMP,
highlighting INTEGER,
wrap INTEGER,
paste BLOB
);"
);
$this->db->exec(
"CREATE TABLE if not exists users (
hash PRIMARY KEY,
nopaste_period TIMESTAMP,
degree INTEGER
);"
);
}
function __destruct() {
$this->db = null;
}
private function add_content($content, $prepend = false) {
if (!$prepend)
$this->contents[] = $content;
else
array_unshift($this->contents, $content);
}
private function render() {
print '<!DOCTYPE html>';
print '<html>';
print '<head>';
print '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
print '<title>Pasthis - Simple pastebin</title>';
print '<link href="./css/normalize.css" rel="stylesheet" type="text/css">';
print '<link href="./css/skeleton.css" rel="stylesheet" type="text/css">';
print '<link href="./css/style.css" rel="stylesheet" type="text/css">';
print '<link href="./css/prettify.css" rel="stylesheet" type="text/css">';
print '</head>';
print '<body>';
print '<div class="container">';
print '<h1>Pasthis</h1>';
foreach ($this->contents as $ct)
print $ct;
print '<div class="footer">';
print 'Powered by <a href="https://github.com/moulecorp/pasthis">Pasthis</a> | ';
print '<a href="./?cli">Command-line tool</a> | ';
print 'No statistics, no list.';
print '</div>';
print '</div>';
print '</body>';
exit();
}
private function remaining_time($timestamp) {
if ($timestamp == -1)
return 'Never expires.';
elseif ($timestamp == 0)
return 'Expired.';
elseif ($timestamp == -2)
return 'One reading remaining.';
$format = function($t,$s) { return $t ? $t.' '.$s.($t>1 ? 's' : '' ).' ' : ''; };
$expiration = new DateTime('@'.$timestamp);
$interval = $expiration->diff(new DateTime(), true);
$ret = 'Expires in '.$format($interval->d, 'day');
$ret .= $format($interval->h, 'hour');
if ($interval->d === 0) {
$ret .= $format($interval->i, 'minute');
if ($interval->h === 0)
$ret .= $format($interval->s, 'second');
}
return rtrim($ret).'.';
}
function prompt_paste() {
$this->add_content(
'<form method="post" action=".">
<div class="row">
<select name="d">
<option value="86400" selected hidden>Expiration (1 day)</option>
<option value="-2">burn after reading</option>
<option value="600">10 minutes</option>
<option value="3600">1 hour</option>
<option value="86400">1 day</option>
<option value="604800">1 week</option>
<option value="-1">eternal</option>
</select>
<button type="submit">Send paste</button>
<div class="right">
<label>
<input type="checkbox" name="wrap">
<span class="label-body">wrap long lines</span>
</label>
<label>
<input type="checkbox" name="highlighting">
<span class="label-body" for="">syntax highlighting</span>
</label>
</div>
</div>
<div class="row">
<input type="text" id="ricard" name="ricard" placeholder="Do not fill me!">
<textarea autofocus required name="p" placeholder="Tab key can be used."></textarea>
</div>
</form>'
);
$this->add_content('<script defer src="./js/textarea.js"></script>');
$this->render();
}
private function generate_id() {
$query = $this->db->prepare(
"SELECT id FROM pastes
WHERE id = :uniqid;"
);
$query->bindParam(':uniqid', $uniqid, PDO::PARAM_STR, 6);
do {
$uniqid = substr(uniqid(), -6);
$query->execute();
} while ($query->fetch() != false);
return $uniqid;
}
private function nopaste_period($degree) {
return time() + intval(pow($degree, 2.5));
}
private function check_spammer() {
$hash = sha1($_SERVER['REMOTE_ADDR']);
$query = $this->db->prepare(
"SELECT * FROM users
WHERE hash = :hash"
);
$query->bindValue(':hash', $hash, PDO::PARAM_STR);
$query->execute();
$result = $query->fetch();
$in_period = (!empty($result) and time() < $result['nopaste_period']);
$obvious_spam = (!isset($_POST['ricard']) or !empty($_POST['ricard']));
$degree = $in_period ? $result['degree']+1 : ($obvious_spam ? 512 : 1);
$nopaste_period = $this->nopaste_period($degree);
$query = $this->db->prepare(
"REPLACE INTO users
(hash, nopaste_period, degree)
VALUES (:hash, :nopaste_period, :degree);"
);
$query->bindValue(':hash', $hash, PDO::PARAM_STR);
$query->bindValue(':nopaste_period', $nopaste_period, PDO::PARAM_INT);
$query->bindValue(':degree', $degree, PDO::PARAM_INT);
$query->execute();
if ($in_period or $obvious_spam)
die('Spam');
}
function add_paste($deletion_date, $highlighting, $wrap, $paste) {
$this->check_spammer();
$deletion_date = intval($deletion_date);
if ($deletion_date > 0)
$deletion_date += time();
$uniqid = $this->generate_id();
$query = $this->db->prepare(
"INSERT INTO pastes (id, deletion_date, highlighting, wrap, paste)
VALUES (:uniqid, :deletion_date, :highlighting, :wrap, :paste);"
);
$query->bindValue(':uniqid', $uniqid, PDO::PARAM_STR);
$query->bindValue(':deletion_date', $deletion_date, PDO::PARAM_INT);
$query->bindValue(':highlighting', $highlighting, PDO::PARAM_INT);
$query->bindValue(':wrap', $wrap, PDO::PARAM_INT);
$query->bindValue(':paste', $paste, PDO::PARAM_STR);
$query->execute();
header('location: ./' . $uniqid);
}
function show_paste($param) {
$id = str_replace("@raw", "", $param);
$is_raw = intval(strtolower(substr($param, -4)) == "@raw");
$fail = false;
$query = $this->db->prepare(
"SELECT * FROM pastes
WHERE id = :id;"
);
$query->bindValue(':id', $id, PDO::PARAM_STR);
$query->execute();
$result = $query->fetch();
if ($result == null) {
$fail = true;
} elseif ($result['deletion_date'] < time()
and $result['deletion_date'] >= 0) {
$query = $this->db->prepare(
"DELETE FROM pastes
WHERE id = :id;"
);
$query->bindValue(':id', $id, PDO::PARAM_STR);
$query->execute();
/* do not fail on "burn after reading" pastes */
if ($result['deletion_date'] != 0)
$fail = true;
} elseif ($result['deletion_date'] == -2) {
$query = $this->db->prepare(
"UPDATE pastes
SET deletion_date=0
WHERE id = :id;"
);
$query->bindValue(':id', $id, PDO::PARAM_STR);
$query->execute();
}
if ($fail) {
$this->add_content('<div class="warning">Meh, no paste for this id :(</div>');
$this->prompt_paste();
} else {
header('X-Content-Type-Options: nosniff');
if ($is_raw) {
header('Content-Type: text/plain; charset=utf-8');
print $result['paste'];
exit();
} else {
header('Content-Type: text/html; charset=utf-8');
$class = 'prettyprint linenums';
if ($result['highlighting']) {
$this->add_content('<script>window.onload=function(){prettyPrint();}</script>');
$this->add_content('<script defer src="./js/prettify.js"></script>', true);
$class .= ' highlighting';
}
$this->add_content(
'<a href="./'.$id.'@raw">Raw</a> | <a href="./">New paste</a>
<div class="right">'.$this->remaining_time($result['deletion_date']).'</div>'
);
if ($result['wrap'])
$class .= ' wrap';
$this->add_content('<pre class="'.$class.'">'.htmlentities($result['paste']).'</pre>');
}
}
$this->render();
}
function serve_cli() {
$uri = $_SERVER['SERVER_NAME'] . explode('?', $_SERVER['REQUEST_URI'])[0];
$script = file_get_contents('./pasthis.py');
$script = str_replace('__PASTHIS_DOMAIN_NAME__', $uri, $script);
header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: inline; filename="pasthis"');
print $script;
exit();
}
function cron() {
$this->db->exec(
"DELETE FROM pastes
WHERE deletion_date > 0
AND strftime ('%s','now') > deletion_date;
DELETE FROM users
WHERE strftime ('%s','now') > nopaste_period;"
);
}
}
$pastebin = new Pasthis();
if (php_sapi_name() == 'cli') {
$pastebin->cron();
exit();
}
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'on')
exit('Meh, not accessed over HTTPS.');
if (isset($_GET['p']))
$pastebin->show_paste($_GET['p']);
elseif (isset($_POST['d']) && isset($_POST['p']))
$pastebin->add_paste($_POST['d'], isset($_POST['highlighting']),
isset($_POST['wrap']), $_POST['p']);
elseif (isset($_GET['cli']))
$pastebin->serve_cli();
else
$pastebin->prompt_paste();
?>