Skip to content

Commit 8546bc1

Browse files
committed
Merge branch 'master' of http://github.com/shaunph/CourseProject into InitFix
2 parents 6614b54 + 07409f4 commit 8546bc1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1878
-635
lines changed

src/dynamic/UserProfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require('pagemaker');
33
exports.getReq = function(req, res) {
44

55

6-
var page = new StandardPage();
6+
var page = new StandardPage(req);
77
page.setContent("<h1>Not yet implemented</h1>");
88
page.standardMenus();
99

src/dynamic/checkCredentials.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var db = require('SQLiteHelper');
2+
var qs = require('querystring');
3+
var upops = require('uploadOps');
4+
var pagemaker = require('pagemaker');
5+
6+
module.exports.postReq = function(request, response, dataBuffer) {
7+
8+
try {
9+
10+
var parsed = upops.parseMultipartFormdata(dataBuffer);
11+
12+
db.userExists(parsed["Email"].toString(), function(codes) {
13+
if(codes.status == 0 && codes.exists == true) {
14+
db.getUser(parsed["Email"].toString(), function(codes) {
15+
16+
//right here we need to make sure the passwords match before success screen
17+
if(parsed["Password"].toString() == codes.rows[0].password) {
18+
//success
19+
response.writeHead(200, { 'content-type':'text/html',
20+
'set-cookie': 'Email='+codes.rows[0].email + ' ; ; ' +
21+
'Nickname='+codes.rows[0].nickname
22+
});
23+
var page = new StandardPage(request);
24+
page.setTitle("Login Successful");
25+
page.setContent("<h1>User found</h1>");
26+
page.standardMenus();
27+
response.end(page.toHTML());
28+
}
29+
else {
30+
//failure
31+
response.writeHead(200, { 'content-type':'text/html' });
32+
var page = new StandardPage(request);
33+
page.setTitle("Login Denied");
34+
page.setContent("<h1> Denied </h1>");
35+
page.addContent("<h2> Username and password don't match </h2>");
36+
page.addContent("<p><a href=\"/login.html\">Try again</a></p>");
37+
page.standardMenus();
38+
response.end(page.toHTML());
39+
}
40+
});
41+
}
42+
else if(codes.status == 0 && codes.exists == false) {
43+
response.writeHead(200, {'content-type':'text/html'});
44+
var page = new StandardPage(request);
45+
page.setTitle("Login");
46+
page.setContent("<h1>User not found</h1>");
47+
page.addContent("<p><a href=\"/login.html\">Try again</a></p>");
48+
page.standardMenus();
49+
response.end(page.toHTML());
50+
}
51+
else {
52+
response.writeHead(200, {'content-type':'text/html'});
53+
var page = new StandardPage(request);
54+
page.setTitle("Login - Error");
55+
page.setContent("<h1>There was a server error</h1>");
56+
page.addContent("<p><a href=\"/login.html\">Try again</a></p>");
57+
page.standardMenus();
58+
response.end(page.toHTML());
59+
}
60+
});
61+
62+
} catch(err) {
63+
console.log(err);
64+
}
65+
}
66+

src/dynamic/comments.js

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
var pagemaker = require('./../js/pagemaker');
2-
var slh = require("./../js/SQLiteHelper.js");
1+
var pagemaker = require('pagemaker');
2+
var slh = require('SQLiteHelper');
33
var url = require('url');
4+
var upops = require('uploadOps');
45

56
exports.getReq = function (request,response) {
6-
send(response,url.parse(request.url,true).query["taskid"]);
7+
send(response,request,url.parse(request.url,true).query["taskid"]);
78
}
89

9-
exports.postReq = function (request,response) {
10-
//TODO: Read incoming post data and store in in the database
11-
send(response,url.parse(request.url,true).query["taskid"]);
10+
exports.postReq = function (request,response, dataBuffer) {
11+
var parsed = upops.parseMultipartFormdata(dataBuffer);
12+
13+
//Add comment to the database (truncated to 500 characters)
14+
slh.addComment(parsed["thecomment"].toString().substring(0,1024), parsed["taskid"].toString(),
15+
parsed["email"].toString(), function(obj) {
16+
if(obj.status != 0) {
17+
//TODO: Send a 500 error page
18+
console.log("Error saving comment: " + obj.detail);
19+
return;
20+
}
21+
else {
22+
// Once comment has been added, display the comments for the task
23+
send(response, request, url.parse(request.url,true).query["taskid"]);
24+
}
25+
});
1226
}
1327

14-
send = function (response, taskId) {
28+
send = function (response,request, taskId) {
1529
response.writeHead(200, {'Content-Type': 'text/html'});
1630

1731
// If taskid isn't set, return an error
1832
if (taskId == undefined) {
19-
var page = new StandardPage();
33+
var page = new StandardPage(request);
2034

2135
page.addContent("<div><span>Error: No Task ID defined.</span></div>" +
2236
"<br />");
@@ -34,46 +48,70 @@ send = function (response, taskId) {
3448
return;
3549
}
3650

37-
var page = new StandardPage();
51+
var page = new StandardPage(request);
3852
page.setTitle("Comments for task " + taskId);
39-
40-
// Add comment form
41-
page.addContent("<div><span>Enter a comment:</span></div>" +
42-
"<br />");
43-
44-
page.addContent("<form action='comments.js' method='post'>" +
45-
"<textarea name='thecomment' rows='4' cols='50'></textarea><br />" +
46-
"<input type='hidden' name='taskid' value=" + taskId + ">" +
47-
"<input type='submit' value='Save'>" +
48-
"</form>");
49-
53+
54+
var cookies = upops.parseCookies(request.headers);
55+
56+
// Show add comement box if user is logged in
57+
if(cookies.Email) {
58+
// Add comment form
59+
page.addContent("<div><span>Enter a comment:</span></div>" +
60+
"<br />");
61+
62+
page.addContent("<script type='text/javascript'>" +
63+
"function validate(form) {" +
64+
"var c = document.getElementById('thecomment');" +
65+
"if(!/\\S/.test(c.value)){" +
66+
"window.alert('Please enter a comment');" +
67+
"return;" +
68+
"}" +
69+
"else if(c.value.length > '1024'){" +
70+
"window.alert('This comment exceeds the maximum length of 500 characters.');" +
71+
"return;" +
72+
"}" +
73+
"form.form.submit();" +
74+
"}" +
75+
"</script>");
76+
77+
page.addContent("<form action='comments?taskid=" + taskId + "' method='post' enctype='multipart/form-data'>" +
78+
"<textarea name='thecomment' id='thecomment' rows='4' cols='50'></textarea><br />" +
79+
"<input type='hidden' name='taskid' value='" + taskId + "'>" +
80+
"<input type='hidden' name='email' value='"+cookies.Email+"'>" +
81+
"<button type='button' class='rounded' id='enter' onclick='validate(this)'>" +
82+
"<span>Save</span>" +
83+
"</button>" +
84+
"</form>");
85+
}
86+
else {
87+
page.addContent("<div><span style='font-weight:bold'>Login to add a comment.</span></div>" +
88+
"<br />");
89+
}
90+
5091
var numComments = obj.rows.length;
5192

5293
// If there are no comments for the task, say so
5394
if (numComments == 0) {
5495
page.addContent("<div><span>There are currently no comments for this task.</span></div>" +
5596
"<br />");
5697
}
57-
// Otherwise, display all comments
98+
// Otherwise, display all comments (newest on top)
5899
else {
59-
for (i = 0; i < numComments; i++) {
60-
page.addContent("<div>" +
100+
for (i = numComments-1; i >= 0; i--) {
101+
page.addContent("<div class='commentBox'>" +
61102
"<span>" + obj.rows[i].email + " says: </span>" +
62103
"<br />" +
104+
"<span class='timeStamp'>" + obj.rows[i].created + "</span>" +
105+
"<br /><br />" +
63106
"<span>" + obj.rows[i].thecomment + "</span>" +
64107
"</div>" +
65-
"<br />");
108+
"<br /><br />");
66109
}
67110
}
68111

69112
page.standardMenus();
70-
71113
response.write(page.toHTML());
72-
73114
response.end();
74-
75115
});
76-
77116
}
78-
79-
}
117+
}

src/dynamic/cookiesExample/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports.getReq = function (request,response) {
4141
//If the user is logged in
4242
} else if (cookies.user!=undefined) {
4343
//Begin dynamic page generation
44-
page1 = new StandardPage();
44+
page1 = new StandardPage(request);
4545
//Set the title
4646
page1.setTitle("Cookies Game");
4747
//Set the initial content

src/dynamic/error_pages/errorPage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ exports.getReq = function(request, response, code) {
2525

2626
function respond404(request, response) {
2727

28-
var page404 = new StandardPage();
28+
var page404 = new StandardPage(request);
2929
page404.standardMenus();
3030
page404.setTitle("404 Page not found");
3131
page404.setContent("<h3>I swear it was here before...</h3>");
@@ -37,7 +37,7 @@ function respond404(request, response) {
3737

3838
function respond500(request, response) {
3939

40-
var page500 = new StandardPage();
40+
var page500 = new StandardPage(request);
4141
page500.standardMenus();
4242
page500.setTitle("500 Server error");
4343
page500.setContent("<h3>Don't worry, we've got a great warranty...</h3>");
@@ -57,4 +57,4 @@ function respondOther(request, response, code) {
5757
pageError.addContent("<h1>500</h1>")
5858
response.write(pageError.toHTML());
5959
response.end();
60-
}
60+
}

src/dynamic/example.js

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports.getReq = function (request,response) {
99
send = function (response, parameters) {
1010
response.writeHead(200, {'Content-Type': 'text/html'});
1111

12-
page1 = new StandardPage();
12+
page1 = new StandardPage(request);
1313
page1.setTitle("Test Page");
1414
page1.setContent("This is a dynamically generated test page <br />");
1515
page1.addContent("Here are the parameters passed to this page: <br />");

src/dynamic/feedGen.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var pagemake = require('pagemaker');
1313
*/
1414
exports.getReq = function (request,response) {
1515
response.writeHead(200, {'Content-Type': 'text/html'});
16+
response.write("<h1>Feeds</h1>");
1617

1718
db.getRecentActivity(function (error){
1819
if (error.status != 0){

src/dynamic/formPostExample/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ write the actual file, once one can determine the current user
1717
exports.postReq = function (request, response, dataBuffer) {
1818
//Extract the file from the data Buffer.
1919
try {
20-
page1 = new StandardPage();
20+
page1 = new StandardPage(request);
2121
page1.setTitle("Test Page");
2222
page1.setContent("This is a dynamically generated test page <br />");
2323
page1.standardMenus();

src/dynamic/logout.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var upops = require('uploadOps');
2+
3+
module.exports.getReq = function(req, res) {
4+
5+
try {
6+
var cookies = upops.parseCookies(req.headers);
7+
var page = new StandardPage();
8+
page.standardMenus();
9+
page.setTitle("Logout");
10+
page.setContent("<h1>Logout successful</h1>");
11+
12+
res.writeHead(200, { 'Content-Type': 'text/html',
13+
'Set-Cookie': 'Email=' + cookies.Email + '; expires=Thu, 01-Jan-1970 00:00:01 GMT;' + ';' +
14+
'Nickname=' + cookies.Nickname + '; expires=Thu, 01-Jan-1970 00:00:01 GMT;'});
15+
res.end(page.toHTML());
16+
} catch(err) {
17+
console.log("There was an error: " + err);
18+
}
19+
}

src/dynamic/menu2example.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var pagemaker = require('pagemaker');
2+
3+
exports.getReq = function (request, response) {
4+
response.writeHead(200, {'Content-Type': 'text/html'});
5+
6+
var page1 = new StandardPage();
7+
page1.setTitle("onClick Menu Demo");
8+
page1.setContent("<h1> Try the menu!</h1>");
9+
10+
page1.standardMenus();
11+
12+
page1.addOnClickItem("Big Feed", "pullGenFeedsTo('column2')");
13+
page1.addOnClickItem("Nest this Page", "pullURLTo('/menu2example','column2')");
14+
page1.addOnClickItem("Alert", "alertfunc();");
15+
page1.addContent("<script type='text/javascript'>function alertfunc() { alert('This menu item works properly') }</script>");
16+
17+
response.write(page1.toHTML());
18+
19+
response.end();
20+
21+
}

0 commit comments

Comments
 (0)