[ Index ]

PHP Cross Reference of WordPress

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


Generated: Wed Sep 18 01:00:03 2019 Cross-referenced by PHPXref 0.7.1