<?php
  
/*
------------------------------
Класс для извлечения ID3 тегов
------------------------------
*/

CLASS ID3{
  
  VAR $aTV23 = ARRAY(
    
    'TIT2',
    'TALB',
    'TPE1',
    'TPE2',
    'TRCK',
    'TYER',
    'TLEN',
    'USLT',
    'TPOS',
    'TCON',
    'TENC',
    'TCOP',
    'TPUB',
    'TOPE',
    'WXXX',
    'COMM',
    'TCOM'
  
  );
  
  VAR $aTV23t = ARRAY(
    
    'Title',
    'Album',
    'Author',
    'AlbumAuthor',
    'Track',
    'Year',
    'Lenght',
    'Lyric',
    'Desc',
    'Genre',
    'Encoded',
    'Copyright',
    'Publisher',
    'OriginalArtist',
    'URL',
    'Comments',
    'Composer'
  
  );
  
  VAR $aTV22 = ARRAY(
    
    'TT2',
    'TAL',
    'TP1',
    'TRK',
    'TYE',
    'TLE',
    'ULT'
  
  );
  
  VAR $aTV22t = ARRAY(
    
    'Title',
    'Album',
    'Author',
    'Track',
    'Year',
    'Lenght',
    'Lyric'
  
  );
  
  FUNCTION getTagsInfo($sFilepath) {
    
    $iFSize = FILESIZE($sFilepath);    
    $vFD = FOPEN($sFilepath, 'r');
    $sSrc = FREAD($vFD, $iFSize);
    FCLOSE($vFD);
    
    $aInfo = NULL;
    
    IF (SUBSTR($sSrc, 0, 3) == 'ID3') {
      
      $aInfo['FileName'] = $sFilepath;
      $aInfo['Version'] = HEXDEC(BIN2HEX(SUBSTR($sSrc, 3, 1))).'.'.HEXDEC(BIN2HEX(SUBSTR($sSrc, 4, 1)));
         
    }
    
    IF ($aInfo['Version'] == '4.0' || $aInfo['Version'] == '3.0') {
      
      FOR ($i = 0; $i < COUNT($this->aTV23); $i++) {
        
        IF (STRPOS($sSrc, $this->aTV23[$i].chr(0)) != FALSE) {
          
          $s = '';
          $iPos = STRPOS($sSrc, $this->aTV23[$i].chr(0));
          
          $iLen = HEXDEC(BIN2HEX(SUBSTR($sSrc, ($iPos + 5), 3)));
          
          $data = SUBSTR($sSrc, $iPos, 9 + $iLen);
          
          FOR ($a = 0; $a < STRLEN($data); $a++) {
            
            $char = SUBSTR($data, $a, 1);
            
            IF ($char >= ' ' && $char <= '~'){
              
              $s .= $char;
              
            }
          
          }
          
          IF (SUBSTR($s, 0, 4) == $this->aTV23[$i]) {
            
            $iSL = 4;
            
            IF ($this->aTV23[$i] == 'USLT') {
              
              $iSL = 7;
            
            }ELSEIF ($this->aTV23[$i] == 'TALB') {
              
              $iSL = 5;
            
            }ELSEIF ($this->aTV23[$i] == 'TENC') {
              
              $iSL = 6;
            
            }
            
            $aInfo[$this->aTV23t[$i]] = SUBSTR($s, $iSL);
          
          }
        
        }
      
      }
    
    }
    
    IF ($aInfo['Version'] == '2.0') {
      
      FOR ($i = 0; $i < COUNT($this->aTV22); $i++) {
        
        IF (STRPOS($sSrc, $this->aTV22[$i].chr(0)) != FALSE) {
          
          $s = '';
          $iPos = STRPOS($sSrc, $this->aTV22[$i].chr(0));
          $iLen = HEXDEC(BIN2HEX(SUBSTR($sSrc, ($iPos + 3), 3)));
          $data = SUBSTR($sSrc, $iPos, 6 + $iLen);
          
          FOR ($a = 0; $a < STRLEN($data); $a++) {
            
            $char = SUBSTR($data, $a, 1);
            
            IF ($char >= ' ' && $char <= '~'){
              
              $s .= $char;
              
            }
          
          }
          
          IF (SUBSTR($s, 0, 3) == $this->aTV22[$i]) {
            
            $iSL = 3;
            
            IF ($this->aTV22[$i] == 'ULT') {
              
              $iSL = 6;
            
            }
            
            $aInfo[$this->aTV22t[$i]] = SUBSTR($s, $iSL);
          
          }
        
        }
      
      }
    
    }
    
    IF (ISSET($aInfo)){
      
      RETURN $aInfo;
      
    }ELSE{
      
      RETURN 0;
      
    }
  
  }

}
  
?>