-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathhawki
executable file
·1097 lines (912 loc) · 40.3 KB
/
hawki
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
#!/usr/bin/env php
<?php
/**
* HAWKI Command Line Utility
*/
// Define color constants
define('GREEN', "\033[32m");
define('BLUE', "\033[34m");
define('RED', "\033[31m");
define('YELLOW', "\033[33m");
define('RESET', "\033[0m");
define('BOLD', "\033[1m");
define('LOGO', "
▗▖ ▗▖ ▗▄▖ ▗▖ ▗▖▗▖ ▗▖▗▄▄▄▖
▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌▗▞▘ █
▐▛▀▜▌▐▛▀▜▌▐▌ ▐▌▐▛▚▖ █
▐▌ ▐▌▐▌ ▐▌▐▙█▟▌▐▌ ▐▌▗▄█▄▖
");
// Get the command argument
$command = $argv[1] ?? 'help';
$flags = array_slice($argv, 2);
// Process the command
switch ($command) {
case 'check':
checkDependencies();
break;
case 'init':
case 'initialize':
if(isset($flags[0]) && isset($flags[0]) === '-all'){
initializeAll();
}
else{
initialize();
}
break;
case 'setup':
setupEnv($flags);
break;
case 'setup-models':
setupModelProviders();
break;
case 'clear-cache':
clearCache();
break;
case 'migrate':
migrate($flags);
break;
case 'token':
if (isset($flags[0]) && $flags[0] === '--revoke') {
passthru('php artisan app:token --revoke');
} else {
passthru('php artisan app:token');
}
break;
case 'remove-user':
passthru('php artisan app:removeuser');
break;
case 'run':
if (isset($flags[0])) {
switch ($flags[0]) {
case '-dev':
runDev();
break;
case '-build':
runBuild();
break;
default:
echo RED . "Invalid flag for run command: {$flags[0]}" . RESET . PHP_EOL;
showHelp();
}
} else {
echo RED . "Missing flag for run command. Use -dev or -build" . RESET . PHP_EOL;
showHelp();
}
break;
case 'stop':
stopProcesses();
break;
case 'help':
showHelp();
break;
default:
echo RED . "Unknown command: $command" . RESET . PHP_EOL;
showHelp();
}
function showHelp() {
echo PHP_EOL . BLUE . LOGO . RESET . PHP_EOL . PHP_EOL;
echo BOLD . "HAWKI Command Line Utility" . RESET . PHP_EOL;
echo "Usage: php hawki [command] [options]" . PHP_EOL . PHP_EOL;
echo "Available commands:" . PHP_EOL;
echo " check - Check required dependencies" . PHP_EOL;
echo " init, initialize - Initialize the project" . PHP_EOL;
echo " Flags:" . PHP_EOL;
echo " -all - Continue to setup process". PHP_EOL;
echo " setup [flag] - Configure environment variables" . PHP_EOL;
echo " Flags:" . PHP_EOL;
echo " -g - General settings" . PHP_EOL;
echo " -db - Database settings" . PHP_EOL;
echo " -auth - Authentication settings" . PHP_EOL;
echo " -reverb - Reverb settings" . PHP_EOL;
echo " setup-models - Configure AI model providers" . PHP_EOL;
echo " clear-cache - Clear all Laravel caches" . PHP_EOL;
echo " migrate [--fresh] - Run database migrations" . PHP_EOL;
echo " token [--revoke] - Create or revoke API tokens for a user" . PHP_EOL;
echo " remove-user - Remove a user from the system" . PHP_EOL;
echo " run -dev - Run development servers" . PHP_EOL;
echo " run -build - Build the project" . PHP_EOL;
echo " stop - Stop all running processes" . PHP_EOL;
echo " help - Show this help message" . PHP_EOL . PHP_EOL;
echo BOLD . GREEN . "For more information please refer to HAWKI documentation at:" . RESET . PHP_EOL;
echo BOLD . "https://hawk-digital-environments.github.io/HAWKI2-Documentation/" . RESET . PHP_EOL . PHP_EOL;
echo "April 2025 - ArianSDF @IxdLab Design Faculty of HAWK Hildesheim Holzminden Göttingen" . RESET . PHP_EOL . PHP_EOL;
}
function checkDependencies() {
echo BOLD . "Checking dependencies..." . RESET . PHP_EOL;
// Check PHP version
$phpVersion = phpversion();
$phpRequired = '8.1.0';
if (version_compare($phpVersion, $phpRequired, '>=')) {
echo GREEN . "✓ PHP Version: $phpVersion" . RESET . PHP_EOL;
} else {
echo RED . "✗ PHP Version: $phpVersion (required: >= $phpRequired)" . RESET . PHP_EOL;
}
// Check Composer
exec('composer --version 2>/dev/null', $composerOutput, $composerReturnVar);
if ($composerReturnVar === 0) {
echo GREEN . "✓ Composer: " . $composerOutput[0] . RESET . PHP_EOL;
} else {
echo RED . "✗ Composer not found" . RESET . PHP_EOL;
$missingDeps[] = 'composer';
}
// Check Node.js
exec('node --version 2>/dev/null', $nodeOutput, $nodeReturnVar);
if ($nodeReturnVar === 0) {
$nodeVersion = trim($nodeOutput[0]);
echo GREEN . "✓ Node.js: $nodeVersion" . RESET . PHP_EOL;
} else {
echo RED . "✗ Node.js not found" . RESET . PHP_EOL;
$missingDeps[] = 'nodejs';
}
// Check npm
exec('npm --version 2>/dev/null', $npmOutput, $npmReturnVar);
if ($npmReturnVar === 0) {
$npmVersion = trim($npmOutput[0]);
echo GREEN . "✓ npm: $npmVersion" . RESET . PHP_EOL;
} else {
echo RED . "✗ npm not found" . RESET . PHP_EOL;
$missingDeps[] = 'npm';
}
// Check PHP extensions
$requiredExtensions = ['mbstring', 'xml', 'pdo', 'curl', 'zip', 'json', 'fileinfo', 'openssl'];
$missingExtensions = [];
foreach ($requiredExtensions as $extension) {
if (extension_loaded($extension)) {
echo GREEN . "✓ PHP extension: $extension" . RESET . PHP_EOL;
} else {
echo RED . "✗ PHP extension: $extension (missing)" . RESET . PHP_EOL;
$missingExtensions[] = $extension;
}
}
// Summary and offer to install missing dependencies
if (!empty($missingDeps) || !empty($missingExtensions)) {
echo PHP_EOL . YELLOW . "Some dependencies are missing. Installation instructions:" . RESET . PHP_EOL;
if (!empty($missingDeps)) {
echo BOLD . "Missing tools:" . RESET . PHP_EOL;
foreach ($missingDeps as $dep) {
switch ($dep) {
case 'composer':
echo "- Composer: https://getcomposer.org/download/" . PHP_EOL;
break;
case 'nodejs':
case 'npm':
echo "- Node.js and npm: https://nodejs.org/en/download/" . PHP_EOL;
break;
}
}
}
if (!empty($missingExtensions)) {
echo BOLD . "Missing PHP extensions:" . RESET . PHP_EOL;
echo "You can install them using your package manager, for example:" . PHP_EOL;
// Detect OS
$os = PHP_OS;
if (stripos($os, 'darwin') !== false) {
// macOS
echo " brew install php" . PHP_EOL;
} elseif (stripos($os, 'linux') !== false) {
// Linux (assume Debian/Ubuntu)
$extensionsList = implode(' ', array_map(function($ext) {
return "php-$ext";
}, $missingExtensions));
echo " sudo apt update" . PHP_EOL;
echo " sudo apt install $extensionsList" . PHP_EOL;
} elseif (stripos($os, 'win') !== false) {
// Windows
echo " Enable extensions in your php.ini file" . PHP_EOL;
echo " Find the php.ini file and uncomment the lines with these extensions" . PHP_EOL;
}
}
// Ask if auto-installation should be attempted (for some dependencies)
if (!empty($missingDeps) && (stripos(PHP_OS, 'linux') !== false || stripos(PHP_OS, 'darwin') !== false)) {
echo PHP_EOL . "Would you like to try to install the missing tools? (y/n) ";
$answer = trim(fgets(STDIN));
if (strtolower($answer) === 'y') {
foreach ($missingDeps as $dep) {
switch ($dep) {
case 'composer':
echo YELLOW . "Installing Composer..." . RESET . PHP_EOL;
passthru('curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer');
break;
case 'nodejs':
case 'npm':
if (stripos(PHP_OS, 'darwin') !== false) {
echo YELLOW . "Installing Node.js using Homebrew..." . RESET . PHP_EOL;
passthru('brew install node');
} elseif (stripos(PHP_OS, 'linux') !== false) {
echo YELLOW . "Installing Node.js using apt..." . RESET . PHP_EOL;
passthru('sudo apt update && sudo apt install nodejs npm');
}
break;
}
}
// Recheck dependencies after installation attempts
echo PHP_EOL . BOLD . "Rechecking dependencies after installation attempts..." . RESET . PHP_EOL;
checkDependencies();
}
}
} else {
echo PHP_EOL . GREEN . BOLD . "All dependencies are satisfied!" . RESET . PHP_EOL;
}
}
#NOTE:
function initializeAll(){
initialize();
if(askForNextStep('Variables Setup')){
setupEnv(null);
}
if(askForNextStep('AI Model Providers Setup')){
setupModelProviders();
}
if(askForNextStep('Database Migration')){
migrate(null);
}
}
function askForNextStep($nextStep){
echo PHP_EOL . "Do you want to proceed to " . BOLD . $nextStep . RESET . "? (y/n): ";
$answer = strtolower(trim(fgets(STDIN)));
if ($answer === 'y') {
return true;
}
else{
return false;
}
}
function initialize() {
echo BOLD . "Initializing HAWKI project..." . RESET . PHP_EOL;
echo PHP_EOL . BLUE . LOGO . RESET . PHP_EOL . PHP_EOL;
// Check for .env file
if (!file_exists('.env')) {
if (file_exists('.env.example')) {
echo "Creating .env file from .env.example..." . PHP_EOL;
copy('.env.example', '.env');
echo GREEN . "✓ Created .env file" . RESET . PHP_EOL;
} else {
echo RED . "✗ .env.example not found" . RESET . PHP_EOL;
}
} else {
echo GREEN . "✓ .env file already exists" . RESET . PHP_EOL;
}
// Check for test_users.json
if (!file_exists('storage/app/test_users.json')) {
if (file_exists('storage/app/test_users.json.example')) {
echo "Creating test_users.json from example..." . PHP_EOL;
copy('storage/app/test_users.json.example', 'storage/app/test_users.json');
echo GREEN . "✓ Created test_users.json" . RESET . PHP_EOL;
} else {
echo YELLOW . "! test_users.json.example not found" . RESET . PHP_EOL;
}
} else {
echo GREEN . "✓ test_users.json already exists" . RESET . PHP_EOL;
}
// Check for model_providers.php
if (!file_exists('config/model_providers.php')) {
if (file_exists('config/model_providers.php.example')) {
echo "Creating model_providers.php from example..." . PHP_EOL;
copy('config/model_providers.php.example', 'config/model_providers.php');
echo GREEN . "✓ Created model_providers.php" . RESET . PHP_EOL;
} else {
echo YELLOW . "! model_providers.php.example not found" . RESET . PHP_EOL;
}
} else {
echo GREEN . "✓ model_providers.php already exists" . RESET . PHP_EOL;
}
// Run initialization commands
echo PHP_EOL . "Running initialization commands..." . PHP_EOL;
// Composer install
echo YELLOW . "Running composer install..." . RESET . PHP_EOL;
passthru('composer install');
// Check if we need to update composer
echo YELLOW . "Checking for composer updates..." . RESET . PHP_EOL;
exec('composer outdated --direct', $outdatedOutput);
if (!empty($outdatedOutput) && count($outdatedOutput) > 1) { // First line is header
echo "Outdated packages found. Running composer update..." . PHP_EOL;
passthru('composer update');
} else {
echo GREEN . "✓ All composer packages are up to date" . RESET . PHP_EOL;
}
// npm install
echo YELLOW . "Running npm install..." . RESET . PHP_EOL;
passthru('npm install');
// Create storage symlink
echo YELLOW . "Creating storage link..." . RESET . PHP_EOL;
passthru('php artisan storage:link');
// Generate app key if not present
$env = getEnvContent();
if (strpos($env, 'APP_KEY=') === false || strpos($env, 'APP_KEY=base64:') === false) {
echo YELLOW . "Generating application key..." . RESET . PHP_EOL;
passthru('php artisan key:generate');
$env = getEnvContent(); // Reload env content after key generation
}
// Generate security keys and salts if not present
echo YELLOW . "Checking security keys and salts..." . RESET . PHP_EOL;
// Reverb App Keys
if (!getEnvValue('REVERB_APP_KEY', $env)) {
$reverbAppKey = generateAlphanumeric(32);
$env = setEnvValue('REVERB_APP_KEY', $reverbAppKey, $env);
echo GREEN . "✓ Generated REVERB_APP_KEY" . RESET . PHP_EOL;
}
if (!getEnvValue('REVERB_APP_SECRET', $env)) {
$reverbAppSecret = generateAlphanumeric(32);
$env = setEnvValue('REVERB_APP_SECRET', $reverbAppSecret, $env);
echo GREEN . "✓ Generated REVERB_APP_SECRET" . RESET . PHP_EOL;
}
// Security Salts - use secure random bytes for these
$saltKeys = [
'USERDATA_ENCRYPTION_SALT',
'INVITATION_SALT',
'AI_CRYPTO_SALT',
'PASSKEY_SALT',
'BACKUP_SALT'
];
foreach ($saltKeys as $saltKey) {
if (!getEnvValue($saltKey, $env)) {
$salt = bin2hex(random_bytes(16)); // 32 character hex string
$env = setEnvValue($saltKey, $salt, $env);
echo GREEN . "✓ Generated $saltKey" . RESET . PHP_EOL;
}
}
// Save environment changes
saveEnv($env);
// Clear all caches to ensure everything is fresh
clearCache();
echo PHP_EOL . GREEN . BOLD . "Initialization complete!" . RESET . PHP_EOL;
}
function setupEnv($flags) {
// If no flags, run all sections
if (empty($flags)) {
setupGeneralEnv();
setupDatabaseEnv();
setupAuthEnv();
setupReverbEnv();
return;
}
// Process specific flags
switch ($flags[0]) {
case '-g':
setupGeneralEnv();
break;
case '-db':
setupDatabaseEnv();
break;
case '-auth':
setupAuthEnv();
break;
case '-reverb':
setupReverbEnv();
break;
default:
echo RED . "Unknown flag: {$flags[0]}" . RESET . PHP_EOL;
echo "Available flags: -g (General), -db (Database), -auth (Authentication), -reverb (Reverb)" . PHP_EOL;
}
}
function setupGeneralEnv() {
echo BOLD . "Setting up General Environment Variables" . RESET . PHP_EOL;
$env = getEnvContent();
// APP_NAME
$current = getEnvValue('APP_NAME', $env);
$appName = promptWithDefault("APP_NAME", $current);
$env = setEnvValue('APP_NAME', $appName, $env);
// APP_URL
$current = getEnvValue('APP_URL', $env);
$appUrl = promptWithDefault("APP_URL", $current ?: 'http://localhost:8000');
$env = setEnvValue('APP_URL', $appUrl, $env);
// APP_TIMEZONE
$current = getEnvValue('APP_TIMEZONE', $env);
$appTimezone = promptWithDefault("APP_TIMEZONE", $current ?: 'UTC');
$env = setEnvValue('APP_TIMEZONE', $appTimezone, $env);
// APP_LOCALE
$current = getEnvValue('APP_LOCALE', $env);
$appLocale = promptWithDefault("APP_LOCALE (de_DE or en_US)", $current ?: 'en_US');
$env = setEnvValue('APP_LOCALE', $appLocale, $env);
// ALLOW_EXTERNAL_COMMUNICATION
$current = getEnvValue('ALLOW_EXTERNAL_COMMUNICATION', $env);
$allowExtComm = promptWithDefault("ALLOW_EXTERNAL_COMMUNICATION (true/false)", $current ?: 'false');
$env = setEnvValue('ALLOW_EXTERNAL_COMMUNICATION', $allowExtComm, $env);
saveEnv($env);
echo GREEN . "✓ General environment variables updated" . RESET . PHP_EOL;
}
function setupDatabaseEnv() {
echo BOLD . "Setting up Database Environment Variables" . RESET . PHP_EOL;
$env = getEnvContent();
// DB_CONNECTION
$current = getEnvValue('DB_CONNECTION', $env);
$dbConnection = promptWithDefault("DB_CONNECTION (mysql, sqlite, pgsql)", $current ?: 'mysql');
$env = setEnvValue('DB_CONNECTION', $dbConnection, $env);
// DB_HOST
$current = getEnvValue('DB_HOST', $env);
$dbHost = promptWithDefault("DB_HOST", $current ?: '127.0.0.1');
$env = setEnvValue('DB_HOST', $dbHost, $env);
// DB_PORT
$current = getEnvValue('DB_PORT', $env);
$defaultPort = $dbConnection === 'mysql' ? '3306' : ($dbConnection === 'pgsql' ? '5432' : '');
$dbPort = promptWithDefault("DB_PORT", $current ?: $defaultPort);
$env = setEnvValue('DB_PORT', $dbPort, $env);
// DB_DATABASE
$current = getEnvValue('DB_DATABASE', $env);
$dbDatabase = promptWithDefault("DB_DATABASE", $current ?: 'hawki');
$env = setEnvValue('DB_DATABASE', $dbDatabase, $env);
// DB_USERNAME
$current = getEnvValue('DB_USERNAME', $env);
$dbUsername = promptWithDefault("DB_USERNAME", $current ?: 'root');
$env = setEnvValue('DB_USERNAME', $dbUsername, $env);
// DB_PASSWORD
$current = getEnvValue('DB_PASSWORD', $env);
$dbPassword = promptWithDefault("DB_PASSWORD", $current ?: '');
$env = setEnvValue('DB_PASSWORD', $dbPassword, $env);
saveEnv($env);
echo GREEN . "✓ Database environment variables updated" . RESET . PHP_EOL;
}
function setupAuthEnv() {
echo BOLD . "Setting up Authentication Environment Variables" . RESET . PHP_EOL;
$env = getEnvContent();
// AUTHENTICATION_METHOD
$current = getEnvValue('AUTHENTICATION_METHOD', $env);
$authMethod = promptWithDefault("AUTHENTICATION_METHOD (LDAP, Shibboleth, OIDC)", $current ?: 'LDAP');
$env = setEnvValue('AUTHENTICATION_METHOD', $authMethod, $env);
// Setup specific auth method variables
switch (strtoupper($authMethod)) {
case 'LDAP':
setupLdapEnv($env);
break;
case 'SHIBBOLETH':
setupShibbolethEnv($env);
break;
case 'OIDC':
setupOidcEnv($env);
break;
default:
echo YELLOW . "Unknown authentication method: $authMethod" . RESET . PHP_EOL;
}
}
function setupLdapEnv(&$env) {
// LDAP_CONNECTION
$current = getEnvValue('LDAP_CONNECTION', $env);
$ldapConnection = promptWithDefault("LDAP_CONNECTION", $current ?: 'default');
$env = setEnvValue('LDAP_CONNECTION', $ldapConnection, $env);
// LDAP_HOST
$current = getEnvValue('LDAP_HOST', $env);
$ldapHost = promptWithDefault("LDAP_HOST", $current ?: 'ldap.example.com');
$env = setEnvValue('LDAP_HOST', $ldapHost, $env);
// LDAP_PORT
$current = getEnvValue('LDAP_PORT', $env);
$ldapPort = promptWithDefault("LDAP_PORT", $current ?: '389');
$env = setEnvValue('LDAP_PORT', $ldapPort, $env);
// LDAP_USERNAME
$current = getEnvValue('LDAP_USERNAME', $env);
$ldapUsername = promptWithDefault("LDAP_USERNAME", $current ?: 'cn=admin,dc=example,dc=com');
$env = setEnvValue('LDAP_USERNAME', $ldapUsername, $env);
// LDAP_BIND_PW
$current = getEnvValue('LDAP_BIND_PW', $env);
$ldapBindPw = promptWithDefault("LDAP_BIND_PW", $current ?: '');
$env = setEnvValue('LDAP_BIND_PW', $ldapBindPw, $env);
// LDAP_BASE_DN
$current = getEnvValue('LDAP_BASE_DN', $env);
$ldapBaseDn = promptWithDefault("LDAP_BASE_DN", $current ?: 'dc=example,dc=com');
$env = setEnvValue('LDAP_BASE_DN', $ldapBaseDn, $env);
// LDAP_ATTRIBUTES
$current = getEnvValue('LDAP_ATTRIBUTES', $env);
$ldapAttributes = promptWithDefault("LDAP_ATTRIBUTES", $current ?: 'cn,mail,displayname');
$env = setEnvValue('LDAP_ATTRIBUTES', $ldapAttributes, $env);
// TEST_USER_LOGIN
$current = getEnvValue('TEST_USER_LOGIN', $env);
$testUserLogin = promptWithDefault("TEST_USER_LOGIN (true/false)", $current ?: 'false');
$env = setEnvValue('TEST_USER_LOGIN', $testUserLogin, $env);
saveEnv($env);
echo GREEN . "✓ LDAP environment variables updated" . RESET . PHP_EOL;
}
function setupShibbolethEnv(&$env) {
// SHIBBOLETH_LOGIN_URL
$current = getEnvValue('SHIBBOLETH_LOGIN_URL', $env);
$shibLoginUrl = promptWithDefault("SHIBBOLETH_LOGIN_URL", $current ?: '/Shibboleth.sso/Login');
$env = setEnvValue('SHIBBOLETH_LOGIN_URL', $shibLoginUrl, $env);
// SHIBBOLETH_LOGOUT_URL
$current = getEnvValue('SHIBBOLETH_LOGOUT_URL', $env);
$shibLogoutUrl = promptWithDefault("SHIBBOLETH_LOGOUT_URL", $current ?: '/Shibboleth.sso/Logout');
$env = setEnvValue('SHIBBOLETH_LOGOUT_URL', $shibLogoutUrl, $env);
// SHIBBOLETH_NAME_VAR
$current = getEnvValue('SHIBBOLETH_NAME_VAR', $env);
$shibNameVar = promptWithDefault("SHIBBOLETH_NAME_VAR", $current ?: 'HTTP_DISPLAYNAME');
$env = setEnvValue('SHIBBOLETH_NAME_VAR', $shibNameVar, $env);
// SHIBBOLETH_EMAIL_VAR
$current = getEnvValue('SHIBBOLETH_EMAIL_VAR', $env);
$shibEmailVar = promptWithDefault("SHIBBOLETH_EMAIL_VAR", $current ?: 'HTTP_MAIL');
$env = setEnvValue('SHIBBOLETH_EMAIL_VAR', $shibEmailVar, $env);
// SHIBBOLETH_EMPLOYEETYPE_VAR
$current = getEnvValue('SHIBBOLETH_EMPLOYEETYPE_VAR', $env);
$shibEmployeeVar = promptWithDefault("SHIBBOLETH_EMPLOYEETYPE_VAR", $current ?: 'HTTP_EMPLOYEETYPE');
$env = setEnvValue('SHIBBOLETH_EMPLOYEETYPE_VAR', $shibEmployeeVar, $env);
saveEnv($env);
echo GREEN . "✓ Shibboleth environment variables updated" . RESET . PHP_EOL;
}
function setupOidcEnv(&$env) {
// OIDC_IDP
$current = getEnvValue('OIDC_IDP', $env);
$oidcIdp = promptWithDefault("OIDC_IDP", $current ?: 'https://oidc.example.com');
$env = setEnvValue('OIDC_IDP', $oidcIdp, $env);
// OIDC_CLIENT_ID
$current = getEnvValue('OIDC_CLIENT_ID', $env);
$oidcClientId = promptWithDefault("OIDC_CLIENT_ID", $current ?: '');
$env = setEnvValue('OIDC_CLIENT_ID', $oidcClientId, $env);
// OIDC_CLIENT_SECRET
$current = getEnvValue('OIDC_CLIENT_SECRET', $env);
$oidcClientSecret = promptWithDefault("OIDC_CLIENT_SECRET", $current ?: '');
$env = setEnvValue('OIDC_CLIENT_SECRET', $oidcClientSecret, $env);
// OIDC_LOGOUT_URI
$current = getEnvValue('OIDC_LOGOUT_URI', $env);
$oidcLogoutUri = promptWithDefault("OIDC_LOGOUT_URI", $current ?: 'https://oidc.example.com/logout');
$env = setEnvValue('OIDC_LOGOUT_URI', $oidcLogoutUri, $env);
// OIDC_SCOPES
$current = getEnvValue('OIDC_SCOPES', $env);
$oidcScopes = promptWithDefault("OIDC_SCOPES", $current ?: 'openid profile email');
$env = setEnvValue('OIDC_SCOPES', $oidcScopes, $env);
// OIDC_FIRSTNAME_VAR
$current = getEnvValue('OIDC_FIRSTNAME_VAR', $env);
$oidcFirstnameVar = promptWithDefault("OIDC_FIRSTNAME_VAR", $current ?: 'given_name');
$env = setEnvValue('OIDC_FIRSTNAME_VAR', $oidcFirstnameVar, $env);
// OIDC_LASTNAME_VAR
$current = getEnvValue('OIDC_LASTNAME_VAR', $env);
$oidcLastnameVar = promptWithDefault("OIDC_LASTNAME_VAR", $current ?: 'family_name');
$env = setEnvValue('OIDC_LASTNAME_VAR', $oidcLastnameVar, $env);
// OIDC_EMAIL_VAR
$current = getEnvValue('OIDC_EMAIL_VAR', $env);
$oidcEmailVar = promptWithDefault("OIDC_EMAIL_VAR", $current ?: 'email');
$env = setEnvValue('OIDC_EMAIL_VAR', $oidcEmailVar, $env);
// OIDC_EMPLOYEETYPE_VAR
$current = getEnvValue('OIDC_EMPLOYEETYPE_VAR', $env);
$oidcEmployeetypeVar = promptWithDefault("OIDC_EMPLOYEETYPE_VAR", $current ?: 'employee_type');
$env = setEnvValue('OIDC_EMPLOYEETYPE_VAR', $oidcEmployeetypeVar, $env);
saveEnv($env);
echo GREEN . "✓ OIDC environment variables updated" . RESET . PHP_EOL;
}
function setupReverbEnv() {
echo BOLD . "Setting up Reverb Environment Variables" . RESET . PHP_EOL;
$env = getEnvContent();
// REVERB_HOST
$current = getEnvValue('REVERB_HOST', $env);
$reverbHost = promptWithDefault("REVERB_HOST", $current ?: '127.0.0.1');
$env = setEnvValue('REVERB_HOST', $reverbHost, $env);
// REVERB_PORT
$current = getEnvValue('REVERB_PORT', $env);
$reverbPort = promptWithDefault("REVERB_PORT", $current ?: '8080');
$env = setEnvValue('REVERB_PORT', $reverbPort, $env);
// REVERB_SERVER_HOST
$current = getEnvValue('REVERB_SERVER_HOST', $env);
$reverbServerHost = promptWithDefault("REVERB_SERVER_HOST", $current ?: '127.0.0.1');
$env = setEnvValue('REVERB_SERVER_HOST', $reverbServerHost, $env);
// REVERB_SERVER_PORT
$current = getEnvValue('REVERB_SERVER_PORT', $env);
$reverbServerPort = promptWithDefault("REVERB_SERVER_PORT", $current ?: '8085');
$env = setEnvValue('REVERB_SERVER_PORT', $reverbServerPort, $env);
// REVERB_SCHEME
$current = getEnvValue('REVERB_SCHEME', $env);
$reverbScheme = promptWithDefault("REVERB_SCHEME (http, https)", $current ?: 'http');
$env = setEnvValue('REVERB_SCHEME', $reverbScheme, $env);
saveEnv($env);
echo GREEN . "✓ Reverb environment variables updated" . RESET . PHP_EOL;
}
function runDev() {
echo BOLD . "Starting development servers..." . RESET . PHP_EOL;
// Create a temporary bash script
$script = "#!/bin/bash
trap 'kill \$(jobs -p) 2>/dev/null' EXIT
# Start services in parallel
php artisan serve &
npm run dev &
php artisan reverb:start &
php artisan queue:work &
php artisan queue:work --queue=mails &
php artisan queue:work --queue=message_broadcast &
# Print process info
echo \"All services started. Press Ctrl+C to stop all.\"
# Wait for user to press Ctrl+C
wait
";
$tmpfile = tempnam(sys_get_temp_dir(), 'hawki_');
file_put_contents($tmpfile, $script);
chmod($tmpfile, 0755);
// Run the script
passthru("bash $tmpfile");
// Clean up the temporary file
unlink($tmpfile);
}
function runBuild() {
echo BOLD . "Building project..." . RESET . PHP_EOL;
// Run composer install
echo YELLOW . "Running composer install..." . RESET . PHP_EOL;
passthru('composer install');
// Check if we need to update composer
echo YELLOW . "Checking for composer updates..." . RESET . PHP_EOL;
exec('composer outdated --direct', $outdatedOutput);
if (!empty($outdatedOutput) && count($outdatedOutput) > 1) { // First line is header
echo "Outdated packages found. Running composer update..." . PHP_EOL;
passthru('composer update');
}
// Run npm build
echo YELLOW . "Running npm run build..." . RESET . PHP_EOL;
passthru('npm run build');
// Clear all caches to ensure build is fresh
clearCache();
echo PHP_EOL . GREEN . BOLD . "Build completed!" . RESET . PHP_EOL;
}
function stopProcesses() {
echo BOLD . "Stopping all HAWKI processes..." . RESET . PHP_EOL;
// Find and kill PHP artisan processes
echo "Finding and stopping PHP artisan processes..." . PHP_EOL;
$script = "
pkill -f 'php artisan serve' 2>/dev/null
pkill -f 'php artisan queue:work' 2>/dev/null
pkill -f 'php artisan reverb:start' 2>/dev/null
";
// Find and kill any Node processes related to Vite
echo "Finding and stopping npm/node processes..." . PHP_EOL;
$script .= "
pkill -f 'vite' 2>/dev/null
";
$tmpfile = tempnam(sys_get_temp_dir(), 'hawki_stop_');
file_put_contents($tmpfile, $script);
chmod($tmpfile, 0755);
// Run the script
passthru("bash $tmpfile");
// Clean up the temporary file
unlink($tmpfile);
echo GREEN . "All HAWKI processes have been stopped." . RESET . PHP_EOL;
}
// Helper functions
function getEnvContent() {
if (!file_exists('.env')) {
echo YELLOW . "Warning: .env file not found. Creating a new one." . RESET . PHP_EOL;
if (file_exists('.env.example')) {
copy('.env.example', '.env');
} else {
touch('.env');
}
}
return file_get_contents('.env');
}
function getEnvValue($key, $envContent) {
$pattern = "/^{$key}=(.*)$/m";
if (preg_match($pattern, $envContent, $matches)) {
return trim($matches[1]);
}
return '';
}
function setEnvValue($key, $value, $envContent) {
$value = trim($value);
$pattern = "/^{$key}=(.*)$/m";
// If the key exists, replace it at its current position
if (preg_match($pattern, $envContent)) {
return preg_replace($pattern, "{$key}={$value}", $envContent);
}
// If the key doesn't exist, add it in the right section if possible
// Split the content into lines
$lines = explode(PHP_EOL, $envContent);
$newLines = [];
$added = false;
// Try to determine which section this key belongs to
$keyPrefix = strtoupper(explode('_', $key)[0]);
$sectionMatches = [];
$currentSection = null;
$lastLine = count($lines) - 1;
// First pass: identify sections and where our key might belong
foreach ($lines as $i => $line) {
// Skip comment lines for section detection, but check if they mark a section
if (preg_match('/^\s*#/', $line)) {
if (strpos(strtoupper($line), $keyPrefix) !== false) {
$sectionMatches[] = $i;
}
continue;
}
// Check if this line starts a variable with our prefix
if (preg_match('/^' . $keyPrefix . '_[A-Z0-9_]+=/', $line)) {
$currentSection = $keyPrefix;
$sectionMatches[] = $i;
}
}
// If we found matching sections, try to add our key after the last match
if (!empty($sectionMatches)) {
$lastMatch = max($sectionMatches);
// Find the end of this section (next empty line or next section start)
for ($i = $lastMatch + 1; $i <= $lastLine; $i++) {
if (!isset($lines[$i])) {
break;
}
$line = $lines[$i];
// If we hit an empty line or a new section, we've found the insertion point
if (trim($line) === '' ||
(preg_match('/^[A-Z0-9_]+=/', $line) && !preg_match('/^' . $keyPrefix . '_[A-Z0-9_]+=/', $line))) {
array_splice($lines, $i, 0, "{$key}={$value}");
$added = true;
break;
}
}
// If we reached the end of the file without finding a break
if (!$added && isset($lines[$lastLine])) {
array_splice($lines, $lastLine + 1, 0, "{$key}={$value}");
$added = true;
}
}
// If we couldn't find an appropriate section or add it within a section, just append to the end
if (!$added) {
$lines[] = "{$key}={$value}";
}
return implode(PHP_EOL, $lines);
}
function saveEnv($content) {
file_put_contents('.env', $content);
}
function promptWithDefault($prompt, $default = '') {
if (!empty($default)) {
echo "$prompt ($default): ";
} else {
echo "$prompt: ";
}
$input = trim(fgets(STDIN));
// Return default if input is empty
return !empty($input) ? $input : $default;
}
/**
* Generate a random alphanumeric string
*/
function generateAlphanumeric($length = 32) {
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[random_int(0, $charactersLength - 1)];
}
return $randomString;
}
/**
* Clear all Laravel caches
*/
function clearCache() {
echo BOLD . "Clearing Laravel caches..." . RESET . PHP_EOL;
// Execute cache clearing commands
echo YELLOW . "Running config:clear..." . RESET . PHP_EOL;
passthru('php artisan config:clear');
echo YELLOW . "Running cache:clear..." . RESET . PHP_EOL;
passthru('php artisan cache:clear');
echo YELLOW . "Running view:clear..." . RESET . PHP_EOL;
passthru('php artisan view:clear');
echo YELLOW . "Running route:clear..." . RESET . PHP_EOL;
passthru('php artisan route:clear');
echo YELLOW . "Running event:clear..." . RESET . PHP_EOL;
passthru('php artisan event:clear');
echo YELLOW . "Running compiled:clear..." . RESET . PHP_EOL;
passthru('php artisan clear-compiled');
echo YELLOW . "Running optimize:clear..." . RESET . PHP_EOL;
passthru('php artisan optimize:clear');
echo GREEN . "✓ All caches have been cleared" . RESET . PHP_EOL;
}
/**
* Run database migrations
*/
function migrate($flags) {
echo BOLD . "Running database migrations..." . RESET . PHP_EOL;
// Check if we need to run a fresh migration
$fresh = in_array('--fresh', $flags);
if ($fresh) {
// Get database information for warning message
$env = getEnvContent();
$dbConnection = getEnvValue('DB_CONNECTION', $env) ?: 'mysql';
$dbHost = getEnvValue('DB_HOST', $env) ?: 'localhost';
$dbPort = getEnvValue('DB_PORT', $env) ?: '3306';
$dbName = getEnvValue('DB_DATABASE', $env) ?: 'hawki';
// Display warning about data loss
echo RED . BOLD . "⚠️ WARNING: You are about to run a fresh migration!" . RESET . PHP_EOL;
echo RED . "This will delete ALL data in your database:" . RESET . PHP_EOL;
echo " - Connection: $dbConnection" . PHP_EOL;
echo " - Database: $dbName" . PHP_EOL;
echo " - Host: $dbHost:$dbPort" . PHP_EOL . PHP_EOL;
echo YELLOW . "Are you sure you want to continue? Type 'yes' to confirm: " . RESET;
$confirmation = trim(fgets(STDIN));
if (strtolower($confirmation) !== 'yes') {
echo YELLOW . "Migration cancelled by user." . RESET . PHP_EOL;
return;
}
echo YELLOW . "Running migrate:fresh..." . RESET . PHP_EOL;
passthru('php artisan migrate:fresh');
} else {
echo YELLOW . "Running migrate..." . RESET . PHP_EOL;
passthru('php artisan migrate');
}
// Run seeders
echo YELLOW . "Running db:seed..." . RESET . PHP_EOL;
passthru('php artisan db:seed');
// Clear cache to ensure fresh database reflection
echo YELLOW . "Clearing cache to refresh database schema..." . RESET . PHP_EOL;
passthru('php artisan cache:clear');
echo GREEN . "✓ Database migration completed!" . RESET . PHP_EOL;
}
/**
* Setup model providers configuration
*/
function setupModelProviders() {
echo BOLD . "Setting up AI Model Providers" . RESET . PHP_EOL;
// Check if model_providers.php exists
if (!file_exists('config/model_providers.php')) {
if (file_exists('config/model_providers.php.example')) {
echo "Creating model_providers.php from example..." . PHP_EOL;
copy('config/model_providers.php.example', 'config/model_providers.php');
echo GREEN . "✓ Created model_providers.php" . RESET . PHP_EOL;