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 /////////////////////////////////////////////////////////////////
11 // module.misc.cue.php //
12 // module for analyzing CUEsheet files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
17 // Module originally written [2009-Mar-25] by //
18 // Nigel Barnes <ngbarnesĂhotmail*com> //
19 // Minor reformatting and similar small changes to integrate //
20 // into getID3 by James Heinrich <info@getid3.org> //
22 /////////////////////////////////////////////////////////////////
25 * CueSheet parser by Nigel Barnes.
27 * This is a PHP conversion of CueSharp 0.5 by Wyatt O'Day (wyday.com/cuesharp)
31 * A CueSheet class used to open and parse cuesheets.
35 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
39 class getid3_cue
extends getid3_handler
41 public $cuesheet = array();
46 public function Analyze() {
47 $info = &$this->getid3
->info
;
49 $info['fileformat'] = 'cue';
50 $this->readCueSheetFilename($info['filenamepath']);
51 $info['cue'] = $this->cuesheet
;
56 * @param string $filename
60 public function readCueSheetFilename($filename)
62 $filedata = file_get_contents($filename);
63 return $this->readCueSheet($filedata);
67 * Parses a cue sheet file.
69 * @param string $filedata
73 public function readCueSheet(&$filedata)
76 foreach (explode("\n", str_replace("\r", null, $filedata)) as $line)
78 if ( (strlen($line) > 0) && ($line[0] != '#'))
80 $cue_lines[] = trim($line);
83 $this->parseCueSheet($cue_lines);
85 return $this->cuesheet
;
89 * Parses the cue sheet array.
91 * @param array $file - The cuesheet as an array of each line.
93 public function parseCueSheet($file)
95 //-1 means still global, all others are track specific
98 for ($i=0; $i < count($file); $i++
)
100 list($key) = explode(' ', strtolower($file[$i]), 2);
109 $this->parseString($file[$i], $track_on);
112 $currentFile = $this->parseFile($file[$i]);
115 $this->parseFlags($file[$i], $track_on);
120 $this->parseIndex($file[$i], $track_on);
123 $this->parseComment($file[$i], $track_on);
127 $this->parseTrack($file[$i], $track_on);
128 if (isset($currentFile)) // if there's a file
130 $this->cuesheet
['tracks'][$track_on]['datafile'] = $currentFile;
134 //save discarded junk and place string[] with track it was found in
135 $this->parseGarbage($file[$i], $track_on);
142 * Parses the REM command.
144 * @param string $line - The line in the cue file that contains the TRACK command.
145 * @param integer $track_on - The track currently processing.
147 public function parseComment($line, $track_on)
149 $explodedline = explode(' ', $line, 3);
150 $comment_REM = (isset($explodedline[0]) ?
$explodedline[0] : '');
151 $comment_type = (isset($explodedline[1]) ?
$explodedline[1] : '');
152 $comment_data = (isset($explodedline[2]) ?
$explodedline[2] : '');
153 if (($comment_REM == 'REM') && $comment_type) {
154 $comment_type = strtolower($comment_type);
155 $commment_data = trim($comment_data, ' "');
156 if ($track_on != -1) {
157 $this->cuesheet
['tracks'][$track_on]['comments'][$comment_type][] = $comment_data;
159 $this->cuesheet
['comments'][$comment_type][] = $comment_data;
165 * Parses the FILE command.
167 * @param string $line - The line in the cue file that contains the FILE command.
169 * @return array - Array of FILENAME and TYPE of file..
171 public function parseFile($line)
173 $line = substr($line, strpos($line, ' ') +
1);
174 $type = strtolower(substr($line, strrpos($line, ' ')));
177 $line = substr($line, 0, strrpos($line, ' ') - 1);
179 //if quotes around it, remove them.
180 $line = trim($line, '"');
182 return array('filename'=>$line, 'type'=>$type);
186 * Parses the FLAG command.
188 * @param string $line - The line in the cue file that contains the TRACK command.
189 * @param integer $track_on - The track currently processing.
191 public function parseFlags($line, $track_on)
195 foreach (explode(' ', strtolower($line)) as $type)
200 // first entry in this line
201 $this->cuesheet
['tracks'][$track_on]['flags'] = array(
214 $this->cuesheet
['tracks'][$track_on]['flags'][$type] = true;
224 * Collect any unidentified data.
226 * @param string $line - The line in the cue file that contains the TRACK command.
227 * @param integer $track_on - The track currently processing.
229 public function parseGarbage($line, $track_on)
231 if ( strlen($line) > 0 )
235 $this->cuesheet
['garbage'][] = $line;
239 $this->cuesheet
['tracks'][$track_on]['garbage'][] = $line;
245 * Parses the INDEX command of a TRACK.
247 * @param string $line - The line in the cue file that contains the TRACK command.
248 * @param integer $track_on - The track currently processing.
250 public function parseIndex($line, $track_on)
252 $type = strtolower(substr($line, 0, strpos($line, ' ')));
253 $line = substr($line, strpos($line, ' ') +
1);
256 if ($type == 'index')
258 //read the index number
259 $number = intval(substr($line, 0, strpos($line, ' ')));
260 $line = substr($line, strpos($line, ' ') +
1);
263 //extract the minutes, seconds, and frames
264 $explodedline = explode(':', $line);
265 $minutes = (isset($explodedline[0]) ?
$explodedline[0] : '');
266 $seconds = (isset($explodedline[1]) ?
$explodedline[1] : '');
267 $frames = (isset($explodedline[2]) ?
$explodedline[2] : '');
271 $this->cuesheet
['tracks'][$track_on][$type][$number] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
275 $this->cuesheet
['tracks'][$track_on][$type] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
281 * @param string $line
282 * @param int $track_on
284 public function parseString($line, $track_on)
286 $category = strtolower(substr($line, 0, strpos($line, ' ')));
287 $line = substr($line, strpos($line, ' ') +
1);
289 //get rid of the quotes
290 $line = trim($line, '"');
302 $this->cuesheet
[$category] = $line;
306 $this->cuesheet
['tracks'][$track_on][$category] = $line;
315 * Parses the TRACK command.
317 * @param string $line - The line in the cue file that contains the TRACK command.
318 * @param integer $track_on - The track currently processing.
320 public function parseTrack($line, $track_on)
322 $line = substr($line, strpos($line, ' ') +
1);
323 $track = ltrim(substr($line, 0, strpos($line, ' ')), '0');
325 //find the data type.
326 $datatype = strtolower(substr($line, strpos($line, ' ') +
1));
328 $this->cuesheet
['tracks'][$track_on] = array('track_number'=>$track, 'datatype'=>$datatype);