예제 #1
0
파일: ID3v2.php 프로젝트: rtdean93/therock
  /**
   * Returns the tag raw data.
   *
   * @return string
   */
  public function __toString()
  {
    unset($this->_options["unsyncronisation"]);
    
    $data = "";
    foreach ($this->_frames as $frames)
      foreach ($frames as $frame)
        $data .= $frame;

    $datalen = strlen($data);
    $padlen = 0;
    
    if (isset($this->_options["unsyncronisation"]) &&
        $this->_options["unsyncronisation"] === true)
      $this->_header->setFlags
        ($this->_header->getFlags() | ID3_Header::UNSYNCHRONISATION);

    /* The tag padding is calculated as follows. If the tag can be written in
       the space of the previous tag, the remaining space is used for padding.
       If there is no previous tag or the new tag is bigger than the space taken
       by the previous tag, the padding is calculated using the following
       logaritmic equation: log(0.2(x + 10)), ranging from some 300 bytes to
       almost 5000 bytes given the tag length of 0..256M. */
    if ($this->hasFooter() === false) {
      if ($this->_reader !== null &&  $datalen < $this->_header->getSize())
        $padlen = $this->_header->getSize() - $datalen;
      else
        $padlen = ceil(log(0.2 * ($datalen / 1024 + 10), 10) * 1024);
    }

    /* ID3v2.4.0 CRC calculated w/ padding */
    if (!isset($this->_options["version"]) || $this->_options["version"] >= 4)
      $data = str_pad($data, $datalen + $padlen, "\0");

    if ($this->hasExtendedHeader()) {
      $this->_extendedHeader->setPadding($padlen);
      if ($this->_extendedHeader->hasFlag(ID3_ExtendedHeader::CRC32)) {
        $crc = crc32($data);
        if ($crc & 0x80000000)
          $crc = -(($crc ^ 0xffffffff) + 1);
        $this->_extendedHeader->setCrc($crc);
      }
      $data = $this->getExtendedHeader() . $data;
    }

    /* ID3v2.3.0 CRC calculated w/o padding */
    if (isset($this->_options["version"]) && $this->_options["version"] < 4)
      $data = str_pad($data, $datalen + $padlen, "\0");

    $this->_header->setSize(strlen($data));

    return "ID3" . $this->_header . $data .
      ($this->hasFooter() ? "3DI" . $this->getFooter() : "");
  }