File: feature-web.pl

package info (click to toggle)
webmin-virtual-server 2.50-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,608 kB
  • ctags: 392
  • sloc: perl: 15,687; makefile: 95; sh: 8
file content (842 lines) | stat: -rw-r--r-- 23,879 bytes parent folder | download
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

$default_web_port = $config{'web_port'} || 80;

sub require_apache
{
return if ($require_apache++);
&foreign_require("apache", "apache-lib.pl");
}

# setup_web(&domain)
# Setup a virtual website for some domain
sub setup_web
{
local $tmpl = &get_template($_[0]->{'template'});
local $web_port = $tmpl->{'web_port'} || 80;
if ($_[0]->{'alias'} && $tmpl->{'web_alias'}) {
	&$first_print($text{'setup_webalias'});
	}
else {
	&$first_print($text{'setup_web'});
	}
&require_apache();
local $conf = &apache::get_config();
local $f;
if ($apache::config{'virt_file'}) {
	$f = -d $apache::config{'virt_file'} ?
		"$apache::config{'virt_file'}/www.$_[0]->{'dom'}.conf" :
		$apache::config{'virt_file'};
	}
else {
	$vconf = &apache::get_virtual_config();
	$f = $vconf->[0]->{'file'};
	}
&lock_file($f);

# add NameVirtualHost if needed
local $nvstar;
if ($_[0]->{'name'}) {
	local ($found, $found_no_port);
	local @nv = &apache::find_directive("NameVirtualHost", $conf);
	foreach $nv (@nv) {
		$found++ if ( #$nv eq $_[0]->{'ip'} ||
			     $nv =~ /^(\S+):(\S+)/ && $1 eq $_[0]->{'ip'} ||
			     $nv eq '*');
		$found_no_port++ if $nv eq $_[0]->{'ip'};
		$nvstar++ if ($nv eq "*");
		}
	if (!$found) {
		@nv = grep { $_ ne $_[0]->{'ip'} } @nv if $found_no_port;
		&apache::save_directive("NameVirtualHost",
					[ @nv, "$_[0]->{'ip'}:$web_port" ],
					$conf, $conf);
		&flush_file_lines();
		}
	}

local @dirs = &apache_template($tmpl->{'web'}, $_[0], $tmpl->{'web_suexec'});
if ($_[0]->{'alias'} && $tmpl->{'web_alias'}) {
	# Update the parent virtual host
	local $alias = &get_domain($_[0]->{'alias'});
	local ($pvirt, $pconf) = &get_apache_virtual($alias->{'dom'},
						     $alias->{'web_port'});
	if (!$pvirt) {
		&$second_print($text{'setup_ewebalias'});
		return 0;
		}
	&lock_file($pvirt->{'file'});
	local @sa = &apache::find_directive("ServerAlias", $pconf);
	local $d;
	foreach $d (@dirs) {
		if ($d =~ /^\s*Server(Name|Alias)\s+(.*)/) {
			push(@sa, $2);
			}
		}
	&apache::save_directive("ServerAlias", \@sa, $pconf, $conf);
	&flush_file_lines();
	&unlock_file($pvirt->{'file'});
	$_[0]->{'alias_mode'} = $tmpl->{'web_alias'};
	}
else {
	# Add the actual <VirtualHost>
	local $vip = $_[0]->{'name'} && $apache::httpd_modules{'core'} >= 1.312 &&
		     $nvstar ? "*" : $_[0]->{'ip'};
	local $lref = &read_file_lines($f);
	push(@$lref, "<VirtualHost $vip:$web_port>");
	if ($_[0]->{'alias'}) {
		# Because this is just an alias to an existing virtual server,
		# create a ProxyPass or Redirect
		@dirs = grep { /^\s*Server(Name|Alias)\s/i } @dirs;
		local $aliasdom = &get_domain($_[0]->{'alias'});
		local $port = $aliasdom->{'web_port'} == 80 ? "" :
				":$aliasdom->{'web_port'}";
		local $url = "http://www.$aliasdom->{'dom'}$port/";
		if ($apache::httpd_modules{'mod_proxy'}) {
			push(@dirs, "ProxyPass / $url",
				    "ProxyPassReverse / $url");
			}
		else {
			push(@dirs, "Redirect / $url");
			}
		}
	push(@$lref, @dirs);
	push(@$lref, "</VirtualHost>");
	&flush_file_lines();
	$_[0]->{'web_port'} = $web_port;
	}
&unlock_file($f);
undef(@apache::get_config_cache);
&create_framefwd_file($_[0]);
&$second_print($text{'setup_done'});
&register_post_action(\&restart_apache);

if ($tmpl->{'web_user'} && !$_[0]->{'alias'}) {
	# Add the Apache user to the group for this virtual server, if missing
	&require_useradmin();
	local @groups = &list_all_groups();
	local ($group) = grep { $_->{'group'} eq $_[0]->{'group'} } @groups;
	if ($group) {
		local @mems = split(/,/, $group->{'members'});
		if (&indexof($tmpl->{'web_user'}, @mems) < 0) {
			# Need to add him
			&$first_print(&text('setup_webuser',
					    $tmpl->{'web_user'}));
			$group->{'members'} =
				join(",", @mems, $tmpl->{'web_user'});
			&foreign_call($usermodule, "set_group_envs", $group, 'MODIFY_GROUP');
			&foreign_call($usermodule, "making_changes");
			&foreign_call($usermodule, "modify_group", $group, $group);
			&foreign_call($usermodule, "made_changes");
			&$second_print($text{'setup_done'});
			}
		}
	}

# Make the web directory accessible under SElinux Apache
if (&has_command("chcon")) {
	local $hdir = &public_html_dir($_[0]);
	system("chcon -R -t httpd_sys_content_t ".quotemeta($hdir).
	       ">/dev/null 2>&1");
	local $cgidir = "$_[0]->{'home'}/cgi-bin";
	system("chcon -R -t httpd_sys_script_exec_t ".quotemeta($cgidir).
	       ">/dev/null 2>&1");
	local $logdir = "$_[0]->{'home'}/logs";
	system("chcon -R -t httpd_log_t ".quotemeta($logdir).
	       ">/dev/null 2>&1");
	}
}

