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; } }