class User {
function User($dbi = null, $uid = null) {
global $AUTH_DB_TBL,
$MIN_USERNAME_SIZE,
$MIN_PASSWORD_SIZE,
$ACTIVITY_LOG_TBL;
$this->user_tbl = $AUTH_DB_TBL;
$this->user_activity_log = $ACTIVITY_LOG_TBL;
$this->dbi = $dbi;
//print_r($this->dbi);
$this->minmum_username_size = $MIN_USERNAME_SIZE;
$this->minmum_pasword_size = $MIN_PASSWORD_SIZE;
$this->USER_ID = $uid;
//$this->debugger = $debugger;
$this->user_tbl_fields = array(‘EMAIL’ => ‘text’,
‘PASSWORD’ => ‘text’,
‘TYPE’ => ‘number’,
‘ACTIVE’ => ‘number’
);
if (isset($this->USER_ID)) {
$this->is_user = $this->getUserInfo();
} else {
$this->is_user = FALSE;
} }
Continued
Trang 2Listing 6-1 (Continued)
function isUser() {
return $this->is_user;
}
function getUserID() {
return $this->USER_ID;
}
function setUserID($uid = null) {
if (! empty($uid)) {
$this->USER_ID = $uid;
}
return $this->USER_ID;
}
function getUserIDByName($name = null) {
if (! $name ) return null;
$stmt = “SELECT USER_ID FROM $this->user_tbl WHERE EMAIL = ‘$name’”;
$result = $this->dbi->query($stmt);
if ($result != null) {
$row = $result->fetchRow();
return $row->USER_ID;
}
return null;
}
function getUserTypeList() {
global $USER_TYPE;
Trang 3function getUID() {
return (isset($this->USER_ID)) ? $this->USER_ID : NULL;
}
function getEMAIL() {
return (isset($this->EMAIL)) ? $this->EMAIL : NULL;
}
function getPASSWORD() {
return (isset($this->PASSWORD)) ? $this->PASSWORD : NULL;
}
function getACTIVE() {
return (isset($this->ACTIVE)) ? $this->ACTIVE : NULL;
}
function getTYPE() {
return (isset($this->TYPE)) ? $this->TYPE : NULL;
}
function getUserFieldList() {
return array(‘USER_ID’, ‘EMAIL’, ‘PASSWORD’, ‘ACTIVE’, ‘TYPE’);
}
function getUserInfo($uid = null) {
$fields = $this->getUserFieldList();
$fieldStr = implode(‘,’, $fields);
$this->setUserID($uid);
$stmt = “SELECT $fieldStr FROM $this->user_tbl “
“WHERE USER_ID = $this->USER_ID”;
//echo “$stmt <P>”;
Continued
Trang 4Listing 6-1 (Continued)
$result = $this->dbi->query($stmt);
if ($result->numRows() > 0) {
$row = $result->fetchRow();
foreach($fields as $f) {
$this->$f = $row->$f;
}
return TRUE;
}
return FALSE;
}
function getUserIDbyEmail($email = null) // needed for EIS {
$stmt = “SELECT USER_ID FROM $this->user_tbl “
“WHERE EMAIL = ‘$email’”;
$result = $this->dbi->query($stmt);
if($result->numRows() > 0) {
$row = $result->fetchRow();
return $row->USER_ID;
} else {
return 0;
} }
function getUserList() {
Trang 5$result = $this->dbi->query($stmt);
$retArray = array();
if ($result != null) {
while($row = $result->fetchRow()) {
$retArray[$row->USER_ID] = $row->EMAIL;
} }
return $retArray;
}
function makeUpdateKeyValuePairs($fields = null, $data = null) {
$setValues = array();
while(list($k, $v) = each($fields)) {
if (isset($data[$k])) {
//echo “DATA $k = $data[$k] <br>”;
if (! strcmp($v, ‘text’)) {
$v = $this->dbi->quote(addslashes($data[$k]));
$setValues[] = “$k = $v”;
} else {
$setValues[] = “$k = $data[$k]”;
} } }
Continued