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 124... Line 124...
124
   /** @var string[] $errors Contains errors that can occur */
124
   /** @var string[] $errors Contains errors that can occur */
125
   protected $errors = array();
125
   protected $errors = array();
126
   /** @var string[] $workingOn During administration, contains the record being modified */
126
   /** @var string[] $workingOn During administration, contains the record being modified */
127
   protected $workingOn = array();
127
   protected $workingOn = array();
128
 
128
 
-
 
129
   public function data() {
-
 
130
      return $this->data;
-
 
131
   }
-
 
132
 
129
   /**
133
   /**
130
    * constructor for an instance of the class
134
    * constructor for an instance of the class
131
    * 
135
    * 
132
    * Anything in $customFields will be recursively merged with $configuration, overwriting
136
    * Anything in $customFields will be recursively merged with $configuration, overwriting
133
    * as necessary.
137
    * as necessary.
Line 220... Line 224...
220
         $this->data['id'] = $result['id'];
224
         $this->data['id'] = $result['id'];
221
         foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
225
         foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
222
            if ( $key != 'pass' )
226
            if ( $key != 'pass' )
223
               $this->data[$key] = $result[$key];
227
               $this->data[$key] = $result[$key];
224
         }
228
         }
-
 
229
         return true;
225
      } else {
230
      } else {
226
         $this->errors[] = 'Login Failed: Unknown username or password';
231
         $this->errors[] = 'Login Failed: Unknown username or password';
227
         foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
232
         foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
228
            $this->data[$key] = null;
233
            $this->data[$key] = null;
229
         }
234
         }
-
 
235
         return false;
230
      }
236
      }
231
   } // validate
237
   } // validate
232
               
238
               
233
   /**
239
   /**
234
    * Get all users from data source and put them in an HTML list
240
    * Get all users from data source and put them in an HTML list
Line 371... Line 377...
371
    * 
377
    * 
372
    * NOTE: this will not create the form; the form is created someplace else
378
    * NOTE: this will not create the form; the form is created someplace else
373
    * 
379
    * 
374
    * @return string HTML containing all of the INPUT records a user can edit
380
    * @return string HTML containing all of the INPUT records a user can edit
375
    */
381
    */
376
   public function editScreen() {
382
   public function editScreen( $connection ) {
377
      $return = array();
383
      $return = array();
378
      $return[] = $this->configuration['screens']['adminScreen'];
384
      $return[] = $this->configuration['screens']['adminScreen'];
379
      $return[] = "<input type='hidden' name='id' value=" . $this->workingOn['id'] . "'>\n";
385
      $return[] = "<input type='hidden' name='id' value=" . $this->workingOn['id'] . "'>\n";
380
      foreach ( $this->configuration['tables']['users']['fields'] as $field => $record ) {
386
      foreach ( $this->configuration['tables']['users']['fields'] as $field => $record ) {
381
         // if this field is restricted and we are not admin, just skip it
387
         // if this field is restricted and we are not admin, just skip it
Line 414... Line 420...
414
         } // else
420
         } // else
415
      } // foreach
421
      } // foreach
416
      return $new;
422
      return $new;
417
   }
423
   }
418
   
424
   
-
 
