ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
Cue.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
9 // available at http://getid3.sourceforge.net //
10 // or http://www.getid3.org //
12 // See readme.txt for more details //
14 // //
15 // module.misc.cue.php //
16 // module for analyzing CUEsheet files //
17 // dependencies: NONE //
18 // //
20 // //
21 // Module originally written [2009-Mar-25] by //
22 // Nigel Barnes <ngbarnesØhotmail*com> //
23 // Minor reformatting and similar small changes to integrate //
24 // into GetId3 by James Heinrich <info@getid3.org> //
25 // ///
27 
46 class Cue extends BaseHandler
47 {
52  public $cuesheet = array();
53 
58  public function analyze()
59  {
60  $info = &$this->getid3->info;
61 
62  $info['fileformat'] = 'cue';
63  $this->readCueSheetFilename($info['filenamepath']);
64  $info['cue'] = $this->cuesheet;
65 
66  return true;
67  }
68 
75  {
76  $filedata = file_get_contents($filename);
77 
78  return $this->readCueSheet($filedata);
79  }
80 
86  public function readCueSheet(&$filedata)
87  {
88  $cue_lines = array();
89  foreach (explode("\n", str_replace("\r", null, $filedata)) as $line) {
90  if ( (strlen($line) > 0) && ($line[0] != '#')) {
91  $cue_lines[] = trim($line);
92  }
93  }
94  $this->parseCueSheet($cue_lines);
95 
96  return $this->cuesheet;
97  }
98 
103  public function parseCueSheet($file)
104  {
105  //-1 means still global, all others are track specific
106  $track_on = -1;
107 
108  for ($i=0; $i < count($file); $i++) {
109  list($key) = explode(' ', strtolower($file[$i]), 2);
110  switch ($key) {
111  case 'catalog':
112  case 'cdtextfile':
113  case 'isrc':
114  case 'performer':
115  case 'songwriter':
116  case 'title':
117  $this->parseString($file[$i], $track_on);
118  break;
119  case 'file':
120  $currentFile = $this->parseFile($file[$i]);
121  break;
122  case 'flags':
123  $this->parseFlags($file[$i], $track_on);
124  break;
125  case 'index':
126  case 'postgap':
127  case 'pregap':
128  $this->parseIndex($file[$i], $track_on);
129  break;
130  case 'rem':
131  $this->parseComment($file[$i], $track_on);
132  break;
133  case 'track':
134  $track_on++;
135  $this->parseTrack($file[$i], $track_on);
136  if (isset($currentFile)) { // if there's a file
137  $this->cuesheet['tracks'][$track_on]['datafile'] = $currentFile;
138  }
139  break;
140  default:
141  //save discarded junk and place string[] with track it was found in
142  $this->parseGarbage($file[$i], $track_on);
143  break;
144  }
145  }
146  }
147 
154  public function parseComment($line, $track_on)
155  {
156  $explodedline = explode(' ', $line, 3);
157  $comment_REM = (isset($explodedline[0]) ? $explodedline[0] : '');
158  $comment_type = (isset($explodedline[1]) ? $explodedline[1] : '');
159  $comment_data = (isset($explodedline[2]) ? $explodedline[2] : '');
160  if (($comment_REM == 'REM') && $comment_type) {
161  $comment_type = strtolower($comment_type);
162  $commment_data = trim($comment_data, ' "');
163  if ($track_on != -1) {
164  $this->cuesheet['tracks'][$track_on]['comments'][$comment_type][] = $comment_data;
165  } else {
166  $this->cuesheet['comments'][$comment_type][] = $comment_data;
167  }
168  }
169  }
170 
178  public function parseFile($line)
179  {
180  $line = substr($line, strpos($line, ' ') + 1);
181  $type = strtolower(substr($line, strrpos($line, ' ')));
182 
183  //remove type
184  $line = substr($line, 0, strrpos($line, ' ') - 1);
185 
186  //if quotes around it, remove them.
187  $line = trim($line, '"');
188 
189  return array('filename'=>$line, 'type'=>$type);
190  }
191 
198  public function parseFlags($line, $track_on)
199  {
200  if ($track_on != -1) {
201  foreach (explode(' ', strtolower($line)) as $type) {
202  switch ($type) {
203  case 'flags':
204  // first entry in this line
205  $this->cuesheet['tracks'][$track_on]['flags'] = array(
206  '4ch' => false,
207  'data' => false,
208  'dcp' => false,
209  'pre' => false,
210  'scms' => false,
211  );
212  break;
213  case 'data':
214  case 'dcp':
215  case '4ch':
216  case 'pre':
217  case 'scms':
218  $this->cuesheet['tracks'][$track_on]['flags'][$type] = true;
219  break;
220  default:
221  break;
222  }
223  }
224  }
225  }
226 
233  public function parseGarbage($line, $track_on)
234  {
235  if ( strlen($line) > 0 ) {
236  if ($track_on == -1) {
237  $this->cuesheet['garbage'][] = $line;
238  } else {
239  $this->cuesheet['tracks'][$track_on]['garbage'][] = $line;
240  }
241  }
242  }
243 
250  public function parseIndex($line, $track_on)
251  {
252  $type = strtolower(substr($line, 0, strpos($line, ' ')));
253  $line = substr($line, strpos($line, ' ') + 1);
254 
255  if ($type == 'index') {
256  //read the index number
257  $number = intval(substr($line, 0, strpos($line, ' ')));
258  $line = substr($line, strpos($line, ' ') + 1);
259  }
260 
261  //extract the minutes, seconds, and frames
262  $explodedline = explode(':', $line);
263  $minutes = (isset($explodedline[0]) ? $explodedline[0] : '');
264  $seconds = (isset($explodedline[1]) ? $explodedline[1] : '');
265  $frames = (isset($explodedline[2]) ? $explodedline[2] : '');
266 
267  switch ($type) {
268  case 'index':
269  $this->cuesheet['tracks'][$track_on][$type][$number] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
270  break;
271  case 'pregap':
272  case 'postgap':
273  $this->cuesheet['tracks'][$track_on][$type] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
274  break;
275  }
276  }
277 
283  public function parseString($line, $track_on)
284  {
285  $category = strtolower(substr($line, 0, strpos($line, ' ')));
286  $line = substr($line, strpos($line, ' ') + 1);
287 
288  //get rid of the quotes
289  $line = trim($line, '"');
290 
291  switch ($category) {
292  case 'catalog':
293  case 'cdtextfile':
294  case 'isrc':
295  case 'performer':
296  case 'songwriter':
297  case 'title':
298  if ($track_on == -1) {
299  $this->cuesheet[$category] = $line;
300  } else {
301  $this->cuesheet['tracks'][$track_on][$category] = $line;
302  }
303  break;
304  default:
305  break;
306  }
307  }
308 
315  public function parseTrack($line, $track_on)
316  {
317  $line = substr($line, strpos($line, ' ') + 1);
318  $track = ltrim(substr($line, 0, strpos($line, ' ')), '0');
319 
320  //find the data type.
321  $datatype = strtolower(substr($line, strpos($line, ' ') + 1));
322 
323  $this->cuesheet['tracks'][$track_on] = array('track_number'=>$track, 'datatype'=>$datatype);
324  }
325 
326 }
readCueSheetFilename($filename)
Definition: Cue.php:74
parseComment($line, $track_on)
Parses the REM command.
Definition: Cue.php:154
GetId3() by James Heinrich info@getid3.org //.
Definition: BaseHandler.php:25
parseFlags($line, $track_on)
Parses the FLAG command.
Definition: Cue.php:198
parseIndex($line, $track_on)
Parses the INDEX command of a TRACK.
Definition: Cue.php:250
readCueSheet(&$filedata)
Definition: Cue.php:86
$info
Definition: example_052.php:80
parseGarbage($line, $track_on)
Collect any unidentified data.
Definition: Cue.php:233
parseCueSheet($file)
Definition: Cue.php:103
GetId3() by James Heinrich info@getid3.org //.
Definition: Cue.php:46
parseString($line, $track_on)
Definition: Cue.php:283
Create styles array
The data for the language used.
parseTrack($line, $track_on)
Parses the TRACK command.
Definition: Cue.php:315
parseFile($line)
Parses the FILE command.
Definition: Cue.php:178
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file