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