1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace BSN\CryptUser;
24: use Exception, mysqli;
25:
26: require_once 'CryptDataSource.php';
27:
28: 29: 30: 31: 32: 33:
34: class CryptMySQLSource implements CryptDataSource {
35: private $mysqli;
36: private $usersTable;
37: private $databaseConfig;
38:
39:
40: 41: 42: 43:
44: public function __construct($databaseConfig) {
45: $this->databaseConfig = $databaseConfig;
46: $this->connectToDatabase();
47:
48: $this->usersTable = (!empty($databaseConfig['usersTable']) ? $databaseConfig['usersTable'] : 'users');
49: }
50:
51:
52: 53: 54:
55: private function connectToDatabase() {
56:
57: if ( !empty($this->databaseConfig['host']) &&
58: !empty($this->databaseConfig['username']) &&
59: !empty($this->databaseConfig['password']) &&
60: !empty($this->databaseConfig['database'])) {
61: $this->mysqli = new mysqli(
62: $this->databaseConfig['host'],
63: $this->databaseConfig['username'],
64: $this->databaseConfig['password'],
65: $this->databaseConfig['database']);
66:
67: if ($this->mysqli->connect_errno) {
68: throw new Exception("Failed to connect to MySQL: (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error);
69: }
70: }
71: }
72:
73:
74: 75: 76: 77: 78:
79: public function getSourceType() {
80: return 'MySQL';
81: }
82:
83:
84: 85: 86: 87: 88: 89:
90: public function getUserByName($username) {
91: $sql = "SELECT * FROM " . $this->usersTable . " WHERE username='" . $this->mysqli->real_escape_string($username) . "'";
92: $rs = $this->mysqli->query($sql);
93: if ($rs && $rs->num_rows) {
94: return $rs->fetch_assoc();
95: }
96:
97:
98: return FALSE;
99: }
100:
101:
102: 103: 104: 105:
106: public function getUsernames() {
107: $sql = "SELECT * FROM " . $this->usersTable;
108: $rs = $this->mysqli->query($sql);
109: if ($rs && $rs->num_rows) {
110: $usernames = array();
111: while ($row = $rs->fetch_assoc()) {
112: $usernames[] = $row['username'];
113: }
114: return $usernames;
115: }
116:
117:
118: return FALSE;
119: }
120:
121:
122: 123: 124: 125: 126:
127: public function saveUser($user) {
128: if ($this->getUserByName($user['username']) !== FALSE) {
129:
130: $sql = "UPDATE " . $this->usersTable . " SET " .
131: "passwordHash='" . $this->mysqli->real_escape_string($user['passwordHash']) . "', " .
132: "sslKey='" . $this->mysqli->real_escape_string($user['sslKey']) . "', " .
133: "flags='" . $this->mysqli->real_escape_string($user['flags']) . "' " .
134: "WHERE username='" . $this->mysqli->real_escape_string($user['username']) . "'";
135: }
136: else {
137:
138: $sql = "INSERT INTO " . $this->usersTable . "(username, passwordHash, sslKey, flags) VALUES (" .
139: "'" . $this->mysqli->real_escape_string($user['username']) . "', " .
140: "'" . $this->mysqli->real_escape_string($user['passwordHash']) . "', " .
141: "'" . $this->mysqli->real_escape_string($user['sslKey']) . "', " .
142: "'" . $this->mysqli->real_escape_string($user['flags']) . "'" .
143: ")";
144: }
145:
146: return $this->mysqli->query($sql);
147: }
148:
149:
150: 151: 152: 153: 154:
155: public function deleteUser($username) {
156: $sql = "DELETE FROM " . $this->usersTable . " WHERE username='" . $this->mysqli->real_escape_string($username) . "'";
157: return $this->mysqli->query($sql);
158: }
159:
160:
161: 162: 163: 164: 165:
166: private function searchUsersForUser($users, $username) {
167: if ($users) {
168: foreach ($users as $ui => $user) {
169: if ($user['username'] == $username) return $ui;
170: }
171: }
172:
173: return FALSE;
174: }
175:
176:
177: 178: 179: 180: 181:
182: public function getCreateUserTableSQL() {
183: return "CREATE TABLE `" . $this->usersTable . "` (" .
184: "`username` VARCHAR (255), " .
185: "`passwordHash` VARCHAR (255) DEFAULT '', " .
186: "`sslKey` TEXT DEFAULT '', " .
187: "`flags` INTEGER DEFAULT 0, " .
188: "PRIMARY KEY (`username`) " .
189: ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
190: }
191:
192:
193: 194: 195: 196: 197: 198:
199: public function createUsersTable($createSQL = NULL) {
200: if ($this->mysqli->ping()) {
201: if (empty($createSQL)) $createSQL = $this->getCreateUserTableSQL();
202: return $this->mysqli->query($createSQL);
203: }
204:
205: return FALSE;
206: }
207:
208:
209: 210: 211: 212: 213:
214: public function usersTableExists() {
215: if ($this->mysqli->ping()) {
216: $result = $this->mysqli->query("SHOW TABLES LIKE '" . $this->usersTable . "'");
217: return $result->num_rows > 0;
218: }
219:
220: return FALSE;
221: }
222:
223:
224: 225: 226: 227:
228: public function __wakeup() {
229: $this->connectToDatabase();
230: }
231: }
232:
233: