<commit_before>var request = require('request'),
    ip      = require('../helpers/ip.js');

/*
 * GET home page.
 */

var navItems = [
//  [ page-id, nav-title, uri-path, page-title, show-in-nav, is-isolated ]
    [ "intro", "Home", "/", "Protecting rainforests with real-time data", true, false ],
    [ "about", "About", "/about", "About", true, false ],
    [ "get_involved", "Get Involved", "/get_involved", "Get Involved", true, false ],
    [ "media", "Media", "/media", "Media", true, false ],

    [ "donatephone", "Donate", "/donatephone", "Donate a Phone", false, false ],

    [ "press", "Press", "/press", "Press Clippings", false, false ],
    [ "donate", "Donate", "/donate", "Donate", false, true ],
    [ "ecuador", "Ecuador", "/ecuador", "Ecuador", false, false ],
    [ "tumblr", "Tumblr", "http://tumblr.rfcx.org/", "", false, false ]
  ];
for (var i = 0; i < navItems.length; i++) { navItems[i][3] = "Rainforest Connection | "+navItems[i][3]; }

var socialMedia = [
  [ "github", "https://github.com/rfcx", "github-square", "Fork our repos on Github!", "Github"],
  [ "youtube", "http://www.youtube.com/user/RfcxOrg", "youtube-square", "Watch our videos on YouTube!", "YouTube"],
  [ "instagram", "http://instagram.com/rainforestcx", "instagram", "Check us out on Instagram!", "Instagram"],
  [ "tumblr", "http://tumblr.rfcx.org/", "tumblr-square", "Check out our Tumblr!", "Tumblr"],
  [ "flickr", "http://flickr.com/photos/rainforestcx/", "flickr", "Check us out on Flickr!", "Flickr"],
  [ "linkedin", "http://www.linkedin.com/company/rainforest-connection", "linkedin-square", "Check us out on LinkedIn!", "LinkedIn"],
  [ "google-plus", "https://plus.google.com/+RfcxOrg", "google-plus-square", "Check us out on Google+!", "Google+"],
  [ "twitter", "https://twitter.com/RainforestCx", "twitter-square", "Check us out on Twitter!", "Twitter"],
  [ "facebook", "https://www.facebook.com/RainforestCx", "facebook-square", "Check us out on Facebook!", "Facebook"]
];

function setJadeVars(process, jV) {
  var inProd = (process.env.NODE_ENV === "production");
  jV.app_version = process.env.productionVersionId;
  jV.node_env = process.env.NODE_ENV;
  jV.title += (inProd ? "" : (" ("+process.env.NODE_ENV+")"));
  jV.segment_analytics_client_id =  process.env.SEGMENT_ANALYTICS_CLIENT_ID;
  jV.addthis_pubid = process.env.ADDTHIS_PUBID;
  jV.bootstrap_cdn = inProd ? "//netdna.bootstrapcdn.com" : "/vendor";
  jV.googlelibs_cdn = inProd ? "//ajax.googleapis.com/ajax/libs" : "/vendor";
  jV.jsdelivr_cdn = inProd ? "//cdn.jsdelivr.net" : "/vendor";
  jV.videojs_cdn = inProd ? "//vjs.zencdn.net" : "/vendor/video.js";
  jV.cdnjs_cdn = inProd ? "//cdnjs.cloudflare.com/ajax/libs" : "/vendor";
  jV.rfcx_cdn = inProd ? "//d265tty8j31r8c.cloudfront.net/cdn" : "/cdn";
  jV.rfcx_vendor_cdn = inProd ? "//d265tty8j31r8c.cloudfront.net/vendor" : "/vendor";
  jV.rfcx_static_cdn = inProd ? "//d1gbw6jvuihkp4.cloudfront.net" : "//rfcx-static.s3.amazonaws.com";
  jV.nav_items = navItems;
  jV.social_media = socialMedia;
  jV.media_assets = []; if (jV.current_page[0] === "media") { jV.media_assets = require("./../data/media.js").load(); }
  return jV;
}

exports.navItems = navItems;

exports.page = function(req, res, process){
  var navItem = [];
  for (var i = 0; i < navItems.length; i++) {
    if (req.route.path === navItems[i][2]) {
      navItem = navItems[i]; break;
    }
  }
  if (  (process.env.NODE_ENV==="production")
    &&  (   (req.host!=="rfcx.org")
        ||  (req.headers["x-forwarded-proto"]!=="https")
        )
    ) {
    res.writeHead(302, { "Location": "https://rfcx.org"+req.path } );
    res.end();
  } else {
    res.setHeader("Access-Control-Allow-Origin","*");
    res.render(navItem[0], setJadeVars(process, {
      current_page: navItem,
      http_headers: req.headers
    }));
  }
};

exports.redirectToHomePage = function(req,res) {
  res.writeHead(302, { "Location": "/" } );
  res.end();
};

exports.returnHealthCheck = function(req,res) {
  var sendString = "rfcx";
  if (req.query.headers==="1") { sendString += "<br />"+JSON.stringify(req.headers); }
  res.send(sendString);
};

exports.donatePhonePost = function(req, res) {
  var body     = req.body;
  var clientIp = ip.getIp(req);
  // if request body is not parsed
  if (!body) {
    return sendJson(res, 'error', 'Internal server error.');
  }

  var recaptchaToken = body['g-recaptcha-response'];
  // if user has not checked recaptcha checkbox
  if (!recaptchaToken && !recaptchaToken.length) {
    return sendJson(res, 'error', 'Recaptcha task is not resolved');
  }

  // if getIp method returned undefined value
  if (!ip) {
    return sendJson(res, 'error', 'Cannot resolve client\'s ip address');
  }

  // Send verify request to Google
  request.post({
    url: 'https://www.google.com/recaptcha/api/siteverify',
    formData: {
      secret: process.env.RECAPTCHA_SECRET_KEY || '',
      response: recaptchaToken,
      remoteip: clientIp
    }
  }, function (error, response, body) {
    if (error) {
      return sendJson(res, 'error', 'Internal server error.');
    }
    try {
      var parsedBody = JSON.parse(body);
      if (parsedBody.success == true) {
        sendJson(res, 'success');
      }
      else {
        return sendJson(res, 'error', 'Recaptcha is not valid');
      }
    }
    catch(e) {
      return sendJson(res, 'error', 'Internal server error.');
    }
  });
};

function sendJson(res, type, msg) {
  res.json({
    status: type,
    message: msg
  })
}<commit_msg>add backend console notifications for donatephone post<commit_after>var request = require('request'),
    ip      = require('../helpers/ip.js');

/*
 * GET home page.
 */

var navItems = [
//  [ page-id, nav-title, uri-path, page-title, show-in-nav, is-isolated ]
    [ "intro", "Home", "/", "Protecting rainforests with real-time data", true, false ],
    [ "about", "About", "/about", "About", true, false ],
    [ "get_involved", "Get Involved", "/get_involved", "Get Involved", true, false ],
    [ "media", "Media", "/media", "Media", true, false ],

    [ "donatephone", "Donate", "/donatephone", "Donate a Phone", false, false ],

    [ "press", "Press", "/press", "Press Clippings", false, false ],
    [ "donate", "Donate", "/donate", "Donate", false, true ],
    [ "ecuador", "Ecuador", "/ecuador", "Ecuador", false, false ],
    [ "tumblr", "Tumblr", "http://tumblr.rfcx.org/", "", false, false ]
  ];
for (var i = 0; i < navItems.length; i++) { navItems[i][3] = "Rainforest Connection | "+navItems[i][3]; }

var socialMedia = [
  [ "github", "https://github.com/rfcx", "github-square", "Fork our repos on Github!", "Github"],
  [ "youtube", "http://www.youtube.com/user/RfcxOrg", "youtube-square", "Watch our videos on YouTube!", "YouTube"],
  [ "instagram", "http://instagram.com/rainforestcx", "instagram", "Check us out on Instagram!", "Instagram"],
  [ "tumblr", "http://tumblr.rfcx.org/", "tumblr-square", "Check out our Tumblr!", "Tumblr"],
  [ "flickr", "http://flickr.com/photos/rainforestcx/", "flickr", "Check us out on Flickr!", "Flickr"],
  [ "linkedin", "http://www.linkedin.com/company/rainforest-connection", "linkedin-square", "Check us out on LinkedIn!", "LinkedIn"],
  [ "google-plus", "https://plus.google.com/+RfcxOrg", "google-plus-square", "Check us out on Google+!", "Google+"],
  [ "twitter", "https://twitter.com/RainforestCx", "twitter-square", "Check us out on Twitter!", "Twitter"],
  [ "facebook", "https://www.facebook.com/RainforestCx", "facebook-square", "Check us out on Facebook!", "Facebook"]
];

