"use strict";(()=>{var e={};e.id=460,e.ids=[460],e.modules={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")},66912:(e,r,t)=>{t.r(r),t.d(r,{config:()=>p,default:()=>f,routeModule:()=>g});var s={};t.r(s),t.d(s,{default:()=>E});var a=t(89947),o=t(2706),i=t(96762),n=t(77377),d=t(98e3),l=t(31495),u=t(60125),c=t(67619),_=t(69842);async function E(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,_.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"User Auth Failed"});let{tableName:s,payload:a,tableSchema:o,dbSlug:i,dbId:E,delegated:f}=e.body;try{let c=await (0,u.A)({databaseSlug:i,request:e,user:t,query:e.query});if(!await (0,l.A)({user:t,confirmedDelegetedUser:c,database:i,table:s,dbId:E,priviledgeRegex:/Update Entries/i}))return r.json({success:!1,msg:"Unauthorized"});let _=c?.rootUserId?`datasquirel_user_${c.rootUserId}_${i}`:`datasquirel_user_${t.id}_${i}`,f=await (0,d.A)({dbContext:"Dsql User",paradigm:"Full Access",dbFullName:_,tableName:s,data:a,tableSchema:o});if(!f.insertId)throw console.log("Table Insertion Failed =>",f),Error(f?.error||"Table Insertion Failed");await (0,n.A)({dbName:_,entryId:f.insertId,entryOrder:a.order,tableName:s,tableSchema:o}),r.json({success:!0})}catch(e){(0,c.default)({component:"/api/addTableEntry/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!",err:e.message})}}let f=(0,i.M)(s,"default"),p=(0,i.M)(s,"config"),g=new a.PagesAPIRouteModule({definition:{kind:o.A.PAGES_API,page:"/api/addTableEntry",pathname:"/api/addTableEntry",bundlePath:"",filename:""},userland:s})},31495:(e,r,t)=>{t.d(r,{A:()=>o});var s=t(48786),a=t(67619);async function o({user:e,confirmedDelegetedUser:r,database:t,table:o,priviledgeRegex:i,dbId:n}){let d,l;try{r?.delegated?(d=await (0,s.A)("SELECT priviledge FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=? AND `database`=? AND `table`=?",[r.rootUserId,e.id,t,o]),l=d[0]?.priviledge?.match(i)):(d=await (0,s.A)("SELECT table_slug FROM user_database_tables WHERE user_id=? AND table_slug=? AND db_id=?",[e.id,o,n]),l=!0)}catch(r){(0,a.default)({component:"checkUserRights",message:r.message,user:e})}return!!d&&!!d[0]&&!!l}},60125:(e,r,t)=>{t.d(r,{A:()=>a});var s=t(95969);async function a({request:e,databaseSlug:r,user:t,query:a}){try{let o=t.id,i=null;if(!a?.delegated)return{dbUserId:o};let n=a.dbUserId,d=`${process.env.DSQL_USER_DB_PREFIX}${n}_${r}`,l=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${d}`;try{if(!e.cookies?.[l])throw Error("Cookie not present");let t=(0,s.A)({encryptedString:e.cookies[l]});if(!t)throw Error("Invalid Token");if((i=JSON.parse(t)).databaseSlug===r)return{dbUserId:o=i.rootUserId,rootUserId:i.rootUserId,rootUserName:i.rootUserName,rootUserEmail:i.rootUserEmail,rootUserImage:i.rootUserImage,databaseFullName:i.databaseFullName,databaseSlug:i.databaseSlug,allowedTables:i.allowedTables,priviledges:i.priviledges,database:i.databaseSlug,delegated:!0}}catch(e){}return{dbUserId:o}}catch(e){return null}}},77377:(e,r,t)=>{t.d(r,{A:()=>a});let s=t(37275);async function a({dbName:e,entryId:r,entryOrder:t,tableName:a,tableSchema:o}){try{if(o.fields.find(e=>!!e.fieldName?.match(/^order$/i))&&t){let o=await s({database:e,paradigm:"Full Access",queryString:`SELECT * FROM ${a} WHERE \`order\` = '${t}' AND id != ?`,queryValues:[String(r)]});if(!o?.[0])throw Error("No Existing Order");await s({database:e,paradigm:"Full Access",queryString:`UPDATE ${a} SET \`order\` = \`order\` + 1 WHERE \`order\` >= ${t} AND id != ?`,queryValues:[String(r)]})}return!0}catch(e){return!1}}},69842:(e,r,t)=>{t.d(r,{A:()=>d});var s=t(48786),a=t(95969),o=t(29021),i=t.n(o),n=t(89159);async function d(e,r,t){try{let{keyCookieName:o,csrfCookieName:d}=(0,n.A)();if(!e.cookies?.[o]?.match(/./))throw Error("Request Cookies not Found!");let l=(0,a.A)({encryptedString:e.cookies[o]});if(!l)throw Error("Couldn't decrypt auth cookie!");let u=JSON.parse(l);if(!u.csrf_k)throw Error("No `csrf_k` present in user Object!");if(t&&!Object.keys(e.headers).find(e=>e.includes(u.csrf_k)))throw Error("Deep Auth Failed. `csrf` key not found in request headers!");let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(t&&!i().existsSync(`${c}/${u.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==u.verification_status&&!t){let e=await (0,s.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"])}if(u?.date&&Date.now()-u.date>6048e5)throw Error("Key expired!");return u}catch(e){return console.log(`userAuth Error: ${e.message}`),null}}},89159:(e,r,t)=>{t.d(r,{A:()=>s});function s(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",s=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",a=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",o=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),i=r;e?.userId&&(i+=`user_${e.userId}_`),o&&(i+=`${o}_`),i+=t;let n=r;e?.userId&&(n+=`user_${e.userId}_`),o&&(n+=`${o}_`),n+=s;let d=r;return e?.userId&&(d+=`user_${e.userId}_`),o&&(d+=`${o}_`),{keyCookieName:i,csrfCookieName:n,oneTimeCodeName:d+=a}}},67619:(e,r,t)=>{t.r(r),t.d(r,{default:()=>o});var s=t(29021),a=t.n(s);async function o({user:e,message:r,component:t,noMail:s,req:o}){let i=new Date,n=(()=>{if(!o)return null;try{let e=o.headers["x-forwarded-for"],r=o.headers["x-real-ip"],t=o.headers["cf-connecting-ip"],s=Array.isArray(e)?e[0]:e?.split(",")[0],a=t||s||r||o.socket.remoteAddress;if(!a)return null;return String(a)}catch(e){return null}})();try{let s=`🚀 SERVER ERROR =========================== Error Message: ${r} Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(s+=` User Id: ${e?.id} User Name: ${e?.first_name} ${e?.last_name} User Email: ${e?.email}`),o?.url&&(s+=` URL: ${o.url}`),o?.body&&(s+=` Request Body: ${JSON.stringify(o.body,null,4)}`),n&&(s+=` IP: ${n}`),s+=` Date: ${i.toDateString()} ========================================`,a().existsSync("./.tmp/error.log")||a().writeFileSync("./.tmp/error.log","","utf-8");let d=a().readFileSync("./.tmp/error.log","utf-8");a().writeFileSync("./.tmp/error.log",s),a().appendFileSync("./.tmp/error.log",` ${d}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>o});var s=t(55511),a=t(79428);function o({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let o=r||process.env.DSQL_ENCRYPTION_PASSWORD,i=t||process.env.DSQL_ENCRYPTION_SALT,n=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!o?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!i?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let d=(0,s.scryptSync)(o,i,n),l=a.Buffer.alloc(16,0),u=(0,s.createDecipheriv)("aes-192-cbc",d,l);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),s=r.X(0,[1251],()=>t(66912));module.exports=s})();