Subversion Repositories php_users

Rev

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

Rev 18 Rev 21
Line 39... Line 39...
39
 * class.
39
 * class.
40
 * 
40
 * 
41
 * To build a data access class for Users, the following 5 methods must
41
 * To build a data access class for Users, the following 5 methods must
42
 * exist.
42
 * exist.
43
 * getPassword(username)
43
 * getPassword(username)
44
 * getRecord(username)
-
 
45
 * getAllUsers()
44
 * getAllUsers()
46
 * getARecord
45
 * getARecord
47
 * update
46
 * update
48
 * 
47
 * 
49
 * Additionally, where appropriate, the following function is useful
48
 * Additionally, where appropriate, the following function is useful
Line 59... Line 58...
59
 * 
58
 * 
60
 * @version 0.9.0 (beta)
59
 * @version 0.9.0 (beta)
61
 * @copyright 2021 Daily Data, Inc.
60
 * @copyright 2021 Daily Data, Inc.
62
 * 
61
 * 
63
 */
62
 */
-
 
63
require_once( 'UsersDataSource.class.php' );
64
 
64
 
65
class usersDataSource {
65
class usersDataSourceMySQLi extends usersDataSource {
66
   
66
   
67
   /**
67
   /**
68
    * @var string[] $configuration Contains the configuration for the class
68
    * @var string[] $configuration Contains the configuration for the class
69
    * 
69
    * 
70
    * May be modified by the calling program. Must be replicated in userDataSource class
70
    * May be modified by the calling program. Must be replicated in userDataSource class
Line 83... Line 83...
83
            'fields' => array(
83
            'fields' => array(
84
               'login'  => array(
84
               'login'  => array(
85
                     'dbColumn'  =>  'login',       // login name column name
85
                     'dbColumn'  =>  'login',       // login name column name
86
                     'type'      => 'varchar',
86
                     'type'      => 'varchar',
87
                     'size'      => 64,
87
                     'size'      => 64,
88
                     'required'  => true
88
                     'required'  => true,
-
 
89
                     'unique'    => true
89
                     ),
90
                     ),
90
               'pass'   => array( 
91
               'pass'   => array( 
91
                     'dbColumn'  => 'password',    // password column name
92
                     'dbColumn'  => 'password',    // password column name
92
                     'type'   => 'varchar',
93
                     'type'   => 'varchar',
93
                     'size'      => 128,
94
                     'size'      => 128,
Line 119... Line 120...
119
       * If $dbLoginInfo is not null, will override $dbConnection, make
120
       * If $dbLoginInfo is not null, will override $dbConnection, make
120
       * a new connection and use that.
121
       * a new connection and use that.
121
       * 
122
       * 
122
       * If $dbDef is set, will be merged with $configuration
123
       * If $dbDef is set, will be merged with $configuration
123
       * 
124
       * 
124
       * @param mysqli $dbConnection Existing mysqli database connection
125
       * @param mysqli $dbConnection Existing mysqli database connection or array with login information
125
       * @param string[] $dbDef Array to be merged with $configuration
126
       * @param string[] $dbDef Array to be merged with $configuration
126
       * @param string[] $dbLoginInfo Array containing username, hostname, etc.. to make mysqli connection_aborted
-
 
127
       * 
127
       * 
128
       * @return null
128
       * @return null
129
       * 
129
       * 
130
       */
130
       */
131
      public function __construct( $dbConnection = null, $dbDef = array(), $dbLoginInfo = array() ) {
131
      public function __construct( $connection, $customFields = array() ) {
-
 
132
         parent::__construct( $customFields );
-
 
133
         if ( is_array( $connection ) ) { // they sent us some login values
132
         $this->dbConnection = $dbConnection;
134
            $this->setDBConnection( $connection );
133
         if ( $dbDef ) {
135
         } elseif ( get_class( $connection ) == 'mysqli' ) {
134
            $this->configuration = array_merge_recursive( $this->configuration, $dbDef );
136
            $this->dbConnection = $connection;
135
         }
137
         } else {
136
         if ( $dbLoginInfo ) {
-
 
137
            $this->setDBConnection( $dbLoginInfo );
138
            throw new Exception( 'Can not open database using; must give open mysqli class or array of login information' );
138
         }
139
         }
139
      }
140
      }
140
      
141
      
141
      /**
142
      /**
142
       * Make string safe for MySQL
143
       * Make string safe for MySQL
Line 233... Line 234...
233
      public function getPassword( $username ) {
234
      public function getPassword( $username ) {
234
         return $this->getARecord( array('login' => $username,'enabled' => 1), array('pass' => 1 ) );
235
         return $this->getARecord( array('login' => $username,'enabled' => 1), array('pass' => 1 ) );
235
      }
236
      }
236
      
237
      
237
      /**
238
      /**
-
 
239
       * retrieves the userID from the table
-
 
240
       * 
-
 
241
       * @param string $username
-
 
242
       * @return integer user id
-
 
243
       */
-
 
244
      public function getUserID( $username ) {
-
 
245
         $record = $this->getARecord( array( 'login' => $username ), array('id' => 1 ) );
-
 
246
         return $record['id'];
-
 
247
      }
-
 
248
         
-
 
249
      /**
238
       * Make the database connection
250
       * Make the database connection
239
       * 
251
       * 
240
       * @param string[] $parameters Parameters for makeing the connection
252
       * @param string[] $parameters Parameters for makeing the connection
241
       * @return mysqli|false
253
       * @return mysqli|false
242
       */
254
       */
Line 266... Line 278...
266
                  $fieldDef = $record['dbColumn'];
278
                  $fieldDef = $record['dbColumn'];
267
                  $fieldDef .= ' ' . $record['type'];
279
                  $fieldDef .= ' ' . $record['type'];
268
                  if ( isset( $record['size'] ) ) {
280
                  if ( isset( $record['size'] ) ) {
269
                     $fieldDef .= '(' . $record['size'] . ')';
281
                     $fieldDef .= '(' . $record['size'] . ')';
270
                  }
282
                  }
-
 
283
                  if ( isset( $record['unique'] ) ) {
-
 
284
                     $fieldDef .= ' unique ';
-
 
285
                  }
271
                  if ( isset( $record['required'] ) ) {
286
                  if ( isset( $record['required'] ) ) {
272
                     $fieldDef .= $record['required'] ? ' not null ' : '';
287
                     $fieldDef .= $record['required'] ? ' not null ' : '';
273
                  }
288
                  }
274
                  if ( isset( $record['default'] ) ) {
289
                  if ( isset( $record['default'] ) ) {
275
                     $fieldDef .= sprintf( " default '%s'", $record['default'] );
290
                     $fieldDef .= sprintf( " default '%s'", $record['default'] );
Line 309... Line 324...
309
                  );
324
                  );
310
            $this->doSQL( $query );
325
            $this->doSQL( $query );
311
         }
326
         }
