summaryrefslogtreecommitdiff
path: root/inc/mod
diff options
context:
space:
mode:
authorczaks <marcin@6irc.net>2013-07-20 17:28:34 -0400
committerczaks <marcin@6irc.net>2013-07-20 17:28:34 -0400
commit9f323ae65fb9370a93d1aaa869ab5edfbaca49cf (patch)
tree926a464440df8a37509ae54165edefac4a89d874 /inc/mod
parentf04566f6d30553560d5a69e25e38de72a60f297c (diff)
parente8b693b0447e5a61f740b27c5f3a57f68d544342 (diff)
Merge branch 'master' of https://github.com/savetheinternet/Tinyboard
Conflicts: templates/themes/sitemap/info.php
Diffstat (limited to 'inc/mod')
-rw-r--r--inc/mod/pages.php134
1 files changed, 134 insertions, 0 deletions
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;