forked from jerrywickey/AJAX-Encryption
/
testJerrysLibrary.php
1257 lines (831 loc) · 52.3 KB
/
testJerrysLibrary.php
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// AJAX response code
if ( isset( $_GET['someGETvar']) || isset( $_POST['somePOSTvar'])){
$tm= microtime( true);
$dt= date( 'h:i:s');
$out= 'request received '.$dt. substr( $tm, 10, 4).'<br>';
foreach ( $_GET as $n=>$v){
$out.= 'GET: '.$n.'='.$v.'<br>';
}
foreach ( $_POST as $n=>$v){
$out.= 'POST: '.$n.'='.$v.'<br>';
}
// simulate some hard work to show off the advantages of multihttp
sleep( rand( 1, 3));
if ( $_GET['someGETvar'] > 10 || $_POST['somePOSTvar'] > 10){
foreach ( $_COOKIE as $n=>$v){
$out.= 'COOKIE: '.$n.'='.$v.'<br>';
}
session_start();
foreach ( $_SESSION as $n=>$v){
$out.= 'SESSION: '.$n.'='.$v.'<br>';
}
}
$out.= 'elapsed time '. floor(( microtime( true) - $tm) * 1000).' milliseconds<br><br>';
echo $out;
exit();
}
// AJAX encryption
if ( isset( $_POST['encrypted']) || isset( $_GET['testRememberMe'])){
include( 'jerrysLibrary.php');
$decrypted= decryptFromClient( $_POST['encrypted']);
if ( isset( $_GET['testRememberMe'])){
$decrypted= decryptFromClient( $_GET['testRememberMe']);
}
$out = 'The message that was sent to the server:<br><span style="font-size:0.25em;"><br></span><b>';
$out.= $decrypted.'</b><br><br>';
$out.= 'This message was encrypted before being sent. This is the encrypted message which the server received:<br><span style="font-size:0.25em;"><br></span><b>';
$out.= $_POST['encrypted'].$_GET['testRememberMe'] . '</b><br><br>';
$out.= 'PHP can now do something with it. Respond to it. In this example all the text you are reading in this div is the response. Before the response is sent back it must be encrypted again:<br><span style="font-size:0.25em;"><br></span><b>';
$enc= encryptToClient( $out);
$out.= str_replace( '<', '<', encryptToClient( $enc)).'</b><br><br>';
$out.= 'The client then decrypts which re-produces the whole message you are reading right now.';
echo encryptToClient( $out);
exit();
}
//redacted comment handler
$message= '';
if ( file_exists('comments.php') && true){
include ('comments.php');
}
if ( isset( $_POST['submitcomment'])){
include( 'jerrysLibrary.php');
echo encryptToClient( '<!-- 0 init -->Comments have been redacted from this server<br><br><a href="http://jerrywickey.com/test/testJerrysLibrary.php">Jerry\'s Library</a>');
//redacted
exit();
}
if ( isset( $_GET['abuse'])){
//redacted
exit();
}
if ( isset( $_GET['cryplink'])){
//redacted
exit();
}
?>
<!DOCTYPE HTML PUBLIC>
<html>
<head>
<!--
Jerry's Crypto-AJAX JavaScript and PHP Library
Jan 8, 2014 Key West, FL US
email - jerry (@-symbol-) jerrywickey (.-symbol-) com
phone - eight hundred - seven two two - two two eight zero
thanks to Stevish RSA http://stevish.com/rsa-encryption-in-pure-php
thanks to Ali Farhadi RC4 https://gist.github.com/farhadi/2185197
<!--
This newest verstion of this file along with the PHP portion
of the source can be downloaded from
http://jerrywickey.com/test/testJerrysLibrary.php
<!-- All example code is clearly remarked in this way
* initCrypto() *
*************************************************************************
* code example *
set these important constants either here or
in the first lines of the file jerrysLibrary.js -->
<script src="http://jerrywickey.com/test/jerrysLibrary.js"></script>
<script>
// path to jerrysLibrary.php domain and subdirectory
// set the directory path here. Example + '/your-path/';
// the setting below presumes you've saved the PHP library file
// to the 'test/' directory on your server.
var PHPlibpath= 'http://'+window.location.hostname + '/test/jerrysLibrary.php';
// if PHP encryption or AJAX is not desired set JL_setAjax to false
// miscellaneous functions are still availble
// this can be set in the <body onload= as well
var JL_setAjax= true;
// These security issues must be set in the first lines
// of the file jerrysLibrary.php
// someone could abuse your server if you allow xdomaim
// define ( 'ALLOWXDOMAIN', 'true'); // true / false
// store session decryption key in SESSION variable
// define ( 'STOREKEYINSESSION', 'true'); // true / false -->
</script>
<title>Jerry's Cryptography AJAX Library</title>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<meta name="viewport" content="width=800">
<style>
img.ex
{height: 0.7em; margin: 15px 5px 0px 20px; valign: bottom;}
div.code
{font-family: courier; margin: 10px 5px 10px 30px; color: #66a;}
div.sec
{font-size: 1.2em; font-weight: bold; margin: 30px 0px 15px 0px;}
div.explain
{font-size: 0.8em; font-weight: normal; color: black; margin: 15px 0px 20px 30px; padding: 5px; border-radius: 5px; background: #f4f4f4;}
div.example
{font-size: 0.8em; font-weight: normal; color: black; margin: 15px 0px 20px 30px; padding: 5px; border-radius: 5px; background: #f4f4f4;}
span.workex
{font-size: 0.7em; font-weight: normal; color: black; }
div.comment
{ margin-bottom: 15px; width: 100%; border-radius: 5px; border: thin solid #ddd; background: #eee; }
div.commentname
{padding: 3px 5px 0px 5px; border-radius: 5px; font-size: 1em; font-weight: bold; background: #eef; color: #a88; }
div.commenttext
{padding: 5px; border-radius: 5px; background: #f8f8f8; color: #888; }
span.time
{font-size: 0.7em; font-weight: normal; color: #aaa; }
div.report
{float: right; font-size: 0.7em; color: red;}
span.more
{font-size: 0.7em; color: #faa;}
</style>
</head>
<body onload="initCrypto( 1024)" style="margin: 0px; font-family: tahoma;">
<div style="<?php if ( stripos( $_SERVER['HTTP_USER_AGENT'], 'mob') !== false){ echo 'width:799px;'; } ?>">
<div style="float: right; text-align: right; margin: 0px 0px 10px 20px; font-size: 0.7em; color: #888; background: #eef; border-radius: 5px; padding: 5px; width: 150px;">
<a href="http://www.ipage.com/green-certified/" onclick="MyWindow=window.open('http://www.ipage.com/green-certified/jerrywickey.com','greenCertified','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=700,left=50,top=50'); return false;">
<img src="http://www.ipage.com/green-certified/hosting-badge-1.png" border="0"></a>
<br>
<br>
Jerry Wickey<br>
Key West, FL US<br>
<a style="color: #888;" href="http://jerrywickey.com">jerrywickey.com</a><br>
<a style="color: #888;" href="mailto:jerry@jerrywickey.com">jerry@jerrywickey.com</a><br>
<br><br>
<div style="color: red; font-size: 1.5em;">
<span id="wow"><b>Download</b></span><hr>
<a href="jerrysLibrary.js">jerrysLibrary.js</a><br>
<br>
<a href="jerrysLibrary.php.txt">jerrysLibrary.php</a><br>
<br>
<br>
<a href="testJerrysLibrary.php.txt">testJerrysLibrary.php</a>
<div style="color: black; font-size: 0.7em; text-align: left; margin-left: 5px;">
<br>
PHP, HTML and Javascript source for this page.
The AJAX server form handler example is in this page
<br>
<br>
<div style="color: red; font-weight: bold;">
<div id="comments">
<img src="http://jerrywickey.com/israel/images/whirl.gif" width="15" height="15">
<span style="font-size: 0.7em;">
Retrieving comments.<br>
Waiting for secure channel.</span>
</div>
<div id="followers"></div>
<a href="#tocom">Comment below</a>
</div>
</div>
</div>
<br>
<br>
</div>
<div style="margin: 0px 20px 10px 20px; font-size: 1em; color: #555;">
<br>
<center><h2>Jerry's Cryptography and AJAX javascript and PHP Library</h2>
<h2>Ubiquitous Robust Encryption</h2></center>
<b>Any web page can easily employ robust encryption</b><br><br>
<table style="font-family: Tahoma, Arial, sans-serif; font-size: 1em; color: #888;">
<tr><td style="width: 30px;">
</td><td style="width: 250px;">
<b>RC4 over RSA<br>
really is just this easy</b>
<span style="font-size: 0.7em;"><br><br>
No SSL, no secure server needed. javascript and PHP function library does everything, even client to server URL encoding automatically
</span>
</td><td style="width: 20px;">
</td><td style="font-family: Courier; font-size: 0.7em;">
<b>js: <body onload= initCrypto( 1024 bit)<br><br>
js: encryptToServer( clientPlainText)<br><br>
PHP: decryptFromClient( encryptedDataFromClient)<br><br>
PHP: encryptToClient( serverPlainText)<br><br>
js: decryptFromServer( encryptedDataFromServer)</b><br>
</td></tr>
</table><br>
<div style="font-size: 0.7em; color: #666;">
Encrypt everything on the Internet from mobile device web apps to dinner recipe web pages. The technology is now free.<br><br>
Encrypting only important documents merely identifies which documents are important, attracting the most powerful decryption efforts. Which is better? A society in which no one can have any secrets? Or a society in which anyone can keep everything secret?
</div><br>
<div class="sec">
Cryptography
</div>
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'cryp')">
<span style="color: #844">void</span> <b>initCrypto</b>( no_bits )<br>
<div id="cryp" class="explain" style="display: none;">
<!-- initCrypto( no_bits ) *
*************************************************************************
* code example -->
<center><h3>initCrypto()</h3></center><br>
This initializes the key generating handshake with the server. It takes the number of bits for the key size and returns void. <br><br>
Call this function in <body onload=""
<div class="code">
<body onload="initCrypto( 1024 )">
</div>
or let the user start crypto by selecting his desired bit strength
<div class="code">
<input type="text" id="bitstrength"><br>
<span onclick="initCrypto( getElementById( 'bitstrength').value)">Start</span>
</div>
The Red Lock icon top right can be clicked at any time to show the current key and to reinitialize encryption.<br><br>
To incorporate the Red Lock icon in your page, simply include this div at the bottom of your HTML, just before the </body>
<div class="code">
<div id="secureComIcon"></div>
</div>
If you use the Red Lock icon, please download these three graphics files and put them on your server in the directory "/images/" so that your pages don't leach off my server. Or feel free to replace them with your own images. Search jerrysLibrary.js for them to change the code in any way.<br>
<br>
<a href="http://jerrywickey.com/images/spacer.gif">spacer.gif</a> This is a smalls transparent blank space image. So don't be confused when you don't see anything.<br>
<a href="http://jerrywickey.com/images/lockred.gif">lockred.gif</a><br>
<a href="http://jerrywickey.com/images/unlockred.gif">unlockred.gif</a><br><br>
If the "secureComIcon" div is not found, the encryption status is not reported except by the two functions below.<br><br>
When initCrypt() has completed securing an encrypted channel it calls this function
<div class="code">
cryptoStarted( JL_crybits, JL_cryname, JL_rc4keys)
</div>
If a secure channel could not be obtained for any reason, this function is called instead
<div class="code">
cryptoUnavalable( JL_crybits, JL_cryname, JL_rc4keys)
</div>
These functions are not in the library. You may write them. If you don't write them and they don't exist, they are ignored. They are not needed for secure communications. They are provided for user information only. You can use these to inform the user of status or for any other purpose. You do not have to use nor write them at all.<br><br>
Calling initCrypto() for the very first time creates a directory "temp" on your server. This directory is created in the directory in which you place jerrysLibrary.php library file. The contents of this directory will change constantly. You don't have to do anything with its contents.<br><br>
To use these functions you must include this line in the HTML header
<div class="code">
<script src="your-server-path/jerrysLibrary.js"></script>
</div>
and this line in all php scripts that respond to encrypted messages
<div class="code">
<?php include ('your-server-path/jerrysLibrary.php');
</div>
</div>
<!-- end -->
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'ents')">
<span style="color: #844">string</span>
<b>encryptToServer</b>( dataFromClient )<br>
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'ents')">
<span style="color: #844">string</span>
<b>decryptFromServer</b>( encryptedDataFromServer )<br>
<div id="ents" class="explain" style="display: none;">
<!-- encryptToServer() decryptFromServer() *
*************************************************************************
* code example -->
<center><h3>encryptToServer() decryptFromServer()</h3></center><br>
This is the only function you need to receive encrypted data from the server. The minimum needed is this.
<div class="code">
var encryptedValue= encryptToServer( plainText);<br>
var encryptedResponse= synchttp(( 'serverScript.php?handler=' + encryptedValue), '')<br>
var decryptedResponse= decryptFromServer( encryptedResponse);
</div>
PHP scripts which must decrypt and encrypt must include
<div class="code">
<?php include('/your_path/jerrysLibrary.php');
</div>
The server script decrypts the data sent above like this
<div class="code">
if ( isset( $_GET['handler'])){<br>
$plaintext= decryptFromClient( $_GET['handler']);<br>
// do something with $plaintext and generate $response<br>
echo encryptToClient( $response);<br>
exit();<br>
}
</div>
When passing encrypted data to the server, there is no need to URL encode it, encodeURIComponent(). The encryption library ensures that data uploaded to the server contains only URL safe characters.<br><br>
It is really just that simple. There is never any need to encodeURIComponent(). All the encryption functions generate only URI safe characters on the uplink side.
To use these functions you must include this line in the HTML header
<div class="code">
<script src="your-server-path/jerrysLibrary.js"></script>
</div>
and this line in all php scripts that respond to encrypted messages
<div class="code">
<?php include ('your-server-path/jerrysLibrary.php');
</div>
<br><br>
<center><b>A Sample Form Sending and Receiving Encrypted</b></center><br><br>
This sample is an example of both encrypted GET and encrypted POST and of encrypting only the value and of encrypting both the name and value pair.<br><br>
<div class="code">
<input type="text" id="testName"><br>
<input type="password" id="testPass"><br>
<input type="checkbox" id="testCheck" onclick= "ge('encResponse').innerHTML= <b>synchttp</b>(( '?testRememberMe='+encryptToServer( checked)), '')"><br>
<textarea id="testComment" style="width: 200px; height: 50px;"></textarea><br><br>
<button onclick="sendEncryptedForm()"><b>Submit</b></button><br><br>
<input type="text" id="testName" > User Name<br>
<input type="password" id="testPass" > Pass Word
<input type="checkbox" id="testCheck" onclick="makecheckbox()"> remember me<br>
<textarea id="testComment" style="width: 200px; height: 50px;"></textarea><br>
<button onclick="makeform()">Submit</button><br><br>
function makeform(){<br>
var a= '&testName=' +ge( 'testName').value;<br>
a+= '&testPass='+ge('testPass').value;<br>
a+= '&testComment='+ge('testComment').value;<br>
var enc= encryptToServer( a);<br>
multihttp( '', ( '&encrypted'+ enc), 'viewResponse');<br>
}<br><br>
function viewResponse( a){<br>
ge('encResponse').innerHTML+= '';<br>
}<br>
<br>
<button style="float: right;" onclick="ge('encResponse').innerHTML= ' '">Clear</button>
<div id="encResponse"><br>
<div id="encResponse" style="border: thin black solid; border-radius: 5px; padding: 5px; color: black;"> </div>
</div>
</div>
<script>
function makecheckbox(){
var checked= 'no';
if ( ge('testCheck').checked){ checked= 'yes';}
var enc= encryptToServer( checked);
var res= synchttp(( '?testRememberMe='+enc), '');
ge('encResponse').innerHTML= decryptFromServer( res);
}
function makeform(){
var a= '&testName='+ge('testName').value;
a+= '&testPass='+ge('testPass').value;
a+= '&testComment='+ge('testComment').value;
var enc= encryptToServer( a);
multihttp( '', ('&encrypted='+ enc), 'viewResponse');
}
function viewResponse( a){
ge('encResponse').innerHTML= decryptFromServer( unescape( a));
}
</script>
</div>
<!-- end -->
<div class="sec">
Download, save path and settings
</div>
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'downl')">
<b>Download Instructions</b><br>
<div id="downl" class="explain" style="display: none;">
<center><h3>Download Instructions</h3></center><br>
To use these functions you must download the javascript and PHP libraries<br><br>
<a href="jerrysLibrary.js">jerrysLibrary.js</a><br>
<br>
<a href="jerrysLibrary.php.txt">jerrysLibrary.php</a><br>
<br>
save them to your server. Remember to remove the .txt extension on the file jerrysLibrary.php <br><br>
Set the path where you saved the files in line 25 of jerrysLibrary.js and set JL_setAjax in line 31 as desired. 'true' if you want to use the AJAX functions. AJAX must be set to true if cryptography is desired.
<div class="code">
line 25: var PHPlibpath= 'http://'+window.location.hostname + '/test/jerrysLibrary.php';<br><br>
line 31: var JL_setAjax= true;
</div>
Set ALLOWXDOMAIN and STOREKEYINSESSION according to your taste.<br><br>
Allowing Xdomain is really convenient, but it also opens a certain vulnerability on your server. Javascript does not allow this because this could be a security issue. A hacker could send requests to your copy of jerrysLibrary.php having it access web pages to carry out his own nefarious ends. The web sights accessed would record the IP address of your server not the hacker's.<br><br>
This is set true in the downloaded file.
<div class="code">
line 20: define ( 'ALLOWXDOMAIN', 'true'); // true / false<br><br>
</div>
You must include this line in the HTML header of any page using the libraries.
<div class="code">
<script src="your-server-path/jerrysLibrary.js"></script>
</div>
and include this line in all php scripts that responds to encrypted messages
<div class="code">
<?php include ('your-server-path/jerrysLibrary.php');
</div>
</div>
<!-- end -->
<div class="sec">
AJAX
</div>
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'multi')">
<span style="color: #844">string</span> <b>synchttp</b>( UrlInYourDomainGET, POST )<br>
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'multi')">
<span style="color: #844">void</span> <b>multihttp</b>( UrlInYourDomainGET, POST, callBackFunctionName )<br>
<div id="multi" class="explain" style="display: none;">
<!-- synchttp() multihttp() *
*************************************************************************
* code example -->
<center><h3>synchttp() multihttp()</h3></center><br>
These are the two main AJAX functions.<br><br>
Place GET name value pairs in the first argument and POST name value pairs in the second argument.<br><br>
The third argument for multihttp() is the name of a call back function. When the server responds, the call back function will be called. In the mean time javascript can go on doing what ever else it wants. Whereas, in synchttp(), javascript is halted until the response and the response is returned as text. These are not encrypted. See Cryptography section.
<div class="code">
synchhttp( URL, POST)<br><br>
multihttp( URL, POST, 'callBackFunctionName')<br><br>
function callBackFunctionName( response){<br>
var resp= unescape( response);<br>
// do anything with resp. <br>
}<br>
</div>
Try it out. Since the URL base is our own domain and the server script to which we send this request is the PHP portion of the file for this page, we can simply use '?varName=value' for our GET address. Download
<a href="testJerrysLibrary.php.txt">testJerrysLibrary.php</a>,
the source file of this page to see the PHP.<br><br>
Notice that synchhttp() makes javascript wait until the response from the server comes back, while multihttp() can issue multiple calls and do other things. When the response does come the callback function is called and the response is passed to it escaped. You write the callback function and pass its name as the third parameter of multihttp(). Always unescape the response in the callback function.<br><br>
The test AJAX server function, found in testJerrysLibrary.php, simply returns the names and values for all GET, POST, COOKIE and SESSION variables that exist. Notice the pereventCaching GET variable. It is added by the AJAX library to precent any caching that could be going on at any point along the Internet route.<br><br>
The test AJAX server function also has an intentional delay to showcase the features of multihttp(). In this example a whirl image is placed inside the div until the response arrives. Of course with synchttp() this would be impossible, because javascript is tied up until the response is received.<br><br>
Click either <b>synchttp(</b> or the <b>multihttp(</b> to try them out.
<div class="code">
<span onmouseover="ms(2)" onmouseout="ms(1)" onclick="ge('testAJAX').innerHTML= synchttp( '?someGETvar=999', '')">
document.getElementById('testAJAX').innerHTML= <b>synchttp( '?someGETvar=999', '')</b></span><br><br>
<span onmouseover="ms(2)" onmouseout="ms(1)" onclick="ge('testAJAX').innerHTML= synchttp( '', '&somePOSTvar=777')">
document.getElementById('testAJAX').innerHTML= <b>synchhttp( '', '&somePOSTvar=777')</b></span><br><br>
<span onmouseover="ms(2)" onmouseout="ms(1)" onclick="ge('testAJAX').innerHTML='<img src=http://jerrywickey.com/israel/images/whirl.gif>';for(i=1;i<4;i++){multihttp(('?someGETvar='+i), '', 'multihttpCallBackFunctionName')}">
<script>
function multihttpCallBackFunctionName( a){
if (( ''+ge( 'testAJAX').innerHTML).indexOf( 'images/whirl.gif')>-1){
ge( 'testAJAX').innerHTML= '';
}
ge( 'testAJAX').innerHTML+= unescape( a);
}
</script>
document.getElementById('testAJAX').innerHTML='<img src="whirl"><br>
for ( var i=1; i<3; i++){<br>
 <b>;multihttp(( '?someGETvar='+i), '', 'callBackFunctionName' )</b><br>
}</span><br><br>
function callBackFunctionName( a){<br>
document.getElementById( 'testAJAX').innerHTML+= unescape( a);<br>
}<br><br>
<div id="testAJAX">
<button style="float: right;" onclick="ge('testAJAX').innerHTML= ' '">Clear</button>
<div id="testAJAX" style="border: thin black solid; border-radius: 5px; padding: 5px; color: black;"> </div>
</div>
</div>
</div>
<!-- end -->
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'mxdom')">
<span style="color: #844">string</span>
<b>Xdomain</b>( anyUrlGET, POST, cookiejar, agent, timeout ) <br>
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'mxdom')">
<span style="color: #844">void</span>
<b>multiXdomain</b>( anyUrlGET, POST, cookiejar, agent, timeout, callBackFunctionName )<br>
<div id="mxdom" class="explain" style="display: none;">
<!-- Xdomain() multiXdomain() *
*************************************************************************
* code example -->
<center><h3>Xdomain() multiXdomain()</h3></center><br>
The two Xdomain functions retrieve the contents of web pages that are not on your domain.<br><br>
Javascript does not allow this because this could be a security issue. A hacker could send requests to your copy of jerrysLibrary.php having it access web pages to carry out his own nefarious ends. The web sights accessed would record the IP address of your server not the hacker. While this function can be convenient, it could also be dangerous.<br><br>
There are three ways to shut down Xdomain access.<br><br>
1) Open the file jerrysLibrary.php and change line number 20 to "define ( 'ALLOWXDOMAIN', 'false')"<br>
2) Execute the javascript function Xdomain( 'noaccess', '')<br>
3) Place a file named 'xdomain.txt' with the contents 'no' on your server in the same path as jerrysLibrary.php<br><br>
Any of these three will cause both Xdomain functions to always return false and never access any web page. The only way to restore access is to delete the file 'xdomain.txt' from your server and restore line 20 to 'true'.<br><br>
Both functions do exactly the same as thing except that multiXdomain does not halt javascript while waiting for the response. When the response comes it calls the call back function and passes the response as an escaped string to that function.
<div class="code">
<input id="mxdomainurl" type="text">
<input id="mxdomainurl" type="text" value="http://yahoo.com"> URL<br>
<input id="mxdomainpost" type="text">
<input id="mxdomainpost" type="text"> send POST data if any <br><br>
<span onclick="ge('MXdomain').innerHTML= '<img src=http://jerrywickey.com/israel/images/whirl.gif>';ge('MXdomain').innerHTML= displayCleanPage( Xdomain( ge('mxdomainurl').value, ge('mxdomainpost').value, '', '', ''))" onmouseover="ms(2)" onmouseout="ms(1)">
<b>ge('MXdomain').innerHTML= "<img src='whirl.gif'>";<br>
ge('MXdomain').innerHTML= Xdomain( ge('mxdomainurl').value, ge('mxdomainpost').value, '', '', '')</span></b><br><br>
</div>
As one sees, even though whirling "working" image is placed in the div that will receive the text, nothing happens. The computer is frozen until the source code of the page is received.
With multiXdomain() the computer can go on to do other things until the response is received. The whirling image does show up while during the time the response takes to arrive. The response which is a the source code of a web page is passed to the callback function when it arrives which places it in the div.<br><br>
<div class="code">
<span onclick=" ge('MXdomain').innerHTML= '<img src=http://jerrywickey.com/israel/images/whirl.gif>'; multiXdomain( ge('mxdomainurl').value, ge('mxdomainpost').value, '', '', '', 'mxdomaincallback')" onmouseover="ms(2)" onmouseout="ms(1)">
<b>ge('MXdomain').innerHTML= "<img src='whirl.gif'>";<br>
multiXdomain( ( ge('mxdomainurl').value, ge('mxdomainpost').value, '', '', '', 'mxdomaincallback')</b></span> <br><br>
</div>
If the response arrived too soon to see the whirl image, put the following URL in the URL input field. Just click it.<br>
<div class="code">
<span onclick="ge('mxdomainurl').value='http://jerrywickey.com/test/testJerrysLibrary.php?someGETvar=11'" onmouseover="ms(2)" onmouseout="ms(1)">
<b>getElementById( 'mxdomainurl').value= 'http://jerrywickey.com/test/testJerrysLibrary.php?someGETvar=11'</b></span><br>
</div>
That is one of the test AJAX server commands and it intentionally delays responding for 1 to 3 seconds.
<div class="code">
<div style="float: right;">
<button onclick="ge('MXdomain').innerHTML= ' '">Clear</button>
</div>
<div id="MXdomain">
<div id="MXdomain" style="border: thin black solid; border-radius: 5px; padding: 5px; color: black;"> </div>
</div><br><br>
function displayCleanPage( a){<br>
var pagecontents= unescape( a);<br>
pagecontents= subtute( '<', '&lt;', pagecontents);<br>
pagecontents= subtute( "\n", '<br>', pagecontents);<br>
ge('MXdomain').innerHTML= pagecontents;<br>
}<br>
<br>
function mxdomaincallback( a){<br>
ge('MXdomain').innerHTML= displayCleanPage( a);<br>
}<br>
<script>
//var randsit= ['yahoo.com', 'msn.com', 'google.com', 'yandex.com'];
//ge( 'mxdomainurl').value= 'http://'+ randsit[ Math.floor( Math.random() * randsit.length)];
function displayCleanPage( a){
var pagecontents= unescape( a);
pagecontents= subtute( '<', '<', pagecontents);
pagecontents= subtute( "\n", '<br>', pagecontents);
return pagecontents;
}
function mxdomaincallback( a){
ge('MXdomain').innerHTML= displayCleanPage( a);
}
</script>
</div>
</div>
<!-- end -->
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'cck')">
<span style="color: #844">bool</span> <b>setcookie</b>( name, value, time, path, domain ) <br>
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'cck')">
<span style="color: #844">string</span> <b>getcookie</b>( name ) <br>
<div id="cck" class="explain" style="display: none;">
<!-- setcookie() getcookie() *
*************************************************************************
* code example -->
<center><h3>setcookie() getcookie()</h3></center><br>
Set the value or create a cookie on the client from javascript. You don't have to be too careful with the value types. They pretty much all get turned into strings, except for in some rare occasions. Keeping them strings is good practice.<br><br>
If the cookie doesn't exist, it will be created. Time is the only important value. It is relative to now and in seconds. So you don't have to add to the current timestamp. A value of 3600 will set the cookie to expire an hour from now. It is in seconds, not milliseconds. Setting time to zero, erases the cookie. Path and domain have the same meaning as cookie path and domain.
<div class="code">
setcookie( 'nameOf_COOKIE_Variable', 'value', 'time', 'path', 'domain' )<br><br>
var yourVariable= getcookie( 'nameOf_COOKIE_Variable' )
</div>
Retrieve the value of any COOKIE variable on this client for this domain. If the variable doesn't exist, it will return NULL string or empty string.
<table width="100%" style="font-size: 1em;">
<tr><td valign="top"><div class="code">
<input id="ct1" type="text"><br>
<input id="ct1" type="text" value="cookiename">
</div></td><td valign="top"><div class="code">
<input id="ct2" type="text"><br>
<input id="ct2" type="text">
</div></td></tr>
</table>
<div class="code">
<button onclick="setcookie( ge('ct1').value, ge('ct2').value, 300, '/', '')">
<button onclick="setcookie( ge('ct1').value, ge('ct2').value, 300, '/', ''); alert( 'cookie '+ge('ct1').value+' set to '+ ge('ct2').value+' for five minutes')">Set Cookie</button>
</button><br>
<button onclick="ge( 'ct2').value= getcookie( ge('ct1').value)">
<button onclick="ge( 'ct2').value= getcookie( ge('ct1').value)">Get Cookie</button>
</button>
</div>
Make a new cookie name and value. Place them in their respective text fields. Set the cookie. Delete the value but not the name and click Get cookie. Take note of some actual cookie names from the section on synchttp() and put those names in the name input field then click Get Cookie.<br><br>
A true cookie will retain its value on reload. Try it. Just remember that these cookies are set for only five minutes. Use the function in your own javascript to set it for a different time.
</div>
<!-- end -->
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'sck')">
<span style="color: #844">bool</span> <b>setsession</b>( name, value ) <br>
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'sck')">
<span style="color: #844">string</span> <b>getsession</b>( name )<br>
<div id="sck" class="explain" style="display: none;">
<!-- setsession() getsession() *
*************************************************************************
* code example -->
<center><h3>setsession() getsession()</h3></center><br>
Set the value of any SESSION variable on the server from javascript. If the session variable doesn't exist, it will create it. You don't have to be too careful with the value types. They pretty much get turned into strings, except for in some rare occasions. Keeping them strings is good practice.
<div class="code">
setsession( 'nameOf_SESSION_Variable', 'value')<br><br>
var yourVariable= getsession( 'nameOf_SESSION_Variable' )
</div>
Retrieve the value of any SESSION variable that the server is keeping for this session. If the session variable doesn't exist, it will return NULL string or empty string.
<table width="100%" style="font-size: 1em;">
<tr><td valign="top"><div class="code">
<input id="sct1" type="text"><br>
<input id="sct1" type="text" value="sessionVariableName">
</div></td><td valign="top"><div class="code">
<input id="sct2" type="text"><br>
<input id="sct2" type="text">
</div></td></tr>
</table>
<div class="code">
<button onclick="setsession( ge('sct1').value, ge('sct2').value)">
<button onclick="setsession( ge('sct1').value, ge('sct2').value); alert('SESSION variable '+ge('sct1').value+ ' set to '+ ge('sct2').value)">Set Session</button>
</button><br>
<button onclick="ge( 'sct2').value= getsession( ge('sct1').value)">
<button onclick="ge( 'sct2').value= getsession( ge('sct1').value)">Get Session</button>
</button>
</div>
Make a new SESSION name and value. Place them in their respective text fields. Set the SESSION variable. Delete the value but not the name and click Get session. Take note of some actual session variable names from the section on synchttp() and put those names in the name input field then click Get session.<br><br>
A true SESSION variable will retain its value on reload. Try it. Just don't close the browser or do something that would cause a new session.
</div>
<!-- end -->
<div class="sec">
Miscellany
</div>
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'replacealle')">
<span style="color: #844">string</span> <b>subtute</b>( replaceThis, withThis, inString )<br>
<div id="replacealle" class="explain" style="display: none;">
<!-- subtute() *
*************************************************************************
* code example -->
<center><h3>subtute()</h3></center><br>
If you get tired of the limitations of regular expressions, especially if you are working with special characters, this function will replace any string with any other string in any string.<br><br>
Even "substitute" was too long for me.
<div class="code">
<table width="100%" style="font-size: 1em;">
<tr><td valign="top"><div class="code">
<span id="ts"><br>
<b><span id="ts" style="color: black">ing to trim</span></b><br>
</span>
</div></td><td valign="top"><div class="code">
<input id="ts1" type="text"><br>
<input id="ts1" type="text" value="string to replace" onkeyup="ge('ts').innerHTML= subtute( ge('ts1').value, ge('ts2').value, ge('ts3').value)">
</div></td><td valign="top"><div class="code">
<input id="ts2" type="text"><br>
<input id="ts2" type="text" value="with this string" onkeyup="ge('ts').innerHTML= subtute( ge('ts1').value, ge('ts2').value, ge('ts3').value)">
</div></td><td valign="top"><div class="code">
<input id="ts3" type="text"><br>
<input id="ts3" type="text" value="in this string" onkeyup="ge('ts').innerHTML= subtute( ge('ts1').value, ge('ts2').value, ge('ts3').value)">
</div></td></tr>
</table>
<div class="code">
ge( 'ts').innerHTML=
subtute( ge( 'ts1').value
, ge( 'ts2').value
, ge( 'ts3').value )
</div>
</div>
</div>
<!-- end -->
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'trime')">
<span style="color: #844">string</span> <b>trim</b>( str, trim )<br>
<div id="trime" class="explain" style="display: none;">
<!-- trim() *
*************************************************************************
* code example -->
<center><h3>trim()</h3></center><br>
The native javascript function String.trim() works in some browsers and not in others. trim() in this library works in all browsers old and new.<br>
<br>
The second parameter are the characters to be trimmed from both ends. If an empty string, it trims new line, carriage return space and tab "\n\r\t "
<div class="code">
<table width="100%" style="font-size: 1em;">
<tr><td valign="top"><div class="code">
<span id="tt"><br>
<b><span id="tt" style="color: black">ing to trim</span></b><br>
</span>
</div></td><td valign="top"><div class="code">
<input id="tt1" type="text"><br>
<input id="tt1" type="text" value="string to trim" onkeyup="ge('tt').innerHTML= trim(ge('tt1').value, ge('tt2').value)">
</div></td><td valign="top"><div class="code">
<input id="tt2" type="text"><br>
<input id="tt2" type="text" value="of these characters" onkeyup="ge('tt').innerHTML= trim(ge('tt1').value, ge('tt2').value)">
</div></td></tr>
</table>
<div class="code">
ge( 'tt').innerHTML=
trim( ge( 'tt1').value
, ge( 'tt2').value )
</div>
</div>
</div>
<!-- end -->
<img src="http://jerrywickey.com/images/expand.gif" class="ex" onmouseover="ms(2)" onmouseout="ms(1)" onclick="expand( this, 'eke')">
<span style="color: #844">bool</span> <b>ekey</b>( this.event)<br>
<div id="eke" class="explain" style="display: none;">
<!-- ekey( no_bits ) *
*************************************************************************
* code example -->
<center><h3>ekey()</h3></center><br>