# delete_web(&domain)
# Delete the virtual server from the Apache config
sub delete_web
{
&require_apache();
local $conf = &apache::get_config();
if ($_[0]->{'alias_mode'}) {
	# Just delete ServerAlias directives from parent
	&$first_print($text{'delete_apachealias'});
	local $alias = &get_domain($_[0]->{'alias'});
	local ($pvirt, $pconf) = &get_apache_virtual($alias->{'dom'},
						     $alias->{'web_port'});
	if (!$pvirt) {
		&$second_print($text{'setup_ewebalias'});
		return 0;
		}
	&lock_file($pvirt->{'file'});
	local @sa = &apache::find_directive("ServerAlias", $pconf);
	@sa = grep { !/\Q$_[0]->{'dom'}\E$/ } @sa;
	&apache::save_directive("ServerAlias", \@sa, $pconf, $conf);
	&flush_file_lines();
	&unlock_file($pvirt->{'file'});
	&$second_print($text{'setup_done'});
	&register_post_action(\&restart_apache);
	}
elsif ($config{'delete_indom'}) {
	# Delete all matching virtual servers
	&$first_print($text{'delete_apache'});
	local @virt = reverse(&apache::find_directive_struct("VirtualHost",
							     $conf));
	foreach $v (@virt) {
		local $sn = &apache::find_directive("ServerName",
						    $v->{'members'});
		if ($sn =~ /\Q$_[0]->{'dom'}\E$/) {
			&delete_virtual_server($v);
			}
		}
	&$second_print($text{'setup_done'});
	&register_post_action(\&restart_apache);
	}
else {
	# Just delete one virtual server
	&$first_print($text{'delete_apache'});
	local ($virt, $vconf) = &get_apache_virtual($_[0]->{'dom'},
						    $_[0]->{'web_port'});
	if ($virt) {
		&delete_virtual_server($virt);
		&$second_print($text{'setup_done'});
		&register_post_action(\&restart_apache);
		}
	else {
		&$second_print($text{'delete_noapache'});
		}
	}
undef(@apache::get_config_cache);
}

