ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Swf.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
10 // available at http://getid3.sourceforge.net //
11 // or http://www.getid3.org //
13 // See readme.txt for more details //
15 // //
16 // module.audio-video.swf.php //
17 // module for analyzing Shockwave Flash files //
18 // dependencies: NONE //
19 // ///
21 
29 class Swf extends BaseHandler
30 {
35  public $ReturnAllTagData = false;
36 
41  public function analyze()
42  {
43  $info = &$this->getid3->info;
44 
45  $info['fileformat'] = 'swf';
46  $info['video']['dataformat'] = 'swf';
47 
48  // http://www.openswf.org/spec/SWFfileformat.html
49 
50  fseek($this->getid3->fp, $info['avdataoffset'], SEEK_SET);
51 
52  $SWFfileData = fread($this->getid3->fp, $info['avdataend'] - $info['avdataoffset']); // 8 + 2 + 2 + max(9) bytes NOT including Frame_Size RECT data
53 
54  $info['swf']['header']['signature'] = substr($SWFfileData, 0, 3);
55  switch ($info['swf']['header']['signature']) {
56  case 'FWS':
57  $info['swf']['header']['compressed'] = false;
58  break;
59 
60  case 'CWS':
61  $info['swf']['header']['compressed'] = true;
62  break;
63 
64  default:
65  $info['error'][] = 'Expecting "FWS" or "CWS" at offset '.$info['avdataoffset'].', found "'.Helper::PrintHexBytes($info['swf']['header']['signature']).'"';
66  unset($info['swf']);
67  unset($info['fileformat']);
68 
69  return false;
70  break;
71  }
72  $info['swf']['header']['version'] = Helper::LittleEndian2Int(substr($SWFfileData, 3, 1));
73  $info['swf']['header']['length'] = Helper::LittleEndian2Int(substr($SWFfileData, 4, 4));
74 
75  if ($info['swf']['header']['compressed']) {
76  $SWFHead = substr($SWFfileData, 0, 8);
77  $SWFfileData = substr($SWFfileData, 8);
78  if ($decompressed = @gzuncompress($SWFfileData)) {
79  $SWFfileData = $SWFHead.$decompressed;
80  } else {
81  $info['error'][] = 'Error decompressing compressed SWF data ('.strlen($SWFfileData).' bytes compressed, should be '.($info['swf']['header']['length'] - 8).' bytes uncompressed)';
82 
83  return false;
84  }
85  }
86 
87  $FrameSizeBitsPerValue = (ord(substr($SWFfileData, 8, 1)) & 0xF8) >> 3;
88  $FrameSizeDataLength = ceil((5 + (4 * $FrameSizeBitsPerValue)) / 8);
89  $FrameSizeDataString = str_pad(decbin(ord(substr($SWFfileData, 8, 1)) & 0x07), 3, '0', STR_PAD_LEFT);
90  for ($i = 1; $i < $FrameSizeDataLength; $i++) {
91  $FrameSizeDataString .= str_pad(decbin(ord(substr($SWFfileData, 8 + $i, 1))), 8, '0', STR_PAD_LEFT);
92  }
93  list($X1, $X2, $Y1, $Y2) = explode("\n", wordwrap($FrameSizeDataString, $FrameSizeBitsPerValue, "\n", 1));
94  $info['swf']['header']['frame_width'] = Helper::Bin2Dec($X2);
95  $info['swf']['header']['frame_height'] = Helper::Bin2Dec($Y2);
96 
97  // http://www-lehre.informatik.uni-osnabrueck.de/~fbstark/diplom/docs/swf/Flash_Uncovered.htm
98  // Next in the header is the frame rate, which is kind of weird.
99  // It is supposed to be stored as a 16bit integer, but the first byte
100  // (or last depending on how you look at it) is completely ignored.
101  // Example: 0x000C -> 0x0C -> 12 So the frame rate is 12 fps.
102 
103  // Byte at (8 + $FrameSizeDataLength) is always zero and ignored
104  $info['swf']['header']['frame_rate'] = Helper::LittleEndian2Int(substr($SWFfileData, 9 + $FrameSizeDataLength, 1));
105  $info['swf']['header']['frame_count'] = Helper::LittleEndian2Int(substr($SWFfileData, 10 + $FrameSizeDataLength, 2));
106 
107  $info['video']['frame_rate'] = $info['swf']['header']['frame_rate'];
108  $info['video']['resolution_x'] = intval(round($info['swf']['header']['frame_width'] / 20));
109  $info['video']['resolution_y'] = intval(round($info['swf']['header']['frame_height'] / 20));
110  $info['video']['pixel_aspect_ratio'] = (float) 1;
111 
112  if (($info['swf']['header']['frame_count'] > 0) && ($info['swf']['header']['frame_rate'] > 0)) {
113  $info['playtime_seconds'] = $info['swf']['header']['frame_count'] / $info['swf']['header']['frame_rate'];
114  }
115 //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
116 
117  // SWF tags
118 
119  $CurrentOffset = 12 + $FrameSizeDataLength;
120  $SWFdataLength = strlen($SWFfileData);
121 
122  while ($CurrentOffset < $SWFdataLength) {
123 //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
124 
125  $TagIDTagLength = Helper::LittleEndian2Int(substr($SWFfileData, $CurrentOffset, 2));
126  $TagID = ($TagIDTagLength & 0xFFFC) >> 6;
127  $TagLength = ($TagIDTagLength & 0x003F);
128  $CurrentOffset += 2;
129  if ($TagLength == 0x3F) {
130  $TagLength = Helper::LittleEndian2Int(substr($SWFfileData, $CurrentOffset, 4));
131  $CurrentOffset += 4;
132  }
133 
134  unset($TagData);
135  $TagData['offset'] = $CurrentOffset;
136  $TagData['size'] = $TagLength;
137  $TagData['id'] = $TagID;
138  $TagData['data'] = substr($SWFfileData, $CurrentOffset, $TagLength);
139  switch ($TagID) {
140  case 0: // end of movie
141  break 2;
142 
143  case 9: // Set background color
144  //$info['swf']['tags'][] = $TagData;
145  $info['swf']['bgcolor'] = strtoupper(str_pad(dechex(Helper::BigEndian2Int($TagData['data'])), 6, '0', STR_PAD_LEFT));
146  break;
147 
148  default:
149  if ($this->ReturnAllTagData) {
150  $info['swf']['tags'][] = $TagData;
151  }
152  break;
153  }
154 
155  $CurrentOffset += $TagLength;
156  }
157 
158  return true;
159  }
160 }
static PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8')
Definition: Helper.php:36
GetId3() by James Heinrich info@getid3.org //.
Definition: BaseHandler.php:25
GetId3() by James Heinrich info@getid3.org //.
Definition: Swf.php:29
fseek($bytes, $whence=SEEK_SET)
$info
Definition: example_052.php:80
static LittleEndian2Int($byteword, $signed=false)
Definition: Helper.php:413
static BigEndian2Int($byteword, $synchsafe=false, $signed=false)
Definition: Helper.php:374
static Bin2Dec($binstring, $signed=false)
Definition: Helper.php:496