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