diff --git a/app.js b/app.js index 1e26776..24eeb9e 100644 --- a/app.js +++ b/app.js @@ -5,7 +5,7 @@ var express_session = require('express-session'); var path = require("path"); var ejs = require('ejs'); var favicon = require('serve-favicon'); -var crypto = require('crypto'); +var randomstring = require("randomstring"); var mysql = require('mysql'); var dbpool = mysql.createPool( @@ -225,19 +225,64 @@ app.post('/challenge', function(req,res) return; } + var challengeId = req.body.challenge_id; + var answer = req.body.options; + + if (!answer) + { + res.render(path.join(__dirname+'/www/error.html'), {errormessage : "please select your answer."}); + return; + } + dbpool.getConnection(function(err, connection) { - connection.query("SELECT * from TA_CHALLENGE where sp_number=?", + connection.query("select sp_answer, sp_score from TA_CHALLENGE where sp_number = ?", [challengeId], function(err, rows) { - connection.release(); - if (err) { + connection.release(); res.render(path.join(__dirname+'/www/error.html'), {errormessage : err}); return; } + + if (rows.length == 1) + { + var challenge = rows[0]; + var score = 0; + + if (answer == challenge.sp_correct_answer) + { + score = challenge.sp_score; + } + + connection.query("insert into TA_BADGE_CHALLENGE (fk_badge, fk_challenge, sp_score, sp_answer) " + + "values ((select sp_oid from TA_BADGE where sp_badge_id = ?), " + + "(select sp_oid from TA_CHALLENGE where sp_number = ?), ?, ?);", + [session.badgeid, challengeId, score, challenge.sp_correct_answer], + function(err, rows) + { + connection.release(); + + if (err) + { + res.render(path.join(__dirname+'/www/error.html'), {errormessage : err}); + return; + } + + res.render(path.join(__dirname+'/www/success.html'), { + message : "your answer was submited, we added " + score + " points to your score", + target : "/" + }); + }); + } + else + { + connection.release(); + res.render(path.join(__dirname+'/www/error.html'), {errormessage : "error: your answer could not be submited. please try again"}); + return; + } }); }); }); @@ -324,8 +369,11 @@ app.post('/registerbadge',function(req,res) } else { + var newId = randomstring.generate(32); + //console.log('new ID: ', newId); + session.badgeid = newId; sqlStatement = "insert into TA_BADGE (sp_nickname, sp_callsign, sp_email, sp_password, sp_register_time, sp_badge_id) values (?, ?, ?, ?, now(), ?)"; - sqlValues = [req.body.badge_nick, req.body.badge_callsign, req.body.badge_email, req.body.badge_password1, crypto.randomBytes(32)]; + sqlValues = [req.body.badge_nick, req.body.badge_callsign, req.body.badge_email, req.body.badge_password1, newId]; } connection.query(sqlStatement, sqlValues, function(err, result) { @@ -338,6 +386,8 @@ app.post('/registerbadge',function(req,res) if (result.affectedRows == 1) { + session.nickname = req.body.badge_nick; + res.render(path.join(__dirname+'/www/success.html'), { message : "badge data updated. if you want, you can now upload an image as avatar. if not, please select something from the menu.", target : "/imageconverter" @@ -372,7 +422,7 @@ app.post('/imageupload',function(req,res) return; } - if (true || req.body.image_avatar) + if (req.body.image_avatar) { dbpool.getConnection(function(err, connection) { diff --git a/node_modules/randomstring/.npmignore b/node_modules/randomstring/.npmignore new file mode 100755 index 0000000..594bea6 --- /dev/null +++ b/node_modules/randomstring/.npmignore @@ -0,0 +1,2 @@ +.DS_Store +*.log \ No newline at end of file diff --git a/node_modules/randomstring/.travis.yml b/node_modules/randomstring/.travis.yml new file mode 100644 index 0000000..9b36bf8 --- /dev/null +++ b/node_modules/randomstring/.travis.yml @@ -0,0 +1,7 @@ +language: node_js +node_js: + - "0.12" + - "0.11" + - "0.10" + - "iojs" + - "iojs-v1.0.4" \ No newline at end of file diff --git a/node_modules/randomstring/CHANGELOG.md b/node_modules/randomstring/CHANGELOG.md new file mode 100755 index 0000000..bdf6da4 --- /dev/null +++ b/node_modules/randomstring/CHANGELOG.md @@ -0,0 +1,24 @@ +1.0.7 / Jul 03, 2015 +================== + * Use node.crypto instead of Math.random as random number generator + +1.0.6 / Jun 01, 2015 +================== + * Added licence for npmjs.org + * Enhanced readme for Github and npm + +1.0.5 / Apr 03, 2015 +================== + * Better charset setting → Less error-proneness + +1.0.4 / Apr 03, 2015 +================== + * Added tests + +1.0.3 / Feb 17, 2014 +================== + * Fixed typo in character set + +1.0.0 / Jan 21, 2012 +================== + * Start of the project \ No newline at end of file diff --git a/node_modules/randomstring/LICENSE b/node_modules/randomstring/LICENSE new file mode 100755 index 0000000..f374d10 --- /dev/null +++ b/node_modules/randomstring/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2012 Elias Klughammer + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/randomstring/README.md b/node_modules/randomstring/README.md new file mode 100755 index 0000000..c306409 --- /dev/null +++ b/node_modules/randomstring/README.md @@ -0,0 +1,34 @@ +# node-randomstring + +[![Build Status](https://travis-ci.org/klughammer/node-randomstring.svg?branch=master)](https://travis-ci.org/klughammer/node-randomstring) [![Download Stats](https://img.shields.io/npm/dm/randomstring.svg)](https://github.com/klughammer/node-randomstring) + +## Installation + +To install randomstring, use [npm](http://github.com/npm/npm): + +``` +npm install randomstring +``` + +## Usage + +```javascript +var randomstring = require("randomstring"); + +randomstring.generate(); +// >> "XwPp9xazJ0ku5CZnlmgAx2Dld8SHkAeT" + +randomstring.generate(7); +// >> "xqm5wXX" +``` + +## Tests + +``` +npm install +npm test +``` + +## LICENSE + +node-randomstring is licensed under the MIT license. diff --git a/node_modules/randomstring/examples/.empty b/node_modules/randomstring/examples/.empty new file mode 100755 index 0000000..e69de29 diff --git a/node_modules/randomstring/index.js b/node_modules/randomstring/index.js new file mode 100644 index 0000000..1fddded --- /dev/null +++ b/node_modules/randomstring/index.js @@ -0,0 +1 @@ +module.exports = require("./lib/randomstring"); \ No newline at end of file diff --git a/node_modules/randomstring/lib/randomstring.js b/node_modules/randomstring/lib/randomstring.js new file mode 100644 index 0000000..ae64395 --- /dev/null +++ b/node_modules/randomstring/lib/randomstring.js @@ -0,0 +1,26 @@ +"use strict"; + +var charsNumbers = '0123456789'; +var charsLower = 'abcdefghijklmnopqrstuvwxyz'; +var charsUpper = charsLower.toUpperCase(); + +var crypto = require('crypto'); + +var chars = charsNumbers + charsLower + charsUpper; + +exports.generate = function(length) { + + length = length || 32; + + var string = ''; + + while(string.length < length){ + var bf = crypto.randomBytes(length); + for(var i = 0; i < bf.length; i++){ + var index = bf.readUInt8(i) % chars.length; + string += chars.charAt(index); + } + } + + return string; +} diff --git a/node_modules/randomstring/package.json b/node_modules/randomstring/package.json new file mode 100644 index 0000000..f9fffe0 --- /dev/null +++ b/node_modules/randomstring/package.json @@ -0,0 +1,51 @@ +{ + "name": "randomstring", + "version": "1.0.7", + "author": { + "name": "Elias Klughammer", + "email": "elias@klughammer.com", + "url": "http://www.klughammer.com" + }, + "description": "A module for generating random strings", + "homepage": "https://github.com/klughammer/node-randomstring", + "repository": { + "type": "git", + "url": "git://github.com/klughammer/node-randomstring.git" + }, + "main": "./index", + "engines": { + "node": "*" + }, + "devDependencies": { + "mocha": "^1.20.1" + }, + "license": "MIT", + "scripts": { + "test": "mocha" + }, + "gitHead": "e8a86bb5aba4c4b4c3cc65906e23129104d18200", + "bugs": { + "url": "https://github.com/klughammer/node-randomstring/issues" + }, + "_id": "randomstring@1.0.7", + "_shasum": "cf0cf5848228a5767f9145b37b1937689941c64f", + "_from": "randomstring@*", + "_npmVersion": "2.7.4", + "_nodeVersion": "0.12.2", + "_npmUser": { + "name": "eliaskg", + "email": "elias.klughammer@me.com" + }, + "maintainers": [ + { + "name": "eliaskg", + "email": "elias.klughammer@me.com" + } + ], + "dist": { + "shasum": "cf0cf5848228a5767f9145b37b1937689941c64f", + "tarball": "http://registry.npmjs.org/randomstring/-/randomstring-1.0.7.tgz" + }, + "directories": {}, + "_resolved": "https://registry.npmjs.org/randomstring/-/randomstring-1.0.7.tgz" +} diff --git a/node_modules/randomstring/test/index.js b/node_modules/randomstring/test/index.js new file mode 100644 index 0000000..a343bfe --- /dev/null +++ b/node_modules/randomstring/test/index.js @@ -0,0 +1,22 @@ +"use strict"; + +var assert = require("assert") +var random = require("..").generate + +describe("randomstring.generate()", function() { + + it("returns a string", function() { + var rds = random(); + assert.equal(typeof(rds), "string"); + console.log(" String return: " + rds); + }) + + it("defaults to 32 characters in length", function() { + assert.equal(random().length, 32) + }) + + it("accepts length as an optional first argument", function() { + assert.equal(random(10).length, 10) + }) + +}) diff --git a/node_modules/randomstring/test/mocha.opts b/node_modules/randomstring/test/mocha.opts new file mode 100644 index 0000000..f633acd --- /dev/null +++ b/node_modules/randomstring/test/mocha.opts @@ -0,0 +1,2 @@ +--reporter spec +--ui tdd diff --git a/package.json b/package.json index 74e11e7..0d5f961 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "express": "^4.13.1", "express-session": "^1.11.3", "mysql": "^2.8.0", + "randomstring": "^1.0.7", "serve-favicon": "^2.3.0" } } diff --git a/www/challenge.html b/www/challenge.html index 4d82100..5b626c9 100644 --- a/www/challenge.html +++ b/www/challenge.html @@ -45,12 +45,12 @@

@@ -64,7 +64,7 @@
by <%= credits%>
+ style="float: right;" type="submit" id="challenge_sendanswer" value="send answer">