1 | // ========================================== |
||
2 | // HELPER Prototype |
||
3 | // ========================================== |
||
4 | |||
5 | String.prototype.endsWith = function(suffix) { |
||
6 | if (!suffix) return false; |
||
0 ignored issues
–
show
|
|||
7 | return this.indexOf(suffix, this.length - suffix.length) !== -1; |
||
8 | }; |
||
9 | |||
10 | String.prototype.startsWith = function(prefix) { |
||
11 | if (!prefix) return false; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
12 | return this.indexOf(prefix) === 0; |
||
13 | }; |
||
14 | |||
15 | String.prototype.capitalize = function() { |
||
16 | return this.charAt(0).toUpperCase() + this.slice(1); |
||
17 | } |
||
18 | |||
19 | global.Helper = {}; |
||
20 | |||
21 | Helper.parse = function(str){ |
||
22 | if (str.trim) str = str.trim(); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
23 | if (str === 'true') return true; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
24 | if (str === 'false') return false; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
25 | var num = Helper.isNumeric(str) ? parseFloat(str) : NaN; |
||
26 | return isNaN(num) ? str : num; |
||
27 | } |
||
28 | |||
29 | Helper.isNumeric = function(value) { |
||
30 | return /^\d+(\.\d+)?$/.test(value); |
||
31 | } |
||
32 | |||
33 | Helper.secToTime = function(duration) { |
||
34 | var seconds = parseInt((duration)%60) |
||
35 | , minutes = parseInt((duration/60)%60) |
||
36 | , hours = parseInt((duration/(60*60))%24); |
||
37 | |||
38 | hours = (hours < 10) ? "0" + hours : hours; |
||
39 | minutes = (minutes < 10) ? "0" + minutes : minutes; |
||
40 | seconds = (seconds < 10) ? "0" + seconds : seconds; |
||
41 | |||
42 | return hours + ":" + minutes + ":" + seconds; |
||
43 | } |
||
44 | |||
45 | // ========================================== |
||
46 | // LOG MANAGER |
||
47 | // ========================================== |
||
48 | |||
49 | var winston = require('winston'); |
||
50 | winston.add(winston.transports.File, { filename: __dirname+'/data/server.log' }); |
||
51 | winston.addColors({ info : 'blue', warn : 'orange', error : 'orange' }); |
||
52 | |||
53 | // Add global function for logging |
||
54 | // global.debug = winston.debug; |
||
55 | // global.log = winston.log; |
||
56 | // global.info = winston.info; |
||
57 | // global.warn = winston.warn; |
||
58 | // global.error = winston.error; |
||
59 | |||
60 | // Catch all |
||
61 | // process.on('uncaughtException', function (err) { |
||
62 | // error('Caught exception: '+err.stack); |
||
63 | // }); |
||
64 | |||
65 | // Starting SARAH |
||
66 | // info("=========================================="); |
||
67 | // info(" STARTING SARAH Server "); |
||
68 | // info(" Path: ", __dirname); |
||
69 | // debug(" Modules: ", process.env.NODE_PATH); |
||
70 | // info("=========================================="); |
||
71 | |||
72 | |||
73 | // ========================================== |
||
74 | // SARAH |
||
75 | // ========================================== |
||
76 | |||
77 | // Add SARAH to global functions |
||
78 | // require('./server/sarah.js').init(); |
||
79 | |||
80 | |||
81 | |||
82 | // ========================================== |
||
83 | // EXPRESS SERVER |
||
84 | // ========================================== |
||
85 | |||
86 | // Init Express |
||
87 | // var __webapp = __dirname + '/webapp'; |
||
88 | // var express = require('express'); |
||
89 | // var http = require('http'); |
||
90 | |||
91 | var app = module.exports = express(); |
||
92 | var server = http.createServer(app); |
||
93 | |||
94 | // SOCKET-IO |
||
95 | // var io = require('socket.io')(server); |
||
96 | // SARAH.PluginManager.socket(io); |
||
97 | |||
98 | // Set EJS Engine |
||
99 | // var engine = require('ejs-locals'); |
||
100 | // app.engine('ejs', engine); |
||
101 | // app.engine('html', engine); |
||
102 | // app.set('views', __webapp + '/views'); |
||
103 | // app.set('view engine', 'ejs'); |
||
104 | |||
105 | // Set Middleware |
||
106 | // var multer = require('multer'); |
||
107 | // var less = require('less-middleware'); |
||
108 | // var methodOverride = require('method-override'); |
||
109 | // var serveStatic = require('serve-static'); |
||
110 | // var cookieParser = require('cookie-parser'); |
||
111 | // var bodyParser = require('body-parser'); |
||
112 | // var session = require('express-session'); |
||
113 | |||
114 | app.use(multer({ dest: __webapp+'/uploads' })) |
||
115 | app.use(methodOverride('X-HTTP-Method-Override')); |
||
116 | app.use(bodyParser.json()); |
||
117 | app.use(bodyParser.urlencoded({ extended: false })); |
||
118 | app.use(cookieParser()); |
||
119 | app.use(session({ |
||
120 | secret: 'sarah', |
||
121 | resave: true, |
||
122 | saveUninitialized: true |
||
123 | })); |
||
124 | |||
125 | // Set local helpers |
||
126 | // app.locals.SARAH = SARAH; |
||
127 | |||
128 | // Serve static files |
||
129 | // app.use(less(__webapp + '/static' , {}, {}, { 'compress' : false })); |
||
130 | // app.use(less(SARAH.ConfigManager.PLUGIN, {}, {}, { 'compress' : false })); |
||
131 | |||
132 | app.use(serveStatic(__webapp + '/static')); |
||
133 | |||
134 | // ========================================== |
||
135 | // ROUTERS |
||
136 | // ========================================== |
||
137 | |||
138 | |||
139 | var static_plugins = serveStatic(SARAH.ConfigManager.PLUGIN); |
||
140 | app.use(function(req, res, next){ |
||
141 | var path = req.path |
||
142 | if (/^(\/plugin)*\/\w+\/www\/.*$/.test(path)){ |
||
143 | if (path.startsWith('/plugin')){ |
||
144 | res.redirect(path.substring('/plugin'.length)); |
||
145 | return res.end(); |
||
146 | } |
||
147 | static_plugins(req, res, next); |
||
148 | } |
||
149 | else { next(); } |
||
150 | }); |
||
151 | |||
152 | |||
153 | app.use(SARAH.LangManager.Router); |
||
154 | app.use(SARAH.PrivacyManager.Router); |
||
155 | app.use(SARAH.PortalManager.Router); |
||
156 | app.use(SARAH.ConfigManager.Router); |
||
157 | app.use(SARAH.PluginManager.Router); |
||
158 | app.use(SARAH.ProfileManager.Router); |
||
159 | app.use(SARAH.RuleEngine.Router); |
||
160 | app.use(SARAH.Marketplace.Router); |
||
161 | app.use(SARAH.Router); |
||
162 | |||
163 | |||
164 | // ========================================== |
||
165 | // START CRON |
||
166 | // ========================================== |
||
167 | |||
168 | SARAH.CRONManager.start(); |
||
169 | |||
170 | // ========================================== |
||
171 | // START SERVER |
||
172 | // ========================================== |
||
173 | |||
174 | var webapp = server.listen(SARAH.ConfigManager.Config.http.port); |
||
175 | info("Express server listening on port: %s", webapp.address().port); |
||
176 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.