Added a login page example
[ga4php.git] / example / login / index.php
1 <?php 
2 /* 
3  * This example rely's on the provisioning example, you must first create accounts in the provisioning 
4  * example then use them here. 
5  * 
6  * This example is solely an example of how a login page might look and/or work
7  * 
8  * If a user doesnt have a token assigned, they wont require it on the login page. This is an example
9  * of when your allowing the user to increase security of their OWN account, not the security of the
10  * site as such.
11  * 
12  */ 
13
14 require_once("../provisioning/dbfunctions.php");
15 require_once("../provisioning/token.php");
16
17
18 session_start();
19
20 $myga = new myGA();
21
22 // check if we're logged in
23 if(isset($_SESSION["loginname"])) {
24         if($_SESSION["loginname"]!="") {
25                 
26                 // handle logout
27                 if(isset($_REQUEST["logout"])) {
28                         error_log("session killer");
29                         unset($_SESSION["loginname"]);
30                         header("Location: index.php");
31                         return;
32                 }
33                 
34                 // display the logged in page
35                 displayLogedInPage();
36                 return;
37         }
38 }
39
40
41 // here is where we process the login
42 if(isset($_REQUEST["login"])) {
43         $db = getDatabase();
44         
45         // get the data from the post request
46         error_log("begin login");
47         $username = $_REQUEST["username"];
48         $password = $_REQUEST["password"];
49         $tokencode = $_REQUEST["tokencode"];
50         
51         // pull the password hash from the database
52         $sql = "select users_password from users where users_username='$username'";
53         error_log("running sql: $sql");
54         $res = $db->query($sql);
55         
56         foreach($res as $row) {
57                 $passhash = $row["users_password"];
58         }
59         
60         // user entered a tokencode, fail the login and tell the user
61         // if they dont have a token code assigned to them
62         if($tokencode != "") {
63                 if(!$myga->hasToken($username)) {
64                         $msg = urlencode("Attempted to login with a token when username isnt assigned one");
65                         header("Location: index.php?failure=$msg");
66                 }
67         }
68         
69         // check the password hash versus the login password
70         error_log("checking $passhash against $password (".sha1($password).")");
71         if($passhash == sha1($password)) $passright = true;
72         else {
73                 header("Location: index.php?failure=LoginIncorrect");
74                 return;
75         }
76         
77         // now get myGA to check the token code
78         error_log("passed password auth");
79         if($myga->hasToken($username)) if(!$myga->authenticateUser($username, $tokencode)) {
80                 header("Location: index.php?failure=LoginIncorrect");
81                 return;
82         }
83
84         // and we're loged in
85         $_SESSION["loginname"] = "$username";
86         
87         header("Location: index.php");
88         return;
89 }
90
91
92
93
94 // and our "your logged in" page
95 function displayLogedInPage()
96 {
97 ?>
98 <html>
99 <h1>Welcome</h1>
100 Welcome <?php echo $_SESSION["loginname"]?>, you are logged in.
101 Click <a href="index.php?logout">here</a> to log out.
102 </html>
103 <?php
104         echo "<pre>";
105         print_r($_REQUEST);
106         print_r($_SESSION);
107         echo "</pre>";
108         
109         return; 
110 }
111
112
113
114
115 ?>
116 <html>
117 <h2>Welcome to Generic Site</h2>
118 <i><b>Note:</b> if the user you've provisioned has not got a token code, its not required for login</i><br>
119 Please login:
120 <?php
121 if(isset($_REQUEST["failure"])) {
122         echo "<hr><font color=\"red\">Login Failure: ".$_REQUEST["failure"]."</font><hr>";      
123
124 ?>
125 <form method="post" action="index.php?login">
126 <table>
127 <tr><td>Username</td><td><input type="text" name="username"></td></tr>
128 <tr><td>Password</td><td><input type="password" name="password"></td></tr>
129 <tr><td>Pin Code</td><td><input type="text" name="tokencode"></td></tr>
130 <tr><td><input type="submit" name="login" value="Login"></td></tr>
131 </table>
132 </form>
133 <hr>
134 <pre>
135 <?php 
136         print_r($_REQUEST);
137         print_r($_SESSION);
138 ?>
139 </pre>
140 </html>