883ccb939bf2439cf777f201b794e159a22995dc
[ga4php.git] / lib / lib.php
1 <?php
2
3 class GoogleAuthenticator {
4         
5         // first we init google authenticator by passing it a filename
6         // for its sqlite database.
7         function __construct($file) {
8                 if(file_exists($file)) {
9                         try {
10                                 $this->dbConnector = new PDO("sqlite:$file");
11                         } catch(PDOException $exep) {
12                                 $this->errorText = $exep->getMessage();
13                                 $this->dbConnector = false;
14                         }                       
15                 } else {
16                         setupDB();
17                 }
18                 
19                 $this->dbFile = $file;
20         }
21         
22         // creates the database (tables);
23         function setupDB() {
24                 try {
25                         $this->$dbConnector = new PDO("sqlite:$file");
26                 } catch(PDOException $exep) {
27                         $this->errorText = $exep->getMessage();
28                         $this->dbConnector = false;
29                 }                       
30         
31                 // here we create some tables and stuff
32         }
33         
34         // creates "user" in the database and returns a url for
35         // the phone. If user already exists, this returns false
36         // if any error occurs, this returns false
37         function setupUser($username) {
38                 $key = _createBase32Key();
39         }
40         
41         // Replcate "user" in the database... All this really
42         // does is to replace the key for the user. Returns false
43         // if the user doesnt exist of the key is poop
44         function replaceUser($username) {
45                 
46         }
47         
48         // sets the key for a user - this is assuming you dont want
49         // to use one created by the application. returns false
50         // if the key is invalid or the user doesn't exist.
51         function setUserKey($username, $key) {
52                 
53         }
54         
55         // self explanitory?
56         function deleteUser($username) {
57                 
58         }
59         
60         // user has input their user name and some code, authenticate
61         // it
62         function authenticateUser($username, $code) {
63                 
64         }
65         
66         // this function allows a user to resync their key. If too
67         // many codes are called, we only check up to 20 codes in the future
68         // so if the user is at 21, they'll always fail. 
69         function resyncCode($username, $code1, $code2) {
70                         
71         }
72         
73         // gets the error text associated with the last error
74         function getErrorText() {
75                 return $this->errorText;
76         }
77         
78         // create a url compatibile with google authenticator.
79         function createURL($user, $key) {
80                 $url = "otpauth://hotp/$user?secret=$key";
81                 return $url;
82         }
83         
84         // creeates a base 32 key (random)
85         function createBase32Key() {
86                 $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
87                 $key = "";
88                 for($i=0; $i<16; $i++) {
89                         $offset = rand(0,strlen($alphabet)-1);
90                         //echo "$i off is $offset\n";
91                         $key .= $alphabet[$offset];
92                 }
93                 
94                 return $key;
95         }
96                 
97         
98         function helperb322hex($b32) {
99         $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
100
101         $out = "";
102         $dous = "";
103
104         for($i = 0; $i < strlen($b32); $i++) {
105                 $in = strrpos($alphabet, $b32[$i]);
106                 $b = str_pad(base_convert($in, 10, 2), 5, "0", STR_PAD_LEFT);
107                 $out .= $b;
108                 $dous .= $b.".";
109         }
110
111         $ar = str_split($out,20);
112
113         //echo "$dous, $b\n";
114
115         //print_r($ar);
116         $out2 = "";
117         foreach($ar as $val) {
118                 $rv = str_pad(base_convert($val, 2, 16), 5, "0", STR_PAD_LEFT);
119                 //echo "rv: $rv from $val\n";
120                 $out2 .= $rv;
121
122         }
123         //echo "$out2\n";
124
125         return $out2;
126         }
127         
128         function helperhex2b32($hex) {
129         $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
130
131         $ar = str_split($hex, 5);
132
133         $out = "";
134         foreach($ar as $var) {
135                 $bc = base_convert($var, 16, 2);
136                 $bin = str_pad($bc, 20, "0", STR_PAD_LEFT);
137                 $out .= $bin;
138                 //echo "$bc was, $var is, $bin are\n";
139         }
140
141         $out2 = "";
142         $ar2 = str_split($out, 5);
143         foreach($ar2 as $var2) {
144                 $bc = base_convert($var2, 2, 10);
145                 $out2 .= $alphabet[$bc];
146         }
147
148         return $out2;
149         }
150         
151         
152         // some private data bits.
153         private $errorText;
154         private $dbFile;
155         private $dbConnector;
156 }
157 ?>