Pushing all files from my usb
This commit is contained in:
35
lib/classes/Account.php
Normal file
35
lib/classes/Account.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
class Account {
|
||||
private $current_user;
|
||||
private $conn;
|
||||
|
||||
public function __construct($current_user, $conn) {
|
||||
$this->current_user = $current_user;
|
||||
$this->conn = $conn;
|
||||
}
|
||||
|
||||
public function isLoggedIn() {
|
||||
return isset($_SESSION["user"]) && $_SESSION["user"] == $this->current_user;
|
||||
}
|
||||
|
||||
public function accountExists() {
|
||||
$stmt = $this->conn->prepare("SELECT username FROM accounts WHERE username = :username");
|
||||
$stmt->bindParam(":username", $this->current_user);
|
||||
$stmt->execute();
|
||||
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
|
||||
public function getDetails($info) {
|
||||
if (!$this->accountExists() || !$this->isLoggedIn())
|
||||
return NULL;
|
||||
|
||||
$stmt = $this->conn->prepare("SELECT * FROM accounts WHERE username = :username");
|
||||
$stmt->bindParam(":username", $this->current_user);
|
||||
$stmt->execute();
|
||||
|
||||
$current_user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
return $current_user[$info] ?? NULL;
|
||||
}
|
||||
}
|
||||
?>
|
||||
52
lib/classes/Login.php
Normal file
52
lib/classes/Login.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
class Login {
|
||||
private $username;
|
||||
private $password;
|
||||
private $conn;
|
||||
public $error;
|
||||
|
||||
public function __construct($username, $password, $conn) {
|
||||
$this->username = trim($username);
|
||||
$this->password = trim($password);
|
||||
$this->conn = $conn;
|
||||
}
|
||||
|
||||
public function checkUsername() {
|
||||
if (empty($this->username)) {
|
||||
$this->error = "Please fill out all of the fields.";
|
||||
} else {
|
||||
$stmt = $this->conn->prepare("SELECT username FROM accounts WHERE username = :username");
|
||||
$stmt->bindParam(":username", $this->username);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() == 0)
|
||||
$this->error = "Incorrect username or password.";
|
||||
}
|
||||
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
public function checkPassword() {
|
||||
if (empty($this->password)) {
|
||||
$this->error = "Please fill out all of the fields.";
|
||||
} else {
|
||||
$stmt = $this->conn->prepare("SELECT password FROM accounts WHERE username = :username");
|
||||
$stmt->bindParam(":username", $this->username);
|
||||
$stmt->execute();
|
||||
|
||||
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($user == NULL || !password_verify($this->password, $user["password"]))
|
||||
$this->error = "Incorrect username or password.";
|
||||
}
|
||||
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
public function authUser() {
|
||||
session_regenerate_id(true);
|
||||
$_SESSION['user'] = $this->username;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
||||
87
lib/classes/Post.php
Normal file
87
lib/classes/Post.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
class Post extends Account {
|
||||
private $current_user;
|
||||
private $title;
|
||||
private $content;
|
||||
|
||||
private $conn;
|
||||
public $error;
|
||||
|
||||
public function __construct($current_user, $conn) {
|
||||
parent::__construct($current_user, $conn);
|
||||
$this->current_user = $current_user;
|
||||
$this->conn = $conn;
|
||||
}
|
||||
|
||||
public function getForm($title, $content) {
|
||||
$this->title = $title;
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function postUpdate() {
|
||||
if (!$this->isLoggedIn())
|
||||
return;
|
||||
|
||||
switch(true) {
|
||||
case empty($this->content):
|
||||
$this->error = "Your post cannot be left empty.";
|
||||
break;
|
||||
case strlen($this->content) > 2500:
|
||||
$this->error = "Post exceeds the character limit. Are you trying to bypass it?";
|
||||
break;
|
||||
}
|
||||
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
public function insertPost() {
|
||||
if (!$this->isLoggedIn())
|
||||
return;
|
||||
|
||||
$new_content = $this->content;
|
||||
|
||||
if (empty($this->title)) {
|
||||
// micro post
|
||||
$type = 1;
|
||||
$new_content = substr($this->content, 0, 300);
|
||||
} else {
|
||||
// marco post
|
||||
$type = 2;
|
||||
}
|
||||
|
||||
$stmt = $this->conn->prepare("INSERT INTO posts (title, content, author, type) VALUES (:title, :content, :author, :type)");
|
||||
$stmt->bindParam(":title", $this->title);
|
||||
$stmt->bindParam(":content", $new_content);
|
||||
$stmt->bindParam(":author", $this->getDetails("username"));
|
||||
$stmt->bindParam(":type", $type);
|
||||
|
||||
$stmt->execute();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getPosts($type) {
|
||||
$stmt = $this->conn->prepare("SELECT * FROM posts WHERE type = :type ORDER BY date DESC");
|
||||
$stmt->bindParam(":type", $type);
|
||||
$stmt->execute();
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
public function getSpecifcPost($id) {
|
||||
$stmt = $this->conn->prepare("SELECT * FROM posts WHERE id = :id");
|
||||
$stmt->bindParam(":id", $id);
|
||||
$stmt->execute();
|
||||
|
||||
$post_details = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
return $post_details;
|
||||
}
|
||||
|
||||
public function deletePost($id) {
|
||||
$stmt = $this->conn->prepare("DELETE FROM posts WHERE id = :id");
|
||||
$stmt->bindParam(":id", $id);
|
||||
$stmt->execute();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
||||
64
lib/classes/Register.php
Normal file
64
lib/classes/Register.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
class Register {
|
||||
private $username;
|
||||
private $password;
|
||||
private $conn;
|
||||
public $error;
|
||||
|
||||
public function __construct($username, $password, $conn) {
|
||||
$this->username = trim($username);
|
||||
$this->password = trim($password);
|
||||
$this->conn = $conn;
|
||||
}
|
||||
|
||||
public function checkUsername() {
|
||||
switch(true) {
|
||||
case empty($this->username):
|
||||
$this->error = "Please fill out all of the fields";
|
||||
break;
|
||||
case preg_match("/[^a-z0-9 ]/i", $this->username):
|
||||
$this->error = "Username cannot have any special characters.";
|
||||
break;
|
||||
case strlen($this->username) > 16:
|
||||
$this->error = "Username cannot be longer than 16 characters.";
|
||||
break;
|
||||
case strlen($this->username) < 3:
|
||||
$this->error = "Your username cannot be shorter than 3 characters.";
|
||||
break;
|
||||
default:
|
||||
$stmt = $this->conn->prepare("SELECT username FROM accounts WHERE username = :username");
|
||||
$stmt->bindParam(":username", $this->username);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() > 0)
|
||||
$this->error = "Username has been already taken.";
|
||||
}
|
||||
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
public function checkPassword($passwordConfirm) {
|
||||
if (empty($this->password)) {
|
||||
$this->error = "Please fill out all of the fields.";
|
||||
} else {
|
||||
if ($this->password != $passwordConfirm)
|
||||
$this->error = "Passwords don't match.";
|
||||
}
|
||||
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
public function insertUser() {
|
||||
$hashed_password = password_hash($this->password, PASSWORD_BCRYPT);
|
||||
|
||||
$stmt = $this->conn->prepare("INSERT INTO accounts (username, password) VALUES (:username, :password)");
|
||||
$stmt->bindParam(":username", $this->username);
|
||||
$stmt->bindParam(":password", $hashed_password);
|
||||
$stmt->execute();
|
||||
|
||||
session_regenerate_id(true);
|
||||
$_SESSION["user"] = $this->username;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
||||
107
lib/classes/Settings.php
Normal file
107
lib/classes/Settings.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
class Settings extends Account {
|
||||
private $current_user;
|
||||
|
||||
private $new_username;
|
||||
private $new_bio;
|
||||
private $new_password;
|
||||
|
||||
private $conn;
|
||||
public $error;
|
||||
|
||||
public function __construct($current_user, $conn) {
|
||||
parent::__construct($current_user, $conn);
|
||||
$this->current_user = $current_user;
|
||||
$this->conn = $conn;
|
||||
}
|
||||
|
||||
public function getForm($username, $bio, $password) {
|
||||
$this->new_username = $username;
|
||||
$this->new_bio = $bio;
|
||||
$this->new_password = $password;
|
||||
}
|
||||
|
||||
public function updateUsername() {
|
||||
if ($this->new_username == $this->getDetails("username"))
|
||||
return NULL;
|
||||
|
||||
switch(true) {
|
||||
case empty($this->new_username):
|
||||
$this->error = "Your username cannot be left blank.";
|
||||
break;
|
||||
case preg_match("/[^a-z0-9 ]/i", $this->new_username):
|
||||
$this->error = "Username cannot have any special characters.";
|
||||
break;
|
||||
case strlen($this->new_username) > 16:
|
||||
$this->error = "Username cannot be longer than 16 characters.";
|
||||
break;
|
||||
case strlen($this->new_username) < 3:
|
||||
$this->error = "Username cannot be shorter than 3 characters.";
|
||||
break;
|
||||
default:
|
||||
$stmt = $this->conn->prepare("SELECT username FROM accounts WHERE username = :username");
|
||||
$stmt->bindParam(":username", $this->new_username);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() > 0)
|
||||
$this->error = "Username has been already taken.";
|
||||
}
|
||||
|
||||
|
||||
if ($this->error == NULL) {
|
||||
$stmt = $this->conn->prepare("UPDATE accounts SET username = :username WHERE id = :id");
|
||||
$stmt->bindParam(":username", $this->new_username);
|
||||
$stmt->bindParam(":id", $this->getDetails("id"));
|
||||
$stmt->execute();
|
||||
|
||||
session_regenerate_id(true);
|
||||
$_SESSION["user"] = $this->new_username;
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
public function updateBio() {
|
||||
if ($this->new_bio == $this->getDetails("bio"))
|
||||
return NULL;
|
||||
|
||||
|
||||
if (strlen($this->new_bio) > 150)
|
||||
$this->error = "Bio cannot be longer than 150 characters.";
|
||||
|
||||
if ($this->error == NULL) {
|
||||
$stmt = $this->conn->prepare("UPDATE accounts SET bio = :bio WHERE id = :id");
|
||||
$stmt->bindParam(":bio", $this->new_bio);
|
||||
$stmt->bindParam(":id", $this->getDetails("id"));
|
||||
$stmt->execute();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
public function updatePassword($passwordConfirm) {
|
||||
if (empty($this->new_password)) {
|
||||
return NULL;
|
||||
} else {
|
||||
if ($this->new_password != $passwordConfirm)
|
||||
$this->error = "Passwords don't match.";
|
||||
}
|
||||
|
||||
if ($this->error == NULL) {
|
||||
$hashed_password = password_hash($this->new_password, PASSWORD_BCRYPT);
|
||||
|
||||
$stmt = $this->conn->prepare("UPDATE accounts SET password = :password WHERE id = :id");
|
||||
$stmt->bindParam(":password", $hashed_password);
|
||||
$stmt->bindParam(":id", $this->getDetails("id"));
|
||||
$stmt->execute();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->error;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user