|
|
@@ -0,0 +1,56 @@
|
|
|
+<?php
|
|
|
+class Token {
|
|
|
+ private $passwd, $token, $time;
|
|
|
+ private $bn = false;
|
|
|
+
|
|
|
+ public function __construct($passwd, $bn = null) {
|
|
|
+ $this->passwd = $passwd;
|
|
|
+ if ($bn !== null) {
|
|
|
+ if ($bn === "JakOnZyjeToMyTezMozemy") $this->bn = true;
|
|
|
+ else die();
|
|
|
+ }
|
|
|
+ $this->time = floor(time()/60);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static function chr($n) {
|
|
|
+ $n = $n % 62;
|
|
|
+ if ($n > 35) $n += 61;
|
|
|
+ elseif ($n > 9) $n += 55;
|
|
|
+ else $n += 48;
|
|
|
+ return chr($n);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function genToken() {
|
|
|
+ $token = '';
|
|
|
+ for ($i = 0; $i < 8; $i++) $token .= self::chr(time() + rand(0, pow(2, 18) - 1));
|
|
|
+ $this->token = $token;
|
|
|
+ return $token;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function genHash($token, $next = false) {
|
|
|
+ $time = $this->time;
|
|
|
+ if ($next) $time++;
|
|
|
+ $s = md5($this->passwd . $token . $time);
|
|
|
+ $s = md5(base64_encode(gzcompress($s . $time . $s)));
|
|
|
+ $w = 0;
|
|
|
+ for ($i = 0; $i<strlen($s); $i++) $w += pow(2, $i) * ord($s[$i]);
|
|
|
+ $r = '';
|
|
|
+ while ($w > 0) {
|
|
|
+ $r .= self::chr($w);
|
|
|
+ $w = floor($w / 62);
|
|
|
+ }
|
|
|
+ return $r;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function verify($hash) {
|
|
|
+ if (($hash === $this->genHash($this->token)) || ($hash === $this->genHash($this->token, true))) return $this->passwd;
|
|
|
+ return "ThisAttemptHasBeenLogged";
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getHash($token) {
|
|
|
+ if ($this->bn) return $this->genHash($token);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+?>
|