Add support for encoding option

git-svn-id: http://php-reader.googlecode.com/svn/trunk@129 51a70ab9-7547-0410-9469-37e369ee0574
This commit is contained in:
svollbehr
2008-12-28 19:00:44 +00:00
parent b4cae3bf42
commit c97e87fbc6
16 changed files with 602 additions and 276 deletions

View File

@@ -183,7 +183,7 @@ class ID3_Frame extends ID3_Object
$this->_size = $dataLength; $this->_size = $dataLength;
if ($this->hasFlag(self::UNSYNCHRONISATION) || if ($this->hasFlag(self::UNSYNCHRONISATION) ||
$this->getOption("unsyncronisation", false) === true) $this->getOption("unsynchronisation", false) === true)
$this->_data = $this->decodeUnsynchronisation($this->_data); $this->_data = $this->decodeUnsynchronisation($this->_data);
} }
} }
@@ -246,7 +246,7 @@ class ID3_Frame extends ID3_Object
$this->_data = $data; $this->_data = $data;
$this->_size = strlen($data); $this->_size = strlen($data);
} }
/** /**
* Returns the frame raw data. * Returns the frame raw data.
* *
@@ -285,8 +285,9 @@ class ID3_Frame extends ID3_Object
$data = Transform::toUInt32BE($this->encodeSynchsafe32($this->_size)) . $data = Transform::toUInt32BE($this->encodeSynchsafe32($this->_size)) .
$data; $data;
$flags |= self::DATA_LENGTH_INDICATOR | self::UNSYNCHRONISATION; $flags |= self::DATA_LENGTH_INDICATOR | self::UNSYNCHRONISATION;
$this->setOption("unsyncronisation", true); $this->setOption("unsynchronisation", true);
} } else
$flags &= ~(self::DATA_LENGTH_INDICATOR | self::UNSYNCHRONISATION);
} }
return Transform::toString8(substr($this->_identifier, 0, 4), 4) . return Transform::toString8(substr($this->_identifier, 0, 4), 4) .
Transform::toUInt32BE($this->encodeSynchsafe32($size)) . Transform::toUInt32BE($this->encodeSynchsafe32($size)) .

View File

@@ -80,7 +80,7 @@ final class ID3_Frame_APIC extends ID3_Frame
"Publisher/Studio logotype"); "Publisher/Studio logotype");
/** @var integer */ /** @var integer */
private $_encoding = ID3_Encoding::UTF8; private $_encoding;
/** @var string */ /** @var string */
private $_mimeType = "image/unknown"; private $_mimeType = "image/unknown";
@@ -107,29 +107,41 @@ final class ID3_Frame_APIC extends ID3_Frame
{ {
parent::__construct($reader, $options); parent::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
$this->_mimeType = substr $this->_mimeType = substr
($this->_data, 1, ($pos = strpos($this->_data, "\0", 1)) - 1); ($this->_data, 1, ($pos = strpos($this->_data, "\0", 1)) - 1);
$this->_imageType = Transform::fromUInt8($this->_data[++$pos]); $this->_imageType = Transform::fromUInt8($this->_data[++$pos]);
$this->_data = substr($this->_data, $pos + 1); $this->_data = substr($this->_data, $pos + 1);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
list ($this->_description, $this->_imageData) = list ($this->_description, $this->_imageData) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16($this->_description); $this->_description = $this->convertString
(Transform::fromString16($this->_description), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
list ($this->_description, $this->_imageData) = list ($this->_description, $this->_imageData) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16BE($this->_description); $this->_description = $this->convertString
(Transform::fromString16BE($this->_description), "utf-16be");
break;
case self::UTF8:
list ($this->_description, $this->_imageData) =
$this->explodeString8($this->_data, 2);
$this->_description = $this->convertString
($this->_description, "utf-8");
break; break;
default: default:
list ($this->_description, $this->_imageData) = list ($this->_description, $this->_imageData) =
$this->explodeString8($this->_data, 2); $this->explodeString8($this->_data, 2);
$this->_description = $this->convertString
($this->_description, "iso-8859-1");
} }
$this->_imageSize = strlen($this->_imageData); $this->_imageSize = strlen($this->_imageData);
@@ -138,6 +150,11 @@ final class ID3_Frame_APIC extends ID3_Frame
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer * @return integer
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
@@ -145,6 +162,12 @@ final class ID3_Frame_APIC extends ID3_Frame
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -59,7 +59,7 @@ abstract class ID3_Frame_AbstractText extends ID3_Frame
* *
* @var integer * @var integer
*/ */
protected $_encoding = ID3_Encoding::UTF8; protected $_encoding;
/** /**
* The text array. * The text array.
@@ -78,29 +78,43 @@ abstract class ID3_Frame_AbstractText extends ID3_Frame
{ {
parent::__construct($reader, $options); parent::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
$this->_data = substr($this->_data, 1); $this->_data = substr($this->_data, 1);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
$this->_text = $this->_text = $this->convertString
$this->explodeString16(Transform::fromString16($this->_data)); ($this->explodeString16(Transform::fromString16($this->_data)),
"utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
$this->_text = $this->_text = $this->convertString
$this->explodeString16(Transform::fromString16BE($this->_data)); ($this->explodeString16(Transform::fromString16BE($this->_data)),
"utf-16be");
break;
case self::UTF8:
$this->_text = $this->convertString
($this->explodeString8(Transform::fromString8($this->_data)), "utf-8");
break; break;
default: default:
$this->_text = $this->_text = $this->convertString
$this->explodeString8(Transform::fromString8($this->_data)); ($this->explodeString8(Transform::fromString8($this->_data)),
"iso-8859-1");
} }
} }
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer * @return integer
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
@@ -108,6 +122,12 @@ abstract class ID3_Frame_AbstractText extends ID3_Frame
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -62,7 +62,7 @@ final class ID3_Frame_COMM extends ID3_Frame
implements ID3_Encoding, ID3_Language implements ID3_Encoding, ID3_Language
{ {
/** @var integer */ /** @var integer */
private $_encoding = ID3_Encoding::UTF8; private $_encoding;
/** @var string */ /** @var string */
private $_language = "und"; private $_language = "und";
@@ -83,39 +83,60 @@ final class ID3_Frame_COMM extends ID3_Frame
{ {
parent::__construct($reader, $options); parent::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
$this->_language = substr($this->_data, 1, 3); $this->_language = substr($this->_data, 1, 3);
if ($this->_language == "XXX") if ($this->_language == "XXX")
$this->_language = "und"; $this->_language = "und";
$this->_data = substr($this->_data, 4); $this->_data = substr($this->_data, 4);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
list ($this->_description, $this->_text) = list ($this->_description, $this->_text) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16($this->_description); $this->_description = $this->convertString
$this->_text = Transform::fromString16($this->_text); (Transform::fromString16($this->_description), "utf-16");
$this->_text = $this->convertString
(Transform::fromString16($this->_text), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
list ($this->_description, $this->_text) = list ($this->_description, $this->_text) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16BE($this->_description); $this->_description = $this->convertString
$this->_text = Transform::fromString16BE($this->_text); (Transform::fromString16BE($this->_description), "utf-16be");
$this->_text = $this->convertString
(Transform::fromString16BE($this->_text), "utf-16be");
break;
case self::UTF8:
list ($this->_description, $this->_text) =
$this->explodeString8($this->_data, 2);
$this->_description = $this->convertString
(Transform::fromString8($this->_description), "utf-8");
$this->_text = $this->convertString
(Transform::fromString8($this->_text), "utf-8");
break; break;
default: default:
list ($this->_description, $this->_text) = list ($this->_description, $this->_text) =
$this->explodeString8($this->_data, 2); $this->explodeString8($this->_data, 2);
$this->_description = Transform::fromString8($this->_description); $this->_description = $this->convertString
$this->_text = Transform::fromString8($this->_text); (Transform::fromString8($this->_description), "iso-8859-1");
$this->_text = $this->convertString
(Transform::fromString8($this->_text), "iso-8859-1");
} }
} }
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer * @return integer
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
@@ -123,6 +144,12 @@ final class ID3_Frame_COMM extends ID3_Frame
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -72,7 +72,7 @@ final class ID3_Frame_COMR extends ID3_Frame
"Non-musical merchandise"); "Non-musical merchandise");
/** @var integer */ /** @var integer */
private $_encoding = ID3_Encoding::UTF8; private $_encoding;
/** @var string */ /** @var string */
private $_currency = "EUR"; private $_currency = "EUR";
@@ -114,10 +114,12 @@ final class ID3_Frame_COMR extends ID3_Frame
{ {
parent::__construct($reader, $options); parent::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
list($pricing, $this->_data) = list($pricing, $this->_data) =
$this->explodeString8(substr($this->_data, 1), 2); $this->explodeString8(substr($this->_data, 1), 2);
$this->_currency = substr($pricing, 0, 3); $this->_currency = substr($pricing, 0, 3);
@@ -128,24 +130,38 @@ final class ID3_Frame_COMR extends ID3_Frame
$this->_delivery = Transform::fromUInt8($this->_data[0]); $this->_delivery = Transform::fromUInt8($this->_data[0]);
$this->_data = substr($this->_data, 1); $this->_data = substr($this->_data, 1);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
list ($this->_seller, $this->_description, $this->_data) = list ($this->_seller, $this->_description, $this->_data) =
$this->explodeString16($this->_data, 3); $this->explodeString16($this->_data, 3);
$this->_seller = Transform::fromString16($this->_seller); $this->_seller = $this->convertString
$this->_description = Transform::fromString16($this->_description); (Transform::fromString16($this->_seller), "utf-16");
$this->_description = $this->convertString
(Transform::fromString16($this->_description), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
list ($this->_seller, $this->_description, $this->_data) = list ($this->_seller, $this->_description, $this->_data) =
$this->explodeString16($this->_data, 3); $this->explodeString16($this->_data, 3);
$this->_seller = Transform::fromString16BE($this->_seller); $this->_seller = $this->convertString
$this->_description = Transform::fromString16BE($this->_description); (Transform::fromString16BE($this->_seller), "utf-16be");
$this->_description = $this->convertString
(Transform::fromString16BE($this->_description), "utf-16be");
break;
case self::UTF8:
list ($this->_seller, $this->_description, $this->_data) =
$this->explodeString8($this->_data, 3);
$this->_seller = $this->convertString
(Transform::fromString8($this->_seller), "utf-8");
$this->_description = $this->convertString
(Transform::fromString8($this->_description), "utf-8");
break; break;
default: default:
list ($this->_seller, $this->_description, $this->_data) = list ($this->_seller, $this->_description, $this->_data) =
$this->explodeString8($this->_data, 3); $this->explodeString8($this->_data, 3);
$this->_seller = Transform::fromString8($this->_seller); $this->_seller = $this->convertString
$this->_description = Transform::fromString8($this->_description); (Transform::fromString8($this->_seller), "iso-8859-1");
$this->_description = $this->convertString
(Transform::fromString8($this->_description), "iso-8859-1");
} }
if (strlen($this->_data) == 0) if (strlen($this->_data) == 0)
@@ -160,6 +176,11 @@ final class ID3_Frame_COMR extends ID3_Frame
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer * @return integer
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
@@ -167,6 +188,12 @@ final class ID3_Frame_COMR extends ID3_Frame
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -56,7 +56,7 @@ final class ID3_Frame_GEOB extends ID3_Frame
implements ID3_Encoding implements ID3_Encoding
{ {
/** @var integer */ /** @var integer */
private $_encoding = ID3_Encoding::UTF8; private $_encoding;
/** @var string */ /** @var string */
private $_mimeType; private $_mimeType;
@@ -80,38 +80,59 @@ final class ID3_Frame_GEOB extends ID3_Frame
{ {
parent::__construct($reader, $options); parent::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
$this->_mimeType = substr $this->_mimeType = substr
($this->_data, 1, ($pos = strpos($this->_data, "\0", 1)) - 1); ($this->_data, 1, ($pos = strpos($this->_data, "\0", 1)) - 1);
$this->_data = substr($this->_data, $pos + 1); $this->_data = substr($this->_data, $pos + 1);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
list ($this->_filename, $this->_description, $this->_objectData) = list ($this->_filename, $this->_description, $this->_objectData) =
$this->explodeString16($this->_data, 3); $this->explodeString16($this->_data, 3);
$this->_filename = Transform::fromString16($this->_filename); $this->_filename = $this->convertString
$this->_description = Transform::fromString16($this->_description); (Transform::fromString16($this->_filename), "utf-16");
$this->_description = $this->convertString
(Transform::fromString16($this->_description), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
list ($this->_filename, $this->_description, $this->_objectData) = list ($this->_filename, $this->_description, $this->_objectData) =
$this->explodeString16($this->_data, 3); $this->explodeString16($this->_data, 3);
$this->_filename = Transform::fromString16BE($this->_filename); $this->_filename = $this->convertString
$this->_description = Transform::fromString16BE($this->_description); (Transform::fromString16BE($this->_filename), "utf-16be");
$this->_description = $this->convertString
(Transform::fromString16BE($this->_description), "utf-16be");
break;
case self::UTF8:
list ($this->_filename, $this->_description, $this->_objectData) =
$this->explodeString8($this->_data, 3);
$this->_filename = $this->convertString
(Transform::fromString8($this->_filename), "utf-8");
$this->_description = $this->convertString
(Transform::fromString8($this->_description), "utf-8");
break; break;
default: default:
list ($this->_filename, $this->_description, $this->_objectData) = list ($this->_filename, $this->_description, $this->_objectData) =
$this->explodeString8($this->_data, 3); $this->explodeString8($this->_data, 3);
$this->_filename = Transform::fromString8($this->_filename); $this->_filename = $this->convertString
$this->_description = Transform::fromString8($this->_description); (Transform::fromString8($this->_filename), "iso-8859-1");
$this->_description = $this->convertString
(Transform::fromString8($this->_description), "iso-8859-1");
} }
} }
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer * @return integer
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
@@ -119,6 +140,12 @@ final class ID3_Frame_GEOB extends ID3_Frame
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -59,7 +59,7 @@ final class ID3_Frame_IPLS extends ID3_Frame
implements ID3_Encoding implements ID3_Encoding
{ {
/** @var integer */ /** @var integer */
private $_encoding = ID3_Encoding::UTF8; private $_encoding;
/** @var Array */ /** @var Array */
private $_people = array(); private $_people = array();
@@ -74,25 +74,32 @@ final class ID3_Frame_IPLS extends ID3_Frame
{ {
parent::__construct($reader, $options); parent::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
$data = substr($this->_data, 1); $data = substr($this->_data, 1);
$order = Transform::MACHINE_ENDIAN_ORDER; $order = Transform::MACHINE_ENDIAN_ORDER;
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
$data = $this->explodeString16($data); $data = $this->explodeString16($data);
foreach ($data as &$str) foreach ($data as &$str)
$str = Transform::fromString16($str, $order); $str = $this->convertString
(Transform::fromString16($str, $order), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
$data = $this->explodeString16($data); $data = $this->explodeString16($data);
foreach ($data as &$str) foreach ($data as &$str)
$str = Transform::fromString16BE($str); $str = $this->convertString
(Transform::fromString16BE($str), "utf-16be");
break;
case self::UTF8:
$data = $this->convertString($this->explodeString8($data), "utf-8");
break; break;
default: default:
$data = $this->explodeString8($data); $data = $this->convertString($this->explodeString8($data), "iso-8859-1");
} }
for ($i = 0; $i < count($data) - 1; $i += 2) for ($i = 0; $i < count($data) - 1; $i += 2)
@@ -101,14 +108,25 @@ final class ID3_Frame_IPLS extends ID3_Frame
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer * @return integer
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -59,7 +59,7 @@ final class ID3_Frame_OWNE extends ID3_Frame
implements ID3_Encoding implements ID3_Encoding
{ {
/** @var integer */ /** @var integer */
private $_encoding = ID3_Encoding::UTF8; private $_encoding;
/** @var string */ /** @var string */
private $_currency = "EUR"; private $_currency = "EUR";
@@ -83,10 +83,12 @@ final class ID3_Frame_OWNE extends ID3_Frame
{ {
parent::__construct($reader, $options); parent::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
list($tmp, $this->_data) = list($tmp, $this->_data) =
$this->explodeString8(substr($this->_data, 1), 2); $this->explodeString8(substr($this->_data, 1), 2);
$this->_currency = substr($tmp, 0, 3); $this->_currency = substr($tmp, 0, 3);
@@ -94,21 +96,33 @@ final class ID3_Frame_OWNE extends ID3_Frame
$this->_date = substr($this->_data, 0, 8); $this->_date = substr($this->_data, 0, 8);
$this->_data = substr($this->_data, 8); $this->_data = substr($this->_data, 8);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
$this->_seller = Transform::fromString16($this->_data); $this->_seller = $this->convertString
(Transform::fromString16($this->_data), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
$this->_seller = Transform::fromString16BE($this->_data); $this->_seller = $this->convertString
(Transform::fromString16BE($this->_data), "utf-16be");
break;
case self::UTF8:
$this->_seller = $this->convertString
(Transform::fromString8($this->_data), "utf-8");
break; break;
default: default:
$this->_seller = Transform::fromString8($this->_data); $this->_seller = $this->convertString
(Transform::fromString8($this->_data), "iso-8859-1");
} }
} }
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer * @return integer
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
@@ -116,6 +130,12 @@ final class ID3_Frame_OWNE extends ID3_Frame
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -72,7 +72,7 @@ final class ID3_Frame_SYLT extends ID3_Frame
"Chord", "Trivia", "URLs to webpages", "URLs to images"); "Chord", "Trivia", "URLs to webpages", "URLs to images");
/** @var integer */ /** @var integer */
private $_encoding = ID3_Encoding::UTF8; private $_encoding;
/** @var string */ /** @var string */
private $_language = "und"; private $_language = "und";
@@ -99,10 +99,12 @@ final class ID3_Frame_SYLT extends ID3_Frame
{ {
parent::__construct($reader, $options); parent::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
$this->_language = substr($this->_data, 1, 3); $this->_language = substr($this->_data, 1, 3);
if ($this->_language == "XXX") if ($this->_language == "XXX")
$this->_language = "und"; $this->_language = "und";
@@ -110,39 +112,57 @@ final class ID3_Frame_SYLT extends ID3_Frame
$this->_type = Transform::fromUInt8($this->_data[5]); $this->_type = Transform::fromUInt8($this->_data[5]);
$this->_data = substr($this->_data, 6); $this->_data = substr($this->_data, 6);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
list($this->_description, $this->_data) = list($this->_description, $this->_data) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16($this->_description); $this->_description = $this->convertString
(Transform::fromString16($this->_description), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
list($this->_description, $this->_data) = list($this->_description, $this->_data) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16BE($this->_description); $this->_description = $this->convertString
(Transform::fromString16BE($this->_description), "utf-16be");
break;
case self::UTF8:
list($this->_description, $this->_data) =
$this->explodeString8($this->_data, 2);
$this->_description = $this->convertString
(Transform::fromString8($this->_description), "utf-8");
break; break;
default: default:
list($this->_description, $this->_data) = list($this->_description, $this->_data) =
$this->explodeString8($this->_data, 2); $this->explodeString8($this->_data, 2);
$this->_description = Transform::fromString8($this->_description); $this->_description = $this->convertString
(Transform::fromString8($this->_description), "iso-8859-1");
} }
while (strlen($this->_data) > 0) { while (strlen($this->_data) > 0) {
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
list($syllable, $this->_data) = list($syllable, $this->_data) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$syllable = Transform::fromString16($syllable); $syllable = $this->convertString
(Transform::fromString16($syllable), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
list($syllable, $this->_data) = list($syllable, $this->_data) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$syllable = Transform::fromString16BE($syllable); $syllable = $this->convertString
(Transform::fromString16BE($syllable), "utf-16be");
break;
case self::UTF8:
list($syllable, $this->_data) =
$this->explodeString8($this->_data, 2);
$syllable = $this->convertString
(Transform::fromString8($syllable), "utf-8");
break; break;
default: default:
list($syllable, $this->_data) = list($syllable, $this->_data) =
$this->explodeString8($this->_data, 2); $this->explodeString8($this->_data, 2);
$syllable = Transform::fromString8($syllable); $syllable = $this->convertString
(Transform::fromString8($syllable), "iso-8859-1");
} }
$this->_events[Transform::fromUInt32BE(substr($this->_data, 0, 4))] = $this->_events[Transform::fromUInt32BE(substr($this->_data, 0, 4))] =
$syllable; $syllable;
@@ -154,6 +174,11 @@ final class ID3_Frame_SYLT extends ID3_Frame
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer * @return integer
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
@@ -161,6 +186,12 @@ final class ID3_Frame_SYLT extends ID3_Frame
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -72,28 +72,39 @@ final class ID3_Frame_TXXX extends ID3_Frame_AbstractText
{ {
ID3_Frame::__construct($reader, $options); ID3_Frame::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
$this->_data = substr($this->_data, 1); $this->_data = substr($this->_data, 1);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
list($this->_description, $this->_text) = list($this->_description, $this->_text) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16($this->_description); $this->_description = $this->convertString
$this->_text = array(Transform::fromString16($this->_text)); (Transform::fromString16($this->_description), "utf-16");
$this->_text = $this->convertString
(array(Transform::fromString16($this->_text)), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
list($this->_description, $this->_text) = list($this->_description, $this->_text) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16BE($this->_description); $this->_description = $this->convertString
$this->_text = array(Transform::fromString16BE($this->_text)); (Transform::fromString16BE($this->_description), "utf-16be");
$this->_text = $this->convertString
(array(Transform::fromString16BE($this->_text)), "utf-16be");
break;
case self::UTF8:
list($this->_description, $this->_text) = $this->convertString
($this->explodeString8($this->_data, 2), "utf-8");
$this->_text = array($this->_text);
break; break;
default: default:
list($this->_description, $this->_text) = list($this->_description, $this->_text) = $this->convertString
$this->explodeString8($this->_data, 2); ($this->explodeString8($this->_data, 2), "iso-8859-1");
$this->_text = array($this->_text); $this->_text = array($this->_text);
} }
} }

View File

@@ -60,7 +60,7 @@ final class ID3_Frame_USER extends ID3_Frame
implements ID3_Encoding, ID3_Language implements ID3_Encoding, ID3_Language
{ {
/** @var integer */ /** @var integer */
private $_encoding = ID3_Encoding::UTF8; private $_encoding;
/** @var string */ /** @var string */
private $_language = "und"; private $_language = "und";
@@ -78,30 +78,44 @@ final class ID3_Frame_USER extends ID3_Frame
{ {
parent::__construct($reader, $options); parent::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
$this->_language = substr($this->_data, 1, 3); $this->_language = substr($this->_data, 1, 3);
if ($this->_language == "XXX") if ($this->_language == "XXX")
$this->_language = "und"; $this->_language = "und";
$this->_data = substr($this->_data, 4); $this->_data = substr($this->_data, 4);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
$this->_text = Transform::fromString16($this->_data); $this->_text = $this->convertString
(Transform::fromString16($this->_data), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
$this->_text = Transform::fromString16BE($this->_data); $this->_text = $this->convertString
(Transform::fromString16BE($this->_data), "utf-16be");
break;
case self::UTF8:
$this->_text = $this->convertString
(Transform::fromString8($this->_data), "utf-8");
break; break;
default: default:
$this->_text = Transform::fromString8($this->_data); $this->_text = $this->convertString
(Transform::fromString8($this->_data), "iso-8859-1");
} }
} }
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer * @return integer
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
@@ -109,6 +123,12 @@ final class ID3_Frame_USER extends ID3_Frame
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -59,7 +59,7 @@ final class ID3_Frame_USLT extends ID3_Frame
implements ID3_Encoding, ID3_Language implements ID3_Encoding, ID3_Language
{ {
/** @var integer */ /** @var integer */
private $_encoding = ID3_Encoding::UTF8; private $_encoding;
/** @var string */ /** @var string */
private $_language = "und"; private $_language = "und";
@@ -80,39 +80,60 @@ final class ID3_Frame_USLT extends ID3_Frame
{ {
parent::__construct($reader, $options); parent::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
$this->_language = substr($this->_data, 1, 3); $this->_language = substr($this->_data, 1, 3);
if ($this->_language == "XXX") if ($this->_language == "XXX")
$this->_language = "und"; $this->_language = "und";
$this->_data = substr($this->_data, 4); $this->_data = substr($this->_data, 4);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
list ($this->_description, $this->_text) = list ($this->_description, $this->_text) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16($this->_description); $this->_description = $this->convertString
$this->_text = Transform::fromString16($this->_text); (Transform::fromString16($this->_description), "utf-16");
$this->_text = $this->convertString
(Transform::fromString16($this->_text), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
list ($this->_description, $this->_text) = list ($this->_description, $this->_text) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16BE($this->_description); $this->_description = $this->convertString
$this->_text = Transform::fromString16BE($this->_text); (Transform::fromString16BE($this->_description), "utf-16be");
$this->_text = $this->convertString
(Transform::fromString16BE($this->_text), "utf-16be");
break;
case self::UTF8:
list ($this->_description, $this->_text) =
$this->explodeString8($this->_data, 2);
$this->_description = $this->convertString
(Transform::fromString8($this->_description), "utf-8");
$this->_text = $this->convertString
(Transform::fromString8($this->_text), "utf-8");
break; break;
default: default:
list ($this->_description, $this->_text) = list ($this->_description, $this->_text) =
$this->explodeString8($this->_data, 2); $this->explodeString8($this->_data, 2);
$this->_description = Transform::fromString8($this->_description); $this->_description = $this->convertString
$this->_text = Transform::fromString8($this->_text); (Transform::fromString8($this->_description), "iso-8859-1");
$this->_text = $this->convertString
(Transform::fromString8($this->_text), "iso-8859-1");
} }
} }
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer * @return integer
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
@@ -120,6 +141,12 @@ final class ID3_Frame_USLT extends ID3_Frame
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -59,7 +59,7 @@ final class ID3_Frame_WXXX extends ID3_Frame_AbstractLink
implements ID3_Encoding implements ID3_Encoding
{ {
/** @var integer */ /** @var integer */
private $_encoding = ID3_Encoding::UTF8; private $_encoding;
/** @var string */ /** @var string */
private $_description; private $_description;
@@ -74,27 +74,37 @@ final class ID3_Frame_WXXX extends ID3_Frame_AbstractLink
{ {
ID3_Frame::__construct($reader, $options); ID3_Frame::__construct($reader, $options);
$this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
if ($reader === null) if ($reader === null)
return; return;
$this->_encoding = Transform::fromUInt8($this->_data[0]); $encoding = Transform::fromUInt8($this->_data[0]);
$this->_data = substr($this->_data, 1); $this->_data = substr($this->_data, 1);
switch ($this->_encoding) { switch ($encoding) {
case self::UTF16: case self::UTF16:
list($this->_description, $this->_link) = list($this->_description, $this->_link) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16($this->_description); $this->_description = $this->convertString
(Transform::fromString16($this->_description), "utf-16");
break; break;
case self::UTF16BE: case self::UTF16BE:
list($this->_description, $this->_link) = list($this->_description, $this->_link) =
$this->explodeString16($this->_data, 2); $this->explodeString16($this->_data, 2);
$this->_description = Transform::fromString16BE($this->_description); $this->_description = $this->convertString
(Transform::fromString16BE($this->_description), "utf-16be");
break;
case self::UTF8:
list($this->_description, $this->_link) =
$this->explodeString8($this->_data, 2);
$this->_description = $this->convertString($this->_description, "utf-8");
break; break;
default: default:
list($this->_description, $this->_link) = list($this->_description, $this->_link) =
$this->explodeString8($this->_data, 2); $this->explodeString8($this->_data, 2);
break; $this->_description = $this->convertString
($this->_description, "iso-8859-1");
} }
$this->_link = implode($this->explodeString8($this->_link, 1), ""); $this->_link = implode($this->explodeString8($this->_link, 1), "");
} }
@@ -102,6 +112,11 @@ final class ID3_Frame_WXXX extends ID3_Frame_AbstractLink
/** /**
* Returns the text encoding. * Returns the text encoding.
* *
* All the strings read from a file are automatically converted to the
* character encoding specified with the <var>encoding</var> option. See
* {@link ID3v2} for details. This method returns the original text encoding
* used to write the frame.
*
* @return integer The encoding. * @return integer The encoding.
*/ */
public function getEncoding() { return $this->_encoding; } public function getEncoding() { return $this->_encoding; }
@@ -109,6 +124,12 @@ final class ID3_Frame_WXXX extends ID3_Frame_AbstractLink
/** /**
* Sets the text encoding. * Sets the text encoding.
* *
* All the string written to the frame are done so using given character
* encoding. No conversions of existing data take place upon the call to this
* method thus all texts must be given in given character encoding.
*
* The default character encoding used to write the frame is UTF-8.
*
* @see ID3_Encoding * @see ID3_Encoding
* @param integer $encoding The text encoding. * @param integer $encoding The text encoding.
*/ */

View File

@@ -195,7 +195,7 @@ abstract class ID3_Object
/** /**
* Reverses the unsynchronisation scheme from the given data string. * Reverses the unsynchronisation scheme from the given data string.
* *
* @see encodeUnsyncronisation * @see encodeUnsynchronisation
* @param string $data The input data. * @param string $data The input data.
* @return string * @return string
*/ */
@@ -248,4 +248,42 @@ abstract class ID3_Object
{ {
return preg_split("/\\x00/", $value, $limit); return preg_split("/\\x00/", $value, $limit);
} }
/**
* Converts string to requested character encoding and returns it. See the
* documentation of iconv for accepted values for encoding.
*
* @param string|Array $string
* @param string $encoding
*/
protected function convertString($string, $encoding)
{
$target = $this->getOption("encoding", ID3_Encoding::UTF8);
switch ($target) {
case ID3_Encoding::UTF16:
$target = "utf-16";
break;
case ID3_Encoding::UTF16LE:
$target = "utf-16le";
break;
case ID3_Encoding::UTF16BE:
$target = "utf-16be";
break;
case ID3_Encoding::UTF8:
$target = "utf-8";
break;
default:
$target = "iso-8859-1";
}
if (strtolower($target) == strtolower($encoding))
return $string;
if (is_array($string))
foreach ($string as $key => $value)
$string[$key] = iconv($encoding, $target, $value);
else
$string = iconv($encoding, $target, $string);
return $string;
}
} }

View File

@@ -96,6 +96,10 @@ final class ID3v2
* may also be given as the only parameter. * may also be given as the only parameter.
* *
* The following options are currently recognized: * The following options are currently recognized:
* o encoding -- Indicates the encoding that all the texts are presented
* with. By default this is set to ID3_Encoding::UTF8. See the
* documentation of the {@link ID3_Encoding} interface for accepted
* values. Conversions are carried out using iconv.
* o version -- The ID3v2 tag version to use in write operation. This option * o version -- The ID3v2 tag version to use in write operation. This option
* is automatically set when a tag is read from a file and defaults to * is automatically set when a tag is read from a file and defaults to
* version 4.0 for tag write. * version 4.0 for tag write.

File diff suppressed because it is too large Load Diff