Exemplo n.º 1
0
 /**
  * Constructs the ID3v2 class with given file and options. The options array
  * may also be given as the only parameter.
  *
  * The following options are currently recognized:
  *   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 
  *     version 4.0 for tag write.
  *
  * @todo  Only limited subset of flags are processed.
  * @todo  Utilize the SEEK frame and search for a footer to find the tag
  * @todo  Utilize the LINK frame to fetch frames from other sources
  * @param string $filename The path to the file.
  * @param Array  $options  The options array.
  */
 public function __construct($filename = false, $options = array())
 {
     if (is_array($filename)) {
         $options = $filename;
         $filename = false;
     }
     $this->_options =& $options;
     if (($this->_filename = $filename) === false || file_exists($filename) === false) {
         $this->_header = new ID3_Header(null, $options);
     } else {
         $this->_reader = new Reader($filename);
         if ($this->_reader->readString8(3) != "ID3") {
             throw new ID3_Exception("File does not contain ID3v2 tag: " . $filename);
         }
         $this->_header = new ID3_Header($this->_reader, $options);
         if ($this->_header->getVersion() < 3 || $this->_header->getVersion() > 4) {
             throw new ID3_Exception("File does not contain ID3v2 tag of supported version: " . $filename);
         }
         if ($this->_header->hasFlag(ID3_Header::EXTENDEDHEADER)) {
             $this->_extendedHeader = new ID3_ExtendedHeader($this->_reader, $options);
         }
         if ($this->_header->hasFlag(ID3_Header::FOOTER)) {
             $this->_footer =& $this->_header;
         }
         // skip footer, and rather copy header
         while (true) {
             $offset = $this->_reader->getOffset();
             // Jump off the loop if we reached the end of the tag
             if ($offset - 10 >= $this->_header->getSize() - ($this->hasFooter() ? 10 : 0)) {
                 break;
             }
             // Jump off the loop if we reached the last frame
             if ($this->_reader->available() < 4 || Transform::fromUInt32BE($identifier = $this->_reader->read(4)) == 0) {
                 break;
             }
             $this->_reader->setOffset($offset);
             if (@fopen($filename = "ID3/Frame/" . strtoupper($identifier) . ".php", "r", true) !== false) {
                 require_once $filename;
             }
             if (class_exists($classname = "ID3_Frame_" . $identifier)) {
                 $frame = new $classname($this->_reader, $options);
             } else {
                 $frame = new ID3_Frame($this->_reader, $options);
             }
             if (!isset($this->_frames[$frame->getIdentifier()])) {
                 $this->_frames[$frame->getIdentifier()] = array();
             }
             $this->_frames[$frame->getIdentifier()][] = $frame;
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Checks whether there are objects left in the stream. Returns
  * <var>true</var> if there are objects left in the stream, <var>false</var>
  * otherwise.
  * 
  * @return boolean
  */
 public function hasObjects()
 {
     return $this->_reader->available();
 }
Exemplo n.º 3
0
  /**
   * Constructs the ID3v2 class with given file and options. The options array
   * may also be given as the only parameter.
   *
   * The following options are currently recognized:
   *   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
   *     version 4.0 for tag write.
   *   o readonly -- Indicates that the tag is read from a temporary file or
   *     another source it cannot be written back to. The tag can, however,
   *     still be written to another file.
   *
   * @todo  Only limited subset of flags are processed.
   * @todo  Utilize the SEEK frame and search for a footer to find the tag
   * @todo  Utilize the LINK frame to fetch frames from other sources
   * @param string|Reader $filename The path to the file, file descriptor of an
   *                                opened file, or {@link Reader} instance.
   * @param Array         $options  The options array.
   */
  public function __construct($filename = false, $options = array())
  {
    if (is_array($filename)) {
      $options = $filename;
      $filename = false;
    }

    $this->_options = &$options;
    if ($filename === false ||
        (is_string($filename) && file_exists($filename) === false) ||
        (is_resource($filename) && 
         in_array(get_resource_type($filename), array("file", "stream")))) {
      $this->_header = new ID3_Header(null, $options);
    } else {
      if (is_string($filename) && !isset($options["readonly"]))
        $this->_filename = $filename;
      if ($filename instanceof Reader)
        $this->_reader = &$filename;
      else
        $this->_reader = new Reader($filename);
      if ($this->_reader->readString8(3) != "ID3")
        throw new ID3_Exception("File does not contain ID3v2 tag");
      
      $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
          ("File does not contain ID3v2 tag of supported version");
      if ($this->_header->getVersion() < 4 &&
          $this->_header->hasFlag(ID3_Header::UNSYNCHRONISATION))
        throw new ID3_Exception
          ("Unsynchronisation not supported for this version of ID3v2 tag");
      unset($this->_options["unsyncronisation"]);
      if ($this->_header->hasFlag(ID3_Header::UNSYNCHRONISATION))
        $this->_options["unsyncronisation"] = true;
      if ($this->_header->hasFlag(ID3_Header::EXTENDEDHEADER))
        $this->_extendedHeader =
          new ID3_ExtendedHeader($this->_reader, $options);
      if ($this->_header->hasFlag(ID3_Header::FOOTER))
        $this->_footer = &$this->_header; // skip footer, and rather copy header

      while (true) {
        $offset = $this->_reader->getOffset();

        // Jump off the loop if we reached the end of the tag
        if ($offset - $startOffset - 10 >= $this->_header->getSize() -
            ($this->hasFooter() ? 10 : 0))
          break;

        // Jump off the loop if we reached the last frame
        if ($this->_reader->available() < 4 || Transform::fromUInt32BE
            ($identifier = $this->_reader->read(4)) == 0)
          break;
        $this->_reader->setOffset($offset);
        
        if (@fopen($filename = "ID3/Frame/" .
                   strtoupper($identifier) . ".php", "r", true) !== false)
          require_once($filename);
        if (class_exists($classname = "ID3_Frame_" . $identifier))
          $frame = new $classname($this->_reader, $options);
        else
          $frame = new ID3_Frame($this->_reader, $options);

        if (!isset($this->_frames[$frame->getIdentifier()]))
          $this->_frames[$frame->getIdentifier()] = array();
        $this->_frames[$frame->getIdentifier()][] = $frame;
      }
    }
  }