zaliczeniePP/main.go

527 lines
14 KiB
Go
Raw Normal View History

2018-11-21 23:47:31 +01:00
package main
2018-11-20 22:17:07 +01:00
2018-12-10 15:52:29 +01:00
import (
2019-01-12 15:24:28 +01:00
//"container/list"
2018-12-28 22:10:55 +01:00
"database/sql"
2018-12-10 15:52:29 +01:00
"fmt"
"html/template"
"log"
"net/http"
2018-12-28 22:10:55 +01:00
2019-01-12 15:24:28 +01:00
//"sync"
2018-12-29 04:54:47 +01:00
//"time"
2019-01-12 15:24:28 +01:00
//"net/url"
2018-12-29 04:54:47 +01:00
2019-01-12 15:24:28 +01:00
"github.com/gorilla/sessions"
2018-12-28 22:10:55 +01:00
_ "github.com/mattn/go-sqlite3"
2018-12-10 15:52:29 +01:00
)
2018-11-21 23:47:31 +01:00
2019-01-03 23:57:38 +01:00
var (
2019-01-12 15:24:28 +01:00
//sesja
key = []byte("1010101010101")
store = sessions.NewCookieStore(key)
//do rejestracji biletu
2019-01-04 17:58:41 +01:00
KosztBiletuSUMA int
2019-01-03 23:57:38 +01:00
2019-01-05 13:15:14 +01:00
//zmiana pinu, hasła i blokada karty
pinFROMsite int
RFIDblockORno string
hasloFROMsite string
2019-01-03 23:57:38 +01:00
//logowanie ------------
loginFROMsite string
passwordFROMsite string
//users ---------------
IDusers int
User string
Password string
PIN int
RFID int
Login string
Blokada bool
//bilety ---------------
IDbiletu int
2019-01-05 14:32:51 +01:00
KtoZabralID int
2019-01-03 23:57:38 +01:00
DataCzas string
KosztBiletu int
2019-01-05 13:15:14 +01:00
CzyZaplacony string
2019-01-03 23:57:38 +01:00
//dania ---------------
IDdania int
NazwaDania string
KosztDania int
)
//const localHost = string("192.168.8.102:9197")
2018-12-29 16:02:40 +01:00
2018-12-29 00:08:27 +01:00
//***************************************************************************
//funkcje sterujące
//***************************************************************************
2019-01-03 23:57:38 +01:00
2019-01-12 15:24:28 +01:00
//#############################################################################
func sesja(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("sesja.gtpl")
t.Execute(w, nil)
}
2018-12-29 16:02:40 +01:00
//#############################################################################
2018-12-27 21:58:43 +01:00
func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) {
2019-01-04 17:58:41 +01:00
t, _ := template.ParseFiles("StronaGlowna.gohtml")
t.Execute(w, nil)
2018-12-29 01:36:46 +01:00
}
2018-12-29 00:08:27 +01:00
2018-12-29 16:02:40 +01:00
//#############################################################################
2018-12-29 01:36:46 +01:00
func panel(w http.ResponseWriter, r *http.Request) {
2019-01-12 15:24:28 +01:00
if User == "lost" {
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
} else {
fmt.Println("method:", r.Method)
t, _ := template.ParseFiles("panel.gtpl")
d := struct {
UserView string
}{
UserView: User}
t.ExecuteTemplate(w, "panel.gtpl", d)
2019-01-02 16:32:13 +01:00
}
2018-12-29 16:02:40 +01:00
}
2019-01-02 16:32:13 +01:00
//#############################################################################
func wylogowano(w http.ResponseWriter, r *http.Request) {
2019-01-05 13:15:14 +01:00
//--------------------------------------------------
hasloFROMsite := r.FormValue("hasloFROMsite")
if hasloFROMsite != "" {
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
akt, err := db.Prepare("UPDATE users SET Password=? WHERE IDusers=?")
checkErr(err)
up, err := akt.Exec(hasloFROMsite, IDusers)
checkErr(err)
dzialanie, err := up.RowsAffected()
checkErr(err)
fmt.Println(dzialanie)
db.Close()
}
2019-01-02 16:32:13 +01:00
//--------------------------------------------------
2019-01-12 15:24:28 +01:00
//sesja----
session, err := store.Get(r, "sesja1")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
session.Values[User] = false
session.Save(r, w)
//---------
//ustaw wartości na null
KosztBiletuSUMA = 0
pinFROMsite = 0
RFIDblockORno = "lost"
hasloFROMsite = "lost"
loginFROMsite = "lost"
passwordFROMsite = "lost"
IDusers = 0
User = "lost"
Password = "lost"
PIN = 0
RFID = 0
Login = "lost"
IDbiletu = 0
KtoZabralID = 0
DataCzas = "lost"
KosztBiletu = 0
CzyZaplacony = "lost"
IDdania = 0
NazwaDania = "lost"
KosztDania = 0
User = "lost"
2019-01-05 14:52:43 +01:00
2019-01-02 16:32:13 +01:00
t, _ := template.ParseFiles("wylogowano.gtpl")
2019-01-05 13:15:14 +01:00
type user struct {
UserView string
}
u := user{
UserView: User}
t.ExecuteTemplate(w, "wylogowano.gtpl", u)
2019-01-12 15:24:28 +01:00
2019-01-05 13:15:14 +01:00
}
2019-01-02 01:13:39 +01:00
//#############################################################################
func login(w http.ResponseWriter, r *http.Request) {
2019-01-03 23:57:38 +01:00
//**************************************************************************
2019-01-02 01:13:39 +01:00
loginFROMsite := r.FormValue("loginFROMsite")
passwordFROMsite := r.FormValue("passwordFROMsite")
2018-12-29 00:08:27 +01:00
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
2019-01-02 01:13:39 +01:00
rows0, err := db.Query("SELECT * FROM users")
2018-12-29 00:08:27 +01:00
checkErr(err)
2018-12-29 16:02:40 +01:00
2019-01-02 01:13:39 +01:00
for rows0.Next() {
2019-01-05 13:15:14 +01:00
err = rows0.Scan(&IDusers, &User, &Password, &PIN, &RFID, &Login, &Blokada)
2019-01-02 01:13:39 +01:00
checkErr(err)
2019-01-04 17:58:41 +01:00
if r.Method == "POST" {
if Login == loginFROMsite {
if Password == passwordFROMsite {
2019-01-12 15:24:28 +01:00
//sesja start-----
session, err := store.Get(r, "sesja1")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
session.Values[User] = true
session.Save(r, w)
//----------------
2019-01-04 17:58:41 +01:00
http.Redirect(w, r, "http://localhost:9197/panel", http.StatusSeeOther)
panel(w, r)
break
}
2018-12-29 16:02:40 +01:00
}
}
2018-12-29 00:08:27 +01:00
}
2018-12-10 16:09:22 +01:00
2018-12-10 18:00:33 +01:00
if r.Method == "GET" {
2018-12-28 14:22:11 +01:00
t, _ := template.ParseFiles("login.gtpl")
2019-01-04 17:58:41 +01:00
t.Execute(w, nil)
2018-12-10 18:00:33 +01:00
r.ParseForm()
2018-12-27 21:58:43 +01:00
}
2018-12-29 04:11:48 +01:00
2019-01-02 01:13:39 +01:00
if r.Method == "POST" {
if loginFROMsite != Login {
if loginFROMsite == "" {
if passwordFROMsite != Password {
t, _ := template.ParseFiles("login.gtpl")
2019-01-04 17:58:41 +01:00
t.Execute(w, nil)
2019-01-02 01:13:39 +01:00
fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***")
}
} else {
if passwordFROMsite != Password {
t, _ := template.ParseFiles("login.gtpl")
2019-01-04 17:58:41 +01:00
t.Execute(w, nil)
2019-01-02 01:13:39 +01:00
fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***")
}
}
}
}
2019-01-03 23:57:38 +01:00
//**************************************************************************
2019-01-02 01:13:39 +01:00
rows0.Close()
db.Close()
2019-01-03 23:57:38 +01:00
}
//#############################################################################
func menuDan(w http.ResponseWriter, r *http.Request) {
2019-01-12 15:24:28 +01:00
if User == "lost" {
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
} else {
t, _ := template.ParseFiles("menuDanNaglowek.gtpl")
2019-01-03 23:57:38 +01:00
d := struct {
2019-01-12 15:24:28 +01:00
UserView string
2019-01-03 23:57:38 +01:00
}{
2019-01-12 15:24:28 +01:00
UserView: User}
t.ExecuteTemplate(w, "menuDanNaglowek.gtpl", d)
//dania ----------------------------------
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr2(err)
rows2, err := db.Query("SELECT * FROM dania")
checkErr(err)
for rows2.Next() {
err = rows2.Scan(&IDdania, &NazwaDania, &KosztDania)
checkErr3(err)
//fmt.Printf("%d. %s, %d \n", IDdania, NazwaDania, KosztDania)
t, _ := template.ParseFiles("menuDan.gtpl")
d := struct {
IDdaniaView int
NazwaDaniaView string
KosztDaniaView int
}{
IDdaniaView: IDdania,
NazwaDaniaView: NazwaDania,
KosztDaniaView: KosztDania}
t.ExecuteTemplate(w, "menuDan.gtpl", d)
}
//----------------------------------
rows2.Close()
db.Close()
2019-01-03 23:57:38 +01:00
}
2019-01-02 01:13:39 +01:00
}
//#############################################################################
func historiaPosilkow(w http.ResponseWriter, r *http.Request) {
2019-01-12 15:24:28 +01:00
if User == "lost" {
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
} else {
t, _ := template.ParseFiles("historiaPosilkowNaglowek.gtpl")
d := struct {
UserView string
}{
UserView: User}
t.ExecuteTemplate(w, "historiaPosilkowNaglowek.gtpl", d)
2019-01-02 01:13:39 +01:00
2019-01-12 15:24:28 +01:00
//bilety ----------------------------------
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
2018-12-29 04:11:48 +01:00
checkErr(err)
2019-01-12 15:24:28 +01:00
rows3, err := db.Query("SELECT * FROM bilety")
checkErr(err)
for rows3.Next() {
err = rows3.Scan(&IDbiletu, &KtoZabralID, &DataCzas, &KosztBiletu, &CzyZaplacony)
checkErr(err)
if KtoZabralID == IDusers {
t, _ := template.ParseFiles("historiaPosilkow.gtpl")
d := struct {
IDbiletuView int
KtoZabralView int
DataCzasView string
KosztBiletuView int
CzyZaplaconyView string
}{
IDbiletuView: IDbiletu,
KtoZabralView: KtoZabralID,
DataCzasView: DataCzas,
KosztBiletuView: KosztBiletu,
CzyZaplaconyView: CzyZaplacony}
t.ExecuteTemplate(w, "historiaPosilkow.gtpl", d)
}
//----------------------------------
2018-12-29 04:11:48 +01:00
}
2019-01-12 15:24:28 +01:00
rows3.Close()
db.Close()
2018-12-29 04:11:48 +01:00
}
2019-01-02 01:13:39 +01:00
}
//#############################################################################
func platnosci(w http.ResponseWriter, r *http.Request) {
2019-01-12 15:24:28 +01:00
if User == "lost" {
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
} else {
2019-01-02 01:13:39 +01:00
2019-01-12 15:24:28 +01:00
//bilety ----------------------------------
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
rows5, err := db.Query("SELECT * FROM bilety WHERE KtoZabralID=?", IDusers)
checkErr(err)
KosztBiletuSUMA = 0
for rows5.Next() {
err = rows5.Scan(&IDbiletu, &KtoZabralID, &DataCzas, &KosztBiletu, &CzyZaplacony)
checkErr3(err)
//if KtoZabralID == User {
if CzyZaplacony == "nie" {
KosztBiletuSUMA = KosztBiletu + KosztBiletuSUMA
}
//----------------------------------
2019-01-05 13:15:14 +01:00
}
2019-01-12 15:24:28 +01:00
//fmt.Println(KosztBiletuSUMA)
t, _ := template.ParseFiles("platnosci.gtpl")
checkErr(err)
type zaplac struct {
UserView string
Zaplac int
}
z := zaplac{
UserView: User,
Zaplac: KosztBiletuSUMA}
2019-01-04 17:58:41 +01:00
2019-01-12 15:24:28 +01:00
t.ExecuteTemplate(w, "platnosci.gtpl", z)
rows5.Close()
db.Close()
}
2018-12-17 22:35:22 +01:00
}
2019-01-05 13:15:14 +01:00
//#############################################################################
func placeOnline(w http.ResponseWriter, r *http.Request) {
2019-01-12 15:24:28 +01:00
if User == "lost" {
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
} else {
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
rows5, err := db.Query("SELECT * FROM bilety WHERE KtoZabralID=?", IDusers)
checkErr(err)
KosztBiletuSUMA = 0
for rows5.Next() {
err = rows5.Scan(&IDbiletu, &KtoZabralID, &DataCzas, &KosztBiletu, &CzyZaplacony)
checkErr3(err)
if CzyZaplacony == "nie" {
KosztBiletuSUMA = KosztBiletu + KosztBiletuSUMA
}
2019-01-05 13:15:14 +01:00
}
2019-01-12 15:24:28 +01:00
t, _ := template.ParseFiles("placeOnline.gtpl")
checkErr(err)
type zaplac struct {
UserView string
Zaplac int
}
z := zaplac{
UserView: User,
Zaplac: KosztBiletuSUMA}
2019-01-05 14:32:51 +01:00
2019-01-12 15:24:28 +01:00
t.ExecuteTemplate(w, "placeOnline.gtpl", z)
rows5.Close()
db.Close()
2019-01-05 14:32:51 +01:00
}
}
2019-01-05 13:15:14 +01:00
//#############################################################################
2019-01-12 15:24:28 +01:00
func zaplacono(w http.ResponseWriter, r *http.Request) {
if User == "lost" {
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
} else {
2019-01-05 13:15:14 +01:00
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
2019-01-12 15:24:28 +01:00
akt, err := db.Prepare("UPDATE bilety SET CzyZaplacony=? WHERE KtoZabralID=?")
2019-01-05 13:15:14 +01:00
checkErr(err)
2019-01-12 15:24:28 +01:00
up, err := akt.Exec("tak", KtoZabralID)
2019-01-05 13:15:14 +01:00
checkErr(err)
dzialanie, err := up.RowsAffected()
checkErr(err)
fmt.Println(dzialanie)
db.Close()
2019-01-12 15:24:28 +01:00
t, _ := template.ParseFiles("zaplacono.gtpl")
2019-01-05 14:52:43 +01:00
checkErr(err)
2019-01-12 15:24:28 +01:00
type zaplac struct {
UserView string
Zaplac int
}
z := zaplac{
UserView: User,
Zaplac: KosztBiletuSUMA}
2019-01-05 14:52:43 +01:00
2019-01-12 15:24:28 +01:00
t.ExecuteTemplate(w, "zaplacono.gtpl", z)
2019-01-05 14:52:43 +01:00
2019-01-12 15:24:28 +01:00
}
}
2019-01-05 14:52:43 +01:00
2019-01-12 15:24:28 +01:00
//#############################################################################
func zmianaPINU(w http.ResponseWriter, r *http.Request) {
if User == "lost" {
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
2019-01-05 13:15:14 +01:00
} else {
2019-01-12 15:24:28 +01:00
pinFROMsite := r.FormValue("pinFROMsite")
RFIDblockORno := r.FormValue("RFID")
t, _ := template.ParseFiles("zmianaPINU.gtpl")
type pinIblokada struct {
UserView string
}
wykonaj := pinIblokada{
UserView: User}
t.ExecuteTemplate(w, "zmianaPINU.gtpl", wykonaj)
if pinFROMsite != "" {
fmt.Fprintln(w, "*** --- Pin został POMYŚLNIE ZMIENIONY! --- *** \n")
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
akt, err := db.Prepare("UPDATE users SET PIN=? WHERE IDusers=?")
checkErr(err)
up, err := akt.Exec(pinFROMsite, IDusers)
checkErr(err)
dzialanie, err := up.RowsAffected()
checkErr(err)
fmt.Println(dzialanie)
db.Close()
}
if RFIDblockORno == "RFIDblock" {
fmt.Fprintln(w, "*** --- Karta RFID została ZABLOKOWANA! --- *** \n")
2019-01-05 13:15:14 +01:00
2019-01-05 14:52:43 +01:00
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
akt, err := db.Prepare("UPDATE users SET Blokada=? WHERE IDusers=?")
checkErr(err)
2019-01-12 15:24:28 +01:00
up, err := akt.Exec("true", IDusers)
2019-01-05 14:52:43 +01:00
checkErr(err)
dzialanie, err := up.RowsAffected()
checkErr(err)
fmt.Println(dzialanie)
db.Close()
2019-01-12 15:24:28 +01:00
} else {
if RFIDblockORno == "RFIDunlock" {
fmt.Fprintln(w, "*** --- Karta RFID została ODBLOKOWANA! --- *** \n")
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
akt, err := db.Prepare("UPDATE users SET Blokada=? WHERE IDusers=?")
checkErr(err)
up, err := akt.Exec("false", IDusers)
checkErr(err)
dzialanie, err := up.RowsAffected()
checkErr(err)
fmt.Println(dzialanie)
db.Close()
}
2019-01-05 13:15:14 +01:00
}
}
}
//#############################################################################
func zmianaHasla(w http.ResponseWriter, r *http.Request) {
2019-01-12 15:24:28 +01:00
if User == "lost" {
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
} else {
//templatka przekierowuje do funkcji wylogowano
t, _ := template.ParseFiles("zmianaHasla.gtpl")
type zmhaslo struct {
UserView string
}
wykonaj := zmhaslo{
UserView: User}
t.ExecuteTemplate(w, "zmianaHasla.gtpl", wykonaj)
2019-01-05 13:15:14 +01:00
}
}
2018-12-29 00:08:27 +01:00
//***************************************************************************
2019-01-02 16:32:13 +01:00
//Obsługa błędów
2018-12-29 00:08:27 +01:00
//***************************************************************************
func checkErr(err error) {
if err != nil {
fmt.Println("Błąd")
}
2018-12-28 22:10:55 +01:00
}
2019-01-02 16:32:13 +01:00
func checkErr2(err error) {
if err != nil {
fmt.Println("Błąd dwa")
}
}
func checkErr3(err error) {
if err != nil {
fmt.Println("Błąd trzy")
}
}
2018-12-28 22:10:55 +01:00
2018-12-29 00:08:27 +01:00
//***************************************************************************
//funkcja główna
//***************************************************************************
2018-11-21 23:47:31 +01:00
func main() {
2018-12-27 21:58:43 +01:00
http.HandleFunc("/", opisStołówkaZPM)
2018-12-10 18:00:33 +01:00
http.HandleFunc("/login", login)
2018-12-17 22:35:22 +01:00
http.HandleFunc("/panel", panel)
2019-01-02 01:13:39 +01:00
http.HandleFunc("/historiaPosilkow", historiaPosilkow)
http.HandleFunc("/platnosci", platnosci)
2019-01-05 13:15:14 +01:00
http.HandleFunc("/platnosci/placeOnline", placeOnline)
2019-01-05 14:32:51 +01:00
http.HandleFunc("/zaplacono", zaplacono)
2019-01-02 16:32:13 +01:00
http.HandleFunc("/wylogowano", wylogowano)
2019-01-03 23:57:38 +01:00
http.HandleFunc("/menuDan", menuDan)
2019-01-05 13:15:14 +01:00
http.HandleFunc("/zmianaPINU", zmianaPINU)
http.HandleFunc("/zmianaHasla", zmianaHasla)
2019-01-12 15:24:28 +01:00
http.HandleFunc("/sesja", sesja)
2018-12-29 16:02:40 +01:00
2019-01-04 17:58:41 +01:00
err := http.ListenAndServe(":9197", nil)
2018-12-10 15:52:29 +01:00
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
2018-11-21 23:47:31 +01:00
}