Subversion Repositories php_users

Rev

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

Rev 7 Rev 8
Line 2... Line 2...
2
<?php
2
<?php
3
   include_once( '../UsersDataSourceMySQLi.class.php' );
3
   include_once( '../UsersDataSourceMySQLi.class.php' );
4
   include_once( '../Users.class.php' );
4
   include_once( '../Users.class.php' );
5
   session_start();
5
   session_start();
6
   //session_destroy(); die;
6
   //session_destroy(); die;
7
   
-
 
8
   /*
-
 
9
    * this is an example of adding additional fields to a user record
-
 
10
    * In this case, we have added a MySQL text field named where_clause
-
 
11
    * to the database. It will be displayed as a textarea in the input
-
 
12
    * forms.
-
 
13
    * While the "For Users Class" is needed only for the new User call
-
 
14
    * and the For Data Source is only used for the new usersDataSource
-
 
15
    * call, it is completely acceptable to pass all the data to both
-
 
16
    * since they will ignore any array entries they don't know how to 
-
 
17
    * handle.
-
 
18
    */
-
 
19
   $customFields = array( 
-
 
20
      'tables' => array(
-
 
21
         'users' => array(
-
 
22
            'fields' => array(
-
 
23
               'where_clause' => array(
-
 
24
                  // For Users class
7
   // make a connection to the database
25
                     // this will be the display label on the form
-
 
26
                     'label'  => 'Limit via SQL where clause',
8
   mysqli_report( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
27
                     // the input type to use for data entry
-
 
28
                     'html type' => 'textarea',
-
 
29
                     // you can only edit this if an admin and changing someone
-
 
30
                     // else' record
-
 
31
                     'restrict' => true,
-
 
32
                     // will be displayed on a hover in HTML5 (ie, title=)
9
   $mysqlConnection = new mysqli( 'localhost', 'test', 'test', 'test' );
33
                     'instructions' => 'This will be added to every SQL query to limit access, or 1=1 for everything',
-
 
34
                     // this is entered in an empty box, ie placeholder=
-
 
35
                     'hint'     => 'Enter an SQL where clause',
-
 
36
                  // for Data Source
10
   // create a data source
37
                     'dbColumn'  =>  'where_clause',
-
 
38
                     // actual mySQL column type
11
   $connection = new usersDataSource( $mysqlConnection );
39
                     'type'      => 'text',
-
 
40
                     // set it to not null if we build the table ourselves
12
   // check if table exists and, if not, create it with username admin, password admin
41
                     'required'  => false
13
   if ( ! $connection->test() ) {
42
                     )
-
 
43
                  )
-
 
44
               )
-
 
45
            )
-
 
46
      );
-
 
47
         
-
 
48
 
-
 
49
   $connection = new usersDataSource( 
14
      $connection->buildTable( 'admin', 'admin' );
50
         null,
-
 
51
         $customFields, 
-
 
52
         array( 'username' => 'test', 'password' => 'test', 'database' => 'test' ) 
-
 
53
      );
15
   }
54
   // ensure we always have a (possibly invalid) instance of user
16
   // create an empty Users instance and save it in the session
55
   if ( ! isset( $_SESSION['user'] ) ) { 
17
   if ( ! isset( $_SESSION['user'] ) ) { 
56
      //print "Creating session users<br />";
-
 
57
      $_SESSION['user'] = new Users( $customFields );
18
      $_SESSION['user'] = new Users( );
58
   }
19
   }
-
 
20
   // if they asked to log out, log out
59
   if ( isset( $_REQUEST['logout'] ) )
21
   if ( isset( $_REQUEST['logout'] ) )
60
      $_SESSION['user']->logout();
22
      $_SESSION['user']->logout();
61
 
23
 
62
   $url = htmlentities($_SERVER["PHP_SELF"]);
24
   $url = htmlentities($_SERVER["PHP_SELF"]);
63
   
25
   
Line 67... Line 29...
67
		<meta charset="utf-8">
29
		<meta charset="utf-8">
68
		<title>Login</title>
30
		<title>Login</title>
69
     <link href='users.css' rel='stylesheet' type='text/css'>
31
     <link href='users.css' rel='stylesheet' type='text/css'>
70
	</head>
32
	</head>
71
	<body>
33
	<body>
72
      <?php
-
 
73
         //print '<pre>' . print_r( $_SESSION, true ) . "</pre>\n";
-
 
74
         //print '<pre>' . print_r( $_REQUEST, true ) . "</pre>\n";
34
      <!-- this div is only shown if we need to log in -->
75
      ?>
-
 
76
      <div class="login">
35
      <div class="login">
77
         <?php 
36
         <?php
78
            if ( isset( $_SESSION['user'] ) )
37
            // displays/processes login page if needed, empty otherwise
79
               print $_SESSION['user']->HTML($connection); 
38
            print $_SESSION['user']->HTML($connection); 
80
         ?>
39
         ?>
81
      </div>
40
      </div>
-
 
41
      <!-- Our menu. Shows who is logged in, and gives a change password
-
 
42
            and logout menu options
-
 
43
      -->
82
      <div class='menu'>
44
      <div class='menu'>
83
         <ul>
45
         <ul>
84
         <?php
46
         <?php
85
            if ( isset( $_SESSION['user'] ) && $_SESSION['user']->name() ) {
47
            if ( isset( $_SESSION['user'] ) && $_SESSION['user']->name() ) {
86
               print "<p>Logged in as " . $_SESSION['user']->name() . '</p>';
48
               print "<p>Logged in as " . $_SESSION['user']->name() . '</p>';
Line 88... Line 50...
88
               print "<li><a href='$url?logout=1'>Log Out</a></li>";
50
               print "<li><a href='$url?logout=1'>Log Out</a></li>";
89
            }
51
            }
90
         ?>
52
         ?>
91
         </ul>
53
         </ul>
92
      </div>
54
      </div>
-
 
55
      <!--
-
 
56
      this is where I'd put my content. The only thing in it right now
-
 
57
      is the code to do the admin page if "Change Password" was clicked above
-
 
58
      -->
93
      <div class='content'>
59
      <div class='content'>
94
         <?php
60
         <?php
95
            if ( isset( $_REQUEST['doAdmin'] ) ) {
61
            if ( isset( $_REQUEST['doAdmin'] ) ) {
96
               print $_SESSION['user']->admin($connection);
62
               print $_SESSION['user']->admin($connection);
97
               print $_SESSION['user']->errors();
63
               print $_SESSION['user']->errors();
98
               $_SESSION['user']->clearErrors();
64
               $_SESSION['user']->clearErrors();
99
            }
65
            }
100
         ?>
66
         ?>
101
      </div>
67
      </div>
102
      
-
 
103
	</body>
68
	</body>
104
</html>
69
</html>