312
      }
327
      }
313
      
328
      
-
 
329
      /**
-
 
330
       * Gets the actual database column name from the configuration file
-
 
331
       * 
-
 
332
       * Since we use a lot of indirection, this is a handy function which
-
 
333
       * allows us to replace something like
-
 
334
       * $this->configuration['tables']['users']['fields']['name']['dbColumn']
-
 
335
       * with
-
 
336
       * $this->tableColumnName( 'users', 'name' )
-
 
337
       * 
-
 
338
       * If called with only one parameter (the table), will return the
-
 
339
       * actual database table name
-
 
340
       * 
-
 
341
       * @param string $table Name of Table
-
 
342
       * @param string $field Name of field in $table
-
 
343
       * @param boolean $fullTableColumn If set to true, will return table.column format
-
 
344
       * 
-
 
345
       * @return string The actual name of the dbColumn in the table
-
 
346
       */
314
      protected function tableColumnName ( $table, $field ) {
347
      protected function tableColumnName ( $table, $field = '', $fullTableColumn = false ) {
-
 
348
         if ( ! $field ) { // just return the table name
-
 
349
            $return = $this->configuration['tables'][$table]['table'];
-
 
350
         } elseif ( $field == 'id' ) { // looking for the index
-
 
351
            $return = $this->configuration['tables'][$table]['id'];
-
 
352
         } else { // return the column name
315
         return $this->configuration['tables'][$table]['fields'][$field]['dbColumn'];
353
            $return = $this->configuration['tables'][$table]['fields'][$field]['dbColumn'];
-
 
354
         }
-
 
355
         if ( $fullTableColumn && $field ) {
-
 
356
            $return = $this->configuration['tables'][$table]['table'] . '.' . $return;
-
 
357
         }
-
 
358
         return $return;
316
      }
359
      }
