<?php
    define('THIS_SCRIPT', 'dv_get_cercode_swf');
    require_once 'global.php';
    session_onstart();
    session_register('swfcode');
    @header("Expires: -1");
    @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
    @header("Pragma: no-cache");

    if(function_exists("bcadd")&&function_exists("bcmul")){
       $swfcode=createSwfCercode(4);
       $swf_binary=createCercodeSwfBinary($swfcode,"23909","2841418339");
    }else{
       $swf_jpgBinary="0DXzdlzMPFwGDbeFFzMPFwGFNDN1zMPFwykq5GVzMPFwF97uK".
                      "FzMPFw51bzV1zMPFwhmJGZVzMPFwO0ntTlzMPFw86gFaFzMPF".
                      "wiJ3tUFzMPFwboVhEFzMPFwHmqmclzMPFwwMgGF1zMPFwpfOI".
                      "p1zMPFwpHP7glzMPFwNZLSG1zMPFwO6UnElzMPFwurfkalzMP".
                      "FwApsBkVzMPFw1jN6eVzMPFwIumdVVzMPFwlAvpK1zMPFwjQOEk1zMPFw";
       $swf_jpg="3ElOBGggNlhxLgbMBan4832uKHBnK7g8deAYmpbbYOUR4BNHlvOxS3pe8VRIZ0NBEqLPpTTlMaLxuWQXGnGy0IW7kr7V";
       mt_srand((double)microtime() * 1000000);
       $jpgindex=mt_rand(0,strlen($swf_jpg)/4-1);
       $swfcode=substr($swf_jpg,4*$jpgindex,4);
       $swf_binary=substr($swf_jpgBinary,11*$jpgindex,11);
    }
    $_SESSION["swfcode"]=$swfcode;
    echo "swfcb64=".$swf_binary."&cw=200"."&ch=48";
    function int2bin($num)
    {
        $result = '';
        do {
            $result .= chr(bcmod($num, '256'));
            $num = bcdiv($num, '256');
        } while (bccomp($num, '0'));
        return $result;
    }

    function bin2int($str)
    {
        $result = '0';
        $n = strlen($str);
        do {
            $result = bcadd(bcmul($result, '256'), ord($str{--$n}));
        } while ($n > 0);
        return $result;
    }

    function bitLen($num)
    {
        $tmp = int2bin($num);
        $bit_len = strlen($tmp) * 8;
        $tmp = ord($tmp{strlen($tmp) - 1});
        if (!$tmp) {
            $bit_len -= 8;
        }
        else {
            while (!($tmp & 0x80)) {
                $bit_len--;
                $tmp <<= 1;
            }
        }
        return $bit_len;
    }

     function subint($num, $start, $length)
    {
        $start_byte = intval($start / 8);
        $start_bit = $start % 8;
        $byte_length = intval($length / 8);
        $bit_length = $length % 8;
        if ($bit_length) {
            $byte_length++;
        }
        $num = bcdiv($num, 1 << $start_bit);
        $tmp = substr(int2bin($num), $start_byte, $byte_length);
        $tmp = str_pad($tmp, $byte_length, "\0");
        $tmp = substr_replace($tmp, $tmp{$byte_length - 1} & chr(0xff >> (8 - $bit_length)), $byte_length - 1, 1);
        return bin2int($tmp);
    }

    function powmod($num, $pow, $mod)
    {
        if (function_exists('bcpowmod')) {
            return bcpowmod($num, $pow, $mod);
        }

        $result = '1';
        do {
            if (!bccomp(bcmod($pow, '2'), '1')) {
                $result = bcmod(bcmul($result, $num), $mod);
            }
            $num = bcmod(bcpow($num, '2'), $mod);
            $pow = bcdiv($pow, '2');
        } while (bccomp($pow, '0'));
        return $result;
    }

    //create a $swf_size_bytes bytes flash file base on the basec_bin_data
   function createCercodeSwfBinary($swf_data_int, $swf_size_bytes,$base_bin_data)
    {
        $swf_data_int .= "\x01";
        $swf_data_int = bin2int($swf_data_int);
        $data_len = bitLen($swf_data_int);
        $chunk_len = 31;
        $block_len = intval(ceil($chunk_len / 8));
        $curr_pos = 0;
        $swf_base64_bin_data = '';  //the binary swf data encode by base64
        while ($curr_pos < $data_len) {
            $tmp = subint($swf_data_int, $curr_pos, $chunk_len);
            $swf_base64_bin_data .= str_pad(
                int2bin(powmod($tmp, $swf_size_bytes, $base_bin_data)),
                $block_len,
                "\0"
            );
            $curr_pos += $chunk_len;
        }
        return  base64_encode($swf_base64_bin_data);
    }


    function createSwfCercode($length) {
	    mt_srand((double)microtime() * 1000000);
	    $hash="";
	    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	    for($i=0;$i<4;$i++)$hash.=$chars[mt_rand(0,61)];
	    return $hash;
    }
?>