removed eronius comment
[ga4php.git] / example / simple / extend.php
1 <?php
2
3 /*
4  * This file just shows a simple example of how to extend the GoogleAuthenticator schema
5  */
6 require_once("../../lib/ga4php.php");
7
8
9 /* we now define the three methods we have to define
10  * the way the class can get and save data. The class calls
11  * these methods frequently
12  * 
13  * What we need to define is methods for saving the auth data
14  * for the user
15  * 
16  * Lets assume our application already has a user table, so what 
17  * we do is add a new column into our table for saving auth data
18  * 
19  * i.e. "alter table users add tokendata text" would add a column
20  * we can use to our "users" tables... Lets also assume that we
21  * have a column called "username" which defines the username
22  * 
23  * Lastly, lets assume we can get a connection to the database
24  * by calling a function GetDatabase(); which is a PDO object
25  */
26
27 class MyGoogleAuth extends GoogleAuthenticator {
28         
29         function getData($username) {
30                 
31                 // get our database connection
32                 $dbObject = GetDatabase();
33                 
34                 // set the sql for retreiving the data
35                 $sql = "select tokendata from users where username='$username'";
36                 
37                 // run the query
38                 $result = $dbObject->query($sql);
39                 
40                 // check the result
41                 if(!$result) return false;
42                 
43                 // now just retreieve all the data (there should only be one, but whatever)
44                 $tokendata = false;
45                 foreach($result as $row) {
46                         $tokendata = $row["tokendata"];
47                 }
48                 
49                 // now we have our data, we just return it. If we got no data
50                 // we'll just return false by default
51                 return $tokendata;
52                 
53                 // and there you have it, simple eh?
54         }
55         
56         
57         // now we need a function for putting the data back into our user table.
58         // in this example, we wont check anything, we'll just overwrite it.
59         function putData($username, $data) {
60                 // get our database connection
61                 $dbObject = GetDatabase();
62                 
63                 // set the sql for updating the data
64                 // token data is stored as a base64 encoded string, it should
65                 // not need to be escaped in any way prior to storing in a database
66                 // but feel free to call your databases "addslashes" (or whatever)
67                 // function on $data prior to doing the SQL.
68                 $sql = "update users set tokendata='$data' where username='$username'"
69                 
70                 // now execute the sql and return straight away - you should probably
71                 // clean up after yourselves, but im going to assume pdo does this
72                 // for us anyway in this exmaple
73                 if($dbObject->query($sql)) {
74                         return true;
75                 } else {
76                         return false;
77                 }
78                 
79                 // even simpler!
80         }
81         
82         // the get users method isnt actually used within the google authenticator
83         // class as yet, but it probably will in the future, so feel free to implement it
84         function getUsers() {
85                 // get our database connection
86                 $dbObject = GetDatabase();
87                 
88                 // now the sql again
89                 $sql = "select username from users";
90                 
91                 // run the query
92                 $result = $dbObject->query($sql);
93                 
94                 // iterate over the results - we expect a simple array containing
95                 // a list of usernames
96                 $i = 0;
97                 $users = array();
98                 foreach($result as $row) {
99                         $users[$i] = $row["username"];
100                         $i++;
101                 }
102                 
103                 // now return the list
104                 return $users;
105         }
106 }
107
108 // and thats it...
109 ?>