Subversion Repositories php_users

Rev

Rev 4 | Rev 10 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4 Rev 7
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
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
/*
-
 
29
 * UsersDataSourceMySQLi.class.php
-
 
30
 * 
-
 
31
 * Authors: R. W. Rodolico
-
 
32
 * 
-
 
33
 */
-
 
34
 
-
 
35
/**
-
 
36
 * usersDataSource class
-
 
37
 * 
4
 * Users class needs a way of accessing a data source. This is for a
38
 * usersDataSource provides the data access capabilities for the Users
-
 
39
 * class.
-
 
40
 * 
-
 
41
 * To build a data access class for Users, the following 5 methods must
-
 
42
 * exist.
-
 
43
 * getPassword(username)
-
 
44
 * getRecord(username)
-
 
45
 * getAllUsers()
-
 
46
 * getARecord
-
 
47
 * update
-
 
48
 * 
-
 
49
 * Additionally, where appropriate, the following function is useful
-
 
50
 * buildTable()
-
 
51
 * 
-
 
52
 * This particular instance provides an interface to MySQL using
-
 
53
 * the mysqli libraries.
-
 
54
 * 
5
 * mysqli instance. Create an instance of this, then pass that instance
55
 * Create an instance of this, then pass the variable to several Users
-
 
56
 * calls.
-
 
57
 * 
-
 
58
 * @author R. W. Rodolico <rodo@unixservertech.com>
-
 
59
 * 
6
 * to the Users class
60
 * @version 0.9.0 (beta)
-
 
61
 * @copyright 2021 Daily Data, Inc.
-
 
62
 * 
7
 */
63
 */
8
 
64
 
9
class usersDataSource {
65
class usersDataSource {
10
   
66
   
-
 
67
   /**
-
 
68
    * @var string[] $dbDefinition Contains the configuration for the class
-
 
69
    * 
-
 
70
    * May be modified by the calling program. Must be replicated in userDataSource class
-
 
71
    */
11
   private $dbDefinition = array(
72
   private $dbDefinition = array(
12
      'tables' => array(
73
      'tables' => array(
13
         'users'  => array(
74
         'users'  => array(
14
            'table'     => '_users',   // table name for user records
75
            'table'     => '_users',   // table name for user records
15
            'id'        => '_user_id', // ID column name
76
            'id'        => '_user_id', // ID column name
Line 48... Line 109...
48
                     )
109
                     )
49
               )
110
               )
50
            )
111
            )
51
         )
112
         )
52
      );
113
      );
53
      
-
 
-
 
114
      /** @var mysqli $dbConnection Holds the mysqli database connection */
54
      private $dbConnection = false;
115
      private $dbConnection = false;
55
      
116
      
-
 
117
      /**
-
 
118
       * constructor for an instance of the class
-
 
119
       * 
-
 
120
       * If $dbConnection is not null, will be used for database access
-
 
121
       * If $dbLoginInfo is not null, will override $dbConnection, make
-
 
122
       * a new connection and use that.
-
 
123
       * 
-
 
124
       * If $dbDef is set, will be merged with $dbDefinition
-
 
125
       * 
-
 
126
       * @param mysqli $dbConnection Existing mysqli database connection
-
 
127
       * @param string[] $dbDef Array to be merged with $dbDefinition
-
 
128
       * @param string[] $dbLoginInfo Array containing username, hostname, etc.. to make mysqli connection_aborted
-
 
129
       * 
-
 
130
       * @return null
-
 
131
       * 
-
 
132
       */
56
      public function __construct( $dbConnection = null, $dbDef = array(), $dbLoginInfo = array() ) {
133
      public function __construct( $dbConnection = null, $dbDef = array(), $dbLoginInfo = array() ) {
57
         $this->dbConnection = $dbConnection;
134
         $this->dbConnection = $dbConnection;
58
         if ( $dbDef ) {
135
         if ( $dbDef ) {
59
            $this->dbDefinition = array_merge_recursive( $this->dbDefinition, $dbDef );
136
            $this->dbDefinition = array_merge_recursive( $this->dbDefinition, $dbDef );
60
         }
137
         }
61
         if ( $dbLoginInfo ) {
138
         if ( $dbLoginInfo ) {
62
            $this->setDBConnection( $dbLoginInfo );
139
            $this->setDBConnection( $dbLoginInfo );
63
         }
140
         }
64
      }
141
      }