# delete_virtual_server(&vhost)
# Delete a single virtual server from the Apache config
sub delete_virtual_server
{
&require_apache();
&lock_file($_[0]->{'file'});
local $lref = &read_file_lines($_[0]->{'file'});
splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1);
&flush_file_lines();
if (&is_empty($lref)) {
	# Don't keep around empty web files
	unlink($_[0]->{'file'});
	}
&unlock_file($_[0]->{'file'});
}

# is_empty(&lref)
sub is_empty
{
local $l;
foreach $l (@{$_[0]}) {
	if ($l =~ /\S/) {
		return 0;
		}
	}
return 1;
}

# modify_web(&domain, &olddomain, [&alias, &oldalias])
# If this server has changed from name-based to IP-based hosting, update
# the Apache configuration
sub modify_web
{
local $rv = 0;
&require_apache();
local $conf = &apache::get_config();
if ($_[0]->{'alias'} && $_[0]->{'alias_mode'}) {
	# Possibly just updating parent virtual server
	if ($_[0]->{'dom'} ne $_[1]->{'dom'}) {
		&$first_print($text{'save_apache5'});
		local $alias = &get_domain($_[0]->{'alias'});
		local ($pvirt, $pconf) = &get_apache_virtual($alias->{'dom'},
						     $alias->{'web_port'});
		if (!$pvirt) {
			&$second_print($text{'setup_ewebalias'});
			}
		else {
			&lock_file($pvirt->{'file'});
			local @sa = &apache::find_directive("ServerAlias", $pconf);
			local $s;
			foreach $s (@sa) {
				$s =~ s/\Q$_[1]->{'dom'}\E($|\s)/$_[0]->{'dom'}$1/g;
				}
			&apache::save_directive("ServerAlias", \@sa, $pconf,
						$conf);
			&flush_file_lines();
			&unlock_file($pvirt->{'file'});
			&$second_print($text{'setup_done'});
			$rv++;
			}
		}
	}
else {
	# Update an actual virtual server
	local ($virt, $vconf) = &get_apache_virtual($_[1]->{'dom'},
						    $_[1]->{'web_port'});
	&lock_file($virt->{'file'});
	if ($_[0]->{'name'} != $_[1]->{'name'} ||
	    $_[0]->{'ip'} ne $_[1]->{'ip'} ||
	    $_[0]->{'ssl'} != $_[1]->{'ssl'}) {
		# Name-based hosting mode or IP has changed
		&$first_print($text{'save_apache'});
		local $lref = &read_file_lines($virt->{'file'});
		$lref->[$virt->{'line'}] =
			$_[0]->{'name'} ? "<VirtualHost *>" :
				"<VirtualHost $_[0]->{'ip'}:$_[1]->{'web_port'}>";
		&flush_file_lines();
		$rv++;
		&$second_print($text{'setup_done'});
		}
	if ($_[0]->{'home'} ne $_[1]->{'home'}) {
		# Home directory has changed .. update any directives that
		# referred to the old directory
		&$first_print($text{'save_apache3'});
		local $lref = &read_file_lines($virt->{'file'});
		for($i=$virt->{'line'}; $i<=$virt->{'eline'}; $i++) {
			$lref->[$i] =~ s/$_[1]->{'home'}/$_[0]->{'home'}/g;
			}
		&flush_file_lines();
		$rv++;
		&$second_print($text{'setup_done'});
		}
	if ($_[0]->{'alias'} && $_[2] && $_[2]->{'dom'} ne $_[3]->{'dom'}) {
		# This is an alias, and the domain it is aliased to has changed.
		# update all Proxy* and Redirect directives
		&$first_print($text{'save_apache4'});
		local $lref = &read_file_lines($virt->{'file'});
		for($i=$virt->{'line'}; $i<=$virt->{'eline'}; $i++) {
			if ($lref->[$i] =~ /^\s*(Proxy|Redirect\s)/) {
				$lref->[$i] =~ s/$_[3]->{'dom'}/$_[2]->{'dom'}/g;
				}
			}
		&flush_file_lines();
		$rv++;
		&$second_print($text{'setup_done'});
		}
	if ($_[0]->{'proxy_pass_mode'} == 1 && 
	    $_[0]->{'proxy_pass'} ne $_[1]->{'proxy_pass'}) {
		# This is a proxying forwarding website and the URL has
		# changed - update all Proxy* directives
		&$first_print($text{'save_apache6'});
		local $lref = &read_file_lines($virt->{'file'});
		for($i=$virt->{'line'}; $i<=$virt->{'eline'}; $i++) {
			if ($lref->[$i] =~ /^\s*ProxyPass(Reverse)?\s/) {
				$lref->[$i] =~ s/$_[1]->{'proxy_pass'}/$_[0]->{'proxy_pass'}/g;
				}
			}
		&flush_file_lines();
		$rv++;
		&$second_print($text{'setup_done'});
		}
	if ($_[0]->{'user'} ne $_[1]->{'user'}) {
		# Username has changed .. update SuexecUserGroup and User
		local $suexec = &apache::find_directive_struct(
			"SuexecUserGroup", $vconf);
		if ($suexec && $suexec->{'words'}->[0] eq $_[1]->{'user'}) {
			&$first_print($text{'save_apache7'});
			&apache::save_directive("SuexecUserGroup",
					[ "$_[0]->{'user'} $_[0]->{'ugroup'}" ],
					$vconf, $conf);
			&flush_file_lines();
			$rv++;
			&$second_print($text{'setup_done'});
			}
		local $user = &apache::find_directive_struct(
			"User", $vconf);
		if ($user && $user->{'words'}->[0] eq $_[1]->{'user'}) {
			&$first_print($text{'save_apache7'});
			&apache::save_directive("User",
					[ $_[0]->{'user'} ],
					$vconf, $conf);
			&flush_file_lines();
			$rv++;
			&$second_print($text{'setup_done'});
			}
		}
	if ($_[0]->{'dom'} ne $_[1]->{'dom'}) {
		# Domain name has changed .. update ServerName and ServerAlias
		&$first_print($text{'save_apache2'});
		&apache::save_directive("ServerName", [ $_[0]->{'dom'} ],
					$vconf, $conf);
		local @sa = map { s/$_[1]->{'dom'}/$_[0]->{'dom'}/g; $_ }
				&apache::find_directive("ServerAlias", $vconf);
		&apache::save_directive("ServerAlias", \@sa, $vconf, $conf);
		&flush_file_lines();
		$rv++;
		if ($virt->{'file'} =~ /$_[1]->{'dom'}/) {
			# Filename contains domain name .. need to re-name
			&unlock_file($virt->{'file'});
			local $nfn = $virt->{'file'};
			$nfn =~ s/$_[1]->{'dom'}/$_[0]->{'dom'}/;
			&rename_logged($virt->{'file'}, $nfn);
			}
		&$second_print($text{'setup_done'});
		}
	&unlock_file($virt->{'file'});
	if ($rv) {
		undef(@apache::get_config_cache);
		}
	&create_framefwd_file($_[0]);
	if (!$_[0]->{'ssl'}) {
		# Only re-start here if we won't re-start later after
		# changing SSL
		&register_post_action(\&restart_apache, 1) if ($rv);
		}
	}
return $rv;
}

