From b8725f33af34e9023fc70989432109b72f739b0c Mon Sep 17 00:00:00 2001 From: N0rdye Date: Wed, 28 Jun 2023 02:57:57 +0500 Subject: [PATCH] some fixes --- db.js | 3 +- index.js | 96 ++++++++++++++++++++++++++--- public/lib/fn.js | 106 +++++++++++++++++--------------- public/lib/inter.js | 38 +++++++++--- public/lib/jquery.cookie.js | 98 +---------------------------- public/lib/jquery.cookie.js.old | 96 +++++++++++++++++++++++++++++ views/header.ejs | 5 +- views/login.ejs | 2 +- views/main.ejs | 21 ++++--- views/project.ejs | 64 +++++++++++++++++++ views/static/start.ejs | 11 +++- 11 files changed, 366 insertions(+), 174 deletions(-) create mode 100644 public/lib/jquery.cookie.js.old create mode 100644 views/project.ejs diff --git a/db.js b/db.js index 19719e9..06cebd4 100644 --- a/db.js +++ b/db.js @@ -59,7 +59,8 @@ module.exports.gv = (table,key,value,callback) => { // set_in module.exports.sv = (table,key,value,ekey,evalue,callback) => { - logcon.query('UPDATE `'+table+'` SET `'+key+'` = "'+value+'" WHERE `'+ekey+'` = "'+evalue+'"', (err , res) => { + logcon.query("UPDATE `"+table+"` SET `"+key+"` = '"+value+"' WHERE `"+ekey+"` = '"+evalue+"'", (err , res) => { + // console.log("UPDATE `"+table+"` SET `"+key+"` = '"+value+"' WHERE `"+ekey+"` = '"+evalue+"'"); if (err) { console.log("sql err"); throw err; diff --git a/index.js b/index.js index 89225c7..4dda7d7 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const { } = require('express'); const { escapeSelector } = require('jquery'); const { name } = require('ejs'); +const { stringify } = require('uuid'); const app = express(); // const { @@ -41,7 +42,7 @@ app.post('/back_login', (req, res) => { db.gv("users","login",`'${ilogin}'`,(udata)=>{udata = udata[0]; if(ipass == udata["pass"]){ console.log(udata["uuid"]+" logged in by login & pass from "+cook["sid"]); - res.cookie("uuid",udata["uuid"],{maxAge:1000000,path:"/;SameSite=Strict"}); + res.cookie("uuid",udata["uuid"],{maxAge:1000000,path:"/;SameSite=Lax"}); // db.sv("users","sids",sids += inp["sid"]+";","uuid",udata["uuid"],()=>{}); db.nr("sids",'`sid`,`uid`',`'${cook["sid"]}','${udata["id"]}'`); @@ -150,7 +151,7 @@ app.post("/get_sid" , (req,res) =>{ let inp = req.body; let sid = func.get_uuid(inp["name"]); var week = 7 * 24 * 3600 * 1000; - res.cookie("sid",sid,{maxAge:(week),path:"/;SameSite=Strict"}); + res.cookie("sid",sid,{maxAge:(week),path:"/;SameSite=Lax"}); res.send({out:"good"}); }); @@ -159,12 +160,13 @@ app.post("/clear_sid" , (req,res) =>{ let inp = req.body; let cook = req.cookies; - if(cook["uuid"] != null && cook["sid"] != null){ - res.send({out:"good"}); - db.dl("sids","sid",`'${cook["sid"]}'`,() =>{ - console.log(cook["uuid"] + "logged out from "+cook["sid"]); - }); + // console.log(cook["uuid"],cook["sid"]); + if(cook["sid"] != null){ + // res.send({out:"good"}); + console.log(cook["uuid"] + " logged out from "+cook["sid"]); + db.dl("sids","sid",`'${cook["sid"]}'`,() =>{}); } + res.send({out:"good"}); }); app.post("/get_cr_uuid", (req,res) => { @@ -190,6 +192,75 @@ app.post("/get_cr_uuid", (req,res) => { } }) +app.post("/save_proj", (req,res) => { + let inp = req.body; + let cook = req.cookies; + let proj = inp["proj"]; + let pname = inp["name"]; + db.gv("users","uuid",`'${cook["uuid"]}'`, (udata)=>{ udata = udata[0] + db.gv("projects","uid",udata["id"],(pdata)=>{ + let projin = null; + // console.log(pdata); + pdata.forEach(projt => { + if(projt["name"] == pname && projt["uid"] == udata["id"]){ + projin = projt; + return; + } + }) + if(projin == null){ + // console.log("proj not in"); + // console.log(pname,udata["id"],proj); + db.nr("projects","`uid`,`name`,`body`",`'${udata["id"]}','${pname}','${proj}'`); + } else if (projin != null){ + db.sv("projects","body",proj,"id",projin["id"],()=>{}); + // console.log("proj in"); + } + console.log(`${udata["uuid"]} saved project ${projin["name"]} from ${cook["sid"]}`); + }) + + }) +}) + +app.post("/load_proj", (req,res) => { + let inp = req.body; + let cook = req.cookies; + if(cook['sid'] != null && cook['uuid'] != null){ + db.gv("users","uuid",`'${cook["uuid"]}'`,(udata)=>{ udata = udata[0]; + db.gv("projects","uid",udata["id"],(pdata)=>{ + let projt = null; + // console.log(inp["name"]); + pdata.forEach(proj => { + if(proj["name"] == inp["name"]){ + projt = proj; + } + }) + if (projt != null){ + console.log(`${udata["uuid"]} loaded project ${projt["name"]} from ${cook["sid"]}`); + res.send({out:"good",body:projt["body"]}); + } + }) + }) + } + else{ + res.send({out:"bad"}); + } +}) + +app.post("/get_projs", (req,res) => { + let inp = req.body; + let cook = req.cookies; + if(cook['sid'] != null && cook['uuid'] != null){ + db.gv("users","uuid",`'${cook["uuid"]}'`,(udata)=>{ udata = udata[0]; + db.gv("projects","uid",udata["id"],(pdata)=>{ + res.send({out:"good",body:pdata}); + }) + }) + } + else{ + res.send({out:"bad"}); + } +}) + // app.post("/set_cr_uuid", (req,res) => { // let inp = req.body; // if(inp["uuid"] != null && inp["sid"] != null){ @@ -230,6 +301,11 @@ app.get("/reg" , (req,res) =>{ res.render('reg'); }) +app.get("/proj/:name" , (req,res) =>{ + // res.cookie + res.render('project',{proj_name:req.params["name"]}); +}) + app.get("/login" , (req,res) =>{ res.render('login'); }) @@ -238,6 +314,10 @@ app.get("/main", (req,res) =>{ res.render('main'); }); +// app.get("/main/:id", (req,res) =>{ +// res.render('main'); +// }); + app.get('/', (req, res) => { if(req.cookies["uuid"] != null){ res.redirect('main'); @@ -248,7 +328,7 @@ app.get('/', (req, res) => { }); app.all('*', (req, res) => { - res.status(404).send('

404! Page not

'); + res.status(404).send('

404! Page not


go to main page'); }); app.listen(process.env.PORT || 3002, () => console.log("started")); \ No newline at end of file diff --git a/public/lib/fn.js b/public/lib/fn.js index 6c98046..ce15909 100644 --- a/public/lib/fn.js +++ b/public/lib/fn.js @@ -23,28 +23,28 @@ function log(nlogin,npass){ }); } +// function get_from_uuid(callback){ +// const uid = $.cookie("uuid"); +// const sid = $.cookie("sid"); +// $.post( "/get_cr_uuid", { uuid:uid,sid:sid }) +// .done(function( res ) { +// if (res["out"] == "good"){ +// callback(res["body"]) +// } +// else if (res["out"] == bad){ +// if (res["body"] == "expired"){ +// clear_ck(); +// } +// } +// }); +// } function get_from_uuid(callback){ const uid = $.cookie("uuid"); const sid = $.cookie("sid"); $.post( "/get_cr_uuid", { uuid:uid,sid:sid }) .done(function( res ) { if (res["out"] == "good"){ - callback(res["body"]) - } - else if (res["out"] == bad){ - if (res["body"] == "expired"){ - clear_ck(); - } - } - }); -} -function get_from_uuid(callback){ - const uid = $.cookie("uuid"); - const sid = $.cookie("sid"); - $.post( "/get_cr_uuid", { uuid:uid,sid:sid }) - .done(function( res ) { - if (res["out"] == "good"){ - console.log("good"); + // console.log("good"); callback(res["body"]) } else if (res["out"] == bad){ @@ -59,10 +59,10 @@ function log_by_sid() { // const uuid = $.cookie("uuid"); // const sid = $.cookie("sid"); // console.log("log"); - if($.cookie('sid') == null){ + if($.cookie('uuid') == null && $.cookie('sid') == null){ // get_sid(location.hostname); - clear_ck(false); - }else{ + get_sid(location.hostname); + }else if ($.cookie('sid') != null && $.cookie('uuid') != null){ $.post( "/sid_log") .done(function( res ) { console.log("ping"); @@ -70,57 +70,59 @@ function log_by_sid() { goto(res["url"]); } else if (res["out"] == "bad"){ - clear_ck(); + clear_ck(false); } })} } function clear_ck(redirect = true){ console.log("sid"); - $.cookie("uuid",null); - $.cookie("sid",null); - get_sid(location.hostname); - if (redirect) goto("login"); + $.post( "/clear_sid") + .done(function( res ) { + console.log("sid"); + $.cookie("uuid",null); + $.cookie("sid",null); + $.removeCookie("uuid"); + $.removeCookie('sid'); + console.log("clear"); + get_sid(location.hostname); + if(res["out"] == "good"){ + if (redirect) goto("/login"); + } + }) } -function check_sid(){ + +function check_sid(redirect = true){ console.log("checking sid"); if($.cookie('sid') == null || $.cookie('uuid') == null){ - clear_ck(); + clear_ck(redirect); } else{ $.post( "/sid_log") .done(function( res ) { if(res["out"] == "bad"){ - clear_ck(); + clear_ck(redirect); } }) } } -function logout(quet = false) { - if (!quet) - { - let dialog = confirm("logout?"); - if(dialog){ - $.post( "/clear_sid") - .done(function( res ) { - if(res["out"] == "good"){ - clear_ck(); - } - }) - } - } - else{ - $.post( "/clear_sid") - .done(function( res ) { - if(res["out"] == "good"){ - clear_ck(); - } - }) +function logout(redirect = true) { + let dialog = confirm("logout?"); + if(dialog){ + clear_ck(redirect); } } +function goto_proj(name){ + $.post( "/proj/"+name, { name:hostname }) + .done(function( res ) { + // if(res["out"] == "good"){ + // console.log(res["body"]); + // } + }); +} function get_sid(hostname){ $.post( "/get_sid", { name:hostname }) @@ -131,7 +133,15 @@ function get_sid(hostname){ }); } - +function load_projs(callback){ + $.post( "/get_projs") + .done(function( res ) { + if(res["out"] == "good"){ + // console.log(res["body"]); + callback(res["body"]); + } + }); +} // redirect function goto(url) { diff --git a/public/lib/inter.js b/public/lib/inter.js index f40881b..ef5138c 100644 --- a/public/lib/inter.js +++ b/public/lib/inter.js @@ -1,10 +1,6 @@ window.dragMoveListener = dragMoveListener let root = document.getElementById("drags"); var objs = {}; -if ($.cookie("objs") != null){ - load_local(); -} - function add(obj){ if (objs[obj] == null) objs[obj] = {}; @@ -13,7 +9,7 @@ function add(obj){ // console.log(obj,objs[obj+"_count"],objs[obj+"_s"]); root.innerHTML += "
"+obj+"
"; objs[obj][obj+"_"+count] = {}; - objs[obj]["count"]+=1; + // objs[obj]["count"]+=1; console.log(objs); } @@ -21,12 +17,14 @@ function create(clas,obj,x,y,inside){ if (inside == null || inside == "") inside = "[]"; root.innerHTML += "
"+inside+"
"; let obj_doc = document.getElementById(obj); + console.log(obj_doc.classList); set_pos(obj_doc,x,y); } -function load_local(){ - objs = JSON.parse($.cookie("objs")); - // console.log(objs); +function load_local(objs){ + // objs = JSON.parse($.cookie("objs")); + console.log(objs); + globalThis.objs = objs; Object.entries(objs).forEach(([keys, values]) => { // console.log(keys,values); Object.entries(values).forEach(([key, value]) => { @@ -38,8 +36,30 @@ function load_local(){ }); } +function load_proj(){ + $.post( "/load_proj",{name:proj_name}) + .done(function( res ) { + if(res["out"] == "good"){ + console.log("good"); + // console.log(JSON.parse(`'${res["body"]}'`)); + // console.log(JSON.parse(res["body"])); + // $.cookie("objs",res["body"]); + load_local(JSON.parse(res["body"])); + } + }) +} + function save(){ - $.cookie("objs",JSON.stringify(objs),{path:"/;SameSite=Strict"}); + // let proj_name = document.getElementById("proj_name").value; + // $.cookie("objs",JSON.stringify(objs)); + console.log(objs); + // // console.log(objs); + $.post( "/save_proj", {proj:JSON.stringify(objs),name:proj_name}) + .done(function( res ) { + if(res["out"] == "good"){ + console.log("good"); + } + }) } function set_pos(obj,x,y){ diff --git a/public/lib/jquery.cookie.js b/public/lib/jquery.cookie.js index 6df1fac..c0f19d8 100644 --- a/public/lib/jquery.cookie.js +++ b/public/lib/jquery.cookie.js @@ -1,96 +1,2 @@ -/** - * Cookie plugin - * - * Copyright (c) 2006 Klaus Hartl (stilbuero.de) - * Dual licensed under the MIT and GPL licenses: - * http://www.opensource.org/licenses/mit-license.php - * http://www.gnu.org/licenses/gpl.html - * - */ - -/** - * Create a cookie with the given name and value and other optional parameters. - * - * @example $.cookie('the_cookie', 'the_value'); - * @desc Set the value of a cookie. - * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true }); - * @desc Create a cookie with all available options. - * @example $.cookie('the_cookie', 'the_value'); - * @desc Create a session cookie. - * @example $.cookie('the_cookie', null); - * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain - * used when the cookie was set. - * - * @param String name The name of the cookie. - * @param String value The value of the cookie. - * @param Object options An object literal containing key/value pairs to provide optional cookie attributes. - * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. - * If a negative value is specified (e.g. a date in the past), the cookie will be deleted. - * If set to null or omitted, the cookie will be a session cookie and will not be retained - * when the the browser exits. - * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). - * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). - * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will - * require a secure protocol (like HTTPS). - * @type undefined - * - * @name $.cookie - * @cat Plugins/Cookie - * @author Klaus Hartl/klaus.hartl@stilbuero.de - */ - -/** - * Get the value of a cookie with the given name. - * - * @example $.cookie('the_cookie'); - * @desc Get the value of a cookie. - * - * @param String name The name of the cookie. - * @return The value of the cookie. - * @type String - * - * @name $.cookie - * @cat Plugins/Cookie - * @author Klaus Hartl/klaus.hartl@stilbuero.de - */ -jQuery.cookie = function(name, value, options) { - if (typeof value != 'undefined') { // name and value given, set cookie - options = options || {}; - if (value === null) { - value = ''; - options.expires = -1; - } - var expires = ''; - if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { - var date; - if (typeof options.expires == 'number') { - date = new Date(); - date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); - } else { - date = options.expires; - } - expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE - } - // CAUTION: Needed to parenthesize options.path and options.domain - // in the following expressions, otherwise they evaluate to undefined - // in the packed version for some reason... - var path = options.path ? '; path=' + (options.path) : ''; - var domain = options.domain ? '; domain=' + (options.domain) : ''; - var secure = options.secure ? '; secure' : ''; - document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); - } else { // only name given, get cookie - var cookieValue = null; - if (document.cookie && document.cookie != '') { - var cookies = document.cookie.split(';'); - for (var i = 0; i < cookies.length; i++) { - var cookie = jQuery.trim(cookies[i]); - // Does this cookie string begin with the name we want? - if (cookie.substring(0, name.length + 1) == (name + '=')) { - cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); - break; - } - } - } - return cookieValue; - } -}; \ No newline at end of file +/*! jquery.cookie v1.4.1 | MIT */ +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?a(require("jquery")):a(jQuery)}(function(a){function b(a){return h.raw?a:encodeURIComponent(a)}function c(a){return h.raw?a:decodeURIComponent(a)}function d(a){return b(h.json?JSON.stringify(a):String(a))}function e(a){0===a.indexOf('"')&&(a=a.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/g,"\\"));try{return a=decodeURIComponent(a.replace(g," ")),h.json?JSON.parse(a):a}catch(b){}}function f(b,c){var d=h.raw?b:e(b);return a.isFunction(c)?c(d):d}var g=/\+/g,h=a.cookie=function(e,g,i){if(void 0!==g&&!a.isFunction(g)){if(i=a.extend({},h.defaults,i),"number"==typeof i.expires){var j=i.expires,k=i.expires=new Date;k.setTime(+k+864e5*j)}return document.cookie=[b(e),"=",d(g),i.expires?"; expires="+i.expires.toUTCString():"",i.path?"; path="+i.path:"",i.domain?"; domain="+i.domain:"",i.secure?"; secure":""].join("")}for(var l=e?void 0:{},m=document.cookie?document.cookie.split("; "):[],n=0,o=m.length;o>n;n++){var p=m[n].split("="),q=c(p.shift()),r=p.join("=");if(e&&e===q){l=f(r,g);break}e||void 0===(r=f(r))||(l[q]=r)}return l};h.defaults={},a.removeCookie=function(b,c){return void 0===a.cookie(b)?!1:(a.cookie(b,"",a.extend({},c,{expires:-1})),!a.cookie(b))}}); \ No newline at end of file diff --git a/public/lib/jquery.cookie.js.old b/public/lib/jquery.cookie.js.old new file mode 100644 index 0000000..6df1fac --- /dev/null +++ b/public/lib/jquery.cookie.js.old @@ -0,0 +1,96 @@ +/** + * Cookie plugin + * + * Copyright (c) 2006 Klaus Hartl (stilbuero.de) + * Dual licensed under the MIT and GPL licenses: + * http://www.opensource.org/licenses/mit-license.php + * http://www.gnu.org/licenses/gpl.html + * + */ + +/** + * Create a cookie with the given name and value and other optional parameters. + * + * @example $.cookie('the_cookie', 'the_value'); + * @desc Set the value of a cookie. + * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true }); + * @desc Create a cookie with all available options. + * @example $.cookie('the_cookie', 'the_value'); + * @desc Create a session cookie. + * @example $.cookie('the_cookie', null); + * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain + * used when the cookie was set. + * + * @param String name The name of the cookie. + * @param String value The value of the cookie. + * @param Object options An object literal containing key/value pairs to provide optional cookie attributes. + * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. + * If a negative value is specified (e.g. a date in the past), the cookie will be deleted. + * If set to null or omitted, the cookie will be a session cookie and will not be retained + * when the the browser exits. + * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). + * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). + * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will + * require a secure protocol (like HTTPS). + * @type undefined + * + * @name $.cookie + * @cat Plugins/Cookie + * @author Klaus Hartl/klaus.hartl@stilbuero.de + */ + +/** + * Get the value of a cookie with the given name. + * + * @example $.cookie('the_cookie'); + * @desc Get the value of a cookie. + * + * @param String name The name of the cookie. + * @return The value of the cookie. + * @type String + * + * @name $.cookie + * @cat Plugins/Cookie + * @author Klaus Hartl/klaus.hartl@stilbuero.de + */ +jQuery.cookie = function(name, value, options) { + if (typeof value != 'undefined') { // name and value given, set cookie + options = options || {}; + if (value === null) { + value = ''; + options.expires = -1; + } + var expires = ''; + if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { + var date; + if (typeof options.expires == 'number') { + date = new Date(); + date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); + } else { + date = options.expires; + } + expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE + } + // CAUTION: Needed to parenthesize options.path and options.domain + // in the following expressions, otherwise they evaluate to undefined + // in the packed version for some reason... + var path = options.path ? '; path=' + (options.path) : ''; + var domain = options.domain ? '; domain=' + (options.domain) : ''; + var secure = options.secure ? '; secure' : ''; + document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); + } else { // only name given, get cookie + var cookieValue = null; + if (document.cookie && document.cookie != '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = jQuery.trim(cookies[i]); + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) == (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } +}; \ No newline at end of file diff --git a/views/header.ejs b/views/header.ejs index c9cd21f..b69d1d5 100644 --- a/views/header.ejs +++ b/views/header.ejs @@ -9,9 +9,11 @@ } #top_panel_right{ float: right; + display: flex; } #top_panel_left{ float: left; + display: flex; } /* button{ /* margin-inline: 5px; */ @@ -24,7 +26,8 @@ let right = document.getElementById("top_panel_right"); let left = document.getElementById("top_panel_left"); - left.innerHTML += res["login"]; + if(document.title != "main") left.innerHTML += ``; + left.innerHTML += `
${res["login"]}
`; if (res["admin"] == true){ right.innerHTML += ""; diff --git a/views/login.ejs b/views/login.ejs index ac6fbe2..3b62a69 100644 --- a/views/login.ejs +++ b/views/login.ejs @@ -26,7 +26,7 @@ + -
+
-
-
+ - - + - - + <%- include('./static/end.ejs') %> \ No newline at end of file diff --git a/views/project.ejs b/views/project.ejs new file mode 100644 index 0000000..95996da --- /dev/null +++ b/views/project.ejs @@ -0,0 +1,64 @@ + + +<%- include('./static/start.ejs',{name:proj_name,async: true}) %> +<%- include('./header.ejs') %> + + + +
+ +
+
+ + + + + + + +<%- include('./static/end.ejs') %> \ No newline at end of file diff --git a/views/static/start.ejs b/views/static/start.ejs index 8e7da05..eb36bf2 100644 --- a/views/static/start.ejs +++ b/views/static/start.ejs @@ -10,8 +10,15 @@ <%= name %> + - -