3 require_once('phpseclib/AES.php');
5 define('SEED_LENGTH', 3);
7 function base64_url_encode($input) {
8 return strtr(base64_encode($input), '+', '_');
11 function base64_url_decode($input) {
12 return base64_decode(strtr($input, '_', '+'));
15 function aes_encrypt($pass, $plaintext)
17 $aes = new Crypt_AES(CRYPT_AES_MODE_CTR
);
21 while (strlen($pass) < $size) {
27 return $aes->encrypt($plaintext);
30 function aes_decrypt($pass, $cipher)
32 $aes = new Crypt_AES(CRYPT_AES_MODE_CTR
);
36 while (strlen($pass) < $size) {
42 return $aes->decrypt($cipher);
45 function perkyToStr($U)
48 foreach ($U['perky'] as $perk) {
49 $i = substr($perk,0,3);
55 function StrToPerky($strlist)
58 $count = strlen($strlist) / 3;
59 for ($i = 0; $i < $count; $i++
) {
60 $perky[] = substr($strlist, $i*3, 3);
68 $perkyStr = perkyToStr($U);
69 $seed = str_pad('', SEED_LENGTH
, 'x');
70 $plain = "$seed${U['login']}!${U['karma']}!${U['penize']}!${U['jidlo']}!${U['skore']}!${U['cheater']}!$perkyStr";
71 while (strlen($plain) %
3 != 0) { // pad to base64 block
74 $sha = sha1(PASSWORD
. $plain, true);
75 for ($i = 0; $i < SEED_LENGTH
; $i++
) {
76 $plain{$i} = $sha{$i};
79 $token = aes_encrypt(PASSWORD
, $plain);
80 $token_b64 = base64_url_encode($token);
84 function decodeToken($token)
87 $token_raw = base64_url_decode($token);
88 $token_dec = aes_decrypt(PASSWORD
, $token_raw);
90 $token_check = $token_dec;
91 for ($i = 0; $i < SEED_LENGTH
; $i++
) {
92 $token_check{$i} = 'x';
94 header('X-Token: '.$token_check );
95 $sha = sha1(PASSWORD
. $token_check, true);
97 if (substr($token_dec,0,SEED_LENGTH
) != substr($sha,0,SEED_LENGTH
)) {
99 header('X-Error: sha1fail');
100 readfile('img/no.png');
103 header('X-Token: ' . $token_check);
105 $U['token'] = $token;
106 $U['token_dec'] = $token_check;
108 $data = substr($token_check, SEED_LENGTH
);
109 $token_list = explode('!', trim($data));
110 $U['login'] = array_shift($token_list);
111 $U['karma'] = array_shift($token_list);
112 $U['penize'] = array_shift($token_list);
113 $U['jidlo'] = array_shift($token_list);
114 $U['skore'] = array_shift($token_list);
115 $U['cheater'] = array_shift($token_list);
116 $p = StrToPerky(array_shift($token_list));
117 $U['perky'] = expandPerks($p);
122 function readPerkdir()
125 if ($handle = opendir('perky')) {
126 /* This is the correct way to loop over the directory. */
127 while (false !== ($f = readdir($handle))) {
128 if (strlen($f) >= 7) {
129 $idx = substr($f, 0, 3);
130 $ext = substr($f, -4);
131 $name = substr($f, 0, -4);
132 if (($ext == '.jpg') ||
($ext == '.png')) {
133 $files[$idx] = $name;
134 $files[$name] = $name;
143 function expandPerks($perks)
145 $files = readPerkdir();
147 foreach ($perks as $p) {