425
   protected function addEdit( $connection ) {
-
 
426
      $data = array();
-
 
427
      foreach ( $this->configuration['tables']['users']['fields'] as $field => $record ) {
-
 
428
         // if this field is restricted it is our record, skip it
-
 
429
         if ( isset( $record['restrict'] ) && ( $this->data['id'] == $this->workingOn['id'] ) )
-
 
430
            continue;
-
 
431
         $htmlFieldName = $this->configuration['input prefix'] . $field;
-
 
432
         $temp = '';
-
 
433
         switch ( $record['html type'] ) {
-
 
434
            case 'password':
-
 
435
               if ( ! empty( $_REQUEST[$htmlFieldName] ) ) {
-
 
436
                  $data[$field] = password_hash( $_REQUEST[$htmlFieldName], PASSWORD_DEFAULT );
-
 
437
                  if ( isset( $this->configuration['tables']['users']['fields']['last password change'] ) ) {
-
 
438
                     $data['last password change'] = date("YmdHis");
-
 
439
                  }
-
 
440
               }
-
 
441
               break;
-
 
442
            case 'boolean' :
-
 
443
               if ( $this->workingOn['id'] == -1 || ( isset( $_REQUEST[$htmlFieldName] ) != $this->workingOn[$field] ) ) {
-
 
444
                  $data[$field] = isset( $_REQUEST[$htmlFieldName] ) ? 1 : 0;
-
 
445
               }
-
 
446
               break;
-
 
447
            default : // text, textarea, other things like this
-
 
448
               if ( $this->workingOn['id'] == -1 || ( isset( $_REQUEST[$htmlFieldName] ) && $_REQUEST[$htmlFieldName] !== $this->workingOn[$field] ) ) {
-
 
449
                  $data[$field] = $_REQUEST[$htmlFieldName];
-
 
450
                  if ( isset( $record['filter'] ) && preg_match( $record['filter'], $data[$field] ) !== 1 ) {
-
 
451
                     $this->errors[] = sprintf( "Invalid characters in %s, %s", $record['label'], $record['instructions'] );
-
 
452
                     unset( $data[$field] );
-
 
453
                  }
-
 
454
               }
-
 
455
               break;
-
 
456
         } // switch
-
 
457
      } // foreach
-
 
458
      if ( count($this->errors) ) { // we have some errors
-
 
459
         $this->errors[] = 'Record not updated';
-
 
460
         return 'Error';
-
 
461
      }
-
 
462
      if ( $data ) {
-
 
463
         $data['id'] = $this->workingOn['id'];
-
 
464
         $return = $connection->update( $data ) ? "Updated" : "Failed";
-
 
465
         if ( $this->workingOn['id'] == $this->data['id'] ) // we just updated us, reload record
-
 
466
            $this->data = $connection->getARecord( array( 'id' => $this->data['id'] ) );
-
 
467
      } else {
-
 
468
         $return = "No changes";
-
 
469
      }
-
 
470
   }
-
 
471
   
-
 
472
   protected function initWorkingOn( $connection, $id ) {
-
 
473
      if ( ! isset($id) ) {
-
 
474
         // we're working on ourself
-
 
475
         $this->workingOn = $this->data;
-
 
476
      } elseif ( isset($id ) && $this->workingOn['id'] != $id ) {
-
 
477
         // we're working on a different user
-
 
478
         if ( $id == -1 ) { // we are adding a new user
-
 
479
            $this->workingOn = $this->emptyWorkingOn();
-
 
480
         } else { // this is an existing user
-
 
481
            $this->workingOn = $connection->getARecord( array( 'id' => $id ) );
-
 
482
         }
-
 
483
      }
-
 
484
      // default to working on ourself
-
 
485
      if ( ! ( isset( $this->workingOn ) && count( $this->workingOn ) ) ) {
-
 
486
         $this->workingOn = $this->data;
-
 
487
      }
-
 
488
   }
-
 
489
   
419
   /**
490
   /**
420
    * Sets up the admin function which allows users to edit themselves and, optionally, others
491
    * Sets up the admin function which allows users to edit themselves and, optionally, others
421
    * 
492
    * 
422
    * This should be called the first time, then repeatedly called until it is done
493
    * This should be called the first time, then repeatedly called until it is done
423
    * (it returns the string "Updated", "Failed" or "No changes".
494
    * (it returns the string "Updated", "Failed" or "No changes".
Line 439... Line 510...
439
    * 
510
    * 
440
    * @return string This may be an HTML table or a single screen
511
    * @return string This may be an HTML table or a single screen
441
    */
512
    */
