-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSessionCookie.php
More file actions
175 lines (154 loc) · 6.03 KB
/
Copy pathSessionCookie.php
File metadata and controls
175 lines (154 loc) · 6.03 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/***
* INTEGRATED FLEET MANAGEMENT SYSTEM
* OBSIDIAN FLEET
* http://www.obsidianfleet.net/ifs/
*
* Developer: Frank Anon
* fanon@obsidianfleet.net
*
* Updated By: Nolan
* john.pbem@gmail.com
*
* Version: 1.16n (Nolan Ed.)
* Release Date: June 3, 2004
* Patch 1.13n: December 2009
* Patch 1.14n: March 2010
* Patch 1.16n: March 2014
*
* Copyright (C) 2003-2004 Frank Anon for Obsidian Fleet RPG
* Distributed under the terms of the GNU General Public License
* See doc/LICENSE for details
*
* This program contains code from Mambo Site Server 4.0.12
* Copyright (C) 2000 - 2002 Miro International Pty Ltd
*
* Date: 5/11/04
* Comments: Checks / sets session information
*
***/
$past = time()-900;
$lifetime= (time() + 60*60*24*365);
// delete outdated sessions
$qry="DELETE FROM {$mpre}session WHERE (time < $past) AND remember='0'";
$database->openConnectionNoReturn($qry);
$qry="UPDATE {$mpre}session SET active='0' WHERE (time < $past) AND remember='1'";
$database->openConnectionNoReturn($qry);
$current_time = time();
$userip = getenv("REMOTE_ADDR");
if ($_COOKIE["session"]=="") // we need to set a cookie?
{
$existSessionID=0;
while ($existSessionID==0)
{
$randnum=getSessionID1();
if ($randnum!="")
{
$cryptrandnum=md5($randnum);
$qry="SELECT session_id FROM {$mpre}session WHERE session_id='$cryptrandnum'";
$result=$database->openConnectionWithReturn($qry);
if (mysql_num_rows($result)==0) // wait until we have a unique session ID
$existSessionID=1;
}
}
if ($existSessionID=1) // set the new cookie, and enter session info in db
{
setcookie("session", "$randnum", $lifetime, "/");
$qry="DELETE FROM {$mpre}session WHERE ip='$userip'";
$database->openConnectionNoReturn($qry);
$qry="INSERT INTO {$mpre}session
SET username='', userid='', time=$current_time, session_id='$cryptrandnum',
guest='1', ip='$userip', active='1'";
$database->openConnectionNoReturn($qry);
}
}
if ($_COOKIE["session"]!="") // make sure we have a cookie
{
$cvar = $_COOKIE["session"];
$sessionCookie=$_COOKIE["session"];
$cryptSessionCookie=md5($_COOKIE["session"]);
$qry = "DELETE FROM {$mpre}session WHERE ip='$userip' AND session_id<>'$cryptSessionCookie'";
$result = $database->openConnectionWithReturn($qry);
if (isset($option) && $option=="logout")
{
$qry="UPDATE {$mpre}session
SET guest=1, username='', userid=null, remember='0'
WHERE session_id='$cryptSessionCookie'";
$database->openConnectionNoReturn($qry);
$option2 = preg_replace("/~/", "&", $option2);
if ($option2)
$option2 = "?" . $option2;
if (substr(dirname($_SERVER['PHP_SELF']), -1) == "/") {
$headerpath = substr(dirname($_SERVER['PHP_SELF']), 0, -1);
} else {
$headerpath = dirname($_SERVER['PHP_SELF']);
}
header("Location: http://" . $_SERVER['HTTP_HOST'] . $headerpath . "/index.php{$option2}");
exit;
}
else
{
$qry = "SELECT username, ip FROM {$mpre}session WHERE session_id='$cryptSessionCookie'";
$result=$database->openConnectionWithReturn($qry);
if (mysql_num_rows($result)> 0) // matching session found in db
{
list ($username, $ip)=mysql_fetch_array($result);
if ($username=="") // no user name -- must be a guest
$guest = 1;
else // if not a guest, then set userlevel
{
$qry = "SELECT u.id, u.flags
FROM {$mpre}session s, {$mpre}users u
WHERE s.userid = u.id AND s.session_id='$cryptSessionCookie'";
$result = $database->openConnectionWithReturn($qry);
list ($uid, $flags) = mysql_fetch_array($result);
if (strstr($flags, 'a') || strstr($flags, 'A'))
$uberadmin = "1";
else
$uberadmin = "0";
$qry = "SELECT flag, admin FROM {$spre}flags ORDER BY flag";
$result = $database->openConnectionWithReturn($qry);
list ($fid, $admin) = mysql_fetch_array($result);
define ("uid", $uid, TRUE);
while ($fid)
{
if ($admin == 1 && !defined("admin"))
define("admin", 1);
if (strstr($flags, strtoupper($fid)) || $uberadmin == "1")
$uflag[$fid] = 2;
elseif (strstr($flags, $fid))
$uflag[$fid] = 1;
else
$uflag[$fid] = 0;
list ($fid, $admin) = mysql_fetch_array($result);
}
}
// don't let the session time-out if it's in use... re-set the cookie, too
$qry="UPDATE {$mpre}session
SET time=$current_time, active='1'
WHERE session_id='$cryptSessionCookie'";
$database->openConnectionNoReturn($qry);
setcookie("session", "$sessionCookie", $lifetime, "/");
}
else // no session found - so let's give them one
{
$qry="INSERT into {$mpre}session
SET username='', userid=null, time=$current_time, session_id='$cryptSessionCookie',
guest='1', ip='$userip'";
$database->openConnectionNoReturn($qry);
}
}
}
function getSessionID1()
{
mt_srand ((double) microtime() * 1000000);
$pass_len = mt_rand (20,40);
$allchar = "abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ0123456789";
$str = "" ;
for ( $i = 0; $i<$pass_len ; $i++ )
$str .= substr( $allchar, mt_rand (0,62), 1 ) ;
$timestamp= time();
$str=$str.$timestamp;
return($str);
}
?>