File: gen_pro_vhost.php

package info (click to toggle)
dtc 0.35.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 18,824 kB
  • sloc: php: 50,739; sh: 8,596; makefile: 572; perl: 148; xml: 25
file content (1118 lines) | stat: -rw-r--r-- 43,431 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
<?php

// This script is launched before restarting apache
// to check if a bastard has deleted his directories
// with ftp: apache would refuse to start otherwise.
$chk_dir_script="#!/bin/sh

echo \"Checking vhosts directories existance...\"\n";
function vhost_chk_dir_sh($dir){
	global $conf_dtc_system_username;
	global $conf_dtc_system_groupname;

	global $chk_dir_script;

/*	$chk_dir_script .= "
if [ ! -d $dir ] ; then
	mkdir -p $dir
	nobodygroup=`cat /etc/group | cut -f 1 -d: | grep ^nobody`
	# if we can't find the nobody group, try nogroup
	if [ -z \"\"\$nobodygroup ]; then
		nobodygroup=`cat /etc/group | cut -f 1 -d: | grep ^nogroup`
	fi
	# if we can't find nogroup, then set to 65534
	if [ -z \"\"\$nobodygroup ]; then
		nobodygroup=65534
	fi
	chown nobody:\$nobodygroup $dir
	echo \"Directory $dir was missing and has been created.\"
fi
";
*/
	$chk_dir_script .= "
if [ ! -d $dir ] ; then
	mkdir -p $dir
	chown $conf_dtc_system_username:$conf_dtc_system_groupname $dir
	echo \"Directory $dir was missing and has been created.\"
fi
";
}

$chk_certs_script = "#!/bin/sh

echo \"Checking certificates validity...\"
EXIT_VAL=0\n";
function check_certs_sh($cert_path,$common_name){
	global $chk_certs_script;
	$chk_certs_script .= "if openssl x509 -in $cert_path/$common_name.cert.cert -subject ; then
	echo \"$common_name checked\"
else
	echo \"$common_name is not a valid cert: will not start apache\"
	EXIT_VAL=1
fi\n";
}

function checkCertificate($cert_path,$common_name){
	global $conf_dtc_system_username;
	global $conf_dtc_system_groupname;
	global $conf_dtcadmin_path;

	if(!is_dir($cert_path)){
		mkdir($cert_path);
	}
	if(		   !file_exists("$cert_path/$common_name".".cert.csr")
			&& !file_exists("$cert_path/privkey.pem")
			&& !file_exists("$cert_path/$common_name".".cert.key")
			&& !file_exists("$cert_path/$common_name".".cert.cert")){
		$cmd = "$conf_dtcadmin_path/genfiles/gen_customer_ssl_cert.sh $cert_path $common_name";
		$return_string = exec ($cmd, $output, $return_var);
		chown("$cert_path/$common_name".".cert.csr","$conf_dtc_system_username:$conf_dtc_system_groupname");
		chown("$cert_path/$common_name".".cert.key","$conf_dtc_system_username:$conf_dtc_system_groupname");
		chown("$cert_path/$common_name".".cert.cert","$conf_dtc_system_username:$conf_dtc_system_groupname");
		chown("$cert_path/privkey.pem","$conf_dtc_system_username:$conf_dtc_system_groupname");
	}
	if(		   file_exists("$cert_path/$common_name".".cert.csr")
			&& file_exists("$cert_path/privkey.pem")
			&& file_exists("$cert_path/$common_name".".cert.key")
			&& file_exists("$cert_path/$common_name".".cert.cert")){
		check_certs_sh($cert_path,$common_name);
		return "yes";
	}else{
		return "no";
	}
}

function test_valid_local_ip($address){
	global $console;
	global $panel_type;
	global $conf_nated_vhost_ip;

	$port = 80;

        if (!function_exists('socket_create')) {
		if($panel_type=="cronjob"){
			echo("The socket_create function does not exist or is not enabled, please ensure you have a php_sockets.so or php_sockets.dll, or have the sockets compiled into PHP.  No IP checks can be done, so assuming all IPs configured are valid.\n");
		}
		return true;
        }

	// turn off error reporting for this function
	$console .= "Checking IP $address:";
	$old_error_reporting = error_reporting('E_NONE');

	if($conf_nated_vhost_ip != '*'){
		if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
			echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n";
			return false;
		}
	}

	if (!($ret = socket_bind($sock, $address, $port))) {
		$error = socket_last_error();
		if ($error == 98 || $error == 48){
			//echo "Address already in use!\n";
			$console .= " already in use -> success\n";
			return true;
		}
		else if ($error == 99)
		{
			//echo "IP not on server...\n";
			$console .= " IP not on server -> failed\n";
			return false;
		}
		else if ($error == 13)
		{
			if($panel_type=="admin"){
				$console .= " permission denied -> assuming succes\n";
				return true;
			}
			//echo "Permission denied...\n";
			$console .= " permission denied -> failed\n";
			return false;
		} else {
			//echo "$error\n";
			echo "socket_bind()[$address] failed: reason: " . socket_strerror($error) . "\n";
			$console .= " error ". socket_strerror($error) . " -> failed\n";
			return false;
		}
	} else {
		// bound ok! (nothing listening on this yet)
		return true;
	}

	// turn it back on to what it was
	error_reporting($old_error_reporting);
}

function get_defaultCharsetDirective($db_entry){
	if(!isset($db_entry) || $db_entry == "dtc-wont-add" || $db_entry == ""){
		return "";
	}else{
		if($db_entry == "Off"){
			return "\tAddDefaultCharset Off\n";
		}else{
			return "\tAddDefaultCharset ".$db_entry."\n";
		}
	}
}

