write.real.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // write.real.php //
  11. // module for writing RealAudio/RealVideo tags //
  12. // dependencies: module.tag.real.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_write_real
  16. {
  17. var $filename;
  18. var $tag_data = array();
  19. var $fread_buffer_size = 32768; // read buffer size in bytes
  20. var $warnings = array(); // any non-critical errors will be stored here
  21. var $errors = array(); // any critical errors will be stored here
  22. var $paddedlength = 512; // minimum length of CONT tag in bytes
  23. function getid3_write_real() {
  24. return true;
  25. }
  26. function WriteReal() {
  27. // File MUST be writeable - CHMOD(646) at least
  28. if (is_writeable($this->filename) && is_file($this->filename) && ($fp_source = fopen($this->filename, 'r+b'))) {
  29. // Initialize getID3 engine
  30. $getID3 = new getID3;
  31. $OldThisFileInfo = $getID3->analyze($this->filename);
  32. if (empty($OldThisFileInfo['real']['chunks']) && !empty($OldThisFileInfo['real']['old_ra_header'])) {
  33. $this->errors[] = 'Cannot write Real tags on old-style file format';
  34. fclose($fp_source);
  35. return false;
  36. }
  37. if (empty($OldThisFileInfo['real']['chunks'])) {
  38. $this->errors[] = 'Cannot write Real tags because cannot find DATA chunk in file';
  39. fclose($fp_source);
  40. return false;
  41. }
  42. foreach ($OldThisFileInfo['real']['chunks'] as $chunknumber => $chunkarray) {
  43. $oldChunkInfo[$chunkarray['name']] = $chunkarray;
  44. }
  45. if (!empty($oldChunkInfo['CONT']['length'])) {
  46. $this->paddedlength = max($oldChunkInfo['CONT']['length'], $this->paddedlength);
  47. }
  48. $new_CONT_tag_data = $this->GenerateCONTchunk();
  49. $new_PROP_tag_data = $this->GeneratePROPchunk($OldThisFileInfo['real']['chunks'], $new_CONT_tag_data);
  50. $new__RMF_tag_data = $this->GenerateRMFchunk($OldThisFileInfo['real']['chunks']);
  51. if (isset($oldChunkInfo['.RMF']['length']) && ($oldChunkInfo['.RMF']['length'] == strlen($new__RMF_tag_data))) {
  52. fseek($fp_source, $oldChunkInfo['.RMF']['offset'], SEEK_SET);
  53. fwrite($fp_source, $new__RMF_tag_data);
  54. } else {
  55. $this->errors[] = 'new .RMF tag ('.strlen($new__RMF_tag_data).' bytes) different length than old .RMF tag ('.$oldChunkInfo['.RMF']['length'].' bytes)';
  56. fclose($fp_source);
  57. return false;
  58. }
  59. if (isset($oldChunkInfo['PROP']['length']) && ($oldChunkInfo['PROP']['length'] == strlen($new_PROP_tag_data))) {
  60. fseek($fp_source, $oldChunkInfo['PROP']['offset'], SEEK_SET);
  61. fwrite($fp_source, $new_PROP_tag_data);
  62. } else {
  63. $this->errors[] = 'new PROP tag ('.strlen($new_PROP_tag_data).' bytes) different length than old PROP tag ('.$oldChunkInfo['PROP']['length'].' bytes)';
  64. fclose($fp_source);
  65. return false;
  66. }
  67. if (isset($oldChunkInfo['CONT']['length']) && ($oldChunkInfo['CONT']['length'] == strlen($new_CONT_tag_data))) {
  68. // new data length is same as old data length - just overwrite
  69. fseek($fp_source, $oldChunkInfo['CONT']['offset'], SEEK_SET);
  70. fwrite($fp_source, $new_CONT_tag_data);
  71. fclose($fp_source);
  72. return true;
  73. } else {
  74. if (empty($oldChunkInfo['CONT'])) {
  75. // no existing CONT chunk
  76. $BeforeOffset = $oldChunkInfo['DATA']['offset'];
  77. $AfterOffset = $oldChunkInfo['DATA']['offset'];
  78. } else {
  79. // new data is longer than old data
  80. $BeforeOffset = $oldChunkInfo['CONT']['offset'];
  81. $AfterOffset = $oldChunkInfo['CONT']['offset'] + $oldChunkInfo['CONT']['length'];
  82. }
  83. if ($tempfilename = tempnam(GETID3_TEMP_DIR, 'getID3')) {
  84. if (is_writable($tempfilename) && is_file($tempfilename) && ($fp_temp = fopen($tempfilename, 'wb'))) {
  85. rewind($fp_source);
  86. fwrite($fp_temp, fread($fp_source, $BeforeOffset));
  87. fwrite($fp_temp, $new_CONT_tag_data);
  88. fseek($fp_source, $AfterOffset, SEEK_SET);
  89. while ($buffer = fread($fp_source, $this->fread_buffer_size)) {
  90. fwrite($fp_temp, $buffer, strlen($buffer));
  91. }
  92. fclose($fp_temp);
  93. if (copy($tempfilename, $this->filename)) {
  94. unlink($tempfilename);
  95. fclose($fp_source);
  96. return true;
  97. }
  98. unlink($tempfilename);
  99. $this->errors[] = 'FAILED: copy('.$tempfilename.', '.$this->filename.')';
  100. } else {
  101. $this->errors[] = 'Could not fopen("'.$tempfilename.'", "wb")';
  102. }
  103. }
  104. fclose($fp_source);
  105. return false;
  106. }
  107. }
  108. $this->errors[] = 'Could not fopen("'.$this->filename.'", "r+b")';
  109. return false;
  110. }
  111. function GenerateRMFchunk(&$chunks) {
  112. $oldCONTexists = false;
  113. foreach ($chunks as $key => $chunk) {
  114. $chunkNameKeys[$chunk['name']] = $key;
  115. if ($chunk['name'] == 'CONT') {
  116. $oldCONTexists = true;
  117. }
  118. }
  119. $newHeadersCount = $chunks[$chunkNameKeys['.RMF']]['headers_count'] + ($oldCONTexists ? 0 : 1);
  120. $RMFchunk = "\x00\x00"; // object version
  121. $RMFchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['.RMF']]['file_version'], 4);
  122. $RMFchunk .= getid3_lib::BigEndian2String($newHeadersCount, 4);
  123. $RMFchunk = '.RMF'.getid3_lib::BigEndian2String(strlen($RMFchunk) + 8, 4).$RMFchunk; // .RMF chunk identifier + chunk length
  124. return $RMFchunk;
  125. }
  126. function GeneratePROPchunk(&$chunks, &$new_CONT_tag_data) {
  127. $old_CONT_length = 0;
  128. $old_DATA_offset = 0;
  129. $old_INDX_offset = 0;
  130. foreach ($chunks as $key => $chunk) {
  131. $chunkNameKeys[$chunk['name']] = $key;
  132. if ($chunk['name'] == 'CONT') {
  133. $old_CONT_length = $chunk['length'];
  134. } elseif ($chunk['name'] == 'DATA') {
  135. if (!$old_DATA_offset) {
  136. $old_DATA_offset = $chunk['offset'];
  137. }
  138. } elseif ($chunk['name'] == 'INDX') {
  139. if (!$old_INDX_offset) {
  140. $old_INDX_offset = $chunk['offset'];
  141. }
  142. }
  143. }
  144. $CONTdelta = strlen($new_CONT_tag_data) - $old_CONT_length;
  145. $PROPchunk = "\x00\x00"; // object version
  146. $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['max_bit_rate'], 4);
  147. $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['avg_bit_rate'], 4);
  148. $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['max_packet_size'], 4);
  149. $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['avg_packet_size'], 4);
  150. $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['num_packets'], 4);
  151. $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['duration'], 4);
  152. $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['preroll'], 4);
  153. $PROPchunk .= getid3_lib::BigEndian2String(max(0, $old_INDX_offset + $CONTdelta), 4);
  154. $PROPchunk .= getid3_lib::BigEndian2String(max(0, $old_DATA_offset + $CONTdelta), 4);
  155. $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['num_streams'], 2);
  156. $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['flags_raw'], 2);
  157. $PROPchunk = 'PROP'.getid3_lib::BigEndian2String(strlen($PROPchunk) + 8, 4).$PROPchunk; // PROP chunk identifier + chunk length
  158. return $PROPchunk;
  159. }
  160. function GenerateCONTchunk() {
  161. foreach ($this->tag_data as $key => $value) {
  162. // limit each value to 0xFFFF bytes
  163. $this->tag_data[$key] = substr($value, 0, 65535);
  164. }
  165. $CONTchunk = "\x00\x00"; // object version
  166. $CONTchunk .= getid3_lib::BigEndian2String((!empty($this->tag_data['title']) ? strlen($this->tag_data['title']) : 0), 2);
  167. $CONTchunk .= (!empty($this->tag_data['title']) ? strlen($this->tag_data['title']) : '');
  168. $CONTchunk .= getid3_lib::BigEndian2String((!empty($this->tag_data['artist']) ? strlen($this->tag_data['artist']) : 0), 2);
  169. $CONTchunk .= (!empty($this->tag_data['artist']) ? strlen($this->tag_data['artist']) : '');
  170. $CONTchunk .= getid3_lib::BigEndian2String((!empty($this->tag_data['copyright']) ? strlen($this->tag_data['copyright']) : 0), 2);
  171. $CONTchunk .= (!empty($this->tag_data['copyright']) ? strlen($this->tag_data['copyright']) : '');
  172. $CONTchunk .= getid3_lib::BigEndian2String((!empty($this->tag_data['comment']) ? strlen($this->tag_data['comment']) : 0), 2);
  173. $CONTchunk .= (!empty($this->tag_data['comment']) ? strlen($this->tag_data['comment']) : '');
  174. if ($this->paddedlength > (strlen($CONTchunk) + 8)) {
  175. $CONTchunk .= str_repeat("\x00", $this->paddedlength - strlen($CONTchunk) - 8);
  176. }
  177. $CONTchunk = 'CONT'.getid3_lib::BigEndian2String(strlen($CONTchunk) + 8, 4).$CONTchunk; // CONT chunk identifier + chunk length
  178. return $CONTchunk;
  179. }
  180. function RemoveReal() {
  181. // File MUST be writeable - CHMOD(646) at least
  182. if (is_writeable($this->filename) && is_file($this->filename) && ($fp_source = fopen($this->filename, 'r+b'))) {
  183. // Initialize getID3 engine
  184. $getID3 = new getID3;
  185. $OldThisFileInfo = $getID3->analyze($this->filename);
  186. if (empty($OldThisFileInfo['real']['chunks']) && !empty($OldThisFileInfo['real']['old_ra_header'])) {
  187. $this->errors[] = 'Cannot remove Real tags from old-style file format';
  188. fclose($fp_source);
  189. return false;
  190. }
  191. if (empty($OldThisFileInfo['real']['chunks'])) {
  192. $this->errors[] = 'Cannot remove Real tags because cannot find DATA chunk in file';
  193. fclose($fp_source);
  194. return false;
  195. }
  196. foreach ($OldThisFileInfo['real']['chunks'] as $chunknumber => $chunkarray) {
  197. $oldChunkInfo[$chunkarray['name']] = $chunkarray;
  198. }
  199. if (empty($oldChunkInfo['CONT'])) {
  200. // no existing CONT chunk
  201. fclose($fp_source);
  202. return true;
  203. }
  204. $BeforeOffset = $oldChunkInfo['CONT']['offset'];
  205. $AfterOffset = $oldChunkInfo['CONT']['offset'] + $oldChunkInfo['CONT']['length'];
  206. if ($tempfilename = tempnam(GETID3_TEMP_DIR, 'getID3')) {
  207. if (is_writable($tempfilename) && is_file($tempfilename) && ($fp_temp = fopen($tempfilename, 'wb'))) {
  208. rewind($fp_source);
  209. fwrite($fp_temp, fread($fp_source, $BeforeOffset));
  210. fseek($fp_source, $AfterOffset, SEEK_SET);
  211. while ($buffer = fread($fp_source, $this->fread_buffer_size)) {
  212. fwrite($fp_temp, $buffer, strlen($buffer));
  213. }
  214. fclose($fp_temp);
  215. if (copy($tempfilename, $this->filename)) {
  216. unlink($tempfilename);
  217. fclose($fp_source);
  218. return true;
  219. }
  220. unlink($tempfilename);
  221. $this->errors[] = 'FAILED: copy('.$tempfilename.', '.$this->filename.')';
  222. } else {
  223. $this->errors[] = 'Could not fopen("'.$tempfilename.'", "wb")';
  224. }
  225. }
  226. fclose($fp_source);
  227. return false;
  228. }
  229. $this->errors[] = 'Could not fopen("'.$this->filename.'", "r+b")';
  230. return false;
  231. }
  232. }
  233. ?>