[674] | 1 | #!/usr/bin/env php |
---|
| 2 | <?php |
---|
| 3 | /** |
---|
| 4 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 5 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
| 6 | * Copyright 2001-2012 Strangecode, LLC |
---|
| 7 | * |
---|
| 8 | * This file is part of The Strangecode Codebase. |
---|
| 9 | * |
---|
| 10 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 11 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 12 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 13 | * any later version. |
---|
| 14 | * |
---|
| 15 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 18 | * details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License along with |
---|
| 21 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | /* |
---|
| 25 | * user.cli.php |
---|
| 26 | * |
---|
| 27 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 28 | * @version 1.0 |
---|
| 29 | * @since 02 May 2019 14:21:12 |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | /******************************************************************** |
---|
| 33 | * CONFIG |
---|
| 34 | ********************************************************************/ |
---|
| 35 | |
---|
| 36 | // Find a _config.inc.php file and load it. |
---|
| 37 | $_config_file = false; |
---|
| 38 | $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')); |
---|
| 39 | $rii->setMaxDepth(1); |
---|
| 40 | foreach ($rii as $filename => $file) { |
---|
| 41 | if (mb_strpos($filename, '/_config.inc.php') !== false) { |
---|
| 42 | $_config_file = $filename; |
---|
[686] | 43 | echo "Loading $_config_file\n"; |
---|
[674] | 44 | break; |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | if (!$_config_file) { |
---|
| 48 | echo "Error: could not find _config.inc.php in current directory or subdirectories.\n"; |
---|
| 49 | exit(1); |
---|
| 50 | } |
---|
| 51 | define('_CLI', true); |
---|
| 52 | require_once $_config_file; |
---|
| 53 | |
---|
| 54 | /******************************************************************** |
---|
| 55 | * MAIN |
---|
| 56 | ********************************************************************/ |
---|
| 57 | |
---|
| 58 | if (isset($CFG) && is_object($CFG)) { |
---|
[686] | 59 | echo "user.cli.php is not compatible with codebase v1.\n"; |
---|
[674] | 60 | exit(1); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | if (!isset($db) || !is_a($db, 'DB')) { |
---|
| 64 | echo "This project doesn't have a \$db object.\n"; |
---|
| 65 | exit(1); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | if (!isset($auth) || !is_a($auth, 'Auth_SQL')) { |
---|
| 69 | echo "This project doesn't have an \$auth object.\n"; |
---|
| 70 | exit(1); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | if (!$auth->getParam('db_table') || |
---|
| 74 | !$auth->getParam('db_primary_key') || |
---|
| 75 | !$auth->getParam('db_username_column')) { |
---|
| 76 | echo "This project's \$auth object does not have the required db_* parameters.\n"; |
---|
| 77 | exit(1); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | // COMMAND |
---|
| 81 | $command = User_CLI::getArg(1, 'command'); |
---|
| 82 | switch ($command) { |
---|
| 83 | case 'help': |
---|
| 84 | User_CLI::usage(); |
---|
| 85 | exit(1); |
---|
| 86 | |
---|
| 87 | case 'list': |
---|
| 88 | $users = User_CLI::getList(); |
---|
| 89 | $positions = "%-3s %-15s %-11s %-15s\n"; |
---|
| 90 | printf($positions, |
---|
| 91 | 'ID', |
---|
| 92 | 'USERNAME', |
---|
| 93 | 'LAST_ACCESS', |
---|
| 94 | 'LAST_IP' |
---|
| 95 | ); |
---|
| 96 | foreach ($users as $u) { |
---|
| 97 | printf($positions, |
---|
| 98 | $u[$auth->getParam('db_primary_key')], |
---|
| 99 | $u['username'], |
---|
| 100 | date($app->getParam('date_format'), strtotime($u['last_access_datetime'])), |
---|
| 101 | $u['last_login_ip'] |
---|
| 102 | ); |
---|
| 103 | } |
---|
| 104 | break; |
---|
| 105 | |
---|
| 106 | case 'create': |
---|
| 107 | $username = User_CLI::getArg(2, 'username'); |
---|
| 108 | $password = User_CLI::getArg(3, 'password'); |
---|
| 109 | $user_id = $auth->getUserID($username); |
---|
| 110 | if (false !== $auth->getUserID($username)) { |
---|
| 111 | printf("User '%s' already exists. Use 'update' instead.\n", $username); |
---|
| 112 | exit(1); |
---|
| 113 | } |
---|
| 114 | $user_id = User_CLI::create($username, $password); |
---|
| 115 | printf("Created user '%s' (user_id %s).\n", $username, $user_id); |
---|
| 116 | break; |
---|
| 117 | |
---|
| 118 | case 'update': |
---|
| 119 | $username = User_CLI::getArg(2, 'username'); |
---|
| 120 | $password = User_CLI::getArg(3, 'password'); |
---|
| 121 | if (!$user_id = $auth->getUserID($username)) { |
---|
| 122 | printf("User '%s' not found. Use 'create' first.\n", $username); |
---|
| 123 | exit(1); |
---|
| 124 | } |
---|
| 125 | $auth->setPassword($user_id, $password); |
---|
| 126 | printf("Updated user '%s' password (user_id %s).\n", $username, $user_id); |
---|
| 127 | break; |
---|
| 128 | |
---|
| 129 | case 'remove': |
---|
| 130 | $username = User_CLI::getArg(2, 'username'); |
---|
| 131 | if (!$user_id = $auth->getUserID($username)) { |
---|
| 132 | printf("User '%s' not found.\n", $username); |
---|
| 133 | exit(1); |
---|
| 134 | } |
---|
| 135 | User_CLI::remove($username); |
---|
| 136 | printf("Removed user '%s' (user_id %s).\n", $username, $user_id); |
---|
| 137 | break; |
---|
| 138 | |
---|
| 139 | default: |
---|
| 140 | printf("Unknown command: %s\n", $command); |
---|
| 141 | break; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | // End of script. |
---|
| 145 | exit(0); |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | /******************************************************************** |
---|
| 149 | * FUNCTIONS |
---|
| 150 | ********************************************************************/ |
---|
| 151 | |
---|
| 152 | /* |
---|
| 153 | * Static methods for this script only. |
---|
| 154 | */ |
---|
| 155 | class User_CLI |
---|
| 156 | { |
---|
| 157 | static public function getArg($pos, $name) |
---|
| 158 | { |
---|
| 159 | if (!isset($_SERVER['argv'][$pos]) || $_SERVER['argv'][$pos] == '') { |
---|
| 160 | // Required arguments missing. |
---|
[675] | 161 | printf("Required argument %s is missing. Lost? Try `%s help`.\n", strtoupper($name), basename($_SERVER['argv'][0])); |
---|
[674] | 162 | exit(1); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | return $_SERVER['argv'][$pos]; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | static public function getList() |
---|
| 169 | { |
---|
| 170 | global $auth, $db; |
---|
| 171 | |
---|
| 172 | $qid = $db->query(" |
---|
| 173 | SELECT * |
---|
[675] | 174 | FROM `" . $auth->getParam('db_table') . "` |
---|
[674] | 175 | LIMIT 1000 |
---|
| 176 | "); |
---|
| 177 | $results = array(); |
---|
| 178 | while ($row = mysql_fetch_assoc($qid)) { |
---|
| 179 | $results[] = $row; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | return $results; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | static public function create($username, $password) |
---|
| 186 | { |
---|
| 187 | global $auth, $db; |
---|
| 188 | |
---|
| 189 | $qid = $db->query("DESCRIBE " . $auth->getParam('db_table')); |
---|
| 190 | $cols = array(); |
---|
| 191 | while ($row = mysql_fetch_row($qid)) { |
---|
| 192 | $cols[] = $row[0]; |
---|
| 193 | } |
---|
| 194 | $addtl_cols = array(); |
---|
| 195 | $addtl_vals = array(); |
---|
| 196 | if (in_array('account_id', $cols)) { |
---|
| 197 | $addtl_cols[] = ", account_id"; |
---|
| 198 | $addtl_vals[] = ", '1'"; |
---|
| 199 | } |
---|
| 200 | $db->query(" |
---|
[675] | 201 | INSERT INTO `" . $auth->getParam('db_table') . "` ( |
---|
| 202 | `" . $auth->getParam('db_primary_key') . "`, |
---|
[674] | 203 | " . $auth->getParam('db_username_column') . join("\n", $addtl_cols) . " |
---|
| 204 | ) VALUES ( |
---|
| 205 | NULL, |
---|
| 206 | '" . $db->escapeString($username) . "'" . join("\n", $addtl_vals) . " |
---|
| 207 | ) |
---|
| 208 | "); |
---|
| 209 | $user_id = mysql_insert_id($db->getDBH()); |
---|
| 210 | $auth->setPassword($user_id, $password); |
---|
| 211 | |
---|
| 212 | return $user_id; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | static public function remove($username) |
---|
| 216 | { |
---|
| 217 | global $auth, $db; |
---|
| 218 | |
---|
| 219 | $qid = $db->query(" |
---|
[675] | 220 | DELETE FROM `" . $auth->getParam('db_table') . "` |
---|
| 221 | WHERE `" . $auth->getParam('db_username_column') . "` = '" . $db->escapeString($username) . "' |
---|
[674] | 222 | "); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | static public function usage() |
---|
| 226 | { |
---|
| 227 | ?> |
---|
| 228 | Manage codebase (Auth_SQL) user accounts. |
---|
| 229 | |
---|
| 230 | Usage: <?php echo basename($_SERVER['argv'][0]); ?> COMMAND [âŠ] |
---|
| 231 | |
---|
| 232 | COMMANDS |
---|
| 233 | |
---|
| 234 | help Display this help |
---|
| 235 | list List all users. |
---|
| 236 | create USERNAME PASSWORD Create a user USERNAME authenticated by PASSWORD. |
---|
| 237 | update USERNAME PASSWORD Update the password for user USERNAME to PASSWORD. |
---|
| 238 | |
---|
| 239 | This script must be run in a common site directory configured with a DB auth file, |
---|
| 240 | e.g., `lib/db_auth.json`, readable by the user executing this script. |
---|
| 241 | <?php |
---|
| 242 | } |
---|
| 243 | } |
---|