ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
BaseHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace GetId3\Handler;
4 
8 
11 // available at http://getid3.sourceforge.net //
12 // or http://www.getid3.org //
14 // //
15 // Please see readme.txt for more information //
16 // ///
18 
25 abstract class BaseHandler
26 {
32  protected $getid3;
33 
39  protected $data_string_flag = false;
40 
46  protected $data_string = '';
47 
53  protected $data_string_position = 0;
54 
60  protected $data_string_length = 0;
61 
62  private $dependency_to;
63 
69  public function __construct(GetId3Core $getid3, $call_module = null)
70  {
71  $this->getid3 = $getid3;
72 
73  if (null !== $call_module) {
74  $this->dependency_to = $call_module;
75  }
76  }
77 
81  abstract public function analyze();
82 
86  public function AnalyzeString(&$string)
87  {
88  // Enter string mode
89  $this->data_string_flag = true;
90  $this->data_string = $string;
91 
92  // Save info
93  $saved_avdataoffset = $this->getid3->info['avdataoffset'];
94  $saved_avdataend = $this->getid3->info['avdataend'];
95  $saved_filesize = (isset($this->getid3->info['filesize']) ? $this->getid3->info['filesize'] : null); // may be not set if called as dependency without openfile() call
96  // Reset some info
97  $this->getid3->info['avdataoffset'] = 0;
98  $this->getid3->info['avdataend'] = $this->getid3->info['filesize'] = $this->data_string_length = strlen($string);
99 
100  // Analyze
101  $this->analyze();
102 
103  // Restore some info
104  $this->getid3->info['avdataoffset'] = $saved_avdataoffset;
105  $this->getid3->info['avdataend'] = $saved_avdataend;
106  $this->getid3->info['filesize'] = $saved_filesize;
107 
108  // Exit string mode
109  $this->data_string_flag = false;
110  }
111 
116  protected function ftell()
117  {
118  if ($this->data_string_flag) {
120  }
121 
122  return ftell($this->getid3->fp);
123  }
124 
130  protected function fread($bytes)
131  {
132  if ($this->data_string_flag) {
133  $this->data_string_position += $bytes;
134 
135  return substr($this->data_string,
136  $this->data_string_position - $bytes, $bytes);
137  }
138 
139  return fread($this->getid3->fp, $bytes);
140  }
141 
148  protected function fseek($bytes, $whence = SEEK_SET)
149  {
150  if ($this->data_string_flag) {
151  switch ($whence) {
152  case SEEK_SET:
153  $this->data_string_position = $bytes;
154  break;
155 
156  case SEEK_CUR:
157  $this->data_string_position += $bytes;
158  break;
159 
160  case SEEK_END:
161  $this->data_string_position = $this->data_string_length + $bytes;
162  break;
163  }
164 
165  return 0;
166  }
167 
168  return fseek($this->getid3->fp, $bytes, $whence);
169  }
170 
175  protected function feof()
176  {
177  if ($this->data_string_flag) {
178  return $this->data_string_position >= $this->data_string_length;
179  }
180 
181  return feof($this->getid3->fp);
182  }
183 
189  final protected function isDependencyFor($module)
190  {
191  return $this->dependency_to == $module;
192  }
193 
199  protected function error($text)
200  {
201  $this->getid3->info['error'][] = $text;
202 
203  return false;
204  }
205 
211  protected function warning($text)
212  {
213  return $this->getid3->warning($text);
214  }
215 
225  public function saveAttachment(&$ThisFileInfoIndex, $filename, $offset,
226  $length)
227  {
228  try {
229  if (!Helper::intValueSupported($offset + $length)) {
230  throw new DefaultException('it extends beyond the ' . round(PHP_INT_MAX / 1073741824) . 'GB limit');
231  }
232 
233  // do not extract at all
234  if ($this->getid3->option_save_attachments === self::ATTACHMENTS_NONE) {
235  unset($ThisFileInfoIndex); // do not set any
236  }
237 
238  // extract to return array
239  else if ($this->getid3->option_save_attachments === self::ATTACHMENTS_INLINE) {
240 
241  // get whole data in one pass, till it is anyway stored in memory
242  $this->fseek($offset);
243  $ThisFileInfoIndex = $this->fread($length);
244  if ($ThisFileInfoIndex === false || strlen($ThisFileInfoIndex) != $length) { // verify
245  throw new DefaultException('failed to read attachment data');
246  }
247  }
248 
249  // assume directory path is given
250  else {
251 
252  // set up destination path
253  $dir = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR,
254  $this->getid3->option_save_attachments),
255  DIRECTORY_SEPARATOR);
256  if (!is_dir($dir) || !is_writable($dir)) { // check supplied directory
257  throw new DefaultException('supplied path (' . $dir . ') does not exist, or is not writable');
258  }
259  $dest = $dir . DIRECTORY_SEPARATOR . $filename;
260 
261  // create dest file
262  if (($fp_dest = fopen($dest, 'wb')) == false) {
263  throw new DefaultException('failed to create file ' . $dest);
264  }
265 
266  // copy data
267  $this->fseek($offset);
268  $buffersize = ($this->data_string_flag ? $length : $this->getid3->fread_buffer_size());
269  $bytesleft = $length;
270  while ($bytesleft > 0) {
271  if (($buffer = $this->fread(min($buffersize, $bytesleft))) === false || ($byteswritten = fwrite($fp_dest,
272  $buffer)) === false) {
273  fclose($fp_dest);
274  unlink($dest);
275  throw new DefaultException($buffer === false ? 'not enough data to read' : 'failed to write to destination file, may be not enough disk space');
276  }
277  $bytesleft -= $byteswritten;
278  }
279 
280  fclose($fp_dest);
281  $ThisFileInfoIndex = $dest;
282  }
283  } catch (DefaultException $e) {
284 
285  unset($ThisFileInfoIndex); // do not set any is case of error
286  $this->warning('Failed to extract attachment ' . $filename . ': ' . $e->getMessage());
287 
288  return false;
289  }
290 
291  return true;
292  }
293 }
GetId3() by James Heinrich info@getid3.org //.
Definition: BaseHandler.php:25
__construct(GetId3Core $getid3, $call_module=null)
Definition: BaseHandler.php:69
fseek($bytes, $whence=SEEK_SET)
GetId3() by James Heinrich info@getid3.org //.
Definition: GetId3Core.php:25
analyze()
Analyze from file pointer.
Create styles array
The data for the language used.
AnalyzeString(&$string)
Analyze from string instead.
Definition: BaseHandler.php:86
static intValueSupported($num)
null $hasINT64
Definition: Helper.php:130
$text
saveAttachment(&$ThisFileInfoIndex, $filename, $offset, $length)