<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0,width=device-width,height=device-height,user-scalable=no">
<title>Menu</title>
<style type="text/css">
nav {
color: #555;
}
</style>
</head>
<body>
<nav></nav>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
var io = io('http://localhost:3000/');
function makeMenuLevel(menuItems) {
return `<ul>${menuItems.map(
item =>`<li>${item.title}${item.children ? makeMenuLevel(item.children) : ''}</li>`
).join('')}</ul>`;
}
$(document).on('click',(e) => {
var xhr = new XMLHttpRequest();
var body = 'id=' + encodeURIComponent(123);
xhr.open("POST", '/menu', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(body);
})
io.on('menu', (data) => {
const menu = makeMenuLevel(data)
$('nav').append(menu)
console.log(data)
})
</script>
</body>
</html>
app.get('/menu', (req,res) => {
res.sendFile(__dirname + '/menu.html')
})
app.post('/menu', (req,res) => {
connection.query('SELECT * FROM menu', (err, result) => {
if(err) {
console.error(err);
return;
}
const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
var menu = [];
for(const row of result) {
if(row.parent_id === 0) {
menu.push(row);
continue;
}
const parent = index[row.parent_id];
if(!parent) {
console.warn(`Undefined parent with id ${row.parent_id}`);
continue;
}
if(!parent.children) {
parent.children = [];
}
parent.children.push(row);
return io.emit('menu',menu)
}
});
})
$(document).on('click',(e) => {
function viewMenu() {
var menu = null;
$.ajax({
async: false,
url: '/menu',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: '',
type: 'post',
success: function(res){
menu = makeMenuLevel(res);
}
});
return menu;
}
console.log(viewMenu())
})
app.get('/menu', (req,res) => {
res.sendFile(__dirname + '/menu.html')
})
app.post('/menu', (req,res) => {
connection.query('SELECT * FROM menu', (err, result) => {
if(err) {
console.error(err);
return;
}
const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
var menu = [];
for(const row of result) {
if(row.parent_id === 0) {
menu.push(row);
continue;
}
const parent = index[row.parent_id];
if(!parent) {
console.warn(`Undefined parent with id ${row.parent_id}`);
continue;
}
if(!parent.children) {
parent.children = [];
}
parent.children.push(row);
}
});
})
app.post('/menu', (req,res) => {
// connection.query('SELECT * FROM menu', (err, result) => {
// if(err) {
// console.error(err);
// return;
// }
// const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
// var menu = [];
// for(const row of result) {
// if(row.parent_id === 0) {
// menu.push(row);
// continue;
// }
// const parent = index[row.parent_id];
// if(!parent) {
// console.warn(`Undefined parent with id ${row.parent_id}`);
// continue;
// }
// if(!parent.children) {
// parent.children = [];
// }
// parent.children.push(row);
// }
// });
var dataToSendToClient = {'message': 'error message from server'};
// convert whatever we want to send (preferably should be an object) to JSON
var JSONdata = JSON.stringify(dataToSendToClient);
res.send(JSONdata);
})
$(document).on('click',(e) => {
function imgName() {
var filename = null;
$.ajax({
async: false,
url: '/menu',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
type: 'post',
success: function(res){
filename = res;
}
});
return filename;
}
alert(imgName())
})
async function sendMenu() {
var menu = null;
connection.query('SELECT * FROM menu', (err, result) => {
if(err) {
console.error(err);
return;
}
const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
menu = [];
for(const row of result) {
if(row.parent_id === 0) {
menu.push(row);
continue;
}
const parent = index[row.parent_id];
if(!parent) {
console.warn(`Undefined parent with id ${row.parent_id}`);
continue;
}
if(!parent.children) {
parent.children = [];
}
parent.children.push(row);
}
});
return await menu;
}
console.log(sendMenu())
app.post('/menu', (req,res) => {
async function sendMenu() {
var menu = null;
connection.query('SELECT * FROM menu', (err, result) => {
if(err) {
console.error(err);
return;
}
const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
menu = [];
for(const row of result) {
if(row.parent_id === 0) {
menu.push(row);
continue;
}
const parent = index[row.parent_id];
if(!parent) {
console.warn(`Undefined parent with id ${row.parent_id}`);
continue;
}
if(!parent.children) {
parent.children = [];
}
parent.children.push(row);
}
});
return await menu;
}
console.log(sendMenu())
var message = {message: 'error message from server'};
// convert whatever we want to send (preferably should be an object) to JSON
res.send(message.message);
})
app.post('/menu', (req,res) => {
connection.query('SELECT * FROM menu', (err, result) => {
if(err) {
console.error(err);
return;
}
const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
var menu = [];
for(const row of result) {
if(row.parent_id === 0) {
menu.push(row);
continue;
}
const parent = index[row.parent_id];
if(!parent) {
console.warn(`Undefined parent with id ${row.parent_id}`);
continue;
}
if(!parent.children) {
parent.children = [];
}
parent.children.push(row);
res.send(menu)
}
});
// console.log(sendMenu())
// var message = {message: 'error message from server'};
// // convert whatever we want to send (preferably should be an object) to JSON
// res.send(message.message);
})
app.get('/menu', (req,res) => {
connection.query('SELECT * FROM menu', (err, result) => {
if(err) {
console.error(err);
return;
}
const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
var menu = [];
for(const row of result) {
if(row.parent_id === 0) {
menu.push(row);
continue;
}
const parent = index[row.parent_id];
if(!parent) {
console.warn(`Undefined parent with id ${row.parent_id}`);
continue;
}
if(!parent.children) {
parent.children = [];
}
parent.children.push(row);
res.send(menu)
res.sendFile(__dirname + '/menu.html')
}
});
// console.log(sendMenu())
// var message = {message: 'error message from server'};
// // convert whatever we want to send (preferably should be an object) to JSON
// res.send(message.message);
})
const express = require('express');
const session = require('express-session');
const FileStore = require('session-file-store')(session);
const passport = require('passport');
const app = express();
var http = require('http').createServer(app);
const io = require('socket.io')(http);
const port = 3000;
const bcrypt = require('bcrypt');
const saltRounds = 10;
app.use(express.static('/'))
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(
session({
secret: "secret",
store: new FileStore(),
cookie: {
path: "/",
httpOnly: true,
maxAge: 60 * 60 * 1000
},
resave: false,
saveUnitialized: false
})
);
const mysql = require('mysql');
var connection = mysql.createConnection({
/*socketPath: "/var/run/mysqld/mysqld.sock",
user: "c13183_tenders_na4u_ru",
password: "",
database: "c13183_tenders_na4u_ru"*/
host: "localhost",
user: "root",
password: "",
database: "test"
});
require('./config');
app.use(passport.initialize());
app.use(passport.session());
const logout = (req,res,next) => {
if(req.isAuthenticated()) {
return res.redirect('/admin');
} else {
next()
}
}
app.get('/',logout,(req, res) => res.sendFile(__dirname + '/index.html'));
io.on('connection', socket => {
console.log('user connected');
socket.on('disconnect', () => {
console.log(socket.id + ' disconnected')
})
});
app.post('/', (req, res, next) => {
passport.authenticate('local', function(err, user) {
if (err) {
return next(err);
}
if (!user) {
return res.redirect('/');
}
req.logIn(user, function(err) {
if (err) {
return next(err);
}
return res.redirect('/admin')
});
})(req, res, next);
});
app.post('/register', (req,res,next) => {
let event = req.body.event_name;
let email = req.body.email;
let firstname = req.body.firstname;
let password = req.body.password;
connection.query("SELECT * FROM users WHERE email = '" + email + "'", function(err,res){
if (res.length < 1) {
bcrypt.genSalt(saltRounds, function(err, salt) {
bcrypt.hash(password, salt, function(err, hash) {
connection.query("INSERT INTO users (event_name,firstname,email,password) VALUES('" + event + "','" + firstname + "','" + email + "','" + hash + "')", function(err,res) {
if(err) console.log(err);
else {
console.log(email)
}
});
});
});
}
else {
return console.log('user registered!');
}
});
return res.redirect('/')
});
app.post('/register_post', (req,res,next) => {
let event = req.body.event_name;
let email = req.body.email;
let firstname = req.body.firstname;
let password = req.body.password;
connection.query("SELECT * FROM users WHERE email = '" + email + "'", function(err,res){
if (res.length < 1) {
bcrypt.genSalt(saltRounds, function(err, salt) {
bcrypt.hash(password, salt, function(err, hash) {
connection.query("INSERT INTO users (event_name,firstname,email,password) VALUES('" + event + "','" + firstname + "','" + email + "','" + hash + "')", function(err,res) {
if(err) console.log(err);
else {
console.log(email)
}
});
});
});
}
else {
return console.log('user registered!');
}
});
});
app.get('/step2',(req, res) => {
res.sendFile(__dirname + '/step2.html')
})
const auth = (req,res,next) => {
if(req.isAuthenticated()) {
next()
} else {
return res.redirect('/');
}
}
app.get('/admin',auth, (req, res) => {
io.emit('user-id',req.user)
connection.query("SELECT * FROM the_company WHERE creator_id = '" + req.user + "'" , function(err,res){
io.emit('select_company', res.map(the_company_list => the_company_list))
})
res.sendFile(__dirname + '/login.html')
});
app.get('/logout',auth, (req,res) => {
req.logout();
res.redirect('/');
});
app.post('/create-tender',auth, (req,res) => {
let category = req.body.category;
let description = req.body.description;
let company = req.body.select_company;
connection.query("INSERT INTO tenders (category,description,company) VALUES('" + category + "','" + description + "','" + company + "')", function(err,res) {
if(err) console.log(err);
else {
console.log(description)
}
});
return res.redirect('/');
});
app.get('/tenders',auth, (req,res) => {
res.sendFile(__dirname + '/tenders.html')
connection.query("SELECT * FROM tenders", function(err,res){
const id = res.map(tenders => tenders.id);
const category = res.map(tenders => tenders.category);
const description = res.map(tenders => tenders.description);
const company = res.map(tenders => tenders.company);
const tender = {
id: id,
category: category,
description: description,
company: company
}
io.emit('tender',tender);
})
});
app.get('/menu', (req,res) => {
res.sendFile(__dirname + '/menu.html')
res.end()
})
app.get('/viewmenu', (req,res) => {
connection.query('SELECT * FROM menu', (err, result) => {
if(err) {
console.error(err);
return;
}
const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
var menu = [];
for(const row of result) {
if(row.parent_id === 0) {
menu.push(row);
continue;
}
const parent = index[row.parent_id];
if(!parent) {
console.warn(`Undefined parent with id ${row.parent_id}`);
continue;
}
if(!parent.children) {
parent.children = [];
}
parent.children.push(row);
res.send(menu)
}
});
})
app.post('/create-company',auth, (req,res) => {
let name = req.body.company_name;
let category = req.body.company_description;
let email = req.body.company_email;
let key = req.body.company_key;
let admin = req.body.company_admin;
let status = req.body.company_status;
let password = req.body.company_password;
connection.query("INSERT INTO the_company (name,category,email,c_key,admin,status,password,creator_id) VALUES('" + name + "','" + category + "','" + email + "','" + key + "','" + admin + "','" + status + "','" + password + "','" + req.user + "')", function(err,res) {
if(err) console.log(err);
else {
console.log(name)
}
});
return res.redirect('/');
});
app.get('/control-company',auth, (req,res) => {
connection.query("SELECT * FROM the_company WHERE creator_id = '" + req.user + "'" , function(err,res){
io.emit('the_company', res.map(the_company_list => the_company_list))
})
res.sendFile(__dirname + '/control-company.html')
})
app.post('/control-company',auth,(req,res) => {
let face = req.body.face,
physic = req.body.physic,
index = req.body.index,
phone = req.body.phone,
orgn = req.body.orgn,
creator = req.body.creator,
admin = req.body.admin,
bank = req.body.bank,
bik = req.body.bik,
rs = req.body.rs,
ks = req.body.ks,
id = req.body.id;
connection.query("UPDATE the_company SET face = '" + face + "', physic = '" + physic + "', indexx = '" + index + "', phone = '" + phone + "', orgn = '" + orgn + "', creator = '" + creator + "', admin = '" + admin + "', bank = '" + bank + "', bik = '" + bik + "', rs = '" + rs + "', ks = '" + ks + "' WHERE id = '" + id + "'", (req,res) => {
})
})
app.post('/find-company',auth,(req,res) => {
let id = req.body.id;
connection.query("SELECT * FROM the_company WHERE id = '" + id + "'" , function(err,res){
let find = res.map(find_company_list => find_company_list.face)
if(find != '') io.emit('find_company', res.map(find_company_list => find_company_list))
else io.emit('find_company', false)
})
})
app.post('/save_company',auth,(req,res) => {
let id = req.body.id;
let key = req.body.key;
console.log(id + '/' + key)
connection.query("SELECT * FROM the_company WHERE id = '" + id + "' AND c_key = '" + key + "'", function(err,res){
let c_key = res.map(find_company_list => find_company_list)
console.log(c_key)
if(c_key != '') io.emit('save_company', true)
else io.emit('save_company', false)
})
})
app.post('/find_password',auth,(req,res) => {
let id = req.body.id;
let password = req.body.password;
console.log(id + '/' + password)
connection.query("SELECT * FROM the_company WHERE id = '" + id + "' AND password = '" + password + "'", function(err,res){
let access = res.map(find_company_list => find_company_list)
console.log(access)
if(access != '') io.emit('access_password', true)
else io.emit('access_password', false)
})
})
http.listen(port, () => console.log(`Example app listening on port ${port}!`));