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
|
<?php /* Copyright (c) 2024 Freya Murphy */
class _comments_model extends Model {
private $profanity;
function __construct()
{
$this->profanity = $this->load_profanity();
}
private function load_profanity()
{
$path = ASSET_ROOT . '/profanity.txt';
$str = file_get_contents($path);
$lines = explode("\n", $str);
$regex = '/(';
foreach ($lines as $idx => $line) {
if ($line == '') {
continue;
}
if ($idx != 0) {
$regex .= '|';
}
$regex .= $line;
}
$regex .= ')/';
return $regex;
}
public function is_vulgar($text)
{
return preg_match($this->profanity, $text);
}
public function get_comments($page)
{
$ip = $this->get_ip();
$query = $this->db()
->select('*')
->from('website.comment c')
->where('c.page')
->eq($page)
->query('AND (
(c.vulgar IS FALSE) OR
(c.vulgar IS TRUE and c.ip = ?)
)')
->order_by('c.id', 'DESC');
$result = $query->rows($ip);
return $result;
}
public function post_comment($author, $content, $page, $vulgar)
{
$ip = $this->get_ip();
return $this->db()
->insert_into('website.comment',
'author', 'content', 'page', 'ip', 'vulgar')
->values($author, $content, $page, $ip, $vulgar)
->execute();
}
public function get_ip(): ?string
{
$headers = array (
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'HTTP_X_REAL_IP',
'REMOTE_ADDR'
);
foreach ($headers as $header) {
if (isset($_SERVER[$header]))
return $_SERVER[$header];
}
return NULL;
}
}
|