diff options
Diffstat (limited to 'inc')
| -rw-r--r-- | inc/config.php | 6 | ||||
| -rw-r--r-- | inc/display.php | 29 | ||||
| -rw-r--r-- | inc/mod/pages.php | 134 |
3 files changed, 166 insertions, 3 deletions
diff --git a/inc/config.php b/inc/config.php index 1140b1c9..bbc899ef 100644 --- a/inc/config.php +++ b/inc/config.php @@ -899,8 +899,8 @@ // Number of news entries to display per page $config['mod']['news_page'] = 40; - // Maximum number of results to display for a search, per board - $config['mod']['search_results'] = 75; + // Number of results to dispaly per page + $config['mod']['search_page'] = 200; // How many entries to show per page in the moderator noticeboard $config['mod']['noticeboard_page'] = 50; @@ -1057,7 +1057,7 @@ $config['mod']['master_pm'] = ADMIN; // Rebuild everything $config['mod']['rebuild'] = ADMIN; - // Search through posts + // Search through posts, IP address notes and bans $config['mod']['search'] = JANITOR; // Read the moderator noticeboard $config['mod']['noticeboard'] = JANITOR; diff --git a/inc/display.php b/inc/display.php index ba55903d..fe8bb048 100644 --- a/inc/display.php +++ b/inc/display.php @@ -241,6 +241,29 @@ function secure_link($href) { return $href . '/' . make_secure_link_token($href); } +function embed_html($link) { + global $config; + + foreach ($config['embedding'] as $embed) { + if ($html = preg_replace($embed[0], $embed[1], $link)) { + if ($html == $link) + continue; // Nope + + $html = str_replace('%%tb_width%%', $config['embed_width'], $html); + $html = str_replace('%%tb_height%%', $config['embed_height'], $html); + + return $html; + } + } + + if ($link[0] == '<') { + // Prior to v0.9.6-dev-8, HTML code for embedding was stored in the database instead of the link. + return $link; + } + + return 'Embedding error.'; +} + class Post { public function __construct($id, $thread, $subject, $email, $name, $trip, $capcode, $body, $time, $thumb, $thumbx, $thumby, $file, $filex, $filey, $filesize, $filename, $ip, $embed, $root=null, $mod=false) { global $config; @@ -269,6 +292,9 @@ class Post { $this->root = $root; $this->mod = $mod; + if ($this->embed) + $this->embed = embed_html($this->embed); + if ($this->mod) // Fix internal links // Very complicated regex @@ -365,6 +391,9 @@ class Thread { $this->mod = $mod; $this->hr = $hr; + if ($this->embed) + $this->embed = embed_html($this->embed); + if ($this->mod) // Fix internal links // Very complicated regex diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 0e261357..e1e8c88f 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -162,6 +162,140 @@ function mod_dashboard() { mod_page(_('Dashboard'), 'mod/dashboard.html', $args); } +function mod_search_redirect() { + global $config; + + if (!hasPermission($config['mod']['search'])) + error($config['error']['noaccess']); + + if (isset($_POST['query'], $_POST['type']) && in_array($_POST['type'], array('posts', 'IP_notes', 'bans', 'log'))) { + $query = $_POST['query']; + $query = urlencode($query); + $query = str_replace('_', '%5F', $query); + $query = str_replace('+', '_', $query); + + header('Location: ?/search/' . $_POST['type'] . '/' . $query, true, $config['redirect_http']); + } else { + header('Location: ?/', true, $config['redirect_http']); + } +} + +function mod_search($type, $search_query_escaped, $page_no = 1) { + global $pdo, $config; + + if (!hasPermission($config['mod']['search'])) + error($config['error']['noaccess']); + + // Unescape query + $query = str_replace('_', ' ', $search_query_escaped); + $query = urldecode($query); + $search_query = $query; + + // Form a series of LIKE clauses for the query. + // This gets a little complicated. + + // Escape "escape" character + $query = str_replace('!', '!!', $query); + + // Escape SQL wildcard + $query = str_replace('%', '!%', $query); + + // Use asterisk as wildcard instead + $query = str_replace('*', '%', $query); + + // Array of phrases to match + $match = array(); + + // Exact phrases ("like this") + if (preg_match_all('/"(.+?)"/', $query, $exact_phrases)) { + $exact_phrases = $exact_phrases[1]; + foreach ($exact_phrases as $phrase) { + $query = str_replace("\"{$phrase}\"", '', $query); + $match[] = $pdo->quote($phrase); + } + } + + // Non-exact phrases (ie. plain keywords) + $keywords = explode(' ', $query); + foreach ($keywords as $word) { + if (empty($word)) + continue; + $match[] = $pdo->quote($word); + } + + // Which `field` to search? + if ($type == 'posts') + $sql_field = 'body'; + if ($type == 'IP_notes') + $sql_field = 'body'; + if ($type == 'bans') + $sql_field = 'reason'; + if ($type == 'log') + $sql_field = 'text'; + + // Build the "LIKE 'this' AND LIKE 'that'" etc. part of the SQL query + $sql_like = ''; + foreach ($match as $phrase) { + if (!empty($sql_like)) + $sql_like .= ' AND '; + $phrase = preg_replace('/^\'(.+)\'$/', '\'%$1%\'', $phrase); + $sql_like .= '`' . $sql_field . '` LIKE ' . $phrase . ' ESCAPE \'!\''; + } + + + // Compile SQL query + + if ($type == 'posts') { + error('Searching posts is under development. Sorry.'); + } + + if ($type == 'IP_notes') { + $query = 'SELECT * FROM `ip_notes` LEFT JOIN `mods` ON `mod` = `mods`.`id` WHERE ' . $sql_like . ' ORDER BY `time` DESC'; + $sql_table = 'ip_notes'; + if (!hasPermission($config['mod']['view_notes']) || !hasPermission($config['mod']['show_ip'])) + error($config['error']['noaccess']); + } + + if ($type == 'bans') { + $query = 'SELECT `bans`.*, `username` FROM `bans` LEFT JOIN `mods` ON `mod` = `mods`.`id` WHERE ' . $sql_like . ' ORDER BY (`expires` IS NOT NULL AND `expires` < UNIX_TIMESTAMP()), `set` DESC'; + $sql_table = 'bans'; + if (!hasPermission($config['mod']['view_banlist'])) + error($config['error']['noaccess']); + } + + if ($type == 'log') { + $query = 'SELECT `username`, `mod`, `ip`, `board`, `time`, `text` FROM `modlogs` LEFT JOIN `mods` ON `mod` = `mods`.`id` WHERE ' . $sql_like . ' ORDER BY `time` DESC'; + $sql_table = 'modlogs'; + if (!hasPermission($config['mod']['modlog'])) + error($config['error']['noaccess']); + } + + // Execute SQL query (with pages) + $q = query($query . ' LIMIT ' . (($page_no - 1) * $config['mod']['search_page']) . ', ' . $config['mod']['search_page']) or error(db_error()); + $results = $q->fetchAll(PDO::FETCH_ASSOC); + + // Get total result count + $q = query('SELECT COUNT(*) FROM `' . $sql_table . '` WHERE ' . $sql_like) or error(db_error()); + $result_count = $q->fetchColumn(); + + if ($type == 'bans') { + foreach ($results as &$ban) { + if (filter_var($ban['ip'], FILTER_VALIDATE_IP) !== false) + $ban['real_ip'] = true; + } + } + + // $results now contains the search results + + mod_page(_('Search results'), 'mod/search_results.html', array( + 'search_type' => $type, + 'search_query' => $search_query, + 'search_query_escaped' => $search_query_escaped, + 'result_count' => $result_count, + 'results' => $results + )); +} + function mod_edit_board($boardName) { global $board, $config; |
