[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ID3/ -> module.tag.id3v1.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.id3v1.php                                        //
  12  // module for analyzing ID3v1 tags                             //
  13  // dependencies: NONE                                          //
  14  //                                                            ///
  15  /////////////////////////////////////////////////////////////////
  16  
  17  
  18  class getid3_id3v1 extends getid3_handler
  19  {
  20      /**
  21       * @return bool
  22       */
  23  	public function Analyze() {
  24          $info = &$this->getid3->info;
  25  
  26          if (!getid3_lib::intValueSupported($info['filesize'])) {
  27              $this->warning('Unable to check for ID3v1 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB');
  28              return false;
  29          }
  30  
  31          $this->fseek(-256, SEEK_END);
  32          $preid3v1 = $this->fread(128);
  33          $id3v1tag = $this->fread(128);
  34  
  35          if (substr($id3v1tag, 0, 3) == 'TAG') {
  36  
  37              $info['avdataend'] = $info['filesize'] - 128;
  38  
  39              $ParsedID3v1['title']   = $this->cutfield(substr($id3v1tag,   3, 30));
  40              $ParsedID3v1['artist']  = $this->cutfield(substr($id3v1tag,  33, 30));
  41              $ParsedID3v1['album']   = $this->cutfield(substr($id3v1tag,  63, 30));
  42              $ParsedID3v1['year']    = $this->cutfield(substr($id3v1tag,  93,  4));
  43              $ParsedID3v1['comment'] =                 substr($id3v1tag,  97, 30);  // can't remove nulls yet, track detection depends on them
  44              $ParsedID3v1['genreid'] =             ord(substr($id3v1tag, 127,  1));
  45  
  46              // If second-last byte of comment field is null and last byte of comment field is non-null
  47              // then this is ID3v1.1 and the comment field is 28 bytes long and the 30th byte is the track number
  48              if (($id3v1tag[125] === "\x00") && ($id3v1tag[126] !== "\x00")) {
  49                  $ParsedID3v1['track_number'] = ord(substr($ParsedID3v1['comment'], 29,  1));
  50                  $ParsedID3v1['comment']      =     substr($ParsedID3v1['comment'],  0, 28);
  51              }
  52              $ParsedID3v1['comment'] = $this->cutfield($ParsedID3v1['comment']);
  53  
  54              $ParsedID3v1['genre'] = $this->LookupGenreName($ParsedID3v1['genreid']);
  55              if (!empty($ParsedID3v1['genre'])) {
  56                  unset($ParsedID3v1['genreid']);
  57              }
  58              if (isset($ParsedID3v1['genre']) && (empty($ParsedID3v1['genre']) || ($ParsedID3v1['genre'] == 'Unknown'))) {
  59                  unset($ParsedID3v1['genre']);
  60              }
  61  
  62              foreach ($ParsedID3v1 as $key => $value) {
  63                  $ParsedID3v1['comments'][$key][0] = $value;
  64              }
  65              // ID3v1 encoding detection hack START
  66              // ID3v1 is defined as always using ISO-8859-1 encoding, but it is not uncommon to find files tagged with ID3v1 using Windows-1251 or other character sets
  67              // Since ID3v1 has no concept of character sets there is no certain way to know we have the correct non-ISO-8859-1 character set, but we can guess
  68              $ID3v1encoding = 'ISO-8859-1';
  69              foreach ($ParsedID3v1['comments'] as $tag_key => $valuearray) {
  70                  foreach ($valuearray as $key => $value) {
  71                      if (preg_match('#^[\\x00-\\x40\\xA8\\xB8\\x80-\\xFF]+$#', $value)) {
  72                          foreach (array('Windows-1251', 'KOI8-R') as $id3v1_bad_encoding) {
  73                              if (function_exists('mb_convert_encoding') && @mb_convert_encoding($value, $id3v1_bad_encoding, $id3v1_bad_encoding) === $value) {
  74                                  $ID3v1encoding = $id3v1_bad_encoding;
  75                                  break 3;
  76                              } elseif (function_exists('iconv') && @iconv($id3v1_bad_encoding, $id3v1_bad_encoding, $value) === $value) {
  77                                  $ID3v1encoding = $id3v1_bad_encoding;
  78                                  break 3;
  79                              }
  80                          }
  81                      }
  82                  }
  83              }
  84              // ID3v1 encoding detection hack END
  85  
  86              // ID3v1 data is supposed to be padded with NULL characters, but some taggers pad with spaces
  87              $GoodFormatID3v1tag = $this->GenerateID3v1Tag(
  88                                              $ParsedID3v1['title'],
  89                                              $ParsedID3v1['artist'],
  90                                              $ParsedID3v1['album'],
  91                                              $ParsedID3v1['year'],
  92                                              (isset($ParsedID3v1['genre']) ? $this->LookupGenreID($ParsedID3v1['genre']) : false),
  93                                              $ParsedID3v1['comment'],
  94                                              (!empty($ParsedID3v1['track_number']) ? $ParsedID3v1['track_number'] : ''));
  95              $ParsedID3v1['padding_valid'] = true;
  96              if ($id3v1tag !== $GoodFormatID3v1tag) {
  97                  $ParsedID3v1['padding_valid'] = false;
  98                  $this->warning('Some ID3v1 fields do not use NULL characters for padding');
  99              }
 100  
 101              $ParsedID3v1['tag_offset_end']   = $info['filesize'];
 102              $ParsedID3v1['tag_offset_start'] = $ParsedID3v1['tag_offset_end'] - 128;
 103  
 104              $info['id3v1'] = $ParsedID3v1;
 105              $info['id3v1']['encoding'] = $ID3v1encoding;
 106          }
 107  
 108          if (substr($preid3v1, 0, 3) == 'TAG') {
 109              // The way iTunes handles tags is, well, brain-damaged.
 110              // It completely ignores v1 if ID3v2 is present.
 111              // This goes as far as adding a new v1 tag *even if there already is one*
 112  
 113              // A suspected double-ID3v1 tag has been detected, but it could be that
 114              // the "TAG" identifier is a legitimate part of an APE or Lyrics3 tag
 115              if (substr($preid3v1, 96, 8) == 'APETAGEX') {
 116                  // an APE tag footer was found before the last ID3v1, assume false "TAG" synch
 117              } elseif (substr($preid3v1, 119, 6) == 'LYRICS') {
 118                  // a Lyrics3 tag footer was found before the last ID3v1, assume false "TAG" synch
 119              } else {
 120                  // APE and Lyrics3 footers not found - assume double ID3v1
 121                  $this->warning('Duplicate ID3v1 tag detected - this has been known to happen with iTunes');
 122                  $info['avdataend'] -= 128;
 123              }
 124          }
 125  
 126          return true;
 127      }
 128  
 129      /**
 130       * @param string $str
 131       *
 132       * @return string
 133       */
 134  	public static function cutfield($str) {
 135          return trim(substr($str, 0, strcspn($str, "\x00")));
 136      }
 137  
 138      /**
 139       * @param bool $allowSCMPXextended
 140       *
 141       * @return string[]
 142       */
 143  	public static function ArrayOfGenres($allowSCMPXextended=false) {
 144          static $GenreLookup = array(
 145              0    => 'Blues',
 146              1    => 'Classic Rock',
 147              2    => 'Country',
 148              3    => 'Dance',
 149              4    => 'Disco',
 150              5    => 'Funk',
 151              6    => 'Grunge',
 152              7    => 'Hip-Hop',
 153              8    => 'Jazz',
 154              9    => 'Metal',
 155              10   => 'New Age',
 156              11   => 'Oldies',
 157              12   => 'Other',
 158              13   => 'Pop',
 159              14   => 'R&B',
 160              15   => 'Rap',
 161              16   => 'Reggae',
 162              17   => 'Rock',
 163              18   => 'Techno',
 164              19   => 'Industrial',
 165              20   => 'Alternative',
 166              21   => 'Ska',
 167              22   => 'Death Metal',
 168              23   => 'Pranks',
 169              24   => 'Soundtrack',
 170              25   => 'Euro-Techno',
 171              26   => 'Ambient',
 172              27   => 'Trip-Hop',
 173              28   => 'Vocal',
 174              29   => 'Jazz+Funk',
 175              30   => 'Fusion',
 176              31   => 'Trance',
 177              32   => 'Classical',
 178              33   => 'Instrumental',
 179              34   => 'Acid',
 180              35   => 'House',
 181              36   => 'Game',
 182              37   => 'Sound Clip',
 183              38   => 'Gospel',
 184              39   => 'Noise',
 185              40   => 'Alt. Rock',
 186              41   => 'Bass',
 187              42   => 'Soul',
 188              43   => 'Punk',
 189              44   => 'Space',
 190              45   => 'Meditative',
 191              46   => 'Instrumental Pop',
 192              47   => 'Instrumental Rock',
 193              48   => 'Ethnic',
 194              49   => 'Gothic',
 195              50   => 'Darkwave',
 196              51   => 'Techno-Industrial',
 197              52   => 'Electronic',
 198              53   => 'Pop-Folk',
 199              54   => 'Eurodance',
 200              55   => 'Dream',
 201              56   => 'Southern Rock',
 202              57   => 'Comedy',
 203              58   => 'Cult',
 204              59   => 'Gangsta Rap',
 205              60   => 'Top 40',
 206              61   => 'Christian Rap',
 207              62   => 'Pop/Funk',
 208              63   => 'Jungle',
 209              64   => 'Native American',
 210              65   => 'Cabaret',
 211              66   => 'New Wave',
 212              67   => 'Psychedelic',
 213              68   => 'Rave',
 214              69   => 'Showtunes',
 215              70   => 'Trailer',
 216              71   => 'Lo-Fi',
 217              72   => 'Tribal',
 218              73   => 'Acid Punk',
 219              74   => 'Acid Jazz',
 220              75   => 'Polka',
 221              76   => 'Retro',
 222              77   => 'Musical',
 223              78   => 'Rock & Roll',
 224              79   => 'Hard Rock',
 225              80   => 'Folk',
 226              81   => 'Folk/Rock',
 227              82   => 'National Folk',
 228              83   => 'Swing',
 229              84   => 'Fast-Fusion',
 230              85   => 'Bebob',
 231              86   => 'Latin',
 232              87   => 'Revival',
 233              88   => 'Celtic',
 234              89   => 'Bluegrass',
 235              90   => 'Avantgarde',
 236              91   => 'Gothic Rock',
 237              92   => 'Progressive Rock',
 238              93   => 'Psychedelic Rock',
 239              94   => 'Symphonic Rock',
 240              95   => 'Slow Rock',
 241              96   => 'Big Band',
 242              97   => 'Chorus',
 243              98   => 'Easy Listening',
 244              99   => 'Acoustic',
 245              100  => 'Humour',
 246              101  => 'Speech',
 247              102  => 'Chanson',
 248              103  => 'Opera',
 249              104  => 'Chamber Music',
 250              105  => 'Sonata',
 251              106  => 'Symphony',
 252              107  => 'Booty Bass',
 253              108  => 'Primus',
 254              109  => 'Porn Groove',
 255              110  => 'Satire',
 256              111  => 'Slow Jam',
 257              112  => 'Club',
 258              113  => 'Tango',
 259              114  => 'Samba',
 260              115  => 'Folklore',
 261              116  => 'Ballad',
 262              117  => 'Power Ballad',
 263              118  => 'Rhythmic Soul',
 264              119  => 'Freestyle',
 265              120  => 'Duet',
 266              121  => 'Punk Rock',
 267              122  => 'Drum Solo',
 268              123  => 'A Cappella',
 269              124  => 'Euro-House',
 270              125  => 'Dance Hall',
 271              126  => 'Goa',
 272              127  => 'Drum & Bass',
 273              128  => 'Club-House',
 274              129  => 'Hardcore',
 275              130  => 'Terror',
 276              131  => 'Indie',
 277              132  => 'BritPop',
 278              133  => 'Negerpunk',
 279              134  => 'Polsk Punk',
 280              135  => 'Beat',
 281              136  => 'Christian Gangsta Rap',
 282              137  => 'Heavy Metal',
 283              138  => 'Black Metal',
 284              139  => 'Crossover',
 285              140  => 'Contemporary Christian',
 286              141  => 'Christian Rock',
 287              142  => 'Merengue',
 288              143  => 'Salsa',
 289              144  => 'Thrash Metal',
 290              145  => 'Anime',
 291              146  => 'JPop',
 292              147  => 'Synthpop',
 293  
 294              255  => 'Unknown',
 295  
 296              'CR' => 'Cover',
 297              'RX' => 'Remix'
 298          );
 299  
 300          static $GenreLookupSCMPX = array();
 301          if ($allowSCMPXextended && empty($GenreLookupSCMPX)) {
 302              $GenreLookupSCMPX = $GenreLookup;
 303              // http://www.geocities.co.jp/SiliconValley-Oakland/3664/alittle.html#GenreExtended
 304              // Extended ID3v1 genres invented by SCMPX
 305              // Note that 255 "Japanese Anime" conflicts with standard "Unknown"
 306              $GenreLookupSCMPX[240] = 'Sacred';
 307              $GenreLookupSCMPX[241] = 'Northern Europe';
 308              $GenreLookupSCMPX[242] = 'Irish & Scottish';
 309              $GenreLookupSCMPX[243] = 'Scotland';
 310              $GenreLookupSCMPX[244] = 'Ethnic Europe';
 311              $GenreLookupSCMPX[245] = 'Enka';
 312              $GenreLookupSCMPX[246] = 'Children\'s Song';
 313              $GenreLookupSCMPX[247] = 'Japanese Sky';
 314              $GenreLookupSCMPX[248] = 'Japanese Heavy Rock';
 315              $GenreLookupSCMPX[249] = 'Japanese Doom Rock';
 316              $GenreLookupSCMPX[250] = 'Japanese J-POP';
 317              $GenreLookupSCMPX[251] = 'Japanese Seiyu';
 318              $GenreLookupSCMPX[252] = 'Japanese Ambient Techno';
 319              $GenreLookupSCMPX[253] = 'Japanese Moemoe';
 320              $GenreLookupSCMPX[254] = 'Japanese Tokusatsu';
 321              //$GenreLookupSCMPX[255] = 'Japanese Anime';
 322          }
 323  
 324          return ($allowSCMPXextended ? $GenreLookupSCMPX : $GenreLookup);
 325      }
 326  
 327      /**
 328       * @param string $genreid
 329       * @param bool   $allowSCMPXextended
 330       *
 331       * @return string|false
 332       */
 333  	public static function LookupGenreName($genreid, $allowSCMPXextended=true) {
 334          switch ($genreid) {
 335              case 'RX':
 336              case 'CR':
 337                  break;
 338              default:
 339                  if (!is_numeric($genreid)) {
 340                      return false;
 341                  }
 342                  $genreid = intval($genreid); // to handle 3 or '3' or '03'
 343                  break;
 344          }
 345          $GenreLookup = self::ArrayOfGenres($allowSCMPXextended);
 346          return (isset($GenreLookup[$genreid]) ? $GenreLookup[$genreid] : false);
 347      }
 348  
 349      /**
 350       * @param string $genre
 351       * @param bool   $allowSCMPXextended
 352       *
 353       * @return string|false
 354       */
 355  	public static function LookupGenreID($genre, $allowSCMPXextended=false) {
 356          $GenreLookup = self::ArrayOfGenres($allowSCMPXextended);
 357          $LowerCaseNoSpaceSearchTerm = strtolower(str_replace(' ', '', $genre));
 358          foreach ($GenreLookup as $key => $value) {
 359              if (strtolower(str_replace(' ', '', $value)) == $LowerCaseNoSpaceSearchTerm) {
 360                  return $key;
 361              }
 362          }
 363          return false;
 364      }
 365  
 366      /**
 367       * @param string $OriginalGenre
 368       *
 369       * @return string|false
 370       */
 371  	public static function StandardiseID3v1GenreName($OriginalGenre) {
 372          if (($GenreID = self::LookupGenreID($OriginalGenre)) !== false) {
 373              return self::LookupGenreName($GenreID);
 374          }
 375          return $OriginalGenre;
 376      }
 377  
 378      /**
 379       * @param string     $title
 380       * @param string     $artist
 381       * @param string     $album
 382       * @param string     $year
 383       * @param int        $genreid
 384       * @param string     $comment
 385       * @param int|string $track
 386       *
 387       * @return string
 388       */
 389  	public static function GenerateID3v1Tag($title, $artist, $album, $year, $genreid, $comment, $track='') {
 390          $ID3v1Tag  = 'TAG';
 391          $ID3v1Tag .= str_pad(trim(substr($title,  0, 30)), 30, "\x00", STR_PAD_RIGHT);
 392          $ID3v1Tag .= str_pad(trim(substr($artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
 393          $ID3v1Tag .= str_pad(trim(substr($album,  0, 30)), 30, "\x00", STR_PAD_RIGHT);
 394          $ID3v1Tag .= str_pad(trim(substr($year,   0,  4)),  4, "\x00", STR_PAD_LEFT);
 395          if (!empty($track) && ($track > 0) && ($track <= 255)) {
 396              $ID3v1Tag .= str_pad(trim(substr($comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT);
 397              $ID3v1Tag .= "\x00";
 398              if (gettype($track) == 'string') {
 399                  $track = (int) $track;
 400              }
 401              $ID3v1Tag .= chr($track);
 402          } else {
 403              $ID3v1Tag .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
 404          }
 405          if (($genreid < 0) || ($genreid > 147)) {
 406              $genreid = 255; // 'unknown' genre
 407          }
 408          switch (gettype($genreid)) {
 409              case 'string':
 410              case 'integer':
 411                  $ID3v1Tag .= chr(intval($genreid));
 412                  break;
 413              default:
 414                  $ID3v1Tag .= chr(255); // 'unknown' genre
 415                  break;
 416          }
 417  
 418          return $ID3v1Tag;
 419      }
 420  
 421  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7