Subversion Repositories php_users

Rev

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

Rev 17 Rev 18
Line 66... Line 66...
66
       * what to use for html input fields
66
       * what to use for html input fields
67
       * These are passed to sprintf, with label, fieldname, title, placeholder and current value, in that order
67
       * These are passed to sprintf, with label, fieldname, title, placeholder and current value, in that order
68
       */
68
       */
69
      'screens'         => array (
69
      'screens'         => array (
70
         'login form' => "<h1>Login</h1>\n<form class='login_form' action='%s' method='post'>\n%s\n<input type='submit' value='Login'></form>\n",
70
         'login form' => "<h1>Login</h1>\n<form class='login_form' action='%s' method='post'>\n%s\n<input type='submit' value='Login'></form>\n",
71
         'edit form'  => "<form class='login_form' action='%s' method='post'>\n%s\n<input type='submit' id='btnUpdate' name='btnUpdate' value='Update'>\n</form>",
71
         'edit form'  => "<form class='login_form' action='%s' method='post'>\n%s\n<input type='submit' name='btnUpdate' value='Update'>\n</form>",
72
         'loginScreen' => "<div class='login_field'>\n<input class='login_field' type='text' name='username' placeholder='Username' required autofocus>\n</div>\n<div class='login_field'>\n<input class='login_field' type='password' name='password' placeholder='Password' required>\n</div>",
72
         'loginScreen' => "<div class='login_field'>\n<input class='login_field' type='text' name='username' placeholder='Username' required autofocus>\n</div>\n<div class='login_field'>\n<input class='login_field' type='password' name='password' placeholder='Password' required>\n</div>",
73
         'adminScreen' => "<input type='hidden' name='doAdmin' value='1'>\n",
73
         'adminScreen' => "<input type='hidden' name='doAdmin' value='1'>\n",
74
         'validateScript' => '',
74
         'validateScript' => '',
75
         ),
75
         ),
76
      'html input fields' => array(
76
      'html input fields' => array(
Line 218... Line 218...
218
    * 
218
    * 
219
    */
219
    */
220
   protected function validate( $username, $password, $connection ) {
220
   protected function validate( $username, $password, $connection ) {
221
      $result = $connection->getPassword( $username );
221
      $result = $connection->getPassword( $username );
222
      if ( password_verify( $password, $result['pass'] ) ) {
222
      if ( password_verify( $password, $result['pass'] ) ) {
223
         $result = $connection->getRecord( $username );
223
         $result = $connection->getARecord( array( 'login' => $username ) );
224
         $this->data['id'] = $result['id'];
224
         $this->data['id'] = $result['id'];
225
         foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
225
         foreach ( $this->configuration['tables']['users']['fields'] as $key => $record ) {
226
            if ( $key != 'pass' )
226
            if ( $key != 'pass' )
227
               $this->data[$key] = $result[$key];
227
               $this->data[$key] = $result[$key];
228
         }
228
         }
Line 380... Line 380...
380
    * @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
381
    */
381
    */
382
   public function editScreen( $connection ) {
382
   public function editScreen( $connection ) {
383
      $return = array();
383
      $return = array();
384
      $return[] = $this->configuration['screens']['adminScreen'];
384
      $return[] = $this->configuration['screens']['adminScreen'];
385
      $return[] = "<input type='hidden' name='id' value=" . $this->workingOn['id'] . "'>\n";
385
      $return[] = "<input type='hidden' name='id' value='" . $this->workingOn['id'] . "'>\n";
386
      foreach ( $this->configuration['tables']['users']['fields'] as $field => $record ) {
386
      foreach ( $this->configuration['tables']['users']['fields'] as $field => $record ) {
387
         // 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
388
         // also skip if it is our record
388
         // also skip if it is our record
389
         if ( isset( $record['restrict'] ) && ( $this->data['id'] == $this->workingOn['id'] ) )
389
         if ( isset( $record['restrict'] ) && ( $this->data['id'] == $this->workingOn['id'] ) )
390
            continue;
390
            continue;
Line 460... Line 460...
460
         return 'Error';
460
         return 'Error';
461
      }
461
      }
462
      if ( $data ) {
462
      if ( $data ) {
463
         $data['id'] = $this->workingOn['id'];
463
         $data['id'] = $this->workingOn['id'];
464
         $return = $connection->update( $data ) ? "Updated" : "Failed";
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 {
465
      } else {
468
         $return = "No changes";
466
         $return = "No changes";
469
      }
467
      }
470
   }
468
   }
471
   
469
   
472
   protected function initWorkingOn( $connection, $id ) {
470
   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 ) ) ) {
471
      if ( ! isset($id) || $id == $this->data['id'] ) { // we're working on ourself
486
         $this->workingOn = $this->data;
472
         $this->workingOn = $this->data;
-
 
473
      } elseif ( $id == -1 ) { // a new user
-
 
474
         $this->workingOn = $this->emptyWorkingOn();
-
 
475
      } else { // this is an existing user
-
 
476
         $this->workingOn = $connection->getARecord( array( 'id' => $id ) );
487
      }
477
      }
-
 
478
   } // initWorkingOn
-
 
479
   
488
   }
480
   
489
   
481
   
490
   /**
482
   /**
491
    * Sets up the admin function which allows users to edit themselves and, optionally, others
483
    * Sets up the admin function which allows users to edit themselves and, optionally, others
492
    * 
484
    * 
493
    * This should be called the first time, then repeatedly called until it is done
485
    * This should be called the first time, then repeatedly called until it is done
Line 509... Line 501...
509
    * @param string $nextPage The URL of the page to be used in the link
501
    * @param string $nextPage The URL of the page to be used in the link
510
    * 
502
    * 
511
    * @return string This may be an HTML table or a single screen
503
    * @return string This may be an HTML table or a single screen
512
    */
504
    */
513
   public function admin ( $connection, $nextScript = null ) {
505
   public function admin ( $connection, $nextScript = null ) {
-
 
506
      /*
-
 
507
       * Entering for first time, 
-
 
508
       *    admin=1, $workingOn not set
-
 
509
       *    update workingOn
-
 
510
       *    display screen
-
 
511
       * entering after selecting a user
-
 
512
       *    doAdmin=1, id=#, $workingOn[id] <> id
-
 
513
       *    update workingOn
-
 
514
       *    display screen
-
 
515
       * Have pressed update button
-
 
516
       *    update
-
 
517
       */
-
 
518
      
514
      $nextScript = $this->getNextScript( $nextScript );
519
      $nextScript = $this->getNextScript( $nextScript );
515
      // set workingOn if not set
520
      $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
516
      if ( ! $this->workingOn || isset($_REQUEST['id']) )
521
      if ( ! $this->workingOn || $this->workingOn['id'] !== $id ) {
517
         $this->initWorkingOn( $connection, isset($_REQUEST['id']) ? $_REQUEST['id'] : null );
522
         $this->initWorkingOn( $connection, $id );
518
      // we have no data, so we should create a form for them to enter something
-
 
519
      if ( ! isset( $_REQUEST[$this->configuration['input prefix'] . $this->configuration['tables']['users']['form test']] ) ) {
-
 
520
         // create the screen
-
 
521
         $return = $this->editScreen( $connection );
523
         $return = $this->editScreen( $connection );
522
         if ( $this->data['admin'] ) {
524
         if ( $this->data['admin'] ) {
523
            $return .= $this->allUsersHTML( $connection );
525
            $return .= $this->allUsersHTML( $connection );
524
         }
526
         }
525
         return sprintf( $this->configuration['screens']['edit form'],
527
         return sprintf( $this->configuration['screens']['edit form'],
526
            $nextScript,
528
            $nextScript,
527
            $return
529
            $return
528
            );
530
            );
529
      } else { // we are processing
531
      } elseif ( $_SERVER['REQUEST_METHOD'] === 'POST' ) { // they submitted the form
530
         $return = $this->addEdit( $connection );
532
         $return = $this->addEdit( $connection );
-
 
533
         /*
-
 
534
          * if ( $this->workingOn['id'] == $this->data['id'] ) // we just updated us, reload record
-
 
535
            $this->data = $connection->getARecord( array( 'id' => $this->data['id'] ) );
-
 
536
         */
531
         unset( $this->workingOn );
537
         unset( $this->workingOn );
532
         return $return;
538
         return $return;
533
      } // else
539
      }
534
   } // admin
540
   } // admin
535
   
541
   
536
} // class Users
542
} // class Users
537
 
543
 
538
?>
544
?>