78caa960a2c2e4f85710012c8fab3630e154e0f4
[ga4php.git] / example / index.php
1 <?php
2
3 require_once("../lib/lib.php");
4 if(isset($_REQUEST["action"])) {
5         switch($_REQUEST["action"]) {
6                 case "destroy":
7                         unlink("/tmp/gaexpage.db");
8                         break;
9         }
10 }
11
12
13 $ga = new GoogleAuthenticator("/tmp/gaexpage.db");
14 ?>
15 <html>
16 <h1>Example Page for GA4PHP</h1>
17
18 <?php
19 if(isset($_REQUEST["action"])) {
20         switch($_REQUEST["action"]) {
21                 case "createuser":
22                         $username = $_REQUEST["username"];
23                         $pr = preg_match('/^[a-zA-Z0-9@\.]+$/',"$username");
24                         $ttype = $_REQUEST["ttype"];
25                         echo "<hr>";
26                         if(strlen($username)<3) {
27                                 echo "<font color=\"red\">Sorry, username must be at least 3 chars</font>";
28                         } else if($pr<1) {
29                                 echo "<font color=\"red\">Sorry, username can only contain a-z, A-Z, 0-9 @ and .</font>";
30                         } else {
31                                 $key = $ga->setupUser($username, $ttype);
32                                 $keyinhex = $ga->helperb322hex($key);
33                                 $url = urlencode($ga->createURL($username, $key, $ttype));
34                                 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)";
35                         }
36                         echo "<hr>";
37                         break;
38                 case "authuser":
39                         $username = $_REQUEST["username"];
40                         $code = $_REQUEST["code"];
41                         if($ga->authenticateUser($username, $code)) {
42                                 echo "<font color=\"green\">Passed!</font>";
43                         } else {
44                                 echo "<font color=\"red\">Failed!</font>";
45                         }
46                         break;
47                 case "resync":
48                         $username = $_REQUEST["username"];
49                         $code1 = $_REQUEST["code1"];
50                         $code2 = $_REQUEST["code2"];
51                         if($ga->resyncCode($username, $code1, $code2)) {
52                                 echo "<font color=\"green\">Passed!</font>";
53                         } else {
54                                 echo "<font color=\"red\">Failed!</font>";
55                         }
56                         break;
57                 default:
58                         // do nothing
59         }
60 }
61
62 ?>
63 <h2>Destroy the DB</h2>
64 <a href="index.php?action=destroy">This is UNDOABLE - but this is a test system, so you dont care</a>
65 <h2>Create a User:</h2>
66 <form method="post" action="index.php?action=createuser">
67 Username: <input type="text" name="username"><br>
68 Type: <select name="ttype"><option value="HOTP">HOTP</option><option value="TOTP">TOTP</option></select><br>
69 <input type="submit" name="go" value="go"><br>
70 </form>
71 <hr>
72 <h2>Test Token</h2>
73 <form method="post" action="index.php?action=authuser">
74 Username: <select name="username">
75 <?php
76 $res = $ga->getUserList();
77 foreach($res as $row) {
78         echo "<option value=\"".$row."\">".$row."</option>";
79 }
80 ?>
81 </select><br>
82 Code: <input type="text" name="code"><br>
83 <input type="submit" name="go" value="go"><br>
84 </form>
85 <hr>
86 <h2>Resync Code (only valid for HOTP codes)</h2>
87 <form method="post" action="index.php?action=resync">
88 Username: <select name="username">
89 <?php
90 $res = $ga->getUserList();
91 foreach($res as $row) {
92         echo "<option value=\"".$row."\">".$row."</option>";
93 }
94 ?>
95 </select><br>
96 Code one: <input type="text" name="code1"><br>
97 Code two: <input type="text" name="code2"><br>
98 <input type="submit" name="go" value="go"><br>
99 </form>
100 <hr>
101 </html>