|
Added
Link Here
|
| 1 |
<?php |
| 2 |
/******************************************************************************* |
| 3 |
* Copyright (c) 2007 Eclipse Foundation and others. |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* http://www.eclipse.org/legal/epl-v10.html |
| 8 |
* |
| 9 |
* Contributors: |
| 10 |
* Denis Roy (Eclipse Foundation)- initial API and implementation |
| 11 |
*******************************************************************************/ |
| 12 |
|
| 13 |
define('ECLIPSE_SESSION', 'ECLIPSESESSION'); |
| 14 |
|
| 15 |
require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/classes/friends/friend.class.php"); |
| 16 |
require_once("/home/data/httpd/eclipse-php-classes/system/dbconnection_rw.class.php"); |
| 17 |
require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/app.class.php"); |
| 18 |
require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/evt_log.class.php"); |
| 19 |
|
| 20 |
class Session { |
| 21 |
|
| 22 |
private $gid = ""; |
| 23 |
private $bugzilla_id= 0; |
| 24 |
private $subnet = ""; |
| 25 |
private $updated_at = ""; |
| 26 |
private $is_persistent = 0; |
| 27 |
private $Friend = null; |
| 28 |
private $data = ""; |
| 29 |
|
| 30 |
/** |
| 31 |
* Default constructor |
| 32 |
* |
| 33 |
* @return null |
| 34 |
*/ |
| 35 |
function Session($persistent=0) { |
| 36 |
$this->setIsPersistent($persistent); |
| 37 |
$this->validate(); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
function getGID() { |
| 42 |
return $this->gid; |
| 43 |
} |
| 44 |
function getBugzillaID() { |
| 45 |
return $this->bugzilla_id; |
| 46 |
} |
| 47 |
function getSubnet() { |
| 48 |
return $this->subnet; |
| 49 |
} |
| 50 |
function getUpdatedAt() { |
| 51 |
return $this->updated_at; |
| 52 |
} |
| 53 |
function getFriend() { |
| 54 |
return $this->Friend; |
| 55 |
} |
| 56 |
function getData() { |
| 57 |
return unserialize($this->data); |
| 58 |
} |
| 59 |
function getIsPersistent() { |
| 60 |
return $this->is_persistent == null ? 0 : $this->is_persistent; |
| 61 |
} |
| 62 |
|
| 63 |
function setGID($_gid) { |
| 64 |
$this->gid = $_gid; |
| 65 |
} |
| 66 |
function setBugzillaID($_bugzilla_id) { |
| 67 |
$this->bugzilla_id = $_bugzilla_id; |
| 68 |
} |
| 69 |
function setSubnet($_subnet) { |
| 70 |
$this->subnet = $_subnet; |
| 71 |
} |
| 72 |
function setUpdatedAt($_updated_at) { |
| 73 |
$this->updated_at = $_updated_at; |
| 74 |
} |
| 75 |
function setFriend($_friend) { |
| 76 |
$this->Friend = $_friend; |
| 77 |
} |
| 78 |
function setData($_data) { |
| 79 |
$this->data = serialize($_data); |
| 80 |
} |
| 81 |
function setIsPersistent($_is_persistent) { |
| 82 |
$this->is_persistent = $_is_persistent; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/** |
| 87 |
* Validate session based on browser cookie |
| 88 |
* |
| 89 |
* @return boolean |
| 90 |
*/ |
| 91 |
function validate() { |
| 92 |
$cookie = (isset($_COOKIE[ECLIPSE_SESSION]) ? $_COOKIE[ECLIPSE_SESSION] : ""); |
| 93 |
$rValue = false; |
| 94 |
if ( (!$this->load($cookie))) { |
| 95 |
# Failed - no such session, or session no match. Need to relogin |
| 96 |
setcookie(ECLIPSE_SESSION, "", -36000, "/", "eclipse.org"); |
| 97 |
$rValue = false; |
| 98 |
} |
| 99 |
else { |
| 100 |
# TODO: update session? |
| 101 |
$rValue = true; |
| 102 |
$this->maintenance(); |
| 103 |
$this->setFriend($this->getData()); |
| 104 |
} |
| 105 |
return $rValue; |
| 106 |
} |
| 107 |
|
| 108 |
function destroy() { |
| 109 |
if($this->getBugzillaID() != 0) { |
| 110 |
$sql = "DELETE FROM sessions WHERE bugzilla_id = " . $this->getBugzillaID(); |
| 111 |
$dbc = new DBConnectionRW(); |
| 112 |
$dbh = $dbc->connect(); |
| 113 |
mysql_query($sql, $dbh); |
| 114 |
$dbc->disconnect(); |
| 115 |
setcookie(ECLIPSE_SESSION, "", -36000, "/", "eclipse.org"); |
| 116 |
|
| 117 |
# Log this event |
| 118 |
$EvtLog = new EvtLog(); |
| 119 |
$EvtLog->setLogTable("sessions"); |
| 120 |
$EvtLog->setPK1($this->getBugzillaID()); |
| 121 |
$EvtLog->setPK2($_SERVER['REMOTE_ADDR']); |
| 122 |
$EvtLog->setLogAction("DELETE"); |
| 123 |
$EvtLog->insertModLog("apache"); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
function create() { |
| 128 |
# create session on the database |
| 129 |
$Friend = $this->getFriend(); |
| 130 |
$this->setData($Friend); |
| 131 |
|
| 132 |
# need to have a bugzilla ID to log in |
| 133 |
if($Friend->getBugzillaID() > 0) { |
| 134 |
$App = new App(); |
| 135 |
$this->setGID(md5(uniqid(rand(),true))); |
| 136 |
$this->setSubnet($this->getClientSubnet()); |
| 137 |
$this->setUpdatedAt($App->getCURDATE()); |
| 138 |
$this->setBugzillaID($Friend->getBugzillaID()); |
| 139 |
|
| 140 |
$dbc = new DBConnectionRW(); |
| 141 |
$dbh = $dbc->connect(); |
| 142 |
|
| 143 |
$sql = "INSERT INTO sessions ( |
| 144 |
gid, |
| 145 |
bugzilla_id, |
| 146 |
subnet, |
| 147 |
updated_at, |
| 148 |
data, |
| 149 |
is_persistent) |
| 150 |
VALUES ( |
| 151 |
" . $App->returnQuotedString($this->getGID()) . ", |
| 152 |
" . $App->sqlSanitize($Friend->getBugzillaID() ,$dbh) . ", |
| 153 |
" . $App->returnQuotedString($this->getSubnet()) . ", |
| 154 |
NOW(), |
| 155 |
'" . $App->returnJSSAfeString($this->data) . "', |
| 156 |
'" . $App->sqlSanitize($this->getIsPersistent(),$dbh) . "')"; |
| 157 |
|
| 158 |
mysql_query($sql, $dbh); |
| 159 |
$dbc->disconnect(); |
| 160 |
|
| 161 |
|
| 162 |
# Log this event |
| 163 |
$EvtLog = new EvtLog(); |
| 164 |
$EvtLog->setLogTable("sessions"); |
| 165 |
$EvtLog->setPK1($this->getBugzillaID()); |
| 166 |
$EvtLog->setPK2($_SERVER['REMOTE_ADDR']); |
| 167 |
$EvtLog->setLogAction("INSERT"); |
| 168 |
$EvtLog->insertModLog("apache"); |
| 169 |
|
| 170 |
|
| 171 |
$cookie_time = 0; |
| 172 |
if($this->getIsPersistent()) { |
| 173 |
$cookie_time = time()+3600*24*365; |
| 174 |
} |
| 175 |
setcookie(ECLIPSE_SESSION, $this->getGID(), $cookie_time, "/", "eclipse.org"); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
function load($_gid) { |
| 180 |
# need to have a bugzilla ID to log in |
| 181 |
|
| 182 |
$rValue = false; |
| 183 |
if($_gid != "") { |
| 184 |
$dbc = new DBConnectionRW(); |
| 185 |
$dbh = $dbc->connect(); |
| 186 |
|
| 187 |
$App = new App(); |
| 188 |
$sql = "SELECT gid, |
| 189 |
bugzilla_id, |
| 190 |
subnet, |
| 191 |
updated_at, |
| 192 |
data, |
| 193 |
is_persistent |
| 194 |
FROM sessions |
| 195 |
WHERE gid = " . $App->sqlSanitize($App->returnQuotedString($_gid),$dbh) . " |
| 196 |
AND subnet = " . $App->returnQuotedString($this->getClientSubnet()); |
| 197 |
|
| 198 |
$result = mysql_query($sql, $dbh); |
| 199 |
if($result && mysql_num_rows($result) > 0) { |
| 200 |
$rValue = true; |
| 201 |
$myrow = mysql_fetch_assoc($result); |
| 202 |
$this->setGID($_gid); |
| 203 |
$this->setBugzillaID($myrow['bugzilla_id']); |
| 204 |
$this->setSubnet($myrow['subnet']); |
| 205 |
$this->setUpdatedAt($myrow['updated_at']); |
| 206 |
$this->data = $myrow['data']; |
| 207 |
$this->setIsPersistent($myrow['is_persistent']); |
| 208 |
} |
| 209 |
$dbc->disconnect(); |
| 210 |
} |
| 211 |
return $rValue; |
| 212 |
} |
| 213 |
|
| 214 |
function maintenance() { |
| 215 |
$dbc = new DBConnectionRW(); |
| 216 |
$dbh = $dbc->connect(); |
| 217 |
|
| 218 |
$sql = "DELETE FROM sessions |
| 219 |
WHERE (updated_at < DATE_SUB(NOW(), INTERVAL 1 DAY) AND is_persistent = 0) |
| 220 |
OR (subnet = '" . $this->getClientSubnet() . "' AND gid <> '" . $App->sqlSanitize($this->getGID(), $dbh) . "')"; |
| 221 |
|
| 222 |
mysql_query($sql, $dbh); |
| 223 |
|
| 224 |
$dbc->disconnect(); |
| 225 |
} |
| 226 |
|
| 227 |
function getClientSubnet() { |
| 228 |
# return class-c subnet |
| 229 |
return substr($_SERVER['REMOTE_ADDR'], 0, strrpos($_SERVER['REMOTE_ADDR'], ".")) . ".0"; |
| 230 |
} |
| 231 |
} |
| 232 |
?> |