#540 -- Add multibyte string truncation to several user-populated fields.

This commit is contained in:
Buster "Silver Eagle" Neece 2018-04-06 16:29:27 -05:00
parent 00ad20af94
commit c2a209f8b4
12 changed files with 80 additions and 47 deletions

View File

@ -7,6 +7,8 @@ namespace Entity;
*/
class ApiKey
{
use Traits\TruncateStrings;
/**
* @Column(name="id", type="string", length=16)
* @Id
@ -105,6 +107,6 @@ class ApiKey
*/
public function setComment(?string $comment): void
{
$this->comment = $comment;
$this->comment = $this->_truncateString($comment);
}
}

View File

@ -10,6 +10,8 @@ namespace Entity;
*/
class Listener
{
use Traits\TruncateStrings;
/**
* Listener constructor.
* @param Station $station
@ -23,7 +25,7 @@ class Listener
$this->timestamp_end = 0;
$this->listener_uid = $client['uid'];
$this->listener_user_agent = $client['user_agent'];
$this->listener_user_agent = $this->_truncateString($client['user_agent']);
$this->listener_ip = $client['ip'];
$this->listener_hash = self::calculateListenerHash($client);
}

View File

@ -10,6 +10,8 @@ use Doctrine\Common\Collections\Collection;
*/
class Role
{
use Traits\TruncateStrings;
/**
* @Column(name="id", type="integer")
* @Id
@ -66,7 +68,7 @@ class Role
*/
public function setName(string $name)
{
$this->name = $name;
$this->name = $this->_truncateString($name, 100);
}
/**

View File

@ -12,6 +12,8 @@ use Doctrine\Common\Collections\Collection;
*/
class Song
{
use Traits\TruncateStrings;
const SYNC_THRESHOLD = 604800; // 604800 = 1 week
/**
@ -78,9 +80,9 @@ class Song
}
}
$this->text = $this->_filterSongHash($song_info['text']);
$this->title = $this->_filterSongHash($song_info['title']);
$this->artist = $song_info['artist'];
$this->text = $this->_truncateString($song_info['text'], 150);
$this->title = $this->_truncateString($song_info['title'], 150);
$this->artist = $this->_truncateString($song_info['artist'], 150);
$this->id = self::getSongHash($song_info);
@ -91,16 +93,6 @@ class Song
$this->history = new ArrayCollection;
}
protected function _filterSongHash($text)
{
preg_match('/#(.*)#$/', $text, $matches);
if (!empty($matches)) {
$text = str_replace($matches[0], '', $text);
}
return $text;
}
/**
* @return string
*/
@ -220,12 +212,8 @@ class Song
}
}
preg_match('/#(.*)#$/', $song_text, $matches);
if (!empty($matches)) {
return $matches[1];
}
// Strip non-alphanumeric characters
$song_text = mb_substr($song_text, 0, 150, 'UTF-8');
$hash_base = mb_strtolower(str_replace([' ', '-'], ['', ''], $song_text), 'UTF-8');
return md5($hash_base);

View File

@ -14,6 +14,8 @@ use Interop\Container\ContainerInterface;
*/
class Station
{
use Traits\TruncateStrings;
/**
* @Column(name="id", type="integer")
* @Id
@ -268,12 +270,12 @@ class Station
/**
* @param null|string $name
*/
public function setName(string $name = null)
public function setName(?string $name = null)
{
$this->name = $name;
$this->name = $this->_truncateString($name, 100);
if (empty($this->short_name) && !empty($name)) {
$this->short_name = self::getStationShortName($name);
$this->setShortName(self::getStationShortName($name));
}
}
@ -294,7 +296,7 @@ class Station
{
$short_name = trim($short_name);
if (!empty($short_name)) {
$this->short_name = $short_name;
$this->short_name = $this->_truncateString($short_name, 100);
}
}
@ -462,7 +464,7 @@ class Station
*/
public function setUrl(string $url = null)
{
$this->url = $url;
$this->url = $this->_truncateString($url);
}
/**
@ -478,6 +480,8 @@ class Station
*/
public function setRadioBaseDir($new_dir)
{
$new_dir = $this->_truncateString(trim($new_dir));
if (strcmp($this->radio_base_dir, $new_dir) !== 0) {
$this->radio_base_dir = $new_dir;
@ -489,7 +493,9 @@ class Station
];
foreach ($radio_dirs as $radio_dir) {
if (!file_exists($radio_dir)) {
mkdir($radio_dir, 0777);
if (!mkdir($radio_dir, 0777) && !is_dir($radio_dir)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $radio_dir));
}
}
}
}
@ -524,13 +530,15 @@ class Station
/**
* @param $new_dir
*/
public function setRadioMediaDir(string $new_dir)
public function setRadioMediaDir(?string $new_dir)
{
if ($new_dir !== $this->radio_media_dir) {
$new_dir = trim($new_dir);
$new_dir = $this->_truncateString(trim($new_dir));
if ($new_dir && $new_dir !== $this->radio_media_dir) {
if (!empty($new_dir) && !file_exists($new_dir)) {
mkdir($new_dir, 0777, true);
if (!mkdir($new_dir, 0777, true) && !is_dir($new_dir)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $new_dir));
}
}
$this->radio_media_dir = $new_dir;

View File

@ -16,7 +16,7 @@ use Doctrine\Common\Collections\Collection;
*/
class StationMedia
{
use Traits\UniqueId;
use Traits\UniqueId, Traits\TruncateStrings;
/**
* @Column(name="id", type="integer")
@ -214,7 +214,7 @@ class StationMedia
*/
public function setTitle(string $title = null)
{
$this->title = $title;
$this->title = $this->_truncateString($title, 200);
}
/**
@ -230,7 +230,7 @@ class StationMedia
*/
public function setArtist(string $artist = null)
{
$this->artist = $artist;
$this->artist = $this->_truncateString($artist, 200);
}
/**
@ -246,7 +246,7 @@ class StationMedia
*/
public function setAlbum(string $album = null)
{
$this->album = $album;
$this->album = $this->_truncateString($album, 200);
}
/**

View File

@ -10,6 +10,8 @@ use AzuraCast\Radio\Frontend\FrontendAbstract;
*/
class StationMount
{
use Traits\TruncateStrings;
/**
* @Column(name="id", type="integer")
* @Id
@ -166,7 +168,7 @@ class StationMount
*/
public function setName(string $new_name)
{
$this->name = '/' . ltrim($new_name, '/');
$this->name = $this->_truncateString('/' . ltrim($new_name, '/'), 100);
}
/**
@ -342,7 +344,7 @@ class StationMount
*/
public function setRemoteUrl($remote_url)
{
$this->remote_url = $remote_url;
$this->remote_url = $this->_truncateString($remote_url);
}
/**
@ -358,7 +360,7 @@ class StationMount
*/
public function setRemoteMount($remote_mount)
{
$this->remote_mount = $remote_mount;
$this->remote_mount = $this->_truncateString($remote_mount, 150);
}
/**
@ -374,7 +376,7 @@ class StationMount
*/
public function setRemoteSourceUsername($remote_source_username)
{
$this->remote_source_username = $remote_source_username;
$this->remote_source_username = $this->_truncateString($remote_source_username, 100);
}
/**
@ -390,7 +392,7 @@ class StationMount
*/
public function setRemoteSourcePassword($remote_source_password)
{
$this->remote_source_password = $remote_source_password;
$this->remote_source_password = $this->_truncateString($remote_source_password, 100);
}
/**

View File

@ -13,6 +13,8 @@ use DateTime;
*/
class StationPlaylist
{
use Traits\TruncateStrings;
/**
* @Column(name="id", type="integer")
* @Id
@ -169,7 +171,7 @@ class StationPlaylist
*/
public function setName(string $name)
{
$this->name = $name;
$this->name = $this->_truncateString($name, 200);
}
/**

View File

@ -10,6 +10,8 @@ namespace Entity;
*/
class StationStreamer
{
use Traits\TruncateStrings;
/**
* @Column(name="id", type="integer")
* @Id
@ -99,7 +101,7 @@ class StationStreamer
*/
public function setStreamerUsername(string $streamer_username)
{
$this->streamer_username = $streamer_username;
$this->streamer_username = $this->_truncateString($streamer_username, 50);
}
/**
@ -115,7 +117,7 @@ class StationStreamer
*/
public function setStreamerPassword(string $streamer_password)
{
$this->streamer_password = $streamer_password;
$this->streamer_password = $this->_truncateString($streamer_password, 50);
}
/**
@ -133,7 +135,7 @@ class StationStreamer
*/
public function setDisplayName(?string $display_name): void
{
$this->display_name = $display_name;
$this->display_name = $this->_truncateString($display_name);
}
/**

View File

@ -7,6 +7,8 @@ namespace Entity;
*/
class StationWebhook
{
use Traits\TruncateStrings;
/**
* @Column(name="id", type="integer")
* @Id
@ -97,7 +99,7 @@ class StationWebhook
*/
public function setName(?string $name): void
{
$this->name = $name;
$this->name = $this->_truncateString($name, 100);
}
/**

View File

@ -0,0 +1,21 @@
<?php
namespace Entity\Traits;
trait TruncateStrings
{
/**
* Truncate the specified string to the maximum length specified, or return NULL if the value was previously NULL.
*
* @param null|string $string
* @param int $length
* @return null|string
*/
protected function _truncateString(?string $string = null, $length = 255): ?string
{
if ($string === null) {
return null;
}
return mb_substr($string, 0, $length, 'UTF-8');
}
}

View File

@ -12,6 +12,8 @@ use Doctrine\ORM\Mapping as ORM;
*/
class User
{
use Traits\TruncateStrings;
/**
* @Column(name="uid", type="integer")
* @Id
@ -122,7 +124,7 @@ class User
*/
public function setEmail($email)
{
$this->email = $email;
$this->email = $this->_truncateString($email, 100);
}
/**
@ -197,7 +199,7 @@ class User
*/
public function setName($name)
{
$this->name = $name;
$this->name = $this->_truncateString($name, 100);
}
/**