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