442
   public function admin ( $connection, $nextScript = null ) {
513
   public function admin ( $connection, $nextScript = null ) {
443
      $nextScript = $this->getNextScript( $nextScript );
514
      $nextScript = $this->getNextScript( $nextScript );
444
      if ( ! isset($_REQUEST['id']) ) {
-
 
445
         // we're working on ourself
515
      // set workingOn if not set
446
         $this->workingOn = $this->data;
-
 
447
      } elseif ( isset($_REQUEST['id'] ) && $this->workingOn['id'] != $_REQUEST['id'] ) {
-
 
448
         // we're working on a different user
-
 
449
         if ( $_REQUEST['id'] == -1 ) { // we are adding a new user
-
 
450
            $this->workingOn = $this->emptyWorkingOn();
516
      if ( ! $this->workingOn || isset($_REQUEST['id']) )
451
         } else { // this is an existing user
-
 
452
            $this->workingOn = $connection->getARecord( array( 'id' => $_REQUEST['id'] ) );
517
         $this->initWorkingOn( $connection, isset($_REQUEST['id']) ? $_REQUEST['id'] : null );
453
         }
-
 
454
      }
-
 
455
      // default to working on ourself
-
 
456
      if ( ! ( isset( $this->workingOn ) && count( $this->workingOn ) ) ) {
-
 
457
         $this->workingOn = $this->data;
-
 
458
      }
-
 
459
      // we have no data, so we should create a form for them to enter something
518
      // we have no data, so we should create a form for them to enter something
460
      if ( ! isset( $_REQUEST[$this->configuration['input prefix'] . $this->configuration['tables']['users']['form test']] ) ) {
519
      if ( ! isset( $_REQUEST[$this->configuration['input prefix'] . $this->configuration['tables']['users']['form test']] ) ) {
461
         // create the screen
520
         // create the screen
462
         $return = $this->editScreen();
521
         $return = $this->editScreen( $connection );
463
         if ( $this->data['admin'] ) {
522
         if ( $this->data['admin'] ) {
464
            $return .= $this->allUsersHTML( $connection );
523
            $return .= $this->allUsersHTML( $connection );
465
         }
524
         }
466
         return sprintf( $this->configuration['screens']['edit form'],
525
         return sprintf( $this->configuration['screens']['edit form'],
467
            $nextScript,
526
            $nextScript,
468
            $return
527
            $return
469
            );
528
            );
470
      } else { // we are processing
529
      } else { // we are processing
471
         $data = array();
-
 
472
         foreach ( $this->configuration['tables']['users']['fields'] as $field => $record ) {
-
 
473
            // if this field is restricted it is our record, skip it
-
 
474
            if ( isset( $record['restrict'] ) && ( $this->data['id'] == $this->workingOn['id'] ) )
-
 
475
               continue;
-
 
476
            $htmlFieldName = $this->configuration['input prefix'] . $field;
-
 
477
            $temp = '';
-
 
478
            switch ( $record['html type'] ) {
-
 
479
               case 'password':
-
 
480
                  if ( ! empty( $_REQUEST[$htmlFieldName] ) ) {
-
 
481
                     $data[$field] = password_hash( $_REQUEST[$htmlFieldName], PASSWORD_DEFAULT );
-
 
482
                     if ( isset( $this->configuration['tables']['users']['fields']['last password change'] ) ) {
-
 
483
                        $data['last password change'] = date("YmdHis");
-
 
484
                     }
-
 
485
                  }
-
 
486
                  break;
-
 
487
               case 'boolean' :
-
 
488
                  if ( $this->workingOn['id'] == -1 || ( isset( $_REQUEST[$htmlFieldName] ) != $this->workingOn[$field] ) ) {
-
 
489
                     $data[$field] = isset( $_REQUEST[$htmlFieldName] ) ? 1 : 0;
-
 
490
                  }
-
 
491
                  break;
-
 
492
               default : // text, textarea, other things like this
-
 
493
                  if ( $this->workingOn['id'] == -1 || ( isset( $_REQUEST[$htmlFieldName] ) && $_REQUEST[$htmlFieldName] !== $this->workingOn[$field] ) ) {
-
 
494
                     $data[$field] = $_REQUEST[$htmlFieldName];
-
 
495
                     if ( isset( $record['filter'] ) && preg_match( $record['filter'], $data[$field] ) !== 1 ) {
-
 
496
                        $this->errors[] = sprintf( "Invalid characters in %s, %s", $record['label'], $record['instructions'] );
-
 
497
                        unset( $data[$field] );
-
 
498
                     }
-
 
499
                  }
-
 
500
                  break;
-
 
501
            } // switch
-
 
502
         } // foreach
-
 
503
         if ( count($this->errors) ) { // we have some errors
-
 
504
            $this->errors[] = 'Record not updated';
-
 
505
            return 'Error';
-
 
506
         }
-
 
507
         if ( $data ) {
-
 
508
            $data['id'] = $this->workingOn['id'];
530
         $return = $this->addEdit( $connection );
509
            $return = $connection->update( $data ) ? "Updated" : "Failed";
-
 
510
            if ( $this->workingOn['id'] == $this->data['id'] ) // we just updated us, reload record
-
 
511
               $this->data = $connection->getARecord( array( 'id' => $this->data['id'] ) );
-
 
512
         } else {
-
 
513
            $return = "No changes";
-
 
514
         }
-
 
515
         unset( $this->workingOn );
531
         unset( $this->workingOn );
516
         return $return;
532
         return $return;
517
      } // else
533
      } // else
518
   } // admin
534
   } // admin
519
   
535