source: trunk/phpgwapi/inc/class.sessions_db.inc.php @ 2

Revision 2, 8.6 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare API - Session management                                      *
4  * This file written by Dan Kuykendall <seek3r@phpgroupware.org>            *
5  * and Joseph Engo <jengo@phpgroupware.org>                                 *
6  * Copyright (C) 2000, 2001 Dan Kuykendall                                  *
7  * -------------------------------------------------------------------------*
8  * This library is part of the eGroupWare API                               *
9  * http://www.egroupware.org/api                                            *
10  * ------------------------------------------------------------------------ *
11  * This library is free software; you can redistribute it and/or modify it  *
12  * under the terms of the GNU Lesser General Public License as published by *
13  * the Free Software Foundation; either version 2.1 of the License,         *
14  * or any later version.                                                    *
15  * This library is distributed in the hope that it will be useful, but      *
16  * WITHOUT ANY WARRANTY; without even the implied warranty of               *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
18  * See the GNU Lesser General Public License for more details.              *
19  * You should have received a copy of the GNU Lesser General Public License *
20  * along with this library; if not, write to the Free Software Foundation,  *
21  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
22  \**************************************************************************/
23
24
25        class sessions extends sessions_
26        {
27                function sessions()
28                {
29                        $this->sessions_();
30                }
31               
32                function read_session()
33                {
34                        $this->db->query("SELECT * FROM phpgw_sessions WHERE session_id='" . $this->sessionid . "'",__LINE__,__FILE__);
35                        $this->db->next_record();
36                       
37                        return $this->db->Record;
38                }
39
40                // This will remove stale sessions out of the database
41                function clean_sessions()
42                {
43                        // If you plan on using the cron apps, please remove the following lines.
44                        // I am going to make this a config option durring 0.9.11, instead of an application (jengo)
45
46                        $GLOBALS['phpgw']->db->query("DELETE FROM phpgw_sessions WHERE session_dla <= '" . (time() - $GLOBALS['phpgw_info']['server']['sessions_timeout'])
47                                . "' AND session_flags !='A'",__LINE__,__FILE__);
48
49                        // This is set a little higher, we don't want to kill session data for anonymous sessions.
50                        $GLOBALS['phpgw']->db->query("DELETE FROM phpgw_app_sessions WHERE session_dla <= '" . (time() - $GLOBALS['phpgw_info']['server']['sessions_timeout'])
51                                . "'",__LINE__,__FILE__);
52                }
53
54                function new_session_id()
55                {
56                        return md5($GLOBALS['phpgw']->common->randomstring(15));
57                }
58
59                function register_session($login,$user_ip,$now,$session_flags)
60                {
61                        $GLOBALS['phpgw']->db->query("DELETE FROM phpgw_sessions WHERE session_id='$this->sessionid'",__LINE__,__FILE__);
62
63                        $GLOBALS['phpgw']->db->query("INSERT INTO phpgw_sessions VALUES ('" . $this->sessionid
64                                . "','".$login."','" . $user_ip . "','"
65                                . $now . "','" . $now . "','" . $_SERVER['PHP_SELF'] . "','" . $session_flags
66                                . "')",__LINE__,__FILE__);
67                }
68
69                // This will update the DateLastActive column, so the login does not expire
70                function update_dla()
71                {
72                        if (@isset($_GET['menuaction']))
73                        {
74                                $action = $_GET['menuaction'];
75                        }
76                        else
77                        {
78                                $action = $_SERVER['PHP_SELF'];
79                        }
80
81                        // This way XML-RPC users aren't always listed as
82                        // xmlrpc.php
83                        if ($this->xmlrpc_method_called)
84                        {
85                                $action = $this->xmlrpc_method_called;
86                        }
87
88                        $GLOBALS['phpgw']->db->query("UPDATE phpgw_sessions SET session_dla='" . time() . "', session_action='$action' "
89                                . "WHERE session_id='" . $this->sessionid."'",__LINE__,__FILE__);
90
91                        $GLOBALS['phpgw']->db->query("UPDATE phpgw_app_sessions SET session_dla='" . time() . "' "
92                                . "WHERE sessionid='" . $this->sessionid."'",__LINE__,__FILE__);
93                        return True;
94                }
95
96                function destroy($sessionid, $kp3)
97                {
98                        if (! $sessionid && $kp3)
99                        {
100                                return False;
101                        }
102
103                        $GLOBALS['phpgw']->db->transaction_begin();
104                        $GLOBALS['phpgw']->db->query("DELETE FROM phpgw_sessions WHERE session_id='"
105                                . $sessionid . "'",__LINE__,__FILE__);
106                        $GLOBALS['phpgw']->db->query("DELETE FROM phpgw_app_sessions WHERE sessionid='"
107                                . $sessionid . "'",__LINE__,__FILE__);
108                        $this->log_access($this->sessionid);    // log logout-time
109
110                        // Only do the following, if where working with the current user
111                        if ($sessionid == $GLOBALS['phpgw_info']['user']['sessionid'])
112                        {
113                                $this->clean_sessions();
114                        }
115                        $GLOBALS['phpgw']->db->transaction_commit();
116
117                        return True;
118                }
119
120                /*************************************************************************\
121                * Functions for appsession data and session cache                         *
122                \*************************************************************************/
123
124                function delete_cache($accountid='')
125                {
126                        $account_id = get_account_id($accountid,$this->account_id);
127
128                        $query = "DELETE FROM phpgw_app_sessions WHERE loginid = '".$account_id."'"
129                                ." AND app = 'phpgwapi' AND location = 'phpgw_info_cache'";
130
131                        $GLOBALS['phpgw']->db->query($query);
132                }
133
134                function appsession($location = 'default', $appname = '', $data = '##NOTHING##')
135                {
136                        if (!$this->account_id || !$this->sessionid)
137                        {
138                                return False;   // this can happen during login or logout
139                        }
140                        if (! $appname)
141                        {
142                                $appname = $GLOBALS['phpgw_info']['flags']['currentapp'];
143                        }
144                       
145                        /* This allows the user to put '' as the value. */
146                        if ($data == '##NOTHING##')
147                        {
148                                $query = "SELECT content FROM phpgw_app_sessions WHERE"
149                                        ." sessionid='".$this->sessionid."' AND loginid='".$this->account_id."'"
150                                        ." AND app = '".$appname."' AND location='".$location."'";
151       
152                                $GLOBALS['phpgw']->db->query($query,__LINE__,__FILE__);
153                                $GLOBALS['phpgw']->db->next_record();
154
155                                // I added these into seperate steps for easier debugging
156                                $data = $GLOBALS['phpgw']->db->f('content');
157                                // Changed by Skeeter 2001 Mar 04 0400Z
158                                // This was not properly decoding structures saved into session data properly
159//                              $data = $GLOBALS['phpgw']->common->decrypt($data);
160//                              return stripslashes($data);
161                                // Changed by milosch 2001 Dec 20
162                                // do not stripslashes here unless this proves to be a problem.
163                                // Changed by milosch 2001 Dec 25
164                                /* do not decrypt and return if no data (decrypt returning garbage) */
165                                if($data)
166                                {
167                                        $data = $GLOBALS['phpgw']->crypto->decrypt($data);
168//                                      echo 'appsession returning: '; _debug_array($data);
169                                        return $data;
170                                }
171                        }
172                        else
173                        {
174                                $GLOBALS['phpgw']->db->query("SELECT content FROM phpgw_app_sessions WHERE "
175                                        . "sessionid = '".$this->sessionid."' AND loginid = '".$this->account_id."'"
176                                        . " AND app = '".$appname."' AND location = '".$location."'",__LINE__,__FILE__);
177
178                                $encrypteddata = $GLOBALS['phpgw']->crypto->encrypt($data);
179                                $encrypteddata = $GLOBALS['phpgw']->db->db_addslashes($encrypteddata);
180
181                                if ($GLOBALS['phpgw']->db->num_rows()==0)
182                                {
183                                        $GLOBALS['phpgw']->db->query("INSERT INTO phpgw_app_sessions (sessionid,loginid,app,location,content,session_dla) "
184                                                . "VALUES ('".$this->sessionid."','".$this->account_id."','".$appname
185                                                . "','".$location."','".$encrypteddata."','" . time() . "')",__LINE__,__FILE__);
186                                }
187                                else
188                                {
189                                        $GLOBALS['phpgw']->db->query("UPDATE phpgw_app_sessions SET content='".$encrypteddata."'"
190                                                . "WHERE sessionid = '".$this->sessionid."'"
191                                                . "AND loginid = '".$this->account_id."' AND app = '".$appname."'"
192                                                . "AND location = '".$location."'",__LINE__,__FILE__);
193                                }
194                                return $data;
195                        }
196                }
197
198                function list_sessions($start, $order, $sort, $all_no_sort = False)
199                {
200                        $values = array();
201                       
202                        $ordermethod = 'order by session_dla asc';
203                        $this->db->limit_query("select * from phpgw_sessions where session_flags != 'A' order by $sort $order",$start,__LINE__,__FILE__);
204
205                        while ($this->db->next_record())
206                        {
207                                $values[] = array(
208                                        'session_id'        => $this->db->f('session_id'),
209                                        'session_lid'       => $this->db->f('session_lid'),
210                                        'session_ip'        => $this->db->f('session_ip'),
211                                        'session_logintime' => $this->db->f('session_logintime'),
212                                        'session_action'    => $this->db->f('session_action'),
213                                        'session_dla'       => $this->db->f('session_dla')
214                                );
215                        }
216                        return $values;
217                }
218               
219                /*!
220                @function total
221                @abstract get number of normal / non-anonymous sessions
222                */
223                function total()
224                {
225                        $this->db->query("select count(*) from phpgw_sessions where session_flags != 'A'",__LINE__,__FILE__);
226                        $this->db->next_record();
227
228                        return $this->db->f(0);
229                }
230        }
231?>
Note: See TracBrowser for help on using the repository browser.