summaryrefslogtreecommitdiffstats
path: root/code/user.inc.php
blob: bbcf4eda734cbe206fce84757d991f0379e8a9e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?PHP
  class User {
    var $uid = 0, $gid = 0;
    var $login_key = '', $login_type = '';
    var $name = '';
    
    function User() {
      if($_COOKIE['login']) {
        $res = $GLOBALS['db']->Execute('SELECT id, gid, sid, user FROM users WHERE id = ? AND sid = ?',
                                       array(substr($_COOKIE['login'], 32),
                                             substr($_COOKIE['login'], 0, 32)));
        
        if($res->RecordCount() && $res->fields[2]) {
          $this->uid = $res->fields[0];
          $this->gid = $res->fields[1];
          $this->name = $res->fields[3];
          
          $this->login_type = 'cookie';
          $this->login_key = $_COOKIE['login'];
        }
      }
      
      if($this->uid == 0 && $_GET['login']) {
        $res = $GLOBALS['db']->Execute('SELECT id, gid, sid, user FROM users WHERE id = ? AND sid = ?',
                                       array(substr($_GET['login'], 32),
                                             substr($_GET['login'], 0, 32)));
          
        if($res->RecordCount() && $res->fields[2]) {
          $this->uid = $res->fields[0];
          $this->gid = $res->fields[1];
          $this->name = $res->fields[3];
          
          $this->login_type = 'url';
          $this->login_key = $_GET['login'];
        }
      }
    }
    
    function Login($name, $pass) {
      $res = $GLOBALS['db']->Execute('SELECT id, gid, user FROM users WHERE user = ? AND password = ?', array($name, $pass));
      
      if($res->RecordCount()) {
        $id = $res->fields[0];
        $sid = md5(uniqid($name . " * " . $pass . " * " . rand()));
        
        $GLOBALS['db']->Execute('UPDATE users SET sid = ? WHERE id = ?', array($sid, $id));
        
        $this->uid = $id;
        $this->gid = $res->fields[1];
        $this->name = $res->fields[2];
        
        $this->login_type = 'url';
        $this->login_key = $sid . $id;
        
        setcookie('login', $this->login_key);
        
        return $id;
      }
      
      return 0;
    }
    
    function Logout() {
      $GLOBALS['db']->Execute('UPDATE users SET sid = \'\' WHERE id = ?', $this->uid);
      
      $this->uid = 0;
      $this->gid = 0;
      
      $this->login_type = '';
      $this->login_key = '';
      
      setcookie('login');
    }
    
    function IsAdmin($id = -1) {
      if($id < 0) return ($this->gid == -1);
      
      return ($this->GetGid($id) == -1);
    }
    
    function GetUid() {
      return $this->uid;
    }
    
    function GetGid($id = -1) {
      if($id < 0) return $this->gid;
      if($id == 0) return 0;
      
      $res = $GLOBALS['db']->Execute('SELECT gid FROM users WHERE id = ?', $id);
      
      return $res->fields[0];
    }
    
    function GetName($id = -1) {
      if($id < 0) return $this->name;
      
      $res = $GLOBALS['db']->Execute('SELECT user FROM users WHERE id = ?', $id);
      
      return $res->fields[0];
    }
    
    function Exists($name) {
      $res = $GLOBALS['db']->Execute('SELECT id FROM users WHERE user = ?', $name);
      
      return ($res->RecordCount() > 0);
    }
    
    function Add($name, $gid, $pass) {
      if($this->Exists($name)) return 0;
      
      $GLOBALS['db']->Execute('INSERT INTO users (user, gid, password) VALUES (?, ?, ?)',
                              array($name, $gid, $pass));
      
      return $GLOBALS['db']->Insert_ID();
    }
    
    function ChangePassword($id = -1, $new_pass) {
      if($id < 0) $id = $this->uid;
      
      $GLOBALS['db']->Execute('UPDATE users SET password = ? WHERE id = ?', array($new_pass, $id));
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function ChangeGroup($id, $gid) {
      $GLOBALS['db']->Execute('UPDATE users SET gid = ? WHERE id = ?', array($gid, $id));
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function Rename($id, $new_name) {
      $GLOBALS['db']->Execute('UPDATE users SET user = ? WHERE id = ?', array($new_name, $id));
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function Delete($id) {
      $GLOBALS['db']->Execute('DELETE FROM users WHERE id = ?', $id);
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function ListUsers() {
      $res = $GLOBALS['db']->Execute('SELECT id, name FROM users ORDER BY id');
      
      return $res->GetArray();
    }
    
    function ListGroups() {
      $res = $GLOBALS['db']->Execute('SELECT id, name FROM groups ORDER BY id');
      
      return $res->GetArray();
    }
  }
  
  $GLOBALS['user'] = new User;
?>