-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathphp-lib.pl
executable file
·3370 lines (3182 loc) · 95.9 KB
/
php-lib.pl
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
# Functions for PHP configuration
# get_domain_php_mode(&domain)
# Returns 'mod_php' if PHP is run via Apache's mod_php, 'cgi' if run via
# a CGI script, 'fcgid' if run via fastCGI. This is detected by looking for the
# Action lines in httpd.conf.
sub get_domain_php_mode
{
local ($d) = @_;
local $p = &domain_has_website($d);
if ($p && $p ne 'web') {
return &plugin_call($p, "feature_get_web_php_mode", $d);
}
elsif (!$p) {
return "Virtual server does not have a website";
}
&require_apache();
local ($virt, $vconf, $conf) = &get_apache_virtual($d->{'dom'},
$d->{'web_port'});
if ($virt) {
# First check for FPM socket, using a single ProxyPassMatch
local @ppm = &apache::find_directive("ProxyPassMatch", $vconf);
foreach my $ppm (@ppm) {
if ($ppm =~ /unix:([^\|]+)/ ||
$ppm =~ /fcgi:\/\/(localhost|127\.0\.0\.1):\d+/) {
return 'fpm';
}
}
# Also check for FPM socket in a FilesMatch block
foreach my $f (&apache::find_directive_struct("FilesMatch", $vconf)) {
next if ($f->{'words'}->[0] ne '\.php$');
foreach my $h (&apache::find_directive("SetHandler", $f->{'members'})) {
if ($h =~ /proxy:fcgi:\/\/(localhost|127\.0\.0\.1)/ ||
$h =~ /proxy:unix:/) {
return 'fpm';
}
}
}
# Look for an action, possibly in a directory, that runs the FCGI
# wrapper for PHP scripts
local @actions = &apache::find_directive("Action", $vconf);
local $pdir = &public_html_dir($d);
local ($dir) = grep { $_->{'words'}->[0] eq $pdir ||
$_->{'words'}->[0] eq $pdir."/" ||
&path_glob_match($_->{'words'}->[0], $pdir) }
&apache::find_directive_struct("Directory", $vconf);
if ($dir) {
push(@actions, &apache::find_directive("Action",
$dir->{'members'}));
foreach my $f (&apache::find_directive("FCGIWrapper",
$dir->{'members'})) {
if ($f =~ /^\Q$d->{'home'}\E\/fcgi-bin\/php\S+\.fcgi/) {
return 'fcgid';
}
}
}
# Look for an action that runs PHP via the CGI wrapper
foreach my $a (@actions) {
if ($a =~ /^application\/x-httpd-php[0-9\.]+\s+\/cgi-bin\/php\S+\.cgi/) {
return 'cgi';
}
}
# Look for a mapping from PHP scripts to plain text for 'none' mode
if ($dir) {
local @types = &apache::find_directive(
"AddType", $dir->{'members'});
foreach my $t (@types) {
if ($t =~ /text\/plain\s+\.php/) {
return 'none';
}
}
}
}
# Fall back to mod_php, if enabled
if (&get_apache_mod_php_version()) {
return 'mod_php';
}
return 'none';
}
# save_domain_php_mode(&domain, mode, [port], [new-domain])
# Changes the method a virtual web server uses to run PHP. Returns undef on
# success or an error message on failure.
sub save_domain_php_mode
{
local ($d, $mode, $port, $newdom) = @_;
local $p = &domain_has_website($d);
$p || return "Virtual server does not have a website";
local $tmpl = &get_template($d->{'template'});
local $oldmode = &get_domain_php_mode($d);
local @vers = sort { &compare_version_numbers($a->[0], $b->[0]) }
&list_available_php_versions($d, $mode);
# Work out the default PHP version for FPM
if ($mode eq "fpm") {
local @fpms = grep { !$_->{'err'} } &list_php_fpm_configs();
@fpms || return "No FPM versions found!";
my $curr = &get_php_fpm_config($d->{'php_fpm_version'});
if (!$curr) {
# Current version isn't actually valid! Fall back to default
delete($d->{'php_fpm_version'});
}
if (!$d->{'php_fpm_version'}) {
# Work out the default FPM version from the template
if (@vers) {
# Use version from template, or the max version
my $fpm;
if ($tmpl->{'web_phpver'}) {
($fpm) = grep { $_->[0] eq
$tmpl->{'web_phpver'} } @vers;
}
$fpm ||= $vers[$#vers];
$d->{'php_fpm_version'} = $fpm->[0];
}
else {
# This should never happen?
my $defconf = $tmpl->{'web_phpver'} ?
&get_php_fpm_config($tmpl->{'web_phpver'}) : undef;
$defconf ||= $fpms[0];
$d->{'php_fpm_version'} = $defconf->{'shortversion'};
}
}
}
if ($mode =~ /mod_php|none/ && $oldmode !~ /mod_php|none/) {
# Save the PHP version for later recovery
local $oldver = &get_domain_php_version($d, $oldmode);
$d->{'last_php_version'} = $oldver;
}
my $oldplog;
my $oldmail;
if ($mode ne $oldmode) {
# Save the PHP error log path and mail mode
$oldplog = &get_domain_php_error_log($d);
$oldmail = &get_php_can_send_mail($d);
}
# Work out source php.ini files
local (%srcini, %subs_ini);
$mode eq "none" || @vers || return "No PHP versions found for mode $mode";
foreach my $ver (@vers) {
$subs_ini{$ver->[0]} = 0;
local $srcini = $tmpl->{'web_php_ini_'.$ver->[0]};
if (!$srcini || $srcini eq "none" || !-r $srcini) {
$srcini = &get_global_php_ini($ver->[0], $mode);
}
else {
$subs_ini{$ver->[0]} = 1;
}
$srcini{$ver->[0]} = $srcini;
}
local @srcinis = &unique(values %srcini);
# Copy php.ini file into etc directory, for later per-site modification
local $etc = "$d->{'home'}/etc";
if (!-d $etc) {
&make_dir_as_domain_user($d, $etc, 0755);
}
foreach my $ver (@vers) {
# Create separate .ini file for each PHP version, if missing
local $subs_ini = $subs_ini{$ver->[0]};
local $srcini = $srcini{$ver->[0]};
local $inidir = "$etc/php$ver->[0]";
if ($srcini && !-r "$inidir/php.ini") {
# Copy file, set permissions, fix session.save_path, and
# clear out extension_dir (because it can differ between
# PHP versions)
if (!-d $inidir) {
&make_dir_as_domain_user($d, $inidir, 0755);
}
if (-r "$etc/php.ini" && !-l "$etc/php.ini") {
# We are converting from the old style of a single
# php.ini file to the new multi-version one .. just
# copy the existing file for all versions, which is
# assumed to be working
©_source_dest_as_domain_user(
$d, "$etc/php.ini", "$inidir/php.ini");
}
elsif ($subs_ini) {
# Perform substitions on config file
local $inidata = &read_file_contents($srcini);
$inidata || return "Failed to read $srcini, ".
"or file is empty";
$inidata = &substitute_virtualmin_template($inidata,$d);
&open_tempfile_as_domain_user(
$d, INIDATA, ">$inidir/php.ini");
&print_tempfile(INIDATA, $inidata);
&close_tempfile_as_domain_user($d, INIDATA);
}
else {
# Just copy verbatim
local ($ok, $err) = ©_source_dest_as_domain_user(
$d, $srcini, "$inidir/php.ini");
$ok || return "Failed to copy $srcini to ".
"$inidir/php.ini : $err";
}
# Clear any caching on file
&unflush_file_lines("$inidir/php.ini");
undef($phpini::get_config_cache{"$inidir/php.ini"});
local ($uid, $gid) = (0, 0);
if (!$tmpl->{'web_php_noedit'}) {
($uid, $gid) = ($d->{'uid'}, $d->{'ugid'});
}
if (&foreign_check("phpini") && -r "$inidir/php.ini") {
# Fix up session save path, extension_dir and
# gc_probability / gc_divisor
&foreign_require("phpini");
local $pconf = &phpini::get_config("$inidir/php.ini");
local $tmp = &create_server_tmp($d);
&phpini::save_directive($pconf, "session.save_path",
$tmp);
&phpini::save_directive($pconf, "upload_tmp_dir", $tmp);
if (scalar(@srcinis) == 1 && scalar(@vers) > 1) {
# Only if the same source is used for multiple
# PHP versions.
&phpini::save_directive($pconf, "extension_dir",
undef);
}
# On some systems, these are not set and so sessions are
# never cleaned up.
local $prob = &phpini::find_value(
"session.gc_probability", $pconf);
local $div = &phpini::find_value(
"session.gc_divisor", $pconf);
&phpini::save_directive($pconf,
"session.gc_probability", 1) if (!$prob);
&phpini::save_directive($pconf,
"session.gc_divisor", 100) if (!$div);
# Set timezone to match system
local $tz;
if (&foreign_check("time")) {
&foreign_require("time");
if (&time::has_timezone()) {
$tz = &time::get_current_timezone();
}
}
if ($tz) {
&phpini::save_directive($pconf,
"date.timezone", $tz);
}
&flush_file_lines("$inidir/php.ini");
}
&set_ownership_permissions($uid, $gid, 0755, "$inidir/php.ini");
}
}
# Call plugin-specific function to perform webserver setup
if ($p ne 'web') {
my $err = &plugin_call($p, "feature_save_web_php_mode",
$d, $mode, $port, $newdom);
if (!$err) {
$d->{'php_mode'} = $mode;
&sync_alias_domain_php_mode($d);
}
return $err;
}
&require_apache();
# Create wrapper scripts
if (&need_php_wrappers($d, $mode)) {
&create_php_wrappers($d, $mode);
}
# Setup PHP-FPM pool
if ($mode eq "fpm" && $oldmode ne "fpm") {
&create_php_fpm_pool($d);
}
elsif ($mode ne "fpm") {
&delete_php_fpm_pool($d);
}
# Add the appropriate directives to the Apache config
local $conf = &apache::get_config();
local @ports = ( $d->{'web_port'},
$d->{'ssl'} ? ( $d->{'web_sslport'} ) : ( ) );
@ports = ( $port ) if ($port); # Overridden to just do SSL or non-SSL
local $fdest = "$d->{'home'}/fcgi-bin";
local $pfound = 0;
foreach my $p (@ports) {
local ($virt, $vconf) = &get_apache_virtual($d->{'dom'}, $p);
next if (!$vconf);
$pfound++;
# Find <directory> sections containing PHP directives.
# If none exist, add them in either the directory for
# public_html, or the <virtualhost> if it already has them
local @phpconfs;
local @dirstrs = &apache::find_directive_struct("Directory",
$vconf);
foreach my $dirstr (@dirstrs) {
local @wrappers = &apache::find_directive("FCGIWrapper",
$dirstr->{'members'});
local @actions =
grep { $_ =~ /^application\/x-httpd-php/ }
&apache::find_directive("Action",
$dirstr->{'members'});
if (@wrappers || @actions) {
push(@phpconfs, $dirstr);
}
}
if (!@phpconfs) {
# No directory has them yet. Add to the <virtualhost> if it
# already directives for cgi, the <directory> otherwise.
# Unless we are using fcgid, in which case it must always be
# added to the directory.
local @pactions =
grep { $_ =~ /^application\/x-httpd-php\d+/ }
&apache::find_directive("Action", $vconf);
local $pdir = &public_html_dir($d);
local ($dirstr) = grep { $_->{'words'}->[0] eq $pdir ||
$_->{'words'}->[0] eq $pdir."/" }
&apache::find_directive_struct("Directory", $vconf);
if ($mode eq "fcgid") {
$dirstr || return "No <Directory> section ".
"found for mod_fcgid directives";
push(@phpconfs, $dirstr);
}
elsif ($dirstr && !@pactions) {
push(@phpconfs, $dirstr);
}
else {
push(@phpconfs, $virt);
}
}
# Work out which PHP version each directory uses currently
local %pdirs;
if (!$newdom) {
%pdirs = map { $_->{'dir'}, $_->{'version'} }
&list_domain_php_directories($d);
}
# Update all of the directories
local @avail = map { $_->[0] }
&list_available_php_versions($d, $mode);
local %allvers = map { $_, 1 } @all_possible_php_versions;
foreach my $phpstr (@phpconfs) {
# Remove all Action and AddType directives for suexec PHP
local $phpconf = $phpstr->{'members'};
local @actions = &apache::find_directive("Action", $phpconf);
@actions = grep { !/^application\/x-httpd-php\d+/ }
@actions;
local @types = &apache::find_directive("AddType", $phpconf);
@types = grep { !/^application\/x-httpd-php\d+/ &&
!/\.php[0-9\.]*$/ } @types;
# Remove all AddHandler and FCGIWrapper directives for fcgid
local @handlers = &apache::find_directive("AddHandler",
$phpconf);
@handlers = grep { !(/^fcgid-script\s+\.php(.*)$/ &&
($1 eq '' || $allvers{$1})) } @handlers;
local @wrappers = &apache::find_directive("FCGIWrapper",
$phpconf);
@wrappers = grep {
!(/^\Q$fdest\E\/php[0-9\.]+\.fcgi\s+\.php(.*)$/ &&
($1 eq '' || $allvers{$1})) } @wrappers;
# Add needed Apache directives. Don't add the AddHandler,
# Alias and Directory if already there.
local $ver = $pdirs{$phpstr->{'words'}->[0]} ||
$tmpl->{'web_phpver'} ||
$avail[$#avail];
$ver = $avail[$#avail] if (&indexof($ver, @avail) < 0);
if ($mode eq "cgi") {
foreach my $v (@avail) {
push(@actions, "application/x-httpd-php$v ".
"/cgi-bin/php$v.cgi");
}
}
elsif ($mode eq "fcgid") {
push(@handlers, "fcgid-script .php");
foreach my $v (@avail) {
push(@handlers, "fcgid-script .php$v");
}
push(@wrappers, "$fdest/php$ver.fcgi .php");
foreach my $v (@avail) {
push(@wrappers, "$fdest/php$v.fcgi .php$v");
}
}
elsif ($mode eq "none") {
foreach my $v (@avail) {
push(@types, "text/plain .php$v");
}
push(@types, "text/plain .php");
}
if ($mode eq "cgi" || $mode eq "mod_php") {
foreach my $v (@avail) {
push(@types,"application/x-httpd-php$v .php$v");
}
}
if ($mode eq "cgi") {
push(@types, "application/x-httpd-php$ver .php");
}
elsif ($mode eq "mod_php") {
push(@types, "application/x-httpd-php .php");
}
@types = &unique(@types);
@actions = &unique(@actions);
&apache::save_directive("Action", \@actions, $phpconf, $conf);
&apache::save_directive("AddType", \@types, $phpconf, $conf);
&apache::save_directive("AddHandler", \@handlers,
$phpconf, $conf);
&apache::save_directive("FCGIWrapper", \@wrappers,
$phpconf, $conf);
# For fcgid mode, the directory needs to have Options ExecCGI
local ($opts) = &apache::find_directive("Options", $phpconf);
if ($opts && $mode eq "fcgid" && $opts !~ /ExecCGI/) {
$opts .= " +ExecCGI";
&apache::save_directive("Options", [ $opts ],
$phpconf, $conf);
}
}
# For FPM mode, we need a proxy directive at the top level
local $fsock = &get_php_fpm_socket_file($d, 1);
local $fport = $d->{'php_fpm_port'};
local $fmode = $fport ? 'port' :
-r $fsock ? 'socket' :
$tmpl->{'php_sock'} ? 'socket' : 'port';
local @ppm = &apache::find_directive("ProxyPassMatch", $vconf);
local @oldppm = grep { /unix:([^\|]+)/ || /fcgi:\/\/(localhost|127\.0\.0\.1):\d+/ } @ppm;
if ($fsock) {
@ppm = grep { !/unix:([^\|]+)/ } @ppm;
}
if ($fport) {
@ppm = grep { !/fcgi:\/\/(localhost|127\.0\.0\.1):\d+/ } @ppm;
}
local $files;
foreach my $f (&apache::find_directive_struct("FilesMatch", $vconf)) {
$files = $f if ($f->{'words'}->[0] eq '\.php$');
}
if ($mode eq "fpm" && ($apache::httpd_modules{'core'} < 2.4 || @oldppm)) {
# Use a proxy directive for older Apache or if this is what's
# already in use
local $phd = $phpconfs[0]->{'words'}->[0];
if ($fmode eq 'socket') {
# Use socket file
push(@ppm, "^/(.*\.php(/.*)?)\$ unix:${fsock}|fcgi://127.0.0.1${phd}/\$1");
}
else {
# Allocate and use a port number
$fport = &get_php_fpm_socket_port($d);
push(@ppm, "^/(.*\.php(/.*)?)\$ fcgi://127.0.0.1:${fport}${phd}/\$1");
}
}
elsif ($mode eq "fpm" && $apache::httpd_modules{'core'} >= 2.4) {
# Can use a FilesMatch block with SetHandler inside instead
my $wanth;
if ($fmode eq 'socket') {
# Use a socket file
$wanth = 'proxy:unix:'.$fsock."|fcgi://127.0.0.1";
}
else {
# Allocate, save and use a TCP port
my $fport = &get_php_fpm_socket_port($d);
$wanth = 'proxy:fcgi://127.0.0.1:'.$fport;
}
if (!$files) {
# Add a new FilesMatch block with the socket
$files = { 'name' => 'FilesMatch',
'type' => 1,
'value' => '\.php$',
'words' => ['\.php$'],
'members' => [
{ 'name' => 'SetHandler',
'value' => $wanth,
},
],
};
&apache::save_directive_struct(
undef, $files, $vconf, $conf);
}
else {
# Add the SetHandler directive to the FilesMatch block,
# and remove the AddType
&apache::save_directive("SetHandler", [$wanth],
$files->{'members'}, $conf);
&apache::save_directive("AddType", [],
$files->{'members'}, $conf);
}
}
elsif ($mode eq "none") {
# When PHP is disabled, a FilesMatch block is needed to make
# sure any global PHP configs are overridden
if ($files) {
&apache::save_directive_struct(
$files, undef, $vconf, $conf);
}
$files = { 'name' => 'FilesMatch',
'type' => 1,
'value' => '\.php$',
'words' => ['\.php$'],
'members' => [
{ 'name' => 'SetHandler',
'value' => 'None',
},
{ 'name' => 'AddType',
'value' => 'text/plain .php',
},
],
};
&apache::save_directive_struct(
undef, $files, $vconf, $conf);
}
elsif ($mode eq "cgi") {
# When PHP is disabled, a FilesMatch block is needed to override
# any global config that would add a handler for .php files
if ($files) {
&apache::save_directive_struct(
$files, undef, $vconf, $conf);
}
$files = { 'name' => 'FilesMatch',
'type' => 1,
'value' => '\.php$',
'words' => ['\.php$'],
'members' => [
{ 'name' => 'SetHandler',
'value' => 'None',
},
],
};
&apache::save_directive_struct(
undef, $files, $vconf, $conf);
}
else {
# For non-FPM mode, remove the whole files block,
# and forget about any ports or sockets
if ($files) {
&apache::save_directive_struct(
$files, undef, $vconf, $conf);
}
@ppm = grep { !/unix:|fcgi:/ } @ppm;
delete($d->{'php_fpm_port'});
}
&apache::save_directive("ProxyPassMatch", \@ppm, $vconf, $conf);
# For non-mod_php mode, we need a RemoveHandler .php directive at
# the <virtualhost> level to supress mod_php which may still be active
local @remove = &apache::find_directive("RemoveHandler", $vconf);
@remove = grep { !(/^\.php(.*)$/ && ($1 eq '' || $allvers{$1})) }
@remove;
if ($mode ne "mod_php") {
push(@remove, ".php");
foreach my $v (@avail) {
push(@remove, ".php$v");
}
}
@remove = &unique(@remove);
&apache::save_directive("RemoveHandler", \@remove, $vconf, $conf);
# For non-mod_php mode, use php_admin_value to turn off mod_php in
# case it gets enabled in a .htaccess file
if (&get_apache_mod_php_version()) {
local @admin = &apache::find_directive("php_admin_value",
$vconf);
@admin = grep { !/^engine\s+/ } @admin;
if ($mode ne "mod_php") {
push(@admin, "engine Off");
}
&apache::save_directive("php_admin_value", \@admin,
$vconf, $conf);
}
# For fcgid mode, set IPCCommTimeout to either the configured value
# or the PHP max execution time + 1, so that scripts run via fastCGI
# aren't disconnected
if ($mode eq "fcgid") {
local $maxex;
if ($config{'fcgid_max'} eq "*") {
# Don't set
$maxex = undef;
}
elsif ($config{'fcgid_max'} eq "") {
# From PHP config
local $inifile = &get_domain_php_ini($d, $ver);
if (-r $inifile) {
&foreign_require("phpini");
local $iniconf = &phpini::get_config($inifile);
$maxex = &phpini::find_value(
"max_execution_time", $iniconf);
}
}
else {
# Fixed number
$maxex = int($config{'fcgid_max'})-1;
}
if (defined($maxex)) {
&set_fcgid_max_execution_time($d, $maxex, $mode, $p);
}
}
else {
# For other modes, don't set
&apache::save_directive("IPCCommTimeout", [ ],
$vconf, $conf);
}
# For fcgid mode, set max request size to 1GB, which is the default
# in older versions of mod_fcgid but is smaller in versions 2.3.6 and
# later.
local $setmax;
if ($mode eq "fcgid") {
if ($gconfig{'os_type'} eq 'debian-linux' &&
$gconfig{'os_version'} >= 6) {
# Debian 6 and Ubuntu 10 definately use mod_fcgid 2.3.6+
$setmax = 1;
}
elsif ($gconfig{'os_type'} eq 'redhat-linux' &&
$gconfig{'os_version'} >= 14 &&
&foreign_check("software")) {
# CentOS 6 and Fedora 14+ may have it..
&foreign_require("software");
local @pinfo = &software::package_info("mod_fcgid");
if (&compare_versions($pinfo[4], "2.3.6") >= 0) {
$setmax = 1;
}
}
}
&apache::save_directive("FcgidMaxRequestLen",
$setmax ? [ 1024*1024*1024 ] : [ ],
$vconf, $conf);
&flush_file_lines();
}
# Update PHP mode cache
$d->{'php_mode'} = $mode;
&sync_alias_domain_php_mode($d);
local @vlist = map { $_->[0] } &list_available_php_versions($d);
if ($mode !~ /mod_php|none/ && $oldmode =~ /mod_php|none/ &&
$d->{'last_php_version'} &&
&indexof($d->{'last_php_version'}, @vlist) >= 0) {
# Restore PHP version from before mod_php or none modes
my $err = &save_domain_php_directory($d, &public_html_dir($d),
$d->{'last_php_version'}, 1);
return $err if ($err);
}
# Update PHP version cache
if ($mode eq "cgi" || $mode eq "fcgid") {
$d->{'php_version'} = &get_domain_php_version($d, $mode);
}
else {
delete($d->{'php_version'});
}
if (defined($oldplog)) {
# Restore the old PHP error log
&save_domain_php_error_log($d, $oldplog);
}
if (defined($oldmail)) {
# Restore the old mail option
&save_php_can_send_mail($d, $oldmail);
}
# Link ~/etc/php.ini to the per-version ini file
&create_php_ini_link($d, $mode);
&create_php_bin_links($d, $mode);
®ister_post_action(\&restart_apache);
$pfound || return "Apache virtual host was not found";
return undef;
}
# sync_alias_domain_php_mode(&domain)
# If a domain has an aliases, update their PHP modes to match this one
sub sync_alias_domain_php_mode
{
my ($d) = @_;
foreach my $ad (&get_domain_by("alias", $d->{'id'})) {
&lock_domain($ad);
$ad->{'php_mode'} = $d->{'php_mode'};
&save_domain($ad);
&unlock_domain($ad);
}
}
# set_fcgid_max_execution_time(&domain, value, [mode], [port])
# Set the IPCCommTimeout directive to follow the given PHP max execution time
sub set_fcgid_max_execution_time
{
local ($d, $max, $mode, $port) = @_;
$mode ||= &get_domain_php_mode($d);
return 0 if ($mode ne "fcgid");
local $p = &domain_has_website($d);
if ($p && $p ne 'web') {
return &plugin_call($p, "feature_set_fcgid_max_execution_time",
$d, $max, $mode, $port);
}
elsif (!$p) {
return "Virtual server does not have a website";
}
local @ports = ( $d->{'web_port'},
$d->{'ssl'} ? ( $d->{'web_sslport'} ) : ( ) );
@ports = ( $port ) if ($port); # Overridden to just do SSL or non-SSL
local $conf = &apache::get_config();
local $pfound = 0;
local $changed = 0;
foreach my $p (@ports) {
local ($virt, $vconf) = &get_apache_virtual($d->{'dom'}, $p);
next if (!$vconf);
$pfound++;
local @newdir = &apache::find_directive("FcgidIOTimeout", $vconf);
local $dirname = @newdir ? "FcgidIOTimeout" : "IPCCommTimeout";
local $oldvalue = &apache::find_directive($dirname, $vconf);
local $want = $max ? $max + 1 : $max_php_fcgid_timeout;
if ($oldvalue ne $want) {
&apache::save_directive($dirname, [ $want ],
$vconf, $conf);
&flush_file_lines($virt->{'file'});
$changed++;
}
}
$pfound || &error("Apache virtual host was not found");
if ($changed) {
®ister_post_action(\&restart_apache);
}
}
# get_fcgid_max_execution_time(&domain)
# Returns the current max FCGId execution time, or undef for unlimited
sub get_fcgid_max_execution_time
{
local $p = &domain_has_website($d);
if ($p && $p ne 'web') {
return &plugin_call($p, "feature_get_fcgid_max_execution_time", $d);
}
elsif (!$p) {
return "Virtual server does not have a website";
}
local ($virt, $vconf) = &get_apache_virtual($d->{'dom'}, $d->{'web_port'});
local $v = &apache::find_directive("IPCCommTimeout", $vconf);
$v ||= &apache::find_directive("FcgidIOTimeout", $vconf);
return $v == 9999 ? undef : $v ? $v-1 : 40;
}
# set_php_max_execution_time(&domain, max)
# Updates the max execution time in all php.ini files, and possibly in the FPM
# config file
sub set_php_max_execution_time
{
my ($d, $max) = @_;
&foreign_require("phpini");
my @errs;
foreach my $ini (&list_domain_php_inis($d)) {
eval {
local $main::error_must_die = 1;
local $f = $ini->[1];
local $conf = &phpini::get_config($f);
&phpini::save_directive($conf, "max_execution_time", $max);
&flush_file_lines($f);
};
push(@errs, $@) if ($@);
}
my $mode = &get_domain_php_mode($d);
if ($mode eq "fpm") {
&save_php_fpm_ini_value($d, "max_execution_time",
$max == 0 ? undef : $max);
}
return @errs ? join(" ", @errs) : undef;
}
# get_php_max_execution_time(&domain)
# Returns the max execution time from a php.ini file
sub get_php_max_execution_time
{
local ($d, $max) = @_;
&foreign_require("phpini");
foreach my $ini (&list_domain_php_inis($d)) {
local $f = $ini->[1];
local $conf = &phpini::get_config($f);
local $max = &phpini::find_value("max_execution_time", $conf);
return $max if ($max ne '');
}
return undef;
}
# need_php_wrappers(&domain, [&mode])
# Returns 1 if this PHP mode needs CGI wrappers
sub need_php_wrappers
{
local ($d, $mode) = @_;
$mode ||= &get_domain_php_mode($d);
return $mode ne "mod_php" && $mode ne "fpm" && $mode ne "none";
}
# create_php_wrappers(&domain, [phpmode])
# Creates all phpN.cgi wrappers for some domain
sub create_php_wrappers
{
local ($d, $mode) = @_;
$mode ||= &get_domain_php_mode($d);
local $dest = $mode eq "fcgid" ? "$d->{'home'}/fcgi-bin" : &cgi_bin_dir($_[0]);
local $tmpl = &get_template($d->{'template'});
if (!-d $dest) {
# Need to create fcgi-bin
&make_dir_as_domain_user($d, $dest, 0755);
}
local $suffix = $mode eq "fcgid" ? "fcgi" : "cgi";
local $dirvar = $mode eq "fcgid" ? "PWD" : "DOCUMENT_ROOT";
# Make wrappers mutable
&set_php_wrappers_writable($d, 1);
# For each version of PHP, create a wrapper
local $pub = &public_html_dir($d);
local $children = &get_domain_php_children($d);
foreach my $v (&list_available_php_versions($d, $mode)) {
next if (!$v->[1]); # No executable available?!
&open_tempfile_as_domain_user($d, PHP, ">$dest/php$v->[0].$suffix");
local $t = "php".$v->[0].$suffix;
if ($tmpl->{$t} && $tmpl->{$t} ne 'none') {
# Use custom script from template
local $s = &substitute_domain_template($tmpl->{$t}, $d);
$s =~ s/\t/\n/g;
$s .= "\n" if ($s !~ /\n$/);
&print_tempfile(PHP, $s);
}
else {
# Automatically generate
local $shell = -r "/bin/bash" ? "/bin/bash" : "/bin/sh";
local $common = "#!$shell\n".
"PHPRC=\$$dirvar/../etc/php$v->[0]\n".
"export PHPRC\n".
"umask 022\n";
if ($mode eq "fcgid") {
local $defchildren = $tmpl->{'web_phpchildren'};
$defchildren = undef if ($defchildren eq "none");
if ($defchildren) {
$common .= "PHP_FCGI_CHILDREN=$defchildren\n";
$common .= "export PHP_FCGI_CHILDREN\n";
}
$common .= "PHP_FCGI_MAX_REQUESTS=99999\n";
$common .= "export PHP_FCGI_MAX_REQUESTS\n";
}
elsif ($mode eq "cgi") {
$common .= "if [ \"\$REDIRECT_URL\" != \"\" ]; then\n";
$common .= " SCRIPT_NAME=\$REDIRECT_URL\n";
$common .= " export SCRIPT_NAME\n";
$common .= "fi\n";
}
&print_tempfile(PHP, $common);
if ($v->[1] =~ /-cgi$/) {
# php-cgi requires the SCRIPT_FILENAME variable
&print_tempfile(PHP,
"SCRIPT_FILENAME=\$PATH_TRANSLATED\n");
&print_tempfile(PHP,
"export SCRIPT_FILENAME\n");
}
&print_tempfile(PHP, "exec $v->[1]\n");
}
&close_tempfile_as_domain_user($d, PHP);
&set_permissions_as_domain_user($d, 0755, "$dest/php$v->[0].$suffix");
# Put back the old number of child processes
if ($children >= 0) {
&save_domain_php_children($d, $children, 1);
}
# Also copy the .fcgi wrapper to public_html, which is needed due to
# broken-ness on some Debian versions!
if ($mode eq "fcgid" && $gconfig{'os_type'} eq 'debian-linux' &&
$gconfig{'os_version'} < 5) {
©_source_dest_as_domain_user(
$d, "$dest/php$v->[0].$suffix",
"$pub/php$v->[0].$suffix");
&set_permissions_as_domain_user(
$d, 0755, "$pub/php$v->[0].$suffix");
}
}
# Re-apply resource limits
if (defined(&supports_resource_limits) && &supports_resource_limits()) {
local $pd = $d->{'parent'} ? &get_domain($d->{'parent'}) : $d;
&set_php_wrapper_ulimits($d, &get_domain_resource_limits($pd));
}
# Make wrappers immutable, to prevent deletion by users (which can crash Apache)
&set_php_wrappers_writable($d, 0);
}
# set_php_wrappers_writable(&domain, flag, [subdomains-too])
# If possible, make PHP wrapper scripts mutable or immutable
sub set_php_wrappers_writable
{
local ($d, $writable, $subs) = @_;
if (&has_command("chattr")) {
foreach my $dir ("$d->{'home'}/fcgi-bin", &cgi_bin_dir($d)) {
foreach my $f (glob("$dir/php?.*cgi")) {
my @st = stat($f);
if (-r $f && !-l $f && $st[4] == $d->{'uid'}) {
&system_logged("chattr ".
($writable ? "-i" : "+i")." ".quotemeta($f).
" >/dev/null 2>&1");
}
}
}
if ($subs) {
# Also do sub-domains, as their CGI directories are under
# parent's domain.
foreach my $sd (&get_domain_by("subdom", $d->{'id'})) {
&set_php_wrappers_writable($sd, $writable);
}
}
}
}
# set_php_wrapper_ulimits(&domain, &resource-limits)
# Add, update or remove ulimit lines to set RAM and process restrictions
sub set_php_wrapper_ulimits
{
local ($d, $rv) = @_;
foreach my $dir ("$d->{'home'}/fcgi-bin", &cgi_bin_dir($d)) {
foreach my $f (glob("$dir/php?.*cgi")) {
local $lref = &read_file_lines_as_domain_user($d, $f);
foreach my $u ([ 'v', int($rv->{'mem'}/1024) ],
[ 'u', $rv->{'procs'} ],
[ 't', $rv->{'time'}*60 ]) {
if ($u->[0] eq 't' &&
$dir eq "$d->{'home'}/fcgi-bin") {
# CPU time limit makes no sense for fcgi, as it
# breaks the long-running php-cgi processes
next;
}
# Find current line
local $lnum;
for(my $i=0; $i<@$lref; $i++) {
if ($lref->[$i] =~ /^ulimit\s+\-(\S)\s+(\d+)/ &&
$1 eq $u->[0]) {
$lnum = $i;
last;
}
}
if ($lnum && $u->[1]) {
# Set value
$lref->[$lnum] = "ulimit -$u->[0] $u->[1]";
}
elsif ($lnum && !$u->[1]) {
# Remove limit
splice(@$lref, $lnum, 1);
}
elsif (!$lnum && $u->[1]) {
# Add at top of file
splice(@$lref, 1, 0, "ulimit -$u->[0] $u->[1]");
}
}
# If using process limits, we can't exec PHP as there will
# be no chance for the limit to be applied :(
local $ll = scalar(@$lref) - 1;
if ($lref->[$ll] =~ /php/) {
if ($rv->{'procs'} && $lref->[$ll] =~ /^exec\s+(.*)/) {
# Remove exec
$lref->[$ll] = $1;
}
elsif (!$rv->{'procs'} && $lref->[$ll] !~ /^exec\s+/) {
# Add exec
$lref->[$ll] = "exec ".$lref->[$ll];
}
}
&flush_file_lines_as_domain_user($d, $f);
}
}
}
# set_php_fpm_ulimits(&domain, &resource-limits)
# Update the FPM config with resource limits
sub set_php_fpm_ulimits
{
my ($d, $res) = @_;
my $conf = &get_php_fpm_config($d);
return 0 if (!$conf);
if ($res->{'procs'}) {
my $php_max_children =
$res->{'procs'} > $max_php_fcgid_children ?
$max_php_fcgid_children : $res->{'procs'};
&save_php_fpm_config_value($d, "pm.max_children", $php_max_children);
&save_php_fpm_config_value($d, "pm.start_servers", &get_php_start_servers($php_max_children));
&save_php_fpm_config_value($d, "pm.max_spare_servers", &get_php_max_spare_servers($php_max_children));
}
else {
my $tmpl = &get_template($d->{'template'});
my $php_max_children = $tmpl->{'web_phpchildren'};
$php_max_children = get_php_max_childred_allowed()
if ($php_max_children eq "none" || !$php_max_children);