3 # This form requires wfpl. See: http://sametwice.com/wfpl
5 # This form was initially auto-generated. If you would like to alter the
6 # parameters and generate a new one try this URL:
8 # http://metaform.localhost.jasonwoof.com/?file_name=admin_users&table_name=users&singular=account&plural=accounts&opt_email=No&opt_db=Yes&opt_listing=Yes&opt_display=No&opt_pass=No&opt_public_form=No&opt_public_display=No&fields=name%0D%0Ausername%0D%0Atextbox+password%0D%0Arole%0D%0Aint+last_active%0D%0Aint+last_login&edit=yes
13 # To save results to a database, you'll need to create the users table.
14 # The file admin_users.sql should help with this
16 # if you rename any of the database fields, you'll need to update this:
17 define('ADMIN_USERS_DB_FIELDS', 'role,name,username,last_login,last_active');
20 require_once(__DIR__.'/'.'inc/wfpl/format.php');
22 $GLOBALS['admin_users_field_to_caption'] = array(
25 'username' => 'Username',
26 'password' => 'Password',
27 'last_login' => 'Last Login',
28 'last_active' => 'Last Active'
31 function admin_users_get_fields() {
34 $data['role'] = format_options(_REQUEST_cut('role'), 'role');
35 $data['name'] = format_oneline(trim(_REQUEST_cut('name')));
36 $data['username'] = format_auth_username(trim(_REQUEST_cut('username')));
37 $data['pass1'] = format_oneline(trim(_REQUEST_cut('pass1')));
38 $data['pass2'] = format_oneline(trim(_REQUEST_cut('pass2')));
44 function admin_users_main() {
45 session_auth_must('admin_users');
47 $id = _REQUEST_cut('edit_id');
49 return admin_users_main_form($id);
52 $id = _REQUEST_cut('admin_users_delete_id');
54 return admin_users_main_delete($id);
57 if (_REQUEST_cut('new')) {
58 return admin_users_main_form();
61 if (_REQUEST_cut('list')) {
62 return admin_users_main_listing();
65 if (_REQUEST_cut('download_csv')) {
66 return admin_users_csv_download();
69 if (isset($_POST['name'])) {
70 return admin_users_main_form();
74 return admin_users_main_listing();
77 function admin_users_main_delete($id) {
78 db_delete('users', 'where id=%i', $id);
79 message('Account deleted.');
80 return './admin_users';
83 function admin_users_csv_download() {
84 require_once(__DIR__.'/'.'inc/wfpl/csv.php');
85 $rows = db_get_rows('users', 'id,'.ADMIN_USERS_DB_FIELDS, 'order by id');
86 $fields = explode(',', 'id,'.ADMIN_USERS_DB_FIELDS);
88 foreach ($fields as $field) {
89 if (isset($GLOBALS['admin_users_field_to_caption'][$field])) {
90 $header[] = $GLOBALS['admin_users_field_to_caption'][$field];
95 array_unshift($rows, $header);
96 array2d_to_csv_download($rows, 'admin_users.csv');
99 function admin_users_main_listing() {
102 $sort = _REQUEST_cut('sort');
103 if ($sort && substr($sort, 0, 1) === '-') {
104 $sort = substr($sort, 1);
107 $data["sorting-by-$sort"] = '-';
109 $legal_sorts = explode(',', ADMIN_USERS_DB_FIELDS);
110 if (!$sort || !in_array($sort, $legal_sorts)) {
111 $sort = 'role, name';
114 $data['rows'] = db_get_assocs('users', 'id,role,name,username,last_login,last_active', "order by $sort $desc limit 1000");
115 tem_set('listings', $data);
119 function admin_users_suggested_password() {
120 $character_set = "ABCDEFHJKLMNPQRTUWXY34789"; # removed all similar-looking characters
123 # PHP 4.2.0 and up seed the random number generator for you.
124 # Lets hope that it seeds with something harder to guess than the clock.
125 for($i = 0; $i < 10; ++$i) {
126 $code{$i} = $character_set{mt_rand(0, 24)}; # inclusive
132 function admin_users_main_form($id = false) {
138 ['admin', 'Site Administrator'],
139 ['disabled', 'Account Disabled']
142 if (isset($_POST['name'])) {
143 $data = admin_users_get_fields();
145 if (strlen($data['username']) < 1) {
146 message("Oop, Username is required");
147 $data['username_bad'] = true;
148 } elseif ($data['pass1'] !== $data['pass2']) {
149 message("Oop, passwords didn't match. Please enter your desired password carefully (twice).");
150 $data['password_bad'] = true;
152 # password hash is slow, so only do it if we're really doing a db write
153 if (isset($data['pass1']) && strlen($data['pass1']) > 0) {
154 # hash password for db storage
155 if (!function_exists('password_hash')) {
156 require_once(DOCROOT . 'inc/password_funcs_backported.php');
158 $data['password'] = password_hash($data['pass1'], PASSWORD_DEFAULT);
160 unset($data['pass1']);
161 unset($data['pass2']);
163 db_update_assoc('users', $data, 'where id=%i', $id);
164 message('Account updated.');
166 db_insert_assoc('users', $data);
167 message('Account saved.');
169 return './admin_users';
171 # else fall through to display the form again. Field values are in $data
173 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
174 $data = db_get_assoc('users', ADMIN_USERS_DB_FIELDS, 'where id=%i', $id);
176 # form not submitted, you can set default values like so:
177 #$data = array('name' => 'Yes');
181 tem_set('password_suggestions', [
182 admin_users_suggested_password(),
183 admin_users_suggested_password(),
184 admin_users_suggested_password(),
185 admin_users_suggested_password(),
186 admin_users_suggested_password()
188 tem_set('form', $data);