600 lines
16 KiB
Go
600 lines
16 KiB
Go
package main
|
|
|
|
import (
|
|
//"container/list"
|
|
"database/sql"
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"net/smtp"
|
|
"time"
|
|
|
|
"github.com/gorilla/sessions"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
var (
|
|
//sesja
|
|
key = []byte("1010101010101")
|
|
store = sessions.NewCookieStore(key)
|
|
|
|
//do rejestracji biletu
|
|
KosztBiletuSUMA int
|
|
|
|
//zmiana pinu, hasła i blokada karty
|
|
pinFROMsite int
|
|
RFIDblockORno string
|
|
hasloFROMsite string
|
|
|
|
//logowanie ------------
|
|
loginFROMsite string
|
|
passwordFROMsite string
|
|
|
|
//users ---------------
|
|
IDusers int
|
|
User string
|
|
Password string
|
|
PIN int
|
|
RFID int
|
|
Login string
|
|
Blokada bool
|
|
|
|
//bilety ---------------
|
|
IDbiletu int
|
|
KtoZabralID int
|
|
DataCzas string
|
|
KosztBiletu int
|
|
CzyZaplacony string
|
|
|
|
//dania ---------------
|
|
IDdania int
|
|
NazwaDania string
|
|
KosztDania int
|
|
|
|
//sprawdza zadluzenie
|
|
dataBiletu string
|
|
dataAktualna string
|
|
roznica float64
|
|
)
|
|
|
|
//const localHost = string("192.168.8.102:9197")
|
|
|
|
//***************************************************************************
|
|
//funkcje sterujące
|
|
//***************************************************************************
|
|
|
|
//#############################################################################
|
|
//wyslij maila
|
|
//#############################################################################
|
|
func wyslijEmail(body string) {
|
|
//sprawdz czy kotś zalega
|
|
|
|
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
|
|
checkErr(err)
|
|
rows3, err := db.Query("SELECT * FROM bilety")
|
|
checkErr(err)
|
|
for rows3.Next() {
|
|
err = rows3.Scan(&IDbiletu, &KtoZabralID, &DataCzas, &KosztBiletu, &CzyZaplacony)
|
|
checkErr(err)
|
|
dataBiletu := DataCzas
|
|
currentTime := time.Now()
|
|
dataAktualna := currentTime.Format("2006-1-2 / 3:4:5")
|
|
|
|
if KtoZabralID == IDusers {
|
|
|
|
input := dataBiletu
|
|
layout := "2006-1-2 / 3:4:5"
|
|
t, _ := time.Parse(layout, input)
|
|
dataBiletu := t
|
|
|
|
input1 := dataAktualna
|
|
layout1 := "2006-1-2 / 3:4:5"
|
|
t1, _ := time.Parse(layout1, input1)
|
|
dataAktualna := t1
|
|
|
|
//fmt.Println(dataAktualna)
|
|
//fmt.Println(dataBiletu)
|
|
|
|
diff := dataAktualna.Sub(dataBiletu)
|
|
//fmt.Println(diff)
|
|
|
|
roznica := diff.Hours()
|
|
//fmt.Println(roznica)
|
|
|
|
if roznica > 720 {
|
|
if CzyZaplacony == "nie" {
|
|
//email start
|
|
from := "golang_zaliczenie_pp@moto-boss.pl"
|
|
pass := "zaq1@WSX"
|
|
to := "kryklo@st.amu.edu.pl"
|
|
msg := "From: " + from + "\n" +
|
|
"To: " + to + "\n" +
|
|
"Subject: Stołówka ZPM - płatności\n\n" +
|
|
body
|
|
err = smtp.SendMail("serwer1812241.home.pl:587", smtp.PlainAuth("", from, pass, "serwer1812241.home.pl"), from, []string{to}, []byte(msg))
|
|
if err != nil {
|
|
log.Printf("smtp error: %s", err)
|
|
return
|
|
}
|
|
log.Print("wysłano poprawnie e-mail z powiadomieniem o zaleganiu")
|
|
//email end
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
//----------------------------------
|
|
rows3.Close()
|
|
db.Close()
|
|
//-----------------------
|
|
|
|
}
|
|
|
|
//#############################################################################
|
|
func sesja(w http.ResponseWriter, r *http.Request) {
|
|
t, _ := template.ParseFiles("sesja.gtpl")
|
|
t.Execute(w, nil)
|
|
}
|
|
|
|
//#############################################################################
|
|
func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) {
|
|
t, _ := template.ParseFiles("StronaGlowna.gohtml")
|
|
t.Execute(w, nil)
|
|
}
|
|
|
|
//#############################################################################
|
|
func panel(w http.ResponseWriter, r *http.Request) {
|
|
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)
|
|
}
|
|
}
|
|
|
|
//#############################################################################
|
|
func wylogowano(w http.ResponseWriter, r *http.Request) {
|
|
//--------------------------------------------------
|
|
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()
|
|
}
|
|
//--------------------------------------------------
|
|
//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"
|
|
|
|
t, _ := template.ParseFiles("wylogowano.gtpl")
|
|
type user struct {
|
|
UserView string
|
|
}
|
|
u := user{
|
|
UserView: User}
|
|
t.ExecuteTemplate(w, "wylogowano.gtpl", u)
|
|
|
|
}
|
|
|
|
//#############################################################################
|
|
func login(w http.ResponseWriter, r *http.Request) {
|
|
//**************************************************************************
|
|
loginFROMsite := r.FormValue("loginFROMsite")
|
|
passwordFROMsite := r.FormValue("passwordFROMsite")
|
|
|
|
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
|
|
checkErr(err)
|
|
rows0, err := db.Query("SELECT * FROM users")
|
|
checkErr(err)
|
|
|
|
for rows0.Next() {
|
|
err = rows0.Scan(&IDusers, &User, &Password, &PIN, &RFID, &Login, &Blokada)
|
|
checkErr(err)
|
|
if r.Method == "POST" {
|
|
if Login == loginFROMsite {
|
|
if Password == passwordFROMsite {
|
|
//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)
|
|
//----------------
|
|
//e-mail-------------------
|
|
wyslijEmail(User + ", zalega z płatnościami za posiłki w stołówce! Skontaktuj się z nim lub z administratorem systemu 'Stołówka ZPM' (admin@example.pl), w celu ustalenia wysokości zadłużenia. \n\n Dziękuję,\n Administrator.\n (wiadomość została wygenerowana automatycznie, proszę nie odpowiadaj na nią)")
|
|
//-------------------------
|
|
http.Redirect(w, r, "http://localhost:9197/panel", http.StatusSeeOther)
|
|
panel(w, r)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if r.Method == "GET" {
|
|
t, _ := template.ParseFiles("login.gtpl")
|
|
t.Execute(w, nil)
|
|
r.ParseForm()
|
|
}
|
|
|
|
if r.Method == "POST" {
|
|
if loginFROMsite != Login {
|
|
if loginFROMsite == "" {
|
|
if passwordFROMsite != Password {
|
|
t, _ := template.ParseFiles("login.gtpl")
|
|
t.Execute(w, nil)
|
|
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")
|
|
t.Execute(w, nil)
|
|
fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***")
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
//**************************************************************************
|
|
rows0.Close()
|
|
db.Close()
|
|
}
|
|
|
|
//#############################################################################
|
|
func menuDan(w http.ResponseWriter, r *http.Request) {
|
|
if User == "lost" {
|
|
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
|
|
} else {
|
|
t, _ := template.ParseFiles("menuDanNaglowek.gtpl")
|
|
d := struct {
|
|
UserView string
|
|
}{
|
|
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()
|
|
}
|
|
}
|
|
|
|
//#############################################################################
|
|
func historiaPosilkow(w http.ResponseWriter, r *http.Request) {
|
|
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)
|
|
|
|
//bilety ----------------------------------
|
|
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
|
|
checkErr(err)
|
|
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)
|
|
}
|
|
//----------------------------------
|
|
}
|
|
rows3.Close()
|
|
db.Close()
|
|
}
|
|
}
|
|
|
|
//#############################################################################
|
|
func platnosci(w http.ResponseWriter, r *http.Request) {
|
|
if User == "lost" {
|
|
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
|
|
} else {
|
|
|
|
//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
|
|
}
|
|
//----------------------------------
|
|
}
|
|
//fmt.Println(KosztBiletuSUMA)
|
|
t, _ := template.ParseFiles("platnosci.gtpl")
|
|
checkErr(err)
|
|
type zaplac struct {
|
|
UserView string
|
|
Zaplac int
|
|
}
|
|
z := zaplac{
|
|
UserView: User,
|
|
Zaplac: KosztBiletuSUMA}
|
|
|
|
t.ExecuteTemplate(w, "platnosci.gtpl", z)
|
|
rows5.Close()
|
|
db.Close()
|
|
}
|
|
}
|
|
|
|
//#############################################################################
|
|
func placeOnline(w http.ResponseWriter, r *http.Request) {
|
|
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
|
|
}
|
|
}
|
|
t, _ := template.ParseFiles("placeOnline.gtpl")
|
|
checkErr(err)
|
|
type zaplac struct {
|
|
UserView string
|
|
Zaplac int
|
|
}
|
|
z := zaplac{
|
|
UserView: User,
|
|
Zaplac: KosztBiletuSUMA}
|
|
|
|
t.ExecuteTemplate(w, "placeOnline.gtpl", z)
|
|
rows5.Close()
|
|
db.Close()
|
|
}
|
|
}
|
|
|
|
//#############################################################################
|
|
func zaplacono(w http.ResponseWriter, r *http.Request) {
|
|
if User == "lost" {
|
|
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
|
|
} else {
|
|
|
|
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
|
|
checkErr(err)
|
|
|
|
akt, err := db.Prepare("UPDATE bilety SET CzyZaplacony=? WHERE KtoZabralID=?")
|
|
checkErr(err)
|
|
up, err := akt.Exec("tak", KtoZabralID)
|
|
checkErr(err)
|
|
dzialanie, err := up.RowsAffected()
|
|
checkErr(err)
|
|
fmt.Println(dzialanie)
|
|
|
|
db.Close()
|
|
|
|
t, _ := template.ParseFiles("zaplacono.gtpl")
|
|
checkErr(err)
|
|
type zaplac struct {
|
|
UserView string
|
|
Zaplac int
|
|
}
|
|
z := zaplac{
|
|
UserView: User,
|
|
Zaplac: KosztBiletuSUMA}
|
|
|
|
t.ExecuteTemplate(w, "zaplacono.gtpl", z)
|
|
|
|
}
|
|
}
|
|
|
|
//#############################################################################
|
|
func zmianaPINU(w http.ResponseWriter, r *http.Request) {
|
|
if User == "lost" {
|
|
http.Redirect(w, r, "http://localhost:9197/sesja", http.StatusSeeOther)
|
|
} else {
|
|
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")
|
|
|
|
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("true", IDusers)
|
|
checkErr(err)
|
|
dzialanie, err := up.RowsAffected()
|
|
checkErr(err)
|
|
fmt.Println(dzialanie)
|
|
|
|
db.Close()
|
|
|
|
} 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()
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//#############################################################################
|
|
func zmianaHasla(w http.ResponseWriter, r *http.Request) {
|
|
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)
|
|
}
|
|
}
|
|
|
|
//***************************************************************************
|
|
//Obsługa błędów
|
|
//***************************************************************************
|
|
|
|
func checkErr(err error) {
|
|
if err != nil {
|
|
fmt.Println("Błąd")
|
|
}
|
|
}
|
|
func checkErr2(err error) {
|
|
if err != nil {
|
|
fmt.Println("Błąd dwa")
|
|
}
|
|
}
|
|
func checkErr3(err error) {
|
|
if err != nil {
|
|
fmt.Println("Błąd trzy")
|
|
}
|
|
}
|
|
|
|
//***************************************************************************
|
|
//funkcja główna
|
|
//***************************************************************************
|
|
func main() {
|
|
http.HandleFunc("/", opisStołówkaZPM)
|
|
http.HandleFunc("/login", login)
|
|
http.HandleFunc("/panel", panel)
|
|
http.HandleFunc("/historiaPosilkow", historiaPosilkow)
|
|
http.HandleFunc("/platnosci", platnosci)
|
|
http.HandleFunc("/platnosci/placeOnline", placeOnline)
|
|
http.HandleFunc("/zaplacono", zaplacono)
|
|
http.HandleFunc("/wylogowano", wylogowano)
|
|
http.HandleFunc("/menuDan", menuDan)
|
|
http.HandleFunc("/zmianaPINU", zmianaPINU)
|
|
http.HandleFunc("/zmianaHasla", zmianaHasla)
|
|
http.HandleFunc("/sesja", sesja)
|
|
|
|
err := http.ListenAndServe(":9197", nil)
|
|
if err != nil {
|
|
log.Fatal("ListenAndServe: ", err)
|
|
}
|
|
}
|