Mysql($authDB); $this->data = $data; } // Is this instance valid? function isValid() { if ($this->data["id"] <> "" && $this->data["password"] <> "" && $this->data["email"] <> "" && $this->data["user_type"] <> "" && $this->data["gecos"] <> "") { return true; } else { return false; } } // Access to member variables function setID($i) { $this->data["id"] = $i; } function getID() { return $this->data["id"]; } function setGecos($g) { $this->data["gecos"] = $g; } function getGecos() { return $this->data["gecos"]; } function setPassword($p) { $this->data["password"] = $p; } function getPassword() { return $this->data["password"]; } function setUserType($t) { $this->data["user_type"] = $t; } function getUserType() { return $this->data["user_type"]; } function setEmail($e) { $this->data["email"] = $e; } function getEmail() { return $this->data["email"]; } function getCookie() { return $this->data["cookie"]; } // Generic function to see if a date has expired function isExpired($date) { return false; } function isCookieExpired() { return $this->isExpired($this->data["cookie_expire_date"]); } function isPwExpired() { return $this->isExpired($this->data["pw_expire_date"]); } // Make a set a random cookie function makeCookie() { global $cookieLen; global $authDB, $authUsers; $cookie = ""; srand((double) microtime() * 1000000); for ($i = 0; $i < $cookieLen; $i++) { $v = rand(0, 26 * 2 + 10); if ($v > (26 + 10)) $v += ord("a") - (26 + 10) - 1; elseif ($v > 10) $v += ord("A") - 10 - 1; else $v += ord("0"); $cookie .= chr($v); } $this->data["cookie"] = $cookie; $this->data["cookie_expire_date"] = date("d-M-Y", time() + (60 * 60 * 24)); // Write this data back to the database, if the record is valid if ($this->data["id"] <> "") { global $db_server; global $db_username; global $db_password; $link = mysql_connect($db_server, $db_username, $db_password); if (mysql_select_db($authDB, $link)) { $ced = $this->data["cookie_expire_date"]; $id = $this->data["id"]; $q = "UPDATE $authUsers SET cookie = '$cookie', cookie_expire_date = '$ced' WHERE id = '$id'"; mysql_query($q); } } } // Clear cookie and save record function clearCookie($c) { $this->data["cookie"] = ""; $this->save(); } // Save or create a user (create if the user does not already exist) function save() { global $authUsers; global $authDB; if (!$this->isValid()) return; // Determine if we need to save or create // Crude, but effective $u = lookupUser($this->data["id"]); $fields[] = "gecos"; $fields[] = "email"; $fields[] = "password"; $fields[] = "pw_expire_date"; $fields[] = "cookie"; $fields[] = "cookie_expire_date"; if ($u->isValid()) { $query = "UPDATE $authUsers SET version = 1, user_type = " . $this->getUserType(); while (list($key, $value) = each($fields)) { $query .= ", $value = '" . $this->data["$value"] . "'"; } $query .= " WHERE id = '" . $this->data["id"] . "'"; } else { $query = "INSERT INTO $authUsers (version, id, user_type"; reset($fields); while (list($key, $value) = each($fields)) $query .= ", $value"; $query .= ") VALUES (1, '" . $this->getID() . "', " . $this->getUserType(); reset($fields); while (list($key, $value) = each($fields)) $query .= ", '" . $this->data["$value"] . "'"; $query .= ")"; } global $db_server; global $db_username; global $db_password; $link = mysql_connect($db_server, $db_username, $db_password); mysql_select_db($authDB, $link); $result = mysql_query($cmd . $query . $where, $link); if ($result == 0) { $foo = mysql_error(); print("

WARNING:MySQL didn't save user properly : $foo\n"); } } } // Global variable for the current user $theUser = new User_lsc(""); // Function to sort users function user_sort($a, $b) { return ($a->getID() < $b->getID()) ? -1 : (($a->getID() > $b->getID()) ? 1 : 0); } // Function to do a generic query and return an array of Users function doUserQuery($q, $want_rows) { global $authDB; $m = new Mysql($authDB); $users = $m->do_query($q, $want_rows); if ($users[0]->isValid()) { for ($i = 0; $i < sizeof($users); $i++) $ret[] = new User_lsc($users[$i]->getData()); usort($ret, user_sort); } else $ret[] = new User_lsc(""); return $ret; } // Lookup a user record by id function lookupUser($name) { global $authUsers; $foo = doUserQuery("SELECT * FROM $authUsers WHERE id = '$name'", 1); return $foo[0]; } // Delete a user record by id function deleteUser($name) { global $authUsers; doUserQuery("DELETE FROM $authUsers WHERE id = '$name'", 0); } // Lookup a user record by cookie function lookupCookie($cookie) { global $authUsers; $foo = doUserQuery("SELECT * FROM $authUsers WHERE cookie = '$cookie'", 1); return $foo[0]; } // Get all users function getAllUsers() { global $authUsers; $foo = doUserQuery("SELECT * FROM $authUsers", 1); return $foo; } }