ajax.pl
677 lines of code
1
#!/usr/bin/perl
2
3
use strict;
4
use warnings;
5
use CGI::Carp qw(fatalsToBrowser);
6
use URI::Escape;
7
8
use lib "/var/www/html/Pm";
9
10
use Html qw(pre_html_header);
11
use Bc_misc qw(clear_spaces minify_js);
12
13
use Security;
14
#Security::count_hits();
15
16
my $DEBUG = 0;
17
18
my $javascript = <<END;
19
var rerun_ajax_time = 25;
20
21
var verifying_label = "Verifying...";
22
var unavailable_label = "Unavailable";
23
var available_label = "Available";
24
var invalid_label = "Invalid";
25
26
var validating_nn = false;
27
var validating_e = false;
28
29
var prev_ajax_nn = 0;
30
var prev_ajax_e = 0;
31
var prev_validated_nn = 0;
32
var prev_validated_e = 0;
33
var prev_input_nn = 0;
34
var prev_indicator_nn = 0;
35
var prev_input_e = 0;
36
var prev_indicator_e = 0;
37
38
END
39
40
if ($DEBUG) {
41
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
42
}
43
44
$javascript .= <<END;
45
async function getURLResponse(url) {
46
  let response = await fetch(url);
47
48
  if (response.ok) {
49
    let t = await response.text();
50
    return t;
51
  } else {
52
    throw new Error(response.statusText);
53
    console.log("server error: " + response.statusText);
54
  }
55
}
56
57
END
58
59
if ($DEBUG) {
60
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
61
}
62
63
$javascript .= <<END;
64
let xhttp = 0;
65
let xhttpurls = [];
66
67
function addhrequest(url) {
68
  let DEBUG = 1;
69
  let debugstr = "";
70
71
  let oldURL = url;
72
  let newURL = url;
73
  let index = 0;
74
  index = oldURL.indexOf('?');
75
  if(index == -1){
76
    index = oldURL.indexOf('#');
77
  }
78
  if(index != -1){
79
    newURL = oldURL.substring(0, index);
80
  }
81
82
  let added = false;
83
84
  if (DEBUG) { console.log(debugstr); }
85
86
  return added;
87
}
88
89
function hrequest(url, abort) {
90
  let DEBUG = 0;
91
  if (DEBUG) console_msg("hrequest: " + url);
92
93
  return new Promise(function(resolve, reject) {
94
    if (abort) { if (xhttp) { xhttp.abort(); } }
95
96
    xhttp = new XMLHttpRequest();
97
98
    xhttp.onload = function() {
99
      if (xhttp.readyState == 4 && xhttp.status == 200) {
100
        resolve(xhttp.response);
101
      } else {
102
        //reject(Error(xhttp.statusText));
103
      }
104
    }
105
106
    xhttp.onerror = function() {
107
      reject(Error("Network Error"));
108
    };
109
110
    xhttp.open('GET', url, true);
111
    xhttp.send();
112
  });
113
}
114
115
function hrequest2(url, callback) {
116
  let DEBUG = 0;
117
  if (DEBUG) console_msg("hrequest2: " + url);
118
119
  let result = new Promise(function(resolve, reject) {
120
    var xhttp = new XMLHttpRequest();
121
122
    xhttp.onload = function() {
123
      if ( xhttp.readyState == 4 && xhttp.status == 200 ) {
124
        resolve(xhttp.response);
125
      } else {
126
        reject(Error(xhttp.statusText));
127
      }
128
    }
129
130
    xhttp.onerror = function() {
131
      reject(Error("Network Error"));
132
    };
133
134
    xhttp.open('GET', url, true);
135
    xhttp.send();
136
  });
137
138
  result.then(function(str) {
139
    return str;
140
  });
141
}
142
143
END
144
145
if ($DEBUG) {
146
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
147
}
148
149
$javascript .= <<END;
150
var userDropDownOffset = 0;
151
function get_users_forDropdowns(eid, start, selected, highlight) {
152
  let DEBUG = 0;
153
  if (DEBUG) console_msg(selected);
154
155
  if (start < 0) { start = 0; }
156
  if (DEBUG) console_msg(start);
157
  var el = document.getElementById(eid);
158
  var url = "/getusers.pl?a=1&o=1"; // a=1 means all users and o=1 means ordered by nickname
159
  if (start) { url += "&s=" + start; }
160
  if (highlight) { url += "&hi=" + highlight; }
161
  if (DEBUG) console_msg(url);
162
163
  if(el == null) {
164
    if (DEBUG) console_msg("HTML element ID '" + eid + "' does not exist!");
165
  } else {
166
    el.innerHTML = "<option disabled selected value=''>loading...</option>";
167
    hrequest(url).then(function(data) {
168
      el.innerHTML = "";
169
      ret = data.split("-----");
170
171
      if (selected == -1) {
172
        var opt = document.createElement("option");
173
        opt.text = "Pick One";
174
        opt.selected = true;
175
        opt.disabled = true;
176
        el.add(opt);
177
      }
178
179
      for (i = 0; i < ret.length; i++) {
180
        temp = ret[i].split("=");
181
        var opt = document.createElement("option");
182
        if (temp[0].startsWith("*")) {
183
          opt.classList.add('highlight');
184
          temp[0] = temp[0].substr(1);
185
        }
186
        opt.text = temp[1];
187
        opt.value = temp[0];
188
        if (selected == i) { opt.selected = true; }
189
        el.add(opt);
190
      }
191
    });
192
  }
193
}
194
195
END
196
197
if ($DEBUG) {
198
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
199
}
200
201
$javascript .= <<END;
202
function update_friend_icon() {
203
  return 0;
204
}
205
END
206
207
if ($DEBUG) {
208
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
209
}
210
211
$javascript .= <<END;
212
function update_mail_icon() {
213
  return 0;
214
}
215
END
216
217
if ($DEBUG) {
218
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
219
}
220
221
$javascript .= <<END;
222
function chat_del_msgs() {
223
  var xhttp = new XMLHttpRequest();
224
  var e = document.getElementById("msgsbox");
225
  var to_uid = document.getElementById("to_uid");
226
227
  xhttp.onreadystatechange = function() {
228
                                          if (xhttp.readyState == 4 && xhttp.status == 200) {
229
                                            if (xhttp.responseText) {
230
                                              e.innerHTML = e.innerHTML + xhttp.responseText;
231
                                            } else {
232
                                              e.innerHTML = e.innerHTML + '<br>failed to disband msgs';
233
                                            }
234
                                          }
235
                                        }
236
237
  xhttp.open("GET", "/delchat.pl?uid=" + to_uid.value, true);
238
  xhttp.send();
239
}
240
241
END
242
243
if ($DEBUG) {
244
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
245
}
246
247
$javascript .= <<END;
248
function chat_send_msg() {
249
  var msg = document.getElementById("msg");
250
  if (msg.value) {
251
    var xhttp = new XMLHttpRequest();
252
    var e = document.getElementById("msgsbox");
253
    var subj = document.getElementById("subject");
254
    var to_uid = document.getElementById("to_uid");
255
256
    xhttp.onreadystatechange = function() {
257
                                            if (xhttp.readyState == 4 && xhttp.status == 200) {
258
                                              if (xhttp.responseText) {
259
                                                e.innerHTML = e.innerHTML + xhttp.responseText;
260
                                                msg.value = '';
261
                                              } else {
262
                                                e.innerHTML = e.innerHTML + '<br>failed to deliver msg';
263
                                              }
264
                                            }
265
                                          }
266
267
    xhttp.open("GET", "/sendmsg.pl?uid=" + to_uid.value + "&subject=" + subj.value + "&msg=" + msg.value, true);
268
    xhttp.send();
269
  }
270
271
  msg.focus();
272
}
273
274
END
275
276
if ($DEBUG) {
277
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
278
}
279
280
$javascript .= <<END;
281
var gettingMessage = 0;
282
function chat_get_msgs(uid, e) {
283
  if (gettingMessage == 1) {
284
    console_msg('currently awaiting server reply');
285
    return;
286
  } else {
287
    console_msg('retrieving msgs');
288
  }
289
  gettingMessage = 1;
290
  var xhttp = new XMLHttpRequest();
291
292
  xhttp.onreadystatechange = function() {
293
                                          if (xhttp.readyState == 4 && xhttp.status == 200) {
294
                                            if (xhttp.responseText) {
295
                                              var wtf = xhttp.responseText;
296
                                              if (e.innerHTML == wtf) {
297
                                                console_msg('no changes detected');
298
                                              } else {
299
                                                console_msg('chat updated');
300
                                                e.innerHTML = unescape(wtf);
301
                                              }
302
                                            } else {
303
                                              e.innerHTML = 'no messages were found';
304
                                            }
305
306
                                            gettingMessage = 0;
307
                                          } else {
308
                                            gettingMessage = 1;
309
                                          }
310
                                        }
311
312
  xhttp.open("GET", "/chat_get_msgs.pl?uid=" + uid, true);
313
  xhttp.send();
314
}
315
316
END
317
318
if ($DEBUG) {
319
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
320
}
321
322
$javascript .= <<END;
323
function store_update_coins() {
324
  var e = document.getElementById("user_coins");
325
  var t = 1000;
326
327
  if (e) {
328
    var xhttp = new XMLHttpRequest();
329
330
    xhttp.onreadystatechange = function() {
331
                                            if (xhttp.readyState == 4 && xhttp.status == 200) {
332
                                              if (xhttp.responseText) {
333
                                                var c = xhttp.responseText;
334
                                                ////console_msg(c + " coins");
335
                                                e.innerHTML = c;
336
                                              } else {
337
                                                ////console_msg("no reply!");
338
                                              }
339
340
                                              setTimeout(store_update_coins, t);
341
                                            }
342
                                          };
343
344
    xhttp.open("GET", "checkcoins.pl", true);
345
    xhttp.send();
346
    console_msg("checking coins...");
347
  } else {
348
    console_msg("checking coins failed!");
349
350
    setTimeout(store_update_coins, t);
351
  }
352
}
353
354
END
355
356
if ($DEBUG) {
357
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
358
}
359
360
$javascript .= <<END;
361
function populate_with_cities(country_element_id, city_element_id, add999) {
362
  var xhttp = new XMLHttpRequest();
363
  document.getElementById(city_element_id).disabled = true;
364
  document.getElementById(city_element_id).innerHTML = "";
365
  xhttp.onreadystatechange = function() {
366
                               if (xhttp.readyState == 4 && xhttp.status == 200) {
367
                                 var rt = xhttp.responseText;
368
                                 if (rt) {
369
                                   var arr = rt.split("\\n");
370
                                   var citiesDD = "";
371
                                   document.getElementById(city_element_id).disabled = false;
372
                                   for (i = 0; i < arr.length; i++) {
373
                                     var info = arr[i].split("=");
374
                                     citiesDD = citiesDD + "<option value='" + info[0] + "'>" + info[1] + "\\n";
375
                                   }
376
                                   document.getElementById(city_element_id).innerHTML = citiesDD;
377
                                 } else {
378
                                 }
379
                               }
380
                             };
381
382
  var url = "cities.pl?c=" + document.getElementById(country_element_id).value;
383
  if (add999) { url = url + "&add999=" + add999; }
384
  xhttp.open("GET", url, true);
385
  xhttp.send();
386
}
387
388
END
389
390
if ($DEBUG) {
391
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
392
}
393
394
$javascript .= <<END;
395
function hide_indicators(indicator, show) {
396
  var i1 = document.getElementById(indicator + "_waiting");
397
  var i2 = document.getElementById(indicator + "_invalid");
398
  var i3 = document.getElementById(indicator + "_available");
399
  var i4 = document.getElementById(indicator + "_unavailable");
400
401
  if (show == "") {
402
    i1.style.display = "none";
403
    i2.style.display = "none";
404
    i3.style.display = "none";
405
    i4.style.display = "none";
406
  } else {
407
    if (show == "wait") i1.style.display = "inline"; else i1.style.display = "none";
408
    if (show == "invalid") i2.style.display = "inline"; else i2.style.display = "none";
409
    if (show == "avail") i3.style.display = "inline"; else i3.style.display = "none";
410
    if (show == "unavail") i4.style.display = "inline"; else i4.style.display = "none";
411
  }
412
}
413
414
END
415
416
if ($DEBUG) {
417
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
418
}
419
420
$javascript .= <<END;
421
function getVisibleIndicator(indicator) {
422
  var i1 = document.getElementById(indicator + "_waiting");
423
  var i2 = document.getElementById(indicator + "_invalid");
424
  var i3 = document.getElementById(indicator + "_available");
425
  var i4 = document.getElementById(indicator + "_unavailable");
426
427
  if (i1.style.display == "inline") { return i1; }
428
  if (i2.style.display == "inline") { return i2; }
429
  if (i3.style.display == "inline") { return i3; }
430
  if (i4.style.display == "inline") { return i4; }
431
432
  return 0;
433
}
434
435
END
436
437
if ($DEBUG) {
438
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
439
}
440
441
$javascript .= <<END;
442
function check_validity(input_element_id, indicator, checktype) {
443
  var e = document.getElementById(input_element_id);
444
  var ind = document.getElementById(indicator);
445
446
  if (e.value == "") {
447
    //console_msg(checktype + " " + invalid_label);
448
    return;
449
  } else {
450
    console_msg("checking validity...");
451
  }
452
453
  var xhttp = new XMLHttpRequest();
454
  xhttp.onreadystatechange = function() {
455
                               if (xhttp.readyState == 4 && xhttp.status == 200) {
456
                                 if (xhttp.responseText != 1) {
457
                                   //console_msg(checktype + " reply received: unavailable");
458
                                   hide_indicators(indicator, "unavail");
459
                                 } else {
460
                                   //console_msg(checktype + " reply received: available");
461
                                   hide_indicators(indicator, "avail");
462
                                 }
463
464
                                 if (checktype == "nn")
465
                                   { prev_ajax_nn = 0; validating_nn = false; } else
466
                                   { prev_ajax_e = 0; validating_e = false; }
467
                               }
468
                             };
469
470
  var urL = "verifynickname.pl?nn=";
471
  var uri = encodeURIComponent(e.value);
472
473
  if (checktype == "nn")
474
    { prev_ajax_nn = xhttp; validating_nn = true; } else
475
    { last_ajax_e = xhttp; validating_e = true; urL = "verifyemail.pl?e="; }
476
477
  xhttp.open("GET", urL + uri, true);
478
  xhttp.send();
479
  //console_msg(checktype + " request (" + urL + uri + ") sent");
480
}
481
482
END
483
484
if ($DEBUG) {
485
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
486
}
487
488
$javascript .= <<END;
489
function check_nn_valid(input_element_id, indicator_id) {
490
  console_msg("checking nickname's validity...");
491
492
  var input_nn = document.getElementById(input_element_id);
493
  var lastVis = getVisibleIndicator(indicator_id);
494
495
  if (input_nn.value)
496
    { hide_indicators(indicator_id, "wait"); } else
497
    { hide_indicators(indicator_id, "invalid"); }
498
499
  console_msg(input_nn.value + "-" + prev_input_nn);
500
501
  if (input_nn.value != prev_input_nn) {
502
    prev_input_nn = input_nn.value;
503
    if (!validating_nn) {
504
      check_validity(input_element_id, indicator_id, "nn");
505
    } else {
506
      if (prev_ajax_nn) {
507
        prev_ajax_nn.onreadystatechange = function() { console_msg("prev nn check aborted"); };
508
        prev_ajax_nn.abort();
509
        //console_msg("prev nn check aborted");
510
        check_validity(input_element_id, indicator_id, "nn");
511
      }
512
    }
513
  } else {
514
    hide_indicators(indicator_id, "");
515
    if (lastVis)
516
      { lastVis.style.display = "inline"; } else
517
      { hide_indicators(indicator_id, "valid"); }
518
  }
519
}
520
521
END
522
523
if ($DEBUG) {
524
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
525
}
526
527
$javascript .= <<END;
528
function check_e_valid(input_element_id, indicator_id) {
529
  var input_e = document.getElementById(input_element_id);
530
  var lastVis = getVisibleIndicator(indicator_id);
531
532
  if (input_e.value)
533
    { hide_indicators(indicator_id, "wait"); } else
534
    { hide_indicators(indicator_id, "invalid"); }
535
536
  //console_msg(input_e.value + "-" + prev_input_e);
537
  if (input_e.value != prev_input_e) {
538
    prev_input_e = input_e.value;
539
    if (!validating_e) {
540
      check_validity(input_element_id, indicator_id, "e");
541
    } else {
542
      if (prev_ajax_e) {
543
        prev_ajax_e.onreadystatechange = function() { };
544
        prev_ajax_e.abort();
545
        //console_msg("prev e check aborted");
546
        check_validity(input_element_id, indicator_id, "e");
547
      }
548
    }
549
  } else {
550
    hide_indicators(indicator_id, "");
551
    if (lastVis)
552
      { lastVis.style.display = "inline"; } else
553
      { hide_indicators(indicator_id, "valid"); }
554
  }
555
}
556
557
END
558
559
if ($DEBUG) {
560
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
561
}
562
563
$javascript .= <<END;
564
function update_friend_icon() {
565
  return 0;
566
}
567
568
END
569
570
if ($DEBUG) {
571
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
572
}
573
574
$javascript .= <<END;
575
function update_mailbox_icon() {
576
  return 0;
577
}
578
579
END
580
581
if ($DEBUG) {
582
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
583
}
584
585
$javascript .= <<END;
586
function server_maint() {
587
  var xhttp = new XMLHttpRequest();
588
  var img = document.getElementById("indicator");
589
590
  xhttp.onreadystatechange = function() {
591
                                          if (xhttp.readyState == 4 && xhttp.status == 200) {
592
                                            if (xhttp.responseText == 1) {
593
                                              //console_msg("server active!");
594
                                              img.src = "/images/yellow/emotion_exciting.png";
595
                                              document.location.reload();
596
                                            } else {
597
                                              //console_msg("server inactive!");
598
                                              img.src = "/images/yellow/emotion_waaaht.png";
599
600
                                              setTimeout(server_maint, 5000);
601
                                            }
602
                                          }
603
                                        }
604
605
  xhttp.open("GET", "checkserver.pl", true);
606
  xhttp.send();
607
  //console_msg("checking server status...");
608
  img.src = "/images/yellow/emotion_detective.png";
609
}
610
611
END
612
613
if ($DEBUG) {
614
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
615
}
616
617
$javascript .= <<END;
618
function get_themes_forDropdowns(eid, a) {
619
  var e = document.getElementById(eid);
620
  if (e) {
621
    var xhttp = new XMLHttpRequest();
622
    xhttp.onreadystatechange = function() {
623
                                            if (xhttp.readyState == 4 && xhttp.status == 200) {
624
                                              if (xhttp.responseText) {
625
                                                //console_msg("data received!");
626
                                                let a = xhttp.responseText.split("-----");
627
                                                e.innerHTML = "";
628
                                                for (i = 0; i < a.length; i++) {
629
                                                  let b = a[i].split("=");
630
                                                  e.add(new Option(b[1], b[0]));
631
                                                }
632
                                              } else {
633
                                                //console_msg("no data received!");
634
                                              }
635
                                            }
636
                                          }
637
638
    e.innerHTML = "";
639
    e.add(new Option("Loading...", 0));
640
    xhttp.open("GET", "getthemes.pl?a=" + a, true);
641
    xhttp.send();
642
    //console_msg("requesting theme ID's...");
643
  } else {
644
    alert("HTML element ID '" + eid + "' does not exist!");
645
  }
646
}
647
END
648
649
if ($DEBUG) {
650
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
651
}
652
653
$javascript .= <<END;
654
function apply_theme(tid) {
655
  var blah = tid.split('=');
656
  tid = blah[1];
657
  console_msg(tid);
658
  var url = "/settheme.pl?tid=" + tid;
659
  document.location.href = url;
660
}
661
END
662
663
if ($DEBUG) {
664
  $javascript .= "/////////////////////////////////////////////////////////////////////\n\n";
665
}
666
667
$javascript .= <<END;
668
END
669
670
print pre_html_header({type=>"text/javascript", skipmaintcheck=>1});
671
if ($DEBUG) {
672
  print $javascript;
673
} else {
674
  print minify_js(clear_spaces($javascript));
675
}
676
677
exit 1;