forked from mgargard/researchers-final
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
228 lines (198 loc) · 5.86 KB
/
Copy pathutils.js
File metadata and controls
228 lines (198 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Import the Express web framework.
var express = require('express');
// Import a library for accessing couchdb.
var cradle = require('cradle');
function initializeWebApp() {
// Create and configure a new web application.
var app = express.createServer();
// Configure the Express web framework.
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('view options', { layout: false });
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
return app;
}
exports.initializeWebApp = initializeWebApp;
function connectToDatabase(name) {
if (! name) { throw Error('Must specify database name'); }
var host = 'https://sils-webinfo.iriscouch.com/';
var port = 443;
return new (
function(db) {
db.exists(function (err, exists) {
if (err) { console.log('error', err); }
else if (! exists) {
db.create();
console.log('Created new database.');
}
});
this.save = function() {
var args = arguments;
db.save.apply(db, args);
};
this.filterDocs = function (accept, callback) {
var that = this;
db.all({ include_docs: true }, function(err, rows) {
if (err) { return that.handleErr(err, callback); }
var results = [];
for (var i = 0; i < rows.length; i++) {
var doc = rows[i].doc;
if (accept(doc)) {
results.push(doc);
}
}
return callback(null, results);
});
};
this.getSome = function(item_type, query, callback) {
if (! item_type) { callback('No item type was specified'); }
this.filterDocs(function (doc) {
if (doc.type !== item_type ) { return false; }
for (var prop in query) {
if (prop in doc) {
if (typeof(query[prop]) == 'string') {
if (doc[prop].toLowerCase().indexOf(query[prop].toLowerCase()) < 0) {
return false;
}
} else {
if (doc.doc[prop] !== query[prop]) {
return false;
}
}
}
}
return true;
}, callback);
};
this.getAll = function(item_type, callback) {
if (! item_type) { callback('No item type was specified'); }
this.filterDocs(function (doc) {
return doc.type === item_type;
}, callback);
};
this.getOne = function(item_type, item_id, callback) {
var that = this;
db.get(item_id, function(err, doc) {
if (err) { return that.handleErr(err, callback); }
if (doc.type !== item_type) {
return callback({
error: 'not found',
reason: 'item ' + item_id + ' is not of type ' + item_type });
}
return callback(null, doc);
});
};
this.handleErr = function(err, callback) {
console.trace();
console.error(err);
callback(err);
};
})(new(cradle.Connection)(host, port).database(name));
}
exports.connectToDatabase = connectToDatabase;
// Authenticate a user using the HTTP Basic Authentication protocol.
function authenticateUser(db, req, res, next) {
var parts, auth, scheme, credentials;
var view, options;
// handle auth stuff
auth = req.headers["authorization"];
if (!auth){
return authRequired(res, 'Microblog');
}
parts = auth.split(' ');
scheme = parts[0];
credentials = new Buffer(parts[1], 'base64').toString().split(':');
if ('Basic' != scheme) {
return res.send('Unsupported authorization scheme', 400);
}
req.credentials = credentials;
// ok, let's look this user up
view = 'microblog/users_by_id';
options = {};
options.descending='true';
options.key=req.credentials[0];
db.view(view, options, function(err, doc) {
try {
if(doc[0].value.password===req.credentials[1]) {
next(req,res);
}
else {
throw new Error('Invalid User');
}
}
catch (ex) {
return authRequired(res, 'Microblog');
}
});
}
exports.authenticateUser = authenticateUser;
// 'Negotiate' content type; i.e. send them what they requested.
function negotiateContentType(req) {
switch(req.headers['accept']) {
case 'text/xml':
return 'text/xml';
case 'application/xml':
return 'application/xml';
case 'application/xhtml+xml':
return 'application/xhtml+xml';
default:
return 'text/html';
}
}
exports.negotiateContentType = negotiateContentType;
// Get today's date as a y-m-d string.
function today() {
var y, m, d, dt;
dt = new Date();
y = String(dt.getFullYear());
m = String(dt.getMonth()+1);
if(m.length===1) {
m = '0'+m;
}
d = String(dt.getDate());
if(d.length===1) {
d = '0'+d.toString();
}
return y+'-'+m+'-'+d;
}
exports.today = today;
// Get the current date and time as a string.
function now() {
var y, m, d, h, i, s, dt;
dt = new Date();
y = String(dt.getFullYear());
m = String(dt.getMonth()+1);
if(m.length===1) {
m = '0'+m;
}
d = String(dt.getDate());
if(d.length===1) {
d = '0'+d.toString();
}
h = String(dt.getHours()+1);
if(h.length===1) {
h = '0'+h;
}
i = String(dt.getMinutes()+1);
if(i.length===1) {
i = '0'+i;
}
s = String(dt.getSeconds()+1);
if(s.length===1) {
s = '0'+s;
}
return y+'-'+m+'-'+d+' '+h+':'+i+':'+s;
}
exports.now = now;
// Return standard 'auth required' response.
function authRequired(res,realm) {
var realm = (realm || 'Authentication Required');
res.send('Unauthorized',
{ 'WWW-Authenticate': 'Basic realm="' + realm + '"' }, 401);
}