| 1 |
<?PHP // $Id$ //
|
| 2 |
|
| 3 |
//-----------------------------------------------------------------------------
|
| 4 |
// SWF HEADER - version 1.0
|
| 5 |
// Small utility class to determine basic data from a SWF file header
|
| 6 |
// Does not need any php-flash extension, based on raw binary data reading
|
| 7 |
// This class is maintained at http://slainte.phpclasses.org/swfheader
|
| 8 |
// This class got 2nd place on PHPCLASSES INNOVATION AWARD in May '04
|
| 9 |
// Suggestion - Set TAB size to 2 for easy reading
|
| 10 |
//-----------------------------------------------------------------------------
|
| 11 |
// SWFHEADER CLASS - PHP SWF header parser
|
| 12 |
// Copyright (C) 2004 Carlos Falo Herv?s
|
| 13 |
//
|
| 14 |
// This library is free software; you can redistribute it and/or
|
| 15 |
// modify it under the terms of the GNU Lesser General Public
|
| 16 |
// License as published by the Free Software Foundation; either
|
| 17 |
// version 2.1 of the License, or (at your option) any later version.
|
| 18 |
//
|
| 19 |
// This library is distributed in the hope that it will be useful,
|
| 20 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 21 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 22 |
// Lesser General Public License for more details.
|
| 23 |
//
|
| 24 |
// You should have received a copy of the GNU Lesser General Public
|
| 25 |
// License along with this library; if not, write to the Free Software
|
| 26 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 27 |
//-----------------------------------------------------------------------------
|
| 28 |
|
| 29 |
class swfheader {
|
| 30 |
|
| 31 |
var $debug ; // Output DEBUG info
|
| 32 |
var $fname ; // SWF file analyzed
|
| 33 |
var $magic ; // Magic in a SWF file (FWS or CWS)
|
| 34 |
var $compressed ; // Flag to indicate a compressed file (CWS)
|
| 35 |
var $version ; // Flash version
|
| 36 |
var $size ; // Uncompressed file size (in bytes)
|
| 37 |
var $width ; // Flash movie native width
|
| 38 |
var $height ; // Flash movie native height
|
| 39 |
var $valid ; // Valid SWF file
|
| 40 |
var $fps ; // Flash movie native frame-rate
|
| 41 |
var $frames ; // Flash movie total frames
|
| 42 |
|
| 43 |
//---------------------------------------------------------------------------
|
| 44 |
// swfheader($debug) : Constructor, basically does nothing but initilize
|
| 45 |
// debug and data fields
|
| 46 |
//---------------------------------------------------------------------------
|
| 47 |
function swfheader($debug = false) {
|
| 48 |
$this->debug = $debug ;
|
| 49 |
$this->init() ;
|
| 50 |
}
|
| 51 |
|
| 52 |
//---------------------------------------------------------------------------
|
| 53 |
// init() : initialize the data fields to "empty" values
|
| 54 |
//---------------------------------------------------------------------------
|
| 55 |
function init() {
|
| 56 |
$this->valid = false ;
|
| 57 |
$this->fname = "" ;
|
| 58 |
$this->magic = "" ;
|
| 59 |
$this->compressed = false ;
|
| 60 |
$this->version = 0 ;
|
| 61 |
$this->width = 0 ;
|
| 62 |
$this->height = 0 ;
|
| 63 |
$this->size = 0 ;
|
| 64 |
$this->frames = 0 ;
|
| 65 |
$this->fps[] = Array() ;
|
| 66 |
if ($this->debug) echo "DEBUG: Data values initialized<br>" ;
|
| 67 |
}
|
| 68 |
|
| 69 |
//---------------------------------------------------------------------------
|
| 70 |
// loadswf($filename) : loads $filename and stores data from it's header
|
| 71 |
//---------------------------------------------------------------------------
|
| 72 |
function loadswf($filename) {
|
| 73 |
$this->fname = $filename ;
|
| 74 |
$fp = @fopen($filename,"rb") ;
|
| 75 |
if ($fp) {
|
| 76 |
if ($this->debug) echo "DEBUG: Opened " . $this->fname . "<br>" ;
|
| 77 |
// Read MAGIC FIELD
|
| 78 |
$this->magic = fread($fp,3) ;
|
| 79 |
if ($this->magic!="FWS" && $this->magic!="CWS") {
|
| 80 |
if ($this->debug) echo "DEBUG: " . $this->fname . " is not a valid/supported SWF file<br>" ;
|
| 81 |
$this->valid = 0 ;
|
| 82 |
} else {
|
| 83 |
// Compression
|
| 84 |
if (substr($this->magic,0,1)=="C") $this->compressed = true ;
|
| 85 |
else $this->compressed = false ;
|
| 86 |
if ($this->debug) echo "DEBUG: Read MAGIC signature: " . $this->magic . "<br>" ;
|
| 87 |
// Version
|
| 88 |
$this->version = ord(fread($fp,1)) ;
|
| 89 |
if ($this->debug) echo "DEBUG: Read VERSION: " . $this->version . "<br>" ;
|
| 90 |
// Size
|
| 91 |
$lg = 0 ;
|
| 92 |
// 4 LSB-MSB
|
| 93 |
for ($i=0;$i<4;$i++) {
|
| 94 |
$t = ord(fread($fp,1)) ;
|
| 95 |
if ($this->debug) echo "DEBUG: Partial SIZE read: " . ($t<<(8*$i)) . "<br>" ;
|
| 96 |
$lg += ($t<<(8*$i)) ;
|
| 97 |
}
|
| 98 |
$this->size = $lg ;
|
| 99 |
if ($this->debug) echo "DEBUG: Total SIZE: " . $this->size . "<br>" ;
|
| 100 |
// RECT... we will "simulate" a stream from now on... read remaining file
|
| 101 |
$buffer = fread($fp,$this->size) ;
|
| 102 |
if ($this->compressed) {
|
| 103 |
// First decompress GZ stream
|
| 104 |
$buffer = gzuncompress($buffer,$this->size) ;
|
| 105 |
}
|
| 106 |
$b = ord(substr($buffer,0,1)) ;
|
| 107 |
$buffer = substr($buffer,1) ;
|
| 108 |
$cbyte = $b ;
|
| 109 |
$bits = $b>>3 ;
|
| 110 |
if ($this->debug) echo "DEBUG: RECT field size: " . $bits . " bits<br>" ;
|
| 111 |
$cval = "" ;
|
| 112 |
// Current byte
|
| 113 |
$cbyte &= 7 ;
|
| 114 |
$cbyte<<= 5 ;
|
| 115 |
// Current bit (first byte starts off already shifted)
|
| 116 |
$cbit = 2 ;
|
| 117 |
// Must get all 4 values in the RECT
|
| 118 |
for ($vals=0;$vals<4;$vals++) {
|
| 119 |
$bitcount = 0 ;
|
| 120 |
while ($bitcount<$bits) {
|
| 121 |
if ($cbyte&128) {
|
| 122 |
$cval .= "1" ;
|
| 123 |
} else {
|
| 124 |
$cval.="0" ;
|
| 125 |
}
|
| 126 |
$cbyte<<=1 ;
|
| 127 |
$cbyte &= 255 ;
|
| 128 |
$cbit-- ;
|
| 129 |
$bitcount++ ;
|
| 130 |
// We will be needing a new byte if we run out of bits
|
| 131 |
if ($cbit<0) {
|
| 132 |
$cbyte = ord(substr($buffer,0,1)) ;
|
| 133 |
$buffer = substr($buffer,1) ;
|
| 134 |
$cbit = 7 ;
|
| 135 |
}
|
| 136 |
}
|
| 137 |
// O.k. full value stored... calculate
|
| 138 |
$c = 1 ;
|
| 139 |
$val = 0 ;
|
| 140 |
// Reverse string to allow for SUM(2^n*$atom)
|
| 141 |
if ($this->debug) echo "DEBUG: RECT binary value: " . $cval ;
|
| 142 |
$tval = strrev($cval) ;
|
| 143 |
for ($n=0;$n<strlen($tval);$n++) {
|
| 144 |
$atom = substr($tval,$n,1) ;
|
| 145 |
if ($atom=="1") $val+=$c ;
|
| 146 |
// 2^n
|
| 147 |
$c*=2 ;
|
| 148 |
}
|
| 149 |
// TWIPS to PIXELS
|
| 150 |
$val/=20 ;
|
| 151 |
if ($this->debug) echo " (" . $val . ")<br>" ;
|
| 152 |
switch ($vals) {
|
| 153 |
case 0:
|
| 154 |
// tmp value
|
| 155 |
$this->width = $val ;
|
| 156 |
break ;
|
| 157 |
case 1:
|
| 158 |
$this->width = $val - $this->width ;
|
| 159 |
break ;
|
| 160 |
case 2:
|
| 161 |
// tmp value
|
| 162 |
$this->height = $val ;
|
| 163 |
break ;
|
| 164 |
case 3:
|
| 165 |
$this->height = $val - $this->height ;
|
| 166 |
break ;
|
| 167 |
}
|
| 168 |
$cval = "" ;
|
| 169 |
}
|
| 170 |
// Frame rate
|
| 171 |
$this->fps = Array() ;
|
| 172 |
for ($i=0;$i<2;$i++) {
|
| 173 |
$t = ord(substr($buffer,0,1)) ;
|
| 174 |
$buffer = substr($buffer,1) ;
|
| 175 |
$this->fps[] = $t ;
|
| 176 |
}
|
| 177 |
if ($this->debug) echo "DEBUG: Frame rate: " . $this->fps[1] . "." . $this->fps[0] . "<br>" ;
|
| 178 |
// Frames
|
| 179 |
$this->frames = 0 ;
|
| 180 |
for ($i=0;$i<2;$i++) {
|
| 181 |
$t = ord(substr($buffer,0,1)) ;
|
| 182 |
$buffer = substr($buffer,1) ;
|
| 183 |
$this->frames += ($t<<(8*$i)) ;
|
| 184 |
}
|
| 185 |
if ($this->debug) echo "DEBUG: Frames: " . $this->frames . "<br>" ;
|
| 186 |
fclose($fp) ;
|
| 187 |
if ($this->debug) echo "DEBUG: Finished processing " . $this->fname . "<br>" ;
|
| 188 |
$this->valid = 1 ;
|
| 189 |
}
|
| 190 |
} else {
|
| 191 |
$this->valid = 0 ;
|
| 192 |
if ($this->debug) echo "DEBUG: Failed to open " . $this->fname . "<br>" ;
|
| 193 |
}
|
| 194 |
return $this->valid ;
|
| 195 |
}
|
| 196 |
|
| 197 |
//---------------------------------------------------------------------------
|
| 198 |
// show() : report to screen all the header info
|
| 199 |
//---------------------------------------------------------------------------
|
| 200 |
function show() {
|
| 201 |
if ($this->valid) {
|
| 202 |
// FNAME
|
| 203 |
echo "<b>FILE: " . $this->fname . "</b><br>" ;
|
| 204 |
// Magic
|
| 205 |
echo "<b>MAGIC:</b> " . $this->magic ;
|
| 206 |
if ($this->compressed) echo " (COMPRESSED)" ;
|
| 207 |
echo "<br>" ;
|
| 208 |
// Version
|
| 209 |
echo "<b>VERSION:</b> " . $this->version . "<br>" ;
|
| 210 |
// Size
|
| 211 |
echo "<b>SIZE:</b> " . $this->size . " bytes <br>" ;
|
| 212 |
// FRAMESIZE
|
| 213 |
echo "<b>WIDHT:</B> " . $this->width . "<br>";
|
| 214 |
echo "<b>HEIGHT:</B> " . $this->height . "<br>" ;
|
| 215 |
// FPS
|
| 216 |
echo "<b>FPS:</b> " . $this->fps[1] . "." . $this->fps[0] . " Frames/s <br>" ;
|
| 217 |
// FRAMES
|
| 218 |
echo "<b>FRAMES:</b> " . $this->frames . " FRAME <br>" ;
|
| 219 |
} else {
|
| 220 |
if (file_exists($this->fname))
|
| 221 |
echo $this->fname . "is not a valid SWF file<br>" ;
|
| 222 |
else
|
| 223 |
if ($this->fname=="")
|
| 224 |
echo "SWFHEADER->SHOW : No file loaded<br>" ;
|
| 225 |
else
|
| 226 |
echo "SWFHEDAR->SHOW : " . $this->fname . "was not found<br>" ;
|
| 227 |
}
|
| 228 |
}
|
| 229 |
|
| 230 |
//---------------------------------------------------------------------------
|
| 231 |
// display($trans) : just echo <OBJECT>/<EMBED> tags for the parsed file, if
|
| 232 |
// trans is set, WMODE is set to transparent. Uses alwyas
|
| 233 |
// Flash 7 player... can be tewaked to use exact versions
|
| 234 |
//---------------------------------------------------------------------------
|
| 235 |
function display($trans = false, $qlty = "high", $bgcolor = "#ffffff", $name = "") {
|
| 236 |
|
| 237 |
$endl = chr(13) ;
|
| 238 |
|
| 239 |
if ($this->valid) {
|
| 240 |
if ($name=="") $name = substr($this->fname,0,strrpos($this->fname,".")) ;
|
| 241 |
echo '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' . $this->version . ',0,0,0" width="' . $this->width . '" height="' . $this->height . '" id="' . $name . '" align="middle">' . $endl ;
|
| 242 |
echo '<param name="allowScriptAccess" value="sameDomain" />' . $endl ;
|
| 243 |
if ($trans) {
|
| 244 |
echo '<param name="wmode" value="transparent" />' . $endl ;
|
| 245 |
}
|
| 246 |
echo '<param name="movie" value="' . $this->fname . '" />' . $endl ;
|
| 247 |
echo '<param name="quality" value="' . $qlty . '" />' . $endl ;
|
| 248 |
echo '<param name="bgcolor" value="' . $bgcolor .'" />' . $endl ;
|
| 249 |
echo '<embed src="' . $this->fname . '" ';
|
| 250 |
if ($trans) echo 'wmode="transparent" ' ;
|
| 251 |
echo 'quality="' . $qlty . '" bgcolor="' . $bgcolor . '" width="' . $this->width . '" height="' . $this->height . '" name="' . $name . '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' . $endl ;
|
| 252 |
echo '</object>' . $endl ;
|
| 253 |
} else {
|
| 254 |
if ($this->debug) {
|
| 255 |
if ($this->fname=="") {
|
| 256 |
echo "SWFHEADER->DISPLAY : No loaded file in the object<br>" ;
|
| 257 |
} else {
|
| 258 |
if (file_exists($this->fname)) {
|
| 259 |
echo "SWFHEADER->DISPLAY : " . $this->fname . " is not a valid SWF file<br>" ;
|
| 260 |
} else {
|
| 261 |
echo "SWFHEADER->DISPLAY : " . $this->fname . " was not found<br>" ;
|
| 262 |
}
|
| 263 |
}
|
| 264 |
}
|
| 265 |
}
|
| 266 |
}
|
| 267 |
}
|
| 268 |
?>
|