65
      
142
      
-
 
143
      /**
-
 
144
       * Make string safe for MySQL
-
 
145
       * 
-
 
146
       * If the string is completely numeric, returns it, otherwise 
-
 
147
       * puts single quotes around it
-
 
148
       * 
-
 
149
       * @param string $string The string to be fixed
-
 
150
       * @return string A copy of the string, ready for SQL
-
 
151
       */
66
      private function escapeString ( $string ) {
152
      private function escapeString ( $string ) {
67
         $string = $this->dbConnection->real_escape_string( $string );
153
         $string = $this->dbConnection->real_escape_string( $string );
68
         if ( ! is_numeric( $string ) )
154
         if ( ! is_numeric( $string ) )
69
            $string = "'$string'";
155
            $string = "'$string'";
70
         return $string;
156
         return $string;
71
      }
157
      }
72
      
158
      
73
      /**
159
      /**
74
       * Create a query to retrieve info from databawse
160
       * Create a query to retrieve info from database
75
       * 
161
       * 
76
       * Builds a query to retrieve records from the database. With all
162
       * Builds a query to retrieve records from the database. With all
77
       * parameters set to null, will retrieve all columns and records
163
       * parameters set to null, will retrieve all columns and records
78
       * Setting $field and $toFind create a where clause, and setting
164
       * Setting $field and $toFind create a where clause, and setting
79
       * $fieldList as a has (ie, 'fieldname' => 1) will limit the 
165
       * $fieldList as a has (ie, 'fieldname' => 1) will limit the 
80
       * fields returned
166
       * fields returned
81
       * 
167
       * 
82
       * @param string $field A valid field definition, which may not be a column in the table
168
       * @param string $field A valid field definition from $dbDefinition
83
       * @param string $toFind The string to find, ie where $field = $username
169
       * @param string $toFind The string to find, ie where $field = $username
84
       * @param string[] $fieldList a hash where the keys make a list of columns to return. If empty, returns all columns
170
       * @param string[] $fieldList a hash where the keys make a list of columns to return. If empty, returns all columns
85
       * 
171
       * 
86
       * @return string A cleaned and formatted SQL Query
172
       * @return string A cleaned and formatted SQL Query
87
       * 
173
       * 
Line 122... Line 208...
122
       * one
208
       * one
123
       * 
209
       * 
124
       * @param string[] $whereFields column=>value pairs for where clause
210
       * @param string[] $whereFields column=>value pairs for where clause
125
       * @param string[] $fieldList a list of columns to return. If empty, returns all columns
211
       * @param string[] $fieldList a list of columns to return. If empty, returns all columns
126
       * 
212
       * 
127
       * @return string[] a hash containing fieldname=>value pairs
213
       * @return string[] a hash containing fieldname=>value pairs from fetch_assoc
128
       * 
214
       * 
129
      */
215
      */
130
      public function getARecord( $whereFields, $fieldList = null ) {
216
      public function getARecord( $whereFields, $fieldList = null ) {
131
         // run the query, placing value in $result
217
         // run the query, placing value in $result
132
         mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
218
         mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Line 136... Line 222...
136
         }
222
         }
137
         // WTFO? nothing, so return empty array
223
         // WTFO? nothing, so return empty array
138
         return array();
224
         return array();
139
      }
225
      }
140
      
226
      
-
 
227
      /**
-
 
228
       * Retrieves the password field from table
-
 
229
       * 
-
 
230
       * Note that the password is stored as a hash in the table
-
 
231
       * 
-
 
232
       * @param string $username username used to find record
-
 
233
       * @return string[] an array of values key/value pairs
-
 
234
       */
141
      public function getPassword( $username ) {
235
      public function getPassword( $username ) {
142
         return $this->getARecord( array('login' => $username,'enabled' => 1), array('pass' => 1 ) );
236
         return $this->getARecord( array('login' => $username,'enabled' => 1), array('pass' => 1 ) );
143
      }
237
      }
144
      
238
      
-
 
239
      /**
-
 
240
       * Gets the entire record for a user
-
 
241
       * 
-
 
242
       * NOTE: this does not actually get all columns. getARecord only gets
-
 
243
       * the columns defined in $dbDefinition
-
 
244
       * 
-
 
245
       * @param string $username the value of the login field to find
-
 
246
       * 
-
 
247
       * @return string[] fieldname=>value array of found record
-
 
248
       */
