forked from jordansamuel/PASTE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paste.php
410 lines (360 loc) · 12.9 KB
/
paste.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
/*
* Paste <https://github.com/jordansamuel/PASTE>
*
* 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 3
* 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 in GPL.txt for more details.
*/
// PHP <5.5 compatibility
require_once('includes/password.php');
session_start();
// UTF-8
header('Content-Type: text/html; charset=utf-8');
// Required functions
require_once('config.php');
require_once('includes/geshi.php');
require_once('includes/functions.php');
// Path of GeSHi object
$path = 'includes/geshi/';
// Path of Parsedown object
$parsedown_path = 'includes/Parsedown/Parsedown.php';
// GET Paste ID
if (isset($_GET['id'])) {
$paste_id = Trim(htmlspecialchars($_GET['id']));
} elseif (isset($_POST['id'])) {
$paste_id = Trim(htmlspecialchars($_POST['id']));
}
// Prevent SQLInjection
settype($paste_id, 'integer');
// Database Connection
$con = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
if (mysqli_connect_errno()) {
die("Unable to connect to database");
}
// Get site info
$query = "SELECT * FROM site_info";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$title = Trim($row['title']);
$des = Trim($row['des']);
$baseurl = Trim($row['baseurl']);
$keyword = Trim($row['keyword']);
$site_name = Trim($row['site_name']);
$email = Trim($row['email']);
$twit = Trim($row['twit']);
$face = Trim($row['face']);
$gplus = Trim($row['gplus']);
$ga = Trim($row['ga']);
$additional_scripts = Trim($row['additional_scripts']);
}
// Set theme and language
$query = "SELECT * FROM interface";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$default_lang = Trim($row['lang']);
$default_theme = Trim($row['theme']);
}
require_once("langs/$default_lang");
// Check if IP is banned
if ( is_banned($con, $ip) ) die($lang['banned']); // "You have been banned from ".$site_name;
// Site permissions
$query = "SELECT * FROM site_permissions where id='1'";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$disableguest = Trim($row['disableguest']);
$siteprivate = Trim($row['siteprivate']);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
} else {
if ($siteprivate =="on") {
$privatesite = "on";
}
}
// Current date & user IP
$date = date('jS F Y');
$ip = $_SERVER["HTTP_CF_CONNECTING_IP"]
$data_ip = file_get_contents('tmp/temp.tdata');
// Ads
$query = "SELECT * FROM ads WHERE id='1'";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$text_ads = Trim($row['text_ads']);
$ads_1 = Trim($row['ads_1']);
$ads_2 = Trim($row['ads_2']);
}
// Logout
if (isset($_GET['logout'])) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
unset($_SESSION['token']);
unset($_SESSION['oauth_uid']);
unset($_SESSION['username']);
session_destroy();
}
// Escape from quotes
if (get_magic_quotes_gpc()) {
function callback_stripslashes(&$val, $name)
{
if (get_magic_quotes_gpc())
$val = stripslashes($val);
}
if (count($_GET))
array_walk($_GET, 'callback_stripslashes');
if (count($_POST))
array_walk($_POST, 'callback_stripslashes');
if (count($_COOKIE))
array_walk($_COOKIE, 'callback_stripslashes');
}
// Page views
$query = "SELECT @last_id := MAX(id) FROM page_view";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$last_id = $row['@last_id := MAX(id)'];
}
$query = "SELECT * FROM page_view WHERE id=" . Trim($last_id);
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$last_date = $row['date'];
}
if ($last_date == $date) {
if (str_contains($data_ip, $ip)) {
$query = "SELECT * FROM page_view WHERE id=" . Trim($last_id);
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$last_tpage = Trim($row['tpage']);
}
$last_tpage = $last_tpage + 1;
// IP exists, so update page views
$query = "UPDATE page_view SET tpage=$last_tpage WHERE id=" . Trim($last_id);
mysqli_query($con, $query);
} else {
$query = "SELECT * FROM page_view WHERE id=" . Trim($last_id);
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$last_tpage = Trim($row['tpage']);
$last_tvisit = Trim($row['tvisit']);
}
$last_tpage = $last_tpage + 1;
$last_tvisit = $last_tvisit + 1;
// Update both tpage and tvisit.
$query = "UPDATE page_view SET tpage=$last_tpage,tvisit=$last_tvisit WHERE id=" . Trim($last_id);
mysqli_query($con, $query);
file_put_contents('tmp/temp.tdata', $data_ip . "\r\n" . $ip);
}
} else {
// Delete the file and clear data_ip
unlink("tmp/temp.tdata");
$data_ip = "";
// New date is created
$query = "INSERT INTO page_view (date,tpage,tvisit) VALUES ('$date','1','1')";
mysqli_query($con, $query);
// Update the IP
file_put_contents('tmp/temp.tdata', $data_ip . "\r\n" . $ip);
}
$query = "SELECT * FROM pastes WHERE id='$paste_id'";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {
$query = "SELECT * FROM pastes WHERE id='$paste_id'";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$p_title = $row['title'];
$p_content = $row['content'];
$p_visible = $row['visible'];
$p_code = $row['code'];
$p_expiry = Trim($row['expiry']);
$p_password = $row['password'];
$p_member = $row['member'];
$p_date = $row['date'];
$p_encrypt = $row['encrypt'];
$p_views = $row['views'];
}
$p_private_error = '0';
if ($p_visible == "2") {
if (isset($_SESSION['username'])) {
if ($p_member == Trim($_SESSION['username'])) {
} else {
$notfound = $lang['privatepaste']; //" This is a private paste.";
$p_private_error = '1';
goto Not_Valid_Paste;
}
} else {
$notfound = $lang['privatepaste']; //" This is a private paste. If you created this paste, please login to view it.";
$p_private_error = '1';
goto Not_Valid_Paste;
}
}
if ($p_expiry == "NULL" || $p_expiry == "SELF") {
} else {
$input_time = $p_expiry;
$current_time = mktime(date("H"), date("i"), date("s"), date("n"), date("j"), date("Y"));
if ($input_time < $current_time) {
$notfound = $lang['expired'];
$p_private_error = 1;
goto Not_Valid_Paste;
}
}
if ($p_encrypt == "" || $p_encrypt == null || $p_encrypt == '0') {
} else {
$p_content = decrypt($p_content);
}
$op_content = Trim(htmlspecialchars_decode($p_content));
// Download the paste
if (isset($_GET['download'])) {
if ($p_password == "NONE") {
doDownload($paste_id, $p_title, $op_content, $p_code);
exit();
} else {
if (isset($_GET['password'])) {
if (password_verify($_GET['password'],$p_password)) {
doDownload($paste_id, $p_title, $op_content, $p_code);
exit();
} else {
$error = $lang['wrongpassword']; // 'Wrong password';
}
} else {
$error = $lang['pwdprotected']; // 'Password protected paste';
}
}
}
// Raw view
if (isset($_GET['raw'])) {
if ($p_password == "NONE") {
rawView($paste_id, $p_title, $op_content, $p_code);
exit();
} else {
if (isset($_GET['password'])) {
if (password_verify($_GET['password'],$p_password)) {
rawView($paste_id, $p_title, $op_content, $p_code);
exit();
} else {
$error = $lang['wrongpassword']; // 'Wrong password';
}
} else {
$error = $lang['pwdprotected']; // 'Password protected paste';
}
}
}
// Preprocess
$highlight = array();
$prefix_size = strlen('!highlight!');
if ($prefix_size) {
$lines = explode("\n", $p_content);
$p_content = "";
foreach ($lines as $idx => $line) {
if (substr($line, 0, $prefix_size) == '!highlight!') {
$highlight[] = $idx + 1;
$line = substr($line, $prefix_size);
}
$p_content .= $line . "\n";
}
$p_content = rtrim($p_content);
}
// Apply syntax highlight
$p_content = htmlspecialchars_decode($p_content);
if ( $p_code == "markdown" ) {
include( $parsedown_path );
$Parsedown = new Parsedown();
$p_content = $Parsedown->text( $p_content );
} else {
$geshi = new GeSHi($p_content, $p_code, $path);
$geshi->enable_classes();
$geshi->set_header_type(GESHI_HEADER_DIV);
$geshi->set_line_style('color: #aaaaaa; width:auto;');
$geshi->set_code_style('color: #757584;');
if (count($highlight)) {
$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
$geshi->highlight_lines_extra($highlight);
$geshi->set_highlight_lines_extra_style('color:#399bff;background:rgba(38,92,255,0.14);');
} else {
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 2);
}
$p_content = $geshi->parse_code();
$style = $geshi->get_stylesheet();
$ges_style = '<style>' . $style . '</style>';
}
// Embed view after GeSHI is applied so that $p_code is syntax highlighted as it should be.
if (isset($_GET['embed'])) {
if ( $p_password == "NONE" ) {
embedView( $paste_id, $p_title, $p_content, $p_code, $title, $baseurl, $ges_style, $lang );
exit();
} else {
if ( isset( $_GET['password'] ) ) {
if ( password_verify( $_GET['password'], $p_password ) ) {
embedView( $paste_id, $p_title, $p_content, $p_code, $title, $p_baseurl, $ges_style, $lang );
exit();
} else {
$error = $lang['wrongpassword']; // 'Wrong password';
}
} else {
$error = $lang['pwdprotected']; // 'Password protected paste';
}
}
}
} else {
header("HTTP/1.1 404 Not Found");
$notfound = $lang['notfound']; // "Not found";
}
require_once('theme/' . $default_theme . '/header.php');
if ($p_password == "NONE") {
// No password & diplay the paste
// Set download URL
if ($mod_rewrite == '1') {
$p_download = "download/$paste_id";
} else {
$p_download = "paste.php?download&id=$paste_id";
}
// Set raw URL
if ($mod_rewrite == '1') {
$p_raw = "raw/$paste_id";
} else {
$p_raw = "paste.php?raw&id=$paste_id";
}
// Set embed URL
if ( $mod_rewrite == '1' ) {
$p_embed = "embed/$paste_id";
} else {
$p_embed = "paste.php?embed&id=$paste_id";
}
// Theme
require_once('theme/' . $default_theme . '/view.php');
updateMyView($con, $paste_id);
if ($p_expiry == "SELF") {
deleteMyPaste($con, $paste_id);
}
} else {
$p_download = "paste.php?download&id=$paste_id&password=" . password_hash(isset($_POST['mypass']), PASSWORD_DEFAULT);
$p_raw = "paste.php?raw&id=$paste_id&password=" . password_hash(isset($_POST['mypass']), PASSWORD_DEFAULT);
// Check password
if (isset($_POST['mypass'])) {
if (password_verify($_POST['mypass'], $p_password)) {
// Theme
require_once('theme/' . $default_theme . '/view.php');
updateMyView($con, $paste_id);
if ($p_expiry == "SELF") {
deleteMyPaste($con, $paste_id);
}
} else {
$error = $lang['wrongpwd']; //"Password is wrong";
require_once('theme/' . $default_theme . '/errors.php');
}
} else {
// Display errors
require_once('theme/' . $default_theme . '/errors.php');
}
}
Not_Valid_Paste:
// Private paste not valid
if ($p_private_error == '1') {
// Display errors
require_once('theme/' . $default_theme . '/header.php');
require_once('theme/' . $default_theme . '/errors.php');
}
// Footer
require_once('theme/' . $default_theme . '/footer.php');
?>