"use strict";(()=>{var e={};e.id=4371,e.ids=[4371],e.modules={60818:e=>{e.exports=require("dotenv")},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")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},96762:(e,t)=>{Object.defineProperty(t,"M",{enumerable:!0,get:function(){return function e(t,r){return r in t?t[r]:"then"in t&&"function"==typeof t.then?t.then(t=>e(t,r)):"function"==typeof t&&"default"===r?t:void 0}}})},56363:(e,t,r)=>{r.r(t),r.d(t,{config:()=>A,default:()=>v,routeModule:()=>L});var s={};r.r(s),r.d(s,{default:()=>y});var a=r(89947),n=r(2706),o=r(96762),l=r(51348),i=r.n(l),c=r(73677),u=r.n(c),d=r(39831),_=r.n(d),p=r(48225),S=r.n(p),f=r(6420),g=r.n(f),E=r(33364),D=r.n(E),m=r(19261),b=r.n(m);let h=r(29021);r(33873);let{execSync:N}=r(79646);async function y(e,t){if("POST"!==e.method)return t.json({msg:"Failed!"});let r=await g()(e,t,!0);if(!r)return t.json({success:!1,msg:"Unauthorized"});let{dbFullName:s,dbSlug:a,dbId:n,tableNameOld:o,payload:l,delegated:c}=e.body,{tableName:d,tableFullName:p,tableDescription:f}=l;if(!d?.match(/./)||d.match(/[^a-zA-Z0-9-_]/))return t.json({success:!1,msg:"Bad Input"});try{let l=await _()({databaseSlug:a,request:e,user:r,query:e.query});if(!await u()({user:r,confirmedDelegetedUser:l,database:a,table:o,dbId:n,priviledgeRegex:/Edit Table/i}))return console.log("User Unauthorized"),t.json({success:!1,msg:"Unauthorized"});let g=l?.rootUserId?l.rootUserId:r.id,E=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${g}/main.json`,m=JSON.parse(h.readFileSync(E,"utf8")),N=m.filter(e=>e.dbFullName===s)[0].tables.filter((e,t)=>{if(e.tableName===o)return!0});if(N&&N[0])N[0].tableName=d,N[0].tableNameOld=o,N[0].tableFullName=p,f&&(N[0].tableDescription=f),N[0].childrenTables&&N[0].childrenTables.forEach(e=>{try{m.filter(t=>t.dbFullName===e.dbNameFull)[0].tables.filter(t=>{if(t.tableName===e.tableName)return!0})[0].childTableName=d}catch(e){S()({component:"/api/updateUserTable/lines-131-138",message:e.message,user:r})}});else throw Error("Table Does not exist");h.writeFileSync(`${E}`,JSON.stringify(m),"utf8"),await D()({dbFullName:"datasquirel",tableName:"user_database_tables",identifierColumnName:"table_slug",identifierValue:o,data:{table_name:p,table_slug:d,table_description:f}}),c&&l&&await i()("UPDATE delegated_user_tables SET `table`=? WHERE delegated_user_id=? AND root_user_id=? AND `table`=? AND `database`=?",[d,r.id,l.rootUserId,o,a]);let y=s.replace(/[^a-z\_0-9]/g,"");await b()({userId:g,targetDatabase:y}),t.json({success:!0})}catch(e){S()({component:"/api/updateUserTable/main-catch-error",message:e.message,user:r}),t.json({success:!1,msg:"Database Files could not be written!"})}}let v=(0,o.M)(s,"default"),A=(0,o.M)(s,"config"),L=new a.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/updateUserTable",pathname:"/api/updateUserTable",bundlePath:"",filename:""},userland:s})},73677:(e,t,r)=>{let s=r(51348),a=r(48225);e.exports=async function({user:e,confirmedDelegetedUser:t,database:r,table:n,priviledgeRegex:o,dbId:l}){let i,c;try{t?.delegated?(i=await s("SELECT priviledge FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=? AND `database`=? AND `table`=?",[t.rootUserId,e.id,r,n]),c=i[0]?.priviledge?.match(o)):(i=await s("SELECT table_slug FROM user_database_tables WHERE user_id=? AND table_slug=? AND db_id=?",[e.id,n,l]),c=!0)}catch(t){a({component:"checkUserRights",message:t.message,user:e})}return!!i&&!!i[0]&&!!c}},39831:(e,t,r)=>{r(81630);let s=r(72835);e.exports=async function({request:e,databaseSlug:t,user:r,query:a}){try{let n=r.id,o=null;if(!a?.delegated)return{dbUserId:n};let l=a.dbUserId,i=`${process.env.DSQL_USER_DB_PREFIX}${l}_${t}`,c=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${i}`;try{if(!e.cookies?.[c])throw Error("Cookie not present");let r=s({encryptedString:e.cookies[c]});if(!r)throw Error("Invalid Token");if((o=JSON.parse(r)).databaseSlug===t)return{dbUserId:n=o.rootUserId,rootUserId:o.rootUserId,rootUserName:o.rootUserName,rootUserEmail:o.rootUserEmail,rootUserImage:o.rootUserImage,databaseFullName:o.databaseFullName,databaseSlug:o.databaseSlug,allowedTables:o.allowedTables,priviledges:o.priviledges,database:o.databaseSlug,delegated:!0}}catch(e){}return{dbUserId:n}}catch(e){return null}}},6420:(e,t,r)=>{r(81630);let s=r(51348),a=r(72835),n=r(29021),o=r(31341);async function l(e,t,r,l){let{keyCookieName:i,csrfCookieName:c}=o();if(!e.cookies?.[i]?.match(/./))return null;let u=a({encryptedString:e.cookies[i]});if(!u)return null;let d=JSON.parse(u);if(!d.csrf_k||r&&!e.headers["x-csrf-auth"]?.match(RegExp(`${d.csrf_k}`)))return null;let _=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!_)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(r&&!n.existsSync(`${_}/${d.csrf_k}`))return null;if(0==d.verification_status&&!r){let e=await s(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&t.setHeader("Set-Cookie",["user_refresh=1"])}return d?.date&&Date.now()-d.date>6048e5?null:d}e.exports=l},31341:e=>{e.exports=function(e){let t=process.env.DSQL_COOKIES_PREFIX||"dsql_",r=process.env.DSQL_COOKIES_KEY_NAME||"key",s=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",a=e?.database||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),n=t;e?.userId&&(n+=`user_${e.userId}_`),a&&(n+=`${a}_`),n+=r;let o=t;return e?.userId&&(o+=`user_${e.userId}_`),a&&(o+=`${a}_`),{keyCookieName:n,csrfCookieName:o+=s}}},33364:(e,t,r)=>{let s=r(72583),a=r(16570),n=r(51348),o=r(21339),l=r(20503),i=r(44900);async function c({dbContext:e,paradigm:t,dbFullName:r,tableName:c,data:u,tableSchema:d,identifierColumnName:_,identifierValue:p,encryptionKey:S,encryptionSalt:f,useLocal:g}){if(!u||!Object.keys(u).length)return null;let E=!!g||!e?.match(/dsql.user/i)&&(!r||!!r.match(/^datasquirel$/)),D=g?i:E?n:o,m=Object.keys(u),b=[],h=[];for(let e=0;ee.fieldName===t):null,o=n&&n[0]?n[0]:null;if(null==r||void 0==r)continue;o?.richText&&(r=s(r,a)),o?.encrypted&&(r=l({data:r,encryptionKey:S,encryptionSalt:f})),"object"==typeof r&&(r=JSON.stringify(r)),o?.pattern&&!new RegExp(o.pattern,o.patternFlags||"").test(r)&&(console.log("DSQL: Pattern not matched =>",r),r=""),"string"==typeof r&&r.match(/^null$/i)&&(r={toSqlString:function(){return"NULL"}}),"string"!=typeof r||r.match(/./i)||(r={toSqlString:function(){return"NULL"}}),b.push(`\`${t}\`=?`),"number"==typeof r?h.push(String(r)):h.push(r)}catch(e){console.log("DSQL: Error in parsing data keys in update function =>",e.message);continue}b.push(`date_updated='${Date()}'`),b.push(`date_updated_code='${Date.now()}'`);let N=`UPDATE ${c} SET ${b.join(",")} WHERE \`${_}\`=?`;return h.push(p),E?await D(N,h):await D({paradigm:t,database:r,queryString:N,queryValues:h})}e.exports=c},16570:e=>{e.exports={allowedTags:["b","i","em","strong","a","p","span","ul","ol","li","h1","h2","h3","h4","h5","h6","img","div","button","pre","code","br"],allowedAttributes:{a:["href"],img:["src","alt","width","height","class","style"],"*":["style","class"]}}},20503:(e,t,r)=>{let{scryptSync:s,createCipheriv:a}=r(55511),{Buffer:n}=r(79428);e.exports=({data:e,encryptionKey:t,encryptionSalt:r})=>{if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let o=t||process.env.DSQL_ENCRYPTION_PASSWORD,l=r||process.env.DSQL_ENCRYPTION_SALT,i=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!o?.match(/.{8,}/))return console.log("Encryption key is invalid"),e;if(!l?.match(/.{8,}/))return console.log("Encryption salt is invalid"),e;let c=a("aes-192-cbc",s(o,l,i),n.alloc(16,0));try{let t=c.update(e,"utf8","hex");return t+=c.final("hex")}catch(t){return console.log("Error in encrypting =>",t.message),e}}},51348:(e,t,r)=>{let s=r(9973),a=r(21580),n=s({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4",ssl:a()}});async function o(...e){try{let t=await n.query(...e);return await n.end(),JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}}e.exports=o},21339:(e,t,r)=>{let s=r(29021);r(33873);let a=r(9973),n=r(21580),o=a({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_READ_ONLY_USERNAME,password:process.env.DSQL_DB_READ_ONLY_PASSWORD,charset:"utf8mb4",ssl:n()}});e.exports=function({paradigm:e,database:t,queryString:r,queryValues:l}){try{return new Promise((i,c)=>{let u=!!e?.match(/full.access|^fa$/i);try{function d(e){o.end(),i(JSON.parse(JSON.stringify(e)))}function _(e){o.end(),i({error:e.message,queryStringGenerated:r,queryValuesGenerated:l,sql:e.sql})}o=u?a({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_FULL_ACCESS_USERNAME,password:process.env.DSQL_DB_FULL_ACCESS_PASSWORD,database:t,ssl:n()}}):a({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_READ_ONLY_USERNAME,password:process.env.DSQL_DB_READ_ONLY_PASSWORD,database:t,ssl:n()}}),l&&Array.isArray(l)&&l[0]?o.query(r,l).then(d).catch(_):o.query(r).then(d).catch(_)}catch(e){s.appendFileSync("./.tmp/dbErrorLogs.txt",e.message+"\n"+Date()+"\n\n\n","utf8"),i({error:e.message})}})}catch(e){return{success:!1,error:e.message}}}},44900:(e,t,r)=>{let s=r(9973),a=r(21580);async function n(...e){let t=s({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4",ssl:a()},onConnect:()=>{console.log("Connection Successful!")},onConnectError:e=>{console.log("Connection Error",e.message)},onError:e=>{console.log("Client Error",e.message)}});console.log("Querying ...");try{let r=await t.query(...e);return await t.end(),JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e.message),{success:!1,error:e.message}}}e.exports=n},21580:(e,t,r)=>{let s=r(29021);e.exports=function(){let e=process.env.DSQL_SSL_DIR;if(!e?.match(/./))return;let t=`${e}/ca-cert.pem`;if(!s.existsSync(t)){console.log(`${t} does not exist`);return}return{ca:s.readFileSync(`${e}/ca-cert.pem`)}}},2706:(e,t)=>{var r;Object.defineProperty(t,"A",{enumerable:!0,get:function(){return r}}),function(e){e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE"}(r||(r={}))},89947:(e,t,r)=>{e.exports=r(75600)}};var t=require("../../webpack-api-runtime.js");t.C(e);var r=e=>t(t.s=e),s=t.X(0,[2451],()=>r(56363));module.exports=s})();