Subversion Repositories php_users

Rev

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

Rev 16 Rev 17
Line 146... Line 146...
146
       * 
146
       * 
147
       * @param string $string The string to be fixed
147
       * @param string $string The string to be fixed
148
       * @return string A copy of the string, ready for SQL
148
       * @return string A copy of the string, ready for SQL
149
       */
149
       */
150
      protected function escapeString ( $string ) {
150
      protected function escapeString ( $string ) {
151
         $string = $this->dbConnection->real_escape_string( $string );
151
         if ( ! is_numeric( $string ) ) {
152
         if ( ! is_numeric( $string ) )
152
            $string = $this->dbConnection->real_escape_string( $string );
153
            $string = "'$string'";
153
            $string = "'$string'";
-
 
154
         }
154
         return $string;
155
         return $string;
155
      }
156
      }
156
      
157
      
157
      /**
158
      /**
158
       * Create a query to retrieve info from database
159
       * Create a query to retrieve info from database
Line 192... Line 193...
192
                  $this->configuration['tables']['users']['fields'][$key]['dbColumn']
193
                  $this->configuration['tables']['users']['fields'][$key]['dbColumn']
193
                  ) . '= ' . $this->escapeString( $value );
194
                  ) . '= ' . $this->escapeString( $value );
194
            }
195
            }
195
            $query .= ' where ' . implode( ' and ', $temp );
196
            $query .= ' where ' . implode( ' and ', $temp );
196
         }
197
         }
197
         //print "<p>$query</p>";
-
 
198
         return $query;
198
         return $query;
199
      }
199
      }
200
      
200
      
201
      /**
201
      /**
202
       * Get a record from the database
202
       * Get a record from the database
Line 273... Line 273...
273
       * 
273
       * 
274
      */
274
      */
275
      public function buildTable( ) {
275
      public function buildTable( ) {
276
         if ( $this->dbConnection ) {
276
         if ( $this->dbConnection ) {
277
            foreach ( $this->configuration['tables'] as $table => $tableRecord ) {
277
            foreach ( $this->configuration['tables'] as $table => $tableRecord ) {
278
               //print "<pre>Building " . $table . "\n</pre>";
-
 
279
               $fields = array( $tableRecord['id'] . ' int unsigned not null auto_increment' );
278
               $fields = array( $tableRecord['id'] . ' int unsigned not null auto_increment' );
280
               foreach ( $tableRecord['fields'] as $key => $record ) {
279
               foreach ( $tableRecord['fields'] as $key => $record ) {
281
                  //print "<pre>\tColumn " . $key . ' using ' . print_r( $record, true)  . "\n</pre>";
-
 
282
                  $fieldDef = $record['dbColumn'];
280
                  $fieldDef = $record['dbColumn'];
283
                  $fieldDef .= ' ' . $record['type'];
281
                  $fieldDef .= ' ' . $record['type'];
284
                  if ( isset( $record['size'] ) ) {
282
                  if ( isset( $record['size'] ) ) {
285
                     $fieldDef .= '(' . $record['size'] . ')';
283
                     $fieldDef .= '(' . $record['size'] . ')';
286
                  }
284
                  }
Line 297... Line 295...
297
               }
295
               }
298
               $fields[] = 'primary key (' . $tableRecord['id'] . ')';
296
               $fields[] = 'primary key (' . $tableRecord['id'] . ')';
299
               $query = implode( ',', $fields );
297
               $query = implode( ',', $fields );
300
               $query = 'create or replace table ' . $tableRecord['table'] .
298
               $query = 'create or replace table ' . $tableRecord['table'] .
301
                     "($query)";
299
                     "($query)";
302
               //print '<pre>' . $query . "\n</pre>";
-
 
303
               $this->doSQL( $query );
300
               $this->doSQL( $query );
304
            }
301
            }
305
         } // foreach table
302
         } // foreach table
306
      } // buildTable
303
      } // buildTable
307
 
304
 
Line 322... Line 319...
322
            $query = sprintf( "insert into %s (%s) values (%s)", 
319
            $query = sprintf( "insert into %s (%s) values (%s)", 
323
                  $this->configuration['tables'][$table]['table'],
320
                  $this->configuration['tables'][$table]['table'],
324
                  implode( ",", $columns ), 
321
                  implode( ",", $columns ), 
325
                  implode( ',', $values ) 
322
                  implode( ',', $values ) 
326
                  );
323
                  );
327
            //print '<pre>' . $query . "\n</pre>";
-
 
328
            $this->doSQL( $query );
324
            $this->doSQL( $query );
329
         }
325
         }
330
      }
326
      }
331
      
327
      
332
      protected function tableColumnName ( $table, $field ) {
328
      protected function tableColumnName ( $table, $field ) {
Line 364... Line 360...
364
                  } // if
360
                  } // if
365
               }
361
               }
366
               $query = 'update ' . $this->configuration['tables']['users']['table'] . ' set ' .
362
               $query = 'update ' . $this->configuration['tables']['users']['table'] . ' set ' .
367
                  implode( ',', $fields ) .
363
                  implode( ',', $fields ) .
368
                  ' where ' . $this->configuration['tables']['users']['id'] . ' = ' . 
364
                  ' where ' . $this->configuration['tables']['users']['id'] . ' = ' . 
369
                  $this->dbConnection->real_escape_string( $newData['id'] );
365
                  $this->escapeString( $newData['id'] );
370
            } else { // we are doing an insert
366
            } else { // we are doing an insert
371
               $columns = array();
367
               $columns = array();
372
               $values = array();
368
               $values = array();
373
               foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
369
               foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
374
                  if ( isset( $newData[$key] ) ) {
370
                  if ( isset( $newData[$key] ) ) {
Line 378... Line 374...
378
               }
374
               }
379
               $query = 'insert into ' . $this->configuration['tables']['users']['table'] . 
375
               $query = 'insert into ' . $this->configuration['tables']['users']['table'] . 
380
                  '(' . implode( ',', $columns ) . ') values (' .
376
                  '(' . implode( ',', $columns ) . ') values (' .
381
                  implode( ',', $values ) . ')';
377
                  implode( ',', $values ) . ')';
382
            }
378
            }
383
            //print "<p>$query</p>";
-
 
384
            return $this->doSQL( $query );
379
            return $this->doSQL( $query );
385
         }
380
         }
386
      } // update
381
      } // update
387
      
382
      
388
      /**
383
      /**
Line 416... Line 411...
416
       * @parameter boolean $testing If set to true, displays query instead of executing it
411
       * @parameter boolean $testing If set to true, displays query instead of executing it
417
       * 
412
       * 
418
       * @returns mysqli_result
413
       * @returns mysqli_result
419
       */
414
       */
420
      protected function doSQL( $query, $testing = false ) {
415
      protected function doSQL( $query, $testing = false ) {
-
 
416
         
421
         if ( $testing ) {
417
         $handle = fopen( '/tmp/log.sql', 'a' );
422
            print "<pre>$query</pre>"; return;
418
         fwrite( $handle, "$query\n" );
-
 
419
         fclose( $handle );
423
         } else {
420
         
424
            mysqli_report( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
421
         mysqli_report( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
425
            $result = $this->dbConnection->query( $query );
422
         $result = $this->dbConnection->query( $query );
426
            return $result;
423
         return $result;
427
         }
-
 
428
      }
424
      }
429
      
425
      
430
      /**
426
      /**
431
       * Gets a single field from a table
427
       * Gets a single field from a table
432
       * 
428
       *