16 lines
7.6 KiB
JavaScript
16 lines
7.6 KiB
JavaScript
"use strict";(()=>{var e={};e.id=538,e.ids=[538],e.modules={27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},72583:e=>{e.exports=require("sanitize-html")},9973:e=>{e.exports=require("serverless-mysql")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},69653:(e,r,t)=>{t.r(r),t.d(r,{config:()=>S,default:()=>E,routeModule:()=>g});var a={};t.r(a),t.d(a,{default:()=>f});var s=t(89947),l=t(2706),o=t(96762),n=t(67619),i=t(69842),d=t(27825),u=t.n(d),c=t(48786),p=t(33170);async function _({tbls:e,user:r,query:t}){let a;if(!t?.delegated)return{isPriviledged:!0,rootUserId:r.id};try{for(let t=0;t<e.length;t++){let s=e[t],l=await (0,c.A)(`SELECT * FROM delegated_user_tables WHERE delegated_user_id='${r.id}' AND \`database\`='${s.db}' AND \`table\`='${s.table}'`);if(!l?.[0]?.root_user_id)throw Error("Unauthorized!");let o=l[0];a||(a=o.root_user_id)}return{isPriviledged:!0,rootUserId:a}}catch(e){return{isPriviledged:!1,rootUserId:a,error:e.message}}}var m=t(4820);async function f(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,i.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{paradigm:a,src:s,dst:l}=e.body;try{let o=await _({tbls:[{db:s.db,table:s.table},{db:l.db,table:l.table}],user:t,query:e.query});if(!o.rootUserId||!o.isPriviledged)throw Error(o.error||"Unauthorized!");let n=(0,m.A)({userId:o.rootUserId});if(!n)throw Error("User Schema Data not Found!");let i=`${process.env.DSQL_USER_DB_PREFIX}${o.rootUserId}_${l.db}`,d=`${process.env.DSQL_USER_DB_PREFIX}${o.rootUserId}_${s.db}`,f=n.find(e=>e.dbFullName==i),E=n.find(e=>e.dbFullName==d),S=f?.tables.find(e=>e.tableName==l.table),g=E?.tables.find(e=>e.tableName==s.table);if(!S||!g)throw Error("Operation Failed! Tables Schemas not present.");if(!u().isEqual(g.fields,S.fields))throw Error("Tables incompatible");switch(a){case"Push":let y=await (0,c.A)(`SELECT * FROM ${d}.${s.table}`),$=await (0,p.A)({dbName:l.db,paradigm:"import",payload:y,tableName:l.table,userId:o.rootUserId,payloadType:"object"});return await b({dbName:l.db,tableName:l.table,data:y,userId:o.rootUserId}),r.json({success:!0,payload:$});case"Pull":let v=await (0,c.A)(`SELECT * FROM ${i}.${l.table}`),N=await (0,p.A)({dbName:s.db,paradigm:"import",payload:v,tableName:s.table,userId:o.rootUserId,payloadType:"object"});return await b({dbName:s.db,tableName:s.table,data:v,userId:o.rootUserId}),r.json({success:!0,payload:N})}r.json({success:!1})}catch(e){console.log(e.message),(0,n.default)({component:"/api/sync-table-data",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}async function b({dbName:e,tableName:r,data:t,userId:a}){let s=`${process.env.DSQL_USER_DB_PREFIX}${a}_${e.replace(/ /g,"")}`,l=t.map(e=>e.id),o=`DELETE FROM ${s}.${r}`;l?.[0]&&(o+=` WHERE id NOT IN (${l.join(",")})`),await (0,c.A)(o)}let E=(0,o.M)(a,"default"),S=(0,o.M)(a,"config"),g=new s.PagesAPIRouteModule({definition:{kind:l.A.PAGES_API,page:"/api/sync-table-data",pathname:"/api/sync-table-data",bundlePath:"",filename:""},userland:a})},33170:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(98e3),s=t(67619),l=t(48786);async function o({paradigm:e,userId:r,dbName:t,tableName:o,payload:n,payloadType:i}){let d={};try{let s=`${process.env.DSQL_USER_DB_PREFIX}${r}_${t.replace(/ /g,"")}`;switch(e){case"export":let u=await (0,l.A)(`SELECT * FROM \`${s}\`.\`${o}\``);d.tableData=u;break;case"import":let c="base64"==i?Buffer.from(n,"base64").toString():n,p="object"===i?n:JSON.parse(c);for(let e=0;e<p.length;e++){let r=p[e],t=await (0,a.A)({data:r,dbFullName:s,tableName:o,dbContext:"Dsql User",paradigm:"Full Access",duplicateColumnName:"id",duplicateColumnValue:r?.id,update:!0});if(t.error)throw Error(t.error)}break;default:return null}return d}catch(e){return(0,s.default)({component:"/functions/backend/importExportTableDataFn",message:e.message}),null}}},69842:(e,r,t)=>{t.d(r,{A:()=>i});var a=t(48786),s=t(95969),l=t(29021),o=t.n(l),n=t(89159);async function i(e,r,t){let{keyCookieName:l,csrfCookieName:i}=(0,n.A)();if(!e.cookies?.[l]?.match(/./))return null;let d=(0,s.A)({encryptedString:e.cookies[l]});if(!d)return null;let u=JSON.parse(d);if(!u.csrf_k||t&&!Object.keys(e.headers).find(e=>e.includes(u.csrf_k)))return null;let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!o().existsSync(`${c}/${u.csrf_k}`))return null;if(0==u.verification_status&&!t){let e=await (0,a.A)(`SELECT verification_status FROM users WHERE id='${u.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return u?.date&&Date.now()-u.date>6048e5?null:u}},89159:(e,r,t)=>{t.d(r,{A:()=>a});function a(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",a=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",s=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",l=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),o=r;e?.userId&&(o+=`user_${e.userId}_`),l&&(o+=`${l}_`),o+=t;let n=r;e?.userId&&(n+=`user_${e.userId}_`),l&&(n+=`${l}_`),n+=a;let i=r;return e?.userId&&(i+=`user_${e.userId}_`),l&&(i+=`${l}_`),{keyCookieName:o,csrfCookieName:n,oneTimeCodeName:i+=s}}},4820:(e,r,t)=>{t.d(r,{A:()=>i});var a=t(67619),s=t(29021),l=t.n(s),o=t(33873),n=t.n(o);function i({userId:e}){try{let r=n().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(l().readFileSync(r,"utf-8"))}catch(e){return(0,a.default)({component:"grabUserSchemaData",message:e.message}),null}}},67619:(e,r,t)=>{t.r(r),t.d(r,{default:()=>l});var a=t(29021),s=t.n(a);async function l({user:e,message:r,component:t,noMail:a,req:l}){let o=new Date,n=(()=>{if(!l)return null;try{let e=l.headers["x-forwarded-for"],r=l.headers["x-real-ip"],t=l.headers["cf-connecting-ip"],a=Array.isArray(e)?e[0]:e?.split(",")[0],s=t||a||r||l.socket.remoteAddress;if(!s)return null;return String(s)}catch(e){return null}})();try{let a=`🚀 SERVER ERROR ===========================
|
|
Error Message: ${r}
|
|
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(a+=`
|
|
User Id: ${e?.id}
|
|
User Name: ${e?.first_name} ${e?.last_name}
|
|
User Email: ${e?.email}`),l?.url&&(a+=`
|
|
URL: ${l.url}`),l?.body&&(a+=`
|
|
Request Body: ${JSON.stringify(l.body,null,4)}`),n&&(a+=`
|
|
IP: ${n}`),a+=`
|
|
Date: ${o.toDateString()}
|
|
========================================`,s().existsSync("./.tmp/error.log")||s().writeFileSync("./.tmp/error.log","","utf-8");let i=s().readFileSync("./.tmp/error.log","utf-8");s().writeFileSync("./.tmp/error.log",a),s().appendFileSync("./.tmp/error.log",`
|
|
|
|
|
|
|
|
|
|
${i}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>l});var a=t(55511),s=t(79428);function l({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let l=r||process.env.DSQL_ENCRYPTION_PASSWORD,o=t||process.env.DSQL_ENCRYPTION_SALT,n=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!l?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!o?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let i=(0,a.scryptSync)(l,o,n),d=s.Buffer.alloc(16,0),u=(0,a.createDecipheriv)("aes-192-cbc",i,d);try{let r=u.update(e,"hex","utf8");return r+=u.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),a=r.X(0,[1251],()=>t(69653));module.exports=a})(); |