dsql-admin/dsql-app/.local_dist/server/pages/api/updateUserDatabase.js

16 lines
13 KiB
JavaScript
Raw Normal View History

2024-12-08 08:57:48 +00:00
"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:()=>h,default:()=>E,routeModule:()=>y});var s={};r.r(s),r.d(s,{default:()=>g});var n=r(89947),a=r(2706),o=r(96762),l=r(33364),i=r.n(l),c=r(48225),u=r.n(c),p=r(6420),d=r.n(p),_=r(57638),S=r.n(_);let m=r(29021),f=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:n,db_slug:a,db_description:o,db_image:l,db_full_name:c,active_clone:p,active_clone_parent_db:_}=e.body;c?.match(/datasquirel_user_.*/)||t.json({success:!1,msg:"Unauthorized"});let g=`datasquirel_user_${r.id}_${a}`;if(await i()({dbFullName:"datasquirel",tableName:"user_databases",identifierColumnName:"id",identifierValue:s,data:{db_name:n,db_slug:a,db_description:o,db_image:l,db_full_name:g}}),c&&c!==g){console.log("Renaming Database");let e=f.platform().match(/win/i)?"'C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe'":"mysql",t=f.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()};f.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(m.readFileSync(e,"utf8")),i=s.filter(e=>e.dbFullName===c);if(i[0].dbName=n,i[0].dbFullName=g,i[0].dbDescription=o,i[0].dbImage=l,i[0].dbSlug=a,p){let e=s.filter(e=>e.dbFullName===i[0].childDatabaseDbFullName)[0],t=e.childrenDatabases?.find(e=>e.dbFullName===c);t&&(t.dbFullName=g)}m.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 E=(0,o.M)(s,"default"),h=(0,o.M)(s,"config"),y=new n.PagesAPIRouteModule({definition:{kind:a.A.PAGES_API,page:"/api/updateUserDatabase",pathname:"/api/updateUserDatabase",bundlePath:"",filename:""},userland:s})},6420:(e,t,r)=>{r(81630);let s=r(51348),n=r(72835),a=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=n({encryptedString:e.cookies[i]});if(!u)return null;let p=JSON.parse(u);if(!p.csrf_k||r&&!e.headers["x-csrf-auth"]?.match(RegExp(`${p.csrf_k}`)))return null;let d=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!d)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(r&&!a.existsSync(`${d}/${p.csrf_k}`))return null;if(0==p.verification_status&&!r){let e=await s(`SELECT verification_status FROM users WHERE id='${p.id}'`);e&&e[0]&&1==e[0].verification_status&&t.setHeader("Set-Cookie",["user_refresh=1"])}return p?.date&&Date.now()-p.date>6048
2024-12-05 07:03:33 +00:00
Error Message: ${t}
2024-12-06 13:24:26 +00:00
Component: ${r}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(n+=`
2024-12-05 07:03:33 +00:00
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
2024-12-06 13:24:26 +00:00
User Email: ${e?.email}`),a?.url&&(n+=`
URL: ${a.url}`),a?.body&&(n+=`
2024-12-08 08:57:48 +00:00
Request Body: ${JSON.stringify(a.body,null,4)}`),l&&(n+=`
IP: ${l}`),n+=`
2024-12-06 13:24:26 +00:00
Date: ${o.toDateString()}
2024-12-08 08:57:48 +00:00
========================================`,s.existsSync("./.tmp/error.log")||s.writeFileSync("./.tmp/error.log","","utf-8");let i=s.readFileSync("./.tmp/error.log","utf-8");s.writeFileSync("./.tmp/error.log",n),s.appendFileSync("./.tmp/error.log",`
2024-11-05 11:12:42 +00:00
2024-12-08 08:57:48 +00:00
${i}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},57638:(e,t,r)=>{r(29021);let s=r(6061),n=r(48225),a=r(51348),o=r(21339),l=r(44900);e.exports=async function({queryString:e,queryValuesArray:t,database:r,tableSchema:i,useLocal:c}){let u;let p=!!r?.match(/^datasquirel$/),d=c?l:p?a:o;try{u=e&&t&&Array.isArray(t)&&t[0]?p?await d(e,t):await d({paradigm:"Full Access",database:r,queryString:e,queryValues:t}):p?await d(e):await d({paradigm:"Full Access",database:r,queryString:e})}catch(e){n({component:"varDatabaseDbHandler/lines-29-32",message:e.message})}if(u&&i)try{let e=u;return await s({unparsedResults:e,tableSchema:i})}catch(e){return console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",r,e),n({component:"varDatabaseDbHandler/lines-52-53",message:e.message}),null}else if(u)return u;else return null}},72835:(e,t,r)=>{let{scryptSync:s,createDecipheriv:n}=r(55511),{Buffer:a}=r(79428);e.exports=({encryptedString:e,encryptionKey:t,encryptionSalt:r})=>{if(!e?.match(/./))return console.log("Encrypted 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("Decrption key is invalid"),e;if(!l?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let c=n("aes-192-cbc",s(o,l,i),a.alloc(16,0));try{let t=c.update(e,"hex","utf8");return t+=c.final("utf8")}catch(t){return console.log("Error in decrypting =>",t.message),e}}},20503:(e,t,r)=>{let{scryptSync:s,createCipheriv:n}=r(55511),{Buffer:a}=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=n("aes-192-cbc",s(o,l,i),a.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),n=r(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 t=await a.query(...e);return await a.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 n=r(9973),a=r(21580),o=n({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:a()}});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 p(e){o.end(),i(JSON.parse(JSON.stringify(e)))}function d(e){o.end(),i({error:e.message,queryStringGenerated:r,queryValuesGenerated:l,sql:e.sql})}o=u?n({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:a()}}):n({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:a()}}),l&&Array.isArray(l)&&l[0]?o.query(r,l).then(p).catch(d):o.query(r).then(p).catch(d)}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),n=r(21580);async function a(...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.DSQ