getid3_handler

The WordPress Core getid3 handler class.

Defined (1)

The class is defined in the following location(s).

/wp-includes/ID3/getid3.php  
  1. abstract class getid3_handler { 
  2.  
  3. /** 
  4. * @var getID3 
  5. */ 
  6. protected $getid3; // pointer 
  7.  
  8. protected $data_string_flag = false; // analyzing filepointer or string 
  9. protected $data_string = ''; // string to analyze 
  10. protected $data_string_position = 0; // seek position in string 
  11. protected $data_string_length = 0; // string length 
  12.  
  13. private $dependency_to = null; 
  14.  
  15.  
  16. public function __construct(getID3 $getid3, $call_module=null) { 
  17. $this->getid3 = $getid3; 
  18.  
  19. if ($call_module) { 
  20. $this->dependency_to = str_replace('getid3_', '', $call_module); 
  21.  
  22.  
  23. // Analyze from file pointer 
  24. abstract public function Analyze(); 
  25.  
  26.  
  27. // Analyze from string instead 
  28. public function AnalyzeString($string) { 
  29. // Enter string mode 
  30. $this->setStringMode($string); 
  31.  
  32. // Save info 
  33. $saved_avdataoffset = $this->getid3->info['avdataoffset']; 
  34. $saved_avdataend = $this->getid3->info['avdataend']; 
  35. $saved_filesize = (isset($this->getid3->info['filesize']) ? $this->getid3->info['filesize'] : null); // may be not set if called as dependency without openfile() call 
  36.  
  37. // Reset some info 
  38. $this->getid3->info['avdataoffset'] = 0; 
  39. $this->getid3->info['avdataend'] = $this->getid3->info['filesize'] = $this->data_string_length; 
  40.  
  41. // Analyze 
  42. $this->Analyze(); 
  43.  
  44. // Restore some info 
  45. $this->getid3->info['avdataoffset'] = $saved_avdataoffset; 
  46. $this->getid3->info['avdataend'] = $saved_avdataend; 
  47. $this->getid3->info['filesize'] = $saved_filesize; 
  48.  
  49. // Exit string mode 
  50. $this->data_string_flag = false; 
  51.  
  52. public function setStringMode($string) { 
  53. $this->data_string_flag = true; 
  54. $this->data_string = $string; 
  55. $this->data_string_length = strlen($string); 
  56.  
  57. protected function ftell() { 
  58. if ($this->data_string_flag) { 
  59. return $this->data_string_position; 
  60. return ftell($this->getid3->fp); 
  61.  
  62. protected function fread($bytes) { 
  63. if ($this->data_string_flag) { 
  64. $this->data_string_position += $bytes; 
  65. return substr($this->data_string, $this->data_string_position - $bytes, $bytes); 
  66. $pos = $this->ftell() + $bytes; 
  67. if (!getid3_lib::intValueSupported($pos)) { 
  68. throw new getid3_exception('cannot fread('.$bytes.' from '.$this->ftell().') because beyond PHP filesystem limit', 10); 
  69. return fread($this->getid3->fp, $bytes); 
  70.  
  71. protected function fseek($bytes, $whence=SEEK_SET) { 
  72. if ($this->data_string_flag) { 
  73. switch ($whence) { 
  74. case SEEK_SET: 
  75. $this->data_string_position = $bytes; 
  76. break; 
  77.  
  78. case SEEK_CUR: 
  79. $this->data_string_position += $bytes; 
  80. break; 
  81.  
  82. case SEEK_END: 
  83. $this->data_string_position = $this->data_string_length + $bytes; 
  84. break; 
  85. return 0; 
  86. } else { 
  87. $pos = $bytes; 
  88. if ($whence == SEEK_CUR) { 
  89. $pos = $this->ftell() + $bytes; 
  90. } elseif ($whence == SEEK_END) { 
  91. $pos = $this->getid3->info['filesize'] + $bytes; 
  92. if (!getid3_lib::intValueSupported($pos)) { 
  93. throw new getid3_exception('cannot fseek('.$pos.') because beyond PHP filesystem limit', 10); 
  94. return fseek($this->getid3->fp, $bytes, $whence); 
  95.  
  96. protected function feof() { 
  97. if ($this->data_string_flag) { 
  98. return $this->data_string_position >= $this->data_string_length; 
  99. return feof($this->getid3->fp); 
  100.  
  101. final protected function isDependencyFor($module) { 
  102. return $this->dependency_to == $module; 
  103.  
  104. protected function error($text) { 
  105. $this->getid3->info['error'][] = $text; 
  106.  
  107. return false; 
  108.  
  109. protected function warning($text) { 
  110. return $this->getid3->warning($text); 
  111.  
  112. protected function notice($text) { 
  113. // does nothing for now 
  114.  
  115. public function saveAttachment($name, $offset, $length, $image_mime=null) { 
  116. try { 
  117.  
  118. // do not extract at all 
  119. if ($this->getid3->option_save_attachments === getID3::ATTACHMENTS_NONE) { 
  120.  
  121. $attachment = null; // do not set any 
  122.  
  123. // extract to return array 
  124. } elseif ($this->getid3->option_save_attachments === getID3::ATTACHMENTS_INLINE) { 
  125.  
  126. $this->fseek($offset); 
  127. $attachment = $this->fread($length); // get whole data in one pass, till it is anyway stored in memory 
  128. if ($attachment === false || strlen($attachment) != $length) { 
  129. throw new Exception('failed to read attachment data'); 
  130.  
  131. // assume directory path is given 
  132. } else { 
  133.  
  134. // set up destination path 
  135. $dir = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->getid3->option_save_attachments), DIRECTORY_SEPARATOR); 
  136. if (!is_dir($dir) || !is_writable($dir)) { // check supplied directory 
  137. throw new Exception('supplied path ('.$dir.') does not exist, or is not writable'); 
  138. $dest = $dir.DIRECTORY_SEPARATOR.$name.($image_mime ? '.'.getid3_lib::ImageExtFromMime($image_mime) : ''); 
  139.  
  140. // create dest file 
  141. if (($fp_dest = fopen($dest, 'wb')) == false) { 
  142. throw new Exception('failed to create file '.$dest); 
  143.  
  144. // copy data 
  145. $this->fseek($offset); 
  146. $buffersize = ($this->data_string_flag ? $length : $this->getid3->fread_buffer_size()); 
  147. $bytesleft = $length; 
  148. while ($bytesleft > 0) { 
  149. if (($buffer = $this->fread(min($buffersize, $bytesleft))) === false || ($byteswritten = fwrite($fp_dest, $buffer)) === false || ($byteswritten === 0)) { 
  150. throw new Exception($buffer === false ? 'not enough data to read' : 'failed to write to destination file, may be not enough disk space'); 
  151. $bytesleft -= $byteswritten; 
  152.  
  153. fclose($fp_dest); 
  154. $attachment = $dest; 
  155.  
  156.  
  157. } catch (Exception $e) { 
  158.  
  159. // close and remove dest file if created 
  160. if (isset($fp_dest) && is_resource($fp_dest)) { 
  161. fclose($fp_dest); 
  162. unlink($dest); 
  163.  
  164. // do not set any is case of error 
  165. $attachment = null; 
  166. $this->warning('Failed to extract attachment '.$name.': '.$e->getMessage()); 
  167.  
  168.  
  169. // seek to the end of attachment 
  170. $this->fseek($offset + $length); 
  171.  
  172. return $attachment; 
  173.