Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once("../db/db.php");
- Class User
- {
- //This constructor functions like a signup function.
- public function __construct($username, $password)
- {
- $hashword = sha1($password);
- $queryOne = $pdo->prepare("SELECT * FROM users WHERE username = :username");
- $result = $queryOne->execute(["username"=>$username]);
- if($result > 0) {
- throw new Exception("Username already exists");
- header("Location: signup.php?usernameexists=1");
- } else {
- $queryTwo = $pdo->prepare("INSERT INTO users (username,password) VALUES(:username, :password)");
- $queryTwo->execute(["username"=>$username,"password"=>$hashword]);
- header("Location: index.php?newuser=1");
- }
- }
- public static function DeleteUser($username)
- {
- $queryOne = $pdo->prepare("DELETE FROM users WHERE username = :username");
- $queryOne->execute(["username"=>$username]);
- }
- public static function EditField($table,$field,$input,$id)
- {
- $queryOne = $pdo->prepare("SELECT ".$field."FROM ".$table."WHERE id = :".$id);
- $result = $queryOne->execute(["field"=>$field,"table"=>$table,"id"=>$id]);
- foreach ($resultTrue as $row) {
- $value = $row[$field];
- }
- if($value === null) {
- throw new Exception("Field is Null");
- header("Location: rip.php");
- } else {
- $queryTwo = $pdo->prepare("INSERT INTO ".$table."(".$field.") VALUES(:".$field.")");
- $queryTwo->execute([$field=>$input]);
- }
- }
- public static function ChangeSetting($setting,$value,$id)
- {
- $queryOne = $pdo->prepare("UPDATE settings SET ".$setting." = ".$value." WHERE id = :".$id);
- $queryOne->execute(["value"=>$value]);
- }
- public static function CreatePost($content,$title,$id)
- {
- $queryOne = $pdo->prepare("INSERT INTO Post (id,content,title) VALUES(:id,:content,:title)");
- $queryOne->execute(["id"=>$id,"content"=>$content,"title"=>$title]);
- }
- private static function GetId(){}
- public static function DeletePost($id,$postid)
- {
- $queryOne = $pdo->prepare("SELECT COUNT(*) FROM posts WHERE userid = :userid AND id = :postid");
- $result = $queryOne->execute(["userid"=>$userid,"postid"=>$postid]);
- if($result->rowCount() === 1)
- {
- $queryTwo = $pdo->prepare("DELETE FROM Post WHERE id = :postid");
- $queryTwo->execute(["postid"=>$postid]);
- } else {
- throw new Exception("User who is trying to delete post is not user who created post");
- }
- }
- public static function Login($username,$password)
- {
- $hashword = sha1($password);
- $queryOne = $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = :username AND password = :password");
- $queryOne->execute(["username"=>$username,"password"=>$hashword]);
- $result = $queryOne->fetchColumn();
- if($result[0] === 1) {
- session_start();
- header("Location: menu.php");
- } else {
- header("Location: login.php?failedlogin=1");
- }
- }
- public static function CreateComment($content,$postid,$userid)
- {
- $queryOne = $pdo->prepare("INSERT INTO comments (content,userid,postid) VALUES(:content,:userid,:postid)");
- $queryOne->execute(["content"=>$content,"userid"=>$userid,"postid"=>$postid]);
- }
- public static function DeleteComment($id)
- {
- $queryOne = $pdo->prepare("DELETE FROM comments WHERE id = :id");
- $queryOne->execute(["id"=>$id]);
- }
- public static function Logout()
- {
- session_destroy();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement