"use strict";(()=>{var e={};e.id=8893,e.ids=[8893],e.modules={60818:e=>{e.exports=require("dotenv")},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")},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")},34932:(e,s,r)=>{r.r(s),r.d(s,{config:()=>R,default:()=>A,routeModule:()=>O});var a={};r.r(a),r.d(a,{default:()=>h});var t=r(89947),i=r(2706),n=r(96762),l=r(51348),u=r.n(l),o=r(48225),d=r.n(o),c=r(6420),_=r.n(c),p=r(47670),S=r.n(p),b=r(51926),m=r.n(b),E=r(19261),D=r.n(E);let f=r(29021);r(33873);let{execSync:N}=r(79646);async function h(e,s){if("POST"!==e.method)return s.json({msg:"Failed!"});let r=await _()(e,s,!0);if(!r)return s.json({success:!1,msg:"Unauthorized"});let a=await u()("SELECT * FROM users WHERE id = ?",[r.id]),t=a?.[0],{db_name:i,db_slug:n,db_description:l,db_image:o,duplicate:c,keepUpdated:p,keepDataUpdated:b}=e.body;if(n.match(/[^a-zA-Z0-9-_]/))return s.json({success:!1,msg:"Input Error"});let E=`datasquirel_user_${r.id}_${n}`,N=await m()(`CREATE DATABASE IF NOT EXISTS \`${E}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`);if(!N?.affectedRows)return s.json({success:!1,msg:"Database Could Not be created!"});let h={user_id:r.id,db_name:i,db_slug:n,db_full_name:E,db_description:l,db_image:o};c&&p&&(h.active_clone="1",h.active_clone_parent_db=c),c&&b&&(h.active_data="1");let A=await S()({dbFullName:"datasquirel",tableName:"user_databases",data:h});if(A.insertId)try{let e=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${r.id}/main.json`,a=JSON.parse(f.readFileSync(e,"utf8")),u={dbName:i,dbSlug:n,dbFullName:E,dbDescription:l,dbImage:o,tables:[]};if(c)try{let e=a.filter(e=>e.dbFullName===c)[0];u.tables=e.tables,p&&(u.childDatabase=!0,u.childDatabaseDbFullName=c,b&&(u.updateData=!0),e.childrenDatabases?e.childrenDatabases.push({dbFullName:E}):e.childrenDatabases=[{dbFullName:E}]);for(let e=0;e{r(81630);let a=r(51348),t=r(72835),i=r(29021),n=r(31341);async function l(e,s,r,l){let{keyCookieName:u,csrfCookieName:o}=n();if(!e.cookies?.[u]?.match(/./))return null;let d=t({encryptedString:e.cookies[u]});if(!d)return null;let c=JSON.parse(d);if(!c.csrf_k||r&&!e.headers["x-csrf-auth"]?.match(RegExp(`${c.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&&!i.existsSync(`${_}/${c.csrf_k}`))return null;if(0==c.verification_status&&!r){let e=await a(`SELECT verification_status FROM users WHERE id='${c.id}'`);e&&e[0]&&1==e[0].verification_status&&s.setHeader("Set-Cookie",["user_refresh=1"])}return c?.date&&Date.now()-c.date>6048e5?null:c}e.exports=l},31341:e=>{e.exports=function(e){let s=process.env.DSQL_COOKIES_PREFIX||"dsql_",r=process.env.DSQL_COOKIES_KEY_NAME||"key",a=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",t=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",i=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),n=s;e?.userId&&(n+=`user_${e.userId}_`),i&&(n+=`${i}_`),n+=r;let l=s;e?.userId&&(l+=`user_${e.userId}_`),i&&(l+=`${i}_`),l+=a;let u=s;return e?.userId&&(u+=`user_${e.userId}_`),i&&(u+=`${i}_`),{keyCookieName:n,csrfCookieName:l,oneTimeCodeName:u+=t}}},51926:(e,s,r)=>{let a=r(29021),t=r(48225),i=r(45356);e.exports=async function(e){let s;"production".match(/dev/)&&a.appendFileSync("./.tmp/sqlQuery.sql",e+"\n"+Date()+"\n\n\n","utf8");try{s=await i(e)}catch(e){t({component:"noDatabaseDbHandler",message:e.message}),console.log("ERROR in noDatabaseDbHandler =>",e.message)}return s||null}},45356:(e,s,r)=>{let a=r(9973),t=r(21580),i=a({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,charset:"utf8mb4",ssl:t()}});e.exports=function(...e){try{return new Promise((s,r)=>{i.query(...e).then(e=>{i.end(),s(JSON.parse(JSON.stringify(e)))}).catch(e=>{i.end(),s({error:e.message,sql:e.sql})})})}catch(e){return{success:!1,error:e.message}}}}};var s=require("../../webpack-api-runtime.js");s.C(e);var r=e=>s(s.s=e),a=s.X(0,[4050,2451],()=>r(34932));module.exports=a})();