module.audio-video.swf.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio-video.swf.php //
  11. // module for analyzing Shockwave Flash files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  16. exit;
  17. }
  18. class getid3_swf extends getid3_handler
  19. {
  20. public $ReturnAllTagData = false;
  21. /**
  22. * @return bool
  23. */
  24. public function Analyze() {
  25. $info = &$this->getid3->info;
  26. $info['fileformat'] = 'swf';
  27. $info['video']['dataformat'] = 'swf';
  28. // http://www.openswf.org/spec/SWFfileformat.html
  29. $this->fseek($info['avdataoffset']);
  30. $SWFfileData = $this->fread($info['avdataend'] - $info['avdataoffset']); // 8 + 2 + 2 + max(9) bytes NOT including Frame_Size RECT data
  31. $info['swf']['header']['signature'] = substr($SWFfileData, 0, 3);
  32. switch ($info['swf']['header']['signature']) {
  33. case 'FWS':
  34. $info['swf']['header']['compressed'] = false;
  35. break;
  36. case 'CWS':
  37. $info['swf']['header']['compressed'] = true;
  38. break;
  39. default:
  40. $this->error('Expecting "FWS" or "CWS" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['swf']['header']['signature']).'"');
  41. unset($info['swf']);
  42. unset($info['fileformat']);
  43. return false;
  44. }
  45. $info['swf']['header']['version'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 3, 1));
  46. $info['swf']['header']['length'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 4, 4));
  47. if ($info['swf']['header']['compressed']) {
  48. $SWFHead = substr($SWFfileData, 0, 8);
  49. $SWFfileData = substr($SWFfileData, 8);
  50. if ($decompressed = @gzuncompress($SWFfileData)) {
  51. $SWFfileData = $SWFHead.$decompressed;
  52. } else {
  53. $this->error('Error decompressing compressed SWF data ('.strlen($SWFfileData).' bytes compressed, should be '.($info['swf']['header']['length'] - 8).' bytes uncompressed)');
  54. return false;
  55. }
  56. }
  57. $FrameSizeBitsPerValue = (ord(substr($SWFfileData, 8, 1)) & 0xF8) >> 3;
  58. $FrameSizeDataLength = ceil((5 + (4 * $FrameSizeBitsPerValue)) / 8);
  59. $FrameSizeDataString = str_pad(decbin(ord(substr($SWFfileData, 8, 1)) & 0x07), 3, '0', STR_PAD_LEFT);
  60. for ($i = 1; $i < $FrameSizeDataLength; $i++) {
  61. $FrameSizeDataString .= str_pad(decbin(ord(substr($SWFfileData, 8 + $i, 1))), 8, '0', STR_PAD_LEFT);
  62. }
  63. list($X1, $X2, $Y1, $Y2) = explode("\n", wordwrap($FrameSizeDataString, $FrameSizeBitsPerValue, "\n", 1));
  64. $info['swf']['header']['frame_width'] = getid3_lib::Bin2Dec($X2);
  65. $info['swf']['header']['frame_height'] = getid3_lib::Bin2Dec($Y2);
  66. // http://www-lehre.informatik.uni-osnabrueck.de/~fbstark/diplom/docs/swf/Flash_Uncovered.htm
  67. // Next in the header is the frame rate, which is kind of weird.
  68. // It is supposed to be stored as a 16bit integer, but the first byte
  69. // (or last depending on how you look at it) is completely ignored.
  70. // Example: 0x000C -> 0x0C -> 12 So the frame rate is 12 fps.
  71. // Byte at (8 + $FrameSizeDataLength) is always zero and ignored
  72. $info['swf']['header']['frame_rate'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 9 + $FrameSizeDataLength, 1));
  73. $info['swf']['header']['frame_count'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 10 + $FrameSizeDataLength, 2));
  74. $info['video']['frame_rate'] = $info['swf']['header']['frame_rate'];
  75. $info['video']['resolution_x'] = intval(round($info['swf']['header']['frame_width'] / 20));
  76. $info['video']['resolution_y'] = intval(round($info['swf']['header']['frame_height'] / 20));
  77. $info['video']['pixel_aspect_ratio'] = (float) 1;
  78. if (($info['swf']['header']['frame_count'] > 0) && ($info['swf']['header']['frame_rate'] > 0)) {
  79. $info['playtime_seconds'] = $info['swf']['header']['frame_count'] / $info['swf']['header']['frame_rate'];
  80. }
  81. //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
  82. // SWF tags
  83. $CurrentOffset = 12 + $FrameSizeDataLength;
  84. $SWFdataLength = strlen($SWFfileData);
  85. while ($CurrentOffset < $SWFdataLength) {
  86. //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
  87. $TagIDTagLength = getid3_lib::LittleEndian2Int(substr($SWFfileData, $CurrentOffset, 2));
  88. $TagID = ($TagIDTagLength & 0xFFFC) >> 6;
  89. $TagLength = ($TagIDTagLength & 0x003F);
  90. $CurrentOffset += 2;
  91. if ($TagLength == 0x3F) {
  92. $TagLength = getid3_lib::LittleEndian2Int(substr($SWFfileData, $CurrentOffset, 4));
  93. $CurrentOffset += 4;
  94. }
  95. unset($TagData);
  96. $TagData['offset'] = $CurrentOffset;
  97. $TagData['size'] = $TagLength;
  98. $TagData['id'] = $TagID;
  99. $TagData['data'] = substr($SWFfileData, $CurrentOffset, $TagLength);
  100. switch ($TagID) {
  101. case 0: // end of movie
  102. break 2;
  103. case 9: // Set background color
  104. //$info['swf']['tags'][] = $TagData;
  105. $info['swf']['bgcolor'] = strtoupper(str_pad(dechex(getid3_lib::BigEndian2Int($TagData['data'])), 6, '0', STR_PAD_LEFT));
  106. break;
  107. default:
  108. if ($this->ReturnAllTagData) {
  109. $info['swf']['tags'][] = $TagData;
  110. }
  111. break;
  112. }
  113. $CurrentOffset += $TagLength;
  114. }
  115. return true;
  116. }
  117. }