$disabled_website = "$module_config_directory/disabled.html";

# disable_web(&domain)
# Adds a directive to force all requests to show an error page
sub disable_web
{
&$first_print($text{'disable_apache'});
&require_apache();
local ($virt, $vconf) = &get_apache_virtual($_[0]->{'dom'},
					    $_[0]->{'web_port'});
if ($virt) {
	&create_disable_directives($virt, $vconf);
	&$second_print($text{'setup_done'});
	&register_post_action(\&restart_apache);
	}
else {
	&$second_print($text{'delete_noapache'});
	}
}

# create_disable_directives(&virt, &vconf)
sub create_disable_directives
{
local ($virt, $vconf) = @_;
&lock_file($virt->{'file'});
local @am = &apache::find_directive("AliasMatch", $vconf);
local $conf = &apache::get_config();
&apache::save_directive("AliasMatch",
			[ @am, "^/.*\$ $disabled_website" ],
			$vconf, $conf);
&flush_file_lines();
if (!-r $disabled_website) {
	open(DISABLED, ">$disabled_website");
	print DISABLED "<h1>Website Disabled</h1>\n";
	close(DISABLED);
	chmod(0644, $disabled_website);
	}
&unlock_file($virt->{'file'});
}

# enable_web(&domain)
# Deletes the special error page directive
sub enable_web
{
&$first_print($text{'enable_apache'});
&require_apache();
local ($virt, $vconf) = &get_apache_virtual($_[0]->{'dom'},
					    $_[0]->{'web_port'});
if ($virt) {
	&remove_disable_directives($virt, $vconf);
	&$second_print($text{'setup_done'});
	&register_post_action(\&restart_apache);
	}
else {
	&$second_print($text{'delete_noapache'});
	}
}

