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

Revision 2, 9.4 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  * and Ralf Becker <ralfbecker@outdoor-training.de>                         *
7  * Copyright (C) 2000, 2001 Dan Kuykendall                                  *
8  * -------------------------------------------------------------------------*
9  * This library is part of the phpGroupWare API                             *
10  * http://www.egroupware.org/api                                            *
11  * ------------------------------------------------------------------------ *
12  * This library is free software; you can redistribute it and/or modify it  *
13  * under the terms of the GNU Lesser General Public License as published by *
14  * the Free Software Foundation; either version 2.1 of the License,         *
15  * or any later version.                                                    *
16  * This library is distributed in the hope that it will be useful, but      *
17  * WITHOUT ANY WARRANTY; without even the implied warranty of               *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
19  * See the GNU Lesser General Public License for more details.              *
20  * You should have received a copy of the GNU Lesser General Public License *
21  * along with this library; if not, write to the Free Software Foundation,  *
22  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
23  \**************************************************************************/
24
25
26        class sessions extends sessions_
27        {
28                function sessions()
29                {
30                        $this->sessions_();
31                        //controls the time out for php4 sessions - skwashd 18-May-2003
32                        ini_set('session.gc_maxlifetime', $GLOBALS['phpgw_info']['server']['sessions_timeout']);
33                        session_name('sessionid');
34                }
35
36                function read_session()
37                {
38                        if (!$this->sessionid)
39                        {
40                                return False;
41                        }
42                        session_id($this->sessionid);
43                        session_start();
44                        return $GLOBALS['phpgw_session'] = $_SESSION['phpgw_session'];
45                }
46
47                function set_cookie_params($domain)
48                {
49                        session_set_cookie_params(0,'/',$domain);
50                }
51
52                function new_session_id()
53                {
54                        session_start();
55
56                        return session_id();
57                }
58
59                function register_session($login,$user_ip,$now,$session_flags)
60                {
61                        // session_start() is now called in new_session_id() !!!
62
63                        $GLOBALS['phpgw_session']['session_id'] = $this->sessionid;
64                        $GLOBALS['phpgw_session']['session_lid'] = $login;
65                        $GLOBALS['phpgw_session']['session_ip'] = $user_ip;
66                        $GLOBALS['phpgw_session']['session_logintime'] = $now;
67                        $GLOBALS['phpgw_session']['session_dla'] = $now;
68                        $GLOBALS['phpgw_session']['session_action'] = $_SERVER['PHP_SELF'];
69                        $GLOBALS['phpgw_session']['session_flags'] = $session_flags;
70                        // we need the install-id to differ between serveral installs shareing one tmp-dir
71                        $GLOBALS['phpgw_session']['session_install_id'] = $GLOBALS['phpgw_info']['server']['install_id'];
72
73                        session_register('phpgw_session');
74                        $_SESSION['phpgw_session'] = $GLOBALS['phpgw_session'];
75                }
76
77                // This will update the DateLastActive column, so the login does not expire
78                function update_dla()
79                {
80                        if (@isset($_GET['menuaction']))
81                        {
82                                $action = $_GET['menuaction'];
83                        }
84                        else
85                        {
86                                $action = $_SERVER['PHP_SELF'];
87                        }
88
89                        // This way XML-RPC users aren't always listed as
90                        // xmlrpc.php
91                        if ($this->xmlrpc_method_called)
92                        {
93                                $action = $this->xmlrpc_method_called;
94                        }
95
96                        $GLOBALS['phpgw_session']['session_dla'] = time();
97                        $GLOBALS['phpgw_session']['session_action'] = $action;
98
99                        session_register('phpgw_session');
100                        $_SESSION['phpgw_session'] = $GLOBALS['phpgw_session'];
101
102                        return True;
103                }
104
105                function destroy($sessionid, $kp3)
106                {
107                        if (!$sessionid && $kp3)
108                        {
109                                return False;
110                        }
111
112                        $this->log_access($this->sessionid);    // log logout-time
113
114                        // Only do the following, if where working with the current user
115                        if ($sessionid == $GLOBALS['phpgw_info']['user']['sessionid'])
116                        {
117                                session_unset();
118                                //echo "<p>sessions_php4::destroy: session_destroy() returned ".(session_destroy() ? 'True' : 'False')."</p>\n";
119                                @session_destroy();
120                                if ($GLOBALS['phpgw_info']['server']['usecookies'])
121                                {
122                                        $this->phpgw_setcookie(session_name());
123                                }
124                        }
125                        else
126                        {
127                                $sessions = $this->list_sessions(0,'','',True);
128                               
129                                if (isset($sessions[$sessionid]))
130                                {
131                                        //echo "<p>session_php4::destroy($session_id): unlink('".$sessions[$sessionid]['php_session_file'].")</p>\n";
132                                        @unlink($sessions[$sessionid]['php_session_file']);
133                                }
134                        }
135
136                        return True;
137                }
138
139                /*************************************************************************\
140                * Functions for appsession data and session cache                         *
141                \*************************************************************************/
142                function delete_cache($accountid='')
143                {
144                        $account_id = get_account_id($accountid,$this->account_id);
145
146                        $GLOBALS['phpgw_session']['phpgw_app_sessions']['phpgwapi']['phpgw_info_cache'] = '';
147
148                        session_register('phpgw_session');
149                        $_SESSION['phpgw_session'] = $GLOBALS['phpgw_session'];
150                }
151
152                function appsession($location = 'default', $appname = '', $data = '##NOTHING##')
153                {
154                        if (! $appname)
155                        {
156                                $appname = $GLOBALS['phpgw_info']['flags']['currentapp'];
157                        }
158
159                        /* This allows the user to put '' as the value. */
160                        if ($data == '##NOTHING##')
161                        {
162                                // I added these into seperate steps for easier debugging
163                                $data = $GLOBALS['phpgw_session']['phpgw_app_sessions'][$appname][$location]['content'];
164
165                                /* do not decrypt and return if no data (decrypt returning garbage) */
166                                if($data)
167                                {
168                                        $data = $GLOBALS['phpgw']->crypto->decrypt($data);
169                                        //echo "appsession returning: location='$location',app='$appname',data=$data"; _debug_array($data);
170                                        return $data;
171                                }
172                        }
173                        else
174                        {
175                                $encrypteddata = $GLOBALS['phpgw']->crypto->encrypt($data);
176                                $GLOBALS['phpgw_session']['phpgw_app_sessions'][$appname][$location]['content'] = $encrypteddata;
177                                session_register('phpgw_session');
178                                $_SESSION['phpgw_session'] = $GLOBALS['phpgw_session'];
179                                return $data;
180                        }
181                }
182
183                function session_sort($a,$b)
184                {
185                        $sign = strcasecmp($GLOBALS['phpgw']->session->sort_order,'ASC') ? 1 : -1;
186
187                        return strcasecmp(
188                                $a[$GLOBALS['phpgw']->session->sort_by],
189                                $b[$GLOBALS['phpgw']->session->sort_by]
190                        ) * $sign;
191                }
192
193                /*!
194                @function list_sessions
195                @abstract get list of normal / non-anonymous sessions
196                @note The data form the session-files get cached in the app_session phpgwapi/php4_session_cache
197                @author ralfbecker
198                */
199                function list_sessions($start,$order,$sort,$all_no_sort = False)
200                {
201                        //echo "<p>session_php4::list_sessions($start,'$order','$sort',$all)</p>\n";
202                        $session_cache = $this->appsession('php4_session_cache','phpgwapi');
203
204                        $values = array();
205                        $maxmatchs = $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
206                        $dir = @opendir($path = ini_get('session.save_path'));
207                        if (!$dir)      // eg. openbasedir restrictions
208                        {
209                                return $values;
210                        }
211                        while ($file = readdir($dir))
212                        {
213                                if (substr($file,0,5) != 'sess_')
214                                {
215                                        continue;
216                                }
217                                if (isset($session_cache[$file]))       // use copy from cache
218                                {
219                                        $session = $session_cache[$file];
220
221                                        if ($session['session_flags'] == 'A' || !$session['session_id'] ||
222                                                $session['session_install_id'] != $GLOBALS['phpgw_info']['server']['install_id'])
223                                        {
224                                                continue;       // no anonymous sessions or other domains or installations
225                                        }
226                                        if (!$all_no_sort)      // we need the up-to-date data --> unset and reread it
227                                        {
228                                                unset($session_cache[$file]);
229                                        }
230                                }
231                                if (!isset($session_cache[$file]))      // not in cache, read and cache it
232                                {
233                                        if (!is_readable($path. '/' . $file))
234                                        {
235                                                continue;       // happens if webserver runs multiple user-ids
236                                        }
237                                        $session = '';
238                                        if (($fd = fopen ($path . '/' . $file,'r')))
239                                        {
240                                                $session = ($size = filesize ($path . '/' . $file)) ? @fread ($fd, $size) : 0;
241                                                fclose ($fd);
242                                        }
243                                        if (substr($session,0,14) != 'phpgw_session|')
244                                        {
245                                                continue;
246                                        }
247                                        $session = unserialize(substr($session,14));
248                                        unset($session['phpgw_app_sessions']);  // not needed, saves memory
249                                        $session_cache[$file] = $session;
250                                }
251                                if($session['session_flags'] == 'A' || !$session['session_id'] ||
252                                        $session['session_install_id'] != $GLOBALS['phpgw_info']['server']['install_id'])
253                                {
254                                        continue;       // no anonymous sessions or other domains or installations
255                                }
256                                //echo "file='$file'=<pre>"; print_r($session); echo "</pre>";
257
258                                $session['php_session_file'] = $path . '/' . $file;
259                                $values[$session['session_id']] = $session;
260                        }
261                        closedir($dir);
262
263                        if(!$all_no_sort)
264                        {
265                                $GLOBALS['phpgw']->session->sort_by = $sort;
266                                $GLOBALS['phpgw']->session->sort_order = $order;
267
268                                uasort($values,array('sessions','session_sort'));
269
270                                $i = 0;
271                                $start = (int)$start;
272                                foreach($values as $id => $data)
273                                {
274                                        if($i < $start || $i > $start+$maxmatchs)
275                                        {
276                                                unset($values[$id]);
277                                        }
278                                        ++$i;
279                                }
280                                reset($values);
281                        }
282                        $this->appsession('php4_session_cache','phpgwapi',$session_cache);
283
284                        return $values;
285                }
286
287                /*!
288                @function total
289                @abstract get number of normal / non-anonymous sessions
290                @author ralfbecker
291                */
292                function total()
293                {
294                        return count($this->list_sessions(0,'','',True));
295                }
296        }
Note: See TracBrowser for help on using the repository browser.