<?php

if (isset($argv[1]) && isset($argv[2])) {
// Usage: php change_background.php <file> <color>
    
$_FILES = array('swffile' => array('tmp_name' => $argv[1]));
    
$_REQUEST = array('color' => $argv[2]);
}

if (! empty(
$_FILES['swffile']['tmp_name']) && isset($_REQUEST['color'])) {
    
$filename $_FILES['swffile']['tmp_name'];
    
$swfdata file_get_contents($filename);
    if (
$swfdata 67108864) {
        echo 
' 64M Bytes 以内のファイルしか受け付けません。'."\n";
        exit(
0);
    }
    
$color$_REQUEST['color'];
    
preg_match('/([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})/',
               
$color$matches);
    list(
$all$red$green$blue) = $matches;
} else {
echo <<< FORM
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<form enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="67108864" />
    SWF ファイルをアップロード: <input name="swffile" type="file" />
    <input type="text" name="color" value="
$color" />
    <input type="submit" name="action" value="change!" />
</form>
          ファイルと色(000000~FFFFFF)を指定してください。(64MBytes 以内に限定してます)
<p />
ソースはこちら → <a href="change_background.phps"> change_background.phps </a>
</body>
</html>
FORM;
    exit(
0);
}

$subject "C\02"// 0x4302 = {tag:9, length:3 }
$header_skip_size 11;
$compress false;

if (
strpos($swfdata"CWS") === 0) {
    
$head4bytes substr($swfdata04);
    
$swfdata gzuncompress(substr($swfdata8));
    
$header_skip_size -= 8;
    
$compress true;
}


$tag_offset strpos($swfdata$subject$header_skip_size);

$replace $subject;
$replace .= sprintf("%c%c%c"hexdec($red), hexdec($green), hexdec($blue));
$swfdata substr_replace($swfdata$replace$tag_offsetstrlen($replace));

if (
$compress) { // for CWS
   
$file_length strlen($swfdata);
   
$swfdata $head4bytes pack("V"$file_length).gzcompress($swfdata5);
}

header('Content-type: application/x-shockwave-flash');
echo 
$swfdata;