"use strict";(()=>{var e={};e.id=7889,e.ids=[7889],e.modules={47795:e=>{e.exports=require("generate-password")},27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},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,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,t){return t in r?r[t]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,t)):"function"==typeof r&&"default"===t?r:void 0}}})},66650:(e,r,t)=>{t.r(r),t.d(r,{config:()=>R,default:()=>y,routeModule:()=>A});var s={};t.r(s),t.d(s,{default:()=>g});var n=t(89947),a=t(2706),o=t(96762),i=t(6420),l=t.n(i),c=t(77538),u=t.n(c);t(27825);var p=t(33873),d=t.n(p),f=t(79646),m=t(51348),S=t.n(m),h=t(82703),E=t.n(h);t(29021);let _=t(47795);async function g(e,r){if("POST"!==e.method)return r.json({user:null,msg:"Registration Failed!"});let t=await l()(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{data:s,edit:n,grants:a}=e.body;try{let e=process.env.DSQL_DB_HOST||"127.0.0.1",n=`dsql_user_${t.id}`,{host:o,password:i}=s;if(!o.match(/./))throw Error("Invalid host");if(o.match(/^%$/))throw Error("Cannot set a catch-all host for this user.");let l=String(o).replace(/[^a-zA-Z0-9-\.\:\/\%]/g,""),c=i?.match(/./)?i:_.generate({length:16,numbers:!0,symbols:!0,uppercase:!0,exclude:"*#.'`\""}),u=await S()("SELECT * FROM mysql.user WHERE User = ? AND (Host = ? OR Host = ?)",[n,l,e]);if(u?.[0]?.User)throw Error("SQL User already exists. Remember if you have the '%' host on your primary user, it catches all other hosts.");if(l.match(/^%$/))throw Error("Can't create a catch-all host for this username. Catch-all host is only allowed on the primary user.");let p=await S()("SELECT COUNT(*) FROM mariadb_users WHERE user_id = ?",[t.id]);if(p?.[0]?.["COUNT(*)"]>=10)throw Error("User limit reached");let m=E()(c),h=await S()("INSERT INTO mariadb_users (user_id, username, host, password, grants) VALUES (?, ?, ?, ?, ?)",[t.id,n,l,m,JSON.stringify(a)]);if(!h?.insertId)throw Error("Error in adding SQL user");let g=d().resolve(process.cwd(),"./shell/mariadb-users");(0,f.execSync)(`node refreshUsersAndGrants.js --userId ${t.id} --username ${n} --host ${l}`,{cwd:g}),r.json({success:!0})}catch(e){console.log("Create MariaDB user ERROR:",e.message),u()({component:"/api/acceptUserInvitation/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Error in adding SQL user =>"+e.message,err:e.message})}}let y=(0,o.M)(s,"default"),R=(0,o.M)(s,"config"),A=new n.PagesAPIRouteModule({definition:{kind:a.A.PAGES_API,page:"/api/sql-users/create",pathname:"/api/sql-users/create",bundlePath:"",filename:""},userland:s})},77538:(e,r,t)=>{let s=t(29021),{IncomingMessage:n}=t(81630);e.exports=async function({user:e,message:r,component:t,noMail:n,req:a}){let o=new Date,i=(()=>{if(!a)return null;try{let e=a.headers["x-forwarded-for"],r=a.headers["x-real-ip"],t=a.headers["cf-connecting-ip"],s=Array.isArray(e)?e[0]:e?.split(",")[0],n=t||s||r||a.socket.remoteAddress;if(!n)return null;return String(n)}catch(e){return null}})();try{let n=`🚀 SERVER ERROR =========================== Error Message: ${r} Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(n+=` User Id: ${e?.id} User Name: ${e?.first_name} ${e?.last_name} User Email: ${e?.email}`),a?.url&&(n+=` URL: ${a.url}`),a?.body&&(n+=` Request Body: ${JSON.stringify(a.body,null,4)}`),i&&(n+=` IP: ${i}`),n+=` Date: ${o.toDateString()} ========================================`,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}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},6420:(e,r,t)=>{t(81630);let s=t(51348),n=t(72907),a=t(29021);async function o(e,r,t,o){if(!e.cookies?.datasquirelAuthKey?.match(/./))return null;let i=n(e.cookies.datasquirelAuthKey);if(!i)return null;let l=JSON.parse(i);if(!l.csrf_k||t&&!e.headers["x-csrf-auth"]?.match(RegExp(`${l.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&&!a.existsSync(`${c}/${l.csrf_k}`))return null;if(0==l.verification_status&&!t){let e=await s(`SELECT verification_status FROM users WHERE id='${l.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return l?.date&&Date.now()-l.date>6048e5?null:l}e.exports=o},72907:(e,r,t)=>{let{scryptSync:s,createDecipheriv:n}=t(55511),{Buffer:a}=t(79428);e.exports=e=>{let r=n("aes-192-cbc",s(process.env.DSQL_ENCRYPTION_PASSWORD||"",process.env.DSQL_ENCRYPTION_SALT||"",24),a.alloc(16,0));try{let t=r.update(e,"hex","utf8");return t+=r.final("utf8")}catch(e){return null}}},82703:(e,r,t)=>{let{scryptSync:s,createCipheriv:n}=t(55511),{Buffer:a}=t(79428),o=t(48225);e.exports=(e,r,t)=>{let i=n("aes-192-cbc",s(r||process.env.DSQL_ENCRYPTION_PASSWORD||"",t||process.env.DSQL_ENCRYPTION_SALT||"",24),a.alloc(16,0));try{let r=i.update(e,"utf8","hex");return r+=i.final("hex")}catch(e){return o({component:"encrypt",message:e.message}),null}}},48225:(e,r,t)=>{let s=t(29021);e.exports=async function({user:e,message:r,component:t,noMail:n}){let a=`🚀 SERVER ERROR =========================== User Id: ${e?.id} User Name: ${e?.first_name} ${e?.last_name} User Email: ${e?.email} Error Message: ${r} Component: ${t} Date: ${Date()} ========================================`;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}`)}},51348:(e,r,t)=>{let s=t(9973),n=t(21580),a=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:n()}});async function o(...e){try{let r=await a.query(...e);return await a.end(),JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}}e.exports=o},21580:(e,r,t)=>{let s=t(29021);e.exports=function(){let e=process.env.DSQL_SSL_DIR;if(!e?.match(/./))return;let r=`${e}/ca-cert.pem`;if(!s.existsSync(r)){console.log(`${r} does not exist`);return}return{ca:s.readFileSync(`${e}/ca-cert.pem`)}}},2706:(e,r)=>{var t;Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}}),function(e){e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE"}(t||(t={}))},89947:(e,r,t)=>{e.exports=t(75600)}};var r=require("../../../webpack-api-runtime.js");r.C(e);var t=r(r.s=66650);module.exports=t})();