From a4bdab802daf240f9e6d41ad1e77b54a8d718cbf Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Tue, 2 Sep 2014 09:44:07 -0400 Subject: [PATCH] Handle empty, invalid, and single-element array arguments without dying, generating invalid SQL, or hitting a str_repeat error. --- jonah/lib/Driver/Sql.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/jonah/lib/Driver/Sql.php b/jonah/lib/Driver/Sql.php index 0041f9bbb81..3ff950f46a2 100644 --- a/jonah/lib/Driver/Sql.php +++ b/jonah/lib/Driver/Sql.php @@ -855,7 +855,11 @@ public function searchTags($names, $max = 10, $from = 0, $channel_id = array(), */ public function getTagNames($ids) { - $sql = 'SELECT t.tag_name FROM jonah_tags as t WHERE t.tag_id IN(' . str_repeat('?,', count($ids) - 1) . '?)'; + if (empty($ids)) { + return array(); + } + + $sql = 'SELECT t.tag_name FROM jonah_tags as t WHERE t.tag_id IN (' . implode(',', array_map(function($v) { return '?'; }, $ids)) . ')'; $tags = $this->_db->getCol($sql, 0, $ids); if ($tags instanceof PEAR_Error) { throw new Jonah_Exception($tags); @@ -873,7 +877,11 @@ public function getTagNames($ids) */ public function getTagIds($names) { - $sql = 'SELECT t.tag_name, t.tag_id FROM jonah_tags as t WHERE t.tag_name IN(' . str_repeat('?,', count($names) - 1) . '?)'; + if (empty($names)) { + return array(); + } + + $sql = 'SELECT t.tag_name, t.tag_id FROM jonah_tags as t WHERE t.tag_name IN (' . implode(',', array_map(function($v) { return '?'; }, $names)) . ')'; $tags = $this->_db->getAssoc($sql, false, $names); if ($tags instanceof PEAR_Error) { throw new Jonah_Exception($tags);