source: trunk/include/user.inc.php @ 45

Last change on this file since 45 was 45, checked in by z0rglub, 21 years ago

optional cookie identification

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1<?php
2/***************************************************************************
3 *                               user.inc.php                              *
4 *                            ------------------                           *
5 *   application          : PhpWebGallery 1.3                              *
6 *   author               : Pierrick LE GALL <pierrick@z0rglub.com>        *
7 *                                                                         *
8 ***************************************************************************
9
10 ***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17// retrieving user informations
18// $infos array is used to know the fields to retrieve in the table "users"
19// Each field becomes an information of the array $user.
20// Example :
21//            status --> $user['status']
22$infos = array( 'id', 'username', 'mail_address', 'nb_image_line',
23                'nb_line_page', 'status', 'language', 'maxwidth',
24                'maxheight', 'expand', 'show_nb_comments', 'short_period',
25                'long_period', 'template' );
26
27$query_user  = 'SELECT ';
28foreach ( $infos as $i => $info ) {
29  if ( $i > 0 ) $query_user.= ',';
30  $query_user.= $info;
31}
32$query_user.= ' FROM '.PREFIX_TABLE.'users';
33$query_done = false;
34$user['is_the_guest'] = false;
35
36// cookie deletion if administrator don't authorize them anymore
37if ( !$conf['authorize_cookies'] and isset( $_COOKIE['id'] ) )
38{
39  setcookie( 'id', '', 0, cookie_path() );
40  $url = 'category.php';
41  header( 'Request-URI: '.$url ); 
42  header( 'Content-Location: '.$url ); 
43  header( 'Location: '.$url );
44  exit();
45}
46
47$user['has_cookie'] = false;
48if     ( isset( $_GET['id']    ) ) $session_id = $_GET['id'];
49elseif ( isset( $_COOKIE['id'] ) )
50{
51  $session_id = $_COOKIE['id'];
52  $user['has_cookie'] = true;
53}
54
55if ( isset( $session_id )
56     and ereg( "^[0-9a-zA-Z]{".$conf['session_id_size']."}$", $session_id ) )
57{
58  $page['session_id'] = $session_id;
59  $query = 'SELECT user_id,expiration,ip';
60  $query.= ' FROM '.PREFIX_TABLE.'sessions';
61  $query.= " WHERE id = '".$page['session_id']."'";
62  $query.= ';';
63  $result = mysql_query( $query );
64  if ( mysql_num_rows( $result ) > 0 )
65  {
66    $row = mysql_fetch_array( $result );
67    if ( !$user['has_cookie'] )
68    {
69      if ( $row['expiration'] < time() )
70      {
71        // deletion of the session from the database,
72        // because it is out-of-date
73        $delete_query = 'DELETE FROM '.PREFIX_TABLE.'sessions';
74        $delete_query.= " WHERE id = '".$page['session_id']."'";
75        $delete_query.= ';';
76        mysql_query( $delete_query );
77      }
78      if ( $_SERVER['REMOTE_ADDR'] == $row['ip'] )
79      {
80        $query_user .= ' WHERE id = '.$row['user_id'];
81        $query_done = true;
82      }
83    }
84    else
85    {
86      $query_user .= ' WHERE id = '.$row['user_id'];
87      $query_done = true;
88    }
89  }
90}
91if ( !$query_done )
92{
93  $query_user .= ' WHERE id = 2';
94  $user['is_the_guest'] = true;
95}
96$query_user .= ';';
97
98$row = mysql_fetch_array( mysql_query( $query_user ) );
99
100// affectation of each value retrieved in the users table into a variable
101// of the array $user.
102foreach ( $infos as $info ) {
103  $user[$info] = $row[$info];
104  // If the field is true or false, the variable is transformed into a
105  // boolean value.
106  if ( $row[$info] == 'true' or $row[$info] == 'false' )
107  {
108    $user[$info] = get_boolean( $row[$info] );
109  }
110}
111?>
Note: See TracBrowser for help on using the repository browser.