<?php

require_once('BitStream.php');
require_once(
'FlashSWF/TagBlock.php');

class 
FlashSWF {
        private 
$_header = array();
        private 
$_movie_header = array();
    private 
$_block_list = array();
        function 
__construct() {
      return ;
    }
        function 
loadfile($filename) {
            
$swfdata file_get_contents($filename);
        if (
$swfdata === FALSE) {
          
// throw Exception("Can't open file($filename)");
                
return FALSE;
        }
            
$this->load($swfdata);
    }
        function 
load($swfdata) {
            
$bs = new BitStream($swfdata);
        
$magic $bs->getStrings(3);
        
$version $bs->getByte();
        
$filelength $bs->getBytesLE(4);
                
$this->_header['magic'] = $magic;
                
$this->_header['version'] = $version;
        
$this->_header['filelength'] = $filelength;
        if (
$magic == 'FWS') {
                ;
        } elseif (
$magic == 'CWS') {
                
$movie_and_body gzuncompress($bs->getStrings(), $filelength);
            
$bs = new BitStream($movie_and_body);
        } else {
                return ;
        }
        
$this->_movie_header $this->get_movie_header($bs);
        while(
true) {
          
$block FlashSWF_TagBlock::Factory($bs);
          
$this->_block_list[] = $block;
          if (
$block->getTagId() == 0) {
            break;
          }
        }
    }
    function 
get_movie_header(&$bs) {
      
$movie_header = array();
      
$movie_align $bs->getBits(5);
      
$movie_header['align'] = $movie_align;
      
$movie_header['x_min'] = $bs->getBits($movie_align);
      
$movie_header['x_max'] = $bs->getBits($movie_align);
      
$movie_header['y_min'] = $bs->getBits($movie_align);
      
$movie_header['y_max'] = $bs->getBits($movie_align);
      
$bs->align();
      
$movie_header['frame_rate_decimal'] = $bs->getByte();
      
$movie_header['frame_rate_integer'] = $bs->getByte();
      
$movie_header['frame_rate_count'] = $bs->getBytesLE(2);
      return 
$movie_header;
    }
        function 
savefile($filename) {
            
$swfdata $this->save();
        
file_put_contents($filename$swfdata);
    }
        function 
save() {
      
/* header */
      
$bs = new BitStream();
      
$bs->putStrings($this->_header['magic']);
      
$bs->putByte($this->_header['version']);
      
$bs->putBytesLE($this->_header['filelength'], 4);
      
$header $bs->getData();
      
/* movie header and body */
      
$bs = new BitStream();
      
$movie_header $this->_movie_header;
      
$movie_align $movie_header['align'];
      
$bs->putBits($movie_align5);
      
$bs->putBits($movie_header['x_min'], $movie_align);
      
$bs->putBits($movie_header['x_max'], $movie_align);
      
$bs->putBits($movie_header['y_min'], $movie_align);
      
$bs->putBits($movie_header['y_max'], $movie_align);
      
$bs->flush();
      
$bs->putByte($movie_header['frame_rate_decimal']);
      
$bs->putByte($movie_header['frame_rate_integer']);
      
$bs->putBytesLE($movie_header['frame_rate_count'], 2);
      
$movie_and_body $bs->getData();
      foreach(
$this->_block_list as $block) {
              
$movie_and_body .=  $block->save();
      }
      return 
$header $movie_and_body;
    }
    function 
dump() {
                
var_dump($this->_header);
                
var_dump($this->_movie_header);
        foreach (
$this->_block_list as $block) {
                
$block->dump();
        }
    }
}