16 lines
11 KiB
JavaScript
16 lines
11 KiB
JavaScript
"use strict";exports.id=4385,exports.ids=[4385],exports.modules={14385:(e,t,a)=>{let s=a(29021),r=a(44767),i=a(59474),{ServerResponse:n}=a(81630),l=a(33873),o=a(5194),u=a(57638),d=a(20503),c=a(47670),m=a(31341),_=a(44900);async function p({user:e,social_platform:t,res:a,invitation:s,database:i,additionalFields:n,useLocal:l}){let o=`SELECT * FROM users WHERE email='${e.email}' AND social_id='${e.social_id}' AND social_platform='${t}'`,c=l?await _(o):await u({database:i||"datasquirel",queryString:o});if(!c?.[0])return{success:!1,user:null};let p=Math.random().toString(36).substring(2)+"-"+Math.random().toString(36).substring(2),g={id:c[0].id,type:c[0].type||"",stripe_id:c[0].stripe_id||"",first_name:c[0].first_name,last_name:c[0].last_name,username:c[0].username,email:c[0].email,social_id:c[0].social_id,image:c[0].image,image_thumbnail:c[0].image_thumbnail,verification_status:c[0].verification_status,social_login:c[0].social_login,social_platform:c[0].social_platform,csrf_k:p,logged_in_status:!0,date:Date.now()};n&&Object.keys(n).length>0&&Object.keys(n).forEach(e=>{g[e]=c[0][e]});let E=d({data:JSON.stringify(g)}),{keyCookieName:S,csrfCookieName:f}=m();return a?.setHeader&&a.setHeader("Set-Cookie",[`${S}=${E};samesite=strict;path=/;HttpOnly=true;Secure=true`,`${f}=${p};samesite=strict;path=/;HttpOnly=true`]),s&&(!i||i?.match(/^datasquirel$/))&&r({query:s,user:g}),{success:!0,user:g}}e.exports=async function({database:e,social_id:t,email:a,social_platform:r,payload:n,res:m,invitation:g,supEmail:E,additionalFields:S,useLocal:f}){try{let y="SELECT * FROM users WHERE social_id = ? AND social_login='1' AND social_platform = ? ",D=[t.toString(),r],b=f?await _(y,D):await u({database:e||"datasquirel",queryString:y,queryValuesArray:D});if(b&&b[0])return await p({user:b[0],social_platform:r,res:m,invitation:g,database:e,additionalFields:S,useLocal:f});let h=a||E||null;if(!h)return{success:!1,user:null,msg:"No Email Present",social_id:t,social_platform:r,payload:n};let R=`SELECT * FROM users WHERE email='${h}'`,v=f?await _(R):await u({database:e||"datasquirel",queryString:R});if(v&&v[0])return{success:!1,user:null,msg:"This Email is already taken",alert:!0};let A=`SELECT * FROM users WHERE email='${h}' AND social_login='1' AND social_platform='${r}' AND social_id='${t}'`,L=f?await _(A):await u({database:e||"datasquirel",queryString:A});if(L&&L[0])return await p({user:n,social_platform:r,res:m,invitation:g,database:e,additionalFields:S,useLocal:f});let N=d({data:t.toString()}),O={social_login:"1",verification_status:E?"0":"1",password:N};Object.keys(n).forEach(e=>{O[e]=n[e]});let w=await c({dbContext:e?"Dsql User":void 0,paradigm:e?"Full Access":void 0,dbFullName:e||"datasquirel",tableName:"users",duplicateColumnName:"email",duplicateColumnValue:h,data:{...O,email:h},useLocal:f});if(!w?.insertId)return console.log("Social User Failed to insert in 'handleSocialDb.js' backend function =>",w),{success:!1,user:null,msg:"Social User Failed to insert in 'handleSocialDb.js' backend function => ",newUser:w};{e||await o({userId:w.insertId,useLocal:f});let t=`SELECT * FROM users WHERE id='${w.insertId}'`,a=f?await _(t):await u({database:e||"datasquirel",queryString:t});if(!a||!a[0])return{success:!1,user:null,msg:"User Insertion Failed!"};if(E&&e?.match(/^datasquirel$/)){let e=d({data:JSON.stringify({id:w.insertId,email:E,dateCode:Date.now()})});i({to:E,subject:"Verify Email Address",text:"Please click the link to verify your email address",html:s.readFileSync("./email/send-email-verification-link.html","utf8").replace(/{{host}}/,process.env.DSQL_HOST||"").replace(/{{token}}/,e||"")}).then(e=>{})}let n=process.env.DSQL_STATIC_SERVER_DIR;if(!n)return console.log("Static File ENV not Found!"),null;if(!e||e?.match(/^datasquirel$/)){let e=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${w.insertId}`,t=l.join(n,`images/user-images/user-${w.insertId}`);s.mkdirSync(e),s.mkdirSync(t),s.writeFileSync(`${e}/main.json`,JSON.stringify([]),"utf8")}return await p({user:a[0],social_platform:r,res:m,invitation:g,database:e,additionalFields:S,useLocal:f})}}catch(e){return console.log("ERROR in 'handleSocialDb.js' backend function =>",e.message),{success:!1,user:null,error:e.message}}}},44767:(e,t,a)=>{let s=a(48225),r=a(51348),i=a(47670);e.exports=async function({query:e,user:t}){try{let{invite:a,database_access:s,priviledge:n,email:l}=e,o=await r("SELECT date_created_code FROM invitations WHERE inviting_user_id=? AND invited_user_email=?",[a,l]);if(!o||!o[0])throw Error("No Invitation Found");let u=await r("SELECT first_name,last_name,email FROM users WHERE id=?",[a]);if(u?.[0]){let e=await r("SELECT email FROM user_users WHERE user_id=? AND invited_user_id=? AND user_type='admin' AND email=?",[a,t.id,l]);if(e?.[0])console.log("User already added");else{i({dbFullName:"datasquirel",tableName:"user_users",data:{user_id:a,invited_user_id:t.id,database_access:s,first_name:t.first_name,last_name:t.last_name,phone:t.phone,email:t.email,username:t.username,user_type:"admin",user_priviledge:n,image:t.image,image_thumbnail:t.image_thumbnail}});let e=await r("SELECT db_tables_data FROM invitations WHERE inviting_user_id=? AND invited_user_email=?",[a,l]);if(await r("DELETE FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=?",[a,t.id]),e&&e[0]){let s=e[0].db_tables_data.split("|");for(let e=0;e<s.length;e++){let[r,l]=s[e].split("-");await i({dbFullName:"datasquirel",tableName:"delegated_user_tables",data:{delegated_user_id:t.id,root_user_id:a,database:r,table:l,priviledge:n}})}}}await r("UPDATE invitations SET invitation_status='Accepted' WHERE inviting_user_id=? AND invited_user_email=?",[a,l])}}catch(e){s({component:"addAdminUserOnLogin",message:e.message,user:t})}}},5194:(e,t,a)=>{let s=a(47795),r=a(51348),i=a(45356),n=a(47670),l=a(20503),o=a(44900);e.exports=async function({userId:e,useLocal:t}){try{let a=process.env.DSQL_DB_HOST||"127.0.0.1",u=`dsql_user_${e}`,d=s.generate({length:16,numbers:!0,symbols:!0,uppercase:!0,exclude:"*#.'`\""}),c=l({data:d}),m=`CREATE USER IF NOT EXISTS '${u}'@'127.0.0.1' IDENTIFIED BY '${d}' REQUIRE SSL`;t?await o(m):await i(m);let _="UPDATE users SET mariadb_user = ?, mariadb_host = '127.0.0.1', mariadb_pass = ? WHERE id = ?",p=[u,c,e];t?await o(_,p):await r(_,p),await n({tableName:"mariadb_users",data:{user_id:e,username:u,host:a,password:c,primary:"1",grants:'[{"database":"*","table":"*","privileges":["ALL"]}]'},dbContext:"Master",useLocal:t}),console.log(`User ${e} SQL credentials successfully added.`)}catch(e){console.log("Error in adding SQL user in 'addMariadbUser' function =>",e.message)}}},31341:e=>{e.exports=function(){let e=process.env.DSQL_COOKIES_PREFIX||"dsql_";return{keyCookieName:e+(process.env.DSQL_COOKIES_KEY_NAME||"key"),csrfCookieName:e+(process.env.DSQL_COOKIES_CSRF_NAME||"csrf")}}},30003:e=>{e.exports=/^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/},59474:(e,t,a)=>{let s=a(29021),r=a(21572).createTransport({host:process.env.DSQL_MAIL_HOST,port:465,secure:!0,auth:{user:process.env.DSQL_MAIL_EMAIL,pass:process.env.DSQL_MAIL_PASSWORD}});e.exports=async function({to:e,subject:t,text:a,html:i,alias:n}){let l;if(!process.env.DSQL_MAIL_HOST||!process.env.DSQL_MAIL_EMAIL||!process.env.DSQL_MAIL_PASSWORD)return null;let o=(n?.match(/support/i),process.env.DSQL_MAIL_EMAIL);if(!s.existsSync("./email/index.html"))return;let u=s.readFileSync("./email/index.html","utf8").replace(/{{email_body}}/,i||"").replace(/{{issue_date}}/,Date().substring(0,24));try{let s={};s.from=`"Datasquirel" <${o}>`,s.sender=o,n&&(s["replyTo "]=o),s.to=e,s.subject=t,s.text=a,s.html=u,l=await r.sendMail(s)}catch(e){console.log("ERROR in handleNodemailer Function =>",e.message)}return l}},6061:(e,t,a)=>{let s=a(72835),r=a(30003);e.exports=async function({unparsedResults:e,tableSchema:t}){let a=[];try{for(let i=0;i<e.length;i++){let n=e[i],l=Object.keys(n);for(let e=0;e<l.length;e++){let a=l[e],i=t?.fields[e];if(a?.match(r))continue;let o=n[a];("number"==typeof o||o)&&i?.encrypted&&o?.match(/./)&&(n[a]=s({encryptedString:o}))}a.push(n)}return a}catch(t){return console.log("ERROR in parseDbResults Function =>",t.message),e}}},48225:(e,t,a)=>{let s=a(29021),{IncomingMessage:r}=a(81630);e.exports=async function({user:e,message:t,component:a,noMail:r,req:i}){let n=new Date,l=(()=>{if(!i)return null;try{let e=i.headers["x-forwarded-for"],t=i.headers["x-real-ip"],a=i.headers["cf-connecting-ip"],s=Array.isArray(e)?e[0]:e?.split(",")[0],r=a||s||t||i.socket.remoteAddress;if(!r)return null;return String(r)}catch(e){return null}})();try{let r=`🚀 SERVER ERROR ===========================
|
|
Error Message: ${t}
|
|
Component: ${a}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(r+=`
|
|
User Id: ${e?.id}
|
|
User Name: ${e?.first_name} ${e?.last_name}
|
|
User Email: ${e?.email}`),i?.url&&(r+=`
|
|
URL: ${i.url}`),i?.body&&(r+=`
|
|
Request Body: ${JSON.stringify(i.body,null,4)}`),l&&(r+=`
|
|
IP: ${l}`),r+=`
|
|
Date: ${n.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",r),s.appendFileSync("./.tmp/error.log",`
|
|
|
|
|
|
|
|
|
|
${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},57638:(e,t,a)=>{a(29021);let s=a(6061),r=a(48225),i=a(51348),n=a(21339);e.exports=async function({queryString:e,queryValuesArray:t,database:a,tableSchema:l}){let o;let u=!!a?.match(/^datasquirel$/),d=u?i:n;try{o=e&&t&&Array.isArray(t)&&t[0]?u?await d(e,t):await d({paradigm:"Full Access",database:a,queryString:e,queryValues:t}):u?await d(e):await d({paradigm:"Full Access",database:a,queryString:e})}catch(e){r({component:"varDatabaseDbHandler/lines-29-32",message:e.message})}if(o&&l)try{let e=o;return await s({unparsedResults:e,tableSchema:l})}catch(e){return console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",a,e),r({component:"varDatabaseDbHandler/lines-52-53",message:e.message}),null}else if(o)return o;else return null}},72835:(e,t,a)=>{let{scryptSync:s,createDecipheriv:r}=a(55511),{Buffer:i}=a(79428);e.exports=({encryptedString:e,encryptionKey:t,encryptionSalt:a})=>{if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let n=t||process.env.DSQL_ENCRYPTION_PASSWORD,l=a||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("Decrption key is invalid"),e;if(!l?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let u=r("aes-192-cbc",s(n,l,o),i.alloc(16,0));try{let t=u.update(e,"hex","utf8");return t+=u.final("utf8")}catch(t){return console.log("Error in decrypting =>",t.message),e}}},45356:(e,t,a)=>{let s=a(9973),r=a(21580),i=s({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,charset:"utf8mb4",ssl:r()}});e.exports=function(...e){try{return new Promise((t,a)=>{i.query(...e).then(e=>{i.end(),t(JSON.parse(JSON.stringify(e)))}).catch(e=>{i.end(),t({error:e.message,sql:e.sql})})})}catch(e){return{success:!1,error:e.message}}}}}; |