var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var nodemailer = require('nodemailer'); var bodyParser = require('body-parser'); var phantom = require('node-phantom'); var fs = require('fs'); var rootPath = path.join(__dirname, 'public'); var config = require('./config'); var moment = require('moment'); var app = express(); app.use(bodyParser.urlencoded({extended: false})); //app.use(favicon(__dirname + '/public/images/AV4H.ico')); app.use(logger('dev')); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(require('stylus').middleware(path.join(__dirname, 'public'))); //Ensure we have an endsWith method in String. String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; /* //Check for all calls to content pages with a password attached and log it in the password's counter. app.use(function(req, res, next) { if(req.path.endsWith('.html')) { if(req.query.Password) { incrementPwdUseCount(req.query.Password); } } next(); }); */ app.use(express.static(rootPath, {dotfiles: 'deny', index: false})); //app.use('/', routes); app.use('/FrameworkController.java', function(req, res) { console.log("In /FrameworkController.java"); var requestParam = req.query.Request; if(requestParam == 'CreateId') { res.type('json'); res.send({result: 0}); } else { console.log('unexpected value'); res.status(400).send('Unexpected Param'); } }); //Handle the getting a brief and contacting us. var smtpTransport = nodemailer.createTransport({host: config.smtpHost, port: config.smtpPort, secure: true, ignoreTLS: false, requiresAuth: true, auth: {user: config.smtpUser, pass: config.smtpPassword}}); /* //TEST CODE!!!// try { var name = "Wynne Crisman"; var email = "wynne@petitteton.com"; var textPath = rootPath + '/email/downloadBreifText.txt'; var htmlPath = rootPath + '/email/downloadBriefHtml.txt'; var signaturePath = rootPath + '/email/emailSignature.jpg'; var params = {from: config.fromAddress, to: email, subject: "Payback Brief", text: {path: textPath}, html: {path: htmlPath}, attachments: [{filename:'emailSignature.jpg', cid: 'emailSignature', path: signaturePath}]}; smtpTransport.sendMail(params, function(error, response) { if(error) { console.log("Error: " + error); } else { console.log("Successfully sent the email - not an error."); } }); } catch(e) { console.log(e); } //END TEST// */ /* //Setup the passwords used by users to access the content. Allows us to track who has used the site when and how much. //Note: This is mostly for marketing purposes so it won't be very secure at all (no hashing, salting, SSL, or anything), and it allows the search engines to bypass it to the content. var pwdData; try { if(fs.existsSync('pwdData.json')) { pwdData = JSON.parse(fs.readFileSync('pwdData.json', 'utf8')); } else { pwdData = []; pwdData.push({pwd: 'zerotoone'}); pwdData.push({pwd: 'zephyr'}); pwdData.push({pwd: 'borealis'}); pwdData.push({pwd: 'gyroscope'}); pwdData.push({pwd: 'colnago'}); pwdData.push({pwd: 'derosa'}); pwdData.push({pwd: 'wwww&w'}); pwdData.push({pwd: 'catalyst'}); fs.writeFileSync('pwdData.json', JSON.stringify(pwdData), 'utf8'); } } catch(e) {console.log(e);} function writePwdData() { try { fs.writeFileSync('pwdData.json', JSON.stringify(pwdData), 'utf8'); } catch(e) {console.log(e);} } function incrementPwdUseCount(password) { //Note: This is mostly for marketing purposes so it won't be very secure at all (no hashing, salting, SSL, or anything), and it allows the search engines to bypass it to the content. try { if(password) { var index; var found = false; //Convert the password from base64. password = new Buffer(password, 'base64').toString('utf-8'); //Identify which password was used (note: could use a map, but realistically with so few it is pointless complexity). for(index = 0; !found && index < pwdData.length; index++) { if(pwdData[index].pwd == password) { //Track the number of times a user requests a content page.// if(pwdData[index].accessCount) pwdData[index].accessCount++; else pwdData[index].accessCount = 1; writePwdData(); found = true; } } } } catch(e) {console.log(e);} } app.use('/RequestBrief/', function(req, res) { try { var firstName = req.body.FirstName; var lastName = req.body.LastName; var email = req.body.Email; var isPartnership = req.body.PartnershipInterest; var isInvestment = req.body.InvestmentInterest; var isOther = req.body.OtherInterest; var textPath = rootPath + '/email/downloadBriefText.txt'; var htmlPath = rootPath + '/email/downloadBriefHtml.txt'; var signaturePath = rootPath + '/email/emailSignature.jpg'; var params = {from: config.fromAddress, to: email, subject: "Payback Brief", text: {path: textPath}, html: {path: htmlPath}, attachments: [{filename:'emailSignature.jpg', cid: 'emailSignature', path: signaturePath}]}; smtpTransport.sendMail(params, function(error, response) { try { if(error) { console.log("Received an error while sending the download brief email to the user. " + error); fs.appendFile(rootPath + '/emailFailures.txt', JSON.stringify(params) + '\n', function(err) {if(err) {console.log("Failed to write email data to file! (request brief)");}}); } else { params = {from: config.fromAddress, to: config.contactUsRecipient, subject: "Downloaded Brief", text: "A user has requested the Payback brief.\n\nFirst Name: " + firstName + "\nLast Name: " + lastName + "\nEmail: " + email + "\nPartnership: " + isPartnership + "\nInvestment: " + isInvestment + "\nOther: " + isOther}; smtpTransport.sendMail(params, function(error, response) { if(error) { try { console.log("Received an error while sending the request brief email to the admin. " + error); fs.appendFile(rootPath + '/emailFailures.txt', JSON.stringify(params) + '\n', function(err) {if(err) {console.log("Failed to write email data to file! (request brief)");}}); } catch(e) {console.log(e);} } }); } res.status(200).send('success'); } catch(e) {console.log(e);} }); } catch(e) {console.log(e);} }); app.use('/RequestFinancials/', function(req, res) { try { var firstName = req.body.FirstName; var lastName = req.body.LastName; var email = req.body.Email; var phone = req.body.Phone; var company = req.body.Company; var message = req.body.Message; var isPartnership = req.body.PartnershipInterest; var isInvestment = req.body.InvestmentInterest; var isOther = req.body.OtherInterest; var textPath = rootPath + '/email/downloadFinancialsText.txt'; var htmlPath = rootPath + '/email/downloadFinancialsHtml.txt'; var textContents = fs.readFileSync(textPath, "UTF8"); var htmlContents = fs.readFileSync(htmlPath, "UTF8"); var signaturePath = rootPath + '/email/emailSignature.jpg'; textContents = textContents.replace("%%NAME%%", firstName); htmlContents = htmlContents.replace("%%NAME%%", firstName); var params = {from: config.fromAddress, to: email, subject: "Payback Financials", text: textContents, html: htmlContents, attachments: [{filename:'emailSignature.jpg', cid: 'emailSignature', path: signaturePath}]}; smtpTransport.sendMail(params, function(error, response) { try { if(error) { console.log("Received an error while sending the request financials email to the user. " + error); fs.appendFile(rootPath + '/emailFailures.txt', JSON.stringify(params) + '\n', function(err) {if(err) {console.log("Failed to write email data to file! (request financials)");}}); } else { params = {from: config.fromAddress, to: config.contactUsRecipient, subject: "Downloaded Financials", text: "A user has requested Payback's financials.\n\nFirst Name: " + firstName + "\nLast Name: " + lastName + "\nEmail: " + email + "\nPhone: " + phone + "\nCompany: " + company + "\nPartnership: " + isPartnership + "\nInvestment: " + isInvestment + "\nOther: " + isOther + "\nMessage: " + message}; smtpTransport.sendMail(params, function(error, response) { if(error) { try { console.log("Received an error while sending the request financials email to the admin. " + error); fs.appendFile(rootPath + '/emailFailures.txt', JSON.stringify(params) + '\n', function(err) {if(err) {console.log("Failed to write email data to file! (request financials)");}}); } catch(e) {console.log(e);} } }); } res.status(200).send('success'); } catch(e) {console.log(e);} }); } catch(e) {console.log(e);} }); */ app.use('/ContactUs', function(req, res) { try { var firstName = req.body.FirstName; var lastName = req.body.LastName; var email = req.body.Email; var message = req.body.Text; var params = {from: config.fromAddress, to: config.contactUsRecipient, subject: "Contact Us", text: "A user has commented via the Petit Teton website.\n\nFirst Name: " + firstName + "\nLast Name: " + lastName + "\nEmail: " + email + "\n" + message}; smtpTransport.sendMail(params, function(error, response) { if(error) { try { console.log("Received an error while sending the contact us email to the admin. " + error); fs.appendFile(rootPath + '/emailFailures.txt', JSON.stringify(params) + '\n', function(err) {if(err) {console.log("Failed to write email data to file! (contact us)");}}); } catch(e) {console.log(e);} } }); res.status(200).send('success'); } catch(e) {console.log(e);} }); /* app.use('/LoginUser', function(req, res) { //Note: This is mostly for marketing purposes so it won't be very secure at all (no hashing, salting, SSL, or anything), and it allows the search engines to bypass it to the content. try { var password = req.body.Password; if(password) { var index; var found = false; //Convert the password from base64. password = new Buffer(password, 'base64').toString('utf-8'); //Identify which password was used (note: could use a map, but realistically with so few it is pointless complexity). for(index = 0; !found && index < pwdData.length; index++) { if(pwdData[index].pwd == password) { //Track the time of the first and last login. if(pwdData[index].firstLogin) pwdData[index].lastLogin = new Date(); else pwdData[index].firstLogin = new Date(); writePwdData(); found = true; } } if(found) { //Notify the client they have logged in. res.status(200).send('success'); } else { res.status(200).send('failed'); } } else { res.status(200).send('failed'); } } catch(e) {console.log(e);} }); */ console.log("Time now is: " + moment(new Date()).format("MMM Do YYYY, h:mm:ss a")); /* app.use('/Admin/UserData', function(req, res) { //TODO: Return a table of user data. This is a hidden function, and since the data is not very sensitive we won't bother with password protection or ssl. var body = "
| Password | Page Request Count | First Login | Last Login |
|---|---|---|---|
| " + pwdData[index].pwd + " | "; body += "" + (pwdData[index].accessCount ? pwdData[index].accessCount : 0) + " | "; body += "" + (pwdData[index].firstLogin ? (moment(pwdData[index].firstLogin).format("MMM Do YYYY, h:mm:ss a") + " (" + moment(pwdData[index].firstLogin).fromNow() + ")") : "") + " | "; body += "" + (pwdData[index].lastLogin ? (moment(pwdData[index].lastLogin).format("MMM Do YYYY, h:mm:ss a") + " (" + moment(pwdData[index].lastLogin).fromNow() + ")") : "") + " | "; body += "