Skip to content
Snippets Groups Projects
Commit 2d961d7a authored by Demian Katz's avatar Demian Katz
Browse files

Added truncation in tag parser to avoid illegal lengths.

parent f6d4bd2a
No related merge requests found
......@@ -38,6 +38,23 @@ namespace VuFind;
*/
class Tags
{
/**
* Maximum tag length.
*
* @var int
*/
protected $maxLength;
/**
* Constructor
*
* @param int $maxLength Maximum tag length
*/
public function __construct($maxLength = 64)
{
$this->maxLength = $maxLength;
}
/**
* Parse a user-submitted tag string into an array of separate tags.
*
......@@ -50,7 +67,8 @@ class Tags
preg_match_all('/"[^"]*"|[^ ]+/', trim($tags), $words);
$result = array();
foreach ($words[0] as $tag) {
$result[] = str_replace('"', '', $tag);
// Wipe out double-quotes and trim over-long tags:
$result[] = substr(str_replace('"', '', $tag), 0, $this->maxLength);
}
return array_unique($result);
}
......
......@@ -85,4 +85,16 @@ class TagsTest extends \PHPUnit_Framework_TestCase
{
$this->assertEquals(array('test'), $this->parser->parse('test test test'));
}
/**
* Test truncation
*
* @return void
*/
public function testTruncation()
{
// Create custom object w/ small size limit:
$parser = new \VuFind\Tags(10);
$this->assertEquals(array('0123456789'), $parser->parse('01234567890'));
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment