3 require_once("../../lib/ga4php.php");
5 // define our token class
6 class myGA extends GoogleAuthenticator {
7 function getData($username) {
9 // get our database connection
10 $dbObject = getDatabase();
12 // set the sql for retreiving the data
13 $sql = "select users_tokendata from users where users_username='$username'";
16 $result = $dbObject->query($sql);
19 if(!$result) return false;
21 // now just retreieve all the data (there should only be one, but whatever)
23 foreach($result as $row) {
24 $tokendata = $row["users_tokendata"];
27 // now we have our data, we just return it. If we got no data
28 // we'll just return false by default
31 // and there you have it, simple eh?
35 // now we need a function for putting the data back into our user table.
36 // in this example, we wont check anything, we'll just overwrite it.
37 function putData($username, $data) {
38 // get our database connection
39 $dbObject = getDatabase();
41 // set the sql for updating the data
42 // token data is stored as a base64 encoded string, it should
43 // not need to be escaped in any way prior to storing in a database
44 // but feel free to call your databases "addslashes" (or whatever)
45 // function on $data prior to doing the SQL.
46 $sql = "update users set users_tokendata='$data' where users_username='$username'";
48 // now execute the sql and return straight away - you should probably
49 // clean up after yourselves, but im going to assume pdo does this
50 // for us anyway in this exmaple
51 if($dbObject->query($sql)) {
61 // get our database connection
62 $dbObject = getDatabase();
65 $sql = "select users_username from users";
68 $result = $dbObject->query($sql);
70 // iterate over the results - we expect a simple array containing
71 // a list of usernames
74 foreach($result as $row) {
75 $users[$i] = $row["username"];
79 // now return the list