forked from jordansamuel/PASTE
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuser.php
More file actions
executable file
·380 lines (338 loc) · 15.7 KB
/
user.php
File metadata and controls
executable file
·380 lines (338 loc) · 15.7 KB
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
<?php
/*
* Paste $v3.4 2026/01/10 https://github.com/boxlabss/PASTE
* demo: https://paste.boxlabs.uk/
*
* https://phpaste.sourceforge.io/
*
* 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 LICENCE for more details.
*/
require_once __DIR__ . '/includes/session.php';
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/functions.php';
// UTF-8
header('Content-Type: text/html; charset=utf-8');
$date = date('Y-m-d H:i:s');
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
global $pdo;
// JSON response for ajax
function send_json($ok, $msg = '', $extra = []) {
header_remove('Content-Type');
header('Content-Type: application/json; charset=utf-8');
echo json_encode(array_merge(['success' => (bool)$ok, 'message' => $msg], $extra));
exit;
}
try {
// site_info
$stmt = $pdo->query("SELECT * FROM site_info WHERE id = '1'");
$si = $stmt->fetch() ?: [];
$title = trim($si['title'] ?? '');
$des = trim($si['des'] ?? '');
$baseurl = rtrim(trim($si['baseurl'] ?? ''), '/') . '/';
$keyword = trim($si['keyword'] ?? '');
$site_name = trim($si['site_name'] ?? '');
$email = trim($si['email'] ?? '');
$twit = trim($si['twit'] ?? '');
$face = trim($si['face'] ?? '');
$gplus = trim($si['gplus'] ?? '');
$ga = trim($si['ga'] ?? '');
$additional_scripts = trim($si['additional_scripts'] ?? '');
$mod_rewrite = (string)($si['mod_rewrite'] ?? '1');
// interface
$stmt = $pdo->query("SELECT * FROM interface WHERE id = '1'");
$iface = $stmt->fetch() ?: [];
$default_lang = trim($iface['lang'] ?? 'en.php');
$default_theme = trim($iface['theme'] ?? 'default');
require_once("langs/$default_lang");
// ban check
if (is_banned($pdo, $ip)) {
if (isset($_POST['ajax']) && $_POST['ajax'] === '1') {
send_json(false, $lang['banned'] ?? 'You are banned from this site.');
}
die(htmlspecialchars($lang['banned'] ?? 'You are banned from this site.', ENT_QUOTES, 'UTF-8'));
}
// permissions
$stmt = $pdo->query("SELECT * FROM site_permissions WHERE id = '1'");
$perm = $stmt->fetch() ?: [];
$siteprivate = trim($perm['siteprivate'] ?? 'off');
if ($_SERVER['REQUEST_METHOD'] !== 'POST' && $siteprivate === "1") {
$privatesite = "1";
}
// profile username required - accept from GET or POST
$profile_username = trim($_GET['user'] ?? $_POST['user'] ?? '');
if ($profile_username === '' || !existingUser($pdo, $profile_username)) {
header("Location: ../");
exit;
}
$p_title = $profile_username . ($lang['user_public_pastes'] ?? 'Public Pastes');
$is_me = (isset($_SESSION['username']) && $_SESSION['username'] === $profile_username);
// Get user info and all stats in ONE query
$stmt = $pdo->prepare("
SELECT
u.id AS user_id,
u.date AS join_date,
COUNT(p.id) AS total_pastes,
SUM(CASE WHEN p.visible = 0 THEN 1 ELSE 0 END) AS total_public,
SUM(CASE WHEN p.visible = 1 THEN 1 ELSE 0 END) AS total_unlisted,
SUM(CASE WHEN p.visible = 2 THEN 1 ELSE 0 END) AS total_private
FROM users u
LEFT JOIN pastes p ON p.member = u.username
WHERE u.username = ?
GROUP BY u.id
");
$stmt->execute([$profile_username]);
$stats = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
$profile_user_id = (int)($stats['user_id'] ?? 0);
$profile_join_date = $stats['join_date'] ?? '';
$profile_total_pastes = (int)($stats['total_pastes'] ?? 0);
$profile_total_public = (int)($stats['total_public'] ?? 0);
$profile_total_unlisted = (int)($stats['total_unlisted'] ?? 0);
$profile_total_private = (int)($stats['total_private'] ?? 0);
// Get total views separately
$stmt = $pdo->prepare("
SELECT COALESCE(COUNT(pv.id), 0)
FROM pastes p
LEFT JOIN paste_views pv ON p.id = pv.paste_id
WHERE p.member = ?
");
$stmt->execute([$profile_username]);
$profile_total_paste_views = (int)$stmt->fetchColumn();
// logout
if (isset($_GET['logout'])) {
$ref = $_SERVER['HTTP_REFERER'] ?? $baseurl;
unset($_SESSION['token'], $_SESSION['oauth_uid'], $_SESSION['username']);
session_destroy();
header('Location: ' . $ref);
exit;
}
// page views tracking
$view_date = date('Y-m-d');
try {
$stmt = $pdo->prepare("SELECT id, tpage, tvisit FROM page_view WHERE date = ?");
$stmt->execute([$view_date]);
$pv = $stmt->fetch();
if ($pv) {
$page_view_id = $pv['id'];
$tpage = (int)$pv['tpage'] + 1;
$tvisit = (int)$pv['tvisit'];
$stmt = $pdo->prepare("SELECT COUNT(*) FROM visitor_ips WHERE ip = ? AND visit_date = ?");
$stmt->execute([$ip, $view_date]);
if ((int)$stmt->fetchColumn() === 0) {
$tvisit++;
$stmt = $pdo->prepare("INSERT INTO visitor_ips (ip, visit_date) VALUES (?, ?)");
$stmt->execute([$ip, $view_date]);
}
$stmt = $pdo->prepare("UPDATE page_view SET tpage = ?, tvisit = ? WHERE id = ?");
$stmt->execute([$tpage, $tvisit, $page_view_id]);
} else {
$stmt = $pdo->prepare("INSERT INTO page_view (date, tpage, tvisit) VALUES (?, ?, ?)");
$stmt->execute([$view_date, 1, 1]);
$stmt = $pdo->prepare("INSERT INTO visitor_ips (ip, visit_date) VALUES (?, ?)");
$stmt->execute([$ip, $view_date]);
}
} catch (PDOException $e) {
error_log("Page view tracking error: " . $e->getMessage());
}
// get current user ID for ownership checks
$getCurrentUserId = function() use ($pdo) {
if (empty($_SESSION['username'])) return 0;
try {
$q = $pdo->prepare("SELECT id FROM users WHERE username = ?");
$q->execute([$_SESSION['username']]);
return (int)($q->fetchColumn() ?: 0);
} catch (Throwable $e) {
return 0;
}
};
// Delete comment (ajax)
if (isset($_GET['action']) && $_GET['action'] === 'delete_comment') {
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST' || ($_POST['ajax'] ?? '') !== '1') {
send_json(false, 'Bad request.');
}
if (empty($_SESSION['token']) || empty($_SESSION['username'])) {
send_json(false, $lang['not_logged_in'] ?? 'You must be logged in.');
}
if (!isset($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'] ?? '', (string)$_POST['csrf_token'])) {
send_json(false, $lang['invalidtoken'] ?? 'Invalid CSRF token.');
}
$comment_id = (int)($_POST['comment_id'] ?? 0);
if ($comment_id <= 0) {
send_json(false, 'Invalid comment.');
}
$uid = $getCurrentUserId();
if (!userOwnsComment($pdo, $comment_id, $uid, $_SESSION['username'])) {
send_json(false, $lang['delete_error_invalid'] ?? 'Not allowed.');
}
if (!deleteComment($pdo, $comment_id)) {
send_json(false, $lang['wentwrong'] ?? 'Failed to delete comment.');
}
send_json(true, $lang['deleted'] ?? 'Deleted.', ['id' => $comment_id]);
}
// Delete comment
if ($_SERVER['REQUEST_METHOD'] === 'POST'
&& isset($_POST['action']) && $_POST['action'] === 'delete_comment') {
$is_ajax = (
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest')
|| (isset($_POST['ajax']) && $_POST['ajax'] === '1')
);
$redir = $baseurl . ($mod_rewrite === '1' ? 'user/' . rawurlencode($profile_username)
: 'user.php?user=' . rawurlencode($profile_username));
$goto = $redir . (strpos($redir, '?') === false ? '?' : '&');
do {
if (empty($_SESSION['username']) || $_SESSION['username'] !== $profile_username) {
$msg = $lang['not_logged_in'] ?? 'You must be logged in.';
if ($is_ajax) { send_json(false, $msg); }
header('Location: ' . $goto . 'c_err=' . rawurlencode($msg) . '#my-comments'); exit;
}
if (empty($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'] ?? '', (string)$_POST['csrf_token'])) {
$msg = $lang['invalidtoken'] ?? 'Invalid CSRF token.';
if ($is_ajax) { send_json(false, $msg); }
header('Location: ' . $goto . 'c_err=' . rawurlencode($msg) . '#my-comments'); exit;
}
$cid = (int)($_POST['comment_id'] ?? 0);
if ($cid <= 0) {
$msg = 'Invalid comment.';
if ($is_ajax) { send_json(false, $msg); }
header('Location: ' . $goto . 'c_err=' . rawurlencode($msg) . '#my-comments'); exit;
}
$uid = $getCurrentUserId();
if (!userOwnsComment($pdo, $cid, $uid, $profile_username)) {
$msg = $lang['delete_error_invalid'] ?? 'Not allowed.';
if ($is_ajax) { send_json(false, $msg); }
header('Location: ' . $goto . 'c_err=' . rawurlencode($msg) . '#my-comments'); exit;
}
if (!deleteComment($pdo, $cid)) {
$msg = $lang['wentwrong'] ?? 'Failed to delete comment.';
if ($is_ajax) { send_json(false, $msg); }
header('Location: ' . $goto . 'c_err=' . rawurlencode($msg) . '#my-comments'); exit;
}
$msg = $lang['deleted'] ?? 'Comment deleted.';
if ($is_ajax) { send_json(true, $msg, ['id' => $cid]); }
header('Location: ' . $goto . 'c_ok=' . rawurlencode($msg) . '#my-comments'); exit;
} while (false);
}
// Delete a paste
if (isset($_GET['del'])) {
$is_ajax = (isset($_POST['ajax']) && $_POST['ajax'] === '1');
if (empty($_SESSION['token']) || empty($_SESSION['username'])) {
if ($is_ajax) {
send_json(false, $lang['not_logged_in'] ?? 'You must be logged in to delete pastes.');
}
$error = $lang['not_logged_in'] ?? 'You must be logged in to delete pastes.';
} else {
$paste_id = (int)($_GET['id'] ?? 0);
$owner = (string)($_SESSION['username'] ?? '');
if ($paste_id <= 0) {
if ($is_ajax) send_json(false, $lang['delete_error_invalid'] ?? 'Invalid paste or not authorized to delete.');
$error = $lang['delete_error_invalid'] ?? 'Invalid paste or not authorized to delete.';
} else {
$stmt = $pdo->prepare("SELECT COUNT(*) FROM pastes WHERE id = ? AND member = ?");
$stmt->execute([$paste_id, $owner]);
if ((int)$stmt->fetchColumn() === 0) {
if ($is_ajax) send_json(false, $lang['delete_error_invalid'] ?? 'Invalid paste or not authorized to delete.');
$error = $lang['delete_error_invalid'] ?? 'Invalid paste or not authorized to delete.';
} else {
// perform delete
$stmt = $pdo->prepare("DELETE FROM pastes WHERE id = ? AND member = ?");
$stmt->execute([$paste_id, $owner]);
// also clean up views
try {
$stmt = $pdo->prepare("DELETE FROM paste_views WHERE paste_id = ?");
$stmt->execute([$paste_id]);
} catch (PDOException $e) {
// ignore
}
if ($is_ajax) {
send_json(true, $lang['pastedeleted'] ?? 'Paste deleted successfully.', ['id' => $paste_id]);
}
$success = $lang['pastedeleted'] ?? 'Paste deleted successfully.';
$redirect = $baseurl . ($mod_rewrite === '1' ? 'user/' . urlencode($owner) : 'user.php?user=' . urlencode($owner));
header('Location: ' . $redirect);
exit;
}
}
}
}
// Pagination for pastes
$per = max(5, min(100, (int)($_GET['per'] ?? 25)));
$page = max(1, (int)($_GET['page'] ?? 1));
// Visibility filter: owner sees all; others see public only
$vis_sql = $is_me ? '' : ' AND p.visible = 0';
// Count total
$stmt = $pdo->prepare("SELECT COUNT(*) FROM pastes p WHERE p.member = ?" . $vis_sql);
$stmt->execute([$profile_username]);
$total_pastes = (int)$stmt->fetchColumn();
$total_pages = max(1, (int)ceil($total_pastes / $per));
$page = min($page, $total_pages);
$offset = ($page - 1) * $per;
// Fetch pastes with views count in one query
$stmt = $pdo->prepare("
SELECT
p.id, p.slug, p.title, p.code, p.date, p.visible,
COALESCE(v.view_count, 0) AS views
FROM pastes p
LEFT JOIN (
SELECT paste_id, COUNT(*) AS view_count
FROM paste_views
GROUP BY paste_id
) v ON v.paste_id = p.id
WHERE p.member = ?
$vis_sql
ORDER BY p.date DESC
LIMIT ? OFFSET ?
");
$stmt->execute([$profile_username, $per, $offset]);
$pastesPage = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
// Range text
$range_from = $total_pastes ? ($offset + 1) : 0;
$range_to = min($offset + count($pastesPage), $total_pastes);
// URL builder for pagination
$profile_url = function(array $add = [], array $drop = []) use ($baseurl, $mod_rewrite, $profile_username) {
$base = $baseurl . ($mod_rewrite === '1' ? ('user/' . rawurlencode($profile_username)) : ('user.php?user=' . rawurlencode($profile_username)));
$q = $_GET;
foreach ($drop as $k) unset($q[$k]);
foreach ($add as $k => $v) {
if ($v === null) unset($q[$k]); else $q[$k] = $v;
}
$qs = http_build_query($q);
return $base . ($mod_rewrite === '1' ? ($qs ? ('?' . $qs) : '') : ($qs ? ('&' . $qs) : ''));
};
// User comments
$stmt = $pdo->query("SELECT * FROM ads WHERE id = '1'");
$ads = $stmt->fetch() ?: [];
$text_ads = trim($ads['text_ads'] ?? '');
$ads_1 = trim($ads['ads_1'] ?? '');
$ads_2 = trim($ads['ads_2'] ?? '');
// Comments: owner sees all, others see public+unlisted pastes only
$vis_comment_sql = $is_me ? '' : ' AND p.visible IN (0,1)';
$stmt = $pdo->prepare("
SELECT c.id, c.paste_id, c.user_id, c.username, c.body, c.created_at,
p.title, p.visible, p.slug AS paste_slug
FROM paste_comments c
JOIN pastes p ON p.id = c.paste_id
WHERE (c.user_id = :uid OR c.username = :uname)
$vis_comment_sql
ORDER BY c.created_at DESC
LIMIT 200
");
$stmt->execute([
':uid' => $profile_user_id,
':uname' => $profile_username
]);
$user_comments = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
$total_user_comments = count($user_comments);
// render
require_once('theme/' . htmlspecialchars($default_theme, ENT_QUOTES, 'UTF-8') . '/header.php');
require_once('theme/' . htmlspecialchars($default_theme, ENT_QUOTES, 'UTF-8') . '/user_profile.php');
require_once('theme/' . htmlspecialchars($default_theme, ENT_QUOTES, 'UTF-8') . '/footer.php');
} catch (PDOException $e) {
die("Database error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'));
}