Skip to content

Commit

Permalink
final logout ref #8
Browse files Browse the repository at this point in the history
  • Loading branch information
dandyray committed Apr 27, 2021
1 parent bc8420a commit bc661b3
Show file tree
Hide file tree
Showing 33 changed files with 1,244 additions and 29 deletions.
11 changes: 11 additions & 0 deletions Library.id/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RewriteEngine on

# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" [NC]
RewriteRule .* - [L,R=404]

# NOTE! FOR APACHE ON WINDOWS: Add [NC] to RewriteCond like this:
# RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" [NC]
26 changes: 26 additions & 0 deletions Library.id/actions/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

require "../connect.php";

session_start();

$login = new LoginUser($db);
$login->email = $_POST["email"];
$login->password = $_POST["password"];
$r = $login->login();

var_dump($r);

if ($r != null)
{
$_SESSION["id"] = $r["id"];
$_SESSION["role"] = "user";
header("Location: ../homepage");
exit;
}
else
{
header("Location: ../login?err=1");
}

?>
26 changes: 26 additions & 0 deletions Library.id/actions/loginadmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

require "../connect.php";

session_start();

$login = new LoginAdmin($db);
$login->username = $_POST["username"];
$login->password = $_POST["password"];
$r = $login->login();

var_dump($r);

if ($r != null)
{
$_SESSION["id"] = $r["id"];
$_SESSION["role"] = "admin";
header("Location: ../homepage");
exit;
}
else
{
header("Location: ../loginadmin?err=1");
}

?>
File renamed without changes.
22 changes: 22 additions & 0 deletions Library.id/actions/register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

require "../connect.php";

session_start();

if ($_POST["password"] != $_POST["confirmpassword"])
{
header("Location: ../registrasi?err=1");
}

$reg = new RegisterUser($db);
$reg->nama = $_POST["nama"];
$reg->email = $_POST["email"];
$reg->username = $_POST["username"];
$reg->no_ktp = $_POST["no_ktp"];
$reg->no_telp = $_POST["no_telp"];
$reg->password = $_POST["password"];

$user = $reg->register();

header("Location: ../login");
Empty file removed Library.id/bebas.txt
Empty file.
247 changes: 247 additions & 0 deletions Library.id/classes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
<?php

class DB {
public $conn;

function __construct($servername,$username,$password,$dbname)
{
$this->conn = new mysqli($servername,$username,$password,$dbname);
if ($this->conn->connect_error) {
die("Connection failed: " . $this->conn->connect_error);
}
}

function getConn()
{
return $this->conn;
}

function query($query,$type,...$params)
{
$stmt = $this->conn->prepare($query);
$stmt->bind_param($type, ...$params);
return $stmt;
}
}

class RegisterUser
{
public $nama,$username,$password,$no_ktp,$no_telp,$email,$db;

function __construct(DB $db)
{
$this->db = $db;
}

function register()
{

$check = $this->db->query(
"SELECT * FROM user WHERE email = ?",
"s",
$this->email
);
$check->execute();
$res = $check->get_result();
if ($res->num_rows > 0)
{
header("location:../registrasi?err=2");
exit;
}

$stmt = $this->db->query(
"INSERT INTO user (nama,email,username,no_ktp,no_telp,password) values (?,?,?,?,?,?)",
"ssssss",
$this->nama,
$this->email,
$this->username,
$this->no_ktp,
$this->no_telp,
$this->password
);

$stmt->execute();

$stmt = $this->db->query(
"SELECT * FROM user WHERE username = ?",
"s",
$this->username
);

$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_assoc();
return $data;
}
}

class User
{
public $db,$nama,$username,$email,$no_ktp,$no_telp,$password;

function __construct(DB $db,$id)
{
$this->db = $db;
$stmt = $this->db->query(
"SELECT * FROM user WHERE id = ?;",
"i",
intval($id)
);

$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_assoc();
$this->nama = $data["nama"];
$this->username = $data["username"];
$this->email = $data["email"];
$this->no_ktp = $data["no_ktp"];
$this->no_telp = $data["no_telp"];
$this->password = $data["password"];
}
}

class Admin
{
public $db,$username,$password;

function __construct(DB $db,$id)
{
$this->db = $db;
$stmt = $this->db->query(
"SELECT * FROM admin WHERE id = ?;",
"i",
intval($id)
);

$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_assoc();
$this->username = $data["username"];
$this->password = $data["password"];
}
}

class LoginUser
{
public $db,$email,$password;

function __construct(DB $db)
{
$this->db = $db;
}

function login()
{
$stmt = $this->db->query(
"SELECT * FROM user WHERE email = ? AND password = ?;",
"ss",
$this->email,
$this->password
);
$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_assoc();
return $data;
}
}

class LoginAdmin
{
public $db,$username,$password;

function __construct(DB $db)
{
$this->db = $db;
}

function login()
{
$stmt = $this->db->query(
"SELECT * FROM admin WHERE username = ? AND password = ?;",
"ss",
$this->username,
$this->password
);
$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_assoc();
return $data;
}
}

// Tambah Kategori
class Kategori
{
public static $kategori = ["fiksi","filsafat","religi","sains","komputer","antropologi","sejarah","musik","bisnis","biografi"];
public $db,$username,$password;

function __construct(DB $db)
{
$this->db = $db;
}

public function getBukuByKategori($kat)
{
$res = $this->db->conn->query(
"SELECT * FROM ktgr_$kat"
);

return $res;
}

}

class Buku
{
public $db,$judul,$pengarang,$foto,$preview;

function __construct(DB $db)
{
$this->db = $db;
}

function getBukuById($id)
{
$stmt = $this->db->query(
"SELECT * FROM buku WHERE id = ?",
"i",
$id
);
$stmt->execute();
$res = $stmt->get_result();
return $res->fetch_assoc();
}

function uploadFoto($foto)
{
$foto_name = strval(rand(1,999999)).$foto["name"];
move_uploaded_file($foto["tmp_name"],"../foto_buku/".$foto_name);
$this->foto = $foto_name;
}

function tambah($kat)
{
$stmt = $this->db->query(
"INSERT INTO buku (judul,pengarang,foto,preview) VALUES (?,?,?,?)",
"ssss",
$this->judul,$this->pengarang,$this->foto,$this->preview
);
$stmt->execute();
$insertid = $stmt->insert_id;

foreach (Kategori::$kategori as $k)
{
if (isset($_POST["ktgr_".$k]))
{
$stmt = $this->db->query(
"INSERT INTO ktgr_$k (id_buku) VALUES (?) ",
"i",
$insertid
);
$stmt->execute();
}
}


}
}
5 changes: 5 additions & 0 deletions Library.id/connect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require "classes.php";

$db = new DB("localhost","root","","library.id");
11 changes: 11 additions & 0 deletions Library.id/css/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.bookbg {
background: url("../img/undraw_Books_l33t.svg") no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

.text-outline {
text-shadow: 0px 0px 10px black;
}
7 changes: 7 additions & 0 deletions Library.id/css/bootstrap.min.css

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions Library.id/homepage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require "connect.php";

session_start();

if ($_SESSION["role"] == "user")
{
$user = new User($db, $_SESSION["id"]);
require "view/homepage-user.php";
}
else
{
$admin = new Admin($db, $_SESSION["id"]);
require "view/tambahbuku.php";
}

?>
Binary file added Library.id/img/books-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Library.id/img/undraw_Books_l33t.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit bc661b3

Please sign in to comment.