# remove_disable_directives(&virt, &vconf)
sub remove_disable_directives
{
local ($virt, $vconf) = @_;
&lock_file($virt->{'file'});
local @am = &apache::find_directive("AliasMatch", $vconf);
@am = grep { $_ ne "^/.*\$ $disabled_website" } @am;
local $conf = &apache::get_config();
&apache::save_directive("AliasMatch", \@am, $vconf, $conf);
&flush_file_lines();
&unlock_file($virt->{'file'});
}

# check_web_clash(&domain, [field])
# Returns 1 if an Apache webserver already exists for some domain
sub check_web_clash
{
if ($_[1] || $_[1] eq 'dom') {
	local $tmpl = &get_template($_[0]->{'template'});
	local $web_port = $tmpl->{'web_port'} || 80;
	local ($cvirt, $cconf) = &get_apache_virtual($_[0]->{'dom'}, $web_port);
	return $cvirt ? 1 : 0;
	}
return 0;
}

# restart_apache([restart])
# Tell Apache to re-read its config file
sub restart_apache
{
&$first_print($_[0] ? $text{'setup_webpid2'} : $text{'setup_webpid'});
local $pid = &get_apache_pid();
if (!$pid || !kill(0, $pid)) {
	&$second_print($text{'setup_notrun'});
	return 0;
	}
if ($_[0]) {
	# Totally stop and start
	if ($apache::config{'stop_cmd'}) {
		&system_logged("$apache::config{'stop_cmd'} >/dev/null 2>&1");
		}
	else {
		&system_logged("$apache::config{'apachectl_path'} stop >/dev/null 2>&1");
		}
	sleep(5);
	if ($apache::config{'start_cmd'}) {
		&system_logged("$apache::config{'start_cmd'} >/dev/null 2>&1");
		}
	else {
		&system_logged("$apache::config{'apachectl_path'} start >/dev/null 2>&1");
		}
	&$second_print($text{'setup_done'});
	return 1;
	}
else {
	# Just signal a re-load
	if ($apache::config{'apply_cmd'}) {
		# use the configured start command
		&system_logged("($apache::config{'apply_cmd'}) >/dev/null 2>&1");
		}
	elsif (-x $apache::config{'apachectl_path'}) {
		# Use apachectl
		&system_logged("$apache::config{'apachectl_path'} restart >/dev/null 2>&1");
		}
	else {
		# Just signal the process
		&kill_logged('HUP', $pid);
		}
	&$second_print($text{'setup_done'});
	return 1;
	}
}

