Minor fixes

git-svn-id: http://php-reader.googlecode.com/svn/trunk@93 51a70ab9-7547-0410-9469-37e369ee0574
This commit is contained in:
svollbehr
2008-05-10 17:11:44 +00:00
parent a97d59bf89
commit f85884f492
7 changed files with 60 additions and 25 deletions

View File

@@ -117,7 +117,7 @@ final class ID3_ExtendedHeader extends ID3_Object
$this->_size = $this->decodeSynchsafe32($this->_reader->readUInt32BE());
/* ID3v2.3.0 ExtendedHeader */
if (isset($this->_options["version"]) && $this->_options["version"] < 4) {
if ($this->getOption("version", 4) < 4) {
if ($this->_reader->readUInt16BE() == 0x8000)
$this->_flags = self::CRC32;
$this->_padding = $this->_reader->readUInt32BE();
@@ -299,7 +299,7 @@ final class ID3_ExtendedHeader extends ID3_Object
public function __toString()
{
/* ID3v2.3.0 ExtendedHeader */
if (isset($this->_options["version"]) && $this->_options["version"] < 4) {
if ($this->getOption("version", 4) < 4) {
return Transform::toUInt32BE($this->_size) .
Transform::toUInt16BE($this->hasFlag(self::CRC32) ? 0x8000 : 0) .
Transform::toUInt32BE($this->_padding) .

View File

@@ -152,7 +152,7 @@ class ID3_Frame extends ID3_Object
$this->_size = $this->decodeSynchsafe32($this->_reader->readUInt32BE());
/* ID3v2.3.0 Flags; convert to 2.4.0 format */
if (isset($this->_options["version"]) && $this->_options["version"] < 4) {
if ($this->getOption("version", 4) < 4) {
$flags = $this->_reader->readUInt16BE();
if (($flags & 0x8000) == 0x8000)
$this->_flags |= self::DISCARD_ON_TAGCHANGE;
@@ -243,7 +243,7 @@ class ID3_Frame extends ID3_Object
public function __toString()
{
/* ID3v2.3.0 Flags; convert from 2.4.0 format */
if (isset($this->_options["version"]) && $this->_options["version"] < 4) {
if ($this->getOption("version", 4) < 4) {
$flags = 0;
if ($this->hasFlag(self::DISCARD_ON_TAGCHANGE))
$flags = $flags | 0x8000;

View File

@@ -119,7 +119,7 @@ final class ID3_Header extends ID3_Object
*/
public function setVersion($version)
{
$this->_version = $this->_options["version"] = $version;
$this->setOption("version", $this->_version = $version);
}
/**

View File

@@ -59,7 +59,7 @@ abstract class ID3_Object
*
* @var Array
*/
protected $_options;
private $_options;
/**
* Constructs the class with given parameters and reads object related data
@@ -81,6 +81,20 @@ abstract class ID3_Object
*/
public function getOptions() { return $this->_options; }
/**
* Returns the given option value, or the default value if the option is not
* defined.
*
* @param string $option The name of the option.
* @param mixed $defaultValue The default value to be returned.
*/
public function getOption($option, $defaultValue = false)
{
if (isset($this->_options[$option]))
return $this->_options[$option];
return $defaultValue;
}
/**
* Sets the options array. See {@link ID3v2} class for available options.
*
@@ -88,6 +102,17 @@ abstract class ID3_Object
*/
public function setOptions(&$options) { $this->_options = $options; }
/**
* Sets the given option the given value.
*
* @param string $option The name of the option.
* @param mixed $value The value to set for the option.
*/
public function setOption($option, $value)
{
$this->_options[$option] = $value;
}
/**
* Magic function so that $obj->value will work.
*

View File

@@ -132,6 +132,8 @@ final class ID3v2
throw new ID3_Exception
("File does not contain ID3v2 tag: " . $filename);
$startOffset = $this->_reader->getOffset();
$this->_header = new ID3_Header($this->_reader, $options);
if ($this->_header->getVersion() < 3 || $this->_header->getVersion() > 4)
throw new ID3_Exception
@@ -146,7 +148,7 @@ final class ID3v2
$offset = $this->_reader->getOffset();
// Jump off the loop if we reached the end of the tag
if ($offset - 10 >= $this->_header->getSize() -
if ($offset - $startOffset - 10 >= $this->_header->getSize() -
($this->hasFooter() ? 10 : 0))
break;
@@ -337,9 +339,6 @@ final class ID3v2
*/
public function write($filename = false)
{
if (empty($this->_frames))
throw new ID3_Exception("Tag must contain at least one frame");
if ($filename === false && ($filename = $this->_filename) === false)
throw new ID3_Exception("No file given to write the tag to");
@@ -349,9 +348,10 @@ final class ID3v2
$oldTagSize = $this->_header->getSize();
$tag = "" . $this;
$tagSize = strlen($tag);
$tagSize = empty($this->_frames) ? 0 : strlen($tag);
if ($this->_reader === null || $tagSize - 10 > $oldTagSize) {
if ($this->_reader === null ||
$tagSize - 10 > $oldTagSize || $tagSize == 0) {
fseek($fd, 0, SEEK_END);
$oldFileSize = ftell($fd);
ftruncate($fd, $newFileSize = $tagSize - $oldTagSize + $oldFileSize);
@@ -363,7 +363,8 @@ final class ID3v2
}
}
fseek($fd, 0);
fwrite($fd, $tag);
fwrite($fd, $tag, $tagLength);
fclose($fd);
$this->_filename = $filename;
}
@@ -393,6 +394,23 @@ final class ID3v2
throw new ID3_Exception("Unknown frame/field: " . $name);
}
/**
* Magic function so that isset($obj->value) will work. This method checks
* whether the frame matching the identifier exists.
*
* @param string $name The frame identifier.
* @return boolean
*/
public function __isset($name) { return isset($this->_boxes[$name]); }
/**
* Magic function so that unset($obj->value) will work. This method removes
* all the frames matching the identifier.
*
* @param string $name The frame identifier.
*/
public function __unset($name) { unset($this->_boxes[$name]); }
/**
* Returns the tag raw data.
*

View File

@@ -263,7 +263,7 @@ class ISO14496_Box
if ($size == 0)
$size = $this->_reader->getSize() - $offset;
if (preg_match("/^\xa9?[a-z]{3,4}$/i", $type) &&
if (preg_match("/^\xa9?[a-z0-9]{3,4}$/i", $type) &&
substr($base, 0, min(strlen($base), strlen
($tmp = $path . ($path ? "." : "") . $type))) ==
substr($tmp, 0, min(strlen($base), strlen($tmp))))
@@ -287,14 +287,6 @@ class ISO14496_Box
array_pop(self::$_path);
}
protected function getPath()
{
$path = "";
echo "box: " .$this->getType() . ", parent: " . $this->getParent()."\n";
for ($child = $this; ($parent = $child->getParent()) !== null; $child = $parent){echo "parent found";$path = $parent->getType() . "." . $path;};
return $path;
}
/**
* Checks whether the box given as an argument is present in the file. Returns
* <var>true</var> if one or more boxes are present, <var>false</var>

View File

@@ -124,8 +124,8 @@ final class ISO14496_Box_ID32 extends ISO14496_Box_Full
{
return parent::__toString
(Transform::toUInt16BE
(((ord($language[0]) - 0x60) << 10) |
((ord($language[1]) - 0x60) << 5) |
ord($language[2]) - 0x60) . $this->_tag);
(((ord($this->_language[0]) - 0x60) << 10) |
((ord($this->_language[1]) - 0x60) << 5) |
ord($this->_language[2]) - 0x60) . $this->_tag);
}
}