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; diff --git a/js/quote-selection.js b/js/quote-selection.js index 0c5b7e7d..0722c0b1 100644 --- a/js/quote-selection.js +++ b/js/quote-selection.js @@ -1,6 +1,9 @@ /* * quote-selection.js * + * This is a little buggy. + * Allows you to quote a post by just selecting some text, then beginning to type. + * * Released under the MIT license * Copyright (c) 2012 Michael Save * @@ -31,12 +34,15 @@ $(document).ready(function(){ var altKey = false; var ctrlKey = false; + var metaKey = false; $(document).keyup(function(e) { - if (e.altKey) + if (e.keyCode == 18) altKey = false; - else if (e.ctrlKey) + else if (e.keyCode == 17) ctrlKey = false; + else if (e.keyCode == 91) + metaKey = false; }); $(document).keydown(function(e) { @@ -44,9 +50,11 @@ $(document).ready(function(){ altKey = true; else if (e.ctrlKey) ctrlKey = true; + else if (e.metaKey) + metaKey = true; - if (altKey || ctrlKey) { - console.log('CTRL/ALT used. Ignoring'); + if (altKey || ctrlKey || metaKey) { + // console.log('CTRL/ALT/Something used. Ignoring'); return; } @@ -56,20 +64,20 @@ $(document).ready(function(){ var selection = window.getSelection(); var $post = $(selection.anchorNode).parents('.post'); if ($post.length == 0) { - console.log('Start of selection was not post div', $(selection.anchorNode).parent()); + // console.log('Start of selection was not post div', $(selection.anchorNode).parent()); return; } var postID = $post.find('.post_no:eq(1)').text(); if (postID != $(selection.focusNode).parents('.post').find('.post_no:eq(1)').text()) { - console.log('Selection left post div', $(selection.focusNode).parent()); + // console.log('Selection left post div', $(selection.focusNode).parent()); return; } ; var selectedText = selection.toString(); - console.log('Selected text: ' + selectedText.replace(/\n/g, '\\n').replace(/\r/g, '\\r')); + // console.log('Selected text: ' + selectedText.replace(/\n/g, '\\n').replace(/\r/g, '\\r')); if ($('body').hasClass('debug')) alert(selectedText); @@ -86,7 +94,7 @@ $(document).ready(function(){ /* to solve some bugs on weird browsers, we need to replace \r\n with \n and then undo that after */ var quote = (last_quote != postID ? '>>' + postID + '\r\n' : '') + $.trim(selectedText).replace(/\r\n/g, '\n').replace(/^/mg, '>').replace(/\n/g, '\r\n') + '\r\n'; - console.log('Deselecting text'); + // console.log('Deselecting text'); selection.removeAllRanges(); if (document.selection) { diff --git a/js/youtube.js b/js/youtube.js new file mode 100644 index 00000000..482a9bc8 --- /dev/null +++ b/js/youtube.js @@ -0,0 +1,34 @@ +/* +* youtube +* https://github.com/savetheinternet/Tinyboard/blob/master/js/youtube.js +* +* Don't load the YouTube player unless the video image is clicked. +* This increases performance issues when many videos are embedded on the same page. +* Currently only compatiable with YouTube. +* +* Proof of concept. +* +* Released under the MIT license +* Copyright (c) 2013 Michael Save +* +* Usage: +* $config['embedding'] = array(); +* $config['embedding'][] = array( +* '/^https?:\/\/(\w+\.)?youtube\.com\/watch\?v=([a-zA-Z0-9\-_]{10,11})(&.+)?$/i', +* '
' +); +* $config['additional_javascript'][] = 'js/jquery.min.js'; +* $config['additional_javascript'][] = 'js/youtube.js'; +* +*/ + + +onready(function(){ + $('div.video-container a').attr('href', 'javascript:void(0)'); + $('div.video-container').click(function() { + var videoID = $(this).data('video'); + + $(this).html('