<?php

/* test key

http://www.estamos.de/makecd
555555555
H8HsRww)7Uz)!iXE5PUMfgP#VymA6

*/

error_reporting(E_ALL); ini_set('display_errors'TRUE);

//include 'stat.php';

if (isset($_GET['source'])) {

    
header('Content-Type: text/html; charset=utf-8');

    
?><!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>MakeCD registration number generator source code</title>
    <body>
    <?php highlight_file(__FILE__)?>
    </body>
    </html>
    <?php exit;
}

$levels = [
    
=> 'private user',
    
=> 'private user with disc-at-once',
    
=> 'commercial user',
    
=> 'professional publisher'
];

function 
Gen_Key($serial$szint) {

    
$mod = [
        
=> ['000000C2160EC163CD3BBA56F2CCFE0BCAA4D77B''256257538298748946109''257725486834819626647'],
        
=> ['0000009DD1BB995C38E20E4EF082722C572007DD''223057111979772211067''240759480166694836871'],
        
=> ['000000B321B91AF445F77290EAD3609284629A6B''223298907890902816151''272976567149646185549'],
        
=> ['000000A1DCAFD7978ED21F8FEF94542386ECE807''233556328724004143549''235826640018421043987']
    ];

    
$chartab = [62495051525354555657656667686970717273747576777880818283848586878889909798991001011021031041051061073510911011211311411511611711811912012112233634041];

    
$key str_pad(gmp_strval(gmp_init($serial10), 16), 8'0'STR_PAD_LEFT);

    
$p $mod[$szint];

    
$m gmp_init($p[0], 16);

    
$p1 gmp_init($p[1]);

    
$p2 gmp_init($p[2]);

    
$d gmp_invert(3gmp_mul(gmp_sub($p11), gmp_sub($p21)));

    
$y gmp_init('1ffffffffffffffffffff00' $key16);

    
$x gmp_powm($y$d$m);

    
$k $key str_pad(gmp_strval($x16), 34'0'STR_PAD_LEFT);

    
$bits str_pad(gmp_strval(gmp_init($k16), 2), 168'0'STR_PAD_LEFT);

    
$chars 'H';

    for (
$i 0$i 168$i += 6) {
        
$b substr($bits$i6);
        
$chars .= chr($chartab[base_convert($b210)]);
    }

    return(
$chars);
}

if (isset(
$_POST['level']) && isset($_POST['serial'])) {
    
$level intval($_POST['level']);
    if ((
$level 0) && ($level 5)) {
        
$serial $_POST['serial'];
        if (
preg_match('!^[0-9]+$!'$serial) && ($serial 0) && ($serial 2147483648)) {
            echo 
Gen_Key($_POST['serial'], $_POST['level']);
        } else {
            echo 
'Serial number must be between 1 and 2147483647!';
        }
    } else {
        echo 
'Choose level!';
    }
    exit;
}

header('Content-Type: text/html; charset=utf-8');

?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MakeCD registration number generator</title>
<style>
html,body {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 16px;
}
.serial {
    font-family: monospace;
    text-align: right;
}
.reg {
    font-family: monospace;
}
h1,h2 {
    text-align: center;
}
td {
    padding: 5px;
}
table, th, td {
    border: 1px solid black;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
function GenKey() {
    $('#regnum').load('<?=$_SERVER['PHP_SELF']?>',{
        level: $('#level').val(),
        serial: $('#serial').val()
    });
}
$(function() {
    $('#serial').on('keyup change', function() {
        GenKey();
    });
    $('#level').on('change', function() {
        GenKey();
    });
});
</script>
</head>
<body>
<h1>MakeCD registration number generator V0.666</h1>
<h2>by LaySoft of Crimson Jihad</h2>
<table style="border-spacing: 0px; border-collapse: separate; margin-left:auto; margin-right:auto">
<tr>
<th>Level</th>
<th>Serial</th>
<th>Registration number</th>
</tr>
<tr>
<td><select id="level"><option value="0">Choose!</option><?php
foreach ($levels as $level => $duma) {
    echo 
'<option value="' $level '">' $duma '</option>';
}
?></select></td>
<td><input type="number" id="serial" placeholder="Enter serial number!" min="1" max="2147483647"/></td>
<td id="regnum" class="reg">&nbsp;</td>
</tr>
<?php for ($i 1$i 20$i++) {
    
$level mt_rand(14);
    
$serial mt_rand(12147483647)?>
    <tr>
    <td><?=$levels[$level]?></td>
    <td class="serial"><?=$serial?></td>
    <td class="reg"><?=Gen_Key($serial$level)?></td>
    </tr>
<?php }?>
</table>
<p style="text-align: right"><a href="<?=$_SERVER['PHP_SELF']?>?source">source</a></p>
</body>
</html>