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


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1