Exemple #1
0
 /**
  * @return string
  */
 public function GenerateCONTchunk()
 {
     foreach ($this->tag_data as $key => $value) {
         // limit each value to 0xFFFF bytes
         $this->tag_data[$key] = substr($value, 0, 65535);
     }
     $CONTchunk = "";
     // object version
     $CONTchunk .= Helper::BigEndian2String(!empty($this->tag_data['title']) ? strlen($this->tag_data['title']) : 0, 2);
     $CONTchunk .= !empty($this->tag_data['title']) ? strlen($this->tag_data['title']) : '';
     $CONTchunk .= Helper::BigEndian2String(!empty($this->tag_data['artist']) ? strlen($this->tag_data['artist']) : 0, 2);
     $CONTchunk .= !empty($this->tag_data['artist']) ? strlen($this->tag_data['artist']) : '';
     $CONTchunk .= Helper::BigEndian2String(!empty($this->tag_data['copyright']) ? strlen($this->tag_data['copyright']) : 0, 2);
     $CONTchunk .= !empty($this->tag_data['copyright']) ? strlen($this->tag_data['copyright']) : '';
     $CONTchunk .= Helper::BigEndian2String(!empty($this->tag_data['comment']) ? strlen($this->tag_data['comment']) : 0, 2);
     $CONTchunk .= !empty($this->tag_data['comment']) ? strlen($this->tag_data['comment']) : '';
     if ($this->paddedlength > strlen($CONTchunk) + 8) {
         $CONTchunk .= str_repeat("", $this->paddedlength - strlen($CONTchunk) - 8);
     }
     $CONTchunk = 'CONT' . Helper::BigEndian2String(strlen($CONTchunk) + 8, 4) . $CONTchunk;
     // CONT chunk identifier + chunk length
     return $CONTchunk;
 }
Exemple #2
0
 /**
  * @param  type    $noerrorsonly
  *
  * @return bool
  */
 public function GenerateID3v2Tag($noerrorsonly = true)
 {
     $this->ID3v2FrameIsAllowed(null, '');
     // clear static array in case this isn't the first call to $this->GenerateID3v2Tag()
     $tagstring = '';
     if (is_array($this->tag_data)) {
         foreach ($this->tag_data as $frame_name => $frame_rawinputdata) {
             foreach ($frame_rawinputdata as $irrelevantindex => $source_data_array) {
                 if (Tag\Id3v2::IsValidID3v2FrameName($frame_name, $this->majorversion)) {
                     unset($frame_length);
                     unset($frame_flags);
                     $frame_data = false;
                     if ($this->ID3v2FrameIsAllowed($frame_name, $source_data_array)) {
                         if ($frame_data = $this->GenerateID3v2FrameData($frame_name, $source_data_array)) {
                             $FrameUnsynchronisation = false;
                             if ($this->majorversion >= 4) {
                                 // frame-level unsynchronisation
                                 $unsynchdata = $frame_data;
                                 if ($this->id3v2_use_unsynchronisation) {
                                     $unsynchdata = $this->Unsynchronise($frame_data);
                                 }
                                 if (strlen($unsynchdata) != strlen($frame_data)) {
                                     // unsynchronisation needed
                                     $FrameUnsynchronisation = true;
                                     $frame_data = $unsynchdata;
                                     if (isset($TagUnsynchronisation) && $TagUnsynchronisation === false) {
                                         // only set to true if ALL frames are unsynchronised
                                     } else {
                                         $TagUnsynchronisation = true;
                                     }
                                 } else {
                                     if (isset($TagUnsynchronisation)) {
                                         $TagUnsynchronisation = false;
                                     }
                                 }
                                 unset($unsynchdata);
                                 $frame_length = Helper::BigEndian2String(strlen($frame_data), 4, true);
                             } else {
                                 $frame_length = Helper::BigEndian2String(strlen($frame_data), 4, false);
                             }
                             $frame_flags = $this->GenerateID3v2FrameFlags($this->ID3v2FrameFlagsLookupTagAlter($frame_name), $this->ID3v2FrameFlagsLookupFileAlter($frame_name), false, false, false, false, $FrameUnsynchronisation, false);
                         }
                     } else {
                         $this->errors[] = 'Frame "' . $frame_name . '" is NOT allowed';
                     }
                     if ($frame_data === false) {
                         $this->errors[] = '$this->GenerateID3v2FrameData() failed for "' . $frame_name . '"';
                         if ($noerrorsonly) {
                             return false;
                         } else {
                             unset($frame_name);
                         }
                     }
                 } else {
                     // ignore any invalid frame names, including 'title', 'header', etc
                     $this->warnings[] = 'Ignoring invalid ID3v2 frame type: "' . $frame_name . '"';
                     unset($frame_name);
                     unset($frame_length);
                     unset($frame_flags);
                     unset($frame_data);
                 }
                 if (isset($frame_name) && isset($frame_length) && isset($frame_flags) && isset($frame_data)) {
                     $tagstring .= $frame_name . $frame_length . $frame_flags . $frame_data;
                 }
             }
         }
         if (!isset($TagUnsynchronisation)) {
             $TagUnsynchronisation = false;
         }
         if ($this->majorversion <= 3 && $this->id3v2_use_unsynchronisation) {
             // tag-level unsynchronisation
             $unsynchdata = $this->Unsynchronise($tagstring);
             if (strlen($unsynchdata) != strlen($tagstring)) {
                 // unsynchronisation needed
                 $TagUnsynchronisation = true;
                 $tagstring = $unsynchdata;
             }
         }
         while ($this->paddedlength < strlen($tagstring) + Tag\Id3v2::ID3v2HeaderLength($this->majorversion)) {
             $this->paddedlength += 1024;
         }
         $footer = false;
         // ID3v2 footers not yet supported in GetId3Core()
         if (!$footer && $this->paddedlength > strlen($tagstring) + Tag\Id3v2::ID3v2HeaderLength($this->majorversion)) {
             // pad up to $paddedlength bytes if unpadded tag is shorter than $paddedlength
             // "Furthermore it MUST NOT have any padding when a tag footer is added to the tag."
             if ($this->paddedlength - strlen($tagstring) - Tag\Id3v2::ID3v2HeaderLength($this->majorversion) > 0) {
                 $tagstring .= str_repeat("", $this->paddedlength - strlen($tagstring) - Tag\Id3v2::ID3v2HeaderLength($this->majorversion));
             }
         }
         if ($this->id3v2_use_unsynchronisation && substr($tagstring, strlen($tagstring) - 1, 1) == "ÿ") {
             // special unsynchronisation case:
             // if last byte == $FF then appended a $00
             $TagUnsynchronisation = true;
             $tagstring .= "";
         }
         $tagheader = 'ID3';
         $tagheader .= chr($this->majorversion);
         $tagheader .= chr($this->minorversion);
         $tagheader .= $this->GenerateID3v2TagFlags(array('unsynchronisation' => $TagUnsynchronisation));
         $tagheader .= Helper::BigEndian2String(strlen($tagstring), 4, true);
         return $tagheader . $tagstring;
     }
     $this->errors[] = 'tag_data is not an array in GenerateID3v2Tag()';
     return false;
 }