working example page
[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\">";
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                 default:
37                         // do nothing
38         }
39 }
40
41 ?>
42
43 Create a User:
44 <form method="post" action="index.php?action=createuser">
45 Username: <input type="text" name="username"><br>
46 Type (ignored for now): <select name="ttype"><option value="HOTP">HOTP</option><option value="TOTP">TOTP</option></select><br>
47 <input type="submit" name="go" value="go"><br>
48 </form>
49 <hr>
50 <form method="post" action="index.php?action=authuser">
51 Username: <input type="text" name="username"><br>
52 Code: <input type="text" name="code"><br>
53 <input type="submit" name="go" value="go"><br>
54 </form>
55 <hr>
56 </html>