1 | #!/usr/bin/env node
|
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 | var multiparty = require('multiparty');
|
27 | var http = require('http');
|
28 | var https = require('https');
|
29 | var util = require('util');
|
30 | var path = require("path");
|
31 | require("date-format-lite");
|
32 | var mv = require('mv');
|
33 | var fs = require('fs');
|
34 | var exec = require('child_process').exec;
|
35 | var fsExtra = require('fs-extra');
|
36 | var request = require("request");
|
37 | var needle = require('needle');
|
38 | var readChunk = require('read-chunk');
|
39 | var async = require('async');
|
40 | var mysql = require('mysql');
|
41 | var os = require('os');
|
42 | const querystring = require('querystring');
|
43 | var crypto = require('crypto');
|
44 | var get_ip = require('ipware')().get_ip;
|
45 |
|
46 |
|
47 | var httpsFlag = false;
|
48 | var serverOptions = {};
|
49 | var listenPort = 3277;
|
50 | var msg = {};
|
51 | var lang;
|
52 | var timeUnits;
|
53 | var agoDefault;
|
54 | var verbose = false;
|
55 | var currentDbServer = [];
|
56 | currentDbServer[0] = 0;
|
57 | var usage = "Usage: node loop-server-fast.js config/path/config.json config/path/messages.json [-production]\n\nOr:\n\nnpm config set loop-server-fast:configFile /path/to/your/loop/server/config.json\nnpm config set loop-server-fast:messagesFile /path/to/your/loop/server/messages.json\n[npm config set loop-server-fast:production true]\nnpm run start\n\n";
|
58 | var defaultPHPScript = "search-chat.php?";
|
59 | var defaultPHPScriptLen = defaultPHPScript.length;
|
60 |
|
61 |
|
62 | process.on('SIGINT', function() {
|
63 |
|
64 | console.log("Requesting a shutdown.");
|
65 | closeAllConnections();
|
66 | setTimeout(function() {
|
67 |
|
68 | console.log("Clean exit.");
|
69 | process.exit(0);
|
70 | }, 300);
|
71 | });
|
72 |
|
73 |
|
74 |
|
75 | if((process.argv)&&(process.argv[2])){
|
76 | var loopServerConfig = process.argv[2];
|
77 | } else {
|
78 | if(process.env.npm_package_config_configFile) {
|
79 | var loopServerConfig = process.env.npm_package_config_configFile;
|
80 | } else {
|
81 |
|
82 | console.log(usage);
|
83 | process.exit(0);
|
84 | }
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | var config = JSON.parse(fs.readFileSync(loopServerConfig));
|
91 | if(!config) {
|
92 | console.log("Couldn't find config file " + loopServerConfig);
|
93 | process.exit(0);
|
94 | }
|
95 |
|
96 |
|
97 | if(((process.argv)&&(process.argv[3]))||(process.env.npm_package_config_messagesFile)){
|
98 |
|
99 | if(process.argv[3]) {
|
100 | var loopServerMessages = process.argv[3];
|
101 | } else {
|
102 |
|
103 | var loopServerMessages = process.env.npm_package_config_messagesFile;
|
104 | }
|
105 | msg = JSON.parse(fs.readFileSync(loopServerMessages));
|
106 | if(!msg) {
|
107 | console.log("Couldn't find messages file " + loopServerMessages);
|
108 | process.exit(0);
|
109 | }
|
110 | lang = msg.defaultLanguage;
|
111 |
|
112 |
|
113 | var time = msg.msgs[lang].time;
|
114 | timeUnits = [
|
115 | { name: time.second, plural: time.seconds, limit: 60, in_seconds: 1 },
|
116 | { name: time.minute, plural: time.minutes, limit: 3600, in_seconds: 60 },
|
117 | { name: time.hour, plural: time.hours, limit: 86400, in_seconds: 3600 },
|
118 | { name: time.day, plural: time.days, limit: 604800, in_seconds: 86400 },
|
119 | { name: time.week, plural: time.weeks, limit: 2629743, in_seconds: 604800 },
|
120 | { name: time.month, plural: time.months, limit: 31556926, in_seconds: 2629743 },
|
121 | { name: time.year, plural: time.years, limit: null, in_seconds: 31556926 }
|
122 | ];
|
123 |
|
124 | agoDefault = time.ago;
|
125 |
|
126 |
|
127 | } else {
|
128 | console.log(usage);
|
129 | process.exit(0);
|
130 | }
|
131 |
|
132 |
|
133 | if((process.argv[4]) && (process.argv[4] == '-production')){
|
134 | var cnf = config.production;
|
135 | } else {
|
136 | if(process.env.npm_package_config_production) {
|
137 | if(process.env.npm_package_config_production == 'true') {
|
138 | var cnf = config.production;
|
139 | } else {
|
140 | var cnf = config.staging;
|
141 | }
|
142 |
|
143 | } else {
|
144 | var cnf = config.staging;
|
145 | }
|
146 | }
|
147 |
|
148 |
|
149 | if(process.env.npm_package_config_verbose) {
|
150 | if(process.env.npm_package_config_verbose == 'true') {
|
151 | verbose = true;
|
152 | } else {
|
153 | verbose = false;
|
154 | }
|
155 |
|
156 | }
|
157 |
|
158 |
|
159 | if(cnf.readPort) {
|
160 | listenPort = cnf.readPort;
|
161 | }
|
162 |
|
163 |
|
164 | if(cnf.httpsKey) {
|
165 |
|
166 | httpsFlag = true;
|
167 | if(!serverOptions.key) {
|
168 | serverOptions.key = fs.readFileSync(cnf.httpsKey);
|
169 | console.log("https key loaded");
|
170 | }
|
171 | }
|
172 |
|
173 | if(cnf.httpsCert) {
|
174 |
|
175 | httpsFlag = true;
|
176 | if(!serverOptions.cert) {
|
177 | serverOptions.cert = fs.readFileSync(cnf.httpsCert);
|
178 | console.log("https cert loaded");
|
179 | }
|
180 |
|
181 | }
|
182 |
|
183 | var connections = [];
|
184 |
|
185 | var closing = false;
|
186 | function closeAllConnections() {
|
187 | if(closing == false) {
|
188 | for(var ccnt = 0; ccnt< cnf.db.hosts.length; ccnt++) {
|
189 | if(connections[0][ccnt]) {
|
190 | connections[0][ccnt].end();
|
191 | }
|
192 | }
|
193 |
|
194 | if(cnf.db.scaleUp) {
|
195 | for(var scaleCnt = 0; scaleCnt< cnf.db.scaleUp.length; scaleCnt++) {
|
196 | for(var ccnt = 0; ccnt< cnf.db.scaleUp[scaleCnt].hosts.length; ccnt++) {
|
197 | if(connections[scaleCnt+1][ccnt]) {
|
198 | connections[scaleCnt+1][ccnt].end();
|
199 | }
|
200 | }
|
201 |
|
202 | }
|
203 | }
|
204 | }
|
205 |
|
206 | }
|
207 |
|
208 |
|
209 | function handleDisconnect() {
|
210 |
|
211 |
|
212 | |
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | console.log("Connecting to the database.");
|
231 | connections[0] = {};
|
232 | for(var cnt = 0; cnt< cnf.db.hosts.length; cnt++) {
|
233 |
|
234 | if(cnf.db.ssl && cnf.db.ssl.use === true) {
|
235 | var ssl = {
|
236 | ca : fs.readFileSync(cnf.db.ssl.capath)
|
237 | };
|
238 | } else {
|
239 | var ssl = null;
|
240 | }
|
241 |
|
242 | connections[0][cnt] = mysql.createConnection({
|
243 | host : cnf.db.hosts[cnt],
|
244 | user : cnf.db.user,
|
245 | password : cnf.db.pass,
|
246 | database : cnf.db.name,
|
247 | port : cnf.db.port,
|
248 | ssl : ssl
|
249 | });
|
250 |
|
251 |
|
252 | connections[0][cnt].connect(function(err) {
|
253 | if(err) {
|
254 |
|
255 | console.log('error when connecting to db:', err);
|
256 | closeAllConnections();
|
257 |
|
258 | if(closing == false) {
|
259 | closing = true;
|
260 | setTimeout(handleDisconnect, 2000);
|
261 | }
|
262 |
|
263 | }
|
264 | });
|
265 |
|
266 | connections[0][cnt].on('error', function(err) {
|
267 | console.log('db error: ', err);
|
268 |
|
269 | closeAllConnections();
|
270 |
|
271 |
|
272 | if(err.code === 'PROTOCOL_CONNECTION_LOST') {
|
273 |
|
274 |
|
275 |
|
276 | if(closing == false) {
|
277 | closing = true;
|
278 | setTimeout(handleDisconnect, 2000);
|
279 | }
|
280 | } else {
|
281 |
|
282 |
|
283 | if(closing == false) {
|
284 | closing = true;
|
285 | setTimeout(handleDisconnect, 2000);
|
286 | }
|
287 |
|
288 | }
|
289 | });
|
290 |
|
291 |
|
292 | }
|
293 |
|
294 | if(cnf.db.scaleUp) {
|
295 |
|
296 | for(var scaleCnt = 0; scaleCnt< cnf.db.scaleUp.length; scaleCnt++) {
|
297 |
|
298 | connections[scaleCnt+1] = [];
|
299 | currentDbServer[scaleCnt+1] = 0;
|
300 | dbCnf = cnf.db.scaleUp[scaleCnt];
|
301 |
|
302 | if(dbCnf.ssl && dbCnf.ssl.use === true) {
|
303 | var ssl = {
|
304 | ca : fs.readFileSync(dbCnf.ssl.capath)
|
305 | };
|
306 | } else {
|
307 | var ssl = null;
|
308 | }
|
309 |
|
310 |
|
311 | for(var cnt = 0; cnt< dbCnf.hosts.length; cnt++) {
|
312 |
|
313 | connections[scaleCnt+1][cnt] = mysql.createConnection({
|
314 | host : dbCnf.hosts[cnt],
|
315 | user : dbCnf.user,
|
316 | password : dbCnf.pass,
|
317 | database : dbCnf.name,
|
318 | port : dbCnf.port,
|
319 | ssl : ssl
|
320 | });
|
321 |
|
322 |
|
323 | connections[scaleCnt+1][cnt].connect(function(err) {
|
324 | if(err) {
|
325 | console.log('error when connecting to db:', err);
|
326 | closeAllConnections();
|
327 |
|
328 | if(closing == false) {
|
329 | closing = true;
|
330 | setTimeout(handleDisconnect, 2000);
|
331 | }
|
332 | }
|
333 | });
|
334 |
|
335 | connections[scaleCnt+1][cnt].on('error', function(err) {
|
336 | console.log('db error: ', err);
|
337 |
|
338 | closeAllConnections();
|
339 |
|
340 |
|
341 | if(err.code === 'PROTOCOL_CONNECTION_LOST') {
|
342 |
|
343 |
|
344 |
|
345 | if(closing == false) {
|
346 | closing = true;
|
347 | setTimeout(handleDisconnect, 2000);
|
348 | }
|
349 | } else {
|
350 |
|
351 |
|
352 | if(closing == false) {
|
353 | closing = true;
|
354 | setTimeout(handleDisconnect, 2000);
|
355 | }
|
356 | }
|
357 |
|
358 |
|
359 | });
|
360 |
|
361 |
|
362 | }
|
363 | }
|
364 |
|
365 | }
|
366 |
|
367 | if(closing == true) {
|
368 |
|
369 | console.log("Finished connecting to the database.");
|
370 | closing = false;
|
371 | }
|
372 |
|
373 |
|
374 | }
|
375 |
|
376 | handleDisconnect();
|
377 |
|
378 | function escapeRegExp(str) {
|
379 | return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
|
380 | }
|
381 |
|
382 |
|
383 | function cleanData(str)
|
384 | {
|
385 |
|
386 | return escapeRegExp(str);
|
387 | }
|
388 |
|
389 |
|
390 | function trimChar(string, charToRemove) {
|
391 | while(string.substring(0,1) == charToRemove) {
|
392 | string = string.substring(1);
|
393 | }
|
394 |
|
395 | while(string.slice(-1) == charToRemove) {
|
396 | string = string.slice(0, -1);
|
397 | }
|
398 |
|
399 | return string;
|
400 | }
|
401 |
|
402 |
|
403 |
|
404 |
|
405 | function readSession(params, cb)
|
406 | {
|
407 |
|
408 | |
409 |
|
410 |
|
411 |
|
412 |
|
413 | var keyValues = {};
|
414 |
|
415 | if(verbose == true) console.log("SessionID" + cleanData(params.sessionId));
|
416 |
|
417 | if(!params.connection) {
|
418 |
|
419 | cb(null);
|
420 | return;
|
421 |
|
422 | }
|
423 |
|
424 | params.connection.query("SELECT * FROM php_session WHERE session_id='" + cleanData(params.sessionId) + "'", function(err, rows, fields) {
|
425 |
|
426 | if (err) {
|
427 | console.log("Database error:" + err);
|
428 | cb(null);
|
429 | } else {
|
430 |
|
431 |
|
432 | if((rows[0])&&(rows[0].session_data)) {
|
433 |
|
434 | if(verbose == true) console.log("Session data:" + rows[0].session_data);
|
435 | var params = rows[0].session_data.split(";");
|
436 | for(var cnt=0; cnt< params.length; cnt++) {
|
437 |
|
438 | var paramData = params[cnt].split("|");
|
439 | if(paramData[1]) {
|
440 |
|
441 | var paramValues = paramData[1].split(":");
|
442 | if(paramValues[0] == 'i') {
|
443 |
|
444 | var paramValue = paramValues[1];
|
445 | } else {
|
446 |
|
447 | var paramValue = trimChar(paramValues[2], '"');
|
448 | }
|
449 |
|
450 | keyValues[paramData[0]] = paramValue;
|
451 | if(verbose == true) console.log("Key:" + paramData[0] + " = " + paramValue);
|
452 | }
|
453 | }
|
454 | }
|
455 |
|
456 | cb(keyValues);
|
457 | }
|
458 | });
|
459 |
|
460 | }
|
461 |
|
462 |
|
463 |
|
464 | function httpHttpsCreateServer(options) {
|
465 | if(httpsFlag == true) {
|
466 | console.log("Starting https server.");
|
467 | https.createServer(options, handleServer).listen(listenPort);
|
468 |
|
469 |
|
470 | } else {
|
471 | console.log("Starting http server.");
|
472 | http.createServer(handleServer).listen(listenPort);
|
473 | }
|
474 |
|
475 |
|
476 |
|
477 | }
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 | function handleServer(_req, _res) {
|
484 |
|
485 | var req = _req;
|
486 | var res = _res;
|
487 | var body = [];
|
488 |
|
489 |
|
490 | req.on('error', function(err) {
|
491 |
|
492 | console.error(err.stack);
|
493 |
|
494 | res.statusCode = 400;
|
495 | res.end();
|
496 | });
|
497 |
|
498 | req.on('data', function(chunk) {
|
499 | body.push(chunk);
|
500 | });
|
501 |
|
502 | req.on('end', function() {
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 | if(verbose == true) console.log("Requesting: " + req.url);
|
511 |
|
512 |
|
513 | if(req.url.indexOf(defaultPHPScript) < 0) {
|
514 | res.writeHead(200, {'content-type': 'text/html'});
|
515 | res.end("Not available");
|
516 | return;
|
517 | }
|
518 |
|
519 | var url = req.url.substring(req.url.indexOf(defaultPHPScript) + defaultPHPScriptLen);
|
520 |
|
521 |
|
522 | if(verbose == true) console.log("Parsed to query string:" + url);
|
523 | var params = querystring.parse(url);
|
524 | if(verbose == true) console.log("Query params = " + JSON.stringify(params));
|
525 |
|
526 | var cookies = parseCookies(req);
|
527 | if(!params.sessionId) {
|
528 | params.sessionId = cookies.PHPSESSID;
|
529 | }
|
530 | if(!params.sessionId) {
|
531 | res.statusCode = 400;
|
532 | res.end();
|
533 | console.log("Error: You must include a PHP Session ID");
|
534 | return;
|
535 |
|
536 | }
|
537 |
|
538 | if(cookies.lang) {
|
539 |
|
540 | var time = msg.msgs[cookies.lang].time;
|
541 | params.timeUnits = [
|
542 | { name: time.second, plural: time.seconds, limit: 60, in_seconds: 1 },
|
543 | { name: time.minute, plural: time.minutes, limit: 3600, in_seconds: 60 },
|
544 | { name: time.hour, plural: time.hours, limit: 86400, in_seconds: 3600 },
|
545 | { name: time.day, plural: time.days, limit: 604800, in_seconds: 86400 },
|
546 | { name: time.week, plural: time.weeks, limit: 2629743, in_seconds: 604800 },
|
547 | { name: time.month, plural: time.months, limit: 31556926, in_seconds: 2629743 },
|
548 | { name: time.year, plural: time.years, limit: null, in_seconds: 31556926 }
|
549 | ];
|
550 |
|
551 | params.ago = time.ago;
|
552 |
|
553 | }
|
554 |
|
555 | params.ip = getFakeIpAddress(params.sessionId);
|
556 |
|
557 |
|
558 | params.connection = connections[0][currentDbServer[0]];
|
559 |
|
560 |
|
561 | currentDbServer[0] ++;
|
562 | if(currentDbServer[0] >= cnf.db.hosts.length) currentDbServer[0] = 0;
|
563 |
|
564 |
|
565 | var myres = res;
|
566 |
|
567 | var jsonData = searchProcess(params, function(err, data) {
|
568 | if(err) {
|
569 | if(err == 'PHP') {
|
570 |
|
571 |
|
572 | var fullUrl = cnf.webRoot + '/' + defaultPHPScript + url;
|
573 | if(verbose == true) console.log("Webroot:" + cnf.webRoot + " Default PHP script:" + defaultPHPScript + " Url:" + url + " fullUrl:" + fullUrl);
|
574 | callPHP(fullUrl, myres);
|
575 | return;
|
576 | }
|
577 |
|
578 | console.log(err);
|
579 | res.statusCode = 400;
|
580 | res.end();
|
581 | }
|
582 |
|
583 |
|
584 | var strData = params.callback + "(" + JSON.stringify( data ) + ")";
|
585 |
|
586 | res.on('error', function(err){
|
587 |
|
588 | res.statusCode = 400;
|
589 | res.end();
|
590 | })
|
591 |
|
592 | res.writeHead(200, {'content-type': 'text/html'});
|
593 |
|
594 |
|
595 | res.end(strData, function(err) {
|
596 |
|
597 | if(err) {
|
598 | console.log(err);
|
599 | } else {
|
600 |
|
601 | process.stdout.write(".");
|
602 | }
|
603 | });
|
604 | });
|
605 |
|
606 | });
|
607 |
|
608 | }
|
609 |
|
610 |
|
611 | function parseCookies (request) {
|
612 | var list = {},
|
613 | rc = request.headers.cookie;
|
614 |
|
615 | rc && rc.split(';').forEach(function( cookie ) {
|
616 | var parts = cookie.split('=');
|
617 | list[parts.shift().trim()] = decodeURI(parts.join('='));
|
618 | });
|
619 |
|
620 | return list;
|
621 | }
|
622 |
|
623 |
|
624 | function ago(timeStr, thisTimeUnits, agoWord) {
|
625 |
|
626 |
|
627 |
|
628 |
|
629 | var recTime = new Date(timeStr);
|
630 | var recTimeSecs = recTime.getTime();
|
631 |
|
632 |
|
633 | var nowSecs = new Date().getTime();
|
634 | var diff = (nowSecs - recTimeSecs) / 1000;
|
635 |
|
636 | var i = 0;
|
637 | var unit = {};
|
638 |
|
639 | while (unit = thisTimeUnits[i]) {
|
640 | if ((diff < unit.limit) || (!unit.limit)){
|
641 | var diff = Math.round(diff / unit.in_seconds);
|
642 | var timeOut = diff + " " + (diff>1 ? unit.plural : unit.name) + " " + agoWord;
|
643 | return timeOut;
|
644 | }
|
645 | i++;
|
646 | }
|
647 |
|
648 | return "Unknown";
|
649 |
|
650 |
|
651 |
|
652 | }
|
653 |
|
654 | function md5(data) {
|
655 |
|
656 | return crypto.createHash('md5').update(data).digest("hex");
|
657 | }
|
658 |
|
659 |
|
660 | function getFakeIpAddress(sessionId) {
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 | if(sessionId) {
|
667 | var ip = "192." + parseInt(sessionId.charCodeAt(0)) + "." +
|
668 | parseInt(sessionId.charCodeAt(1)) + "." +
|
669 | parseInt(sessionId.charCodeAt(2));
|
670 | return ip;
|
671 | } else {
|
672 |
|
673 | return "192.168.10.10";
|
674 | }
|
675 | }
|
676 |
|
677 | function getRealIpAddress(req) {
|
678 |
|
679 | var ip_info = get_ip(req);
|
680 |
|
681 | return ip_info.clientIp.replace(/^[0-9.,]+$/,"");
|
682 | }
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 | function callPHP(url, res) {
|
689 |
|
690 |
|
691 | if(verbose == true) console.log("Redirecting to " + url);
|
692 |
|
693 | res.writeHead(302, {
|
694 | 'Location': url
|
695 |
|
696 | });
|
697 | res.end();
|
698 |
|
699 |
|
700 | }
|
701 |
|
702 |
|
703 |
|
704 | function foundLayer(params,
|
705 | session,
|
706 | layer,
|
707 | ip,
|
708 | userCheck,
|
709 | initialRecords,
|
710 | outputJSON,
|
711 | debug,
|
712 | cb) {
|
713 |
|
714 | if((params.units) && (params.units != '')) {
|
715 | units = params.units;
|
716 | }
|
717 |
|
718 | if((params.dbg) && (params.dbg == 'true')) {
|
719 | debug = true;
|
720 | } else {
|
721 | debug = false;
|
722 | }
|
723 |
|
724 | if(session['logged-user']) {
|
725 | userCheck = " OR int_author_id = " + session['logged-user'] + " OR int_whisper_to_id = " + session['logged-user'];
|
726 |
|
727 | }
|
728 |
|
729 | if(session['logged-group-user']) {
|
730 | userCheck = userCheck + " OR int_author_id = " + session['logged-group-user'] + " OR int_whisper_to_id = " + session['logged-group-user'];
|
731 |
|
732 | }
|
733 |
|
734 | if((params.records) && (params.records < 100)) {
|
735 | initialRecords = 100;
|
736 | } else {
|
737 | if(params.records) {
|
738 | initialRecords = params.records;
|
739 | }
|
740 | }
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 | var sql = "SELECT * FROM tbl_ssshout WHERE int_layer_id = " + layer + " AND enm_active = 'true' AND (var_whisper_to = '' OR ISNULL(var_whisper_to) OR var_whisper_to ='" + ip + "' OR var_ip = '" + ip + "' " + userCheck + ") ORDER BY date_when_shouted DESC LIMIT " + initialRecords;
|
750 | if(verbose == true) console.log("Query: " + sql);
|
751 | if(!params.connection) {
|
752 |
|
753 | cb(err, null);
|
754 | return;
|
755 | }
|
756 |
|
757 | params.connection.query(sql, function(err, rows, fields) {
|
758 |
|
759 |
|
760 | if (err) {
|
761 |
|
762 | console.log("Database query error:" + err);
|
763 | cb(err, null);
|
764 | return;
|
765 | }
|
766 |
|
767 |
|
768 | outputJSON.res = [];
|
769 | outputJSON.ses = params.sessionId;
|
770 |
|
771 |
|
772 | var mymaxResults = rows.length;
|
773 | if(mymaxResults > params.records) {
|
774 | mymaxResults = params.records;
|
775 | var more = true;
|
776 | }
|
777 | var actualCnt = 0;
|
778 |
|
779 |
|
780 | if((session['access-layer-granted'])&&(session['access-layer-granted'] !== "true")
|
781 | &&(session['access-layer-granted'] != true)) {
|
782 | if((session['access-layer-granted'] == 'false') || (session['access-layer-granted'] != layer)) {
|
783 |
|
784 |
|
785 | if((session['access-layers-granted'])&&(Array.isArray(session['access-layers-granted']))) {
|
786 |
|
787 | var granted = session['access-layers-granted'];
|
788 | if(granted.includes(layer)) {
|
789 |
|
790 | } else {
|
791 |
|
792 | outputJSON.res = [];
|
793 | cb(null, outputJSON);
|
794 | return;
|
795 | }
|
796 |
|
797 | } else {
|
798 |
|
799 | outputJSON.res = [];
|
800 | cb(null, outputJSON);
|
801 | return;
|
802 | }
|
803 | }
|
804 | }
|
805 |
|
806 |
|
807 | var thisTimeUnits = timeUnits;
|
808 | var thisAgo = agoDefault;
|
809 | if(params.timeUnits) {
|
810 |
|
811 | thisTimeUnits = params.timeUnits;
|
812 | thisAgo = params.ago;
|
813 | }
|
814 |
|
815 |
|
816 | for(var cnt = 0; cnt< rows.length; cnt++) {
|
817 |
|
818 | var whisper = true;
|
819 | var authorIP = rows[cnt].var_ip;
|
820 | var authorUserID = rows[cnt].int_author_id;
|
821 | var combinedAuthor = authorIP;
|
822 | if(authorUserID) {
|
823 | combinedAuthor = combinedAuthor + ":" + authorUserID;
|
824 | }
|
825 |
|
826 | var whisperToIP = rows[cnt].var_whisper_to;
|
827 | var whisperToUserID = rows[cnt].int_whisper_to_id;
|
828 |
|
829 |
|
830 |
|
831 |
|
832 | if((whisperToIP == '')||
|
833 | ((whisperToIP == ip)&&(whisperToUserID == null))||
|
834 | (whisperToUserID == session['logged-user'])||
|
835 | ((authorIP == ip)&&(authorUserID == null))||
|
836 | (authorUserID == session['logged-user'])||
|
837 | ((session['logged-group-user'] != "")&&(whisperToUserID != "") && (whisperToUserID == session['logged-group-user']))) {
|
838 |
|
839 |
|
840 |
|
841 | if(((whisperToIP == ip) && (whisperToUserID == null))||
|
842 | (whisperToUserID == session['logged-user'])||
|
843 | ((authorIP == ip) && ((whisperToIP != '')||(whisperToUserID)))||
|
844 | (authorUserID == session['logged-user'] && ((whisperToIP != '')|| (whisperToUserID)))) {
|
845 |
|
846 | whisper = true;
|
847 | } else {
|
848 |
|
849 | whisper = false;
|
850 | }
|
851 |
|
852 | if(session['logged-group-user']) {
|
853 | if(whisperToUserID == session['logged-group-user']) {
|
854 | whisper = true;
|
855 |
|
856 | }
|
857 | }
|
858 |
|
859 | if(!session['logged-user']) {
|
860 |
|
861 | whisper = false;
|
862 |
|
863 | }
|
864 |
|
865 |
|
866 |
|
867 | var shade = rows[cnt].int_ssshout_id %2;
|
868 | if(layer == 0) {
|
869 |
|
870 | if(shade == 0) {
|
871 | bgcolor = "public-light";
|
872 | } else {
|
873 | bgcolor = "public-dark";
|
874 | }
|
875 | } else {
|
876 |
|
877 | if(shade == 0) {
|
878 | bgcolor = "private-light";
|
879 | } else {
|
880 | bgcolor = "private-dark";
|
881 | }
|
882 |
|
883 | }
|
884 |
|
885 |
|
886 |
|
887 |
|
888 | if(actualCnt <= mymaxResults) {
|
889 |
|
890 |
|
891 |
|
892 |
|
893 | var newEntry = {
|
894 | 'id': rows[cnt].int_ssshout_id,
|
895 | 'text': rows[cnt].var_shouted_processed,
|
896 | 'lat': rows[cnt].latitude,
|
897 | 'lon': rows[cnt].longtiude,
|
898 | 'dist': rows[cnt].dist,
|
899 | 'ago': ago(rows[cnt].date_when_shouted, thisTimeUnits, thisAgo),
|
900 | 'whisper': whisper
|
901 |
|
902 | }
|
903 |
|
904 | outputJSON.res.push(newEntry);
|
905 |
|
906 | actualCnt ++;
|
907 | }
|
908 |
|
909 |
|
910 | }
|
911 |
|
912 | }
|
913 |
|
914 | cb(null, outputJSON);
|
915 |
|
916 |
|
917 | });
|
918 |
|
919 |
|
920 | }
|
921 |
|
922 |
|
923 | function checkScaleupHorizontally(layerName, params) {
|
924 |
|
925 | if(cnf.db.scaleUp) {
|
926 |
|
927 | for(var scaleCnt = 0; scaleCnt< cnf.db.scaleUp.length; scaleCnt++) {
|
928 |
|
929 | var regExp = new RegExp(cnf.db.scaleUp[scaleCnt].labelRegExp);
|
930 | if(layerName.search(regExp) >= 0) {
|
931 |
|
932 |
|
933 | params.connection = connections[scaleCnt+1][currentDbServer[scaleCnt+1]];
|
934 |
|
935 |
|
936 | currentDbServer[scaleCnt+1] ++;
|
937 | if(currentDbServer[scaleCnt+1] >= cnf.db.scaleUp[scaleCnt].hosts.length) currentDbServer[scaleCnt+1] = 0;
|
938 | return;
|
939 | }
|
940 | }
|
941 | }
|
942 |
|
943 | return;
|
944 | }
|
945 |
|
946 |
|
947 |
|
948 | function searchProcess(params, cb) {
|
949 |
|
950 |
|
951 | checkScaleupHorizontally(params.passcode, params);
|
952 |
|
953 |
|
954 | readSession(params, function(session) {
|
955 | if(verbose == true) console.log("Finished getting session data. Logged user:" + session['logged-user']);
|
956 |
|
957 |
|
958 |
|
959 |
|
960 | if((session)&&(session['logged-user'])&&(session['logged-user'] != '')) {
|
961 |
|
962 | if((!session['user-ip'])||(session['user-ip'] == '')) {
|
963 |
|
964 | console.log('No ip. Going to PHP');
|
965 | cb("PHP", null);
|
966 | return;
|
967 | } else {
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 | if(session['view-count'] == 0) {
|
974 | console.log("view-count = " + session['view-count'] + " Going to PHP");
|
975 | cb("PHP", null);
|
976 | return;
|
977 | }
|
978 |
|
979 | var layer = 1;
|
980 | var ip = params.ip;
|
981 | var userCheck = "";
|
982 | var initialRecords = 100;
|
983 | var outputJSON = {};
|
984 | var debug = false;
|
985 |
|
986 |
|
987 | if((params.passcode) && (params.passcode != '')||((params.reading) && (params.reading != ''))) {
|
988 |
|
989 |
|
990 |
|
991 |
|
992 | var sql = "SELECT int_layer_id FROM tbl_layer WHERE passcode = '" + md5(params.passcode) + "'";
|
993 |
|
994 | if(params.connection) {
|
995 | params.connection.query(sql, function(err, rows, fields) {
|
996 |
|
997 | if(err) {
|
998 | console.log("Database error: " + err);
|
999 | return;
|
1000 | } else {
|
1001 | if((rows)&&(rows[0])) {
|
1002 | layer = rows[0].int_layer_id;
|
1003 |
|
1004 | foundLayer(params, session, layer, ip, userCheck, initialRecords, outputJSON, debug, cb);
|
1005 | } else {
|
1006 | console.log("No layer " + md5(params.passcode) + " - likely new. Going to PHP");
|
1007 | cb("PHP", null);
|
1008 | return;
|
1009 |
|
1010 |
|
1011 |
|
1012 | }
|
1013 | }
|
1014 | });
|
1015 | }
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 |
|
1022 | } else {
|
1023 |
|
1024 | if(session['authenticated-layer']) {
|
1025 | layer = session['authenticated-layer'];
|
1026 | } else {
|
1027 | layer = 1;
|
1028 | }
|
1029 |
|
1030 | foundLayer(params, session, layer, ip, userCheck, initialRecords, outputJSON, debug, cb);
|
1031 | }
|
1032 |
|
1033 |
|
1034 | }
|
1035 |
|
1036 |
|
1037 | } else {
|
1038 |
|
1039 | console.log("Not logged in - back to PHP version");
|
1040 | cb("PHP", null);
|
1041 | return;
|
1042 |
|
1043 | }
|
1044 |
|
1045 |
|
1046 | });
|
1047 |
|
1048 |
|
1049 | }
|
1050 |
|
1051 |
|
1052 |
|
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 | httpHttpsCreateServer(serverOptions);
|
1059 |
|
1060 |
|