From f08eb1640388e1f067102a22ec40c30f263d92c6 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Thu, 2 Jun 2011 16:17:25 -0700 Subject: Added user management --- htdocs/src/Post.inc.php | 3 ++ htdocs/src/Source.inc.php | 78 ++++++++++++++++++++++++++++++++++++++ htdocs/src/User.inc.php | 80 ++++++++++++++++++++++++++++++++++++++- htdocs/src/UserIterator.inc.php | 83 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 htdocs/src/Source.inc.php create mode 100644 htdocs/src/UserIterator.inc.php (limited to 'htdocs/src') diff --git a/htdocs/src/Post.inc.php b/htdocs/src/Post.inc.php index 4a109d2..c29b5a9 100644 --- a/htdocs/src/Post.inc.php +++ b/htdocs/src/Post.inc.php @@ -98,6 +98,7 @@ class Post { $this->info['id'] = $ret; $this->info['stage'] = 'verification'; $this->info['secretid'] = $info['secretid']; + $this->indatabase = true; } return true; @@ -116,6 +117,8 @@ class Post { // Delete Post $db->delete('post', 'id=' . $this->getId()); + + $this->indatabase = false; } public function getId() { diff --git a/htdocs/src/Source.inc.php b/htdocs/src/Source.inc.php new file mode 100644 index 0000000..e869d20 --- /dev/null +++ b/htdocs/src/Source.inc.php @@ -0,0 +1,78 @@ + + * + */ + +require_once "base.inc.php"; + +class Source { + private $info; + + + public function __construct($info=null) { + $this->info = $info; + } + + public static function getSources() { + $db = getDatabase(); + + $query = "SELECT * FROM source ORDER BY name"; + + $rows = $db->fetchAssocRows($query); + + $result = array(); + foreach ($rows as $row) { + $source = new Source($row); + $result[] = $source; + } + + return $result; + } + + public static function getById($id) { + $where = "id='$id'"; + + return Source::getSource($where); + } + + private static function getSource($where) { + $query = "SELECT * FROM source WHERE $where"; + + $db = getDatabase(); + + $row = $db->fetchAssocRow($query); + + if ($row) { + $source = new Source(); + $source->info = $row; + + return $source; + + } else { + return false; + } + } + + public function save() { + $db = getDatabase(); + + // TODO: Implement Save + } + + public function getId() { + return $this->info['id']; + } + + public function getName() { + return htmlspecialchars($this->info['name']); + } +} + +?> + + diff --git a/htdocs/src/User.inc.php b/htdocs/src/User.inc.php index 6ad5ebb..0333520 100644 --- a/htdocs/src/User.inc.php +++ b/htdocs/src/User.inc.php @@ -12,8 +12,19 @@ require_once "base.inc.php"; class User { private $info; + private $indatabase; + public function __construct($info=null) { + $this->info = is_null($info) ? array() : $info; + if ($info !== null and isset($info['id'])) { + $this->indatabase = true; + + } else { + $this->indatabase = false; + } + } + public static function getById($id) { $where = "id='$id'"; @@ -36,6 +47,7 @@ class User { if ($row) { $user = new User(); $user->info = $row; + $user->indatabase = true; return $user; @@ -47,7 +59,45 @@ class User { public function save() { $db = getDatabase(); - // TODO: Implement save + // Cleanup Info + foreach ($this->info as $key=>$value) + $info[$key] = addslashes($value); + + // Save or create? + if ($this->indatabase) { + try { + $db->update('user', $info, "WHERE `id`='" + . $this->getId() ."'"); + return true; + + } catch (Cif_Database_Exception $e) { + return false; + } + + } else { + // Creating... set special fields. + try { + $ret = $db->insert('user', $info); + + if ($ret) { + $this->info['id'] = $ret; + $this->indatabase = true; + } + + return true; + + } catch (Cif_Database_Exception $e) { + return false; + } + } + } + + public function delete() { + $db = getDatabase(); + + $db->delete('user', 'id=' . $this->getId()); + + $this->indatabase = false; } public function getId() { @@ -58,10 +108,34 @@ class User { return $this->info['name']; } + public function setName($value) { + $this->info['name'] = $value; + } + public function getEmail() { return $this->info['email']; } + public function setEmail($value) { + $this->info['email'] = $value; + } + + public function getNotify() { + return $this->info['notify']; + } + + public function setNotify($value) { + $this->info['notify'] = $value ? 1 : 0; + } + + public function getSource() { + return $this->info['source_id']; + } + + public function setSource($value) { + $this->info['source_id'] = $value; + } + public function setPassword($password) { $this->info['password'] = sha1($password); } @@ -73,6 +147,10 @@ class User { public function isAdmin() { return $this->info['admin'] == 1; } + + public function setAdmin($value) { + $this->info['admin'] = $value ? 1 : 0; + } } ?> diff --git a/htdocs/src/UserIterator.inc.php b/htdocs/src/UserIterator.inc.php new file mode 100644 index 0000000..2f8fef2 --- /dev/null +++ b/htdocs/src/UserIterator.inc.php @@ -0,0 +1,83 @@ + + * + */ + +require_once "base.inc.php"; + +class UserIterator implements Iterator { + private $where; + private $rows; + private $position; + private $order; + private $limit; + + public function __construct() { + $this->where = array(); + $this->rows = array(); + $this->position = 0; + $this->order = "name asc"; + $this->limit = 0; + } + + public function filterNotify($notify) { + $sqlnotify = $notify ? '1' : '0'; + $this->where[] = "notify='$sqlnotify'"; + } + + public function orderBy($order) { + $this->order = $order; + } + + public function limit($limit) { + $this->limit = $limit; + } + + public function rewind() { + $this->position = 0; + } + + public function current() { + return new User($this->rows[$this->position]); + } + + public function key() { + return $this->rows[$this->position]['id']; + } + + public function next() { + ++$this->position; + } + + public function valid() { + return isset($this->rows[$this->position]); + } + + public function query() { + $query = "SELECT * FROM user"; + + if (count($this->where) > 0) { + $where = join(' AND ', $this->where); + $query .= " WHERE $where"; + } + + $query .= " ORDER BY ". $this->order; + + if ($this->limit != 0) { + $query .= " LIMIT ". $this->limit; + } + + $db = getDatabase(); + + $this->rows = $db->fetchAssocRows($query); + $this->position = 0; + } +} + +?> + -- cgit v1.2.3