[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ID3/ -> module.tag.lyrics3.php (source)

   1  <?php
   2  
   3  /////////////////////////////////////////////////////////////////
   4  /// getID3() by James Heinrich <info@getid3.org>               //
   5  //  available at https://github.com/JamesHeinrich/getID3       //
   6  //            or https://www.getid3.org                        //
   7  //            or http://getid3.sourceforge.net                 //
   8  //  see readme.txt for more details                            //
   9  /////////////////////////////////////////////////////////////////
  10  ///                                                            //
  11  // module.tag.lyrics3.php                                      //
  12  // module for analyzing Lyrics3 tags                           //
  13  // dependencies: module.tag.apetag.php (optional)              //
  14  //                                                            ///
  15  /////////////////////////////////////////////////////////////////
  16  
  17  if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  18      exit;
  19  }
  20  class getid3_lyrics3 extends getid3_handler
  21  {
  22      /**
  23       * @return bool
  24       */
  25  	public function Analyze() {
  26          $info = &$this->getid3->info;
  27  
  28          // http://www.volweb.cz/str/tags.htm
  29  
  30          if (!getid3_lib::intValueSupported($info['filesize'])) {
  31              $this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB');
  32              return false;
  33          }
  34  
  35          $scanlength = min(128 + 9 + 6, $info['filesize']); // end - ID3v1 - "LYRICSEND" - [Lyrics3size];
  36          $this->fseek(0 - $scanlength, SEEK_END);
  37          $lyrics3offset = null;
  38          $lyrics3version = null;
  39          $lyrics3size   = null;
  40          $lyrics3_id3v1 = $this->fread($scanlength);
  41          $lyrics3lsz    = (int) substr($lyrics3_id3v1, 0, 6); // Lyrics3size
  42          $lyrics3end    = substr($lyrics3_id3v1,  6,   9); // LYRICSEND or LYRICS200
  43          $id3v1tag      = substr($lyrics3_id3v1, 15, 128); // ID3v1
  44  
  45          if ($lyrics3end == 'LYRICSEND') {
  46              // Lyrics3v1, ID3v1, no APE
  47  
  48              $lyrics3size    = 5100;
  49              $lyrics3offset  = $info['filesize'] - 128 - $lyrics3size;
  50              $lyrics3version = 1;
  51  
  52          } elseif ($lyrics3end == 'LYRICS200') {
  53              // Lyrics3v2, ID3v1, no APE
  54  
  55              // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200'
  56              $lyrics3size    = $lyrics3lsz + 6 + strlen('LYRICS200');
  57              $lyrics3offset  = $info['filesize'] - 128 - $lyrics3size;
  58              $lyrics3version = 2;
  59  
  60          } elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICSEND')) {
  61              // Lyrics3v1, no ID3v1, no APE
  62  
  63              $lyrics3size    = 5100;
  64              $lyrics3offset  = $info['filesize'] - $lyrics3size;
  65              $lyrics3version = 1;
  66              $lyrics3offset  = $info['filesize'] - $lyrics3size;
  67  
  68          } elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICS200')) {
  69  
  70              // Lyrics3v2, no ID3v1, no APE
  71  
  72              $lyrics3size    = (int) strrev(substr(strrev($lyrics3_id3v1), 9, 6)) + 6 + strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200'
  73              $lyrics3offset  = $info['filesize'] - $lyrics3size;
  74              $lyrics3version = 2;
  75  
  76          } else {
  77  
  78              if (isset($info['ape']['tag_offset_start']) && ($info['ape']['tag_offset_start'] > 15)) {
  79  
  80                  $this->fseek($info['ape']['tag_offset_start'] - 15);
  81                  $lyrics3lsz = $this->fread(6);
  82                  $lyrics3end = $this->fread(9);
  83  
  84                  if ($lyrics3end == 'LYRICSEND') {
  85                      // Lyrics3v1, APE, maybe ID3v1
  86  
  87                      $lyrics3size    = 5100;
  88                      $lyrics3offset  = $info['ape']['tag_offset_start'] - $lyrics3size;
  89                      $info['avdataend'] = $lyrics3offset;
  90                      $lyrics3version = 1;
  91                      $this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability');
  92  
  93                  } elseif ($lyrics3end == 'LYRICS200') {
  94                      // Lyrics3v2, APE, maybe ID3v1
  95  
  96                      $lyrics3size    = $lyrics3lsz + 6 + strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200'
  97                      $lyrics3offset  = $info['ape']['tag_offset_start'] - $lyrics3size;
  98                      $lyrics3version = 2;
  99                      $this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability');
 100  
 101                  }
 102  
 103              }
 104  
 105          }
 106  
 107          if (isset($lyrics3offset) && isset($lyrics3version) && isset($lyrics3size)) {
 108              $info['avdataend'] = $lyrics3offset;
 109              $this->getLyrics3Data($lyrics3offset, $lyrics3version, $lyrics3size);
 110  
 111              if (!isset($info['ape'])) {
 112                  if (isset($info['lyrics3']['tag_offset_start'])) {
 113                      $GETID3_ERRORARRAY = &$info['warning'];
 114                      if ($this->getid3->option_tag_apetag) {
 115                          getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.apetag.php', __FILE__, true);
 116                          $getid3_temp = new getID3();
 117                          $getid3_temp->openfile($this->getid3->filename, $this->getid3->info['filesize'], $this->getid3->fp);
 118                          $getid3_apetag = new getid3_apetag($getid3_temp);
 119                          $getid3_apetag->overrideendoffset = $info['lyrics3']['tag_offset_start'];
 120                          $getid3_apetag->Analyze();
 121                          if (!empty($getid3_temp->info['ape'])) {
 122                              $info['ape'] = $getid3_temp->info['ape'];
 123                          }
 124                          if (!empty($getid3_temp->info['replay_gain'])) {
 125                              $info['replay_gain'] = $getid3_temp->info['replay_gain'];
 126                          }
 127                          unset($getid3_temp, $getid3_apetag);
 128                      } else {
 129                          $this->warning('Unable to check for Lyrics3 and APE tags interaction since option_tag_apetag=FALSE');
 130                      }
 131                  } else {
 132                      $this->warning('Lyrics3 and APE tags appear to have become entangled (most likely due to updating the APE tags with a non-Lyrics3-aware tagger)');
 133                  }
 134              }
 135  
 136          }
 137  
 138          return true;
 139      }
 140  
 141      /**
 142       * @param int $endoffset
 143       * @param int $version
 144       * @param int $length
 145       *
 146       * @return bool
 147       */
 148  	public function getLyrics3Data($endoffset, $version, $length) {
 149          // http://www.volweb.cz/str/tags.htm
 150  
 151          $info = &$this->getid3->info;
 152  
 153          if (!getid3_lib::intValueSupported($endoffset)) {
 154              $this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB');
 155              return false;
 156          }
 157  
 158          $this->fseek($endoffset);
 159          if ($length <= 0) {
 160              return false;
 161          }
 162          $rawdata = $this->fread($length);
 163  
 164          $ParsedLyrics3 = array();
 165  
 166          $ParsedLyrics3['raw']['lyrics3version'] = $version;
 167          $ParsedLyrics3['raw']['lyrics3tagsize'] = $length;
 168          $ParsedLyrics3['tag_offset_start']      = $endoffset;
 169          $ParsedLyrics3['tag_offset_end']        = $endoffset + $length - 1;
 170  
 171          if (substr($rawdata, 0, 11) != 'LYRICSBEGIN') {
 172              if (strpos($rawdata, 'LYRICSBEGIN') !== false) {
 173  
 174                  $this->warning('"LYRICSBEGIN" expected at '.$endoffset.' but actually found at '.($endoffset + strpos($rawdata, 'LYRICSBEGIN')).' - this is invalid for Lyrics3 v'.$version);
 175                  $info['avdataend'] = $endoffset + strpos($rawdata, 'LYRICSBEGIN');
 176                  $rawdata = substr($rawdata, strpos($rawdata, 'LYRICSBEGIN'));
 177                  $length = strlen($rawdata);
 178                  $ParsedLyrics3['tag_offset_start'] = $info['avdataend'];
 179                  $ParsedLyrics3['raw']['lyrics3tagsize'] = $length;
 180  
 181              } else {
 182  
 183                  $this->error('"LYRICSBEGIN" expected at '.$endoffset.' but found "'.substr($rawdata, 0, 11).'" instead');
 184                  return false;
 185  
 186              }
 187  
 188          }
 189  
 190          switch ($version) {
 191  
 192              case 1:
 193                  if (substr($rawdata, strlen($rawdata) - 9, 9) == 'LYRICSEND') {
 194                      $ParsedLyrics3['raw']['LYR'] = trim(substr($rawdata, 11, strlen($rawdata) - 11 - 9));
 195                      $this->Lyrics3LyricsTimestampParse($ParsedLyrics3);
 196                  } else {
 197                      $this->error('"LYRICSEND" expected at '.($this->ftell() - 11 + $length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead');
 198                      return false;
 199                  }
 200                  break;
 201  
 202              case 2:
 203                  if (substr($rawdata, strlen($rawdata) - 9, 9) == 'LYRICS200') {
 204                      $ParsedLyrics3['raw']['unparsed'] = substr($rawdata, 11, strlen($rawdata) - 11 - 9 - 6); // LYRICSBEGIN + LYRICS200 + LSZ
 205                      $rawdata = $ParsedLyrics3['raw']['unparsed'];
 206                      while (strlen($rawdata) > 0) {
 207                          $fieldname = substr($rawdata, 0, 3);
 208                          $fieldsize = (int) substr($rawdata, 3, 5);
 209                          $ParsedLyrics3['raw'][$fieldname] = substr($rawdata, 8, $fieldsize);
 210                          $rawdata = substr($rawdata, 3 + 5 + $fieldsize);
 211                      }
 212  
 213                      if (isset($ParsedLyrics3['raw']['IND'])) {
 214                          $i = 0;
 215                          $flagnames = array('lyrics', 'timestamps', 'inhibitrandom');
 216                          foreach ($flagnames as $flagname) {
 217                              if (strlen($ParsedLyrics3['raw']['IND']) > $i++) {
 218                                  $ParsedLyrics3['flags'][$flagname] = $this->IntString2Bool(substr($ParsedLyrics3['raw']['IND'], $i, 1 - 1));
 219                              }
 220                          }
 221                      }
 222  
 223                      $fieldnametranslation = array('ETT'=>'title', 'EAR'=>'artist', 'EAL'=>'album', 'INF'=>'comment', 'AUT'=>'author');
 224                      foreach ($fieldnametranslation as $key => $value) {
 225                          if (isset($ParsedLyrics3['raw'][$key])) {
 226                              $ParsedLyrics3['comments'][$value][] = trim($ParsedLyrics3['raw'][$key]);
 227                          }
 228                      }
 229  
 230                      if (isset($ParsedLyrics3['raw']['IMG'])) {
 231                          $imagestrings = explode("\r\n", $ParsedLyrics3['raw']['IMG']);
 232                          foreach ($imagestrings as $key => $imagestring) {
 233                              if (strpos($imagestring, '||') !== false) {
 234                                  $imagearray = explode('||', $imagestring);
 235                                  $ParsedLyrics3['images'][$key]['filename']     =                                $imagearray[0];
 236                                  $ParsedLyrics3['images'][$key]['description']  =                                (isset($imagearray[1]) ? $imagearray[1] : '');
 237                                  $ParsedLyrics3['images'][$key]['timestamp']    = $this->Lyrics3Timestamp2Seconds(isset($imagearray[2]) ? $imagearray[2] : '');
 238                              }
 239                          }
 240                      }
 241                      if (isset($ParsedLyrics3['raw']['LYR'])) {
 242                          $this->Lyrics3LyricsTimestampParse($ParsedLyrics3);
 243                      }
 244                  } else {
 245                      $this->error('"LYRICS200" expected at '.($this->ftell() - 11 + $length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead');
 246                      return false;
 247                  }
 248                  break;
 249  
 250              default:
 251                  $this->error('Cannot process Lyrics3 version '.$version.' (only v1 and v2)');
 252                  return false;
 253          }
 254  
 255  
 256          if (isset($info['id3v1']['tag_offset_start']) && ($info['id3v1']['tag_offset_start'] <= $ParsedLyrics3['tag_offset_end'])) {
 257              $this->warning('ID3v1 tag information ignored since it appears to be a false synch in Lyrics3 tag data');
 258              unset($info['id3v1']);
 259              foreach ($info['warning'] as $key => $value) {
 260                  if ($value == 'Some ID3v1 fields do not use NULL characters for padding') {
 261                      unset($info['warning'][$key]);
 262                      sort($info['warning']);
 263                      break;
 264                  }
 265              }
 266          }
 267  
 268          $info['lyrics3'] = $ParsedLyrics3;
 269  
 270          return true;
 271      }
 272  
 273      /**
 274       * @param string $rawtimestamp
 275       *
 276       * @return int|false
 277       */
 278  	public function Lyrics3Timestamp2Seconds($rawtimestamp) {
 279          if (preg_match('#^\\[([0-9]{2}):([0-9]{2})\\]$#', $rawtimestamp, $regs)) {
 280              return (int) (((int) $regs[1] * 60) + (int) $regs[2]);
 281          }
 282          return false;
 283      }
 284  
 285      /**
 286       * @param array $Lyrics3data
 287       *
 288       * @return bool
 289       */
 290  	public function Lyrics3LyricsTimestampParse(&$Lyrics3data) {
 291          $lyricsarray = explode("\r\n", $Lyrics3data['raw']['LYR']);
 292          $notimestamplyricsarray = array();
 293          foreach ($lyricsarray as $key => $lyricline) {
 294              $regs = array();
 295              $thislinetimestamps = array();
 296              while (preg_match('#^(\\[[0-9]{2}:[0-9]{2}\\])#', $lyricline, $regs)) {
 297                  $thislinetimestamps[] = $this->Lyrics3Timestamp2Seconds($regs[0]);
 298                  $lyricline = str_replace($regs[0], '', $lyricline);
 299              }
 300              $notimestamplyricsarray[$key] = $lyricline;
 301              if (count($thislinetimestamps) > 0) {
 302                  sort($thislinetimestamps);
 303                  foreach ($thislinetimestamps as $timestampkey => $timestamp) {
 304                      if (isset($Lyrics3data['comments']['synchedlyrics'][$timestamp])) {
 305                          // timestamps only have a 1-second resolution, it's possible that multiple lines
 306                          // could have the same timestamp, if so, append
 307                          $Lyrics3data['comments']['synchedlyrics'][$timestamp] .= "\r\n".$lyricline;
 308                      } else {
 309                          $Lyrics3data['comments']['synchedlyrics'][$timestamp] = $lyricline;
 310                      }
 311                  }
 312              }
 313          }
 314          $Lyrics3data['comments']['unsynchedlyrics'][0] = implode("\r\n", $notimestamplyricsarray);
 315          if (isset($Lyrics3data['comments']['synchedlyrics']) && is_array($Lyrics3data['comments']['synchedlyrics'])) {
 316              ksort($Lyrics3data['comments']['synchedlyrics']);
 317          }
 318          return true;
 319      }
 320  
 321      /**
 322       * @param string $char
 323       *
 324       * @return bool|null
 325       */
 326  	public function IntString2Bool($char) {
 327          if ($char == '1') {
 328              return true;
 329          } elseif ($char == '0') {
 330              return false;
 331          }
 332          return null;
 333      }
 334  }


Generated : Tue Sep 22 08:20:31 2026 Cross-referenced by PHPXref