/** * @class User * @author kovshenin * @version 1.01 * For handling users data and meta * MySQL table structure should definately have `id` and `username`. The users meta table nees `id` chaining the users.id. * Declaring can load a user. For multiple users you can use the UserSet class. * Functions are to load() and loadMeta(). Parameter's either an id or the username. * The user data will be put in $user->data. * You can also change and save that data to the database: $user->save([param]) * And meta too: $user->saveMeta(); (you can use save(true) to save both) */ class User { protected $db; // Ugh, database link protected $tb_users; // The users table, duh protected $tb_meta; // The users meta table, jeez protected $save_valid; // Do we have valid user data? Used for saving and loading meta protected $save_meta_valid; // Valid meta data (same as above) public $data; // This is the data array (containing a "meta" array as the last element) function __construct($db, $query = false, $load_meta = false, $tb_users = "users", $tb_meta = "users_meta") { $this->db = $db; $this->tb_users = $tb_users; $this->tb_meta = $tb_meta; $this->save_valid = false; $this->save_meta_valid = false; if ($id) $this->load($query, $load_meta); } function __destruct() { } // Overloading public function __set($name, $value) { $this->data[$name] = $value; } public function __get($name) { if (array_key_exists($name, $this->data)) { return $this->data[$name]; } $trace = debug_backtrace(); trigger_error( 'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE); return null; } // I don't think there'll be much trouble here. We just load in a user (w/ or w/out meta). $query can be either a username or a user id. function load($query, $load_meta = false) { if (is_numeric($query)) $sql = "SELECT * FROM `{$this->tb_users}` WHERE `id` = {$query} LIMIT 1"; else $sql = "SELECT * FROM `{$this->tb_users}` WHERE `username` = {$query} LIMIT 1"; $rs = mysql_query($sql, $this->db); if (!$rs || @mysql_num_rows($rs) == 0) return false; $this->clear(); $row = mysql_fetch_assoc($rs); $this->data = $row; $this->save_valid = true; if ($load_meta) return $this->loadMeta(); return true; } // Load the meta for the current user function loadMeta() { if ($this->save_valid) { $sql = "SELECT * FROM `{$this->tb_meta}` WHERE `id` = {$this->data["id"]} LIMIT 1"; $rs = mysql_query($sql, $this->db); if (!$rs || @mysql_num_rows($rs) == 0) return false; $row = mysql_fetch_assoc($rs); foreach ($row as $key => $value) $this->data["meta_$key"] = $value; $this->save_meta_valid = true; return true; } else return false; } function save($save_meta = false) { if ($this->save_valid) { $id = $this->data["id"]; $query = ""; foreach($this->data as $key => $value) { if (substr($key, 0, 5) != "meta_" && $key != "id") { if (!is_numeric($value)) $value = "'{$value}'"; $query .= "`{$key}` = {$value}, "; } } $query = rtrim($query, " ,"); $sql = "UPDATE `{$this->tb_users}` SET {$query} WHERE `id` = {$id} LIMIT 1"; $return = mysql_query($sql, $this->db); if ($save_meta) return $this->saveMeta(); else return $return; } return false; } function saveMeta() { if ($this->save_meta_valid) { $id = $this->data["id"]; $query = ""; foreach($this->data as $key => $value) { if ($key != "id" && substr($key, 0,5) == "meta_") { $key = substr($key, 5); if (!is_numeric($value)) $value = "'{$value}'"; $query .= "`{$key}` = {$value}, "; } } $query = rtrim($query, " ,"); $sql = "UPDATE `{$this->tb_meta}` SET {$query} WHERE `id` = {$id} LIMIT 1"; return mysql_query($sql, $this->db); } return false; } function create($username, $load_meta = false) { $sql = "SELECT `id` FROM `{$this->tb_users}` WHERE `username` = '{$username}'"; $rs = mysql_query($sql, $this->db); if (mysql_num_rows($rs) > 0) return false; $sql = "INSERT INTO `{$this->tb_users}` (`username`) VALUES ('$username')"; if (!mysql_query($sql, $this->db)) return false; $id = mysql_insert_id($this->db); $sql = "INSERT INTO `{$this->tb_meta}` (`id`) VALUES ($id)"; if (!mysql_query($sql, $this->db)) return false; return $this->load($id, $load_meta); } function validate() { $this->save_valid = true; $this->save_meta_valid = true; } function clear() { $this->data = array(); $this->save_meta_valid = $this->save_valid = false; } }