# get_apache_log(domain, [port], [errorlog])
# Given a domain name, returns the path to its log file
sub get_apache_log
{
&require_apache();
local ($virt, $vconf) = &get_apache_virtual($_[0], $_[1]);
if ($virt) {
	local $log;
	if ($_[2]) {
		# Looking for error log
		$log = &apache::find_directive("ErrorLog", $vconf, 1);
		}
	else {
		# Looking for normal log
		$log = &apache::find_directive("TransferLog", $vconf, 1) ||
		       &apache::find_directive("CustomLog", $vconf, 1);
		}
	return $log;
	}
else {
	return undef;
	}
}

# get_apache_virtual(domain, [port])
# Returns the list of configuration directives and the directive for the
# virtual domain itself for some domain
sub get_apache_virtual
{
&require_apache();
local $conf = &apache::get_config();
local $v;
local $sp = $_[1] || $default_web_port;
foreach $v (&apache::find_directive_struct("VirtualHost", $conf)) {
	local $vp = $v->{'words'}->[0] =~ /:(\d+)$/ ? $1 : $default_web_port;
	next if ($vp != $sp);
        local $sn = &apache::find_directive("ServerName", $v->{'members'});
	return ($v, $v->{'members'}) if ($sn eq $_[0] ||
					 $sn eq "www.$_[0]");
	local $n;
	foreach $n (&apache::find_directive_struct(
			"ServerAlias", $v->{'members'})) {
		return ($v, $v->{'members'})
			if (&indexof($_[0], @{$n->{'words'}}) >= 0 ||
			    &indexof("www.$_[0]", @{$n->{'words'}}) >= 0);
		}
        }
return ();
}

# get_apache_pid()
sub get_apache_pid
{
&require_apache();
local $pidfile = &apache::get_pid_file();
open(PID, $pidfile) || return undef;
chop($pid = <PID>);
close(PID);
return $pid;
}

# apache_template(text, &domain, suexec)
# Returns a suitably substituted Apache template
sub apache_template
{
local $dirs = $_[0];
$dirs =~ s/\t/\n/g;
$dirs = &substitute_template($dirs, $_[1]);
local @dirs = split(/\n/, $dirs);
local ($sudir, $ppdir);
foreach (@dirs) {
	$sudir++ if (/^\s*SuexecUserGroup\s/i || /^\s*User\s/i);
	$ppdir++ if (/^\s*ProxyPass\s/);
	}
if (!$sudir && $_[2] && $_[1]->{'unix'}) {
	# Automatically add suexec directives if missing
	if ($apache::httpd_modules{'core'} >= 2.0) {
		if ($apache::httpd_modules{'mod_suexec'}) {
			unshift(@dirs, "SuexecUserGroup \"#$_[1]->{'uid'}\" ".
				       "\"#$_[1]->{'ugid'}\"");
			}
		}
	else {
		unshift(@dirs, "User \"#$_[1]->{'uid'}\"",
			       "Group \"#$_[1]->{'ugid'}\"");
		}
	}
if (!$ppdir && $_[1]->{'proxy_pass'}) {
	if ($_[1]->{'proxy_pass_mode'} == 1) {
		# Proxy to another server
		push(@dirs, "ProxyPass / $_[1]->{'proxy_pass'}",
			    "ProxyPassReverse / $_[1]->{'proxy_pass'}");
		}
	else {
		# Redirect to /framefwd.html
		local $ff = &framefwd_file($_[1]);
		push(@dirs, "AliasMatch ^/.*\$ $ff");
		}
	}
return @dirs;
}

# backup_web(&domain, file)
# Save the virtual server's Apache config as a separate file
sub backup_web
{
return 1 if ($_[0]->{'alias'} && $_[0]->{'alias_mode'});
&$first_print($text{'backup_apachecp'});
local ($virt, $vconf) = &get_apache_virtual($_[0]->{'dom'},
					    $_[0]->{'web_port'});
local $lref = &read_file_lines($virt->{'file'});
local $l;
open(FILE, ">$_[1]");
foreach $l (@$lref[$virt->{'line'} .. $virt->{'eline'}]) {
	print FILE "$l\n";
	}
close(FILE);
&$second_print($text{'setup_done'});
return 1;
}

# restore_web(&domain, file, &options)
# Update the virtual server's Apache configuration from a file. Does not
# change the actual <Virtualhost> lines!
sub restore_web
{
return 1 if ($_[0]->{'alias'} && $_[0]->{'alias_mode'});
&$first_print($text{'restore_apachecp'});
local ($virt, $vconf) = &get_apache_virtual($_[0]->{'dom'},
					    $_[0]->{'web_port'});
local $srclref = &read_file_lines($_[1]);
local $dstlref = &read_file_lines($virt->{'file'});
&lock_file($virt->{'file'});
splice(@$dstlref, $virt->{'line'}+1, $virt->{'eline'}-$virt->{'line'}-1,
       @$srclref[1 .. @$srclref-2]);
if ($_[2]->{'fixip'}) {
	# Fix IP address in <Virtualhost> section (if needed)
	if ($dstlref->[$virt->{'line'}] =~
	    /^(.*<Virtualhost\s+)([0-9\.]+)(.*)$/i) {
		$dstlref->[$virt->{'line'}] = $1.$_[0]->{'ip'}.$3;
		}
	}
&flush_file_lines();
&unlock_file($virt->{'file'});
&$second_print($text{'setup_done'});

&register_post_action(\&restart_apache);
return 1;
}

%apache_mmap = ( 'jan' => 0, 'feb' => 1, 'mar' => 2, 'apr' => 3,
	  	 'may' => 4, 'jun' => 5, 'jul' => 6, 'aug' => 7,
	  	 'sep' => 8, 'oct' => 9, 'nov' => 10, 'dec' => 11 );

# bandwidth_web(&domain, start, &bw-hash)
# Searches through log files for records after some date, and updates the
# day counters in the given hash
sub bandwidth_web
{
require 'timelocal.pl';
local @logs = ( &get_apache_log($_[0]->{'dom'}, $_[0]->{'web_port'}),
		&get_apache_log($_[0]->{'dom'}, $_[0]->{'web_sslport'}) );
local $l;
local $max_ltime = $_[1];
foreach $l (&unique(@logs)) {
	local $f;
	foreach $f (&all_log_files($l)) {
		if ($f =~ /\.gz$/i) {
			open(LOG, "gunzip -c ".quotemeta($f)." |");
			}
		elsif ($f =~ /\.Z$/i) {
			open(LOG, "uncompress -c ".quotemeta($f)." |");
			}
		else {
			open(LOG, $f);
			}
		while(<LOG>) {
			if (/^(\S+)\s+(\S+)\s+(\S+)\s+\[(\d+)\/(\S+)\/(\d+):(\d+):(\d+):(\d+)\s+(\S+)\]\s+"([^"]*)"\s+(\S+)\s+(\S+)/) {
				# Valid-looking log line .. work out the time
				local $ltime = timelocal($9, $8, $7, $4, $apache_mmap{lc($5)}, $6-1900);
				if ($ltime > $_[1]) {
					local $day = int($ltime / (24*60*60));
					$_[2]->{$day} += $13;
					}
				$max_ltime = $ltime if ($ltime > $max_ltime);
				}
			}
		close(LOG);
		}
	}
return $max_ltime;
}

# all_log_files(file)
sub all_log_files
{
$_[0] =~ /^(.*)\/([^\/]+)$/;
local $dir = $1;
local $base = $2;
local ($f, @rv);
opendir(DIR, $dir);
foreach $f (readdir(DIR)) {
	if ($f =~ /^\Q$base\E/ && -f "$dir/$f") {
		push(@rv, "$dir/$f");
		}
	}
closedir(DIR);
return @rv;
}

