-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginable.php
1025 lines (920 loc) · 42.2 KB
/
pluginable.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
/**
* Our Hestia Control Panel Plugin (HCPP) actions/filter API. This file furnishes a basic
* WordPress-like API for extending/modifying HestiaCP's functionality. This file reads the
* /usr/local/hestia/plugins directory and loads any plugins found there.
*
* @version 1.0.0
* @license GPL-3.0
* @link https://github.com/virtuosoft-dev/hestiacp-pluginable
*
*/
if ( ! class_exists( 'HCPP') ) {
class HCPP {
public $hcpp_filters = [];
public $hcpp_filter_count = 0;
public $logging = false;
public $folder_ports = '/usr/local/hestia/data/hcpp/ports';
public $start_port = 50000;
public $installers = [];
/**
* Allow us to extend the HCPP dynamically.
*/
public function __call($method, $args) {
if (isset($this->$method)) {
$func = $this->$method;
return call_user_func_array($func, $args);
}
}
/**
* Add a plugin action/filter
*
* @param string $tag The name of the action/filter to hook the $function_to_add to.
* @param callable $function_to_add The name of the function to be called.
* @param int $priority Optional, default is 10. Determines the execution priority, lower occurring first.
*/
public function add_action( $tag, $function_to_add, $priority = 10) {
$priority = str_pad($priority, 3, '0', STR_PAD_LEFT);
$idx = $priority . '_' . $tag . '_' . $this->hcpp_filter_count;
$this->hcpp_filter_count++;
$this->hcpp_filters[$tag][$idx] = $function_to_add;
ksort($this->hcpp_filters[$tag]);
return true;
}
/**
* Allocate a unique port number for a service. This function will check for an existing port allocation.
* If one is found, it will be returned. If not, a new port will be allocated and returned. If neither
* user nor domain is specified, the port will be allocated for the system; otherwise, if only user is
* specified, the port will be allocated for the user; if domain is specified, the port will be allocated
* specifically for the domain (user required for domain option).
*
* @param string $name The name of the service to allocate a port for.
* @param string $user The optional username to allocate the port for.
* @param string $domain The optional domain to allocate the port for.
* @return int The port number allocated or zero if an error occurred.
*/
public function allocate_port( $name, $user = '', $domain = '' ) {
// Check for existing port
$port = $this->get_port( $name, $user, $domain );
// Return existing port
if ( $port != 0 ) {
return $port;
}
// Determine which ports file to update
$file = '';
if ( $user == '' && $domain == '' ) { // System port
$file = "$this->folder_ports/system.ports";
}
if ( $user != '' && $domain == '' ) { // User port
$file = "$this->folder_ports/$user/user.ports";
}
if ( $user != '' && $domain != '' ) { // Domain port
$file = "$this->folder_ports/$user/$domain.ports";
}
if ( $file == '' ) {
return 0;
}
// Create the ports folder if it doesn't exist
if ( !is_dir( dirname( $file ) ) ) {
mkdir( dirname( $file ), 0755, true );
}
// Update the ports file with the next available port
$port = $this->find_next_port();
file_put_contents( $file, "set \$$name $port;\n", FILE_APPEND );
if ( strpos( $file, 'system.ports' ) == false ) {
chmod( $file, 0640 );
chown( $file, $user );
chgrp( $file, $user );
}
return $port;
}
/**
* Delete a service port allocation.
*
* @param string $name The name of the service to delete the port allocation for.
* @param string $user The optional username to delete the port for; if blank, the system port will be deleted.
* @param string $domain The optional domain to delete the port for (requires $user); if blank, the user port will be deleted.
*/
public function delete_port( $name, $user = '', $domain = '' ) {
// Exit if ports folder doesn't exist
if ( !is_dir( $this->folder_ports ) ) {
return;
}
// Determine which ports file to update
$file = '';
if ( $user == '' && $domain == '' ) { // System port
$file = "$this->folder_ports/system.ports";
}
if ( $user != '' && $domain == '' ) { // User port
$file = "$this->folder_ports/$user/user.ports";
}
if ( $user != '' && $domain != '' ) { // Domain port
$file = "$this->folder_ports/$user/$domain.ports";
}
if ( $file == '' ) {
return 0;
}
// Check for existing ports file
if ( !file_exists( $file ) ) {
return;
}
// Update the file, removing the port allocation
$new_content = "";
$content = file_get_contents( $file );
$content = explode( "\n", $content );
foreach( $content as $line ) {
if ( strpos( $line, "set \$$name " ) === false ) {
$new_content .= "$line\n";
}
}
$new_content = trim( $new_content );
file_put_contents( $file, $new_content );
}
/**
* Get the port number allocated to a service.
*
* @param string $name The name of the service to get the port for.
* @param string $user The optional username to obtain the port for; if blank, the system port will be returned.
* @param string $domain The optional domain to obtain the port for (requires $user); if blank, the user port will be returned.
* @return int The port number allocated or zero if not found.
*/
public function get_port( $name, $user = '', $domain = '' ) {
// Create ports folder if it doesn't exist
if ( !is_dir( $this->folder_ports ) ) {
mkdir( $this->folder_ports, 0755, true );
return 0;
}
// Determine which ports file to read
$file = '';
if ( $user == '' && $domain == '' ) { // System port
$file = "$this->folder_ports/system.ports";
}
if ( $user != '' && $domain == '' ) { // User port
$file = "$this->folder_ports/$user/user.ports";
}
if ( $user != '' && $domain != '' ) { // Domain port
$file = "$this->folder_ports/$user/$domain.ports";
}
if ( $file == '' ) {
return 0;
}
// Check for existing port
$port = 0;
if ( file_exists( $file ) ) {
$content = file_get_contents( $file );
$content = explode( "\n", $content );
foreach( $content as $line ) {
$parse = explode( ' ', $line );
if ( isset( $parse[1] ) && $parse[1] == "\$$name" ) {
$port = intval( $parse[2] );
break;
}
}
}
return $port;
}
/**
* Find the next unique service port number.
*
* @return int The next available port number.
*/
public function find_next_port() {
// Get list of existing Nginx port files
$files = array();
$iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $this->folder_ports ) );
foreach( $iterator as $file ) {
if ( !$file->isDir() && $file->getExtension() == 'ports' ) {
$files[] = $file->getPathname();
}
}
// Read all port numbers from files
$used_ports = [];
foreach( $files as $file ) {
$content = file_get_contents( $file );
$content = explode( "\n", $content );
// Gather all port numbers
foreach( $content as $line ) {
$parse = explode( ' ', $line );
if ( isset( $parse[2] ) ) {
$used_ports[] = intval( $parse[2] );
}
}
}
// Find first available port from starting port
$port = $this->start_port;
while( in_array( $port, $used_ports ) ) {
$port++;
}
return $port;
}
/**
* Invoke specific plugin action/filter hook.
*
* @param string $tag The name of the action/filter hook.
* @param mixed $arg Optional. Arguments to pass to the functions hooked to the action/filter.
* @return mixed The filtered value after all hooked functions are applied to it.
*/
public function do_action( $tag, $arg = '' ) {
if ($this->logging) {
$this->log( 'do action as ' . trim( shell_exec( 'whoami' ) ) . ', ' . $tag );$this->log( $arg );
}
if ( ! isset( $this->hcpp_filters[$tag] ) ) return $arg;
$args = array();
if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
$args[] =& $arg[0];
else
$args[] = $arg;
for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
$args[] = func_get_arg($a);
foreach ( $this->hcpp_filters[$tag] as $func ) {
try {
$arg = call_user_func_array( $func, $args );
} catch (Exception $e) {
// Echo out the error message if an exception occurs
echo 'Error: do_action failed ' . $e->getMessage();
$this->log( 'Error: do_action failed ' . $e->getMessage() );
}
if ($arg != null) {
$args = array();
if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
$args[] =& $arg[0];
else
$args[] = $arg;
for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
$args[] = func_get_arg($a);
}
}
if (is_array($args) && 1 == count($args)) {
return $args[0];
}else{
return $args;
}
}
/**
* Register a script to be exectued once when the plugin is first present
* in /usr/local/hestia/plugins.
*/
public function register_install_script( $file ) {
// Check that the installed flag file doesn't already exist
$plugin_name = basename( dirname( $file ) );
if ( !file_exists( "/usr/local/hestia/data/hcpp/installed/$plugin_name" ) ) {
// Remember the plugin_name to run its install script
$this->log( "Registering install script for $plugin_name");
$this->installers[] = $file;
}
}
/**
* Register a script to be executed after the plugin folder has been
* from /usr/local/hestia/plugins deleted.
*/
public function register_uninstall_script( $file ) {
// Check if the uninstallers file already exists, if not; copy it over
$plugin_name = basename( dirname( $file ) );
if ( !file_exists( "/usr/local/hestia/data/hcpp/uninstallers/$plugin_name" ) ) {
copy( $file, "/usr/local/hestia/data/hcpp/uninstallers/$plugin_name" );
shell_exec( "chmod 700 /usr/local/hestia/data/hcpp/uninstallers/$plugin_name" );
}
}
/**
* Our object contructor
*/
public function __construct() {
$this->logging = file_exists( '/usr/local/hestia/data/hcpp/logging' );
$this->add_action( 'priv_check_user_password', [ $this, 'run_install_scripts' ] );
$this->add_action( 'priv_check_user_password', [ $this, 'run_uninstall_scripts' ] );
}
/**
* Run install scripts for plugins that have been installed
*/
public function run_install_scripts( $args = null ) {
foreach( $this->installers as $file ) {
$plugin_name = basename( dirname( $file ) );
if ( $this->str_ends_with( $plugin_name, '.disabled' ) ) {
continue;
}
// Mark installed flag file to prevent it from running again
touch ( "/usr/local/hestia/data/hcpp/installed/$plugin_name" );
$this->log( "Running install script for $plugin_name" );
$cmd = 'cd ' . dirname( $file ) . ' && ';
$cmd .= "nohup $file ";
$cmd .= ' > /dev/null 2>&1 &';
$this->log( $cmd );
$this->log( shell_exec( $cmd ) );
$this->do_action( 'hcpp_plugin_installed', $plugin_name );
}
return $args;
}
/**
* Run uninstall scripts for plugins that have been removed
*/
public function run_uninstall_scripts( $args = null ) {
$uninstallers = glob( '/usr/local/hestia/data/hcpp/uninstallers/*' );
foreach( $uninstallers as $file ) {
$plugin_name = pathinfo( $file, PATHINFO_FILENAME );
if ( ! is_dir( "/usr/local/hestia/plugins/$plugin_name" ) &&
! is_dir( "/usr/local/hestia/plugins/$plugin_name.disabled" ) ) {
$this->log( "Running uninstall script for $plugin_name" );
$cmd = "cd /usr/local/hestia/data/hcpp/uninstallers && ";
$cmd .= "$file && ";
$cmd .= "rm -f $file && "; // remove uninstall script when done
$cmd .= "rm -f /usr/local/hestia/data/hcpp/installed/$plugin_name"; // remove installed flag file
$this->log( $cmd );
$this->log( shell_exec( $cmd ) );
$this->do_action( 'hcpp_plugin_uninstalled', $plugin_name );
}
}
return $args;
}
/**
* Run a trusted API command and return JSON if applicable.
*
* @param string $cmd The API command to execute along with it's arguments; the 'v-' prefix is optional.
* @return mixed The output of the command; automatically returns JSON decoded if applicable.
*/
public function run( $cmd ) {
if ( file_exists( '/usr/local/hestia/bin/v-' . strtok( $cmd, " " ) ) ) {
$cmd = '/etc/hestiacp/hooks/bin_actions sudo ' . 'v-' . $cmd;
}
if ( file_exists( '/usr/local/hestia/bin/' . strtok( $cmd, " " ) ) ) {
$cmd = '/etc/hestiacp/hooks/bin_actions sudo ' . $cmd;
}
$output = shell_exec( $cmd );
if ( strpos( $cmd, ' json') !== false ) {
return json_decode( $output, true );
}else{
return $output;
}
}
/**
* Run an arbituary command as the given user.
*
* @param string $user The Linux user account to run the given command as.
* @param string $cmd The command to execute.
* @return string The output of the command.
*/
public function runuser( $user, $cmd ) {
$cmd = $this->do_action( 'hcpp_runuser', $cmd );
$cmd = "runuser -s /bin/bash -l $user -c " . escapeshellarg( 'cd /home/$user && ' . $cmd );
global $hcpp;
$hcpp->log( $cmd );
$cmd = $this->do_action( 'hcpp_runuser_exec', $cmd );
$result = shell_exec( $cmd );
$cmd = $this->do_action( 'hcpp_runuser_result', $cmd );
$hcpp->log( $result );
return $result;
}
/**
* Write a log message to the /tmp/hcpp.log file. Why here? Because
* we can't log UI events to /var/log/hestia/ because open_basedir,
* and we are logging privledged (root) and (admin) process space
* events and they are isolated. /tmp/ is the only safe place to
* write w/out causing runtime issues.
*
* @param mixed $msg The message or object to write to the log.
*/
public function log( $msg ) {
if ( $this->logging == false ) return;
// Make sure log file is writable
$logFile = '/tmp/hcpp.log';
// Write timestamp and message as JSON to log file
$t = (new DateTime('Now'))->format('H:i:s.') . substr( (new DateTime('Now'))->format('u'), 0, 2);
$msg = json_encode( $msg, JSON_PRETTY_PRINT );
$msg = $t . ' ' . $msg;
$msg .= "\n";
// Suppress warnings that can hang the UI (i.e. v-restart-web-backend)
$last_err_reporting = error_reporting();
error_reporting( E_ALL & ~E_WARNING );
try {
chmod( $logFile, 0666 );
file_put_contents( $logFile, $msg, FILE_APPEND );
}catch( Exception $e ) {
echo 'An error occured: ' . $e->getMessage();
}
error_reporting( $last_err_reporting );
}
/**
* patch_file function.
*
* Tests if the given file exists and does not contain the content of replace;
* if missing it performs a search and replace on the file.
*
* @param string $file The file to patch.
* @param string $search The search string.
* @param string $replace The replace string.
* @param boolean $retry_tabs If true, retry with spaces instead of tabs.
* @param boolean $backup If true, backup the file before patching.
*/
public function patch_file( $file, $search, $replace, $retry_tabs = false, $backup = true ) {
if ( $retry_tabs ) {
$search = str_replace( "\t", ' ', $search );
$replace = str_replace( "\t", ' ', $replace );
}
if ( file_exists( $file ) ) {
$content = file_get_contents( $file );
if ( !strstr( $content, $replace ) && strstr( $content, $search ) ) {
// Backup file before patch with timestamp of patch yyyy_mm_dd_hh_mm
if ( !file_exists( $file . '.bak' ) && $backup ) {
copy( $file, $file . '.bak_' . date('Y_m_d_H_i') );
}
$content = str_replace( $search, $replace, $content );
file_put_contents( $file, $content );
$this->log( "Patched $file with $replace");
}else{
if ( strstr( $content, $replace ) ) {
$this->log( "Already patched $file with $replace" );
}
}
// Report patch_file failures, Hestia version may not be compatible
if (!strstr( $content, $replace ) && !strstr( $content, $search ) ) {
if ( $retry_tabs ) {
// Try second time with spaces instead of tabs
$this->patch_file( $file, $search, $replace, true );
}else{
$this->log( "!!! Failed to patch $file with $replace" );
}
}
}else{
// Report patch_file failures, Hestia version may not be compatible
$this->log( "!!! Failed to patch $file not found, with $replace" );
}
}
/**
* Copy a folder recursively, quickly, and retain/restore executable permissions.
*/
public function copy_folder( $src, $dst, $user ) {
// Append / to source and destination if necessary
if (substr($src, -1) != '/') {
$src .= '/';
}
$dst = rtrim( $dst, '/' );
if ( ! is_dir( $dst ) ) {
mkdir( $dst, 0750, true );
chown( $dst, $user );
chgrp( $dst, $user );
}
$cmd = 'cp -RTp ' . $src . ' ' . $dst . ' && chown -R ' . $user . ':' . $user . ' ' . $dst;
shell_exec( $cmd );
$cmd = 'find "' . $dst . '" -type f -perm /111 -exec chmod +x {} \;';
shell_exec( $cmd );
$cmd = 'find "' . $dst . '" -type d -perm /111 -exec chmod +x {} \;';
shell_exec( $cmd );
}
/**
* Re-apply pluginable when hestiacp core updates
*/
public function update_core() {
$detail = $this->run( 'list-sys-info json' );
if ( !is_dir( '/usr/local/hestia/data/hcpp/' ) ) {
mkdir( '/usr/local/hestia/data/hcpp/', 0755, true );
}
$hestia = $detail['sysinfo']['HESTIA'];
if ( !file_exists( '/usr/local/hestia/data/hcpp/last_hestia.txt' ) ) {
file_put_contents( '/usr/local/hestia/data/hcpp/last_hestia.txt', $hestia );
}
$last_hestia = file_get_contents( '/usr/local/hestia/data/hcpp/last_hestia.txt' );
if ( $hestia != $last_hestia ) {
$this->log( 'HestiaCP core updated from ' . $last_hestia . ' to ' . $hestia );
$cmd = 'cd /etc/hestiacp/hooks && /etc/hestiacp/hooks/post_install.sh && service hestia restart';
$cmd = $this->do_action( 'hcpp_update_core_cmd', $cmd );
shell_exec( $cmd );
}
file_put_contents( '/usr/local/hestia/data/hcpp/last_hestia.txt', $hestia );
}
/**
* Perform self update of the pluginable (hooks folder) from the git repo.
*/
public function self_update() {
sleep(mt_rand(1, 30)); // stagger actual update check
$this->log( 'Running self update...' );
$url = 'https://github.com/virtuosoft-dev/hestiacp-pluginable';
$installed_version = shell_exec( 'cd /etc/hestiacp/hooks && git describe --tags --abbrev=0' );
$installed_version = trim( $installed_version );
$latest_version = $this->find_latest_repo_tag( $url );
$this->log( 'Installed version: ' . $installed_version . ', Latest version: ' . $latest_version );
if ( $installed_version != $latest_version && $latest_version != '' ) {
// Do a force reset on the repo to avoid merge conflicts, and obtain found latest version
$cmd = 'cd /etc/hestiacp/hooks && git reset --hard';
$cmd .= ' && git clean -f -d';
$cmd .= ' && git fetch origin tag ' . $latest_version . ' && git checkout tags/' . $latest_version;
$this->log( 'Update HestiaCP-Pluginable from ' . $installed_version . ' to ' . $latest_version);
$this->log( $cmd );
$this->log( shell_exec( $cmd ) );
// Run the post_install.sh script
$cmd = 'cd /etc/hestiacp/hooks && /etc/hestiacp/hooks/post_install.sh';
$this->log( shell_exec( $cmd ) );
}
}
/**
* Update plugins from their given git repo.
*/
public function update_plugins() {
sleep(mt_rand(1, 30)); // stagger actual update check
$this->log( 'Running update plugins...' );
$pluginsDir = '/usr/local/hestia/plugins';
$subfolders = glob( $pluginsDir . '/*', GLOB_ONLYDIR );
foreach ( $subfolders as $subfolder ) {
// Skip disabled plugins
if ( $this->str_ends_with( $subfolder, '.disabled' ) ) {
continue;
}
$pluginFilePath = $subfolder . '/plugin.php';
$pluginGitFolder = $subfolder . '/.git';
if ( file_exists( $pluginFilePath ) && is_dir( $pluginGitFolder ) ) {
$fileLines = file($pluginFilePath);
// Search for the line containing 'Plugin URI:'
$url = '';
foreach ($fileLines as $line) {
if (strpos($line, 'Plugin URI:') !== false) {
$url = trim( $this->delLeftMost( $line, 'Plugin URI:' ) );
break;
}
}
// If the plugin is a git repo with a URL, update it
if ( $url != '' ) {
// Get the installed version number of the plugin
$installed_version = shell_exec( 'cd ' . $subfolder . ' && git describe --tags --abbrev=0' );
$installed_version = trim( $installed_version );
$latest_version = $this->find_latest_repo_tag( $url );
if ( $installed_version != $latest_version && $latest_version != '' ) {
// Do a force reset on the repo to avoid merge conflicts, and obtain found latest version
$cmd = 'cd ' . $subfolder . ' && git reset --hard';
$cmd .= ' && git clean -f -d';
$cmd .= ' && git fetch origin tag ' . $latest_version . ' && git checkout tags/' . $latest_version;
$this->log( 'Update ' . $subfolder . ' from ' . $installed_version . ' to ' . $latest_version);
$this->log( $cmd );
$this->log( shell_exec( $cmd ) );
// Run the update script if it exists
if ( file_exists( $subfolder . '/update' ) ) {
$cmd = 'cd ' . $subfolder . ' && ./update ' . escapeshellarg( $installed_version ) . ' ' . escapeshellarg( $latest_version );
$this->log( $cmd );
$this->log( shell_exec( $cmd ) );
}
}
}
}
}
}
/**
* Find the latest git repo's non-beta release tag.
*
* @param string $url The git repo URL.
* @return string The latest release tag.
*/
public function find_latest_repo_tag( $url ) {
$this->log( 'Finding latest release tag for ' . $url );
// Execute the git ls-remote command
$command = "git ls-remote --tags --sort=\"version:refname\" $url";
$output = explode( PHP_EOL, shell_exec( $command ) );
// Extract version numbers
$versions = [];
foreach ($output as $line) {
// Omit $line if it contains the word beta
if (strpos($line, 'beta') !== false) {
continue;
}
if (preg_match('/refs\/tags\/(v?[0-9]+\.[0-9]+\.[0-9]+)/', $line, $matches)) {
$versions[] = $matches[1];
}
}
// Sort version numbers
usort($versions, 'version_compare');
// Get the most recent version number
$latestRelease = end($versions);
return $latestRelease;
}
// *************************************************************************
// * Conveniently used string parsing and query functions used by this and
// * other plugins. Linear version, lifted from github/steveorevo/GString
// *************************************************************************
/**
* Deletes the right most string from the found search string
* starting from right to left, including the search string itself.
*
* @return string
*/
public function delRightMost( $sSource, $sSearch ) {
for ( $i = strlen( $sSource ); $i >= 0; $i = $i - 1 ) {
$f = strpos( $sSource, $sSearch, $i );
if ( $f !== false ) {
return substr( $sSource, 0, $f );
break;
}
}
return $sSource;
}
/**
* Deletes the left most string from the found search string
* starting from
*
* @return string
*/
public function delLeftMost( $sSource, $sSearch ) {
for ( $i = 0; $i < strlen( $sSource ); $i = $i + 1 ) {
$f = strpos( $sSource, $sSearch, $i );
if ( $f !== false ) {
return substr( $sSource, $f + strlen( $sSearch ), strlen( $sSource ) );
break;
}
}
return $sSource;
}
/**
* Returns the left most string from the found search string
* starting from left to right, excluding the search string itself.
*
* @return string
*/
public function getLeftMost( $sSource, $sSearch ) {
for ( $i = 0; $i < strlen( $sSource ); $i = $i + 1 ) {
$f = strpos( $sSource, $sSearch, $i );
if ( $f !== false ) {
return substr( $sSource, 0, $f );
break;
}
}
return $sSource;
}
/**
* Returns the right most string from the found search string
* starting from right to left, excluding the search string itself.
*
* @return string
*/
public function getRightMost( $sSource, $sSearch ) {
for ( $i = strlen( $sSource ); $i >= 0; $i = $i - 1 ) {
$f = strpos( $sSource, $sSearch, $i );
if ( $f !== false ) {
return substr( $sSource, $f + strlen( $sSearch ), strlen( $sSource ) );
}
}
return $sSource;
}
/**
* PHP 7 compatible poly fills
*/
public function str_ends_with(string $haystack, string $needle) {
$needle_len = strlen($needle);
return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, - $needle_len));
}
function str_starts_with($haystack, $needle) {
return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
}
}
// *************************************************************************
// * Initial HCPP behaviour
// *************************************************************************
global $hcpp;
$hcpp = new HCPP();
// Check/create plugins folder
$plugins_folder = '/usr/local/hestia/plugins';
if ( !is_dir( $plugins_folder ) ) {
mkdir( $plugins_folder );
}
// Load any plugins
$plugins = glob( $plugins_folder . '/*' );
foreach($plugins as $p) {
if ( $hcpp->str_ends_with( $p, '.disabled' ) ) {
continue;
}
$plugin_file = $p . '/plugin.php';
if ( $plugin_file != "/usr/local/hestia/plugins/index.php/plugin.php" ) {
if ( file_exists( $plugin_file ) ) {
require_once( $plugin_file );
}
}
}
// Throw one-time hcpp_new_domain_ready via v-invoke-plugin hook when
// conf folder and public_html folders are first accessible
$hcpp->add_action( 'pre_add_fs_directory', function( $args ) {
global $hcpp;
$user = $args[0];
$domain = $args[1];
if ( $hcpp->getRightMost( $domain, '/' ) != 'public_html' ) return $args;
$domain = $hcpp->delRightMost( $domain, '/' );
$domain = $hcpp->getRightMost( $domain, '/' );
if ( file_exists( "/home/$user/web/$domain/public_html" ) ) return $args;
// Fire off our delay script to await the new domain's folders
$cmd = "nohup /etc/hestiacp/hooks/await_domain.sh ";
$cmd .= escapeshellarg( $user ) . " ";
$cmd .= escapeshellarg( $domain );
$cmd .= ' > /dev/null 2>&1 &';
$hcpp->log( $cmd );
shell_exec( $cmd );
return $args;
});
// Delete the ports file when the domain is deleted
$hcpp->add_action( 'pre_delete_web_domain_backend', function( $args ) {
global $hcpp;
$user = $args[0];
$domain = $args[1];
if ( file_exists( "/usr/local/hestia/data/hcpp/ports/$user/$domain.ports" ) ) {
unlink( "/usr/local/hestia/data/hcpp/ports/$user/$domain.ports" );
}
return $args;
});
// Delete the ports/user folder when user is deleted
$hcpp->add_action( 'priv_delete_user', function( $args ) {
global $hcpp;
$user = $args[0];
if ( file_exists( "/usr/local/hestia/data/hcpp/ports/$user" ) ) {
shell_exec( "rm -rf /usr/local/hestia/data/hcpp/ports/$user" );
}
return $args;
});
// Throw hcpp_rebooted when the system has been started
$hcpp->add_action( 'hcpp_invoke_plugin', function( $args ) {
if ( $args[0] == 'hcpp_rebooted' ) {
global $hcpp;
$hcpp->self_update(); // Update pluginable on reboot
$hcpp->update_core(); // HestiaCP core may have updated before reboot, ensure pluginable is applied
$hcpp->do_action( 'hcpp_rebooted' );
$hcpp->update_plugins(); // Update plugins on reboot
}
return $args;
});
// Throw hcpp_new_domain_ready via v-invoke-plugin hook
$hcpp->add_action( 'hcpp_invoke_plugin', function( $args ) {
if ( $args[0] == 'hcpp_new_domain_ready' ) {
global $hcpp;
array_shift( $args );
$hcpp->do_action( 'hcpp_new_domain_ready', $args );
}
return $args;
});
// Disable/enable/uninstall plugins via trusted command
$hcpp->add_action( 'hcpp_invoke_plugin', function( $args ) {
if ( count( $args ) < 3 ) return $args;
if ( $args[0] != 'hcpp_config' ) return $args;
$v = $args[1];
$plugin = $args[2];
global $hcpp;
switch( $v ) {
case 'yes':
if ( file_exists( "/usr/local/hestia/plugins/$plugin.disabled") ) {
rename( "/usr/local/hestia/plugins/$plugin.disabled", "/usr/local/hestia/plugins/$plugin" );
$hcpp->run( 'invoke-plugin hcpp_plugin_enabled ' . escapeshellarg( $plugin ) );
}
$hcpp->run_install_scripts();
break;
case 'no':
if ( file_exists( "/usr/local/hestia/plugins/$plugin") ) {
$hcpp->do_action( 'hcpp_plugin_disabled', $plugin );
rename( "/usr/local/hestia/plugins/$plugin", "/usr/local/hestia/plugins/$plugin.disabled" );
}
break;
case 'uninstall':
if ( file_exists( "/usr/local/hestia/plugins/$plugin.disabled") && !file_exists( "/usr/local/hestia/plugins/$plugin") ) {
rename( "/usr/local/hestia/plugins/$plugin.disabled", "/usr/local/hestia/plugins/$plugin" );
}
$hcpp->do_action( 'hcpp_plugin_uninstall', $plugin );
if ( file_exists( "/usr/local/hestia/plugins/$plugin") ) {
shell_exec( "rm -rf /usr/local/hestia/plugins/$plugin" );
$hcpp->run_uninstall_scripts();
}
break;
}
return $args;
});
// Get plugin version via trusted command
$hcpp->add_action( 'hcpp_invoke_plugin', function( $args ) {
if ( $args[0] == 'hcpp_get_plugin_version' ) {
$plugin = $args[1];
$version = shell_exec( 'cd "' . $plugin . '" && git describe --tags --abbrev=0' );
echo $version;
}
return $args;
});
// Invoke plugin enabled after plugin is renamed and loaded
$hcpp->add_action( 'hcpp_invoke_plugin', function( $args ) {
if ( $args[0] == 'hcpp_plugin_enabled' ) {
$plugin = $args[1];
global $hcpp;
$hcpp->do_action( 'hcpp_plugin_enabled', $plugin );
}
return $args;
});
// List plugins in HestiaCP's Configure Server UI
$hcpp->add_action( 'hcpp_render_body', function( $args ) {
global $hcpp;
$content = $args['content'];
if ( false == ($args['page'] == 'edit_server' && $args['TAB'] == 'SERVER' ) ) {
return $args;
}
// Process any submissions
foreach( $_REQUEST as $k => $v ) {
if ( $hcpp->str_starts_with( $k, 'hcpp_' ) ) {
$plugin = substr( $k, 5 );
$hcpp->run( 'invoke-plugin hcpp_config ' . escapeshellarg( $v ) . ' ' . escapeshellarg( $plugin ) );
}
}
// Parse the page content
$before = $hcpp->delRightMost( $content, 'name="v_firewall"' ) . 'name="v_firewall"';
$after = $hcpp->getRightMost( $content, 'name="v_firewall"' );
// Parse the page content under HestiaCP 1.6.X
$before .= $hcpp->getLeftMost( $after, '</div>' ) . '</div>';
$after = $hcpp->delLeftMost( $after, '</div>' );
// Create a block to list our plugins
$block = '<div class="u-mb10">
<label for="hcpp_%name%" class="form-label">
%label% <span style="font-size:smaller;font-weight:lighter;">(%version%)</span>
</label>
<select class="form-select" name="hcpp_%name%" id="hcpp_%name%">
<option value="no">No</option>
<option value="yes">Yes</option>
<option value="uninstall">Uninstall</option>
</select>
</div>';
// List the plugins
$plugins = glob( '/usr/local/hestia/plugins/*' );
$insert = '';
foreach($plugins as $p) {
// Extract name from plugin.php header or default to folder name
if ( ! file_exists( $p . '/plugin.php' ) ) continue;
$label = file_get_contents( $p . '/plugin.php' );
$name = basename( $p, '.disabled' );
if ( strpos( $label, 'Plugin Name: ') !== false ) {
$label = $hcpp->delLeftMost( $label, 'Plugin Name: ' );
$label = trim( $hcpp->getLeftMost( $label, "\n") );
}else{
$label = $name;
}
// Extract version if git repo
$version = '';
if ( file_exists( $p . '/.git' ) ) {
$version = trim( $hcpp->run( 'invoke-plugin hcpp_get_plugin_version ' . escapeshellarg( $p ) ) );
$version = trim( $version );
}
if ( is_dir( $p ) && ($p[0] != '.') ) {
if ( file_exists( $p . '/plugin.php' ) ) {
$item = str_replace( array( '%label%', '%name%', '%version%' ), array( $label, $name, $version ), $block );
if ( strpos( $p, '.disabled') === false) {
$item = str_replace( 'value="yes"', 'value="yes" selected=true', $item );
}else{
$item = str_replace( 'value="no"', 'value="no" selected=true', $item );
}
$insert .= $item;
}
}
}
// Inject HestiaCP-Pluginable version
$version = trim( $hcpp->run( 'invoke-plugin hcpp_get_plugin_version "/etc/hestiacp/hooks"' ) );
$version = trim( $version );
$insert .= '<div class="u-mb10"><small style="float:right;">HestiaCP-Pluginable (' . $version . ')</small></div>';
$content = $before . $insert . $after;
$args['content'] = $content;
return $args;
});
// Check for updates to plugins daily
$hcpp->add_action( 'update_sys_queue', function( $args ) {
if (is_array($args) && count($args) === 1 && $args[0] === 'daily') {
global $hcpp;
$hcpp->self_update();
$hcpp->update_core();
$hcpp->update_plugins();
}
return $args;
});
// Frequently check for updates in logging mode
if ( $hcpp->logging ) {
$hcpp->add_action( 'priv_update_sys_rrd', function( $args ) { // every 5 minutes
//$hcpp->add_action( 'priv_update_sys_queue', function( $args ) { // every 2 minutes
global $hcpp;
$hcpp->self_update();
$hcpp->update_core();
$hcpp->update_plugins();
return $args;
});
}
// Ensure we reload nginx to read our plugin configurations (req. by NodeApp, Collabora, etc.)
$hcpp->add_action( 'post_restart_service', function( $args ) {
global $hcpp;
$cmd = '(sleep 5 && service nginx reload) > /dev/null 2>&1 &';
$cmd = $hcpp->do_action( 'hcpp_nginx_reload', $cmd );
shell_exec( $cmd );
}, 50 );
// Restore jquery
$hcpp->add_action( 'hcpp_render_header', function( $args ) {