a stupid implementaiton of function passing into an object... rediculouse thing to do
[ga4php.git] / example / index.php
1 <?php
2
3 require_once("../lib/lib.php");
4 require_once("tokenstore.php");
5
6 $ga = new GoogleAuthenticator("myGetData", "mySetData");
7
8 if(isset($_REQUEST["action"])) {
9         switch($_REQUEST["action"]) {
10                 case "destroy":
11                         unlink("/tmp/gadata.sqlite");
12                         break;
13         }
14 }
15
16 // create/connect a db
17 global $dbobject;
18 $dbobject = false;
19 if(file_exists("/tmp/gadata.sqlite")) {
20         try {
21                 $dbobject = new PDO("sqlite:/tmp/gadata.sqlite");
22         } catch(PDOException $exep) {
23                 error_log("execpt on db open");
24         }
25 } else {
26         try {
27                 $dbobject = new PDO("sqlite:/tmp/gadata.sqlite");
28         } catch(PDOException $exep) {
29                 error_log("execpt on db open");
30         }
31         $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT,"users_tokentype" TEXT,"users_tokenkey" TEXT,"users_tokencounter" integer);';
32         $dbobject->query($sql);
33 }
34
35
36 ?>
37 <html>
38 <h1>Example Page for GA4PHP</h1>
39 <a href="index.php">home</a><br>
40
41 <?php
42 if(isset($_REQUEST["action"])) {
43         switch($_REQUEST["action"]) {
44                 case "createuser":
45                         $username = $_REQUEST["username"];
46                         $pr = preg_match('/^[a-zA-Z0-9@\.]+$/',"$username");
47                         echo "<hr>";
48                         if(strlen($username)<3) {
49                                 echo "<font color=\"red\">Sorry, username must be at least 3 chars</font>";
50                         } else if($pr<1) {
51                                 echo "<font color=\"red\">Sorry, username can only contain a-z, A-Z, 0-9 @ and .</font>";
52                         } else {
53                                 //$key = $ga->setUser($username, "", $ttype);
54                                 //$keyinhex = $ga->helperb322hex($key);
55                                 //$url = urlencode($ga->createURL($username, $key, $ttype));
56                                 //echo "QRCode for user \"$username\" is <img src=\"http://chart.apis.google.com/chart?cht=qr&chl=$url&chs=120x120\"> or type in $key (google authenticator) or $keyinhex (for most other otp's)";
57                                 $sql = "insert into users values (NULL, '$username', 'TOTP', 'ASDF', '0')";
58                                 $dbobject->query($sql);
59                         }
60                         echo "<hr>";
61                         break;
62                 case "provisiontoken":
63                         $username = $_REQUEST["username"];
64                         $ttype = $_REQUEST["ttype"];
65                         $key = $ga->setUser($username, "", $ttype);
66                         $keyinhex = $ga->helperb322hex($key);
67                         $url = urlencode($ga->createURL($username, $key, $ttype));
68                         echo "QRCode for user \"$username\" is <img src=\"http://chart.apis.google.com/chart?cht=qr&chl=$url&chs=420x420\"> or type in $key (google authenticator) or $keyinhex (for most other otp's), $ttype";
69                         break;
70                 case "authuser":
71                         $username = $_REQUEST["username"];
72                         $code = $_REQUEST["code"];
73                         if($ga->authenticateUser($username, $code)) {
74                                 echo "<font color=\"green\">Passed!</font>";
75                         } else {
76                                 echo "<font color=\"red\">Failed!</font>";
77                         }
78                         break;
79                 case "resync":
80                         $username = $_REQUEST["username"];
81                         $code1 = $_REQUEST["code1"];
82                         $code2 = $_REQUEST["code2"];
83                         if($ga->resyncCode($username, $code1, $code2)) {
84                                 echo "<font color=\"green\">Passed!</font>";
85                         } else {
86                                 echo "<font color=\"red\">Failed!</font>";
87                         }
88                         break;
89                 default:
90                         // do nothing
91         }
92 }
93
94 ?>
95 <h2>Destroy the DB</h2>
96 <a href="index.php?action=destroy">This is UNDOABLE - but this is a test system, so you dont care</a>
97 <h2>Create a User:</h2>
98 <form method="post" action="index.php?action=createuser">
99 Username: <input type="text" name="username"><br>
100 <input type="submit" name="go" value="go"><br>
101 </form>
102 <hr>
103 <h2>Provision Token</h2>
104 <form method="post" action="index.php?action=provisiontoken">
105 Username: <select name="username">
106 <?php
107 $res = $ga->getUserList();
108 foreach($res as $row) {
109         echo "<option value=\"".$row."\">".$row."</option>";
110 }
111 ?>
112 </select><br>
113 Type: <select name="ttype"><option value="HOTP">HOTP</option><option value="TOTP">TOTP</option></select><br>
114 <input type="submit" name="go" value="go"><br>
115 </form>
116 <hr>
117 <h2>Test Token</h2>
118 <form method="post" action="index.php?action=authuser">
119 Username: <select name="username">
120 <?php
121 $res = $ga->getUserList();
122 foreach($res as $row) {
123         echo "<option value=\"".$row."\">".$row."</option>";
124 }
125 ?>
126 </select><br>
127 Code: <input type="text" name="code"><br>
128 <input type="submit" name="go" value="go"><br>
129 </form>
130 <hr>
131 <h2>Resync Code (only valid for HOTP codes)</h2>
132 <form method="post" action="index.php?action=resync">
133 Username: <select name="username">
134 <?php
135 $res = $ga->getUserList();
136 foreach($res as $row) {
137         echo "<option value=\"".$row."\">".$row."</option>";
138 }
139 ?>
140 </select><br>
141 Code one: <input type="text" name="code1"><br>
142 Code two: <input type="text" name="code2"><br>
143 <input type="submit" name="go" value="go"><br>
144 </form>
145 <hr>
146 </html>