function pro_vhost_generate(){
	global $pro_mysql_domain_table;
	global $pro_mysql_admin_table;
	global $pro_mysql_subdomain_table;
	global $pro_mysql_ssl_ips_table;
	global $pro_mysql_product_table;

	global $conf_db_version;
	global $conf_unix_type;

	global $conf_apache_vhost_path;
	global $conf_generated_file_path;
	global $conf_dtcshared_path;
	global $conf_dtcadmin_path;
	global $conf_dtcclient_path;
	global $conf_dtcdoc_path;
	global $conf_dtcemail_path;
	global $conf_main_site_ip;
	global $conf_use_multiple_ip;
	global $conf_site_addrs;
	global $conf_php_library_path;
	global $conf_php_additional_library_path;
	global $conf_administrative_site;
	global $conf_administrative_ssl_port;
	global $conf_use_ssl;

	global $conf_shared_renewal_shutdown;

	global $conf_use_nated_vhost;
	global $conf_nated_vhost_ip;

	global $console;
	global $chk_dir_script;
	global $chk_certs_script;

	global $conf_main_domain;
	global $conf_404_subdomain;

	global $conf_mysql_db;

	global $conf_apache_version;
	global $conf_apache_directoryindex;

	global $conf_autogen_webmail_alias;
	global $conf_autogen_webmail_type;
	global $conf_autogen_webmail_protocol;
	
	global $conf_use_shared_ssl;
	$vhost_file = "";

	$aufs_list = "";

	$logrotate_file_chk = array();
	$logrotate_file = "# Do not edit this file, it's generated
# edit /etc/dtc/logrotate.template instead!
";

$vhost_file_start = "# WARNING ! This file is automatically edited by the dtc cron
# daemon: do not edit. All manual changes to hosts that are configured within
# the dtc panel will be removed with the next cron job. It's the same for all
# files in this folder exept the ssl, the 404 and the template folder.
#
# If you feel an option is missing, feel free to edit the script that generate
# this file in dtc/admin/genfiles/gen_pro_vhosts.php. Best is to send us your
# patch if you feel it's good enough to share.
#
# All non dtc hosts should be added in a SEPARATE file that you should include
# in your httpd.conf or apache.conf See your distribution manual to know where
# to find this file (somewhere in /etc/httpd or /etc/apache2 or even in
# /usr/local/etc/apache/httpd.conf ...).

# Loading the php5 module, as we had to disable the default config file which
# has in /etc/apache2/mods-enabled/php5.conf a SetHandler directive that is
# conflicting with the one we're using with SBOX
LoadModule php5_module /usr/lib/apache2/modules/libphp5.so

# Disabling TRACE (for security reasons)
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(DELETE|TRACE|TRACK)
RewriteRule .* - [F]

";

	$vhost_file_listen = "";

	if($conf_unix_type == "gentoo"){
		$conf_tools_prefix = "/var/www/localhost/htdocs";
	}else if($conf_unix_type == "bsd"){
		$conf_tools_prefix = "/usr/local/www";
	}else{
		$conf_tools_prefix = "/usr/share";
	}

	// DB version check
	if(($conf_db_version < 10000 || !isset($conf_db_version)) ||
		!isset($conf_use_ssl) || !isset($conf_use_nated_vhost)){
		$vhost_file .= "# WARNING !!! DATABASE SCHEMA IS COMMING FROM AN HOLD DTC VERSION : PLEASE UPGRADE YOUR TABLES TO NEW VERSION !!!\n";
	}

	$num_generated_vhosts=0;
	$query = "SELECT * FROM $pro_mysql_domain_table WHERE 1 ORDER BY name;";
	$result = mysql_query ($query)or die("Cannot execute query \"$query\"");
	$num_rows = mysql_num_rows($result);

	if($num_rows < 1){
		die("No account to generate : database has to contain AT LEAST one domain name");
	}

	$query2 = "SELECT $pro_mysql_admin_table.path
FROM $pro_mysql_domain_table,$pro_mysql_admin_table
WHERE $pro_mysql_domain_table.name='$conf_main_domain'
AND $pro_mysql_admin_table.adm_login=$pro_mysql_domain_table.owner;";
	$result2 = mysql_query ($query2)or die("Cannot execute query \"$query2\"!");
	$enable404feature = true;
	//echo "Query $query2 resulted in ".mysql_num_rows($result2)."\n";
	if(mysql_num_rows($result2) != 1){
		$enable404feature=false;
	}
	//don't die here... 	we will try and do things to work around this bug
	//die("Cannot find main domain admin path!!!");

	if ($enable404feature == true)
	{
		$a = mysql_fetch_array($result2);
		$path_404 = $a["path"]."/$conf_main_domain/subdomains/$conf_404_subdomain";
		// make sure the vhost_chk_dir script has the 404 entries
                vhost_chk_dir_sh("$path_404/html");
                vhost_chk_dir_sh("$path_404/logs");
                vhost_chk_dir_sh("$path_404/cgi-bin");
	}

	if($conf_use_multiple_ip == "yes" && $conf_use_nated_vhost == "no"){
		$all_site_addrs = explode("|",$conf_site_addrs);
		$nbr_addrs = sizeof($all_site_addrs);
		for($i=0;$i<$nbr_addrs;$i++){
			// first write all config'ed IPs with the Listen
			if (test_valid_local_ip($all_site_addrs[$i]) && !preg_match("/Listen ".$all_site_addrs[$i].":80/", $vhost_file_listen))
			{
				$vhost_file_listen .= "Listen ".$all_site_addrs[$i].":80\n";
			} else {
				$vhost_file_listen .= "#Listen ".$all_site_addrs[$i].":80\n";
			}
			$query2 = "SELECT * FROM $pro_mysql_domain_table WHERE ip_addr='".$all_site_addrs[$i]."' LIMIT 1;";
			$result2 = mysql_query ($query2)or die("Cannot execute query \"$query\"");
			$num_rows2 = mysql_num_rows($result2);
			if($num_rows2 > 0){
				$vhost_file .= "NameVirtualHost ".$all_site_addrs[$i].":80\n";
				if ($conf_use_shared_ssl == "yes") {
					$vhost_file .= "NameVirtualHost ".$all_site_addrs[$i].":443\n";
				}
				if ($enable404feature == true){
					$vhost_file .= "<VirtualHost ".$all_site_addrs[$i].":80>
	ServerName $conf_404_subdomain.$conf_main_domain
	DocumentRoot $path_404/html
	<Directory $path_404/html>
		Allow from all
	</Directory>
	ScriptAlias /cgi-bin $path_404/cgi-bin
	ErrorLog $path_404/logs/error.log
	LogSQLTransferLogTable ".str_replace("-","A",str_replace(".","_",$conf_main_domain)).'$'.$conf_404_subdomain.'$'."xfer
	LogSQLScoreDomain $conf_main_domain
	LogSQLScoreSubdomain $conf_404_subdomain
	LogSQLScoreTable $conf_mysql_db.http_accounting
	DirectoryIndex $conf_apache_directoryindex
</VirtualHost>\n";
					$logrotate_file_chk[] = "$path_404/logs/error.log";
					//$logrotate_file .= "$path_404/logs/error.log ";
				}
			}
		}
	}else{
		$ip_for_404=$conf_main_site_ip;
		if($conf_use_nated_vhost=="yes"){
			$ip_for_404 = $conf_nated_vhost_ip;
			if (test_valid_local_ip($conf_nated_vhost_ip) && !preg_match("/Listen ".$conf_nated_vhost_ip.":80/", $vhost_file_listen))
			{
				$vhost_file_listen .= "Listen ".$conf_nated_vhost_ip.":80\n";
			} else {
				$vhost_file_listen .= "#Listen ".$conf_nated_vhost_ip.":80\n";
			}
			$vhost_file .= "NameVirtualHost ".$conf_nated_vhost_ip.":80\n";
		}else{
			if (test_valid_local_ip($conf_main_site_ip) && !preg_match("/Listen ".$conf_main_site_ip.":80/", $vhost_file_listen))
			{
				$vhost_file_listen .= "Listen ".$conf_main_site_ip.":80\n";
			} else {
				$vhost_file_listen .= "#Listen ".$conf_main_site_ip.":80\n";
			}
			$vhost_file .= "NameVirtualHost ".$conf_main_site_ip.":80\n";
			if ($conf_use_shared_ssl == "yes") {
				$vhost_file .= "NameVirtualHost ".$conf_main_site_ip.":443\n";
			}
		}
		if ($enable404feature == true){
			$vhost_file .= "<VirtualHost ".$ip_for_404.":80>
        ServerName $conf_404_subdomain.$conf_main_domain
        DocumentRoot $path_404/html
        ScriptAlias /cgi-bin $path_404/cgi-bin
        ErrorLog $path_404/logs/error.log
        LogSQLTransferLogTable ".str_replace("-","A",str_replace(".","_",$conf_main_domain)).'$'.$conf_404_subdomain.'$'."xfer
        LogSQLScoreDomain $conf_main_domain
        LogSQLScoreSubdomain $conf_404_subdomain
        LogSQLScoreTable $conf_mysql_db.http_accounting
        DirectoryIndex $conf_apache_directoryindex
</VirtualHost>\n";

		if ($conf_use_shared_ssl == "yes") {
			
		    $vhost_file .= "<VirtualHost ".$conf_main_site_ip.":443>
        ServerName $conf_404_subdomain.$conf_main_domain
        DocumentRoot $path_404/html
        ScriptAlias /cgi-bin $path_404/cgi-bin
        ErrorLog $path_404/logs/error.log
        LogSQLTransferLogTable ".str_replace("-","A",str_replace(".","_",$conf_main_domain)).'$'.$conf_404_subdomain.'$'."xfer
        LogSQLScoreDomain $conf_main_domain
        LogSQLScoreSubdomain $conf_404_subdomain
        LogSQLScoreTable $conf_mysql_db.http_accounting
        DirectoryIndex $conf_apache_directoryindex
	SSLEngine on
	SSLCertificateFile ".$conf_generated_file_path."/ssl/new.cert.cert
	SSLCertificateKeyFile ".$conf_generated_file_path."/ssl/new.cert.key
</VirtualHost>\n\n";	
		}

		$logrotate_file_chk[] = "$path_404/logs/error.log";
		// $logrotate_file .= "$path_404/logs/error.log ";
	    }
	
	}

	$vhost_file .= "<Directory $conf_dtcadmin_path>
	Options FollowSymLinks
	Order Deny,Allow
	Allow from all
</Directory>
<Directory $conf_dtcclient_path>
	Options FollowSymLinks
	Order Deny,Allow
	Allow from all
</Directory>
<Directory $conf_dtcemail_path>
	Options FollowSymLinks
	Order Deny,Allow
	Allow from all
</Directory>\n";

	if($conf_autogen_webmail_alias == "yes"){
		$vhost_file .= "RedirectPermanent /webmail $conf_autogen_webmail_protocol//$conf_administrative_site/$conf_autogen_webmail_type\n";
	}

	#############################
	# mod_cband user generation #
	#############################
	$vhost_file .= "<IfModule mod_cband.c>\n";
	$q = "SELECT DISTINCT adm_login,$pro_mysql_product_table.bandwidth FROM $pro_mysql_domain_table,$pro_mysql_admin_table,$pro_mysql_product_table
WHERE $pro_mysql_domain_table.owner=$pro_mysql_admin_table.adm_login
AND $pro_mysql_product_table.id=$pro_mysql_admin_table.prod_id
AND $pro_mysql_admin_table.prod_id != '0'
AND $pro_mysql_admin_table.id_client != '0'";
	$r = mysql_query($q)or die("Cannot query $q line ".__LINE__." file ".__FILE__." sql said: ".mysql_error());
	$n = mysql_num_rows($r);
	for($i=0;$i<$n;$i++){
		$a = mysql_fetch_array($r);
		$vhost_file .= "
<CBandUser ".$a["adm_login"].">
	CBandUserSpeed 10Mbps 10 30
	CBandUserLimit ".$a["bandwidth"]."M
	CBandUserPeriod 4W
	CBandUserPeriodSlice 1W
	CBandUserExceededSpeed 32kbps 2 5
	CBandUserScoreboard /var/lib/dtc/etc/cband_scores/".$a["adm_login"]."
</CBandUser>
";
	}
	$vhost_file .= "</IfModule>\n";
	#################################
	# end mod_cband user generation #
	#################################

	// autodiscover vhost
	$autodiscover_hosts = array();
	for ($i=0;$i<$num_rows;$i++) {
		$row = mysql_fetch_array($result) or die ("Cannot fetch user");
		$web_name = $row["name"];
		if ($web_name == "" || $row['primary_mx'] != 'default' || $row['other_mx'] != 'default') {
			continue;
		}
		$query2 = "SELECT COUNT(*) FROM $pro_mysql_subdomain_table WHERE domain_name='$web_name' AND subdomain_name='autodiscover';";
		$result2 = mysql_query($query2) or die("Cannot execute query \"$query2\"");
		if (mysql_result($result2, 0, 'COUNT(*)') > 0) {
			continue;
		}
		$autodiscover_hosts[] = $web_name;
	}			
	mysql_data_seek($result, 0);
	if (count($autodiscover_hosts) > 0) {
		$vhost_file .= "<VirtualHost ".$conf_main_site_ip.":80>
	ServerName autodiscover.$conf_main_domain\n";
		foreach ($autodiscover_hosts as $autodiscover_host) {
			$vhost_file .= "	ServerAlias autodiscover.$autodiscover_host\n";	
		}
		$vhost_file .="	Alias /autodiscover/autodiscover.xml $conf_generated_file_path/autodiscover-redirect.xml
	<Location /autodiscover/autodiscover.xml>
		Order Deny,Allow
	</Location>
	<LocationMatch \"^/(?!autodiscover/autodiscover.xml)\">
		Order Allow,Deny
	</LocationMatch>
</VirtualHost>\n\n";
	}
	
	for($i=0;$i<$num_rows;$i++){
		$row = mysql_fetch_array($result) or die ("Cannot fetch user");
		$web_name = $row["name"];
		if ($web_name == "")
		{
			print("No name specified for domain, skipping...");
			continue;
		}
		$web_owner = $row["owner"];
		$ip_addr = $row["ip_addr"];
		$domain_safe_mode = $row["safe_mode"];
		$domain_sbox_protect = $row["sbox_protect"];
		$domain_parking = $row["domain_parking"];
		$domain_parking_type = $row["domain_parking_type"];
		$domain_wildcard_dns = $row["wildcard_dns"];
		$domain_default_sub_server_alias = $row["default_sub_server_alias"];
		$create_mail_autoconfig = $row["primary_mx"] == "default";
		
		unset($backup_ip_addr);
		if (isset($row["backup_ip_addr"])){
			$backup_ip_addr = $row["backup_ip_addr"];
		}
		if (isset($backup_ip_addr) && ($backup_ip_addr == "NULL" || trim($backup_ip_addr) == "")){
			unset($backup_ip_addr);
		} 
		// need to check if we have a NameVirtualHost entry for this backup IP, to support multiple backup sites on one IP
		if (isset($backup_ip_addr)){
			if (test_valid_local_ip($backup_ip_addr) && !preg_match("/Listen ".$backup_ip_addr.":80/", $vhost_file_listen)){
				$vhost_file_listen .= "Listen ".$backup_ip_addr.":80\n";
			} else {
				$vhost_file_listen .= "#Listen ".$backup_ip_addr.":80\n";
			}
			if (!preg_match("/NameVirtualHost $backup_ip_addr/", $vhost_file)){
				$vhost_file .= "NameVirtualHost ".$backup_ip_addr.":80\n";
			}
		}
		if($conf_use_multiple_ip == "yes"){
			$ip_to_write = $ip_addr;
		}else{
			$ip_to_write = $conf_main_site_ip;
		}
		if($conf_use_nated_vhost == "yes"){
			$ip_to_write = $conf_nated_vhost_ip;
		}
		$web_default_subdomain = $row["default_subdomain"];

		// Get the owner informations
		$query2 = "SELECT * FROM $pro_mysql_admin_table WHERE adm_login='$web_owner';";
		$result2 = mysql_query ($query2)or die("Cannot execute query \"$query2\"");
		$num_rows2 = mysql_num_rows($result2);
		if($num_rows2 != 1){
			echo("No user of that name ($web_owner)!\n");
			continue;
		}
		$webadmin = mysql_fetch_array($result2) or die ("Cannot fetch user");
		$web_path = $webadmin["path"];
		$expire_stored = $webadmin["expire"];
		if($expire_stored == "0000-00-00"){
			$site_expired = "no";
		}else{
			$calc_expire_date = calculateExpirationDate($expire_stored,"0000-00-$conf_shared_renewal_shutdown");
			$calc_expire_date_array = explode("-",$calc_expire_date);
			$expire_timestamp = mktime(1,1,1,$calc_expire_date_array[1],$calc_expire_date_array[2],$calc_expire_date_array[0]);
			if($expire_timestamp < mktime()){
				$site_expired = "yes";
			}else{
				$site_expired = "no";
			}
		}

		if($domain_parking != "no-parking" && $web_name != $conf_main_domain){
			$domain_to_get = $domain_parking;
		}else{
			$domain_to_get = $web_name;
		}

		// Grab all subdomains
		if($web_name == $conf_main_domain){
			$query2 = "SELECT * FROM $pro_mysql_subdomain_table WHERE domain_name='$web_name' AND ip='default' AND subdomain_name!='$conf_404_subdomain' AND subdomain_name!='$web_default_subdomain' ORDER BY subdomain_name;";
		}else{
			$query2 = "SELECT * FROM $pro_mysql_subdomain_table WHERE domain_name='$domain_to_get' AND ip='default' AND subdomain_name!='$web_default_subdomain' ORDER BY subdomain_name;";
		}
		$result2 = mysql_query ($query2)or die("Cannot execute query \"$query2\"");
		$num_rows2 = mysql_num_rows($result2);

		unset($temp_array_subs);
		$temp_array_subs = array();
		for($j=0;$j<$num_rows2;$j++){
			$temp_array_subs[] = mysql_fetch_array($result2) or die ("Cannot fetch user line ".__LINE__." file ".__FILE__);
		}

		// We get the default subdomain and we add it at the end of the array. The goal is to have the
		// wildcard subdomain be the last in the list of the vhosts.conf
		$query2 = "SELECT * FROM $pro_mysql_subdomain_table WHERE domain_name='$domain_to_get' AND ip='default' AND subdomain_name='$web_default_subdomain';";
		$result2 = mysql_query ($query2)or die("Cannot execute query \"$query2\"");
		$my_num_rows = mysql_num_rows($result2);
		if($my_num_rows == 1){
			$temp_array_subs[] = mysql_fetch_array($result2) or die ("Cannot fetch user".__LINE__." file ".__FILE__);
			$num_rows2++;
		}

		for($j=0;$j<$num_rows2;$j++){
			$subdomain = $temp_array_subs[$j];
//			$subdomain = mysql_fetch_array($result2) or die ("Cannot fetch user");
			$web_subname = $subdomain["subdomain_name"];
			$shared_hosting_subdomain_security = $subdomain["shared_hosting_security"];
			if( $subdomain["customize_vhost"] == ""){
				$custom_directives = "";
			}else{
				$custom_directives = "
	# Start of custom directives
	".$subdomain["customize_vhost"]."
	# End of custom directives";
			}

//			$console .= "Working on $web_subname.$web_name\n";

			// if we explicitly don't want to generate a vhost entry for this subdomain
			if (isset($subdomain["generate_vhost"]) && $subdomain["generate_vhost"] == "no")
			{
				continue;
			}

// ------------------------------------------------
// --- Start of the conf of the panel subdomain ---
// ------------------------------------------------
			if($conf_administrative_site == "$web_subname.$web_name"){
			// generate SSL and non SSL if we have enabled SSL
			$gen_iterations = 1;
			if ($conf_use_ssl == "yes"){
				$gen_iterations++;
			}

			// if we want to generate a backup IP (transitional)
			// need to loop through this one
			if (isset($backup_ip_addr))
			{
				$gen_iterations++;
			}
			for ($k = 0; $k < $gen_iterations; $k++){
				$log_tablename = str_replace("-","A",str_replace(".","_",$web_name)).'$'.str_replace("-","A",str_replace(".","_",$web_subname));
				if($conf_use_ssl == "yes" && $k == 0){
					# add the directive for SSL here
					if (test_valid_local_ip($ip_to_write) && !preg_match("/Listen ".$ip_to_write.":".$conf_administrative_ssl_port."/", $vhost_file_listen))
					{
						$vhost_file_listen .= "Listen ".$ip_to_write.":".$conf_administrative_ssl_port."\n";
					} else {
						$vhost_file_listen .= "#Listen ".$ip_to_write.":".$conf_administrative_ssl_port."\n";
					}
					$vhost_file .= "<VirtualHost ".$ip_to_write.":".$conf_administrative_ssl_port.">\n";
				} else if ($k == 1 && isset($backup_ip_addr) || ($conf_use_ssl != "yes" && $k == 0 && isset($backup_ip_addr))) {
					$vhost_file .= "<VirtualHost ".$backup_ip_addr.":80>\n";
				}else {
					$vhost_file .= "<VirtualHost ".$ip_to_write.":80>\n";
				}

				// Added by Luke
				// Needed to create an Alias in httpd.conf for non-resolvable domains
				// This does http://dtc.your-domain.com/unresolved-domain.com
				// TG: added a flag to say yes/no to that alias for each domains
				$alias_domain_query = "SELECT * FROM $pro_mysql_domain_table WHERE gen_unresolved_domain_alias='yes' ORDER BY name;";
				$result_alias = mysql_query ($alias_domain_query)or die("Cannot execute query \"$query\" line ".__LINE__." file ".__FILE__." mysql said: ".mysql_error());
				$num_rows_alias = mysql_num_rows($result_alias);
				for($x=0;$x<$num_rows_alias;$x++) {
					$rowX = mysql_fetch_array($result_alias) or die ("Cannot fetch domain for Alias");
					$web_nameX = $rowX["name"];
					$web_ownerX = $rowX["owner"];
					$ip_addrX = $rowX["ip_addr"];
					$backup_ip_addrX = $rowX["backup_ip_addr"];
					$alias_user_query = "SELECT * FROM $pro_mysql_admin_table WHERE adm_login='$web_ownerX';";
					$alias_user_result = mysql_query($alias_user_query) or die("Cannot fetch user for Alias");
					$num_rows_alias_user = mysql_num_rows($alias_user_result);
					if ($num_rows_alias_user != 1) {
						echo("No user of that name ($web_ownerX)!\n");
						continue;
					}
					$alias_path = mysql_fetch_array($alias_user_result) or die ("Cannot fetch user");
					$web_pathX = $alias_path["path"];
					// TG: Added open_basedir restriction (for obvious security reasons)
					$qsubdom = "SELECT * FROM $pro_mysql_subdomain_table WHERE domain_name='$web_nameX' AND ip='default';";
					$rx = mysql_query ($qsubdom)or die("Cannot execute query \"$qsubdom\" line ".__LINE__." file ".__FILE__." mysql said: ".mysql_error());
					$numx =  mysql_num_rows($rx);
					for($subx=0;$subx<$numx;$subx++){
						$ax = mysql_fetch_array($rx) or die ("Cannot fetch subdomain for Alias");
						$subdomx = $ax["subdomain_name"];
						$globalx = $ax["register_globals"];
						if($globalx == "yes"){
							$gblx = "php_admin_value register_globals 1";
						}else{
							$gblx = "php_admin_value register_globals 0";
						}
						if($php_vers_bigger_than_53 = 0){
							if($rowX["safe_mode"] == "no" && $ax["safe_mode"] == "no"){
								$safex = "php_admin_value safe_mode 0";
							}else{
								$safex = "php_admin_value safe_mode 1";
							}
							$my_open_basedir = "\n\t\tphp_admin_value open_basedir \"$web_pathX/$web_nameX/:$conf_php_library_path:$conf_php_additional_library_path:\"";
						}else{
							$my_open_basedir = "";
							$safex = "";
						}
						$vhost_file .= "\tAlias /$subdomx.$web_nameX $web_pathX/$web_nameX/subdomains/$subdomx/html
	<Location /$subdomx.$web_nameX>
		".$safex.$custom_directives.$my_open_basedir."
		php_admin_value open_basedir \"$web_pathX/$web_nameX/:$conf_php_library_path:$conf_php_additional_library_path:\"
		$gblx
	</Location>\n";
					}
				}
				// End of Luke's patch

				$vhost_file .= "	ServerName $web_subname.$web_name\n";
				if($conf_use_ssl == "yes" && $k == 0){
					$vhost_file .= "	SSLEngine on
	SSLCertificateFile ".$conf_generated_file_path."/ssl/new.cert.cert
	SSLCertificateKeyFile ".$conf_generated_file_path."/ssl/new.cert.key\n";
					if (file_exists($conf_generated_file_path."/ssl/new.cert.ca")) {
						$vhost_file .= "	SSLCertificateChainFile ".$conf_generated_file_path."/ssl/new.cert.ca\n";
					}
				}
				vhost_chk_dir_sh("$web_path/$web_name/subdomains/$web_subname/html");
				vhost_chk_dir_sh("$web_path/$web_name/subdomains/$web_subname/logs");
				vhost_chk_dir_sh("$web_path/$web_name/subdomains/$web_subname/cgi-bin");
				if ( $conf_unix_type == "bsd" ) {
					$vhost_file .= "	Alias /phpmyadmin ".$conf_tools_prefix."/phpMyAdmin\n";
				}else{
					$vhost_file .= "	Alias /phpmyadmin ".$conf_tools_prefix."/phpmyadmin\n";
				}
				$vhost_file .= "	Alias /dtc $conf_dtcclient_path
	Alias /dtcdoc $conf_dtcdoc_path/html/en
	Alias /dtcemail $conf_dtcemail_path
	Alias /dtcadmin $conf_dtcadmin_path/
	Alias /stats $web_path/$web_name/subdomains/$web_subname/logs
	Alias /awstats-icon $conf_tools_prefix/awstats/icon
	Alias /squirrelmail ".$conf_tools_prefix."/squirrelmail
	Alias /roundcube /var/lib/roundcube
	Alias /extplorer /usr/share/extplorer
	AliasMatch ^/autodiscover/autodiscover\.xml\$ $conf_generated_file_path/autodiscover.xml.php
	php_admin_value sendmail_from webmaster@$web_name
	DocumentRoot $web_path/$web_name/subdomains/$web_subname/html
	<Directory $web_path/$web_name/subdomains/$web_subname/html>
		Allow from all
	</Directory>
	<Directory $web_path/$web_name/subdomains/$web_subname/logs>
		Allow from all
	</Directory>
# No ScriptAlias: we want to use system's /usr/lib/cgi-bin !!!
#	ScriptAlias /cgi-bin $web_path/$web_name/subdomains/$web_subname/cgi-bin
	ErrorLog $web_path/$web_name/subdomains/$web_subname/logs/error.log
	LogSQLTransferLogTable $log_tablename\$xfer
	LogSQLScoreDomain $web_name
	LogSQLScoreSubdomain $web_subname
	LogSQLScoreTable $conf_mysql_db.http_accounting
	DirectoryIndex $conf_apache_directoryindex$custom_directives
	<IfModule mod_bwshare.c>
		BW_throttle_off 1
	</IfModule>
	<IfModule mod_security2.c>
		SecRuleEngine Off
	</IfModule>";
	if($conf_force_use_https == "yes" && $conf_use_ssl == "yes"){
		$vhost_file .= "
	RewriteEngine On
	RewriteCond %{HTTPS} off
	RewriteRule (.*) https://$conf_administrative_site$1 [R,L]";
	}
$vhost_file .= "
</VirtualHost>

";
			$logrotate_file_chk[] = "$web_path/$web_name/subdomains/$web_subname/logs/error.log";
			//$logrotate_file .= "$web_path/$web_name/subdomains/$web_subname/logs/error.log ";
} // - end of for loop

// ---------------------------------------------------
// --- Start of the conf of server users subdomain ---
// ---------------------------------------------------
			} else {
				// Generate a permanet redirect for all subdomains of target if using a domain parking
				if($domain_parking != "no-parking" && ($domain_parking_type == "redirect" || $conf_administrative_site == "$web_subname.$domain_to_get")){
					if($j == 0){
						$console .= "Making domain parking for $web_name\n";
						$vhost_file .= "<VirtualHost ".$ip_to_write.":80>
	ServerName $web_name
	Redirect permanent / http://$domain_parking/
</VirtualHost>\n\n";
					}
					$console .= "Making domain parking for $web_subname.$web_name\n";
					$vhost_file .= "<VirtualHost ".$ip_to_write.":80>
	ServerName $web_subname.$web_name
	Redirect permanent / http://$web_subname.$domain_parking/
</VirtualHost>\n\n";
                                } else if ($domain_parking != "no-parking" && $domain_parking_type == "serveralias") {
                                        // do nothing here, as serveralias parking will be injected throughout the generation of the main domain
				}else{
					vhost_chk_dir_sh("$web_path/$domain_to_get/subdomains/$web_subname/logs");
					vhost_chk_dir_sh("$web_path/$domain_to_get/subdomains/$web_subname/html");
					vhost_chk_dir_sh("$web_path/$domain_to_get/subdomains/$web_subname/cgi-bin");
					// We need to make it for both in case of a domain parking
					if($domain_to_get != $web_name){
						vhost_chk_dir_sh("$web_path/$web_name/subdomains/$web_subname/logs");
						vhost_chk_dir_sh("$web_path/$web_name/subdomains/$web_subname/html");
						vhost_chk_dir_sh("$web_path/$web_name/subdomains/$web_subname/cgi-bin");
					}
					if($webadmin["shared_hosting_security"] == 'sbox_aufs' || ($webadmin["shared_hosting_security"] == 'mod_php' && $subdomain["shared_hosting_security"] == 'sbox_aufs')){
						vhost_chk_dir_sh("$web_path/$web_name/subdomains.aufs");
						vhost_chk_dir_sh("$web_path/$web_name/subdomains.aufs/".$web_subname);
						$aufs_list .= "$web_path/$web_name/subdomains/".$web_subname."\n";
					}
					$iteration_table = array();
					$iteration_table[] = "normal";
					$ssl_cert_folder_path = "$web_path/$domain_to_get/subdomains/$web_subname/ssl";
					if($subdomain["ssl_ip"] != "none"){
						$ssl_returns = checkCertificate($ssl_cert_folder_path,$web_subname.".".$web_name);
						if($ssl_returns == "yes"){
							$iteration_table[] = "ssl";
							// Start of <krystian@ezpear.com> patch
							if($conf_use_nated_vhost=="yes"){
								$q="select port from $pro_mysql_ssl_ips_table where ip_addr='${subdomain["ssl_ip"]}' and available='no';";
								$r=mysql_query($q)or die("Cannot query \"$q\" line ".__LINE__." file ".__FILE__." sql said: ".mysql_error());
								$n = mysql_num_rows($r);
								if($n > 0){
									$row=mysql_fetch_array($r);
									$port=$row["port"];
									$ip_vhost=$ip_to_write;
									if(empty($port)){
										$port = "443";
									}
								}else{
									$port = "443";
									$ip_vhost = $subdomain["ssl_ip"];
								 }
							}else{
								$port = "443";
							}
							// End of <krystian@ezpear.com> patch
						}
					}

					// if we want to generate a backup IP (transitional)
					// need to loop through this one
					if (isset($backup_ip_addr)){
						$iteration_table[] = "backup";
					}

					$log_tablename = str_replace("-","A",str_replace(".","_",$web_name)).'$'.str_replace("-","A",str_replace(".","_",$web_subname));
					$vhost_more_conf = "";
					$php_more_conf = "";
					if($subdomain["register_globals"] == "yes"){
						$php_more_conf .= "	php_admin_value register_globals 1\n";
					}
					if($subdomain["php_memory_limit"] != ""){
						$php_more_conf .= "	php_admin_value memory_limit ".$subdomain["php_memory_limit"]."M\n";
					}
					if($subdomain["php_max_execution_time"] != ""){
						$php_more_conf .= "	php_admin_value max_execution_time ".$subdomain["php_max_execution_time"]."\n";
					}
					if($subdomain["php_session_auto_start"] == "yes"){
						$php_more_conf .= "	php_admin_flag session_autostart ".$subdomain["php_session_auto_start"]."\n";
					}
					if($subdomain["php_allow_url_fopen"] == "yes"){
						$php_more_conf .= "	php_admin_flag allow_url_fopen on\n";
					}
					if($subdomain["php_post_max_size"] != ""){
						$php_more_conf .= "	php_admin_value post_max_size ".$subdomain["php_post_max_size"]."M\n";
					}
					if($subdomain["php_upload_max_filesize"] != ""){
						$php_more_conf .= "	php_admin_value upload_max_filesize ".$subdomain["php_upload_max_filesize"]."M\n";
					}
					
					if($subdomain["use_shared_ssl"] == "yes" && $conf_use_shared_ssl == "yes"){
						$iteration_table[]="shared_ssl";
					}
					if (preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $subdomain["redirect_url"])) {
						$vhost_more_conf .= "Redirect / ".$subdomain["redirect_url"]."\n";
					}
					if($web_subname == "$web_default_subdomain"){
						if ($domain_parking == "no-parking"){
							// no domain parking
							$server_alias_domain = $domain_to_get;
						} else {
							// parking: same_docroot
							$server_alias_domain = $web_name;
							// parking: redirect doesn't happen in this else branch
						}
						if($domain_default_sub_server_alias == "yes"){
							$vhost_more_conf .= "	ServerAlias $server_alias_domain\n";
						}
						if($domain_wildcard_dns == "yes"){
							$vhost_more_conf .= "   ServerAlias *.$server_alias_domain\n";
						}
					}

					// ServerAlias for parked domains
					$q_serveralias = "select * from $pro_mysql_domain_table where domain_parking_type='serveralias' and domain_parking='$web_name'";
					$r_serveralias = mysql_query($q_serveralias) or die("Cannot query \"$q\" line ".__LINE__." file ".__FILE__." sql said: ".mysql_error());
					while ($row_serveralias = mysql_fetch_array($r_serveralias)) {
						// default subdomain and wildcard subdomain settings are inherited from the main domain, not the parked domain
						// this is because in the gui these settings are not accessable for a parked domain
						if ($web_subname == "$web_default_subdomain") {
							$vhost_more_conf .= "        ServerAlias ${row_serveralias["name"]}\n";
						}
						$vhost_more_conf .= "        ServerAlias $web_subname.${row_serveralias["name"]}\n";
						if ($domain_wildcard_dns == "yes") {
							$vhost_more_conf .= "        ServerAlias *.${row_serveralias["name"]}\n";
						}
					}

					// Sbox and safe mode protection values
					if($domain_safe_mode == "no" && $subdomain["safe_mode"] == "no"){
						$safe_mode_value = "0";
					}else{
						$safe_mode_value = "1";
					}
					if($domain_sbox_protect == "no" && $subdomain["sbox_protect"] == "no"){
						$cgi_directive = "ScriptAlias /cgi-bin $web_path/$domain_to_get/subdomains/$web_subname/cgi-bin";
					}else{
						$cgi_directive = "";
//						$cgi_directive = "RewriteEngine on
//	RewriteRule ^/cgi-bin/(.*) /cgi-bin/sbox/$1 [PT]";
					}
					$gen_iterations = sizeof($iteration_table);
					for ($k = 0; $k < $gen_iterations; $k++){
						switch($iteration_table[$k]){
						case "backup":
							$vhost_file .= "<VirtualHost ".$backup_ip_addr.":80>\n";
							break;
						case "normal":
							$vhost_file .= "<VirtualHost ".$ip_to_write.":80>\n";
							break;
						case "ssl":
							//if($conf_use_nated_vhost=="no"){
							//	$vhost_file .= "Listen ".$ip_vhost.":$port\n";
							//}
							$vhost_file .= "Listen ".$subdomain["ssl_ip"].":$port\n";
							$vhost_file .= "<VirtualHost ".$subdomain["ssl_ip"].":$port>\n";
							$vhost_file .= "	SSLEngine on\n";
							$vhost_file .= "	SSLCertificateFile $ssl_cert_folder_path/".$web_subname.".".$domain_to_get.".cert.cert\n";
							$vhost_file .= "	SSLCertificateKeyFile $ssl_cert_folder_path/".$web_subname.".".$domain_to_get.".cert.key\n";
							if (file_exists("$ssl_cert_folder_path/".$web_subname.".".$domain_to_get.".cert.ca")) {
								$vhost_file .= "	SSLCertificateChainFile $ssl_cert_folder_path/".$web_subname.".".$domain_to_get.".cert.ca\n";
							}
							break;
						case "shared_ssl":
							$vhost_file .= "<VirtualHost ".$ip_to_write.":443>\n";
							$vhost_file .= "	SSLEngine on\n";
							$vhost_file .= "	SSLCertificateFile ".$conf_generated_file_path."/ssl/new.cert.cert\n";
							$vhost_file .= "	SSLCertificateKeyFile ".$conf_generated_file_path."/ssl/new.cert.key\n";
							if (file_exists($conf_generated_file_path."/ssl/new.cert.ca")) {
								$vhost_file .= "	SSLCertificateChainFile ".$conf_generated_file_path."/ssl/new.cert.ca\n";
							}
							break;
						}
						$vhost_file .= "	ServerName $web_subname.$web_name
	Alias /stats $web_path/$web_name/subdomains/$web_subname/logs
	Alias /awstats-icon /usr/share/awstats/icon\n";
						// Disable the site if expired
						if($site_expired == "yes"){
							$document_root = $conf_generated_file_path."/expired_site";
							$vhost_file .= "	DocumentRoot $document_root
	<Directory $document_root>
		Allow from all
	</Directory>\n";
						}else{
							if($webadmin["shared_hosting_security"] == 'sbox_aufs' || ($webadmin["shared_hosting_security"] == 'mod_php' && $subdomain["shared_hosting_security"] == 'sbox_aufs')){
								$document_root = "$web_path/$domain_to_get/subdomains.aufs/$web_subname/html";
							}else{
								$document_root = "$web_path/$domain_to_get/subdomains/$web_subname/html";
							}

							$vhost_file .= "	DocumentRoot $document_root
	<Directory $document_root>
		Allow from all
	</Directory>\n";
							if($webadmin["shared_hosting_security"] == 'mod_php' && $shared_hosting_subdomain_security == 'mod_php'){
								if($php_vers_bigger_than_53){
									$my_php_safe_mode = "";
									$my_open_basedir = "";
								}else{
									$my_php_safe_mode = "\n\tphp_admin_value safe_mode $safe_mode_value";
									$my_open_basedir = "\n\t<Location />
		php_admin_value open_basedir \"$web_path:$conf_php_library_path:$conf_php_additional_library_path:\"\n\t</Location>";
								}
								$vhost_file .= $vhost_more_conf.$php_more_conf.$my_php_safe_mode."
	php_admin_value sendmail_from phpmailfunction$web_subname@$web_name
	php_admin_value sendmail_path \"/usr/sbin/sendmail -t -i -f phpmailfunction$web_subname@$domain_to_get\"
	php_value session.save_path $web_path/$domain_to_get/subdomains/$web_subname/tmp".$my_open_basedir."
	$cgi_directive\n";
							}else{
								$vhost_file .= "$vhost_more_conf	ScriptAlias /cgi-bin /usr/lib/cgi-bin
	php_admin_flag engine off
	AddHandler php-cgi-wrapper .php
	Action php-cgi-wrapper /cgi-bin/sbox
	AddHandler python-cgi-wrapper .py
	Action python-cgi-wrapper /cgi-bin/sbox
	AddHandler ruby-cgi-wrapper .rb
	Action ruby-cgi-wrapper /cgi-bin/sbox
	AddHandler ruby-cgi-wrapper .pl
	Action ruby-cgi-wrapper /cgi-bin/sbox
	ErrorDocument 404 /sbox404/404.php
	ErrorDocument 400 /sbox404/406.php
	ErrorDocument 406 /sbox404/406.php
	ErrorDocument 500 /sbox404/406.php
	ErrorDocument 501 /sbox404/406.php
	Options +ExecCGI\n";
							}
							$vhost_file .= get_defaultCharsetDirective($subdomain["add_default_charset"]);
						}
						$vhost_file .= "	ErrorLog $web_path/$web_name/subdomains/$web_subname/logs/error.log
	LogSQLTransferLogTable $log_tablename\$xfer
	LogSQLScoreDomain $web_name
	LogSQLScoreSubdomain $web_subname
	LogSQLScoreTable $conf_mysql_db.http_accounting
	DirectoryIndex $conf_apache_directoryindex$custom_directives
	<IfModule mod_security.c>
		SecUploadDir $web_path/$domain_to_get/subdomains/$web_subname/tmp
	</IfModule>
	<IfModule mod_cband.c>
		CBandUser $web_owner
		CBandRemoteSpeed 2Mbps 3 3
	</IfModule>\n";

						if ($create_mail_autoconfig && ($domain_default_sub_server_alias == "yes") && ($web_subname == "$web_default_subdomain")) {
							$vhost_file .= "	AliasMatch ^/.well-known/autoconfig/mail/config-v1.1.xml\$ $conf_generated_file_path/config-v1.1.xml\n";
						}
	
						$vhost_file .= "</VirtualHost>\n\n";
						$logrotate_file_chk[] = "$web_path/$web_name/subdomains/$web_subname/logs/error.log";
						//$logrotate_file .= "$web_path/$web_name/subdomains/$web_subname/logs/error.log ";
						$num_generated_vhosts += $num_rows2;
					}
				}
			}
	        }
	}

	// Writting the vhosts.conf file
	$filep = fopen("$conf_generated_file_path/$conf_apache_vhost_path", "w+");
	if( $filep == NULL){
		die("Cannot open $conf_generated_file_path/$conf_apache_vhost_path file for writting");
	}
	fwrite($filep,$vhost_file_start);
	fwrite($filep,$vhost_file_listen);
	fwrite($filep,$vhost_file);
	fclose($filep);
	$console .= "$num_generated_vhosts vhosts generated !<br>";

	// Writting the vhost_check_dir script
	$filep = fopen("$conf_generated_file_path/vhost_check_dir","w+");
	if( $filep == NULL){
		echo("Cannot open $conf_generated_file_path/vhost_check_dir file for writting");
	}else{
		fwrite($filep,$chk_dir_script);
		fclose($filep);
	}
	$console .= "vhost_check_dir.sh script written !<br>";

	// Writing the vhost_check_ssl_cert script
	$chk_certs_script .= "exit \$EXIT_VAL";
	$filep = fopen("$conf_generated_file_path/vhost_check_ssl_cert","w+");
	if( $filep == NULL){
		echo("Cannot open $conf_generated_file_path/vhost_check_ssl_cert file for writting");
	}else{
		fwrite($filep,$chk_certs_script);
		fclose($filep);
		chmod("$conf_generated_file_path/vhost_check_ssl_cert",0700);
	}
	$console .= "vhost_check_ssl_cert script written !<br>";

	// Writing $aufs_list list
	$filep = fopen("$conf_generated_file_path/aufs_list","w+");
	if( $filep == NULL){
		echo("Cannot open $conf_generated_file_path/aufs_lis file for writting");
	}else{
		fwrite($filep,$aufs_list);
		fclose($filep);
	}
	$console .= "aufs_list written !<br>";

	// Writing the logrotate configuration file
	if($logrotate_file != ""){
		$fname = "";
		if( file_exists("/etc/dtc/logrotate.template") ){
			$fname = "/etc/dtc/logrotate.template";
		}else if( file_exists("/usr/local/etc/dtc/logrotate.template") ){
			$fname = "/usr/local/etc/dtc/logrotate.template";
		}
		if($fname != ""){
			$fp = fopen($fname,"r");
			if($fp != NULL){
				$logrotate_template = fread($fp,filesize($fname));
				fclose($fp);
			}else{
				$logrotate_template = "";
			}
		}else{
			$logrotate_template = "";
		}
		$logrotate_file_checked = array_unique($logrotate_file_chk);
		foreach($logrotate_file_checked as $logrotate_entry){ 
			if(!empty($logrotate_entry)) 
				$logrotate_file .= $logrotate_entry." ";
		}
		$logrotate_file .= " {
$logrotate_template

	sharedscripts
";
		if($conf_apache_version == "2"){
			$logrotate_file .= "
	postrotate
		if [ -f /var/run/apache2.pid ]; then
			/etc/init.d/apache2 restart > /dev/null
		fi
	endscript
}
";
		}else{
			$logrotate_file .= "
	postrotate
		if [ -f /var/run/apache.pid ]; then \
			if [ -x /usr/sbin/invoke-rc.d ]; then \
				invoke-rc.d apache reload > /dev/null; \
			else \
				if [ -x /etc/init.d/apache ]; then \
					/etc/init.d/apache reload > /dev/null; \
				elif [ -x /etc/init.d/httpd ]; then \
					/etc/init.d/httpd reload > /dev/null; \
				fi; \
			fi; \
		fi;
	endscript
}
";
		}
		$filep = fopen("$conf_generated_file_path/logrotate","w+");
		if( $filep == NULL){
			echo ("Cannot open $conf_generated_file_path/logrotate for writting");
		}else{
			fwrite($filep,$logrotate_file);
			fclose($filep);
		}
		$console .= "logrotate config file generated!<br>";
	}
	return true;
}

?>