1 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* Filename: auth.class.php
|
|
|
5 |
*
|
|
|
6 |
* Description:
|
|
|
7 |
* Authorization class for web application.
|
|
|
8 |
* Requires database table with following minimal structure
|
|
|
9 |
* id int unsigned not null auto_increment
|
|
|
10 |
* name varchar(64)
|
|
|
11 |
* passwd varchar(256)
|
|
|
12 |
* access text
|
|
|
13 |
* defaults are table _user, with columns name, passwd and access
|
|
|
14 |
* name and pass are matched against database entries, then name
|
|
|
15 |
* and access are stored in instantiation of class
|
|
|
16 |
*
|
|
|
17 |
* passwd should be able to handle 256 chars as hashes become
|
|
|
18 |
* longer.
|
|
|
19 |
*
|
|
|
20 |
*
|
|
|
21 |
* $Rev:: 6 $: Revision in Repository
|
|
|
22 |
* $Author:: rodolico $: Last Author
|
|
|
23 |
* $Date:: 2017-07-28 0$: Last Commit
|
|
|
24 |
*
|
|
|
25 |
*
|
|
|
26 |
* History:
|
|
|
27 |
* 20170728 - RWR - 1.0
|
|
|
28 |
* Initial build
|
|
|
29 |
*/
|
|
|
30 |
|
|
|
31 |
require_once( 'DBQuery.class.php' );
|
6 |
rodolico |
32 |
|
|
|
33 |
global $DEBUG;
|
1 |
rodolico |
34 |
|
|
|
35 |
class Auth {
|
|
|
36 |
|
|
|
37 |
// all data stored in $parameters. We will initialize $parameters
|
|
|
38 |
// with some default data
|
|
|
39 |
protected $parameters = array(
|
|
|
40 |
'table name' => '_user', // table storing auth info
|
4 |
rodolico |
41 |
'column username' => 'username', // field to match username to
|
1 |
rodolico |
42 |
'column password' => 'passwd', // field to match password to
|
|
|
43 |
'column access' => 'access', // field which contains access information
|
6 |
rodolico |
44 |
'column id' => '_user_id', // unique user id in table
|
4 |
rodolico |
45 |
);
|
1 |
rodolico |
46 |
|
4 |
rodolico |
47 |
|
1 |
rodolico |
48 |
// constructor simply loads parameters into array from
|
|
|
49 |
public function __construct( $parameters = array() ) {
|
|
|
50 |
// first, gather all info from the parameters array
|
|
|
51 |
foreach ( $parameters as $key => $value )
|
|
|
52 |
$this->parameters[$key] = $value;
|
6 |
rodolico |
53 |
$this->logIt( 1, 'Constructed Auth' );
|
1 |
rodolico |
54 |
} // construct
|
6 |
rodolico |
55 |
|
1 |
rodolico |
56 |
|
6 |
rodolico |
57 |
protected function logIt ( $level, $message, $file = null, $class = null, $function = null, $line = null ) {
|
|
|
58 |
global $DEBUG;
|
|
|
59 |
if ( isset( $DEBUG ) ) {
|
|
|
60 |
$DEBUG->writeLog( $level, $message, $file, $class, $function, $line );
|
|
|
61 |
}
|
|
|
62 |
} // logIt
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
public function save() {
|
|
|
66 |
return $this->parameters;
|
|
|
67 |
} // save
|
|
|
68 |
|
|
|
69 |
public function load( $parameters ) {
|
|
|
70 |
$this->parameters = $parameters;
|
|
|
71 |
} // load
|
|
|
72 |
|
1 |
rodolico |
73 |
public function __get( $parameter ) {
|
|
|
74 |
return $this->parameters[$parameter];
|
|
|
75 |
} // function get
|
|
|
76 |
|
|
|
77 |
public function __set ($parameter, $value ) {
|
4 |
rodolico |
78 |
$oldValue = $this->parameters[$parameter];
|
1 |
rodolico |
79 |
$this->parameters[$parameter] = $value;
|
|
|
80 |
return $oldValue;
|
|
|
81 |
} // function set
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
/*
|
|
|
85 |
* authorizes access to a resource
|
|
|
86 |
* just a shell at this time.
|
|
|
87 |
*/
|
|
|
88 |
public function authorize ( $resource = '' ) {
|
|
|
89 |
if ( isset( $this->parameters['username'] ) ) {
|
|
|
90 |
/*
|
|
|
91 |
* code here to actually determine if user is authorized
|
|
|
92 |
* for this page
|
|
|
93 |
*/
|
|
|
94 |
return true;
|
|
|
95 |
} elseif ( isset( $this->parameters['login page'] ) ) {
|
|
|
96 |
// redirect to login page
|
4 |
rodolico |
97 |
$loginPage = $this->parameters['login page'];
|
|
|
98 |
header( "Location: $loginPage" );
|
1 |
rodolico |
99 |
exit();
|
|
|
100 |
} else {
|
|
|
101 |
print $this->createLoginPage();
|
|
|
102 |
exit();
|
|
|
103 |
}
|
|
|
104 |
} // function authorize
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
/*
|
|
|
108 |
* Checks if the username and password are valid.
|
|
|
109 |
* username can either be taken from parameters or from $this->parameters
|
|
|
110 |
* on success, sets username, password, user_id and access from
|
|
|
111 |
* database (I know username should not change, but I want the db val
|
|
|
112 |
*/
|
|
|
113 |
function verifyLogin( $password, $username = null ) {
|
|
|
114 |
if ( isset( $username ) )
|
|
|
115 |
$this->parameters['username'] = $username;
|
|
|
116 |
if ( isset( $this->parameters['username'] ) && isset( $password ) ) {
|
4 |
rodolico |
117 |
$sql = "select " .
|
|
|
118 |
$this->parameters['column id'] . " 'id'," .
|
|
|
119 |
$this->parameters['column username'] . " 'username'," .
|
|
|
120 |
$this->parameters['column password'] . " 'password'," .
|
|
|
121 |
$this->parameters['column access'] . " 'access' " .
|
|
|
122 |
" from " .
|
|
|
123 |
$this->parameters['table name'] .
|
|
|
124 |
" where " .
|
|
|
125 |
$this->parameters['column username'] . " = " .
|
|
|
126 |
DBQuery::makeSafeSQLConstant( $this->parameters['username'] );
|
6 |
rodolico |
127 |
$this->logIt( 3, "Query = $sql", null, __CLASS__, __FUNCTION__, __LINE__ );
|
1 |
rodolico |
128 |
$results = new DBQuery( $sql );
|
|
|
129 |
if ( $results->getOneRow() ) {
|
|
|
130 |
// a special case is when the password stored in the database is null
|
|
|
131 |
// in which case we give full access. This allows us to do a manual reset
|
|
|
132 |
// if passwords are lost.
|
6 |
rodolico |
133 |
$data = $results->__get( 'returnData' );
|
|
|
134 |
$this->logIt( 4, "Results = " . print_r( $results, true) , null, __CLASS__, __FUNCTION__, __LINE__ );
|
|
|
135 |
$this->logIt( 3, "Data = " . print_r( $data, true) , null, __CLASS__, __FUNCTION__, __LINE__ );
|
|
|
136 |
if ( $data->password == '' || $data->password == null || password_verify( $password, $data->password ) ) {
|
|
|
137 |
$this->parameters['username'] = $data->username;
|
|
|
138 |
$this->parameters['user_id'] = $data->id;
|
|
|
139 |
$this->parameters['access'] = $data->access;
|
|
|
140 |
return array( 'username' => $data->username, 'user_id' => $data->id, 'access' => $data->access );
|
1 |
rodolico |
141 |
} // if password verifies
|
|
|
142 |
} // if the query executed
|
|
|
143 |
} // if we have a username and a password
|
|
|
144 |
return false;
|
|
|
145 |
} // function verifyLogin
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
/* update password hash from parameter passed in, saving it in database */
|
|
|
149 |
function setPassword ( $password ) {
|
|
|
150 |
$hash = password_hash( $password, PASSWORD_DEFAULT );
|
|
|
151 |
$sql = "update $this->parameters[table name]
|
|
|
152 |
set $this->parameters[column password] = '$hash'
|
|
|
153 |
where $this->parameters[column id] = $this->parameters[user id]";
|
|
|
154 |
return new DBQuery( $sql, true );
|
|
|
155 |
} // setPassword
|
|
|
156 |
|
|
|
157 |
|
|
|
158 |
public function createLoginPage () {
|
|
|
159 |
return '<?php
|
|
|
160 |
session_start();
|
|
|
161 |
if (isset( $_POST["submit"])) {
|
|
|
162 |
include_once( "Auth.class.php" );
|
|
|
163 |
$_SESSION["authorization information"] = new Auth();
|
|
|
164 |
?>
|
|
|
165 |
<html><body>
|
|
|
166 |
<h3 align="center">Enter your username and password below</h3>
|
|
|
167 |
<FORM action="login.html" method="POST" enctype="multipart/form-data">
|
|
|
168 |
<table border="1" cellpadding="2" align="center">
|
|
|
169 |
<tbody>
|
|
|
170 |
<tr>
|
|
|
171 |
<td>User Name</td>
|
|
|
172 |
<td><input type="text" name="login" size="20"></td>
|
|
|
173 |
</tr>
|
|
|
174 |
<tr>
|
|
|
175 |
<td>Password</td>
|
|
|
176 |
<td><input type="password" name="pass" size="20"></td>
|
|
|
177 |
</tr>
|
|
|
178 |
<tr><TD colspan="2" align="center"><INPUT type="submit" name="Login" value="Log In"></TD></tr>
|
|
|
179 |
</tbody>
|
|
|
180 |
</table>
|
|
|
181 |
</FORM></body></html>';
|
|
|
182 |
} // function createLoginPag
|
|
|
183 |
|
|
|
184 |
|
|
|
185 |
} // class Auth
|
|
|
186 |
|
|
|
187 |
?>
|