Advertisement
Guest User

Untitled

a guest
May 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. <?php
  2. require_once("../db/db.php");
  3. Class User
  4. {
  5. //This constructor functions like a signup function.
  6. public function __construct($username, $password)
  7. {
  8. $hashword = sha1($password);
  9. $queryOne = $pdo->prepare("SELECT * FROM users WHERE username = :username");
  10. $result = $queryOne->execute(["username"=>$username]);
  11. if($result > 0) {
  12. throw new Exception("Username already exists");
  13. header("Location: signup.php?usernameexists=1");
  14. } else {
  15. $queryTwo = $pdo->prepare("INSERT INTO users (username,password) VALUES(:username, :password)");
  16. $queryTwo->execute(["username"=>$username,"password"=>$hashword]);
  17. header("Location: index.php?newuser=1");
  18. }
  19. }
  20. public static function DeleteUser($username)
  21. {
  22. $queryOne = $pdo->prepare("DELETE FROM users WHERE username = :username");
  23. $queryOne->execute(["username"=>$username]);
  24. }
  25. public static function EditField($table,$field,$input,$id)
  26. {
  27. $queryOne = $pdo->prepare("SELECT ".$field."FROM ".$table."WHERE id = :".$id);
  28. $result = $queryOne->execute(["field"=>$field,"table"=>$table,"id"=>$id]);
  29. foreach ($resultTrue as $row) {
  30. $value = $row[$field];
  31. }
  32. if($value === null) {
  33. throw new Exception("Field is Null");
  34. header("Location: rip.php");
  35. } else {
  36. $queryTwo = $pdo->prepare("INSERT INTO ".$table."(".$field.") VALUES(:".$field.")");
  37. $queryTwo->execute([$field=>$input]);
  38. }
  39. }
  40. public static function ChangeSetting($setting,$value,$id)
  41. {
  42. $queryOne = $pdo->prepare("UPDATE settings SET ".$setting." = ".$value." WHERE id = :".$id);
  43. $queryOne->execute(["value"=>$value]);
  44. }
  45. public static function CreatePost($content,$title,$id)
  46. {
  47. $queryOne = $pdo->prepare("INSERT INTO Post (id,content,title) VALUES(:id,:content,:title)");
  48. $queryOne->execute(["id"=>$id,"content"=>$content,"title"=>$title]);
  49. }
  50. private static function GetId(){}
  51. public static function DeletePost($id,$postid)
  52. {
  53. $queryOne = $pdo->prepare("SELECT COUNT(*) FROM posts WHERE userid = :userid AND id = :postid");
  54. $result = $queryOne->execute(["userid"=>$userid,"postid"=>$postid]);
  55. if($result->rowCount() === 1)
  56. {
  57. $queryTwo = $pdo->prepare("DELETE FROM Post WHERE id = :postid");
  58. $queryTwo->execute(["postid"=>$postid]);
  59. } else {
  60. throw new Exception("User who is trying to delete post is not user who created post");
  61. }
  62. }
  63. public static function Login($username,$password)
  64. {
  65. $hashword = sha1($password);
  66. $queryOne = $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = :username AND password = :password");
  67. $queryOne->execute(["username"=>$username,"password"=>$hashword]);
  68. $result = $queryOne->fetchColumn();
  69. if($result[0] === 1) {
  70. session_start();
  71. header("Location: menu.php");
  72. } else {
  73. header("Location: login.php?failedlogin=1");
  74. }
  75. }
  76. public static function CreateComment($content,$postid,$userid)
  77. {
  78. $queryOne = $pdo->prepare("INSERT INTO comments (content,userid,postid) VALUES(:content,:userid,:postid)");
  79. $queryOne->execute(["content"=>$content,"userid"=>$userid,"postid"=>$postid]);
  80. }
  81. public static function DeleteComment($id)
  82. {
  83. $queryOne = $pdo->prepare("DELETE FROM comments WHERE id = :id");
  84. $queryOne->execute(["id"=>$id]);
  85. }
  86. public static function Logout()
  87. {
  88. session_destroy();
  89. }
  90. }
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement