devart-sqlserver-connector 1.3.0__cp314-cp314-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,743 @@
1
+ <?php
2
+
3
+ /* ##########################
4
+
5
+ Devart HttpTunnel v1.69.
6
+
7
+ HTTP tunnel script.
8
+
9
+ This script allows you to manage server even if the corresponding port is blocked or remote access to server is not allowed.
10
+
11
+ ##########################
12
+ */
13
+ if ( !function_exists('sys_get_temp_dir')) {
14
+ function sys_get_temp_dir() {
15
+ if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
16
+ if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
17
+ if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
18
+ $tempfile=tempnam(__FILE__,'');
19
+ if (file_exists($tempfile)) {
20
+ unlink($tempfile);
21
+ return realpath(dirname($tempfile));
22
+ }
23
+ return false;
24
+ }
25
+ }
26
+
27
+ $tmp_dir = sys_get_temp_dir();
28
+ if(!$tmp_dir){
29
+ define('SYSTEM_TMP_DIR', '');
30
+ }
31
+ else{
32
+ $last_symbol = substr($tmp_dir, -1);
33
+ if($last_symbol == DIRECTORY_SEPARATOR){
34
+ define('SYSTEM_TMP_DIR', $tmp_dir);
35
+ }
36
+ else{
37
+ define('SYSTEM_TMP_DIR', $tmp_dir . DIRECTORY_SEPARATOR);
38
+ }
39
+ }
40
+
41
+ $SUB_DIRECTORY = SYSTEM_TMP_DIR . 'tunnel_files';
42
+ $LOG_FILE_NAME = 'httptunnel_server.log';
43
+ $CONN_FILE_NAME = '_connections.id.php';
44
+ $LOGFILE = $SUB_DIRECTORY . '/' . $LOG_FILE_NAME;
45
+ $CONN_FILE = $SUB_DIRECTORY . '/' . $CONN_FILE_NAME;
46
+
47
+ $LOG = 0; // Set to "0" to disable logging
48
+ $LOG_DEBUG = 0; // Set to "0" to disable additional debug logging
49
+ $LOGFILEHANDLE = 0;
50
+ $MAXLOGSIZE = "4000000";
51
+ $LIFETIME = 180; // script lifetime in seconds. If script was started and got no client within that time - it exits.
52
+ $READ_WRITE_ATTEMPTS = 100;
53
+ $CREATE_CLIENT_SOCKET_ATTEMPTS = 3;
54
+ $IPC_TMPDIR = "/tmp/";
55
+
56
+ global $SUB_DIRECTORY;
57
+
58
+ function checkFunctionExists($functionName) {
59
+ if (!function_exists($functionName)) {
60
+ echo "Required function <b>$functionName</b> does not exist.</br>";
61
+ return false;
62
+ }
63
+
64
+ return true;
65
+ }
66
+
67
+ // Creates connection temporary file if not exists and checks permission to write
68
+ function CreateAndCheckConnFile($fileName) {
69
+
70
+ global $SUB_DIRECTORY;
71
+
72
+ if (file_exists($fileName)){
73
+ $newFile = @fopen($fileName, 'a');
74
+ if($newFile)
75
+ fclose($newFile);
76
+ else
77
+ echo "<b>Error</b>: Failed to open ($fileName) file: Permission denied.";
78
+
79
+ }
80
+ else{
81
+ if(!is_dir($SUB_DIRECTORY)){
82
+ mkdir($SUB_DIRECTORY);
83
+ }
84
+ $newFile = @fopen($fileName, 'w');
85
+ if($newFile){
86
+ fwrite($newFile, "<?php echo 'Devart HTTP tunnel temporary file.'; exit; ?>\r\n"); // forbid viewing this file through browser
87
+ fclose($newFile);
88
+ }
89
+ else
90
+ echo "<b>Error</b>: Failed to create ($fileName) file: Permission denied.";
91
+ }
92
+
93
+ if(!$newFile)
94
+ exit;
95
+ }
96
+
97
+ if (!isset($_REQUEST["a"])) { // query from browser
98
+
99
+ echo "Devart HttpTunnel v1.69<br />";
100
+
101
+ $functionList = array(
102
+ // "set_time_limit",
103
+ "stream_socket_server",
104
+ "stream_socket_client",
105
+ "stream_socket_get_name",
106
+ "stream_set_blocking",
107
+ "stream_socket_accept",
108
+ "stream_select"
109
+ );
110
+
111
+ $exist = true;
112
+ foreach($functionList as $functionName) {
113
+ $result = checkFunctionExists($functionName);
114
+ $exist = $exist && $result;
115
+ }
116
+
117
+ if ($exist)
118
+ CreateAndCheckConnFile($CONN_FILE);
119
+
120
+ if ($exist) {
121
+ echo "Tunnel script is installed correctly. <br />You can establish connections through the HTTP tunnel.";
122
+ if ($LOG==1)
123
+ echo "<br /> <br /><b>Loging is enabled.</b><br />Log files are located in the tunnel_files folder, which, in its turn, is located in the temporary folder of the operating system: " .$LOGFILE;
124
+ }
125
+ else
126
+ echo "Required PHP functions listed above are not available. Tunneling script will not work without these functions. Please read PHP manuals about how to install listed functions.";
127
+
128
+ exit;
129
+ }
130
+
131
+ function myErrorHandler($errno, $errstr, $errfile, $errline) {
132
+ switch ($errno) {
133
+ case E_ERROR:
134
+ $errfile = preg_replace('|^.*[\\\\/]|','',$errfile);
135
+ echo $ERRSTR."Error in line $errline of file $errfile: [$errno] $errstr\n";
136
+ exit;
137
+ }
138
+ }
139
+
140
+ function shutdown () {
141
+ global $ipsock, $rmsock, $outcount, $incount, $td, $te, $sockname, $useunix;
142
+
143
+ if (connection_status() & 1) { // ABORTED
144
+ logline ($_SERVER["REMOTE_ADDR"].": Irregular tunnel disconnect -> disconnecting server");
145
+ logline ($_SERVER["REMOTE_ADDR"].": Sent ".$outcount." bytes, received ".$incount." bytes");
146
+ } elseif (connection_status() & 2) { // TIMEOUT
147
+ logline ($_SERVER["REMOTE_ADDR"].": PHP script timeout -> disconnecting server");
148
+ logline ($_SERVER["REMOTE_ADDR"].": Sent ".$outcount." bytes, received ".$incount." bytes");
149
+ }
150
+
151
+ if ($ipsock) fclose($ipsock);
152
+ if ($rmsock) fclose($rmsock);
153
+ }
154
+
155
+ function logline ($msg) {
156
+ log_line_to_file(0, $msg);
157
+ }
158
+
159
+ function logdebug($msg) {
160
+ log_line_to_file(1, $msg);
161
+ }
162
+
163
+ function logerr($msg) {
164
+ global $ERRSTR;
165
+ logline($msg);
166
+ echo $ERRSTR;
167
+ echo $msg;
168
+ }
169
+
170
+ function log_line_to_file ($debug, $msg) {
171
+ global $LOG, $LOG_DEBUG, $MAXLOGSIZE, $LOGFILE, $LOGFILEHANDLE;
172
+ if ($LOG && ((! $debug) || $LOG_DEBUG)) {
173
+ $LOGFILEHANDLE=fopen ($LOGFILE, "a");
174
+ if ($LOGFILEHANDLE) {
175
+ fwrite ($LOGFILEHANDLE, date("d.m.Y H:i:s")." - $msg\r\n");
176
+ $lstat=fstat($LOGFILEHANDLE);
177
+ if ($lstat["size"]>$MAXLOGSIZE) rotatelog();
178
+ fclose($LOGFILEHANDLE);
179
+ }
180
+ }
181
+ }
182
+
183
+ function rotatelog() {
184
+ global $LOG, $MAXLOGSIZE, $LOGFILE, $LOGFILEHANDLE;
185
+ if ($LOG) {
186
+ fwrite ($LOGFILEHANDLE, date("d.m.Y H:i:s")." - Logfile reached maximum size ($MAXLOGSIZE)- rotating.\r\n");
187
+ fclose ($LOGFILEHANDLE);
188
+ rename ($LOGFILE, substr_replace($LOGFILE,md5(microtime()),-3).".log");
189
+ $LOGFILEHANDLE=fopen ($LOGFILE, "a");
190
+ if (!$LOGFILEHANDLE)
191
+ $LOG=0;
192
+ else
193
+ fwrite ($LOGFILEHANDLE, date("d.m.Y H:i:s")." - Opening new Logfile.\r\n");
194
+ }
195
+ }
196
+
197
+ function create_client_socket() {
198
+ global $CREATE_CLIENT_SOCKET_ATTEMPTS, $_REQUEST, $IPC_TMPDIR;
199
+
200
+ if (!isset($_REQUEST["port"])) {
201
+ echo $ERRSTR."Port not set.";
202
+ return 0;
203
+ }
204
+
205
+ $port = $_REQUEST["port"];
206
+ $useunix = in_array("unix", stream_get_transports());
207
+
208
+ $retryCount = 0;
209
+ do {
210
+ if ($retryCount > 0) {
211
+ usleep(10000); // 10ms
212
+ logdebug("Attempt to create client socket #".($retryCount + 1)." ErrorCode:".$errcode." ErrorMessage:".$errmessage." Port:".$port);
213
+ }
214
+
215
+ if ($useunix) { // this is for the unix socket type
216
+ $sockname = preg_replace('/\\\\/','/', $IPC_TMPDIR."tun$port.sock");
217
+ $client = stream_socket_client("unix://".$sockname, $errcode, $errmessage);
218
+ }
219
+ else {
220
+ $client = stream_socket_client("tcp://127.0.0.1:".$port, $errcode, $errmessage);
221
+ }
222
+ $retryCount = $retryCount + 1;
223
+ } while(!$client && $retryCount < $CREATE_CLIENT_SOCKET_ATTEMPTS);
224
+
225
+ if ($client) {
226
+ stream_set_blocking($client, 1);
227
+ }
228
+ return $client;
229
+ }
230
+
231
+ function send_server_script_message($command) {
232
+ global $_REQUEST;
233
+
234
+ $client = create_client_socket();
235
+ if (!$client) {
236
+ logerr("Failed to create client socket");
237
+ return FALSE;
238
+ }
239
+ if (fwrite($client, $command, 1) === FALSE) {
240
+ logerr("Failed to send message to server script.");
241
+ fclose($client);
242
+ return FALSE;
243
+ }
244
+ fclose($client);
245
+ return TRUE;
246
+ }
247
+
248
+ function increase_script_lifetime() {
249
+ global $LIFETIME;
250
+ if (function_exists("set_time_limit")) {
251
+ set_time_limit($LIFETIME);
252
+ logdebug("Script liftetime incremented with $LIFETIME");
253
+ }
254
+ }
255
+
256
+ function write_to_socket($socket, $buffer, $count) {
257
+ global $READ_WRITE_ATTEMPTS;
258
+
259
+ $totalCount = 0;
260
+ $retryCount = 0;
261
+
262
+ do {
263
+ if ($retryCount > 0)
264
+ usleep(10000); // 10ms
265
+
266
+ if (!$socket)
267
+ break;
268
+
269
+ $written = fwrite($socket, $buffer, $count);
270
+ $buffer = substr($buffer, $written);
271
+ $totalCount += $written;
272
+
273
+ if ($retryCount > 0)
274
+ logdebug("Attempt to write #".($retryCount + 1)." Write: ".$written);
275
+
276
+ if ($written <= 0)
277
+ $retryCount = $retryCount + 1;
278
+
279
+ } while($totalCount < $count && $retryCount < $READ_WRITE_ATTEMPTS);
280
+
281
+ if ($totalCount != $count)
282
+ logline("ERROR: Failed to write to socket $count bytes, $totalCount actually written.");
283
+
284
+ return $totalCount;
285
+ }
286
+
287
+ // reads specified byte count from socket
288
+ function read_from_socket($socket, &$buffer, $count) {
289
+ global $READ_WRITE_ATTEMPTS;
290
+
291
+ $totalCount = 0;
292
+ $retryCount = 0;
293
+
294
+ $buffer = "";
295
+ $readBuffer;
296
+
297
+ do {
298
+ if ($retryCount > 0)
299
+ usleep(10000); // 10ms
300
+
301
+ if (!$socket)
302
+ break;
303
+
304
+ $readBuffer = fread($socket, $count);
305
+ $read = strlen($readBuffer);
306
+ $buffer = $buffer.$readBuffer;
307
+
308
+ if ($retryCount > 0)
309
+ logdebug("Attempt to read #".($retryCount + 1)." Read: ".$read);
310
+
311
+ $totalCount += $read;
312
+ if ($read <= 0)
313
+ $retryCount = $retryCount + 1;
314
+
315
+ } while($totalCount < $count && $retryCount < $READ_WRITE_ATTEMPTS);
316
+
317
+ if ($totalCount != $count)
318
+ logerr("Failed to read from socket $count bytes, $totalCount actually read.");
319
+
320
+ return $totalCount;
321
+ }
322
+
323
+ // packet: size of data count | data count | data
324
+ // lengths: 1 byte | up to 255 bytes, typically 1 - 5| up to $MaxCount
325
+ function write_data_packet($socket, &$buffer, $count) {
326
+
327
+ $countLength = strlen($count);
328
+ // write length of data count digit
329
+ write_to_socket($socket, $countLength, 1);
330
+ // write data count
331
+ write_to_socket($socket, $count, $countLength);
332
+ // write data
333
+ $writeCount = write_to_socket($socket, $buffer, $count);
334
+ if ($writeCount == $count)
335
+ return $writeCount;
336
+ else
337
+ return 0;
338
+ }
339
+
340
+ function read_data_packet($socket, &$buffer) {
341
+
342
+ // obtain data length digit length
343
+ read_from_socket($socket, $countSize, 1);
344
+ // read data length
345
+ read_from_socket($socket, $readCount, $countSize);
346
+ $expectedReadCount = $readCount;
347
+ // read data
348
+ $readCount = read_from_socket($socket, $buffer, $readCount);
349
+ if ($readCount == $expectedReadCount)
350
+ return $readCount;
351
+ else
352
+ return FALSE;
353
+ }
354
+
355
+ // Start of the tunnel script
356
+ $isServer = FALSE;
357
+
358
+ if (version_compare("5.0.0", phpversion())==1) die ("Only PHP 5 or above supported");
359
+ error_reporting(0);
360
+ set_error_handler("myErrorHandler");
361
+ register_shutdown_function ("shutdown");
362
+ // no-cache
363
+ Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
364
+ Header("Cache-Control: no-cache, must-revalidate");
365
+ Header("Pragma: no-cache"); // HTTP/1.1
366
+ Header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");
367
+ Header("Content-Type: application/octet-stream");
368
+
369
+ ob_implicit_flush();
370
+
371
+ // Maximum bytes to read at once
372
+ $MaxReadCount = 16*1024;
373
+
374
+ // operation success identification
375
+ $OKSTR = "OK:";
376
+ $ERRSTR = "ER:";
377
+
378
+ $CONN_FILE_MAXSIZE = 100;
379
+
380
+ // Primary tunnel connection
381
+ // need the following REQUEST vars:
382
+ // a: "c"
383
+ // s: remote server name
384
+ // p: remote server port
385
+
386
+ // every operation output at least first three chars identifying the success of operation: "OK:" if succeeded, "ER:" if not.
387
+ //
388
+
389
+ if ($_REQUEST["a"]=="c") { // run server script
390
+ $isServer=TRUE;
391
+ // clear log
392
+ if ($LOG_DEBUG) {
393
+ // truncate log file
394
+ $logfile = fopen($LOGFILE, 'w');
395
+ fclose($logfile);
396
+ }
397
+
398
+ $useunix = in_array("unix", stream_get_transports());
399
+ $dad = $_REQUEST["s"];
400
+ $dpo = $_REQUEST["p"];
401
+
402
+ // open the interprocess socket
403
+ $errno = 0;
404
+ $errstr = "";
405
+ $ipsock = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr);
406
+
407
+ if (!$ipsock) {
408
+ logerr("stream_socket_server() failed: reason:".$errno." ".$errstr);
409
+ exit;
410
+ }
411
+
412
+ $port = stream_socket_get_name($ipsock, false);
413
+ $port = preg_replace('/^.*?:/','', $port);
414
+
415
+ if ($useunix) { // this is for the unix socket type
416
+ if ($ipsock) fclose($ipsock);
417
+ $ipsock = 0;
418
+ $sockname = preg_replace('/\\\\/','/', $IPC_TMPDIR."tun$port.sock");
419
+ unlink($sockname);
420
+ if (file_exists($sockname)) {
421
+ logerr("Temporary UNIX socket exists and cannot be deleted ($sockname)");
422
+ exit;
423
+ }
424
+ $ipsock = stream_socket_server("unix://".$sockname, $errno, $errstr);
425
+ if (!$ipsock) {
426
+ logerr("stream_socket_server() failed: reason:".$errno." ".$errstr);
427
+ exit;
428
+ }
429
+ }
430
+
431
+ stream_set_blocking($ipsock, 1);
432
+
433
+ // open the remote socket
434
+ logline("Connecting to remote $dad: $dpo");
435
+ $rmsock = stream_socket_client("tcp://".$dad.":".$dpo, $errno, $errstr);
436
+
437
+ if (!$rmsock) {
438
+ logerr("Failed to create remote socket at $dad: $dpo. ".$errno." ".$errstr);
439
+ exit;
440
+ }
441
+ else {
442
+ if (isset($_REQUEST["nonblock"]))
443
+ $block = 0;
444
+ else
445
+ $block = 1;
446
+ stream_set_blocking($rmsock, $block);
447
+ logline("Connected to remote $dad: $dpo");
448
+ }
449
+
450
+ // write connection identificator to file. Echo'ing is not appropriate in case of antiviral software, it would be blocked until script finishes
451
+ $newConnFile = FALSE;
452
+ $connFileMode = "a";
453
+
454
+ if (file_exists($CONN_FILE)) {
455
+ $connFile = fopen($CONN_FILE, "r");
456
+ $lstat=fstat($connFile);
457
+ fclose($connFile);
458
+ if ($lstat["size"]>$CONN_FILE_MAXSIZE) {
459
+ $connFileMode = "w";
460
+ $newConnFile = TRUE;
461
+ }
462
+ }
463
+ else {
464
+ $newConnFile = TRUE;
465
+ }
466
+
467
+ $connFile = fopen($CONN_FILE, $connFileMode);
468
+ if ($connFile) {
469
+ if ($newConnFile) {
470
+ fwrite($connFile, "<?php echo 'Devart HTTP tunnel temporary file.'; exit; ?>\r\n"); // forbid viewing this file through browser
471
+ }
472
+ $connectionId = str_replace("_", " ", $_REQUEST["id"]);
473
+ fwrite ($connFile, $connectionId." ".$port."\r\n");
474
+ fclose($connFile);
475
+ }
476
+ else {
477
+ logerr("Failed to create connection temporary file.");
478
+ exit;
479
+ }
480
+
481
+ if (function_exists("set_time_limit")) {
482
+ set_time_limit($LIFETIME);
483
+ }
484
+
485
+ $exit = false;
486
+ $buffer = array();
487
+ $countBuffer = array();
488
+
489
+ while (!$exit) {
490
+ logdebug("Waiting for client...");
491
+ $client = stream_socket_accept($ipsock);
492
+ logline("Client accepted");
493
+ if ($client === FALSE) {
494
+ logline("ERROR: Bad client.");
495
+ continue;
496
+ }
497
+ // read command
498
+ $count = read_from_socket($client, $buffer, 1);
499
+ if ($count == 0) {
500
+ logline("Error reading client command.");
501
+ $exit = true;
502
+ }
503
+
504
+ logdebug("Read from client ($count): ".$buffer[0]);
505
+
506
+ $command = $buffer[0];
507
+
508
+ increase_script_lifetime();
509
+
510
+ if ($command == "x") { // close
511
+ logline("Shutting down on client request.");
512
+ $exit = true; // shutdown
513
+ }
514
+ else if ($command == "r") { // read
515
+ if (!$rmsock) {
516
+ logline("ERROR: rmsock is off");
517
+ $exit = true;
518
+ break;
519
+ }
520
+
521
+ $readCount = 0;
522
+ $buffer = fread($rmsock, $MaxReadCount);
523
+ if ($buffer === FALSE) {
524
+ logline("ERROR: Remote server disconnected.");
525
+ $exit = true;
526
+ break;
527
+ }
528
+ else {
529
+ $readCount = strlen($buffer);
530
+ logline("Read from remote:($readCount)");
531
+ }
532
+
533
+ if ($readCount >= 0) {
534
+ if ($readCount == 0)
535
+ logline("Nothing to read from remote.");
536
+
537
+ $writeCount = write_data_packet($client, $buffer, $readCount);
538
+ logdebug("Write to client($writeCount): $buffer");
539
+ if ($readCount > 0 && $writeCount == 0) {
540
+ logerr("Failed to write to client.");
541
+ $exit = true;
542
+ }
543
+ }
544
+ }
545
+ else if ($command == "s") { // select
546
+ if (!$rmsock) {
547
+ logline("ERROR: rmsock is off");
548
+ $exit = true;
549
+ break;
550
+ }
551
+
552
+ $readSocks = array($ipsock, $rmsock);
553
+ $writeSocks = NULL;
554
+ $exceptSocks = NULL;
555
+ if (!stream_select($readSocks, $writeSocks, $exceptSocks, NULL))
556
+ $res = $ERRSTR;
557
+ else
558
+ if (in_array($rmsock, $readSocks))
559
+ $res = $OKSTR;
560
+ else
561
+ $res = $ERRSTR;
562
+ $writeCount = write_to_socket($client, $res, strlen($res));
563
+ logdebug("Write to client($writeCount): $res");
564
+ }
565
+ else if ($command == "w") { // write
566
+ if (!$rmsock) {
567
+ logline("ERROR: rmsock is off");
568
+ $exit = true;
569
+ break;
570
+ }
571
+ $readCount = read_data_packet($client, $buffer);
572
+ logline("Read from client: $readCount");
573
+ if ($readCount > 0) {
574
+ $writeCount = write_to_socket($rmsock, $buffer, $readCount);
575
+ logdebug("Write to remote($writeCount): $buffer");
576
+ }
577
+ }
578
+ else if ($command == "l") { // increment lease time
579
+ logline("Lease time increased.");
580
+ }
581
+ else if ($command == "t") { // test connection command
582
+ $writeCount = write_to_socket($client, $OKSTR, strlen($OKSTR));
583
+ if ($writeCount == 0)
584
+ $exit = true;
585
+ }
586
+ else {
587
+ logline("ERROR: Unknown command: $command. Exiting.");
588
+ $exit = true;
589
+ }
590
+ }
591
+
592
+ logline("Server script closed.");
593
+ exit;
594
+ }
595
+
596
+ if ($_REQUEST["a"]=="r") { // read
597
+
598
+ $client = create_client_socket();
599
+ if (!$client) {
600
+ logerr("Failed to connect to server script.");
601
+ exit;
602
+ }
603
+
604
+ logdebug("Client: Reading from server script");
605
+
606
+ if (write_to_socket($client, "r", 1) == 0) { // write "Read" command
607
+ logerr("Write to server script failed.");
608
+ fclose($client);
609
+ exit;
610
+ }
611
+
612
+ $buffer;
613
+ $readCount = read_data_packet($client, $buffer);
614
+ if ($readCount === FALSE) {
615
+ logerr("Failed to read response from server script.");
616
+ fclose($client);
617
+ exit;
618
+ }
619
+
620
+ $totalCount = strlen($OKSTR) + $readCount;
621
+
622
+ $outputStr = $OKSTR.$buffer;
623
+
624
+ header("Content-Length: ".$totalCount);
625
+ header("Content-Type: application/octet-stream");
626
+
627
+ logline("Client: Read from server $readCount");
628
+ echo $outputStr;
629
+
630
+ fclose($client);
631
+ exit;
632
+ }
633
+
634
+ if ($_REQUEST["a"]=="s") { // select
635
+
636
+ $client = create_client_socket();
637
+ if (!$client) {
638
+ logerr("Failed to connect to server script.");
639
+ exit;
640
+ }
641
+
642
+ logdebug("Client: Selecting from server script");
643
+
644
+ if (write_to_socket($client, "s", 1) == 0) { // write "Select" command
645
+ logerr("Write to server script failed.");
646
+ fclose($client);
647
+ exit;
648
+ }
649
+
650
+ $buffer;
651
+ $count = read_from_socket($client, $buffer, 3);
652
+ if ($count < 3) {
653
+ logerr("Failed to read response from server script.");
654
+ fclose($client);
655
+ exit;
656
+ }
657
+
658
+ fclose($client);
659
+ echo $buffer;
660
+ exit;
661
+ }
662
+
663
+ if ($_REQUEST["a"]=="w") { // write
664
+
665
+ $client = create_client_socket();
666
+ if (!$client) {
667
+ logerr("Failed to connect to server script.");
668
+ exit;
669
+ }
670
+
671
+ $postBody= isset($_POST['base64body'])?base64_decode($_POST['base64body']):file_get_contents("php://input"); // Retrieve RAW POST data
672
+ $writeData = $postBody;
673
+ $expectedWriteCount = strlen($writeData);
674
+ $writeCount = write_to_socket($client, "w", 1); // indicate that this is the "Write" command
675
+ if ($writeCount > 0)
676
+ $writeCount = write_data_packet($client, $writeData, $expectedWriteCount);
677
+
678
+ if ($writeCount == 0) {
679
+ logerr("Write to server script failed.");
680
+ fclose($client);
681
+ exit;
682
+ }
683
+
684
+ logdebug("Client: Written $writeCount");
685
+
686
+ fclose($client);
687
+ echo $OKSTR;
688
+ exit;
689
+ }
690
+
691
+ if ($_REQUEST["a"]=="x") { // close
692
+
693
+ echo $OKSTR."Shutted down.";
694
+ send_server_script_message("x");
695
+ exit;
696
+ }
697
+
698
+ if ($_REQUEST["a"] == "l") { // increment server script lease time
699
+
700
+ if (send_server_script_message("l"))
701
+ echo $OKSTR."Incremented server script lease time.";
702
+ exit;
703
+ }
704
+
705
+ if ($_REQUEST["a"] == "t") { // test newly created connection
706
+
707
+ $connectionId = str_replace("_", " ", $_REQUEST["id"]);
708
+ logline($connectionId);
709
+ $connections = file_get_contents($CONN_FILE);
710
+
711
+ if ($connections === FALSE) {
712
+ logerr("Failed to open $CONN_FILE.");
713
+ exit;
714
+ }
715
+
716
+ $lines = explode("\r\n", $connections);
717
+
718
+ // skip first line
719
+ for($i = 1; $i < count($lines); ++$i) {
720
+ $line = $lines[$i];
721
+ $pos = strpos($line, $connectionId);
722
+ if ($pos === FALSE)
723
+ continue;
724
+
725
+ if ($pos === 0) { // starts with
726
+ $parts = explode(" ", $line);
727
+ if (count($parts) != 3) {
728
+ echo "Invalid connection record";
729
+ exit;
730
+ }
731
+
732
+ echo $OKSTR.$parts[2]."\n"."$LIFETIME\n";
733
+ exit;
734
+ }
735
+ }
736
+
737
+ logerr("Connection entry not found.");
738
+ exit;
739
+ }
740
+
741
+ logerr("Invalid tunneling script parameter: ".$_REQUEST["a"]);
742
+
743
+ ?>