"use strict";(()=>{var e={};e.id=7214,e.ids=[7214],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")},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")},21820:e=>{e.exports=require("os")},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}}})},29750:(e,t,r)=>{r.r(t),r.d(t,{config:()=>y,default:()=>h,routeModule:()=>b});var s={};r.r(s),r.d(s,{default:()=>g});var a=r(89947),n=r(2706),l=r(96762),i=r(33364),o=r.n(i),c=r(77538),u=r.n(c),p=r(6420),d=r.n(p),m=r(57638),S=r.n(m);let f=r(29021),_=r(21820);r(33873);let{execSync:D}=r(79646);async function g(e,t){if("POST"!==e.method)return t.json({msg:"Failed!"});let r=await d()(e,t,!0);if(!r)return t.json({success:!1,msg:"Unauthorized"});let{id:s,db_name:a,db_slug:n,db_description:l,db_image:i,db_full_name:c,active_clone:p,active_clone_parent_db:m}=e.body;c?.match(/datasquirel_user_.*/)||t.json({success:!1,msg:"Unauthorized"});let g=`datasquirel_user_${r.id}_${n}`;if(await o()({dbFullName:"datasquirel",tableName:"user_databases",identifierColumnName:"id",identifierValue:s,data:{db_name:a,db_slug:n,db_description:l,db_image:i,db_full_name:g}}),c&&c!==g){console.log("Renaming Database");let e=_.platform().match(/win/i)?"'C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe'":"mysql",t=_.platform().match(/win/i)?"'C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump.exe'":"mysqldump";try{await S()({queryString:`CREATE DATABASE \`${g}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`,database:c});let r={cwd:process.cwd()};_.platform().match(/win/i)&&(r.shell="bash.exe");let s=`${t} -u ${process.env.DSQL_DB_USERNAME} -h ${process.env.DSQL_DB_HOST} -p${process.env.DSQL_DB_PASSWORD} ${c} > tmpDbFile.sql`;console.log(`updateUserDatabase.js exe => ${s}`),D(s,r),D(`${e} -u ${process.env.DSQL_DB_USERNAME} -h ${process.env.DSQL_DB_HOST} -p${process.env.DSQL_DB_PASSWORD} ${g} < tmpDbFile.sql`,r),await S()({queryString:`DROP DATABASE \`${c}\``,database:c})}catch(e){u()({component:"/api/updateUserDatabase/lines-88-111",message:e.message,user:r})}}try{let e=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${r.id}/main.json`,s=JSON.parse(f.readFileSync(e,"utf8")),o=s.filter(e=>e.dbFullName===c);if(o[0].dbName=a,o[0].dbFullName=g,o[0].dbDescription=l,o[0].dbImage=i,o[0].dbSlug=n,p){let e=s.filter(e=>e.dbFullName===o[0].childDatabaseDbFullName)[0],t=e.childrenDatabases?.find(e=>e.dbFullName===c);t&&(t.dbFullName=g)}f.writeFileSync(`${e}`,JSON.stringify(s),"utf8"),t.json({success:!0})}catch(e){u()({component:"/api/updateUserDatabase/main-catch-error",message:e.message,user:r}),t.json({success:!1,msg:"Database Files could not be written!"})}}let h=(0,l.M)(s,"default"),y=(0,l.M)(s,"config"),b=new a.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/updateUserDatabase",pathname:"/api/updateUserDatabase",bundlePath:"",filename:""},userland:s})},77538:(e,t,r)=>{let s=r(29021),{IncomingMessage:a}=r(81630);e.exports=async function({user:e,message:t,component:r,noMail:a,req:n}){let l=new Date,i=(()=>{if(!n)return null;try{let e=n.headers["x-forwarded-for"],t=n.headers["x-real-ip"],r=n.headers["cf-connecting-ip"],s=Array.isArray(e)?e[0]:e?.split(",")[0],a=r||s||t||n.socket.remoteAddress;if(!a)return null;return String(a)}catch(e){return null}})();try{let a=`🚀 SERVER ERROR =========================== Error Message: ${t} Component: ${r}`;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}`),n?.url&&(a+=` URL: ${n.url}`),n?.body&&(a+=` Request Body: ${JSON.stringify(n.body,null,4)}`),i&&(a+=` IP: ${i}`),a+=` Date: ${l.toDateString()} ========================================`,s.existsSync("./.tmp/error.log")||s.writeFileSync("./.tmp/error.log","","utf-8");let o=s.readFileSync("./.tmp/error.log","utf-8");s.writeFileSync("./.tmp/error.log",a),s.appendFileSync("./.tmp/error.log",` ${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},6420:(e,t,r)=>{r(81630);let s=r(51348),a=r(72907),n=r(29021);async function l(e,t,r,l){if(!e.cookies?.datasquirelAuthKey?.match(/./))return null;let i=a(e.cookies.datasquirelAuthKey);if(!i)return null;let o=JSON.parse(i);if(!o.csrf_k||r&&!e.headers["x-csrf-auth"]?.match(RegExp(`${o.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(r&&!n.existsSync(`${c}/${o.csrf_k}`))return null;if(0==o.verification_status&&!r){let e=await s(`SELECT verification_status FROM users WHERE id='${o.id}'`);e&&e[0]&&1==e[0].verification_status&&t.setHeader("Set-Cookie",["user_refresh=1"])}return o?.date&&Date.now()-o.date>6048e5?null:o}e.exports=l},33364:(e,t,r)=>{let s=r(82703),a=r(72583),n=r(16570),l=r(51348),i=r(21339);async function o({dbContext:e,paradigm:t,dbFullName:r,tableName:o,data:c,tableSchema:u,identifierColumnName:p,identifierValue:d,encryptionKey:m,encryptionSalt:S}){if(!c||!Object.keys(c).length)return null;let f=!e?.match(/dsql.user/i)&&(!r||!!r.match(/^datasquirel$/)),_=f?l:i,D=Object.keys(c),g=[],h=[];for(let e=0;ee.fieldName===t):null,i=l&&l[0]?l[0]:null;if(null==r||void 0==r)continue;i?.richText&&(r=a(r,n)),i?.encrypted&&(r=s(r,m,S)),"object"==typeof r&&(r=JSON.stringify(r)),i?.pattern&&!new RegExp(i.pattern,i.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"}}),g.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}g.push(`date_updated='${Date()}'`),g.push(`date_updated_code='${Date.now()}'`);let y=`UPDATE ${o} SET ${g.join(",")} WHERE \`${p}\`=?`;return h.push(d),f?await _(y,h):await _({paradigm:t,database:r,queryString:y,queryValues:h})}e.exports=o},72907:(e,t,r)=>{let{scryptSync:s,createDecipheriv:a}=r(55511),{Buffer:n}=r(79428);e.exports=e=>{let t=a("aes-192-cbc",s(process.env.DSQL_ENCRYPTION_PASSWORD||"",process.env.DSQL_ENCRYPTION_SALT||"",24),n.alloc(16,0));try{let r=t.update(e,"hex","utf8");return r+=t.final("utf8")}catch(e){return null}}},30003:e=>{e.exports=/^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/},82703:(e,t,r)=>{let{scryptSync:s,createCipheriv:a}=r(55511),{Buffer:n}=r(79428),l=r(48225);e.exports=(e,t,r)=>{let i=a("aes-192-cbc",s(t||process.env.DSQL_ENCRYPTION_PASSWORD||"",r||process.env.DSQL_ENCRYPTION_SALT||"",24),n.alloc(16,0));try{let t=i.update(e,"utf8","hex");return t+=i.final("hex")}catch(e){return l({component:"encrypt",message:e.message}),null}}},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"]}}},6061:(e,t,r)=>{let s=r(72907),a=r(30003);e.exports=async function({unparsedResults:e,tableSchema:t}){let r=[];try{for(let n=0;n",t.message),e}}},48225:(e,t,r)=>{let s=r(29021);e.exports=async function({user:e,message:t,component:r,noMail:a}){let n=`🚀 SERVER ERROR =========================== User Id: ${e?.id} User Name: ${e?.first_name} ${e?.last_name} User Email: ${e?.email} Error Message: ${t} Component: ${r} Date: ${Date()} ========================================`;s.existsSync("./.tmp/error.log")||s.writeFileSync("./.tmp/error.log","","utf-8");let l=s.readFileSync("./.tmp/error.log","utf-8");s.writeFileSync("./.tmp/error.log",n),s.appendFileSync("./.tmp/error.log",` ${l}`)}},57638:(e,t,r)=>{r(29021);let s=r(6061),a=r(48225),n=r(51348),l=r(21339);e.exports=async function({queryString:e,queryValuesArray:t,database:r,tableSchema:i}){let o;let c=!!r?.match(/^datasquirel$/),u=c?n:l;try{o=e&&t&&Array.isArray(t)&&t[0]?c?await u(e,t):await u({paradigm:"Full Access",database:r,queryString:e,queryValues:t}):c?await u(e):await u({paradigm:"Full Access",database:r,queryString:e})}catch(e){a({component:"varDatabaseDbHandler/lines-29-32",message:e.message})}if(o&&i)try{let e=o;return await s({unparsedResults:e,tableSchema:i})}catch(e){return console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",r,e),a({component:"varDatabaseDbHandler/lines-52-53",message:e.message}),null}else if(o)return o;else return null}},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 l(...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=l},21339:(e,t,r)=>{let s=r(29021);r(33873);let a=r(9973),n=r(21580),l=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:i}){try{return new Promise((o,c)=>{let u=!!e?.match(/full.access|^fa$/i);try{function p(e){l.end(),o(JSON.parse(JSON.stringify(e)))}function d(e){l.end(),o({error:e.message,queryStringGenerated:r,queryValuesGenerated:i,sql:e.sql})}l=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()}}),i&&Array.isArray(i)&&i[0]?l.query(r,i).then(p).catch(d):l.query(r).then(p).catch(d)}catch(e){s.appendFileSync("./.tmp/dbErrorLogs.txt",e.message+"\n"+Date()+"\n\n\n","utf8"),o({error:e.message})}})}catch(e){return{success:!1,error:e.message}}}},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=t(t.s=29750);module.exports=r})();