145
      public function getRecord ( $username ) {
249
      public function getRecord ( $username ) {
146
         return $this->getARecord( array( 'login' => $username ) );
250
         return $this->getARecord( array( 'login' => $username ) );
147
      }
251
      }
148
      
252
      
-
 
253
      /**
-
 
254
       * Make the database connection
149
      
255
       * 
-
 
256
       * @param string[] $parameters Parameters for makeing the connection
-
 
257
       * @return mysqli|false
-
 
258
       */
150
      private function setDBConnection ( $parameters ) {
259
      private function setDBConnection ( $parameters ) {
151
         if ( !isset($parameters['username'], $parameters['password'],$parameters['database']  )) {
260
         if ( !isset($parameters['username'], $parameters['password'],$parameters['database']  )) {
152
            return false;
261
            return false;
153
         }
262
         }
154
         if ( !isset( $parameters['host'] ) ) {
263
         if ( !isset( $parameters['host'] ) ) {
155
            $parameters['host'] = 'localhost';
264
            $parameters['host'] = 'localhost';
156
         }
265
         }
157
         mysqli_report( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
266
         mysqli_report( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
158
         $this->dbConnection = new mysqli( $parameters['host'], $parameters['username'], $parameters['password'],$parameters['database'] );
267
         $this->dbConnection = new mysqli( $parameters['host'], $parameters['username'], $parameters['password'],$parameters['database'] );
159
      }
268
      }
160
      
-
 
161
      
-
 
162
 
269
 
163
      /**
270
      /**
164
       * Convenience function to create the table
271
       * Convenience function to create the table
165
       * 
272
       * 
166
       * Using $dbDefinition, build the table (replacing the current one)
273
       * Using $dbDefinition, build the table (replacing the current one)
Line 205... Line 312...
205
         return false;
312
         return false;
206
      } // buildTable
313
      } // buildTable
207
      
314
      
208
      /**
315
      /**
209
       * Tests that the database connection works and the table is built
316
       * Tests that the database connection works and the table is built
210
       * 
317
       *
-
 
318
       * @return boolean True if table exists (does not verify columns)
211
       */
319
       */
212
      public function test() {
320
      public function test() {
213
         $result = $this->dbConnection->query( sprintf( "show tables like '%s'", $this->dbDefinition['tables']['users']['table'] ) );
321
         $result = $this->dbConnection->query( sprintf( "show tables like '%s'", $this->dbDefinition['tables']['users']['table'] ) );
214
         return $result !== false && $result->num_rows;
322
         return $result !== false && $result->num_rows;
215
      } // test
323
      } // test
216
      
324
      
-
 
325
      /**
-
 
326
       * updates row in database with $newData
-
 
327
       * 
-
 
328
       * @param string[] $newData fieldname/value pairs to be updated in table
-
 
329
       * 
-
 
330
       * @return mysqli_result|bool The mysqli result from a query
-
 
331
       */
217
      public function update ( $newData ) {
332
      public function update ( $newData ) {
218
         $query = '';
333
         $query = '';
219
         foreach ( $newData as $key => $value ) {
334
         foreach ( $newData as $key => $value ) {
220
            $newData[$key] = $this->escapeString( $value );
335
            $newData[$key] = $this->escapeString( $value );
221
         }
336
         }
Line 247... Line 362...
247
            //print "<p>$query</p>";
362
            //print "<p>$query</p>";
248
            return $this->dbConnection->query( $query );
363
            return $this->dbConnection->query( $query );
249
         }
364
         }
250
      } // update
365
      } // update
251
      
366
      
-
 
367
      /**
-
 
368
       * retrieves all users from the database
252
      
369
       * 
-
 
370
       * Retrieves all data for all users from table
-
 
371
       * 
-
 
372
       * @return string[] array of array of rows/columns
-
 
373
       */
253
      public function getAllUsers() {
374
      public function getAllUsers() {
254
         $query = $this->buildQuery( null, null, array('login' => 1) );
375
         $query = $this->buildQuery( null, null, array('login' => 1) );
255
         //print "<p>$query</p>\n";
376
         //print "<p>$query</p>\n";
256
         mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
377
         mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
257
         $result = $this->dbConnection->query( $query );
378
         $result = $this->dbConnection->query( $query );