1 line
11 KiB
JavaScript
1 line
11 KiB
JavaScript
"use strict";(()=>{var e={};e.id=4371,e.ids=[4371],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")},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}}})},81147:(e,t,r)=>{r.r(t),r.d(t,{config:()=>m,default:()=>E,routeModule:()=>b});var a={};r.r(a),r.d(a,{default:()=>S});var s=r(89947),n=r(2706),l=r(96762),o=r(48786),i=r(31495),u=r(60125),c=r(67619),d=r(69842),_=r(30802),f=r(76856);let p=r(29021);r(33873);let{execSync:g}=r(79646);async function S(e,t){if("POST"!==e.method)return t.json({msg:"Failed!"});let r=await (0,d.A)(e,t,!0);if(!r)return t.json({success:!1,msg:"Unauthorized"});let{dbFullName:a,dbSlug:s,dbId:n,tableNameOld:l,payload:g,delegated:S}=e.body,{tableName:E,tableFullName:m,tableDescription:b}=g;if(!E?.match(/./)||E.match(/[^a-zA-Z0-9-_]/))return t.json({success:!1,msg:"Bad Input"});try{let d=await (0,u.A)({databaseSlug:s,request:e,user:r,query:e.query});if(!await (0,i.A)({user:r,confirmedDelegetedUser:d,database:s,table:l,dbId:n,priviledgeRegex:/Edit Table/i}))return console.log("User Unauthorized"),t.json({success:!1,msg:"Unauthorized"});let g=d?.rootUserId?d.rootUserId:r.id,h=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${g}/main.json`,y=JSON.parse(p.readFileSync(h,"utf8")),A=y.filter(e=>e.dbFullName===a)[0].tables.filter((e,t)=>{if(e.tableName===l)return!0});if(A&&A[0])A[0].tableName=E,A[0].tableNameOld=l,A[0].tableFullName=m,b&&(A[0].tableDescription=b),A[0].childrenTables&&A[0].childrenTables.forEach(e=>{try{y.filter(t=>t.dbFullName===e.dbNameFull)[0].tables.filter(t=>{if(t.tableName===e.tableName)return!0})[0].childTableName=E}catch(e){(0,c.default)({component:"/api/updateUserTable/lines-131-138",message:e.message,user:r})}});else throw Error("Table Does not exist");p.writeFileSync(`${h}`,JSON.stringify(y),"utf8"),await (0,_.A)({dbFullName:"datasquirel",tableName:"user_database_tables",identifierColumnName:"table_slug",identifierValue:l,data:{table_name:m,table_slug:E,table_description:b}}),S&&d&&await (0,o.A)("UPDATE delegated_user_tables SET `table`=? WHERE delegated_user_id=? AND root_user_id=? AND `table`=? AND `database`=?",[E,r.id,d.rootUserId,l,s]);let N=a.replace(/[^a-z\_0-9]/g,"");await (0,f.A)({userId:g,targetDatabase:N}),t.json({success:!0})}catch(e){(0,c.default)({component:"/api/updateUserTable/main-catch-error",message:e.message,user:r}),t.json({success:!1,msg:"Database Files could not be written!"})}}let E=(0,l.M)(a,"default"),m=(0,l.M)(a,"config"),b=new s.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/updateUserTable",pathname:"/api/updateUserTable",bundlePath:"",filename:""},userland:a})},31495:(e,t,r)=>{r.d(t,{A:()=>n});var a=r(48786),s=r(67619);async function n({user:e,confirmedDelegetedUser:t,database:r,table:n,priviledgeRegex:l,dbId:o}){let i,u;try{t?.delegated?(i=await (0,a.A)("SELECT priviledge FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=? AND `database`=? AND `table`=?",[t.rootUserId,e.id,r,n]),u=i[0]?.priviledge?.match(l)):(i=await (0,a.A)("SELECT table_slug FROM user_database_tables WHERE user_id=? AND table_slug=? AND db_id=?",[e.id,n,o]),u=!0)}catch(t){(0,s.default)({component:"checkUserRights",message:t.message,user:e})}return!!i&&!!i[0]&&!!u}},60125:(e,t,r)=>{r.d(t,{A:()=>s});var a=r(95969);async function s({request:e,databaseSlug:t,user:r,query:s}){try{let n=r.id,l=null;if(!s?.delegated)return{dbUserId:n};let o=s.dbUserId,i=`${process.env.DSQL_USER_DB_PREFIX}${o}_${t}`,u=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${i}`;try{if(!e.cookies?.[u])throw Error("Cookie not present");let r=(0,a.A)({encryptedString:e.cookies[u]});if(!r)throw Error("Invalid Token");if((l=JSON.parse(r)).databaseSlug===t)return{dbUserId:n=l.rootUserId,rootUserId:l.rootUserId,rootUserName:l.rootUserName,rootUserEmail:l.rootUserEmail,rootUserImage:l.rootUserImage,databaseFullName:l.databaseFullName,databaseSlug:l.databaseSlug,allowedTables:l.allowedTables,priviledges:l.priviledges,database:l.databaseSlug,delegated:!0}}catch(e){}return{dbUserId:n}}catch(e){return null}}},69842:(e,t,r)=>{r.d(t,{A:()=>i});var a=r(48786),s=r(95969),n=r(29021),l=r.n(n),o=r(89159);async function i(e,t,r){let{keyCookieName:n,csrfCookieName:i}=(0,o.A)();if(!e.cookies?.[n]?.match(/./))return null;let u=(0,s.A)({encryptedString:e.cookies[n]});if(!u)return null;let c=JSON.parse(u);if(!c.csrf_k||r&&!Object.keys(e.headers).find(e=>e.includes(c.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&&!l().existsSync(`${d}/${c.csrf_k}`))return null;if(0==c.verification_status&&!r){let e=await (0,a.A)(`SELECT verification_status FROM users WHERE id='${c.id}'`);e&&e[0]&&1==e[0].verification_status&&t.setHeader("Set-Cookie",["user_refresh=1"])}return c?.date&&Date.now()-c.date>6048e5?null:c}},89159:(e,t,r)=>{r.d(t,{A:()=>a});function a(e){let t=process.env.DSQL_COOKIES_PREFIX||"dsql_",r=process.env.DSQL_COOKIES_KEY_NAME||"key",a=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",s=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",n=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),l=t;e?.userId&&(l+=`user_${e.userId}_`),n&&(l+=`${n}_`),l+=r;let o=t;e?.userId&&(o+=`user_${e.userId}_`),n&&(o+=`${n}_`),o+=a;let i=t;return e?.userId&&(i+=`user_${e.userId}_`),n&&(i+=`${n}_`),{keyCookieName:l,csrfCookieName:o,oneTimeCodeName:i+=s}}},30802:(e,t,r)=>{r.d(t,{A:()=>c});var a=r(72583),s=r.n(a),n=r(92652),l=r(48786),o=r(37275),i=r(68829),u=r(7971);async function c({dbContext:e,paradigm:t,dbFullName:r,tableName:a,data:c,tableSchema:d,identifierColumnName:_,identifierValue:f,encryptionKey:p,encryptionSalt:g,useLocal:S}){if(!c||!Object.keys(c).length)return null;let E=!!S||!e?.match(/dsql.user/i)&&(!r||!!r.match(/^datasquirel$/)),m=S?u.A:E?l.A:o.default,b=Object.keys(c),h=[],y=[];for(let e=0;e<b.length;e++)try{let t=b[e],r=c[t],a=d?d?.fields?.filter(e=>e.fieldName===t):null,l=a&&a[0]?a[0]:null;if(null==r||void 0==r)continue;let o=/<[^>]+>/g;(l?.richText||String(r).match(o))&&(r=s()(r,n.A)),l?.encrypted&&(r=(0,i.default)({data:r,encryptionKey:p,encryptionSalt:g})),"object"==typeof r&&(r=JSON.stringify(r)),l?.pattern&&!new RegExp(l.pattern,l.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"}}),h.push(`\`${t}\`=?`),"number"==typeof r?y.push(String(r)):y.push(r)}catch(e){console.log("DSQL: Error in parsing data keys in update function =>",e.message);continue}h.push(`date_updated='${Date()}'`),h.push(`date_updated_code='${Date.now()}'`);let A=`UPDATE \`${r}\`.\`${a}\` SET ${h.join(",")} WHERE \`${_}\`=?`;return y.push(f),E?await m(A,y):await m({paradigm:t,queryString:A,queryValues:y})}},92652:(e,t,r)=>{r.d(t,{A:()=>a});let a={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"]}}},68829:(e,t,r)=>{r.r(t),r.d(t,{default:()=>n});var a=r(55511),s=r(79428);function n({data:e,encryptionKey:t,encryptionSalt:r}){if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let n=t||process.env.DSQL_ENCRYPTION_PASSWORD,l=r||process.env.DSQL_ENCRYPTION_SALT,o=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!n?.match(/.{8,}/))return console.log("Encryption key is invalid"),e;if(!l?.match(/.{8,}/))return console.log("Encryption salt is invalid"),e;let i=(0,a.scryptSync)(n,l,o),u=s.Buffer.alloc(16,0),c=(0,a.createCipheriv)("aes-192-cbc",i,u);try{let t=c.update(e,"utf8","hex");return t+=c.final("hex")}catch(t){return console.log("Error in encrypting =>",t.message),e}}},48786:(e,t,r)=>{r.d(t,{A:()=>a});async function a(...e){let t=global.DSQL_DB_CONN;try{if(!t)throw Error("No Connection provided to DB_HANDLER function!");let r=await t.query(...e);return JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}finally{await t?.end()}}},37275:(e,t,r)=>{async function a(e,t,r){try{if("string"==typeof t){let a=await e.query(s(t),r);return JSON.parse(JSON.stringify(a))}if("object"!=typeof t)return null;{let r=[];for(let a=0;a<t.length;a++)try{let n=t[a],l=await e.query(s(n.query),n.values);r.push(JSON.parse(JSON.stringify(l)))}catch(e){r.push(null)}return r}}catch(e){return null}finally{e.end()}}function s(e){return e.replace(/\n/gm,"").replace(/ {2,}/g,"").trim()}async function n({paradigm:e,queryString:t,queryValues:r}){try{switch(e){case"Read Only":return await a(global.DSQL_READ_ONLY_DB_CONN,t,r);case"Full Access":case"FA":return await a(global.DSQL_FULL_ACCESS_DB_CONN,t,r);default:return null}}catch(e){return console.log(`DSQL_USER_DB_HANDLER Error: ${e.message}`),null}}r.r(t),r.d(t,{default:()=>n})},7971:(e,t,r)=>{r.d(t,{A:()=>o});var a=r(9973),s=r.n(a),n=r(29021),l=r.n(n);async function o(...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:function(){let e=process.env.DSQL_SSL_DIR;if(!e?.match(/./))return;let t=`${e}/ca-cert.pem`;if(!l().existsSync(t)){console.log(`${t} does not exist`);return}return{ca:l().readFileSync(`${e}/ca-cert.pem`),rejectUnauthorized:!1}}()},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}}}},2706:(e,t)=>{Object.defineProperty(t,"A",{enumerable:!0,get:function(){return r}});var r=function(e){return e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE",e}({})},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),a=t.X(0,[2282],()=>r(81147));module.exports=a})(); |