puzzle/func.js
n0rdye f2bbded81b modified: admin.js
modified:   func.js
	modified:   index.js
	modified:   object.js
	modified:   project.js
	new file:   public/img/drop.png
	new file:   public/img/icon/back.png
	new file:   public/img/icon/copy.png
	new file:   public/img/icon/del.png
	new file:   public/img/icon/download.png
	new file:   public/img/icon/forw.png
	new file:   public/img/icon/save.png
	modified:   public/lib/fn.js
	modified:   public/lib/inter.js
	modified:   views/admin.ejs
	new file:   views/admin/objects.ejs
	modified:   views/admin/objects/object_creation.ejs
	modified:   views/admin/objects/object_edit.ejs
	modified:   views/admin/objects/object_groups.ejs
	new file:   views/admin/users.ejs
	modified:   views/admin/users/user_reg.ejs
	modified:   views/header.ejs
	modified:   views/load.ejs
	modified:   views/login.ejs
	modified:   views/main.ejs
	modified:   views/project.ejs
	modified:   views/static/end.ejs
	modified:   views/static/start.ejs
2023-08-15 23:59:02 +05:00

141 lines
5.4 KiB
JavaScript

// import { v4 as uuidv4, v6 as uuidv6 } from 'uuid';
const uuid = require('uuid');
// const bcrypt = require('bcryptjs');
const cryptojs = require('crypto-js');
const e = require('express');
const fs = require('fs');
const db = require('./db');
const moment = require("moment");
const vars = require('./vars');
module.exports.sendfile = (fileName, response) => {
const filePath = "./"+fileName;
fs.exists(filePath, function (exists) {
if (exists) {
response.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-Disposition": "attachment; filename=" + fileName
});
fs.createReadStream(filePath).pipe(response);
return;
}
response.writeHead(400, { "Content-Type": "text/plain" });
response.end("ERROR File does not exist");
});
}
module.exports.sid = (cook,res,callback,auto = true,admin_check = false)=>{
try {
let uuid = cook["uuid"];
let sid = cook["sid"];
if(cook["uuid"] != null && cook["sid"] != null){
db.ggv("sids","`uid`","sid",`'${sid}'`,(sdata)=>{ sdata = sdata[0]
// console.log(sdata);
if(sdata != null){
db.ggv("users","`uuid`,`id`, `admin`","id",`'${sdata["uid"]}'`,(udata)=>{ udata = udata[0]
if (udata != null && udata["id"] == sdata["uid"] && uuid == udata["uuid"]){
if (!admin_check) callback(true);
else if (admin_check && udata["admin"] != false){
db.gv("admins","uid",`'${udata["id"]}'`,(adata)=>{ adata = adata[0]
if(adata != null){
callback(adata["rights"],true);
}
else{
db.sv("users","admin","0","id",udata["id"],()=>{},true);
db.dl("admins","uid",udata["id"],()=>{},true);
if(auto) res.redirect('/');
else if(!auto) callback(false);
}
},true)
}
else{
db.sv("users","admin","0","id",udata["id"],()=>{},true);
db.dl("admins","uid",udata["id"],()=>{},true);
if(auto) res.redirect('/');
else if(!auto) callback(false);
}
}
else{
if(auto) res.send({out:"bad",err:"wrong"});
else if(!auto) callback(false);
}
});
}
else{
if(auto) res.send({out:"bad",err:"expired"});
else if(!auto) callback(false);
}
});
}else{
if(auto) res.send({out:"bad",err:"nocr"});
else if(!auto) callback(false);
}
} catch (error) {
this.log("backend sid checking err0r - "+error);
}
}
module.exports.log = (message) =>{
message = message.replaceAll("'","*")
var date = moment().format('YYYY-MM-DD');
var time = moment().format('hh:mm:ss');
let clog = `${date}_${time}|${message}`;
console.log(clog);
// if(vars.log_to_file) fs.appendFile('./logs.txt', `${clog} \n`, function (err) {if (err) throw err;});
if(vars.log_to_db) db.nr("logs","`date`,`time`,`log`",`'${date}','${time}','${message}'`,true);
}
module.exports.logs_file = (res)=>{
let path = './logs.txt';
fs.exists(path, function(exists) {
if(exists) {
fs.unlink(path,function(err){
if(err) throw err;
write_logs(res);
});
} else {
write_logs(res);
}
});
function write_logs(res){
db.gav("logs","0",(db_logs)=>{
db_logs.forEach(log => {
let date = moment(log[`date_time`]).utc().format('YYYY-MM-DD');
fs.appendFile(path, `${date}_${log["time"]}|${log["log"]} \n`, function (err) {
if (err) throw err;
if(log["id"] == db_logs.at(-1)["id"]){
res.download(path, (err) => {
if (err) { throw err; }
console.log("logs downloaded");
fs.unlink(path, (err) => {
if (err) { throw err; }
});
});
}
});
});
},true);
}
}
module.exports.get_uuid = (name = "/") =>{
if (name != "/") name = this.encrypt(name,"name");
let unid = uuid.v4() + '%%'+name+'#e'+(621);
return unid;
}
module.exports.encrypt = (text,cipher) => {
// return bcrypt.hashSync(password,bcrypt.genSaltSync());
return cryptojs.AES.encrypt(text,cipher).toString();
}
module.exports.decrypt = (text,cipher) =>{
const en = cryptojs.AES.decrypt(text,cipher);
const de = en.toString(cryptojs.enc.Utf8);
// console.log(de + "-dec");
return de;
}