[2] | 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 | { |
---|
[3018] | 127 | if(@opendir($path = ini_get('session.save_path'))){ |
---|
| 128 | $session_file = $path."/sess_".$sessionid; |
---|
| 129 | if (file_exists($session_file)) |
---|
| 130 | @unlink($session_file); |
---|
[2] | 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | return True; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | /*************************************************************************\ |
---|
| 138 | * Functions for appsession data and session cache * |
---|
| 139 | \*************************************************************************/ |
---|
| 140 | function delete_cache($accountid='') |
---|
| 141 | { |
---|
| 142 | $account_id = get_account_id($accountid,$this->account_id); |
---|
| 143 | |
---|
| 144 | $GLOBALS['phpgw_session']['phpgw_app_sessions']['phpgwapi']['phpgw_info_cache'] = ''; |
---|
| 145 | |
---|
| 146 | session_register('phpgw_session'); |
---|
| 147 | $_SESSION['phpgw_session'] = $GLOBALS['phpgw_session']; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | function appsession($location = 'default', $appname = '', $data = '##NOTHING##') |
---|
| 151 | { |
---|
| 152 | if (! $appname) |
---|
| 153 | { |
---|
| 154 | $appname = $GLOBALS['phpgw_info']['flags']['currentapp']; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /* This allows the user to put '' as the value. */ |
---|
| 158 | if ($data == '##NOTHING##') |
---|
| 159 | { |
---|
| 160 | // I added these into seperate steps for easier debugging |
---|
| 161 | $data = $GLOBALS['phpgw_session']['phpgw_app_sessions'][$appname][$location]['content']; |
---|
| 162 | |
---|
| 163 | /* do not decrypt and return if no data (decrypt returning garbage) */ |
---|
| 164 | if($data) |
---|
| 165 | { |
---|
| 166 | $data = $GLOBALS['phpgw']->crypto->decrypt($data); |
---|
| 167 | //echo "appsession returning: location='$location',app='$appname',data=$data"; _debug_array($data); |
---|
| 168 | return $data; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | else |
---|
| 172 | { |
---|
| 173 | $encrypteddata = $GLOBALS['phpgw']->crypto->encrypt($data); |
---|
| 174 | $GLOBALS['phpgw_session']['phpgw_app_sessions'][$appname][$location]['content'] = $encrypteddata; |
---|
| 175 | session_register('phpgw_session'); |
---|
| 176 | $_SESSION['phpgw_session'] = $GLOBALS['phpgw_session']; |
---|
| 177 | return $data; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | function session_sort($a,$b) |
---|
| 182 | { |
---|
| 183 | $sign = strcasecmp($GLOBALS['phpgw']->session->sort_order,'ASC') ? 1 : -1; |
---|
| 184 | |
---|
| 185 | return strcasecmp( |
---|
| 186 | $a[$GLOBALS['phpgw']->session->sort_by], |
---|
| 187 | $b[$GLOBALS['phpgw']->session->sort_by] |
---|
| 188 | ) * $sign; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /*! |
---|
| 192 | @function list_sessions |
---|
| 193 | @abstract get list of normal / non-anonymous sessions |
---|
| 194 | @note The data form the session-files get cached in the app_session phpgwapi/php4_session_cache |
---|
| 195 | @author ralfbecker |
---|
| 196 | */ |
---|
| 197 | function list_sessions($start,$order,$sort,$all_no_sort = False) |
---|
| 198 | { |
---|
| 199 | |
---|
| 200 | $values = array(); |
---|
| 201 | $maxmatchs = $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs']; |
---|
| 202 | $dir = @opendir($path = ini_get('session.save_path')); |
---|
| 203 | if (!$dir) // eg. openbasedir restrictions |
---|
| 204 | { |
---|
| 205 | return $values; |
---|
| 206 | } |
---|
| 207 | while ($file = readdir($dir)) |
---|
| 208 | { |
---|
[3018] | 209 | if (substr($file,0,5) != 'sess_' || !is_readable($path. '/' . $file)) |
---|
[2] | 210 | { |
---|
| 211 | continue; |
---|
| 212 | } |
---|
[3018] | 213 | $session = ''; |
---|
| 214 | if (($fd = fopen ($path . '/' . $file,'r'))) |
---|
| 215 | { |
---|
| 216 | $session = ($size = filesize ($path . '/' . $file)) ? @fread ($fd, $size) : 0; |
---|
| 217 | fclose ($fd); |
---|
| 218 | } |
---|
| 219 | $session = unserialize(substr($session,14)); |
---|
[2] | 220 | $values[$session['session_id']] = $session; |
---|
| 221 | } |
---|
| 222 | closedir($dir); |
---|
| 223 | |
---|
| 224 | if(!$all_no_sort) |
---|
| 225 | { |
---|
| 226 | $GLOBALS['phpgw']->session->sort_by = $sort; |
---|
| 227 | $GLOBALS['phpgw']->session->sort_order = $order; |
---|
| 228 | |
---|
| 229 | uasort($values,array('sessions','session_sort')); |
---|
| 230 | |
---|
| 231 | $i = 0; |
---|
| 232 | $start = (int)$start; |
---|
| 233 | foreach($values as $id => $data) |
---|
| 234 | { |
---|
| 235 | if($i < $start || $i > $start+$maxmatchs) |
---|
| 236 | { |
---|
| 237 | unset($values[$id]); |
---|
| 238 | } |
---|
| 239 | ++$i; |
---|
| 240 | } |
---|
| 241 | reset($values); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | return $values; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /*! |
---|
| 248 | @function total |
---|
| 249 | @abstract get number of normal / non-anonymous sessions |
---|
| 250 | @author ralfbecker |
---|
| 251 | */ |
---|
| 252 | function total() |
---|
| 253 | { |
---|
| 254 | return count($this->list_sessions(0,'','',True)); |
---|
| 255 | } |
---|
| 256 | } |
---|