function setJadeVars(process, jV) {
  var inProd = (process.env.NODE_ENV === "production");
  jV.app_version = process.env.productionVersionId;
  jV.node_env = process.env.NODE_ENV;
  jV.title += (inProd ? "" : (" ("+process.env.NODE_ENV+")"));
  jV.segment_analytics_client_id =  process.env.SEGMENT_ANALYTICS_CLIENT_ID;
  jV.addthis_pubid = process.env.ADDTHIS_PUBID;
  jV.bootstrap_cdn = inProd ? "//netdna.bootstrapcdn.com" : "/vendor";
  jV.googlelibs_cdn = inProd ? "//ajax.googleapis.com/ajax/libs" : "/vendor";
  jV.jsdelivr_cdn = inProd ? "//cdn.jsdelivr.net" : "/vendor";
  jV.videojs_cdn = inProd ? "//vjs.zencdn.net" : "/vendor/video.js";
  jV.cdnjs_cdn = inProd ? "//cdnjs.cloudflare.com/ajax/libs" : "/vendor";
  jV.rfcx_cdn = inProd ? "//d265tty8j31r8c.cloudfront.net/cdn" : "/cdn";
  jV.rfcx_vendor_cdn = inProd ? "//d265tty8j31r8c.cloudfront.net/vendor" : "/vendor";
  jV.rfcx_static_cdn = inProd ? "//d1gbw6jvuihkp4.cloudfront.net" : "//rfcx-static.s3.amazonaws.com";
  jV.nav_items = navItems;
  jV.social_media = socialMedia;
  jV.media_assets = []; if (jV.current_page[0] === "media") { jV.media_assets = require("./../data/media.js").load(); }
  return jV;
}

exports.navItems = navItems;

exports.page = function(req, res, process){
  var navItem = [];
  for (var i = 0; i < navItems.length; i++) {
    if (req.route.path === navItems[i][2]) {
      navItem = navItems[i]; break;
    }
  }
  if (  (process.env.NODE_ENV==="production")
    &&  (   (req.host!=="rfcx.org")
        ||  (req.headers["x-forwarded-proto"]!=="https")
        )
    ) {
    res.writeHead(302, { "Location": "https://rfcx.org"+req.path } );
    res.end();
  } else {
    res.setHeader("Access-Control-Allow-Origin","*");
    res.render(navItem[0], setJadeVars(process, {
      current_page: navItem,
      http_headers: req.headers
    }));
  }
};

exports.redirectToHomePage = function(req,res) {
  res.writeHead(302, { "Location": "/" } );
  res.end();
};

exports.returnHealthCheck = function(req,res) {
  var sendString = "rfcx";
  if (req.query.headers==="1") { sendString += "<br />"+JSON.stringify(req.headers); }
  res.send(sendString);
};

exports.donatePhonePost = function(req, res) {
  var body     = req.body;
  var clientIp = ip.getIp(req);
  // if request body is not parsed
  if (!body) {
    console.log('Donatephone Post Error: req.body is not defined or empty.');
    return sendJson(res, 'error', 'Internal server error.');
  }

  var recaptchaToken = body['g-<PASSWORD>'];
  // if user has not checked recaptcha checkbox
  if (!recaptchaToken && !recaptchaToken.length) {
    return sendJson(res, 'error', 'Recaptcha task is not resolved');
  }

  // if getIp method returned undefined value
  if (!ip) {
    return sendJson(res, 'error', 'Cannot resolve client\'s ip address');
  }

  // Send verify request to Google
  request.post({
    url: 'https://www.google.com/recaptcha/api/siteverify',
    formData: {
      secret: process.env.RECAPTCHA_SECRET_KEY || '',
      response: recaptchaToken,
      remoteip: clientIp
    }
  }, function (error, response, body) {
    if (error) {
      console.log('Donatephone Post Error: Google Recaptcha server error.');
      return sendJson(res, 'error', 'Internal server error.');
    }
    try {
      var parsedBody = JSON.parse(body);
      if (parsedBody.success == true) {
        sendJson(res, 'success');
      }
      else {
        return sendJson(res, 'error', 'Recaptcha is not valid');
      }
    }
    catch(e) {
      console.log('Donatephone Post Error: Error while parsing body from Google Recapthca.');
      return sendJson(res, 'error', 'Internal server error.');
    }
  });
};

function sendJson(res, type, msg) {
  res.json({
    status: type,
    message: msg
  })
}
{'fraction_non_alphanumeric': 0.1937080240367621, 'fraction_numerical': 0.005655708731000354, 'mean_word_length': 3.565147236789028, 'pattern_counts': {'":': 4, '<': 10, '<?xml version=': 0, '>': 6, 'https://': 12, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '865432', 'n_tokens_mistral': 4253, 'n_tokens_neox': 4083, 'n_words': 844}