317
      
360
      
318
      /**
361
      /**
319
       * Tests that the database connection works and the table is built
362
       * Tests that the database connection works and the table is built
320
       *
363
       *
321
       * @return boolean True if table exists (does not verify columns)
364
       * @return boolean True if table exists (does not verify columns)
322
       */
365
       */
323
      public function test() {
366
      public function test() {
324
         $result = $this->doSQL( sprintf( "show tables like '%s'", $this->configuration['tables']['users']['table'] ) );
367
         $query = sprintf( "show tables like '%s'", $this->tableColumnName ( 'users' ) );
-
 
368
         $result = $this->doSQL( $query );
325
         return $result !== false && $result->num_rows;
369
         return $result !== false && $result->num_rows;
326
      } // test
370
      } // test
327
      
371
      
328
      /**
372
      /**
329
       * updates row in database with $newData
373
       * updates row in database with $newData
Line 343... Line 387...
343
               foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
387
               foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
344
                  if ( isset( $newData[$key] ) ) {
388
                  if ( isset( $newData[$key] ) ) {
345
                     $fields[] = $record['dbColumn'] . " = $newData[$key]";
389
                     $fields[] = $record['dbColumn'] . " = $newData[$key]";
346
                  } // if
390
                  } // if
347
               }
391
               }
348
               $query = 'update ' . $this->configuration['tables']['users']['table'] . ' set ' .
392
               $query = 'update ' . $this->tableColumnName ( 'users' ) . ' set ' .
349
                  implode( ',', $fields ) .
393
                  implode( ',', $fields ) .
350
                  ' where ' . $this->configuration['tables']['users']['id'] . ' = ' . 
394
                  ' where ' . $this->tableColumnName ( 'users', 'id' ) . ' = ' . 
351
                  $this->escapeString( $newData['id'] );
395
                  $this->escapeString( $newData['id'] );
352
            } else { // we are doing an insert
396
            } else { // we are doing an insert
353
               $columns = array();
397
               $columns = array();
354
               $values = array();
398
               $values = array();
355
               foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
399
               foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
356
                  if ( isset( $newData[$key] ) ) {
400
                  if ( isset( $newData[$key] ) ) {
357
                     $columns[] = $record['dbColumn'];
401
                     $columns[] = $record['dbColumn'];
358
                     $values[] = $newData[$key];
402
                     $values[] = $newData[$key];
359
                  } // if
403
                  } // if
360
               }
404
               }
361
               $query = 'insert into ' . $this->configuration['tables']['users']['table'] . 
405
               $query = 'insert into ' . $this->tableColumnName ( 'users' ) . 
362
                  '(' . implode( ',', $columns ) . ') values (' .
406
                  '(' . implode( ',', $columns ) . ') values (' .
363
                  implode( ',', $values ) . ')';
407
                  implode( ',', $values ) . ')';
364
            }
408
            }
365
            return $this->doSQL( $query, 'update' );
409
            return $this->doSQL( $query );
366
         }
410
         }
367
      } // update
411
      } // update
368
      
412
      
369
      /**
413
      /**
370
       * retrieves all users from the database
414
       * retrieves all users from the database