芝麻web文件管理V1.00
编辑当前文件:/home/freeclou/app.optimyar.com/backend/api/captcha/controllers/captcha.js
'use strict'; const fs = require("fs"); const moment = require("jalali-moment"); const requestIp = require('request-ip'); const url = require('url'); /** * Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers) * to customize this controller */ module.exports = { async createUserLog(ctx) { const {type, message} = ctx.request.body; const {user} = ctx.state; if (!user) { return ctx.badRequest(null, "invalid token"); } await strapi.services["system-log"].create({ type: `${type}: ${user.phoneNo}`, message: error }); }, async getBehPayUrl(ctx){ const result = await strapi.services.helpers.requestForPayBP(); ctx.send(result); }, async make(ctx) { const isVersionOk= await strapi.services.helpers.checkVersion(ctx); if (!isVersionOk){ return ctx.badRequest(null, "NEW VERSION RELEASED"); } const result = await strapi.services.captcha.make(); ctx.send(result); }, async resendFactorEmail(ctx) { const {id} = ctx.request.body; const userCourse = await strapi.services["user-course"].findOne({id}); if (userCourse === null) { return ctx.badRequest(null, "errors.factorNotFound") } const data = fs.readFileSync('resources/emailTemplate/Factor.html', 'utf8'); const faBuyDate = moment.from(userCourse.buyDate.toUTCString(), 'en').locale("fa").format("YYYY/MM/DD HH:mm"); const html = data.replace(/COURSE_TITLE/g, userCourse.course_levels[0].title) .replace(/FACTOR_NUMBER/g, userCourse.factorNumber) .replace(/PAY_AMOUNT/g, userCourse.buyPrice + " تومان ") .replace(/COURSE_LEVEL_ID/g, userCourse.course_levels[0].id) .replace(/USER_PHONE_NO/g, userCourse.user.phoneNo) .replace(/USER_FULL_NAME/g, userCourse.user.name) .replace(/BUY_Date/g, faBuyDate); strapi.services.helpers.sendEmail(userCourse.user.email, "تاییدیه ثبتنام و شرکت در دورههای آکادمی آموزشی و پژوهشی آپتیمیار", html).then(); ctx.send("ارسال شد."); }, async checkSessionLicence(ctx) { const {user} = ctx.state; if (!user) { return ctx.badRequest(null, "invalid token"); } if (strapi.services.helpers.isAdmin(user)) { return ctx.send(false); } return ctx.send({ name: user.name + " " + user.family?user.family:'', phoneNo: user.phoneNo }); }, async getPublicSessions(ctx) { const {user} = ctx.state; const queryObject = url.parse(ctx.request.url, true).query; const isPrivate = queryObject.isPrivate && queryObject.isPrivate === "true"; const result = await strapi.services.helpers.getBBBPublicSessions(isPrivate); ctx.send({ sessions: result.map(x => ({ meetingID: x.meetingID, meetingName: x.meetingName, ispublic:x.metadata.ispublic, ispublicregistred:x.metadata.ispublicregistred, disallowRecording: x.metadata.disallowrecording })) }); }, async startPublicSession(ctx) { const {user} = ctx.state; let {fullName, phoneNo, email, meetingID, attendeePW, isPrivate} = ctx.request.body; if (!email){ email="none"; } const info = await strapi.services.helpers.getBBBMeetingInfo(meetingID); if (info.returncode !== "SUCCESS" || !info.running) { return ctx.badRequest(null, "errors.classIsNotLive"); } if (isPrivate && info.attendeePW !== attendeePW) { return ctx.badRequest(null, "errors.invalidSessionPassword") } const clientIp = requestIp.getClientIp(ctx.request.req); const userId = await strapi.services.helpers.makeUserIdForBBBSession(isPrivate ? 'em' : email, phoneNo, clientIp); // if (!isPrivate) { // const isUserNotJoined = await strapi.services.helpers.verifyBBBUserNotJoined(info, userId); // if (isUserNotJoined.result === false) { // return ctx.badRequest("session.errors.userIdExist"); // } // } const result = { joinUrl: await strapi.services.helpers .joinUserToMeeting(fullName, meetingID, info.attendeePW, userId,false) }; //console.log("result...",result); if (!isPrivate) { if (strapi.services.helpers.isEmail(email)) { const data = fs.readFileSync('resources/emailTemplate/PublicSession.html', 'utf8'); const html = data.replace(/SESSION_TITLE/g, info.meetingName) .replace(/SESSION_LINK_CODE/g, meetingID) .replace(/SESSION_LINK/g, result.joinUrl); strapi.services.helpers.sendEmail(email, "لینک شرکت در جلسه عمومی آپتیم یار", html).then(); } } return ctx.send(result); }, async deleteRecordings(ctx) { const {id} = ctx.params; const result = await strapi.services.helpers.deleteBBBRecordings(id); ctx.send({...result}); }, async getRecordings(ctx) { const result = await strapi.services.helpers.getBBBRecordings(); ctx.send({...result}); }, async sendBlackListAlert(ctx){ const {user} = ctx.state; //user.phoneNo="9106116696"; const phoneNo=user.phoneNo.toString(); const lastSent = await strapi.query('captcha').find({_limit: 1, _sort: 'createdAt:desc',relatedPhoneNo:phoneNo,isWarning:true}); const {appName} = ctx.request.body; if (lastSent == null || lastSent.length === 0) { await strapi.services.helpers.sendBlackListMessage(user,appName); await strapi.services.captcha.create({ value:appName, isWarning:true, isValid:true, relatedPhoneNo:phoneNo }); }else{ const last = lastSent[0]; const after30Min = strapi.services.helpers.addMinutes(last.createdAt.toString(), 30); const isAfter = strapi.services.helpers.isNowAfterDate(after30Min); if (isAfter){ await strapi.services.helpers.sendBlackListMessage(user,appName); await strapi.services.captcha.create({ value:appName, isWarning:true, isValid:true, relatedPhoneNo:phoneNo }); } } return ctx.send(true); }, async sendSms(ctx) { let {code, paramName, paramValue,phoneNo} = ctx.request.body; const obj = {}; //obj[paramName] = paramValue; const inputData= [ {"name": "abc"}, {"software":"test"} ]; //const inputData = [obj]; await strapi.services.helpers.sendSms("9367202576", code, inputData); ctx.send("اس ام اس با موفقیت ارسال شد"); }, async joinBBB(ctx) { const {user} = ctx.request.body; const result = await strapi.services.helpers.joinBBB(user); ctx.send({...result}); }, async getMeetingInfo(ctx) { const {id} = ctx.params; const result = await strapi.services.helpers.getBBBMeetingInfo(id); delete result.attendeePW; delete result.moderatorPW ctx.send({...result}); }, async getMeetingInfoByInteralId(ctx) { const {id} = ctx.params; const result = await strapi.services.helpers.getBBBMeetingInfoByInternalId(id); if (result.result===false){ return ctx.badRequest( null, result.entity ); } delete result.entity.attendeePW; delete result.entity.moderatorPW return ctx.send(result.entity); }, async getMeetings(ctx) { const result = await strapi.services.helpers.getBBBMeetings(); ctx.send({...result}); }, async endJoin(ctx) { const {user, meetingID, password} = ctx.request.body; const result = await strapi.services.helpers.endJoin(user, meetingID, password); ctx.send({...result}); }, async sendMail(ctx) { const {to, subject, fileName} = ctx.request.body; try { const data = fs.readFileSync(`resources/emailTemplate/${fileName}`, 'utf8'); const html = data.replace(/COURSE_TITLE/g, 'دوره شماره 1') .replace(/TEACHER/g, 'دکتر علی پاپی') .replace(/SESSION_TITLE/g, 'دوره تستی') .replace(/USER_PHONE_NO/g, '0910611666') .replace(/USER_FULL_NAME/g, 'ناصر پاپی') .replace(/SESSION_LINK_CODE/g, "abcdefghialjkj232") .replace(/SESSION_LINK/g, 'https://b3.optimyar.com/bigbluebutton/api/join?userID=09106116696%20-%20papinaser%40gmail.com%20-%20127.0.0.1&fullName=%D9%85%D8%AD%D9%85%D8%AF%20%D8%B1%D8%B6%D8%A7&meetingID=5fdb68f548b2a674dc30176d&password=fCXAQbc2&checksum=b81f1e52a688fdcc35d8631717ba18de822baa43') .replace(/ُُSTART_TIME/g, "دی ماه 99"); await strapi.services.helpers.sendEmail(to, subject, html); ctx.send("ایمیل با موفقیت ارسال شد"); } catch (ex) { strapi.services["system-log"].create({ type: "sendmail", message: ex }); ctx.badRequest(null, "خطا در حین خواندن اطلاعات تملیت"); } }, async sendActiveCodeToUser(ctx) { const {user} = ctx.state; const {phoneNo} = user; const {captcha, captchaId} = ctx.request.body; const isValid = await strapi.services.captcha.isValid(captchaId, captcha); if (isValid) { const newCaptcha = await strapi.services.captcha.make(phoneNo); const inputData = [ {"active-code": newCaptcha.value}, ]; const result = await strapi.services.helpers.sendSms(phoneNo, "lu9d8a66xb", inputData); ctx.send({id: newCaptcha.id}) } else { return ctx.badRequest( null, 'auth.errors.invalidCaptcha' ); } }, async isActiveCodeValid(ctx) { const {user} = ctx.state; const {phoneNo} = user; const {activeCode, activeCodeId} = ctx.request.body; const isValid = await strapi.services.captcha.isValid(activeCodeId, activeCode, phoneNo); return ctx.send(!!isValid); }, async sendToPhone(ctx) { let {phoneNo, captcha, captchaId, checkExist, email} = ctx.request.body; const isValid = await strapi.services.captcha.isValid(captchaId, captcha); if (isValid) { const captcha = await strapi.services.captcha.make(phoneNo); phoneNo = strapi.services.helpers.normalizePhoneNo(phoneNo); if (!phoneNo) { return ctx.badRequest( null, 'auth.errors.invalidPhoneNo' ); } try { if (checkExist) { const isExist = await strapi.services.helpers.verifyPhoneNoExist(ctx, phoneNo); if (!isExist) { return; } } if (phoneNo) { const inputData = [ {"verification-code": captcha.value}, ]; const result = await strapi.services.helpers.sendSms(phoneNo, "f4sn9e36nb", inputData); ctx.send({id: captcha.id}) } if (email !== null && strapi.services.helpers.isEmail(email)) { const data = fs.readFileSync('resources/emailTemplate/VerifyCode.html', 'utf8'); const html = data.replace(/VERIFY_CODE/g, captcha.value); strapi.services.helpers.sendEmail(email, "کد فعال سازی", html).then(); } } catch (ex) { strapi.services["system-log"].create({ type: "sendToPhone-error", message: ex.message ? ex.message : ex, stack: ex.stack ? ex.stack : ex }); return ctx.badRequest(null, ex); } } else { return ctx.badRequest( null, 'auth.errors.invalidCaptcha' ); } }, async question(ctx) { //code : now.time + 1min is encrypted as key ctx.send("code1"); }, async answer(ctx) { //answer must contain 2 part part1:1- time in question 2-new key from client //if now.time+1 min is greater than part1 code is invalid //if calculated key is equal part2 then answer is correct const {answer} = ctx.request.body; if (answer !== "code2") { await strapi.services.helpers.expiredUserSession(ctx.user); return ctx.badRequest(null, "errors.invalidAnswer"); } ctx.send("ok"); } };