# create_framefwd_file(&domain)
# Create a framefwd.html file for a server, if needed
sub create_framefwd_file
{
if ($_[0]->{'proxy_pass_mode'} == 2) {
	local $template = &get_template($_[0]->{'template'});
	local $ff = &framefwd_file($_[0]);
	unlink($ff);
	local $text = $template->{'frame'};
	open(FRAME, ">$ff");
	local %subs = %{$_[0]};
	$subs{'proxy_title'} ||= $tmpl{'owner'};
	$subs{'proxy_meta'} ||= "";
	$subs{'proxy_meta'} = join("\n", split(/\t/, $subs{'proxy_meta'}));
	print FRAME &substitute_template($text, \%subs);
	close(FRAME);
	if ($_[0]->{'unix'}) {
		chown($_[0]->{'uid'}, $_[0]->{'gid'}, $ff);
		}

	# Create a blank HTML page too, used in the frameset
	local $bl = &frameblank_file($_[0]);
	unlink($bl);
	open(BLANK, ">$bl");
	print BLANK "<body bgcolor=#ffffff></body>\n";
	close(BLANK);
	if ($_[0]->{'unix'}) {
		chown($_[0]->{'uid'}, $_[0]->{'gid'}, $bl);
		}
	}
}

# public_html_dir(&domain, [relative])
sub public_html_dir
{
local $tmpl = &get_template($_[0]->{'template'});
local ($hdir) = ($tmpl->{'web_html_dir'} || 'public_html');
$hdir = &substitute_template($hdir, $_[0]);
return $_[1] ? $hdir : "$_[0]->{'home'}/$hdir";
}

# framefwd_file(&domain)
sub framefwd_file
{
local $hdir = &public_html_dir($_[0]);
return "$hdir/framefwd.html";
}

# frameblank_file(&domain)
sub frameblank_file
{
local $hdir = &public_html_dir($_[0]);
return "$hdir/frameblank.html";
}

# check_depends_web(&dom)
# Ensure that a website has a Unix user, if not proxying
sub check_depends_web
{
if ($_[0]->{'alias'}) {
	# If this is an alias domain, then no home is needed
	return undef;
	}
elsif ($_[0]->{'proxy_pass'} == 2) {
	# If proxying using frame forwarding, a home is needed
	return $_[0]->{'dir'} ? undef : $text{'setup_edepframe'};
	}
elsif ($_[0]->{'proxy_pass'} == 1) {
	# If proxying using ProxyPass, no home is needed
	return undef;
	}
else {
	# For a normal website, we need a home
	return $_[0]->{'dir'} ? undef : $text{'setup_edepweb'};
	}
}

sub frame_fwd_input
{
local $rv;
if ($config{'proxy_pass'} == 1) {
	$rv .= "<tr> <td><b>".&hlink($text{'form_proxy'},
				    "proxypass")."</b></td>\n";
	}
else {
	$rv .= "<tr> <td><b>".&hlink($text{'form_framefwd'},
				    "framefwd")."</b></td>\n";
	}
$rv .= "<td colspan=3>";
$rv .= sprintf "<input type=radio name=proxy_def value=1 %s> %s\n",
      $_[0] ? "" : "checked", $text{'form_plocal'};
$rv .= sprintf "<input type=radio name=proxy_def value=0 %s> %s\n",
      $_[0] ? "checked" : "", $text{'form_purl'};
$rv .= "<input name=proxy size=40 value='$_[0]'></td> </tr>\n";
return $rv;
}

# show_restore_web(&options)
# Returns HTML for website restore option inputs
sub show_restore_web
{
# Offer to update IP
return sprintf
	"(<input type=checkbox name=web_fixip value=1 %s> %s)",
	$opts{'fixip'} ? "checked" : "", $text{'restore_webfixip'};
}

# parse_restore_web(&in)
# Parses the inputs for website restore options
sub parse_restore_web
{
local %in = %{$_[0]};
return { 'fixip' => $in{'web_fixip'} };
}

1;