-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunc.php
More file actions
1032 lines (885 loc) · 29.5 KB
/
func.php
File metadata and controls
1032 lines (885 loc) · 29.5 KB
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
define('PHP_PROXY_HUNTER_PROJECT_ROOT', __DIR__);
require_once __DIR__ . '/vendor/symfony/polyfill-mbstring/bootstrap.php';
include __DIR__ . '/src/utils/shim/string.php';
include __DIR__ . '/src/database/env.php';
$isCli = is_cli();
define('PHP_PROXY_HUNTER', 'true');
if (!defined('JSON_THROW_ON_ERROR')) {
define('JSON_THROW_ON_ERROR', 4194304);
}
define('PROJECT_ROOT', __DIR__);
// Detect if the system is Windows
$isWin = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
// Get the current PATH
$currentPath = getenv('PATH');
if ($currentPath === false) {
// Set a default PATH value (customize as needed)
$defaultPath = $isWin ? 'C:\Windows\System32' : '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
$currentPath = $defaultPath;
// var_dump("Setting default PATH", $currentPath);
}
if ($currentPath) {
// var_dump("original PATH", $currentPath);
// Specify the new directories to add
$paths = [__DIR__ . '/venv/Scripts'];
$PathSeparator = $isWin ? ';' : ':';
$newDirectory = implode($PathSeparator, $paths);
// Combine the current PATH with the new directory
$newPath = $newDirectory . $PathSeparator . $currentPath;
$explodePath = array_filter(array_unique(explode($PathSeparator, $newPath)));
$explodePath = array_map(function ($str) {
// Only apply realpath if the directory exists
$realPath = realpath($str);
if ($realPath === false) {
return $str;
}
return $realPath;
}, $explodePath);
// Additional logging for debugging
// var_dump("Directories before filtering", $explodePath);
$newPath = implode($PathSeparator, $explodePath);
// Add the new directory to the PATH
putenv("PATH=$newPath");
// Verify the change
// $updatedPath = getenv('PATH');
// var_dump("modified PATH", $updatedPath);
// $output = shell_exec("echo %PATH%");
// echo $output;
// exit;
}
// Detect admin
$isAdmin = is_debug();
if (!$isAdmin) {
if ($isCli) {
// CLI
$short_opts = 'p::m::';
$long_opts = [
'proxy::',
'max::',
'userId::',
'lockFile::',
'runner::',
'admin::',
];
$options = getopt($short_opts, $long_opts);
$isAdmin = !empty($options['admin']) && $options['admin'] !== 'false';
}
}
// ===== START error reporting settings =====
// debug all errors
$enable_debug = $isAdmin;
if ($enable_debug) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
} else {
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
}
ini_set('log_errors', 1);
// Enable error logging
$error_file = __DIR__ . '/tmp/logs/php-error.txt';
if (!$isCli) {
// Sanitize the user agent string
$user_agent = preg_replace('/[^a-zA-Z0-9-_\.]/', '_', $_SERVER['HTTP_USER_AGENT'] ?? 'unknown');
// Check if the sanitized user agent is empty
if (empty($user_agent)) {
$error_file = __DIR__ . '/tmp/logs/php-error.txt';
} else {
$error_file = __DIR__ . '/tmp/logs/php-error-' . $user_agent . '.txt';
}
}
ini_set('error_log', $error_file);
// set error path
if ($enable_debug) {
error_reporting(E_ALL);
if (!is_debug_device() && version_compare(PHP_VERSION, '8.4', '>=')) {
// Suppress PHP 8.4 deprecation notices about implicit nullable params on production
error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
}
} else {
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR);
}
// ===== END error reporting settings =====
// set default timezone
date_default_timezone_set('Asia/Jakarta');
// allocate memory
ini_set('memory_limit', '128M');
// ignore limitation if exists
if (function_exists('set_time_limit')) {
// Disables the time limit completely
call_user_func('set_time_limit', 0);
}
require_once __DIR__ . '/vendor/autoload.php';
use PhpProxyHunter\Session;
$dotenv = \Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
// keep running when user closed the connection (true)
// ignore_user_abort(true);
// ignore user abort execution to false
if (function_exists('ignore_user_abort')) {
call_user_func('ignore_user_abort', false);
}
// Start session for web server
if (!$isCli) {
/** @noinspection PhpUnhandledExceptionInspection */
new Session(100 * 3600, __DIR__ . '/tmp/sessions');
// web server admin
$isAdmin = !empty($_SESSION['admin']) && $_SESSION['admin'] === true;
}
// Define $argv for web server context
$argv = isset($argv) ? $argv : [];
/**
* Returns an array of unique objects from the provided array based on a specific property.
*
* @param array $array An array of objects
* @param string $property The property name to compare
* @return array An array of unique objects
*/
function uniqueClassObjectsByProperty(array $array, string $property): array
{
$tempArray = [];
$result = [];
foreach ($array as $item) {
if (property_exists($item, $property)) {
$value = $item->$property;
if (!isset($tempArray[$value])) {
$tempArray[$value] = true;
$result[] = $item;
}
}
}
return $result;
}
/**
* Checks if a given string is base64 encoded.
*
* @param string|null $string The string to check.
* @return bool True if the string is base64 encoded, false otherwise.
*/
function isBase64Encoded($string): bool
{
if (empty($string)) {
return false;
}
// Check if the string matches the base64 format
if (preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) {
// Decode the string and then re-encode it
$decoded = base64_decode($string, true);
if ($decoded !== false) {
// Compare the re-encoded string to the original
if (base64_encode($decoded) === $string) {
return true;
}
}
}
return false;
}
/**
* Remove specified string from source file and move it to destination file.
*
* This function reads the source file line by line, removes the specified string,
* and writes the modified content back to the source file. It also appends the removed
* string to the destination file.
*
* @param string $sourceFilePath Path to the source file.
* @param string $destinationFilePath Path to the destination file.
* @param string|null $stringToRemove The string to remove from the source file.
* @return string Message indicating success or failure.
*/
function removeStringAndMoveToFile(string $sourceFilePath, string $destinationFilePath, $stringToRemove): string
{
if (!file_exists($destinationFilePath)) {
file_put_contents($destinationFilePath, '');
}
// Check if $stringToRemove is empty or contains only whitespace characters
if (is_null($stringToRemove) || empty(trim($stringToRemove))) {
return 'Empty string to remove';
}
// Check if source file is writable
if (!is_writable($sourceFilePath)) {
return "$sourceFilePath not writable";
}
// Check if destination file is writable
if (!is_writable($destinationFilePath)) {
return "$destinationFilePath not writable";
}
// Check if source file is locked
if (is_file_locked($sourceFilePath)) {
return "$sourceFilePath locked";
}
// Check if destination file is locked
if (is_file_locked($destinationFilePath)) {
return "$destinationFilePath locked";
}
// Open source file for reading
$sourceHandle = @fopen($sourceFilePath, 'r');
if (!$sourceHandle) {
return 'Failed to open source file';
}
// Open destination file for appending
$destinationHandle = @fopen($destinationFilePath, 'a');
if (!$destinationHandle) {
fclose($sourceHandle);
return 'Failed to open destination file';
}
// Open a temporary file for writing
$tempFilePath = tempnam(sys_get_temp_dir(), 'source_temp');
$tempHandle = @fopen($tempFilePath, 'w');
if (!$tempHandle) {
fclose($sourceHandle);
fclose($destinationHandle);
return 'Failed to create temporary file';
}
// Acquire an exclusive lock on the source file
if (flock($sourceHandle, LOCK_SH)) {
// Iterate through each line in the source file
while (($line = fgets($sourceHandle)) !== false) {
// Remove the string from the current line
$modifiedLine = str_replace($stringToRemove, '', $line);
// Write the modified line to the temporary file
fwrite($tempHandle, $modifiedLine);
}
// Close file handles
flock($sourceHandle, LOCK_UN);
fclose($sourceHandle);
fclose($tempHandle);
fclose($destinationHandle);
// Replace the source file with the temporary file
if (!rename($tempFilePath, $sourceFilePath)) {
unlink($tempFilePath);
return 'Failed to replace source file';
}
// Append the removed string to the destination file
if (file_put_contents($destinationFilePath, PHP_EOL . $stringToRemove . PHP_EOL, FILE_APPEND) === false) {
return 'Failed to append removed string to destination file';
}
return 'Success';
} else {
fclose($sourceHandle);
fclose($destinationHandle);
fclose($tempHandle);
unlink($tempFilePath);
return 'Failed to acquire lock on source file';
}
}
/**
* get cache file from `curlGetWithProxy`
*/
function curlGetCache($url): string
{
return __DIR__ . '/.cache/' . md5($url);
}
/**
* Fetches the content of a URL using cURL with a specified proxy, with caching support.
*
* @param string $url The URL to fetch.
* @param string|null $proxy The proxy IP address and port (e.g., "proxy_ip:proxy_port").
* @param string|null $proxyType The type of proxy. Can be 'http', 'socks4', or 'socks5'. Defaults to 'http'.
* @param float|int $cacheTime The cache expiration time in seconds. Set to 0 to disable caching. Defaults to 1 year (86400 * 360 seconds).
* @param string $cacheDir The directory where cached responses will be stored. Defaults to './.cache/' in the current directory.
* @param string|null $username Optional proxy username to use for proxy authentication.
* @param string|null $password Optional proxy password to use for proxy authentication.
* @return string|false The response content or false on failure.
*/
function curlGetWithProxy(string $url, $proxy = null, $proxyType = 'http', $cacheTime = 86400 * 360, string $cacheDir = __DIR__ . '/.cache/', $username = null, $password = null)
{
// Generate cache file path based on URL
if (!file_exists($cacheDir)) {
mkdir($cacheDir);
}
$cacheFile = $cacheDir . md5($url);
// Check if cached data exists and is still valid
if ($cacheTime > 0 && file_exists($cacheFile) && time() - filemtime($cacheFile) < $cacheTime) {
// Return cached response
return read_file($cacheFile);
}
// Initialize cURL session (forward optional proxy credentials to buildCurl)
// buildCurl signature: buildCurl($proxy, $type, $endpoint, $headers = [], $username = null, $password = null, ...)
$ch = buildCurl($proxy, $proxyType, $url, [], $username, $password);
if ($ch) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
}
// Execute the request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
// echo 'Error: ' . curl_error($ch);
} else {
// Save response to cache file
if ($cacheTime > 0) {
write_file($cacheFile, $response);
}
}
// Close cURL session
curl_close($ch);
// Return the response
return $response;
}
/**
* Function to extract IP:PORT combinations from a text file and rewrite the file with only IP:PORT combinations.
*
* @param string $filename The path to the text file.
* @return bool True on success, false on failure.
*/
function rewriteIpPortFile(string $filename): bool
{
if (!file_exists($filename) || !is_readable($filename) || !is_writable($filename)) {
echo "File '$filename' is not readable or writable" . PHP_EOL;
return false;
}
// Open the file for reading
$file = @fopen($filename, 'r');
if (!$file) {
echo "Error opening $filename for reading" . PHP_EOL;
return false;
}
// Open a temporary file for writing
$tempFilename = tempnam(__DIR__ . '/tmp', 'rewriteIpPortFile');
$tempFile = @fopen($tempFilename, 'w');
if (!$tempFile) {
fclose($file);
// Close the original file
echo "Error opening temporary ($tempFilename) file for writing";
return false;
}
// Read each line from the file and extract IP:PORT combinations
while (($line = fgets($file)) !== false) {
// Match IP:PORT pattern using regular expression
preg_match_all('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)/', $line, $matches);
// Write matched IP:PORT combinations to the temporary file
foreach ($matches[0] as $match) {
if (!empty(trim($match))) {
fwrite($tempFile, $match . "\n");
}
}
}
// Close both files
fclose($file);
fclose($tempFile);
// Replace the original file with the temporary file
if (!rename($tempFilename, $filename)) {
echo 'Error replacing original file with temporary file';
return false;
}
return true;
}
/**
* Reads a file line by line and returns its content as an array.
*
* @param string $filename The path to the file to be read.
* @return array|false An array containing the lines of the file on success, false on failure.
*/
function readFileLinesToArray(string $filename)
{
// Check if the file exists and is readable
if (!is_readable($filename)) {
return false;
}
$lines = [];
// Open the file for reading
$file = @fopen($filename, 'r');
// Read each line until the end of the file
while (!feof($file)) {
// Read the line
$line = fgets($file);
// Add the line to the array
$lines[] = $line;
}
// Close the file
fclose($file);
return $lines;
}
/**
* Get all files with a specific extension in a folder.
*
* @param string $folder The folder path to search for files.
* @param string|null $extension The file extension to filter files by.
*
* @return array An array containing full paths of files with the specified extension.
*/
function getFilesByExtension(string $folder, $extension = 'txt'): array
{
if (!file_exists($folder)) {
echo "$folder not exist" . PHP_EOL;
return [];
}
$files = [];
$folder = rtrim($folder, '/') . '/';
// Ensure folder path ends with a slash
// Open the directory
if ($handle = opendir($folder)) {
// Loop through directory contents
while (false !== ($entry = readdir($handle))) {
$file = $folder . $entry;
// ensure it's a file
if ($entry != '.' && $entry != '..' && is_file($file)) {
if (!empty($extension)) {
// Check if file has the specified extension
if (pathinfo($file, PATHINFO_EXTENSION) === $extension) {
$files[] = realpath($file);
}
}
}
}
closedir($handle);
} else {
echo "cannot open $folder\n";
}
return $files;
}
// Function to parse command line arguments
function parseArgs($args): array
{
$parsedArgs = [];
foreach ($args as $arg) {
if (substr($arg, 0, 2) === '--') {
// Argument is in the format --key=value
$parts = explode('=', substr($arg, 2), 2);
$key = $parts[0];
$value = $parts[1] ?? true;
// If value is not provided, set it to true
$parsedArgs[$key] = $value;
}
}
return $parsedArgs;
}
// Default user ID to "CLI" assuming the script is running from the command line
$user_id = 'CLI';
// Check if the script is running in CLI mode or not
if (!$isCli) {
// --- Case 1: Running via web server ---
// Prioritize email → then user_id → then session_id
if (isset($_SESSION['email']) && isValidEmail($_SESSION['email'])) {
// Use valid session email first
$user_id_source = $_SESSION['email'];
} elseif (!empty($_SESSION['user_id'])) {
// Use session user ID if available
$user_id_source = $_SESSION['user_id'];
} else {
// Fallback: use PHP session ID
$user_id_source = session_id();
}
// Hash the chosen ID for privacy and consistency
$user_id = md5($user_id_source);
} else {
// --- Case 2: Running in CLI mode ---
// Parse command-line arguments using a helper function
$parsedArgs = parseArgs($argv);
// Extract CLI user ID if provided (userId or uid)
$cliUserId = '';
if (!empty($parsedArgs)) {
if (isset($parsedArgs['userId'])) {
$cliUserId = $parsedArgs['userId'];
} elseif (isset($parsedArgs['uid'])) {
$cliUserId = $parsedArgs['uid'];
}
}
// If CLI user ID is non-empty, override default "CLI"
if (!empty(trim($cliUserId))) {
$user_id = $cliUserId;
}
}
// Set the resolved user ID in your system
setUserId($user_id);
/**
* Set the current user ID and create a user config file if it doesn't exist.
*
* @param string $new_user_id The new user ID to be set.
*
* This function sets the global user ID, ensures the user config file exists,
* and writes default config if the file is not already present.
*/
function setUserId(string $new_user_id)
{
global $user_id;
$user_file = !empty($new_user_id) ? getUserFile($new_user_id) : null;
if ($user_file != null) {
// Ensure the directory for the user file exists
if (!file_exists(dirname($user_file))) {
mkdir(dirname($user_file), 0777, true);
}
// Write default user config if file does not exist
if (!file_exists($user_file)) {
$headers = [
'X-Dynatrace: MT_3_6_2809532683_30-0_24d94a15-af8c-49e7-96a0-1ddb48909564_0_1_619',
'X-Api-Key: vT8tINqHaOxXbGE7eOWAhA==',
'Authorization: Bearer',
'X-Request-Id: 63337f4c-ec03-4eb8-8caa-4b7cd66337e3',
'X-Request-At: 2024-04-07T20:57:14.73+07:00',
'X-Version-App: 5.8.8',
'User-Agent: myXL / 5.8.8(741); StandAloneInstall; (samsung; SM-G955N; SDK 25; Android 7.1.2)',
'Content-Type: application/json; charset=utf-8',
];
$data = [
'endpoint' => $new_user_id == 'CLI'
? 'https://api.myxl.xlaxiata.co.id/api/v1/xl-stores/options/list'
: 'https://bing.com',
'headers' => $new_user_id == 'CLI'
? $headers
: ['User-Agent: Mozilla/5.0 (Linux; Android 14; Pixel 6 Pro Build/UPB3.230519.014) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/114.0.5735.60 Mobile Safari/537.36 GNews Android/2022137898'],
'type' => 'http|socks4|socks5',
];
$file = getUserFile($new_user_id);
write_file($file, json_encode($data));
}
// Replace global user ID if different from current
if ($user_id != $new_user_id) {
$user_id = $new_user_id;
}
}
}
/**
* Retrieve the current global user ID.
*
* @return string The current user ID.
*/
function getUserId(): string
{
global $user_id;
return $user_id;
}
if (!file_exists(__DIR__ . '/config')) {
mkdir(__DIR__ . '/config');
}
setMultiPermissions(__DIR__ . '/config');
function getUserFile(string $user_id): string
{
return __DIR__ . "/config/$user_id.json";
}
function getUserStatusFile(string $user_id): string
{
return __DIR__ . "/tmp/status/$user_id.txt";
}
function getUserLogFile(string $user_id): string
{
return __DIR__ . "/tmp/logs/$user_id.txt";
}
function resetUserLogFile(string $user_id): bool
{
$user_file = getUserLogFile($user_id);
$now = date('Y-m-d H:i:s');
$content = "Log reset at $now\n";
return file_put_contents($user_file, $content, LOCK_EX) !== false;
}
function addUserLog(string $user_id, string $message): bool
{
$user_file = getUserLogFile($user_id);
if (!file_exists(dirname($user_file))) {
mkdir(dirname($user_file), 0777, true);
}
if (!file_exists($user_file)) {
$now = date('Y-m-d H:i:s');
$header = "Log created at $now\n";
file_put_contents($user_file, $header, LOCK_EX);
}
return file_put_contents($user_file, date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL, FILE_APPEND | LOCK_EX) !== false;
}
function getConfig(string $user_id): array
{
$user_file = getUserFile($user_id);
if (!file_exists($user_file)) {
setUserId($user_id);
$user_file = getUserFile($user_id);
}
if (!is_readable($user_file)) {
setMultiPermissions($user_file, false);
}
// Read the JSON file into a string
$jsonString = read_file($user_file);
// Decode the JSON string into a PHP array
$data = json_decode($jsonString, true);
// Use true for associative array, false or omit for object
$defaults = [
'endpoint' => 'https://google.com',
'headers' => [],
'type' => 'http|socks4|socks5',
'user_id' => $user_id,
];
// Check if decoding was successful
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
// Decoding failed
// echo 'Error decoding JSON: ' . json_last_error_msg();
return $defaults;
} else {
return mergeArrays($defaults, $data);
}
}
function setConfig($user_id, $data): array
{
$user_file = getUserFile($user_id);
$defaults = getConfig($user_id);
// remove conflict data
unset($defaults['headers']);
// Encode the data to JSON format
$nData = mergeArrays($defaults, $data);
$newData = json_encode($nData);
// write data
file_put_contents($user_file, $newData);
// set permission
setMultiPermissions($user_file);
return $nData;
}
/**
* Merges two shallow multidimensional arrays.
*
* This function merges two multidimensional arrays while preserving the structure.
* If a key exists in both arrays, sub-arrays are merged recursively.
* Values from the second array override those from the first array if they have the same keys.
* If a key exists in the second array but not in the first one, it will be added to the merged array.
*
* @param array $arr1 The first array to merge.
* @param array $arr2 The second array to merge.
* @return array The merged array.
*/
function mergeArrays(array $arr1, array $arr2): array
{
$keys = array_keys($arr2);
foreach ($keys as $key) {
if (isset($arr1[$key]) && is_numeric($key)) {
array_push($arr1, $arr2[$key]);
} elseif (isset($arr1[$key]) && is_array($arr1[$key]) && is_array($arr2[$key])) {
$arr1[$key] = array_unique(mergeArrays((array)$arr1[$key], (array)$arr2[$key]));
} else {
$arr1[$key] = $arr2[$key];
}
}
return $arr1;
}
/**
* Set Cache-Control and Expires headers for HTTP caching.
*
* @param int|float $max_age_minutes The maximum age of the cache in minutes.
* @param bool $cors Whether to include CORS headers. Default is true.
* @return void
*/
function setCacheHeaders($max_age_minutes, bool $cors = true): void
{
if ($cors) {
// Allow from any origin
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: *');
header('Access-Control-Allow-Methods: *');
}
// Set the Cache-Control header to specify the maximum age in minutes and must-revalidate
$max_age_seconds = $max_age_minutes * 60;
header('Cache-Control: max-age=' . $max_age_seconds . ', must-revalidate');
// Set the Expires header to the calculated expiration time
$expiration_time = time() + $max_age_seconds;
header('Expires: ' . gmdate('D, d M Y H:i:s', $expiration_time) . ' GMT');
}
/**
* Prompts the user for confirmation with a message.
*
* @param string $message The confirmation message.
* @return bool True if user confirms (y/yes), false otherwise (n/no).
*/
function confirmAction(string $message = 'Are you sure? (y/n): '): bool
{
$validResponses = ['y', 'yes', 'n', 'no'];
$response = '';
while (!in_array($response, $validResponses)) {
echo $message;
$response = strtolower(trim(fgets(STDIN)));
}
return in_array($response, ['y', 'yes']);
}
/**
* Iterate over the array, limiting the number of iterations to the specified limit.
*
* If the length of the array is greater than the limit, only the first `$limit` items will be iterated over.
*
* @param array $array The array to iterate over.
* @param int $limit The maximum number of items to iterate over. Default is 50.
* @param callable|null $callback A callback function to be called for each item during iteration.
* @return void
*/
function iterateArray(array $array, int $limit = 50, $callback = null)
{
$arrayLength = count($array);
$limit = min($arrayLength, $limit);
// Get the minimum of array length and $limit
for ($i = 0; $i < $limit; $i++) {
// Access array element at index $i and perform desired operations
$item = $array[$i];
if ($callback !== null && is_callable($callback)) {
call_user_func($callback, $item);
} else {
echo $item . "\n";
}
}
}
/**
* Fixes a text file containing NUL characters by removing them.
*
* @param string $inputFile The path to the input file.
* @return void
*/
function fixFile(string $inputFile)
{
if (!file_exists($inputFile)) {
echo "fixFile: $inputFile is not found" . PHP_EOL;
return;
}
if (is_file_locked($inputFile)) {
echo "fixFile: $inputFile is locked" . PHP_EOL;
return;
}
if (!is_writable($inputFile)) {
echo "fixFile: $inputFile is not writable" . PHP_EOL;
return;
}
// Open the file for reading and writing (binary mode)
$fileHandle = @fopen($inputFile, 'r+');
// Attempt to acquire an exclusive lock on the file
if (flock($fileHandle, LOCK_EX)) {
// Iterate through the file and remove NUL characters
while (!feof($fileHandle)) {
// Read a chunk of data
$chunk = fread($fileHandle, 1024 * 1024);
// Read in 500KB chunks
if (!$chunk) {
continue;
}
// Remove NUL characters directly from the chunk
$cleanedChunk = str_replace("\x00", '', $chunk);
// Rewind to the current position
fseek($fileHandle, -strlen($chunk), SEEK_CUR);
// Write the cleaned chunk back to the file
fwrite($fileHandle, $cleanedChunk);
}
// Truncate the file to remove any extra content after cleaning
ftruncate($fileHandle, ftell($fileHandle));
// Release the lock
flock($fileHandle, LOCK_UN);
// Close the file handle
fclose($fileHandle);
} else {
echo 'fixFile: Unable to acquire lock.' . PHP_EOL;
}
}
function isCygwinInstalled()
{
$cygwinExecutables = ['bash', 'ls'];
// List of Cygwin executables to check
foreach ($cygwinExecutables as $executable) {
$output = shell_exec("where $executable 2>&1");
if (strpos($output, 'not found') === false) {
return true;
// Found at least one Cygwin executable
}
}
return false;
// None of the Cygwin executables found
}
function runPythonInBackground($scriptPath, $commandArgs = [], $identifier = null)
{
global $isWin;
// Convert arguments to command line string
$commandArgsString = '';
foreach ($commandArgs as $key => $value) {
$escapedValue = escapeshellarg($value);
$commandArgsString .= "--$key=$escapedValue ";
}
$commandArgsString = trim($commandArgsString);
// Determine paths and commands
$cwd = __DIR__;
$filename = !empty($identifier) ? sanitizeFilename($identifier) : sanitizeFilename(unixPath("$scriptPath/$commandArgsString"));
$runner = unixPath(tmp() . "/runners/$filename" . ($isWin ? '.bat' : '.sh'));
$output_file = unixPath(tmp() . "/logs/$filename.txt");
$pid_file = unixPath(tmp() . "/runners/$filename.pid");
// Truncate output file
truncateFile($output_file);
// Construct the command
$venv = !$isWin ? realpath("$cwd/venv/bin/activate") : realpath("$cwd/venv/Scripts/activate");
$venvCall = $isWin ? "call $venv" : "source $venv";
$cmd = "$venvCall && python $scriptPath $commandArgsString > $output_file 2>&1 & echo $! > $pid_file";
$cmd = trim($cmd);
// Write command to runner script
$write = write_file($runner, $cmd);
if (!$write) {
return ['error' => 'Failed writing shell script ' . $runner];
}
// Change current working directory
chdir($cwd);
// Execute the runner script
if ($isWin) {
$runner_win = 'start /B "window_name" ' . escapeshellarg(unixPath($runner));
pclose(popen($runner_win, 'r'));
} else {
exec('bash ' . escapeshellarg($runner) . ' > /dev/null 2>&1 &');
}
return [
'output' => unixPath($output_file),
'cwd' => unixPath($cwd),
'relative' => str_replace(unixPath($cwd), '', unixPath($output_file)),
'runner' => $runner,
];
}
function runShellCommandLive($command)
{
if (!is_string($command)) {
throw new InvalidArgumentException('Command must be a string');
}
// Open a process for the command
$process = proc_open($command, [
1 => ['pipe', 'w'], // stdout
2 => ['pipe', 'w'], // stderr
], $pipes);
if (is_resource($process)) {
// Get the stdout pipe
$stdout = $pipes[1];
$stderr = $pipes[2];
// Close stderr pipe
fclose($stderr);
// Read stdout line by line
while (($line = fgets($stdout)) !== false) {
echo htmlspecialchars($line) . "\n";
flush();
// Make sure the output is sent to the browser immediately
}
fclose($stdout);
// Wait for the process to finish
$return_value = proc_close($process);
// Check for errors if necessary
if ($return_value !== 0) {
echo "Command failed with return code $return_value";
}
} else {
throw new RuntimeException('Failed to open process');
}
}
function safe_json_decode($json, $assoc = true)
{
$decoded = json_decode($json, $assoc);
// Check for JSON decode errors
if (json_last_error() !== JSON_ERROR_NONE) {