From 8647d3021eaddfb4d2d1b7f08307744107e0de08 Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Fri, 30 Aug 2013 08:38:14 +1000 Subject: [PATCH 01/13] Bugfix: pm_unreadcount cache not working correctly --- inc/mod/pages.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index e782b09b..043b9a5c 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -92,7 +92,7 @@ function mod_dashboard() { } } - if (!$config['cache']['enabled'] || ($args['unread_pms'] = cache::get('pm_unreadcount_' . $mod['id'])) == false) { + if (!$config['cache']['enabled'] || ($args['unread_pms'] = cache::get('pm_unreadcount_' . $mod['id'])) === false) { $query = prepare('SELECT COUNT(*) FROM ``pms`` WHERE `to` = :id AND `unread` = 1'); $query->bindValue(':id', $mod['id']); $query->execute() or error(db_error($query)); From 29780f8e53c38f5e74549c5bb7e5b25e47493544 Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Fri, 30 Aug 2013 08:39:11 +1000 Subject: [PATCH 02/13] More $config['debug'] improvements to time-related stuff --- inc/cache.php | 2 +- inc/database.php | 21 +++++++++++++++------ inc/functions.php | 32 ++++++++++++++++++++++---------- inc/template.php | 6 ++++-- mod.php | 6 +++++- 5 files changed, 47 insertions(+), 20 deletions(-) diff --git a/inc/cache.php b/inc/cache.php index fd89be13..2f3c746c 100644 --- a/inc/cache.php +++ b/inc/cache.php @@ -66,7 +66,7 @@ class Cache { return $data; } public static function set($key, $value, $expires = false) { - global $config; + global $config, $debug; $key = $config['cache']['prefix'] . $key; diff --git a/inc/database.php b/inc/database.php index 8daa20b4..55e1ff81 100644 --- a/inc/database.php +++ b/inc/database.php @@ -28,13 +28,13 @@ class PreparedQueryDebug { $return = call_user_func_array(array($this->query, $function), $args); if ($config['debug'] && $function == 'execute') { - $time = round((microtime(true) - $start) * 1000, 2) . 'ms'; - + $time = microtime(true) - $start; $debug['sql'][] = array( 'query' => $this->query->queryString, 'rows' => $this->query->rowCount(), - 'time' => '~' . $time + 'time' => '~' . round($time * 1000, 2) . 'ms' ); + $debug['time']['db_queries'] += $time; } return $return; @@ -42,10 +42,14 @@ class PreparedQueryDebug { } function sql_open() { - global $pdo, $config; + global $pdo, $config, $debug; if ($pdo) return true; + + if ($config['debug']) + $start = microtime(true); + if (isset($config['db']['server'][0]) && $config['db']['server'][0] == ':') $unix_socket = substr($config['db']['server'], 1); else @@ -64,6 +68,10 @@ function sql_open() { if ($config['db']['persistent']) $options[PDO::ATTR_PERSISTENT] = true; $pdo = new PDO($dsn, $config['db']['user'], $config['db']['password'], $options); + + if ($config['debug']) + $debug['time']['db_connect'] = '~' . round((microtime(true) - $start) * 1000, 2) . 'ms'; + if (mysql_version() >= 50503) query('SET NAMES utf8mb4') or error(db_error()); else @@ -117,12 +125,13 @@ function query($query) { $query = $pdo->query($query); if (!$query) return false; - $time = round((microtime(true) - $start) * 1000, 2) . 'ms'; + $time = microtime(true) - $start; $debug['sql'][] = array( 'query' => $query->queryString, 'rows' => $query->rowCount(), - 'time' => '~' . $time + 'time' => '~' . round($time * 1000, 2) . 'ms' ); + $debug['time']['db_queries'] += $time; return $query; } diff --git a/inc/functions.php b/inc/functions.php index e634b87e..6c18ce3e 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -81,14 +81,6 @@ function loadConfig() { $__version = file_exists('.installed') ? trim(file_get_contents('.installed')) : false; $config['version'] = $__version; - if ($config['debug']) { - if (!isset($debug)) { - $debug = array('sql' => array(), 'exec' => array(), 'purge' => array(), 'cached' => array(), 'write' => array()); - $debug['start'] = $microtime_start; - $debug['start_debug'] = microtime(true);; - } - } - date_default_timezone_set($config['timezone']); if (!isset($config['global_message'])) @@ -199,6 +191,25 @@ function loadConfig() { if ($config['cache']['enabled']) require_once 'inc/cache.php'; event('load-config'); + + if ($config['debug']) { + if (!isset($debug)) { + $debug = array( + 'sql' => array(), + 'exec' => array(), + 'purge' => array(), + 'cached' => array(), + 'write' => array(), + 'time' => array( + 'db_queries' => 0, + 'exec' => 0, + ), + 'start' => $microtime_start, + 'start_debug' => microtime(true) + ); + $debug['start'] = $microtime_start; + } + } } function basic_error_function_because_the_other_isnt_loaded_yet($message, $priority = true) { @@ -2056,12 +2067,13 @@ function shell_exec_error($command, $suppress_stdout = false) { $return = preg_replace('/TB_SUCCESS$/', '', $return); if ($config['debug']) { - $time = round((microtime(true) - $start) * 1000, 2) . 'ms'; + $time = microtime(true) - $start; $debug['exec'][] = array( 'command' => $command, - 'time' => '~' . $time, + 'time' => '~' . round($time * 1000, 2) . 'ms', 'response' => $return ? $return : null ); + $debug['time']['exec'] += $time; } return $return === 'TB_SUCCESS' ? false : $return; diff --git a/inc/template.php b/inc/template.php index 6d417631..d9e1c386 100644 --- a/inc/template.php +++ b/inc/template.php @@ -46,8 +46,8 @@ function Element($templateFile, array $options) { if (isset($options['body']) && $config['debug']) { if (isset($debug['start'])) { - $debug['time'] = '~' . round((microtime(true) - $debug['start']) * 1000, 2) . 'ms'; - $debug['time (initialization)'] = '~' . round(($debug['start_debug'] - $debug['start']) * 1000, 2) . 'ms'; + $debug['time']['total'] = '~' . round((microtime(true) - $debug['start']) * 1000, 2) . 'ms'; + $debug['time']['init'] = '~' . round(($debug['start_debug'] - $debug['start']) * 1000, 2) . 'ms'; unset($debug['start']); unset($debug['start_debug']); } @@ -55,6 +55,8 @@ function Element($templateFile, array $options) { $debug['build_pages'] = $build_pages; $debug['included'] = get_included_files(); $debug['memory'] = round(memory_get_usage(true) / (1024 * 1024), 2) . ' MiB'; + $debug['time']['db_queries'] = '~' . round($debug['time']['db_queries'] * 1000, 2) . 'ms'; + $debug['time']['exec'] = '~' . round($debug['time']['exec'] * 1000, 2) . 'ms'; $options['body'] .= '

Debug

' .
 				str_replace("\n", '
', utf8tohtml(print_r($debug, true))) . diff --git a/mod.php b/mod.php index 0e98c4eb..6ef4644d 100644 --- a/mod.php +++ b/mod.php @@ -8,6 +8,9 @@ require 'inc/functions.php'; require 'inc/mod/pages.php'; require 'inc/mod/auth.php'; +if ($config['debug']) + $parse_start_time = microtime(true); + // Fix for magic quotes if (get_magic_quotes_gpc()) { function strip_array($var) { @@ -156,8 +159,9 @@ foreach ($pages as $uri => $handler) { $debug['mod_page'] = array( 'req' => $query, 'match' => $uri, - 'handler' => $handler + 'handler' => $handler, ); + $debug['time']['parse_mod_req'] = '~' . round((microtime(true) - $parse_start_time) * 1000, 2) . 'ms'; } if (is_string($handler)) { From c3e528378a9314fd5d4a7575c31f8fda50c998d7 Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Fri, 30 Aug 2013 09:00:47 +1000 Subject: [PATCH 03/13] Rendering $debug: Copy to $_debug so that $debug doesn't become broken if we want to generate more than one file in a request --- inc/template.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/inc/template.php b/inc/template.php index d9e1c386..378f36d0 100644 --- a/inc/template.php +++ b/inc/template.php @@ -45,21 +45,23 @@ function Element($templateFile, array $options) { } if (isset($options['body']) && $config['debug']) { + $_debug = $debug; + if (isset($debug['start'])) { - $debug['time']['total'] = '~' . round((microtime(true) - $debug['start']) * 1000, 2) . 'ms'; - $debug['time']['init'] = '~' . round(($debug['start_debug'] - $debug['start']) * 1000, 2) . 'ms'; - unset($debug['start']); - unset($debug['start_debug']); + $_debug['time']['total'] = '~' . round((microtime(true) - $_debug['start']) * 1000, 2) . 'ms'; + $_debug['time']['init'] = '~' . round(($_debug['start_debug'] - $_debug['start']) * 1000, 2) . 'ms'; + unset($_debug['start']); + unset($_debug['start_debug']); } if ($config['try_smarter'] && isset($build_pages) && !empty($build_pages)) - $debug['build_pages'] = $build_pages; - $debug['included'] = get_included_files(); - $debug['memory'] = round(memory_get_usage(true) / (1024 * 1024), 2) . ' MiB'; - $debug['time']['db_queries'] = '~' . round($debug['time']['db_queries'] * 1000, 2) . 'ms'; - $debug['time']['exec'] = '~' . round($debug['time']['exec'] * 1000, 2) . 'ms'; + $_debug['build_pages'] = $build_pages; + $_debug['included'] = get_included_files(); + $_debug['memory'] = round(memory_get_usage(true) / (1024 * 1024), 2) . ' MiB'; + $_debug['time']['db_queries'] = '~' . round($_debug['time']['db_queries'] * 1000, 2) . 'ms'; + $_debug['time']['exec'] = '~' . round($_debug['time']['exec'] * 1000, 2) . 'ms'; $options['body'] .= '

Debug

' .
-				str_replace("\n", '
', utf8tohtml(print_r($debug, true))) . + str_replace("\n", '
', utf8tohtml(print_r($_debug, true))) . '
'; } From be21b11455a77cdb29a4ecba19c0717acd67686c Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Fri, 30 Aug 2013 15:00:33 +1000 Subject: [PATCH 04/13] More track_cites work --- inc/functions.php | 32 ++++++++++++++++---------------- inc/mod/pages.php | 17 +++++++++-------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 6c18ce3e..ccf21c9e 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -168,21 +168,21 @@ function loadConfig() { if (preg_match('/^\:\:(ffff\:)?(\d+\.\d+\.\d+\.\d+)$/', $__ip, $m)) $_SERVER['REMOTE_ADDR'] = $m[2]; - if (_setlocale(LC_ALL, $config['locale']) === false) { - $error('The specified locale (' . $config['locale'] . ') does not exist on your platform!'); + if ($config['locale'] != 'en') { + if (_setlocale(LC_ALL, $config['locale']) === false) { + $error('The specified locale (' . $config['locale'] . ') does not exist on your platform!'); + } + if (extension_loaded('gettext')) { + bindtextdomain('tinyboard', './inc/locale'); + bind_textdomain_codeset('tinyboard', 'UTF-8'); + textdomain('tinyboard'); + } else { + _bindtextdomain('tinyboard', './inc/locale'); + _bind_textdomain_codeset('tinyboard', 'UTF-8'); + _textdomain('tinyboard'); + } } - if (extension_loaded('gettext')) { - bindtextdomain('tinyboard', './inc/locale'); - bind_textdomain_codeset('tinyboard', 'UTF-8'); - textdomain('tinyboard'); - } else { - _bindtextdomain('tinyboard', './inc/locale'); - _bind_textdomain_codeset('tinyboard', 'UTF-8'); - _textdomain('tinyboard'); - } - - if ($config['syslog']) openlog('tinyboard', LOG_ODELAY, LOG_SYSLOG); // open a connection to sysem logger @@ -996,7 +996,7 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); - $query = prepare("SELECT `board`, `post` FROM ``cites`` WHERE `target_board` = :board AND (`target` = " . implode(' OR `target` = ', $ids) . ")"); + $query = prepare("SELECT `board`, `post` FROM ``cites`` WHERE `target_board` = :board AND (`target` = " . implode(' OR `target` = ', $ids) . ") ORDER BY `board`"); $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); while ($cite = $query->fetch(PDO::FETCH_ASSOC)) { @@ -1011,10 +1011,10 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { if (isset($tmp_board)) openBoard($tmp_board); - $query = prepare("DELETE FROM ``cites`` WHERE (`target_board` = :board AND `target` = (" . implode(' OR `target` = ', $ids) . ")) OR (`board` = :board AND (`post` = " . implode(' OR `post` = ', $ids) . "))"); + $query = prepare("DELETE FROM ``cites`` WHERE (`target_board` = :board AND (`target` = " . implode(' OR `target` = ', $ids) . ")) OR (`board` = :board AND (`post` = " . implode(' OR `post` = ', $ids) . "))"); $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); - + if (isset($rebuild) && $rebuild_after) { buildThread($rebuild); } diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 043b9a5c..afd8daf3 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -1001,7 +1001,7 @@ function mod_bumplock($board, $unbumplock, $post) { } function mod_move($originBoard, $postID) { - global $board, $config, $mod; + global $board, $config, $mod, $pdo; if (!openBoard($originBoard)) error($config['error']['noboard']); @@ -1118,13 +1118,14 @@ function mod_move($originBoard, $postID) { $clone($post['file_thumb'], sprintf($config['board_path'], $board['uri']) . $config['dir']['thumb'] . $post['thumb']); } - foreach ($post['tracked_cites'] as $cite) { - $query = prepare('INSERT INTO ``cites`` VALUES (:board, :post, :target_board, :target)'); - $query->bindValue(':board', $board['uri']); - $query->bindValue(':post', $newPostID, PDO::PARAM_INT); - $query->bindValue(':target_board',$cite[0]); - $query->bindValue(':target', $cite[1], PDO::PARAM_INT); - $query->execute() or error(db_error($query)); + if (!empty($post['tracked_cites'])) { + $insert_rows = array(); + foreach ($post['tracked_cites'] as $cite) { + $insert_rows[] = '(' . + $pdo->quote($board['uri']) . ', ' . $newPostID . ', ' . + $pdo->quote($cite[0]) . ', ' . (int)$cite[1] . ')'; + } + query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error());; } } From cdeccd6816b64eb10b715a6809212a769c649701 Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Fri, 30 Aug 2013 15:05:13 +1000 Subject: [PATCH 05/13] Update comments for persistent database connections and increase timeout --- inc/config.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/config.php b/inc/config.php index 02cdbabf..e8b65967 100644 --- a/inc/config.php +++ b/inc/config.php @@ -88,12 +88,12 @@ $config['db']['database'] = ''; // Table prefix (optional) $config['db']['prefix'] = ''; - // Use a persistent connection (experimental; benefits unknown) + // Use a persistent database connection when possible $config['db']['persistent'] = false; // Anything more to add to the DSN string (eg. port=xxx;foo=bar) $config['db']['dsn'] = ''; - // Timeout duration in seconds (not all drivers support this) - $config['db']['timeout'] = 5; + // Connection timeout duration in seconds + $config['db']['timeout'] = 30; /* * ==================== From f131b2396f8f45926b13411f2f1b5dec8acb7336 Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sat, 31 Aug 2013 13:23:29 +1000 Subject: [PATCH 06/13] Fix \t in posts (editing posts, and HTML Tidy) --- inc/functions.php | 8 ++++---- inc/mod/pages.php | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index ccf21c9e..b9d135cf 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -1580,9 +1580,6 @@ function markup(&$body, $track_cites = false) { } } - // replace tabs with 8 spaces - $body = str_replace("\t", ' ', $body); - $tracked_cites = array(); // Cites @@ -1748,6 +1745,7 @@ function markup(&$body, $track_cites = false) { if ($config['markup_repair_tidy']) { $tidy = new tidy(); + $body = str_replace("\t", ' ', $body); $body = $tidy->repairString($body, array( 'doctype' => 'omit', 'bare' => true, @@ -1759,10 +1757,12 @@ function markup(&$body, $track_cites = false) { 'output-html' => true, 'newline' => 'LF', 'quiet' => true, - ), 'utf8'); $body = str_replace("\n", '', $body); } + + // replace tabs with 8 spaces + $body = str_replace("\t", ' ', $body); return $tracked_cites; } diff --git a/inc/mod/pages.php b/inc/mod/pages.php index afd8daf3..202ea182 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -1317,6 +1317,8 @@ function mod_edit_post($board, $edit_raw_html, $postID) { $post['body'] = str_replace("\n", ' ', utf8tohtml($post['body'])); $post['body_nomarkup'] = str_replace("\r", '', $post['body_nomarkup']); $post['body'] = str_replace("\r", '', $post['body']); + $post['body_nomarkup'] = str_replace("\t", ' ', $post['body_nomarkup']); + $post['body'] = str_replace("\t", ' ', $post['body']); } mod_page(_('Edit post'), 'mod/edit_post_form.html', array('token' => $security_token, 'board' => $board, 'raw' => $edit_raw_html, 'post' => $post)); From 4772cdaff065273488dfdf324f0fd82396ee81db Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sat, 31 Aug 2013 13:26:53 +1000 Subject: [PATCH 07/13] fix pm_snippet bug --- inc/display.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/display.php b/inc/display.php index 4639e86e..96624fda 100644 --- a/inc/display.php +++ b/inc/display.php @@ -112,7 +112,7 @@ function pm_snippet($body, $len=null) { $len = &$config['mod']['snippet_length']; // Replace line breaks with some whitespace - $body = str_replace('
', ' ', $body); + $body = preg_replace('@
@i', ' ', $body); // Strip tags $body = strip_tags($body); From 7196cbbe4984ad7c47532d1b133962b82da78c3e Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sat, 31 Aug 2013 13:33:26 +1000 Subject: [PATCH 08/13] lol --- inc/mod/pages.php | 2 +- post.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 202ea182..2997ba6b 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -1125,7 +1125,7 @@ function mod_move($originBoard, $postID) { $pdo->quote($board['uri']) . ', ' . $newPostID . ', ' . $pdo->quote($cite[0]) . ', ' . (int)$cite[1] . ')'; } - query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error());; + query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error()); } } diff --git a/post.php b/post.php index de4bc07d..864438b1 100644 --- a/post.php +++ b/post.php @@ -689,7 +689,7 @@ if (isset($_POST['delete'])) { $pdo->quote($board['uri']) . ', ' . (int)$id . ', ' . $pdo->quote($cite[0]) . ', ' . (int)$cite[1] . ')'; } - query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error());; + query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error()); } if (!$post['op'] && strtolower($post['email']) != 'sage' && !$thread['sage'] && ($config['reply_limit'] == 0 || $numposts['replies']+1 < $config['reply_limit'])) { From 5ce4d874eb9eb48769208247a81d385bb473c5c1 Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sat, 31 Aug 2013 19:49:06 +1000 Subject: [PATCH 09/13] js/download-original.js --- js/download-original.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 js/download-original.js diff --git a/js/download-original.js b/js/download-original.js new file mode 100644 index 00000000..673cdf65 --- /dev/null +++ b/js/download-original.js @@ -0,0 +1,27 @@ +/* + * download-original.js + * https://github.com/savetheinternet/Tinyboard/blob/master/js/download-original.js + * + * Makes image filenames clickable, allowing users to download and save files as their original filename. + * Only works in newer browsers. http://caniuse.com/#feat=download + * + * Released under the MIT license + * Copyright (c) 2012-2013 Michael Save + * + * Usage: + * $config['additional_javascript'][] = 'js/jquery.min.js'; + * $config['additional_javascript'][] = 'js/download-original.js'; + * + */ + +onready(function(){ + $('.postfilename').each(function() { + $(this).replaceWith( + $('') + .attr('download', $(this).text()) + .append($(this).contents()) + .attr('href', $(this).parent().parent().find('a').attr('href')) + .attr('title', _('Save as original filename')) + ); + }); +}); From b4a60943b64f51b29af0a001559a0c504b60834e Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sat, 31 Aug 2013 19:59:26 +1000 Subject: [PATCH 10/13] bugfix: work with truncated filenames --- js/download-original.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/js/download-original.js b/js/download-original.js index 673cdf65..b7c98c35 100644 --- a/js/download-original.js +++ b/js/download-original.js @@ -16,12 +16,20 @@ onready(function(){ $('.postfilename').each(function() { + var filename, truncated; + if ($(this).attr('title')) { + filename = $(this).attr('title'); + truncated = true; + } else { + filename = $(this).text(); + } + $(this).replaceWith( $('') - .attr('download', $(this).text()) + .attr('download', filename) .append($(this).contents()) .attr('href', $(this).parent().parent().find('a').attr('href')) - .attr('title', _('Save as original filename')) + .attr('title', _('Save as original filename') + (truncated ? ' (' + filename + ')' : '')) ); }); }); From d0ec7bf913b5cdd75dd014815c7640f7792292ab Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sun, 1 Sep 2013 02:04:42 +1000 Subject: [PATCH 11/13] Some SQL and indexes improvements --- inc/functions.php | 22 ++++++++++++---------- install.php | 13 ++++++++++++- install.sql | 11 +++++++---- post.php | 11 ++++++----- templates/posts.sql | 2 +- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index b9d135cf..918a6ea9 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -573,15 +573,20 @@ function listBoards() { function checkFlood($post) { global $board, $config; - $query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE (`ip` = :ip AND `time` >= :floodtime) OR (`ip` = :ip AND `body` != '' AND `body` = :body AND `time` >= :floodsameiptime) OR (`body` != '' AND `body` = :body AND `time` >= :floodsametime) LIMIT 1", $board['uri'])); + $query = prepare(sprintf("SELECT COUNT(*) FROM ``posts_%s`` WHERE + (`ip` = :ip AND `time` >= :floodtime) + OR + (`ip` = :ip AND :body != '' AND `body_nomarkup` = :body AND `time` >= :floodsameiptime) + OR + (:body != '' AND `body_nomarkup` = :body AND `time` >= :floodsametime) LIMIT 1", $board['uri'])); $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); $query->bindValue(':body', $post['body']); $query->bindValue(':floodtime', time()-$config['flood_time'], PDO::PARAM_INT); $query->bindValue(':floodsameiptime', time()-$config['flood_time_ip'], PDO::PARAM_INT); $query->bindValue(':floodsametime', time()-$config['flood_time_same'], PDO::PARAM_INT); $query->execute() or error(db_error($query)); - - $flood = (bool) $query->fetch(PDO::FETCH_ASSOC); + + $flood = (bool) $query->fetchColumn(); if (event('check-flood', $post)) return true; @@ -658,12 +663,12 @@ function checkBan($board = 0) { if (event('check-ban', $board)) return true; - $query = prepare("SELECT `set`, `expires`, `reason`, `board`, `seen`, ``bans``.`id` FROM ``bans`` WHERE (`board` IS NULL OR `board` = :board) AND `ip` = :ip ORDER BY `expires` IS NULL DESC, `expires` DESC LIMIT 1"); + $query = prepare("SELECT `set`, `expires`, `reason`, `board`, `seen`, `id` FROM ``bans`` WHERE (`board` IS NULL OR `board` = :board) AND `ip` = :ip ORDER BY `expires` IS NULL DESC, `expires` DESC LIMIT 1"); $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); $query->bindValue(':board', $board); $query->execute() or error(db_error($query)); if ($query->rowCount() < 1 && $config['ban_range']) { - $query = prepare("SELECT `set`, `expires`, `reason`, `board`, `seen`, ``bans``.`id` FROM ``bans`` WHERE (`board` IS NULL OR `board` = :board) AND :ip LIKE REPLACE(REPLACE(`ip`, '%', '!%'), '*', '%') ESCAPE '!' ORDER BY `expires` IS NULL DESC, `expires` DESC LIMIT 1"); + $query = prepare("SELECT `set`, `expires`, `reason`, `board`, `seen`, `id` FROM ``bans`` WHERE (`board` IS NULL OR `board` = :board) AND :ip LIKE REPLACE(REPLACE(`ip`, '%', '!%'), '*', '%') ESCAPE '!' ORDER BY `expires` IS NULL DESC, `expires` DESC LIMIT 1"); $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); $query->bindValue(':board', $board); $query->execute() or error(db_error($query)); @@ -1229,14 +1234,11 @@ function checkRobot($body) { // Returns an associative array with 'replies' and 'images' keys function numPosts($id) { global $board; - $query = prepare(sprintf("SELECT COUNT(*) FROM ``posts_%s`` WHERE `thread` = :thread UNION ALL SELECT COUNT(*) FROM ``posts_%s`` WHERE `file` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri'])); + $query = prepare(sprintf("SELECT COUNT(*) AS `replies`, COUNT(NULLIF(`file`, 0)) AS `images` FROM ``posts_%s`` WHERE `thread` = :thread", $board['uri'], $board['uri'])); $query->bindValue(':thread', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); - $num_posts = $query->fetchColumn(); - $num_images = $query->fetchColumn(); - - return array('replies' => $num_posts, 'images' => $num_images); + return $query->fetch(PDO::FETCH_ASSOC); } function muteTime() { diff --git a/install.php b/install.php index 35c1ecb7..967af7de 100644 --- a/install.php +++ b/install.php @@ -1,7 +1,7 @@ Date: Sun, 1 Sep 2013 09:57:09 +1000 Subject: [PATCH 12/13] Fix issue #136 --- templates/posts.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/posts.sql b/templates/posts.sql index 0b0e32b9..b35865ca 100644 --- a/templates/posts.sql +++ b/templates/posts.sql @@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS ``posts_{{ board }}`` ( `embed` text, UNIQUE KEY `id` (`id`), KEY `thread_id` (`thread`,`id`), - KEY `filehash` (`filehash`), + KEY `filehash` (`filehash`(40)), KEY `time` (`time`), KEY `ip` (`ip`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 ; From bf4e0f0bf51ea60e30edb74177c38eff5261d7d0 Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sun, 1 Sep 2013 22:15:03 +1000 Subject: [PATCH 13/13] New index in post tables for listing threads slightly faster (index pages) --- install.php | 7 ++++++- templates/posts.sql | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/install.php b/install.php index 967af7de..d60e5aab 100644 --- a/install.php +++ b/install.php @@ -1,7 +1,7 @@