gc-fallout - updates
[anomen-overlay.git] / www-apps / gc-fallout / dev / token.php
blob58b4369931e2087d7010a24000efd37dde4e4b5f
1 <?php
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);
19 $size = 32;
20 $i = 'a';
21 while (strlen($pass) < $size) {
22 $pass .= $i;
23 $i++;
25 $aes->setKey($pass);
27 return $aes->encrypt($plaintext);
30 function aes_decrypt($pass, $cipher)
32 $aes = new Crypt_AES(CRYPT_AES_MODE_CTR);
34 $size = 32;
35 $i = 'a';
36 while (strlen($pass) < $size) {
37 $pass .= $i;
38 $i++;
40 $aes->setKey($pass);
42 return $aes->decrypt($cipher);
45 function perkyToStr($U)
47 $res = '';
48 foreach ($U['perky'] as $perk) {
49 $i = substr($perk,0,3);
50 $res .= $i;
52 return $res;
55 function StrToPerky($strlist)
57 $perky = array();
58 $count = strlen($strlist) / 3;
59 for ($i = 0; $i < $count; $i++) {
60 $perky[] = substr($strlist, $i*3, 3);
62 //print_r($perky);
63 return $perky;
66 function getToken($U)
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
72 $plain .= ' ';
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);
81 return $token_b64;
84 function decodeToken($token)
86 $U = array();
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)) {
98 // error
99 header('X-Error: sha1fail');
100 readfile('img/no.png');
101 die;
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);
119 return $U;
122 function readPerkdir()
124 $files = array();
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;
139 closedir($handle);
140 return $files;
143 function expandPerks($perks)
145 $files = readPerkdir();
146 $res = array();
147 foreach ($perks as $p) {
148 $res[] = $files[$p];
150 return $res;