Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

User.class.php 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. require_once('Role.class.php');
  3. class User {
  4. public $id;
  5. public $username;
  6. public $hashedPassword;
  7. public $group;
  8. public $admin;
  9. public $join_date;
  10. public $last_seen;
  11. public $theme;
  12. public $public_key;
  13. public $profile_image;
  14. public $website;
  15. public $about;
  16. public $blog_title;
  17. public $blog_desc;
  18. public $roles;
  19. //Constructor is called whenever a new object is created.
  20. //Takes an associative array with the DB row as an argument.
  21. function __construct($data, $db) {
  22. $this->id = (isset($data['id'])) ? $data['id'] : "";
  23. $this->username = (isset($data['username'])) ? $data['username'] : "";
  24. $this->hashedPassword = (isset($data['password'])) ? $data['password'] : "";
  25. $this->group = (isset($data['group_name'])) ? $data['group_name'] : "";
  26. $this->admin = (isset($data['site_admin'])) ? (bool) $data['site_admin'] : "";
  27. $this->join_date = (isset($data['join_date'])) ? $data['join_date'] : "";
  28. $this->last_seen = (isset($data['last_seen'])) ? $data['last_seen'] : "";
  29. $this->theme = (isset($data['theme'])) ? $data['theme'] : "";
  30. $this->public_key = (isset($data['public_key'])) ? $data['public_key'] : "";
  31. $this->minecraft_user = (isset($data['minecraft_user'])) ? $data['minecraft_user'] : "";
  32. $this->website = (isset($data['website'])) ? $data['website'] : "";
  33. $this->about = (isset($data['about'])) ? $data['about'] : "";
  34. $this->quote = (isset($data['quote'])) ? $data['quote'] : "";
  35. $this->blog_title = (isset($data['blog_title'])) ? $data['blog_title'] : "";
  36. $this->blog_desc = (isset($data['blog_desc'])) ? $data['blog_desc'] : "";
  37. $this->roles = array();
  38. $results = $db->select("user_role as ur JOIN roles as r ON ur.role_id = r.role_id", "WHERE ur.user_id=?", array($this->id), "ur.role_id, r.role_name");
  39. $users = array();
  40. foreach ($results as $result)
  41. {
  42. $this->roles[$result["role_name"]] = Role::getRolePerms($result["role_id"], $db);
  43. }
  44. }
  45. public function save($db, $isNewUser = false) {
  46. //if the user is already registered and we're
  47. //just updating their info.
  48. if(!$isNewUser) {
  49. //set the data array
  50. $data = array(
  51. "username" => $this->username,
  52. "password" => $this->hashedPassword,
  53. "last_seen" => date("Y-m-d H:i:s",time()),
  54. "theme" => $this->theme,
  55. "public_key" => $this->public_key,
  56. "minecraft_user" => $this->minecraft_user,
  57. "website" => $this->website,
  58. "quote" => $this->quote,
  59. "about" => $this->about,
  60. "blog_title" => $this->blog_title,
  61. "blog_desc" => $this->blog_desc
  62. );
  63. //update the row in the database
  64. $db->update($data, 'users', 'id=?', array($this->id));
  65. }else {
  66. //if the user is being registered for the first time.
  67. $data = array(
  68. "username" => $this->username,
  69. "password" => $this->hashedPassword,
  70. "join_date" => date("Y-m-d H:i:s",time()),
  71. "last_seen" => date("Y-m-d H:i:s",time())
  72. );
  73. $this->id = $db->insert($data, 'users');
  74. $this->join_date = time();
  75. }
  76. return true;
  77. }
  78. public function delete($db) {
  79. //if the user is already registered and we're
  80. //just updating their info.
  81. //if the user is being registered for the first time.
  82. $db->delete('users', 'id=?', array($this->id));
  83. return true;
  84. }
  85. }
  86. ?>