| Line 1... |
Line 1... |
| 1 |
<?php
|
1 |
<?php
|
| 2 |
|
2 |
|
| - |
|
3 |
/*
|
| - |
|
4 |
Copyright (c) 2021, Daily Data, Inc. Redistribution and use in
|
| - |
|
5 |
source and binary forms, with or without modification, are permitted
|
| - |
|
6 |
provided that the following conditions are met:
|
| - |
|
7 |
|
| - |
|
8 |
* Redistributions of source code must retain the above copyright
|
| - |
|
9 |
notice, this list of conditions and the following disclaimer.
|
| - |
|
10 |
* Redistributions in binary form must reproduce the above copyright
|
| - |
|
11 |
notice, this list of conditions and the following disclaimer in the
|
| - |
|
12 |
documentation and/or other materials provided with the distribution.
|
| - |
|
13 |
|
| - |
|
14 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| - |
|
15 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| - |
|
16 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| - |
|
17 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| - |
|
18 |
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| - |
|
19 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| - |
|
20 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| - |
|
21 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| - |
|
22 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| - |
|
23 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| - |
|
24 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| - |
|
25 |
|
| - |
|
26 |
*/
|
| - |
|
27 |
|
| - |
|
28 |
/*
|
| 3 |
class Users {
|
29 |
* users.class.php
|
| - |
|
30 |
*
|
| - |
|
31 |
* Authors: R. W. Rodolico
|
| - |
|
32 |
*
|
| - |
|
33 |
*/
|
| - |
|
34 |
|
| - |
|
35 |
/**
|
| - |
|
36 |
* User Login class
|
| - |
|
37 |
*
|
| - |
|
38 |
* IMPORTANT: Requires a data source. See UsersDataSourceMySQLi.class.php
|
| - |
|
39 |
* for code which provides this for MySQLi
|
| - |
|
40 |
*
|
| - |
|
41 |
* Users encapsulates a basic login and authentication package. It
|
| - |
|
42 |
* provides a login screen, authentication, the ability to edit oneself,
|
| - |
|
43 |
* and for users with the admin flag set, the ability to edit others.
|
| - |
|
44 |
*
|
| - |
|
45 |
* It also allows a user to be disabled.
|
| - |
|
46 |
*
|
| - |
|
47 |
* Users was designed to be extensible, adding new fields by the calling
|
| - |
|
48 |
* program, modifying the HTML elements, etc...
|
| - |
|
49 |
*
|
| - |
|
50 |
* @author R. W. Rodolico <rodo@unixservertech.com>
|
| - |
|
51 |
*
|
| - |
|
52 |
* @version 0.9.0 (beta)
|
| - |
|
53 |
* @copyright 2021 Daily Data, Inc.
|
| 4 |
|
54 |
*
|
| - |
|
55 |
*/
|
| - |
|
56 |
|
| - |
|
57 |
class Users {
|
| 5 |
|
58 |
|
| - |
|
59 |
/**
|
| - |
|
60 |
* @var string[] $dbDefinition Contains the configuration for the class
|
| - |
|
61 |
*
|
| - |
|
62 |
* May be modified by the calling program. Must be replicated in userDataSource class
|
| - |
|
63 |
*/
|
| 6 |
private $dbDefinition = array(
|
64 |
private $dbDefinition = array(
|
| 7 |
/*
|
65 |
/*
|
| 8 |
* what to use for html input fields
|
66 |
* what to use for html input fields
|
| 9 |
* These are passed to sprintf, with label, fieldname, title, placeholder and current value, in that order
|
67 |
* These are passed to sprintf, with label, fieldname, title, placeholder and current value, in that order
|
| 10 |
*/
|
68 |
*/
|
| 11 |
'screens' => array (
|
69 |
'screens' => array (
|
| 12 |
'login form' => "<h1>Login</h1>\n<form class='login_form' action='%s' method='post'>\n%s\n<input type='submit' value='Login'></form>\n",
|
70 |
'login form' => "<h1>Login</h1>\n<form class='login_form' action='%s' method='post'>\n%s\n<input type='submit' value='Login'></form>\n",
|
| 13 |
'edit form' => "<form class='login_form' action='%s' method='post'>\n%s\n<input type='submit' id='btnUpdate' name='btnUpdate' value='Update'>\n</form>",
|
71 |
'edit form' => "<form class='login_form' action='%s' method='post'>\n%s\n<input type='submit' id='btnUpdate' name='btnUpdate' value='Update'>\n</form>",
|
| 14 |
'loginScreen' => "<div class='login_field'>\n<input class='login_field' type='text' name='username' placeholder='Username' required>\n</div>\n<div class='login_field'>\n<input class='login_field' type='password' name='password' placeholder='Password' required>\n</div>",
|
72 |
'loginScreen' => "<div class='login_field'>\n<input class='login_field' type='text' name='username' placeholder='Username' required autofocus>\n</div>\n<div class='login_field'>\n<input class='login_field' type='password' name='password' placeholder='Password' required>\n</div>",
|
| 15 |
'adminScreen' => "<input type='hidden' name='doAdmin' value='1'>\n",
|
73 |
'adminScreen' => "<input type='hidden' name='doAdmin' value='1'>\n",
|
| 16 |
'validateScript' => '',
|
74 |
'validateScript' => '',
|
| 17 |
),
|
75 |
),
|
| 18 |
'html input fields' => array(
|
76 |
'html input fields' => array(
|
| 19 |
'text' => "<div class='login_field'>\n<label>%s\n<input class='login_field' type='text' name='%s' title='%s' placeholder='%s' value='~~%s~~'>\n</label>\n</div>",
|
77 |
'text' => "<div class='login_field'>\n<label>%s\n<input class='login_field' type='text' name='%s' title='%s' placeholder='%s' value='~~%s~~'>\n</label>\n</div>",
|
| Line 59... |
Line 117... |
| 59 |
) // fields
|
117 |
) // fields
|
| 60 |
) // table users
|
118 |
) // table users
|
| 61 |
) // tables
|
119 |
) // tables
|
| 62 |
);
|
120 |
);
|
| 63 |
|
121 |
|
| - |
|
122 |
/** @var string[] $data Contains the information for the current logged in user */
|
| 64 |
|
123 |
private $data = array();
|
| - |
|
124 |
/** @var string[] $errors Contains errors that can occur */
|
| 65 |
private $data = array(
|
125 |
private $errors = array();
|
| - |
|
126 |
/** @var string[] $workingOn During administration, contains the record being modified */
|
| - |
|
127 |
private $workingOn = array();
|
| - |
|
128 |
|
| 66 |
);
|
129 |
/**
|
| - |
|
130 |
* constructor for an instance of the class
|
| 67 |
|
131 |
*
|
| - |
|
132 |
* Anything in $customFields will be recursively merged with $dbDefinition, overwriting
|
| 68 |
// private $workingOn = array();
|
133 |
* as necessary.
|
| 69 |
|
134 |
*
|
| - |
|
135 |
* @param string[] $customFields array to merge into $dbDefinition
|
| 70 |
|
136 |
*/
|
| 71 |
public function __construct( $customFields = array() ) {
|
137 |
public function __construct( $customFields = array() ) {
|
| 72 |
if ( $customFields ) {
|
138 |
if ( $customFields ) {
|
| 73 |
$this->dbDefinition = array_merge_recursive( $this->dbDefinition, $customFields );
|
139 |
$this->dbDefinition = array_merge_recursive( $this->dbDefinition, $customFields );
|
| 74 |
}
|
140 |
}
|
| 75 |
} // constructor
|
141 |
} // constructor
|
| 76 |
|
142 |
|
| - |
|
143 |
/**
|
| - |
|
144 |
* getter for $this->errors
|
| - |
|
145 |
*
|
| - |
|
146 |
* @return string html div containing one paragraph for every error
|
| - |
|
147 |
*/
|
| - |
|
148 |
public function errors() {
|
| - |
|
149 |
$return = "<p>" . implode( "</p>\n<p>", $this->errors ) . "</p>\n";
|
| - |
|
150 |
return "<div class='login_errors'>\n$return</div>\n";
|
| - |
|
151 |
}
|
| - |
|
152 |
|
| - |
|
153 |
/**
|
| - |
|
154 |
* clears the errors array
|
| - |
|
155 |
*/
|
| - |
|
156 |
public function clearErrors() {
|
| - |
|
157 |
$this->errors = array();
|
| - |
|
158 |
}
|
| - |
|
159 |
|
| - |
|
160 |
/**
|
| - |
|
161 |
* getter for isAdmin
|
| - |
|
162 |
*
|
| - |
|
163 |
* @return boolean true if user is an admin, false if not
|
| - |
|
164 |
*/
|
| 77 |
public function isAdmin() {
|
165 |
public function isAdmin() {
|
| 78 |
return $this->data['admin'];
|
166 |
return $this->data['admin'];
|
| 79 |
}
|
167 |
}
|
| 80 |
|
168 |
|
| - |
|
169 |
/**
|
| - |
|
170 |
* getter for login name
|
| - |
|
171 |
*
|
| - |
|
172 |
* @return string user name
|
| - |
|
173 |
*/
|
| 81 |
public function name() {
|
174 |
public function name() {
|
| 82 |
return isset( $this->data['login'] ) ? $this->data['login'] : null;
|
175 |
return isset( $this->data['login'] ) ? $this->data['login'] : null;
|
| 83 |
}
|
176 |
}
|
| 84 |
|
177 |
|
| - |
|
178 |
|
| - |
|
179 |
/**
|
| - |
|
180 |
* Main display function.
|
| - |
|
181 |
*
|
| - |
|
182 |
* This function should be called to perform the login. It performs all functions
|
| - |
|
183 |
* needed to log in and validate, but once logged in, will return an empty string.
|
| - |
|
184 |
*
|
| - |
|
185 |
* @param usersDataSource $connection A connection to the data source
|
| - |
|
186 |
* @param string $nextScript The url to be run when logged in
|
| - |
|
187 |
*
|
| - |
|
188 |
* @return string A (possibly empty) HTML div
|
| - |
|
189 |
*/
|
| 85 |
public function HTML( $connection, $nextScript = null ) {
|
190 |
public function HTML( $connection, $nextScript = null ) {
|
| 86 |
if ( isset( $_REQUEST['username'], $_REQUEST['password'] ) ) {
|
191 |
if ( isset( $_REQUEST['username'], $_REQUEST['password'] ) ) {
|
| 87 |
$this->validate( $_REQUEST['username'], $_REQUEST['password'], $connection );
|
192 |
$this->validate( $_REQUEST['username'], $_REQUEST['password'], $connection );
|
| 88 |
}
|
193 |
}
|
| 89 |
if ( isset( $_REQUEST['logout'] ) && $_REQUEST['logout'] == 'Logout' ) {
|
194 |
if ( isset( $_REQUEST['logout'] ) && $_REQUEST['logout'] == 'Logout' ) {
|
| Line 92... |
Line 197... |
| 92 |
if ( ! isset( $this->data['login'], $this->data['id'] ) ) {
|
197 |
if ( ! isset( $this->data['login'], $this->data['id'] ) ) {
|
| 93 |
return $this->logInScreen();
|
198 |
return $this->logInScreen();
|
| 94 |
}
|
199 |
}
|
| 95 |
}
|
200 |
}
|
| 96 |
|
201 |
|
| - |
|
202 |
/**
|
| - |
|
203 |
* Validates a connection and, on success, populates $data
|
| - |
|
204 |
*
|
| - |
|
205 |
* Function will validate the username and password passed in, using
|
| - |
|
206 |
* data connection $connection. On success, populates class member $data
|
| - |
|
207 |
* with the values from the database (only those listed in $dbDefinition)
|
| - |
|
208 |
*
|
| - |
|
209 |
* On Failure, appends $error with a failure string
|
| - |
|
210 |
*
|
| - |
|
211 |
* @param string $username The username to be matched in database
|
| - |
|
212 |
* @param string $password The password (unencrypted) the user entered
|
| - |
|
213 |
* @param usersDataSource $connection A connection to the data source
|
| - |
|
214 |
*
|
| - |
|
215 |
*/
|
| 97 |
public function validate( $username, $password, $connection ) {
|
216 |
private function validate( $username, $password, $connection ) {
|
| 98 |
$result = $connection->getPassword( $username );
|
217 |
$result = $connection->getPassword( $username );
|
| 99 |
if ( password_verify( $password, $result['pass'] ) ) {
|
218 |
if ( password_verify( $password, $result['pass'] ) ) {
|
| 100 |
$result = $connection->getRecord( $username );
|
219 |
$result = $connection->getRecord( $username );
|
| 101 |
$this->data['id'] = $result['id'];
|
220 |
$this->data['id'] = $result['id'];
|
| 102 |
foreach ( $this->dbDefinition['tables']['users']['fields'] as $key => $record ) {
|
221 |
foreach ( $this->dbDefinition['tables']['users']['fields'] as $key => $record ) {
|
| 103 |
if ( $key != 'pass' )
|
222 |
if ( $key != 'pass' )
|
| 104 |
$this->data[$key] = $result[$key];
|
223 |
$this->data[$key] = $result[$key];
|
| 105 |
}
|
224 |
}
|
| 106 |
} else {
|
225 |
} else {
|
| 107 |
$this->errors[] = 'Login Failed';
|
226 |
$this->errors[] = 'Login Failed: Unknown username or password';
|
| 108 |
foreach ( $this->dbDefinition['tables']['users']['fields'] as $key => $record ) {
|
227 |
foreach ( $this->dbDefinition['tables']['users']['fields'] as $key => $record ) {
|
| 109 |
$this->data[$key] = null;
|
228 |
$this->data[$key] = null;
|
| 110 |
}
|
229 |
}
|
| 111 |
}
|
230 |
}
|
| 112 |
} // validate
|
231 |
} // validate
|
| 113 |
|
232 |
|
| - |
|
233 |
/**
|
| - |
|
234 |
* Get all users from data source and put them in an HTML list
|
| 114 |
|
235 |
*
|
| - |
|
236 |
* Will retrieve the ID and login name of all users, putting them
|
| - |
|
237 |
* in a list of anchors to allow an admin to select one for editing
|
| - |
|
238 |
*
|
| - |
|
239 |
* @param usersDataSource $connection A connection to the data source
|
| - |
|
240 |
* @param string $nextPage The URL of the page to be used in the link
|
| - |
|
241 |
*
|
| - |
|
242 |
* @return string an unordered list (UL) containing links with names
|
| - |
|
243 |
*/
|
| 115 |
public function allUsersHTML ( $connection, $nextPage = null ) {
|
244 |
public function allUsersHTML ( $connection, $nextPage = null ) {
|
| 116 |
$nextPage = self::getNextScript( $nextPage );
|
245 |
$nextPage = self::getNextScript( $nextPage );
|
| 117 |
$return = '';
|
246 |
$return = '';
|
| 118 |
$allUsers = $connection->getAllUsers();
|
247 |
$allUsers = $connection->getAllUsers();
|
| 119 |
foreach ( $allUsers as $row ) {
|
248 |
foreach ( $allUsers as $row ) {
|
| Line 126... |
Line 255... |
| 126 |
$return = "<ul class='login_list'>\n$return\n</ul>\n";
|
255 |
$return = "<ul class='login_list'>\n$return\n</ul>\n";
|
| 127 |
$return = "<div class='login_list'>\n$return\n</div>\n";
|
256 |
$return = "<div class='login_list'>\n$return\n</div>\n";
|
| 128 |
return $return;
|
257 |
return $return;
|
| 129 |
}
|
258 |
}
|
| 130 |
|
259 |
|
| - |
|
260 |
/**
|
| - |
|
261 |
* Logs user out of system
|
| - |
|
262 |
*
|
| - |
|
263 |
* destroys itself ($_SESSION['user'], then session, then calls
|
| - |
|
264 |
* $nextScript by doing a header call.
|
| - |
|
265 |
*
|
| - |
|
266 |
* @param string $nextScript URL of next script to call
|
| - |
|
267 |
*/
|
| 131 |
public function logOut( $nextScript = null ) {
|
268 |
public function logOut( $nextScript = null ) {
|
| 132 |
$nextScript = $this->getNextScript( $nextScript );
|
269 |
$nextScript = $this->getNextScript( $nextScript );
|
| 133 |
$_SESSION['user'] = null;
|
270 |
$_SESSION['user'] = null;
|
| 134 |
session_destroy();
|
271 |
session_destroy();
|
| 135 |
header( "Location: $nextScript" );
|
272 |
header( "Location: $nextScript" );
|
| 136 |
}
|
273 |
}
|
| 137 |
|
274 |
|
| - |
|
275 |
/**
|
| - |
|
276 |
* Simple helper script to calculate next script to call
|
| - |
|
277 |
*
|
| - |
|
278 |
* Returns one of three URL strings, in order of precedence
|
| - |
|
279 |
* $nextScript
|
| - |
|
280 |
* $dbDefinition['screens']['validateScript']
|
| - |
|
281 |
* PHP_SELF
|
| - |
|
282 |
*
|
| - |
|
283 |
* @param string $nextScript URL to call
|
| - |
|
284 |
* @return string URL
|
| - |
|
285 |
*/
|
| 138 |
private function getNextScript( $nextScript = null ) {
|
286 |
private function getNextScript( $nextScript = null ) {
|
| 139 |
if ( ! isset( $nextScript ) ) {
|
287 |
if ( ! isset( $nextScript ) ) {
|
| 140 |
$nextScript = $this->dbDefinition['screens']['validateScript'] ?:
|
288 |
$nextScript = $this->dbDefinition['screens']['validateScript'] ?:
|
| 141 |
htmlentities($_SERVER["PHP_SELF"]);
|
289 |
htmlentities($_SERVER["PHP_SELF"]);
|
| 142 |
}
|
290 |
}
|
| 143 |
return $nextScript;
|
291 |
return $nextScript;
|
| 144 |
}
|
292 |
}
|
| 145 |
|
293 |
|
| - |
|
294 |
/**
|
| - |
|
295 |
* Creates the fields needed for a login screen
|
| - |
|
296 |
*
|
| - |
|
297 |
* Populates %s's in 'login form' with values for $nextScript and
|
| - |
|
298 |
* 'loginScreen'
|
| - |
|
299 |
*
|
| - |
|
300 |
* @param string $nextScript URL to call form
|
| - |
|
301 |
*
|
| - |
|
302 |
* @return string HTML code for display
|
| - |
|
303 |
*/
|
| 146 |
public function logInScreen( $nextScript = null ) {
|
304 |
private function logInScreen( $nextScript = null ) {
|
| 147 |
return sprintf(
|
305 |
$return = sprintf(
|
| 148 |
$this->dbDefinition['screens']['login form'],
|
306 |
$this->dbDefinition['screens']['login form'],
|
| 149 |
$this->getNextScript( $nextScript ),
|
307 |
$this->getNextScript( $nextScript ),
|
| 150 |
$this->dbDefinition['screens']['loginScreen']
|
308 |
$this->dbDefinition['screens']['loginScreen']
|
| 151 |
);
|
309 |
);
|
| - |
|
310 |
$return .= $this->errors();
|
| - |
|
311 |
$this->clearErrors();
|
| - |
|
312 |
return $return;
|
| 152 |
}
|
313 |
}
|
| 153 |
|
314 |
|
| - |
|
315 |
/**
|
| - |
|
316 |
* Creates an HTML field for display
|
| - |
|
317 |
*
|
| - |
|
318 |
* Retrieves the template for the record type, then populates it from
|
| - |
|
319 |
* $record, $value and $field. The template MUST have %s's in the
|
| - |
|
320 |
* following order for an HTML INPUT field
|
| - |
|
321 |
* label=
|
| - |
|
322 |
* name=
|
| - |
|
323 |
* title=
|
| - |
|
324 |
* placeholder=
|
| - |
|
325 |
* value
|
| - |
|
326 |
*
|
| - |
|
327 |
* Knows how to handle INPUT types TEXT, TEXTAREA, PASSWORD and
|
| - |
|
328 |
* special html type boolean, which is checkboxes.
|
| - |
|
329 |
*
|
| - |
|
330 |
* @param string $field name of the field to populate
|
| - |
|
331 |
* @param string[] $record Record from $dbDefinition[...][fields]
|
| - |
|
332 |
* @param string $value the current value to put in INPUT
|
| - |
|
333 |
*
|
| - |
|
334 |
* @return string An HTML INPUT entity
|
| - |
|
335 |
*/
|
| 154 |
private function makeHTMLField ( $field, $record, $value ) {
|
336 |
private function makeHTMLField ( $field, $record, $value ) {
|
| 155 |
$return = array();
|
337 |
$return = array();
|
| 156 |
$temp = sprintf( $this->dbDefinition['html input fields'][$record['html type']],
|
338 |
$temp = sprintf( $this->dbDefinition['html input fields'][$record['html type']],
|
| 157 |
$record['label'] ?: $field,
|
339 |
$record['label'] ?: $field,
|
| 158 |
$this->dbDefinition['input prefix'] . $field,
|
340 |
$this->dbDefinition['input prefix'] . $field,
|
| Line 174... |
Line 356... |
| 174 |
} // case
|
356 |
} // case
|
| 175 |
return $temp;
|
357 |
return $temp;
|
| 176 |
|
358 |
|
| 177 |
} // makeHTMLField
|
359 |
} // makeHTMLField
|
| 178 |
|
360 |
|
| - |
|
361 |
/**
|
| - |
|
362 |
* Creates an edit screen for display to user
|
| 179 |
|
363 |
*
|
| - |
|
364 |
* This function will create an edit screen which, when displayed to
|
| - |
|
365 |
* the user, allows them to edit a users record. The record is stored
|
| - |
|
366 |
* in $this->workingOn
|
| - |
|
367 |
*
|
| - |
|
368 |
* Function will go through each field in the users table and call makeHTMLField
|
| - |
|
369 |
* for it, unless the field is restricted and the user is editing their own
|
| - |
|
370 |
* entry. It will also create a hidden input field with the users ID
|
| - |
|
371 |
*
|
| - |
|
372 |
* NOTE: this will not create the form; the form is created someplace else
|
| - |
|
373 |
*
|
| - |
|
374 |
* @return string HTML containing all of the INPUT records a user can edit
|
| - |
|
375 |
*/
|
| 180 |
public function editScreen() {
|
376 |
public function editScreen() {
|
| 181 |
$return = array();
|
377 |
$return = array();
|
| 182 |
$return[] = $this->dbDefinition['screens']['adminScreen'];
|
378 |
$return[] = $this->dbDefinition['screens']['adminScreen'];
|
| 183 |
$return[] = "<input type='hidden' name='id' value=" . $this->workingOn['id'] . "'>\n";
|
379 |
$return[] = "<input type='hidden' name='id' value=" . $this->workingOn['id'] . "'>\n";
|
| 184 |
foreach ( $this->dbDefinition['tables']['users']['fields'] as $field => $record ) {
|
380 |
foreach ( $this->dbDefinition['tables']['users']['fields'] as $field => $record ) {
|
| Line 190... |
Line 386... |
| 190 |
$return[] = $this->makeHTMLField( $field, $record, $this->workingOn[$field] ?? '' );
|
386 |
$return[] = $this->makeHTMLField( $field, $record, $this->workingOn[$field] ?? '' );
|
| 191 |
}
|
387 |
}
|
| 192 |
return implode( "\n", $return );
|
388 |
return implode( "\n", $return );
|
| 193 |
} // editScreen
|
389 |
} // editScreen
|
| 194 |
|
390 |
|
| - |
|
391 |
/**
|
| 195 |
public function adminScreen( $connection ) {
|
392 |
* Creates a variable designed to replace $this->workingOn
|
| - |
|
393 |
*
|
| 196 |
return $this->allUsersHTML( $connection );
|
394 |
* Initializes all fields to something non-null and sets id to -1
|
| 197 |
}
|
395 |
*
|
| - |
|
396 |
* @return string[] An array initialized with all records needed
|
| 198 |
|
397 |
*/
|
| 199 |
private function emptyWorkingOn() {
|
398 |
private function emptyWorkingOn() {
|
| 200 |
$new = array();
|
399 |
$new = array();
|
| 201 |
$new['id'] = -1;
|
400 |
$new['id'] = -1;
|
| 202 |
foreach ( $this->dbDefinition['tables']['users']['fields'] as $field => $record ) {
|
401 |
foreach ( $this->dbDefinition['tables']['users']['fields'] as $field => $record ) {
|
| 203 |
if ( isset( $record['default'] ) ) {
|
402 |
if ( isset( $record['default'] ) ) {
|
| Line 215... |
Line 414... |
| 215 |
} // else
|
414 |
} // else
|
| 216 |
} // foreach
|
415 |
} // foreach
|
| 217 |
return $new;
|
416 |
return $new;
|
| 218 |
}
|
417 |
}
|
| 219 |
|
418 |
|
| - |
|
419 |
/**
|
| - |
|
420 |
* Sets up the admin function which allows users to edit themselves and, optionally, others
|
| - |
|
421 |
*
|
| - |
|
422 |
* This should be called the first time, then repeatedly called until it is done
|
| - |
|
423 |
* (it returns the string "Updated", "Failed" or "No changes".
|
| - |
|
424 |
*
|
| - |
|
425 |
* The first iteration returns an edit screen displaying the users
|
| - |
|
426 |
* information for them to edit. It will display an HTML INPUT for
|
| - |
|
427 |
* each field that is not restricted. The user can then edit the
|
| - |
|
428 |
* chosen entries and press the button, which will call the script
|
| - |
|
429 |
* again, and update the record.
|
| - |
|
430 |
*
|
| - |
|
431 |
* If the user has the admin right, the Edit screen also displays a
|
| - |
|
432 |
* list of all users as an unsigned list of anchors. If the user
|
| - |
|
433 |
* clicks on one of those, it will choose that user, load their data
|
| - |
|
434 |
* and allow the user to edit that users record. NOTE: this is the
|
| - |
|
435 |
* only way to edit fields with the restrict flag set.
|
| - |
|
436 |
*
|
| - |
|
437 |
* @param usersDataSource $connection A connection to the data source
|
| - |
|
438 |
* @param string $nextPage The URL of the page to be used in the link
|
| - |
|
439 |
*
|
| - |
|
440 |
* @return string This may be an HTML table or a single screen
|
| - |
|
441 |
*/
|
| 220 |
public function admin ( $connection, $nextScript = null ) {
|
442 |
public function admin ( $connection, $nextScript = null ) {
|
| 221 |
$nextScript = $this->getNextScript( $nextScript );
|
443 |
$nextScript = $this->getNextScript( $nextScript );
|
| 222 |
if ( ! isset($_REQUEST['id']) ) {
|
444 |
if ( ! isset($_REQUEST['id']) ) {
|
| 223 |
// we're working on ourself
|
445 |
// we're working on ourself
|
| 224 |
$this->workingOn = $this->data;
|
446 |
$this->workingOn = $this->data;
|
| Line 232... |
Line 454... |
| 232 |
}
|
454 |
}
|
| 233 |
// default to working on ourself
|
455 |
// default to working on ourself
|
| 234 |
if ( ! ( isset( $this->workingOn ) && count( $this->workingOn ) ) ) {
|
456 |
if ( ! ( isset( $this->workingOn ) && count( $this->workingOn ) ) ) {
|
| 235 |
$this->workingOn = $this->data;
|
457 |
$this->workingOn = $this->data;
|
| 236 |
}
|
458 |
}
|
| 237 |
//print "<pre>thisworkingOn=\n" . print_r($this->workingOn, true ) . "</pre>";
|
- |
|
| 238 |
// we have no data, so we should create a form for them to enter something
|
459 |
// we have no data, so we should create a form for them to enter something
|
| 239 |
if ( ! isset( $_REQUEST[$this->dbDefinition['input prefix'] . $this->dbDefinition['tables']['users']['form test']] ) ) {
|
460 |
if ( ! isset( $_REQUEST[$this->dbDefinition['input prefix'] . $this->dbDefinition['tables']['users']['form test']] ) ) {
|
| 240 |
// create the screen
|
461 |
// create the screen
|
| 241 |
$return = $this->editScreen();
|
462 |
$return = $this->editScreen();
|
| 242 |
if ( $this->data['admin'] ) {
|
463 |
if ( $this->data['admin'] ) {
|
| 243 |
$return .= self::adminScreen( $connection );
|
464 |
$return .= $this->allUsersHTML( $connection );
|
| 244 |
}
|
465 |
}
|
| 245 |
return sprintf( $this->dbDefinition['screens']['edit form'],
|
466 |
return sprintf( $this->dbDefinition['screens']['edit form'],
|
| 246 |
$nextScript,
|
467 |
$nextScript,
|
| 247 |
$return
|
468 |
$return
|
| 248 |
);
|
469 |
);
|
| Line 257... |
Line 478... |
| 257 |
switch ( $record['html type'] ) {
|
478 |
switch ( $record['html type'] ) {
|
| 258 |
case 'textarea':
|
479 |
case 'textarea':
|
| 259 |
case 'text' :
|
480 |
case 'text' :
|
| 260 |
if ( $this->workingOn['id'] == -1 || ( isset( $_REQUEST[$htmlFieldName] ) && $_REQUEST[$htmlFieldName] !== $this->workingOn[$field] ) ) {
|
481 |
if ( $this->workingOn['id'] == -1 || ( isset( $_REQUEST[$htmlFieldName] ) && $_REQUEST[$htmlFieldName] !== $this->workingOn[$field] ) ) {
|
| 261 |
$data[$field] = $_REQUEST[$htmlFieldName];
|
482 |
$data[$field] = $_REQUEST[$htmlFieldName];
|
| 262 |
/* if ( isset( $record['filter'] ) && preg_filter( $record['filter'], '', $temp ) !== $temp ) {
|
483 |
if ( isset( $record['filter'] ) && preg_filter( $record['filter'], '', $temp ) !== $temp ) {
|
| 263 |
$this->errors[] = "Invalid characters in $field";
|
484 |
$this->errors[] = sprintf( "Invalid characters in %s, %s", $record['label'], $record['instructions'] );
|
| 264 |
$temp = '';
|
485 |
$temp = '';
|
| 265 |
}
|
486 |
}
|
| 266 |
*/ }
|
487 |
}
|
| 267 |
break;
|
488 |
break;
|
| 268 |
case 'password':
|
489 |
case 'password':
|
| 269 |
if ( ! empty( $_REQUEST[$htmlFieldName] ) )
|
490 |
if ( ! empty( $_REQUEST[$htmlFieldName] ) )
|
| 270 |
$data[$field] = password_hash( $_REQUEST[$htmlFieldName], PASSWORD_DEFAULT );
|
491 |
$data[$field] = password_hash( $_REQUEST[$htmlFieldName], PASSWORD_DEFAULT );
|
| 271 |
break;
|
492 |
break;
|
| Line 274... |
Line 495... |
| 274 |
$data[$field] = isset( $_REQUEST[$htmlFieldName] ) ? 1 : 0;
|
495 |
$data[$field] = isset( $_REQUEST[$htmlFieldName] ) ? 1 : 0;
|
| 275 |
}
|
496 |
}
|
| 276 |
break;
|
497 |
break;
|
| 277 |
} // switch
|
498 |
} // switch
|
| 278 |
} // foreach
|
499 |
} // foreach
|
| - |
|
500 |
if ( count($this->errors) ) { // we have some errors
|
| - |
|
501 |
$this->errors[] = 'Record not updated';
|
| - |
|
502 |
return 'Error';
|
| - |
|
503 |
}
|
| 279 |
if ( $data ) {
|
504 |
if ( $data ) {
|
| 280 |
$data['id'] = $this->workingOn['id'];
|
505 |
$data['id'] = $this->workingOn['id'];
|
| 281 |
$return = $connection->update( $data ) ? "Updated" : "Failed";
|
506 |
$return = $connection->update( $data ) ? "Updated" : "Failed";
|
| 282 |
if ( $this->workingOn['id'] == $this->data['id'] ) // we just updated us, reload record
|
507 |
if ( $this->workingOn['id'] == $this->data['id'] ) // we just updated us, reload record
|
| 283 |
$this->data = $connection->getARecord( array( 'id' => $this->data['id'] ) );
|
508 |
$this->data = $connection->getARecord( array( 'id' => $this->data['id'] ) );
|
| Line 287... |
Line 512... |
| 287 |
unset( $this->workingOn );
|
512 |
unset( $this->workingOn );
|
| 288 |
return $return;
|
513 |
return $return;
|
| 289 |
} // else
|
514 |
} // else
|
| 290 |
} // admin
|
515 |
} // admin
|
| 291 |
|
516 |
|
| 292 |
public function errors() {
|
- |
|
| 293 |
return $this->errors;
|
- |
|
| 294 |
}
|
- |
|
| 295 |
} // class Users
|
517 |
} // class Users
|
| 296 |
|
518 |
|
| 297 |
?>
|
519 |
?>
|