forked from jordansamuel/PASTE
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharchive.php
More file actions
executable file
·331 lines (289 loc) · 12 KB
/
archive.php
File metadata and controls
executable file
·331 lines (289 loc) · 12 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
<?php
/*
* Paste $v3.3 2025/10/24 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';
// Search API
if (isset($_GET['api']) && $_GET['api'] === 'search') {
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$query = trim($_GET['q'] ?? '');
if (strlen($query) < 2) {
echo json_encode(['success' => false, 'error' => 'Query too short (min 2 chars)']);
exit;
}
$limit = min(50, max(1, (int)($_GET['limit'] ?? 10)));
$page = max(1, (int)($_GET['page'] ?? 1));
$offset = ($page - 1) * $limit;
$search_term = '%' . $query . '%';
// Check if slug column exists
$hasSlug = false;
try {
$check = $pdo->query("SHOW COLUMNS FROM pastes LIKE 'slug'");
$hasSlug = $check->fetch() !== false;
} catch (PDOException $e) {}
$slugCol = $hasSlug ? ', p.slug' : '';
// Get site info for URLs
$stmt = $pdo->query("SELECT baseurl FROM site_info WHERE id = 1");
$si = $stmt->fetch() ?: [];
$baseurl = rtrim($si['baseurl'] ?? '', '/') . '/';
// mod_rewrite comes from config.php (global)
global $mod_rewrite;
$use_mod_rewrite = ((string)($mod_rewrite ?? '0') === '1');
$sql = "SELECT p.id{$slugCol}, p.title, p.code, p.date, p.member, COUNT(pv.id) AS views
FROM pastes p LEFT JOIN paste_views pv ON p.id = pv.paste_id
WHERE p.visible = '0' AND p.password = 'NONE' AND p.encrypt = '0'
AND (p.title LIKE ? OR p.content LIKE ?)
GROUP BY p.id ORDER BY p.date DESC LIMIT ? OFFSET ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$search_term, $search_term, $limit, $offset]);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Add URLs to results
foreach ($results as &$row) {
$identifier = ($hasSlug && !empty($row['slug'])) ? $row['slug'] : $row['id'];
$row['url'] = $use_mod_rewrite ? $baseurl . $identifier : $baseurl . 'paste.php?id=' . $identifier;
}
unset($row);
$count_sql = "SELECT COUNT(*) FROM pastes WHERE visible = '0' AND password = 'NONE' AND encrypt = '0' AND (title LIKE ? OR content LIKE ?)";
$count_stmt = $pdo->prepare($count_sql);
$count_stmt->execute([$search_term, $search_term]);
$total = (int)$count_stmt->fetchColumn();
echo json_encode([
'success' => true,
'query' => $query,
'results' => $results,
'pagination' => [
'page' => $page,
'limit' => $limit,
'total' => $total,
'pages' => (int)ceil($total / $limit)
]
]);
exit;
}
// Disable non-GET requests
if ($_SERVER['REQUEST_METHOD'] != 'GET') {
http_response_code(405);
exit('405 Method Not Allowed.');
}
$date = date('Y-m-d H:i:s'); // Use DATETIME format for database
$ip = $_SERVER['REMOTE_ADDR'];
// Database Connection
global $pdo;
try {
// Get site info
$stmt = $pdo->query("SELECT * FROM site_info WHERE id = '1'");
$row = $stmt->fetch();
$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
$stmt = $pdo->query("SELECT * FROM interface WHERE id = '1'");
$row = $stmt->fetch();
$default_lang = trim($row['lang']);
$default_theme = trim($row['theme']);
require_once("langs/$default_lang");
$p_title = $lang['archive'];
// Check if IP is banned
if (is_banned($pdo, $ip)) die($lang['banned']);
// Site permissions
$stmt = $pdo->query("SELECT * FROM site_permissions WHERE id = 1 LIMIT 1");
$row = $stmt->fetch();
$siteprivate = trim($row['siteprivate']);
$privatesite = ($siteprivate === '0' || $siteprivate === 0) ? '0' : '1';
// Logout
if (isset($_GET['logout'])) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
unset($_SESSION['token']);
unset($_SESSION['oauth_uid']);
unset($_SESSION['username']);
session_destroy();
}
// Page views
$date = date('Y-m-d');
$ip = $_SERVER['REMOTE_ADDR'];
try {
// Fetch or create the page_view record for today
$stmt = $pdo->prepare("SELECT id, tpage, tvisit FROM page_view WHERE date = ?");
$stmt->execute([$date]);
$row = $stmt->fetch();
if ($row) {
// Record exists for today
$page_view_id = $row['id'];
$tpage = (int)$row['tpage'] + 1; // Increment total page views
$tvisit = (int)$row['tvisit'];
// Check if this IP has visited today
$stmt = $pdo->prepare("SELECT COUNT(*) FROM visitor_ips WHERE ip = ? AND visit_date = ?");
$stmt->execute([$ip, $date]);
if ($stmt->fetchColumn() == 0) {
// New unique visitor
$tvisit += 1;
$stmt = $pdo->prepare("INSERT INTO visitor_ips (ip, visit_date) VALUES (?, ?)");
$stmt->execute([$ip, $date]);
}
// Update page_view with new counts
$stmt = $pdo->prepare("UPDATE page_view SET tpage = ?, tvisit = ? WHERE id = ?");
$stmt->execute([$tpage, $tvisit, $page_view_id]);
} else {
// No record for today: create one
$tpage = 1;
$tvisit = 1;
$stmt = $pdo->prepare("INSERT INTO page_view (date, tpage, tvisit) VALUES (?, ?, ?)");
$stmt->execute([$date, $tpage, $tvisit]);
// Log the visitor's IP
$stmt = $pdo->prepare("INSERT INTO visitor_ips (ip, visit_date) VALUES (?, ?)");
$stmt->execute([$ip, $date]);
}
} catch (PDOException $e) {
error_log("Page view tracking error: " . $e->getMessage());
}
// Ads
$stmt = $pdo->query("SELECT * FROM ads WHERE id = '1'");
$row = $stmt->fetch();
$text_ads = trim($row['text_ads']);
$ads_1 = trim($row['ads_1']);
$ads_2 = trim($row['ads_2']);
// Search, pagination, and sorting
$search_query = isset($_GET['q']) && !empty($_GET['q']) ? trim($_GET['q']) : '';
$sort = isset($_GET['sort']) && in_array($_GET['sort'], ['date_desc', 'date_asc', 'title_asc', 'title_desc', 'code_asc', 'code_desc', 'views_desc', 'views_asc']) ? $_GET['sort'] : 'date_desc';
$perPage = 50; // Increased to 50 pastes per page
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$offset = ($page - 1) * $perPage;
// Determine sort column and direction
$sortColumn = 'p.date';
$sortDirection = 'DESC';
switch ($sort) {
case 'date_asc':
$sortDirection = 'ASC';
break;
case 'title_asc':
$sortColumn = 'p.title';
$sortDirection = 'ASC';
break;
case 'title_desc':
$sortColumn = 'p.title';
$sortDirection = 'DESC';
break;
case 'code_asc':
$sortColumn = 'p.code';
$sortDirection = 'ASC';
break;
case 'code_desc':
$sortColumn = 'p.code';
$sortDirection = 'DESC';
break;
case 'views_desc':
$sortColumn = 'view_count';
$sortDirection = 'DESC';
break;
case 'views_asc':
$sortColumn = 'view_count';
$sortDirection = 'ASC';
break;
}
// Initialize variables
$pastes = [];
$totalItems = 0;
$totalPages = 1;
$error = '';
// Check if slug column exists (for backwards compatibility)
$hasSlug = columnExists($pdo, 'pastes', 'slug');
$slugCol = $hasSlug ? ', p.slug' : '';
$slugGroup = $hasSlug ? ', p.slug' : '';
// Base query with LEFT JOIN to paste_views
$baseQuery = "SELECT p.id{$slugCol}, p.title, p.code, p.date, UNIX_TIMESTAMP(p.date) AS now_time, p.encrypt, p.member, COUNT(pv.id) AS view_count
FROM pastes p
LEFT JOIN paste_views pv ON p.id = pv.paste_id
WHERE p.visible = '0' AND p.password = 'NONE'";
$countQuery = "SELECT COUNT(*)
FROM pastes p
WHERE p.visible = '0' AND p.password = 'NONE'";
$params = [];
if ($search_query && strlen($search_query) >= 3) { // Search query provided
$search_term = '%' . $search_query . '%';
$baseQuery .= " AND (p.title LIKE ? OR p.content LIKE ?)";
$countQuery .= " AND (p.title LIKE ? OR p.content LIKE ?)";
$params = [$search_term, $search_term];
}
// Add GROUP BY and ORDER BY
$baseQuery .= " GROUP BY p.id, p.title, p.code, p.date, p.encrypt, p.member{$slugGroup} ORDER BY $sortColumn $sortDirection LIMIT ? OFFSET ?";
$params[] = $perPage;
$params[] = $offset;
// Execute main query
try {
$stmt = $pdo->prepare($baseQuery);
$stmt->execute($params);
$pastes = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Count total matching pastes for pagination
$stmt = $pdo->prepare($countQuery);
$stmt->execute($search_query ? [$search_term, $search_term] : []);
$totalItems = $stmt->fetchColumn();
} catch (PDOException $e) {
error_log("Paste query error: " . $e->getMessage());
$pastes = [];
$totalItems = 0;
}
$totalPages = $totalItems > 0 ? ceil($totalItems / $perPage) : 1;
// Decrypt titles and format time
foreach ($pastes as &$row) {
if ($row['encrypt'] == '1') {
$row['title'] = decrypt($row['title'], hex2bin(SECRET)) ?? $row['title'];
}
// Ensure slug key exists even if column doesn't
if (!isset($row['slug'])) {
$row['slug'] = '';
}
// Use slug if available, otherwise fall back to numeric ID
$url_identifier = !empty($row['slug']) ? $row['slug'] : $row['id'];
$row['time_display'] = formatRealTime($row['date']);
$row['url'] = $mod_rewrite == '1' ? $baseurl . $url_identifier : $baseurl . 'paste.php?id=' . $url_identifier;
$row['title'] = truncate($row['title'], 20, 50);
$row['views'] = $row['view_count'];
}
unset($row);
if (isset($_GET['q']) && (empty($search_query) || strlen($search_query) < 3)) {
$error = "Please use a keyword to search. Here are the latest 50 pastes.";
}
// Pagination
$prev_page_query = http_build_query(array_merge($_GET, ['page' => $page > 1 ? $page - 1 : 1]));
$next_page_query = http_build_query(array_merge($_GET, ['page' => $page < $totalPages ? $page + 1 : $totalPages]));
$page_queries = [];
for ($i = 1; $i <= $totalPages; $i++) {
$page_queries[$i] = http_build_query(array_merge($_GET, ['page' => $i]));
}
// Set archives title
$archives_title = htmlspecialchars($lang['archives'] ?? 'Archives', ENT_QUOTES, 'UTF-8');
if ($search_query && !empty($search_query)) {
$archives_title .= ' - ' . htmlspecialchars($lang['search_results_for'] ?? 'Search Results for', ENT_QUOTES, 'UTF-8') . ' "' . htmlspecialchars($search_query, ENT_QUOTES, 'UTF-8') . '"';
}
// Theme
require_once('theme/' . $default_theme . '/header.php');
require_once('theme/' . $default_theme . '/archive.php');
require_once('theme/' . $default_theme . '/footer.php');
} catch (PDOException $e) {
die("Database error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'));
}
?>