Repository navigation
Expand file tree
/
Copy pathcomments.php
More file actions
361 lines (293 loc) · 9.95 KB
/
Copy pathcomments.php
File metadata and controls
361 lines (293 loc) · 9.95 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
<?php
if (!defined('MOD_INDEX')) {
include 'defaults.php';
include INCLUDES_DIR . 'includes.php';
/* backward compatibility */
if (empty($_GET ['entry'])) {
@utils_redirect();
} else {
@utils_status_header(301);
@utils_redirect(str_replace('&', '&', get_comments_link($_GET ['entry'])), true);
}
}
$module = comment_main(isset($module) ? $module : null);
function comment_main($module) {
global $fpdb, $fp_params, $smarty, $fp_config, $current_entry;
// register Smarty modifier function
$smarty->registerPlugin('modifier', 'is_numeric', 'is_numeric');
$smarty->registerPlugin('modifier', 'theme_comments_feed_link', 'theme_comments_feed_link');
if (!isset($smarty->registered_plugins['modifier']['fix_encoding_issues'])) {
// This modifier converts characters such as ä to ä or “ to “. See core.language.php
$smarty->registerPlugin('modifier', 'fix_encoding_issues', 'fix_encoding_issues');
}
// hackish solution to get title before fullparse starts dunno, I don't like it
$q = & $fpdb->getQuery();
list ($id, $entry) = @$q->peekEntry();
if (empty($entry)) {
return $module;
}
$current_entry = $entry;
if (!empty($fp_params ['feed'])) {
switch ($fp_params ['feed']) {
case 'atom':
$charset = strtoupper($fp_config ['locale'] ['charset']);
header('Content-Type: application/atom+xml; charset=' . $charset);
$module = SHARED_TPLS . 'comment-atom.tpl';
break;
case 'rss2':
default:
$charset = strtoupper($fp_config ['locale'] ['charset']);
header('Content-Type: application/rss+xml; charset=' . $charset);
$module = SHARED_TPLS . 'comment-rss.tpl';
}
} elseif (!in_array('commslock', $entry ['categories'])) {
commentform();
}
return $module;
}
function comment_feed() {
global $fp_params, $fp_config, $lang, $current_entry;
if (isset($current_entry ['subject']) && !empty($current_entry ['subject'])) {
$entry_title = $current_entry ['subject'];
} elseif (isset($fp_config ['404error'] ['subject']) && !empty($fp_config ['404error'] ['subject'])) {
$entry_title = $fp_config ['404error'] ['subject'];
} else {
$entry_title = 'Undefined Title';
}
echo '
<!-- BOF Comments RSS and Atom Feed -->
<link rel="alternate" type="application/rss+xml" title="' . $entry_title . ' » ' . $lang ['main'] ['comments'] . ' | RSS 2.0" href="' . theme_comments_feed_link('rss2', $fp_params ['entry']) . '">
<link rel="alternate" type="application/atom+xml" title="' . $entry_title . ' » ' . $lang ['main'] ['comments'] . ' | Atom 1.0" href="' . theme_comments_feed_link('atom', $fp_params ['entry']) . '">
<!-- EOF Comments RSS and Atom Feed -->
';
}
add_action('wp_head', 'comment_feed');
function comment_pagetitle($val, $sep) {
global $fpdb, $lang;
$q = & $fpdb->getQuery();
list ($id, $e) = @$q->peekEntry();
if (!empty($e)) {
return $e ['subject'] . ' : ' . $lang ['main'] ['comments'] . ' ' . $sep . ' ' . $val . ' ';
} else {
return $val;
}
}
remove_filter('wp_title', 'index_permatitle');
add_filter('wp_title', 'comment_pagetitle', 10, 2);
function comment_validate() {
global $smarty, $lang;
$lerr = & $lang ['comments'] ['error'];
$r = true;
/**
* $lang['comments']['error'] = array(
* 'name' => 'You must enter a name',
* 'email' => 'You must enter a valid email',
* 'www' => 'You must enter a valid URL',
* 'comment' => 'You must enter a comment',
* );
*/
$content = isset($_POST ['content']) ? trim(sanitize_comment($_POST ['content'], FILTER_UNSAFE_RAW)) : '';
$errors = array();
$loggedin = false;
if (user_loggedin()) {
$user = user_get();
$loggedin = $arr ['loggedin'] = true;
$email = $user ['email'] ?? '';
$url = $user ['www'] ?? '';
$name = $user ['userid'] ?? '';
} else {
$name = strip_tags(sanitize_comment($_POST ['name'] ?? ''));
$email = strip_tags(sanitize_comment($_POST ['email'] ?? '', FILTER_VALIDATE_EMAIL));
$url = strip_tags(sanitize_comment(ensure_https_url($_POST ['url'] ?? ''), FILTER_SANITIZE_URL));
// Check name
if (empty($name)) {
$errors ['name'] = $lerr ['name'];
}
// Check email
if (!empty($_POST ['email']) && !$email) {
$errors ['email'] = $lerr ['email'];
}
// Check url
if (!empty($_POST ['url']) && !$url) {
$errors ['url'] = $lerr ['www'];
}
}
// Check content
if (empty($content)) {
$errors ['content'] = $lerr ['comment'];
}
// Assign error messages to template
if (!empty($errors)) {
$smarty->assign('error', $errors);
return false;
}
$arr ['version'] = system_ver();
$arr ['name'] = $name;
if (!empty($email)) {
($arr ['email'] = $email);
}
if (!empty($url)) {
($arr ['url'] = ($url));
}
$arr ['content'] = $content;
if ($v = utils_ipget()) {
$arr ['ip-address'] = $v;
}
if ($loggedin || apply_filters('comment_validate', true, $arr)) {
return $arr;
} else {
return false;
}
}
function sanitize_comment($data, $filter = FILTER_UNSAFE_RAW) {
global $fp_config;
$charset = strtoupper($fp_config ['locale'] ['charset'] ?? 'UTF-8');
$data = trim($data);
$data = filter_var($data, $filter) ? : '';
// Removes dangerous characters for flat file manipulation and potential code injections
$unsafe_patterns = [
"<?", "?>", ";", "eval", "base64_decode", "base64_encode",
"system", "exec", "shell_exec", "proc_open", "passthru", "popen",
"curl_exec", "curl_multi_exec", "fopen", "fwrite", "file_put_contents"
];
$data = str_replace($unsafe_patterns, "", $data);
// Block potential SQL injection patterns
$sql_patterns = [
'/\bSELECT\b/i', '/\bINSERT\b/i', '/\bUPDATE\b/i', '/\bDELETE\b/i', '/\bDROP\b/i',
'/\bUNION\b/i', '/\bALTER\b/i', '/\bEXEC\b/i', '/\bOR\b\s*\d+=\d+/i', '/\bAND\b\s*\d+=\d+/i',
'/--/', '/;/', '/\bNULL\b/i', '/\bTRUE\b/i', '/\bFALSE\b/i', '/\bINFORMATION_SCHEMA\b/i',
'/\bTABLE_SCHEMA\b/i', '/\bCONCAT\b/i', '/\bSLEEP\b/i', '/\bBENCHMARK\b/i'
];
$data = preg_replace($sql_patterns, '', $data) ? : '';
// Check for potentially dangerous flat file characters
if (preg_match('/\|{2,}|::{2,}|;;/', $data)) {
return '';
}
return htmlspecialchars($data, ENT_NOQUOTES, $charset);
}
// Add https to url if not given
function ensure_https_url($url) {
$url = trim(stripslashes((string)$url));
if ($url === '') {
return '';
}
if (!preg_match('/^https?:\/\//i', $url)) {
$url = 'https://' . $url;
}
if (preg_match('/[\x00-\x20\x7F<>"\'`\\\\]/', $url)) {
return '';
}
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return '';
}
$parts = parse_url($url);
if ($parts === false || empty($parts ['scheme']) || empty($parts ['host'])) {
return '';
}
return preg_match('/^https?$/i', $parts ['scheme']) ? $url : '';
}
function commentform() {
global $smarty, $lang, $fpdb, $fp_params;
// Ensure session is started
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (empty($_SESSION ['csrf_token'])) {
// Generate CSRF token
$_SESSION ['csrf_token'] = bin2hex(random_bytes(32));
}
// Check CSRF protection
$request_method = $_SERVER ['REQUEST_METHOD'] ?? '';
$is_post_request = ($request_method === 'POST');
if (!$is_post_request && isset($_SERVER ['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
$override_method = strtoupper(trim($_SERVER ['HTTP_X_HTTP_METHOD_OVERRIDE']));
if ($override_method === 'POST') {
$is_post_request = true;
}
}
if ($is_post_request) {
if (!isset($_POST ['csrf_token']) || $_POST ['csrf_token'] !== ($_SESSION ['csrf_token'] ?? '')) {
// Renew CSRF token to prevent replay attacks
unset($_SESSION ['csrf_token']);
$_SESSION ['csrf_token'] = bin2hex(random_bytes(32));
utils_redirect('comments.php');
exit();
}
unset($_SESSION ['csrf_token']);
$_SESSION ['csrf_token'] = bin2hex(random_bytes(32));
}
$comment_formid = 'fp-comments';
$smarty->assign('comment_formid', $comment_formid);
// Transfer token to Smarty
$smarty->assign('csrf_token', $_SESSION ['csrf_token']);
// Define panel strings for success or error message
$panelstrings = array(
'msgs' => array(
1 => $lang ['comments'] ['success'],
-1 => $lang ['comments'] ['error'],
),
);
$smarty->assign('panelstrings', $panelstrings);
if (!empty($_POST)) {
// New form, we (re)set the session data
utils_nocache_headers();
// Custom hook here!!
if ($arr = comment_validate()) {
global $fp_config;
$id = comment_save($fp_params ['entry'], $arr);
do_action('comment_post', $fp_params ['entry'], array(
$id,
$arr
));
$q = new FPDB_Query(array(
'id' => $fp_params ['entry'],
'fullparse' => false
), null);
list ($entryid, $e) = $q->getEntry();
if ($fp_config ['general'] ['notify'] && !user_loggedin()) {
global $post;
$comm_mail = isset($arr ['email']) ? '<' . $arr ['email'] . '>' : '';
$from_mail = $fp_config ['general'] ['email'];
// Plugin such as prettyurls might need this...
$post = $e;
$lang = lang_load('comments');
$mail = str_replace(array(
'%toname%',
'%fromname%',
'%frommail%',
'%entrytitle%',
'%commentlink%',
'%content%',
'%blogtitle%'
), array(
$fp_config ['general'] ['author'],
$arr ['name'],
$comm_mail,
$e ['subject'],
get_comments_link($entryid) . '#' . $id,
$arr ['content'],
$fp_config ['general'] ['title']
), $lang ['comments'] ['mail']);
// For non-ASCII characters in the e-mail header use RFC 1342 — Encodes $subject with MIME base64 via core.utils.php
@utils_mail($from_mail, $lang ['comments'] ['newcomment'] . ' ' . $fp_config ['general'] ['title'], $mail);
}
// Set success message for the current request
$smarty->assign('success', 1);
// Save success status in session for use after redirect
system_seterr('comment', 1);
// If comment is valid, this redirect will clean the postdata
$location = str_replace('&', '&', get_comments_link($entryid)) . '#' . $id;
utils_redirect($location, true);
exit();
} else {
$smarty->assign('values', $_POST);
}
} else {
// Check for any previous success or error state
$success = system_geterr('comment');
if ($success === 1) {
$smarty->assign('success', 1);
}
}
}
?>