root/trunk/globals/classes/userdata.php @ 1

Revision 1, 4.1 kB (checked in by Winner, 5 years ago)

Initial import

Line 
1<?php
2/*
3 * WiND - Wireless Nodes Database
4 *
5 * Copyright (C) 2005 Nikolaos Nikalexis <winner@cube.gr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 dated June, 1991.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 *
20 */
21
22class userdata {
23       
24        var $logged=FALSE;
25        var $user='';
26        var $info;
27        var $privileges;
28       
29        #CONFIG
30        var $users_table = "users";
31        var $primary_key = "id";
32        var $username_key = "username";
33        var $password_key = "password";
34        var $info_keys = "username, name, surname, date_in, last_visit, last_session, status";
35        var $last_session_key = "last_session";
36        var $last_visit_key = "last_visit";
37       
38        function userdata() {
39                session_start();
40                if (isset($_SESSION['userdata'][$this->primary_key])) {
41                        $this->logged = TRUE;
42                        $this->user = $_SESSION['userdata'][$this->primary_key];
43                        $this->refresh_session();
44                } else {
45                        if (isset($_COOKIE['userdata'][$this->primary_key])) {
46                                $uid = $_COOKIE['userdata'][$this->primary_key];
47                                $p_md5 = $_COOKIE['userdata'][$this->password_key];
48                                if ($this->check_login($uid, $p_md5, TRUE)) {
49                                        $this->logged = TRUE;
50                                        $this->user_id = $uid;
51                                        $_SESSION['userdata'][$this->primary_key] = $uid;
52                                        $this->reset_visit();
53                                        $this->refresh_session();
54                                } else {
55                                        $this->logged = FALSE;
56                                }
57                        } else {
58                                $this->logged = FALSE;                 
59                        }
60                }
61                $this->load_info();
62        }
63       
64        function load_info() {
65                if ($this->logged) {
66                        global $db;
67                        $get_res = $db->get($this->info_keys, $this->users_table, $this->primary_key." = $this->user");         
68                        $this->info = $get_res[0];
69                       
70                        // EDIT HERE
71                        $get_res = $db->get('type', 'rights', "user_id = '$this->user'");               
72                        foreach( (array) $get_res as $key => $value) {
73                                $this->privileges[$value['type']] = TRUE;
74                        }
75                        //
76                       
77                } else {
78                        unset($this->info);
79                        unset($this->privileges);
80                }
81        }
82       
83        function login($username, $password, $save = FALSE) {
84                $this->logout();
85                if ($this->check_login($username, md5($password))) {
86                        global $db;
87                        $get_res = $db->get($this->primary_key, $this->users_table, $this->username_key." = '$username'");
88                        $uid = $get_res[0][$this->primary_key];
89                        $this->logged = TRUE;
90                        $this->user = $uid;
91                        $_SESSION['userdata'][$this->primary_key] = $uid;
92                        $this->reset_visit();
93                        $this->refresh_session();
94                        if ($save) {
95                                cookie('userdata['.$this->primary_key.']', $uid);
96                                cookie('userdata['.$this->password_key.']', md5($password));
97                        }
98                }
99                $this->load_info();
100                return $this->logged;
101        }
102       
103        function logout() {
104                if ($this->logged) {
105                        cookie('userdata['.$this->primary_key.']', '');
106                        cookie('userdata['.$this->password_key.']', '');
107                        $this->logged = FALSE;
108                        $this->user = '';
109                        session_destroy();
110                }
111                $this->load_info();
112        }
113       
114        function check_login($username, $password, $user_pk = FALSE) {
115                global $db;
116                $get_res = $db->get($this->password_key, $this->users_table, ($user_pk?$this->primary_key:$this->username_key)." = '$username'");
117                if (isset($get_res[0][$this->password_key]) && $password == $get_res[0][$this->password_key]) {
118                        return TRUE;
119                } else {
120                        return FALSE;
121                }
122        }
123       
124        function reset_visit($uid="") {
125                if ($uid == "") $uid = $this->user;
126                global $db;
127                $ret = $db->get($this->last_session_key, $this->users_table, $this->primary_key." = $uid");
128                $ret = $ret[0];
129                $db->set($this->users_table, array($this->last_visit_key => $ret[$this->last_session_key]), $this->primary_key." = $uid", FALSE);
130        }
131       
132        function refresh_session($uid="") {
133                if ($uid == "") $uid = $this->user;
134                global $db;
135                $db->set($this->users_table, array($this->last_session_key => date_now()), $this->primary_key." = $uid", FALSE);
136        }
137
138}
139
140?>
Note: See TracBrowser for help on using the browser.