Project

General

Profile

Bug #941 ยป vhosts.inc

Moshe Katz, 10/10/2010 07:39 PM

 
1
<?php
2
/* $Id$ */
3
/*
4
/* ========================================================================== */
5
/*
6
    vhosts.inc
7
    Copyright (C) 2008 Mark J Crane
8
    All rights reserved.
9
                                                                              */
10
/* ========================================================================== */
11
/*
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14

    
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17

    
18
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21

    
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33

    
34
//show errors
35
	ini_set('display_errors', '1');
36
	//error_reporting (E_ALL); // Report everything
37
	//error_reporting (E_ALL ^ E_NOTICE); // Report everything
38
	error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ); //hide notices and warnings
39

    
40
function pkg_is_service_running($servicename) 
41
{
42
	exec("/bin/ps ax | awk '{ print $5 }'", $psout);
43
	array_shift($psout);
44
	foreach($psout as $line) {
45
		$ps[] = trim(array_pop(explode(' ', array_pop(explode('/', $line)))));
46
	}
47
	if(is_service_running($servicename, $ps) or is_process_running($servicename) ) {
48
		return true;
49
	}
50
	else {
51
		return false;
52
	}
53
}
54

    
55
function byte_convert( $bytes ) {
56
	if ($bytes<=0)
57
		return '0 Byte';
58

    
59
	$convention=1000; //[1000->10^x|1024->2^x]
60
	$s=array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
61
	$e=floor(log($bytes,$convention));
62
	return round($bytes/pow($convention,$e),2).' '.$s[$e];
63
}
64

    
65

    
66
//sort array
67
	function sort_host($a, $b){
68
		return strcmp($a["host"], $b["host"]);
69
	}
70

    
71
	function sort_port($a, $b){
72
		return strcmp($a["port"], $b["port"]);
73
	}
74

    
75
	function sort_ip_address($a, $b){
76
		return natsort($a["ipaddress"], $b["ipaddress"]);
77
	}
78

    
79

    
80
//check to see if the service is installed if it is return the id
81
	function get_service_id ($service_array, $fieldname, $fieldvalue) {
82
		$x = 0;
83
		$id = '';
84
		foreach($service_array as $rowhelper) {
85
			if ($rowhelper[$fieldname] == $fieldvalue) {
86
				$id = $x; //return the id
87
			}
88
			$x++;
89
		}
90
		if (strlen($id) > 0) {
91
			return ($id);
92
		}
93
		else {
94
			return false;
95
		}
96
	}
97

    
98

    
99
function vhosts_sync_package_php()
100
{
101
	global $config;
102

    
103
	if($config['installedpackages']['vhosts']['config'] != "") {
104

    
105
		conf_mount_rw();
106
		config_unlock();
107

    
108
		//sort the vhosts array 
109
			$vhostarray = $config['installedpackages']['vhosts']['config'];
110
			if (count(vhostarray) > 1) {
111
				usort($vhostarray, 'sort_ipaddress');
112
				usort($vhostarray, 'sort_host');
113
				usort($vhostarray, 'sort_port');
114
			}
115
			$vhostarray_http = '';
116
			$vhostarray_https = '';
117
			$x = 0;
118
			foreach($vhostarray as $rowhelper) {
119
				if ($rowhelper['enabled'] != "false") {
120
					if (strlen($rowhelper['certificate']) > 0 && strlen($rowhelper['privatekey']) > 0) {
121
						$vhostarray_https[$x]['host'] = $rowhelper['host'];
122
						$vhostarray_https[$x]['ipaddress'] = $rowhelper['ipaddress'];
123
						$vhostarray_https[$x]['port'] = $rowhelper['port'];
124
						$vhostarray_https[$x]['directory'] = $rowhelper['directory'];
125
						$vhostarray_https[$x]['certificate'] = $rowhelper['certificate'];
126
						$vhostarray_https[$x]['privatekey'] = $rowhelper['privatekey'];
127
						$vhostarray_https[$x]['enabled'] = $rowhelper['enabled'];
128
						$vhostarray_https[$x]['description'] = $rowhelper['description'];
129
					}
130
					else {
131
						$vhostarray_http[$x]['host'] = $rowhelper['host'];
132
						$vhostarray_http[$x]['ipaddress'] = $rowhelper['ipaddress'];
133
						$vhostarray_http[$x]['port'] = $rowhelper['port'];
134
						$vhostarray_http[$x]['directory'] = $rowhelper['directory'];
135
						$vhostarray_http[$x]['certificate'] = '';
136
						$vhostarray_http[$x]['privatekey'] = '';
137
						$vhostarray_http[$x]['enabled'] = $rowhelper['enabled'];
138
						$vhostarray_http[$x]['description'] = $rowhelper['description'];
139
						$server_port = $rowhelper['port'];
140
					}
141
					$x++;
142
				}
143
			}
144
			unset($x);
145

    
146
		//HTTP configuration
147
		if (count($vhostarray_http) > 0) {
148

    
149
			$tmp = "#\n";
150
			$tmp .= "# lighttpd configuration file\n";
151
			$tmp .= "#\n";
152
			$tmp .= "# use a it as base for lighttpd 1.0.0 and above\n";
153
			$tmp .= "#\n";
154
			$tmp .= "############ Options you really have to take care of ####################\n";
155
			$tmp .= "\n";
156
			$tmp .= "## FreeBSD!\n";
157
			$tmp .= "server.event-handler		= \"freebsd-kqueue\"\n";
158
			$tmp .= "server.network-backend		= \"writev\"  ## Fixes 7.x upload issues\n";
159
			$tmp .= "\n";
160
			$tmp .= "## modules to load\n";
161
			$tmp .= "server.modules =   (\n";
162
			$tmp .= "				  \"mod_access\", \"mod_accesslog\",\n";
163
			$tmp .= "                  \"mod_fastcgi\", \"mod_cgi\",\"mod_rewrite\"\n";
164
			$tmp .= "				)\n";
165
			$tmp .= "\n";
166
			$tmp .= "## Unused modules\n";
167
			$tmp .= "#                               \"mod_setenv\",\n";
168
			$tmp .= "#                               \"mod_compress\"\n";
169
			$tmp .= "#                               \"mod_redirect\",\n";
170
			$tmp .= "#                               \"mod_rewrite\",\n";
171
			$tmp .= "#                               \"mod_ssi\",\n";
172
			$tmp .= "#                               \"mod_usertrack\",\n";
173
			$tmp .= "#                               \"mod_expire\",\n";
174
			$tmp .= "#                               \"mod_secdownload\",\n";
175
			$tmp .= "#                               \"mod_rrdtool\",\n";
176
			$tmp .= "#                               \"mod_auth\",\n";
177
			$tmp .= "#                               \"mod_status\",\n";
178
			$tmp .= "#                               \"mod_alias\",\n";
179
			$tmp .= "#                               \"mod_proxy\",\n";
180
			$tmp .= "#                               \"mod_simple_vhost\",\n";
181
			$tmp .= "#                               \"mod_evhost\",\n";
182
			$tmp .= "#                               \"mod_userdir\",\n";
183
			$tmp .= "#                               \"mod_cgi\",\n";
184
			$tmp .= "#                                \"mod_accesslog\"\n";
185
			$tmp .= "\n";
186
			$tmp .= "## a static document-root, for virtual-hosting take look at the\n";
187
			$tmp .= "## server.virtual-* options\n";
188
			$tmp .= "server.document-root        = \"/usr/local/vhosts/\"\n";
189
			$tmp .= "\n";
190
			$tmp .= "# Maximum idle time with nothing being written (php downloading)\n";
191
			$tmp .= "server.max-write-idle = 999\n";
192
			$tmp .= "\n";
193
			$tmp .= "## where to send error-messages to\n";
194
			$tmp .= "server.errorlog             = \"/var/log/lighttpd.error.log\"\n";
195
			$tmp .= "\n";
196
			$tmp .= "# files to check for if .../ is requested\n";
197
			$tmp .= "server.indexfiles           = ( \"index.php\", \"index.html\",\n";
198
			$tmp .= "                                \"index.htm\", \"default.htm\" )\n";
199
			$tmp .= "\n";
200
			$tmp .= "# mimetype mapping\n";
201
			$tmp .= "mimetype.assign             = (\n";
202
			$tmp .= "  \".pdf\"          =>      \"application/pdf\",\n";
203
			$tmp .= "  \".sig\"          =>      \"application/pgp-signature\",\n";
204
			$tmp .= "  \".spl\"          =>      \"application/futuresplash\",\n";
205
			$tmp .= "  \".class\"        =>      \"application/octet-stream\",\n";
206
			$tmp .= "  \".ps\"           =>      \"application/postscript\",\n";
207
			$tmp .= "  \".torrent\"      =>      \"application/x-bittorrent\",\n";
208
			$tmp .= "  \".dvi\"          =>      \"application/x-dvi\",\n";
209
			$tmp .= "  \".gz\"           =>      \"application/x-gzip\",\n";
210
			$tmp .= "  \".pac\"          =>      \"application/x-ns-proxy-autoconfig\",\n";
211
			$tmp .= "  \".swf\"          =>      \"application/x-shockwave-flash\",\n";
212
			$tmp .= "  \".tar.gz\"       =>      \"application/x-tgz\",\n";
213
			$tmp .= "  \".tgz\"          =>      \"application/x-tgz\",\n";
214
			$tmp .= "  \".tar\"          =>      \"application/x-tar\",\n";
215
			$tmp .= "  \".zip\"          =>      \"application/zip\",\n";
216
			$tmp .= "  \".mp3\"          =>      \"audio/mpeg\",\n";
217
			$tmp .= "  \".m3u\"          =>      \"audio/x-mpegurl\",\n";
218
			$tmp .= "  \".wma\"          =>      \"audio/x-ms-wma\",\n";
219
			$tmp .= "  \".wax\"          =>      \"audio/x-ms-wax\",\n";
220
			$tmp .= "  \".ogg\"          =>      \"audio/x-wav\",\n";
221
			$tmp .= "  \".wav\"          =>      \"audio/x-wav\",\n";
222
			$tmp .= "  \".gif\"          =>      \"image/gif\",\n";
223
			$tmp .= "  \".jpg\"          =>      \"image/jpeg\",\n";
224
			$tmp .= "  \".jpeg\"         =>      \"image/jpeg\",\n";
225
			$tmp .= "  \".png\"          =>      \"image/png\",\n";
226
			$tmp .= "  \".xbm\"          =>      \"image/x-xbitmap\",\n";
227
			$tmp .= "  \".xpm\"          =>      \"image/x-xpixmap\",\n";
228
			$tmp .= "  \".xwd\"          =>      \"image/x-xwindowdump\",\n";
229
			$tmp .= "  \".css\"          =>      \"text/css\",\n";
230
			$tmp .= "  \".html\"         =>      \"text/html\",\n";
231
			$tmp .= "  \".htm\"          =>      \"text/html\",\n";
232
			$tmp .= "  \".js\"           =>      \"text/javascript\",\n";
233
			$tmp .= "  \".asc\"          =>      \"text/plain\",\n";
234
			$tmp .= "  \".c\"            =>      \"text/plain\",\n";
235
			$tmp .= "  \".conf\"         =>      \"text/plain\",\n";
236
			$tmp .= "  \".text\"         =>      \"text/plain\",\n";
237
			$tmp .= "  \".txt\"          =>      \"text/plain\",\n";
238
			$tmp .= "  \".dtd\"          =>      \"text/xml\",\n";
239
			$tmp .= "  \".xml\"          =>      \"text/xml\",\n";
240
			$tmp .= "  \".mpeg\"         =>      \"video/mpeg\",\n";
241
			$tmp .= "  \".mpg\"          =>      \"video/mpeg\",\n";
242
			$tmp .= "  \".mov\"          =>      \"video/quicktime\",\n";
243
			$tmp .= "  \".qt\"           =>      \"video/quicktime\",\n";
244
			$tmp .= "  \".avi\"          =>      \"video/x-msvideo\",\n";
245
			$tmp .= "  \".asf\"          =>      \"video/x-ms-asf\",\n";
246
			$tmp .= "  \".asx\"          =>      \"video/x-ms-asf\",\n";
247
			$tmp .= "  \".wmv\"          =>      \"video/x-ms-wmv\",\n";
248
			$tmp .= "  \".bz2\"          =>      \"application/x-bzip\",\n";
249
			$tmp .= "  \".tbz\"          =>      \"application/x-bzip-compressed-tar\",\n";
250
			$tmp .= "  \".tar.bz2\"      =>      \"application/x-bzip-compressed-tar\"\n";
251
			$tmp .= " )\n";
252
			$tmp .= "\n";
253
			$tmp .= "# Use the \"Content-Type\" extended attribute to obtain mime type if possible\n";
254
			$tmp .= "#mimetypes.use-xattr        = \"enable\"\n";
255
			$tmp .= "\n";
256
			$tmp .= "#### accesslog module\n";
257
			$tmp .= "#accesslog.filename          = \"/dev/null\"\n";
258
			$tmp .= "\n";
259
			$tmp .= "## deny access the file-extensions\n";
260
			$tmp .= "#\n";
261
			$tmp .= "# ~    is for backupfiles from vi, emacs, joe, ...\n";
262
			$tmp .= "# .inc is often used for code includes which should in general not be part\n";
263
			$tmp .= "#      of the document-root\n";
264
			$tmp .= "url.access-deny             = ( \"~\", \".db\" )\n";
265
			$tmp .= "\n";
266
			$tmp .= "\n";
267
			$tmp .= "######### Options that are good to be but not neccesary to be changed #######\n";
268
			$tmp .= "\n";
269
			$tmp .= "## bind to port (default: 80)\n";
270
			$tmp .= "server.port                = $server_port\n";
271
			$tmp .= "\n";
272
			$tmp .= "#the regex method isn't working\n";
273
			$tmp .= "#\$HTTP[\"host\"] =~ \"(^|\\.)host01\\.com\$\" {\n";
274
			$tmp .= "#	server.document-root        = \"/usr/local/vhosts/host01\"\n";
275
			$tmp .= "#}\n";
276
			$tmp .= "\n";
277

    
278
			$ipaddress_previous_value = '';
279
			$host_previous_value = '';
280
			$port_previous_value = '';
281
			$ipaddress_and_port_previous_value = '';
282
			$x=1;
283
			foreach($vhostarray_http as $rowhelper) {
284
				if ($rowhelper['enabled'] != "false") {
285

    
286
					$host = $rowhelper['host'];
287
					$ipaddress = $rowhelper['ipaddress'];
288
					$port = $rowhelper['port'];
289
					$directory = $rowhelper['directory'];
290
					if (strlen($rowhelper['certificate']) > 0) {
291
						$certificate = base64_decode($rowhelper['certificate']);
292
					}
293
					if (strlen($rowhelper['privatekey']) > 0) {
294
						$privatekey = base64_decode($rowhelper['privatekey']);
295
					}
296

    
297
					//set directory default to the host
298
						if (strlen($directory) == 0) { $directory = $host; }
299

    
300
					//if the vhost directory doesn't exist then create it
301
						//echo '/usr/local/vhosts/'.$directory.'<br />';
302
						if (!is_dir('/usr/local/vhosts/'.$directory)) {
303
							exec("mkdir /usr/local/vhosts/".$directory);
304
						}
305
						if (!file_exists('/usr/local/vhosts/'.$directory.'/index.php')) {
306
							$index_file = '/usr/local/vhosts/'.$directory.'/index.php';
307
							$index_tmp = "<?php\n";
308
							$index_tmp .= "	echo phpinfo();\n";
309
							$index_tmp .= "?>\n";
310
							$fout = fopen($index_file,"w");
311
							fwrite($fout, $index_tmp);
312
							fclose($fout);
313
							unset($index_file);
314
						}
315

    
316
					//set the default port
317
						if (strlen($port) == 0) { $port = '8001'; }
318

    
319
					if ($ipaddress.':'.$port != $ipaddress_and_port_previous_value) {
320
						if ($x > 1) { $tmp .= "}\n\n"; }
321
						$tmp .= "\$SERVER[\"socket\"] == \"".$ipaddress.":".$port."\" {\n";
322
					}
323

    
324
					$tmp .= "	\$HTTP[\"host\"] == \"".$host."\" {\n";
325
					$tmp .= "		server.document-root        = \"/usr/local/vhosts/".$directory."\"\n";
326

    
327
					//enable ssl if the cert and key were both provided
328
					if (strlen($rowhelper['certificate']) > 0 && strlen($rowhelper['privatekey']) > 0) {
329
						$pem_file = "/var/etc/cert-vhosts-".$ipaddress."-".$port.".pem";
330
						$fout = fopen($pem_file,"w");
331
						fwrite($fout, $certificate.$privatekey);
332
						fclose($fout);
333
						$tmp .= "		ssl.pemfile = \"".$pem_file."\"\n";
334
						$tmp .= "		ssl.engine = \"enable\"\n";
335
						unset($pem_file);
336
					}
337
					if (count($vhostarray_http) > 0) {
338
						$tmp .= "	}\n";
339
					}
340

    
341
					$ipaddress_previous_value = $ipaddress;
342
					$host_previous_value = $host;
343
					$port_previous_value = $port;
344
					$ipaddress_and_port_previous_value = $ipaddress.':'.$port;
345
					$x++;
346
				}
347
			}
348
			$tmp .= "}\n";
349

    
350

    
351
			/*
352
			$tmp .= "\$SERVER[\"socket\"] == \"10.7.0.1:8001\" {\n";
353
			$tmp .= "	\$HTTP[\"host\"] == \"vhost01.com\" {\n";
354
			$tmp .= "		#ssl.pemfile = \"/var/www/certs/localhost.pem\"\n";
355
			$tmp .= "		#ssl.engine = \"enable\"\n";
356
			$tmp .= "		server.document-root        = \"/usr/local/vhosts/host01\"\n";
357
			$tmp .= "		#server.errorlog = \"/var/log/lighttpd/nixcraft/error.log\"\n";
358
			$tmp .= "		#accesslog.filename = \"/var/log/lighttpd/nixcraft/access.log\"\n";
359
			$tmp .= "		#server.error-handler-404 = \"/e404.php\"\n";
360
			$tmp .= "	}\n";
361
			$tmp .= "\n";
362
			$tmp .= "	\$HTTP[\"host\"] == \"vhost03.com\" {\n";
363
			$tmp .= "		#ssl.pemfile = \"/var/www/certs/localhost.pem\"\n";
364
			$tmp .= "		#ssl.engine = \"enable\"\n";
365
			$tmp .= "		server.document-root        = \"/usr/local/vhosts/host03\"\n";
366
			$tmp .= "		#server.errorlog = \"/var/log/lighttpd/nixcraft/error.log\"\n";
367
			$tmp .= "		#accesslog.filename = \"/var/log/lighttpd/nixcraft/access.log\"\n";
368
			$tmp .= "		#server.error-handler-404 = \"/e404.php\"\n";
369
			$tmp .= "	}\n";
370
			$tmp .= "}\n";
371
			$tmp .= "\n";
372
			$tmp .= "\$SERVER[\"socket\"] == \"10.7.0.1:8002\" {\n";
373
			$tmp .= "	\$HTTP[\"host\"] == \"vhost02.com\" {\n";
374
			$tmp .= "		#ssl.pemfile = \"/var/www/certs/localhost.pem\"\n";
375
			$tmp .= "		#ssl.engine = \"enable\"\n";
376
			$tmp .= "		server.document-root = \"/usr/local/vhosts/host02\"\n";
377
			$tmp .= "		#server.errorlog = \"/var/log/lighttpd/nixcraft/error.log\"\n";
378
			$tmp .= "		#accesslog.filename = \"/var/log/lighttpd/nixcraft/access.log\"\n";
379
			$tmp .= "		#server.error-handler-404 = \"/e404.php\"\n";
380
			$tmp .= "	}\n";
381
			$tmp .= "}\n";
382
			*/
383
			$tmp .= "\n";
384
			$tmp .= "## error-handler for status 404\n";
385
			$tmp .= "#server.error-handler-404   = \"/error-handler.html\"\n";
386
			$tmp .= "#server.error-handler-404   = \"/error-handler.php\"\n";
387
			$tmp .= "\n";
388
			$tmp .= "## to help the rc.scripts\n";
389
			$tmp .= "server.pid-file            = \"/var/run/vhosts-http.pid\"\n";
390
			$tmp .= "\n";
391
			$tmp .= "## virtual directory listings\n";
392
			$tmp .= "server.dir-listing         = \"disable\"\n";
393
			$tmp .= "\n";
394
			$tmp .= "## enable debugging\n";
395
			$tmp .= "debug.log-request-header   = \"disable\"\n";
396
			$tmp .= "debug.log-response-header  = \"disable\"\n";
397
			$tmp .= "debug.log-request-handling = \"disable\"\n";
398
			$tmp .= "debug.log-file-not-found   = \"disable\"\n";
399
			$tmp .= "\n";
400
			$tmp .= "#### compress module\n";
401
			$tmp .= "#compress.cache-dir         = \"/tmp/lighttpd/cache/compress/\"\n";
402
			$tmp .= "#compress.filetype          = (\"text/plain\", \"text/html\")\n";
403
			$tmp .= "\n";
404
			$tmp .= "#server.network-backend = \"writev\"\n";
405
			$tmp .= "server.upload-dirs = ( \"/root/\", \"/tmp/\", \"/var/\" )\n";
406
			$tmp .= "server.max-request-size    = 2097152\n";
407
			$tmp .= "\n";
408
			$tmp .= "#### fastcgi module\n";
409
			$tmp .= "## read fastcgi.txt for more info\n";
410
			$tmp .= "fastcgi.server = ( \".php\" =>\n";
411
			$tmp .= "	( \"localhost\" =>\n";
412
			$tmp .= "		(\n";
413
			$tmp .= "			\"socket\" => \"/tmp/php5-fastcgi.socket\",\n";
414
			$tmp .= "			\"min-procs\" => 1,\n";
415
			$tmp .= "			\"max-procs\" => 1,\n";
416
			$tmp .= "			\"idle-timeout\" => 0,\n";
417
			$tmp .= "			\"bin-environment\" => (\n";
418
			$tmp .= "				\"PHP_FCGI_MAX_REQUESTS\" => \"500\",\n";
419
			$tmp .= "				\"PHP_FCGI_CHILDREN\" => \"1\"\n";
420
			$tmp .= "			),\n";
421
			$tmp .= "			\"bin-path\" => \"/usr/local/php5/php-cgi\"\n";
422
			$tmp .= "		)\n";
423
			$tmp .= "	)\n";
424
			$tmp .= ")\n";
425
			$tmp .= "\n";
426
			$tmp .= "#### CGI module\n";
427
			$tmp .= "cgi.assign                 = ( \".cgi\" => \"\" )\n";
428

    
429
			$fout = fopen("/var/etc/vhosts-http.conf","w");
430
			fwrite($fout, $tmp);
431
			unset($tmp);
432
			fclose($fout);
433
		} //end (if count(vhostarray_http) > 0)
434

    
435
		//HTTPS configuration
436
		$ipaddress_previous_value = '';
437
		$host_previous_value = '';
438
		$port_previous_value = '';
439
		$ipaddress_and_port_previous_value = '';
440
		$x=1;
441
		if (count($vhostarray_https) > 0) {
442
			foreach($vhostarray_https as $rowhelper) {
443
				if ($rowhelper['enabled'] != "false") {
444
					$host = $rowhelper['host'];
445
					$ipaddress = $rowhelper['ipaddress'];
446
					$port = $rowhelper['port'];
447
					$directory = $rowhelper['directory'];
448
					$description = $rowhelper['description'];
449
					if (strlen($rowhelper['certificate']) > 0) {
450
						$certificate = base64_decode($rowhelper['certificate']);
451
					}
452
					if (strlen($rowhelper['privatekey']) > 0) {
453
						$privatekey = base64_decode($rowhelper['privatekey']);
454
					}
455

    
456
					//set directory default to the host
457
						if (strlen($directory) == 0) { $directory = $host; }
458

    
459
					//if the vhost directory doesn't exist then create it
460
						//echo '/usr/local/vhosts/'.$directory.'<br />';
461
						if (!is_dir('/usr/local/vhosts/'.$directory)) {
462
							exec("mkdir /usr/local/vhosts/".$directory);
463
						}
464
						if (!file_exists('/usr/local/vhosts/'.$directory.'/index.php')) {
465
							$index_file = '/usr/local/vhosts/'.$directory.'/index.php';
466
							$index_tmp = "<?php\n";
467
							$index_tmp .= "	echo phpinfo();\n";
468
							$index_tmp .= "?>\n";
469
							$fout = fopen($index_file,"w");
470
							fwrite($fout, $index_tmp);
471
							fclose($fout);
472
							unset($index_file);
473
						}
474

    
475
					//set the default port
476
						if (strlen($port) == 0) { $port = '443'; }
477

    
478
					$tmp = "#\n";
479
					$tmp .= "# lighttpd configuration file\n";
480
					$tmp .= "#\n";
481
					$tmp .= "# use a it as base for lighttpd 1.0.0 and above\n";
482
					$tmp .= "#\n";
483
					$tmp .= "############ Options you really have to take care of ####################\n";
484
					$tmp .= "\n";
485
					$tmp .= "## FreeBSD!\n";
486
					$tmp .= "server.event-handler		= \"freebsd-kqueue\"\n";
487
					$tmp .= "server.network-backend		= \"writev\"  ## Fixes 7.x upload issues\n";
488
					$tmp .= "\n";
489
					$tmp .= "## modules to load\n";
490
					$tmp .= "server.modules =   (\n";
491
					$tmp .= "				  \"mod_accesslog\",\n";
492
					$tmp .= "				  \"mod_access\", \"mod_accesslog\",\n";
493
					$tmp .= "                  \"mod_fastcgi\", \"mod_cgi\",\"mod_rewrite\"\n";
494
					$tmp .= "				)\n";
495
					$tmp .= "\n";
496
					$tmp .= "## Unused modules\n";
497
					$tmp .= "#                               \"mod_setenv\",\n";
498
					$tmp .= "#                               \"mod_compress\"\n";
499
					$tmp .= "#                               \"mod_redirect\",\n";
500
					$tmp .= "#                               \"mod_rewrite\",\n";
501
					$tmp .= "#                               \"mod_ssi\",\n";
502
					$tmp .= "#                               \"mod_usertrack\",\n";
503
					$tmp .= "#                               \"mod_expire\",\n";
504
					$tmp .= "#                               \"mod_secdownload\",\n";
505
					$tmp .= "#                               \"mod_rrdtool\",\n";
506
					$tmp .= "#                               \"mod_auth\",\n";
507
					$tmp .= "#                               \"mod_status\",\n";
508
					$tmp .= "#                               \"mod_alias\",\n";
509
					$tmp .= "#                               \"mod_proxy\",\n";
510
					$tmp .= "#                               \"mod_simple_vhost\",\n";
511
					$tmp .= "#                               \"mod_evhost\",\n";
512
					$tmp .= "#                               \"mod_userdir\",\n";
513
					$tmp .= "#                               \"mod_cgi\",\n";
514
					$tmp .= "#                               \"mod_accesslog\"\n";
515
					$tmp .= "\n";
516
					$tmp .= "## a static document-root, for virtual-hosting take look at the\n";
517
					$tmp .= "## server.virtual-* options\n";
518
					$tmp .= "\n";
519
					$tmp .= "## ".$description." \n\n";
520
					$tmp .= "server.document-root        = \"/usr/local/vhosts/".$directory."\"\n";
521

    
522
					$tmp .= "\n";
523
					$tmp .= "# Maximum idle time with nothing being written (php downloading)\n";
524
					$tmp .= "server.max-write-idle = 999\n";
525
					$tmp .= "\n";
526
					$tmp .= "## where to send error-messages to\n";
527
					$tmp .= "server.errorlog             = \"/var/log/lighttpd.error.log\"\n";
528
					$tmp .= "\n";
529
					$tmp .= "# files to check for if .../ is requested\n";
530
					$tmp .= "server.indexfiles           = ( \"index.php\", \"index.html\",\n";
531
					$tmp .= "                                \"index.htm\", \"default.htm\" )\n";
532
					$tmp .= "\n";
533
					$tmp .= "# mimetype mapping\n";
534
					$tmp .= "mimetype.assign             = (\n";
535
					$tmp .= "  \".pdf\"          =>      \"application/pdf\",\n";
536
					$tmp .= "  \".sig\"          =>      \"application/pgp-signature\",\n";
537
					$tmp .= "  \".spl\"          =>      \"application/futuresplash\",\n";
538
					$tmp .= "  \".class\"        =>      \"application/octet-stream\",\n";
539
					$tmp .= "  \".ps\"           =>      \"application/postscript\",\n";
540
					$tmp .= "  \".torrent\"      =>      \"application/x-bittorrent\",\n";
541
					$tmp .= "  \".dvi\"          =>      \"application/x-dvi\",\n";
542
					$tmp .= "  \".gz\"           =>      \"application/x-gzip\",\n";
543
					$tmp .= "  \".pac\"          =>      \"application/x-ns-proxy-autoconfig\",\n";
544
					$tmp .= "  \".swf\"          =>      \"application/x-shockwave-flash\",\n";
545
					$tmp .= "  \".tar.gz\"       =>      \"application/x-tgz\",\n";
546
					$tmp .= "  \".tgz\"          =>      \"application/x-tgz\",\n";
547
					$tmp .= "  \".tar\"          =>      \"application/x-tar\",\n";
548
					$tmp .= "  \".zip\"          =>      \"application/zip\",\n";
549
					$tmp .= "  \".mp3\"          =>      \"audio/mpeg\",\n";
550
					$tmp .= "  \".m3u\"          =>      \"audio/x-mpegurl\",\n";
551
					$tmp .= "  \".wma\"          =>      \"audio/x-ms-wma\",\n";
552
					$tmp .= "  \".wax\"          =>      \"audio/x-ms-wax\",\n";
553
					$tmp .= "  \".ogg\"          =>      \"audio/x-wav\",\n";
554
					$tmp .= "  \".wav\"          =>      \"audio/x-wav\",\n";
555
					$tmp .= "  \".gif\"          =>      \"image/gif\",\n";
556
					$tmp .= "  \".jpg\"          =>      \"image/jpeg\",\n";
557
					$tmp .= "  \".jpeg\"         =>      \"image/jpeg\",\n";
558
					$tmp .= "  \".png\"          =>      \"image/png\",\n";
559
					$tmp .= "  \".xbm\"          =>      \"image/x-xbitmap\",\n";
560
					$tmp .= "  \".xpm\"          =>      \"image/x-xpixmap\",\n";
561
					$tmp .= "  \".xwd\"          =>      \"image/x-xwindowdump\",\n";
562
					$tmp .= "  \".css\"          =>      \"text/css\",\n";
563
					$tmp .= "  \".html\"         =>      \"text/html\",\n";
564
					$tmp .= "  \".htm\"          =>      \"text/html\",\n";
565
					$tmp .= "  \".js\"           =>      \"text/javascript\",\n";
566
					$tmp .= "  \".asc\"          =>      \"text/plain\",\n";
567
					$tmp .= "  \".c\"            =>      \"text/plain\",\n";
568
					$tmp .= "  \".conf\"         =>      \"text/plain\",\n";
569
					$tmp .= "  \".text\"         =>      \"text/plain\",\n";
570
					$tmp .= "  \".txt\"          =>      \"text/plain\",\n";
571
					$tmp .= "  \".dtd\"          =>      \"text/xml\",\n";
572
					$tmp .= "  \".xml\"          =>      \"text/xml\",\n";
573
					$tmp .= "  \".mpeg\"         =>      \"video/mpeg\",\n";
574
					$tmp .= "  \".mpg\"          =>      \"video/mpeg\",\n";
575
					$tmp .= "  \".mov\"          =>      \"video/quicktime\",\n";
576
					$tmp .= "  \".qt\"           =>      \"video/quicktime\",\n";
577
					$tmp .= "  \".avi\"          =>      \"video/x-msvideo\",\n";
578
					$tmp .= "  \".asf\"          =>      \"video/x-ms-asf\",\n";
579
					$tmp .= "  \".asx\"          =>      \"video/x-ms-asf\",\n";
580
					$tmp .= "  \".wmv\"          =>      \"video/x-ms-wmv\",\n";
581
					$tmp .= "  \".bz2\"          =>      \"application/x-bzip\",\n";
582
					$tmp .= "  \".tbz\"          =>      \"application/x-bzip-compressed-tar\",\n";
583
					$tmp .= "  \".tar.bz2\"      =>      \"application/x-bzip-compressed-tar\"\n";
584
					$tmp .= " )\n";
585
					$tmp .= "\n";
586
					$tmp .= "# Use the \"Content-Type\" extended attribute to obtain mime type if possible\n";
587
					$tmp .= "#mimetypes.use-xattr        = \"enable\"\n";
588
					$tmp .= "\n";
589
					$tmp .= "#### accesslog module\n";
590
					$tmp .= "#accesslog.filename          = \"/dev/null\"\n";
591
					$tmp .= "\n";
592
					$tmp .= "## deny access the file-extensions\n";
593
					$tmp .= "#\n";
594
					$tmp .= "# ~    is for backupfiles from vi, emacs, joe, ...\n";
595
					$tmp .= "# .inc is often used for code includes which should in general not be part\n";
596
					$tmp .= "#      of the document-root\n";
597
					$tmp .= "url.access-deny             = ( \"~\", \".db\" )\n";
598
					$tmp .= "\n";
599
					$tmp .= "\n";
600
					$tmp .= "######### Options that are good to be but not neccesary to be changed #######\n";
601
					$tmp .= "\n";
602
					$tmp .= "## bind to port (default: 80)\n";
603
					$tmp .= "server.port                = ".$port."\n";
604
					$tmp .= "\n";
605
					$tmp .= "#the regex method isn't working\n";
606
					$tmp .= "#\$HTTP[\"host\"] =~ \"(^|\\.)host01\\.com\$\" {\n";
607
					$tmp .= "#	server.document-root        = \"/usr/local/vhosts/host01\"\n";
608
					$tmp .= "#}\n";
609
					$tmp .= "\n";
610

    
611
					//enable ssl if the cert and key were both provided
612
						$pem_file = "/var/etc/cert-vhosts-".$ipaddress."-".$port.".pem";
613
						$fout = fopen($pem_file,"w");
614
						//echo $certificate; //exit;
615
						fwrite($fout, $certificate.$privatekey);
616
						fclose($fout);
617
						$tmp .= "## ssl configuration\n";
618
						$tmp .= "ssl.pemfile = \"".$pem_file."\"\n";
619
						$tmp .= "ssl.engine = \"enable\"\n";
620
						unset($pem_file);
621

    
622
					$tmp .= "\n";
623
					$tmp .= "## error-handler for status 404\n";
624
					$tmp .= "#server.error-handler-404   = \"/error-handler.html\"\n";
625
					$tmp .= "#server.error-handler-404   = \"/error-handler.php\"\n";
626
					$tmp .= "\n";
627
					$tmp .= "## to help the rc.scripts\n";
628
					$tmp .= "server.pid-file            = \"/var/run/vhosts-".$ipaddress."-".$port."-ssl.pid\"\n";
629
					$tmp .= "\n";
630
					$tmp .= "## virtual directory listings\n";
631
					$tmp .= "server.dir-listing         = \"disable\"\n";
632
					$tmp .= "\n";
633
					$tmp .= "## enable debugging\n";
634
					$tmp .= "debug.log-request-header   = \"disable\"\n";
635
					$tmp .= "debug.log-response-header  = \"disable\"\n";
636
					$tmp .= "debug.log-request-handling = \"disable\"\n";
637
					$tmp .= "debug.log-file-not-found   = \"disable\"\n";
638
					$tmp .= "\n";
639
					$tmp .= "#### compress module\n";
640
					$tmp .= "#compress.cache-dir         = \"/tmp/lighttpd/cache/compress/\"\n";
641
					$tmp .= "#compress.filetype          = (\"text/plain\", \"text/html\")\n";
642
					$tmp .= "\n";
643
					$tmp .= "#server.network-backend = \"writev\"\n";
644
					$tmp .= "server.upload-dirs = ( \"/root/\", \"/tmp/\", \"/var/\" )\n";
645
					$tmp .= "server.max-request-size    = 2097152\n";
646
					$tmp .= "\n";
647
					$tmp .= "#### fastcgi module\n";
648
					$tmp .= "## read fastcgi.txt for more info\n";
649
					$tmp .= "fastcgi.server = ( \".php\" =>\n";
650
					$tmp .= "	( \"localhost\" =>\n";
651
					$tmp .= "		(\n";
652
					$tmp .= "			\"socket\" => \"/tmp/php5-fastcgi.socket\",\n";
653
					$tmp .= "			\"min-procs\" => 1,\n";
654
					$tmp .= "			\"max-procs\" => 1,\n";
655
					$tmp .= "			\"idle-timeout\" => 0,\n";
656
					$tmp .= "			\"bin-environment\" => (\n";
657
					$tmp .= "				\"PHP_FCGI_MAX_REQUESTS\" => \"500\",\n";
658
					$tmp .= "				\"PHP_FCGI_CHILDREN\" => \"1\"\n";
659
					$tmp .= "			),\n";
660
					$tmp .= "			\"bin-path\" => \"/usr/local/php5/php-cgi\"\n";
661
					$tmp .= "		)\n";
662
					$tmp .= "	)\n";
663
					$tmp .= ")\n";
664
					$tmp .= "\n";
665
					$tmp .= "#### CGI module\n";
666
					$tmp .= "cgi.assign                 = ( \".cgi\" => \"\" )\n";
667

    
668
					$fout = fopen("/var/etc/vhosts-".$ipaddress."-".$port."-ssl.conf","w");
669
					fwrite($fout, $tmp);
670
					unset($tmp);
671
					fclose($fout);
672

    
673
					if (!function_exists('write_rcfile')) { require("/etc/inc/service-utils.inc"); }
674
					write_rcfile(array(
675
						"file" => "vhosts-".$ipaddress."-".$port."-ssl.sh",
676
						"start" => "/usr/local/sbin/lighttpd -f /var/etc/vhosts-".$ipaddress."-".$port."-ssl.conf",
677
						"stop" => "kill `cat /var/run/vhosts-".$ipaddress."-".$port."-ssl.pid`"
678
						)
679
					);
680

    
681
					//add or update a service
682
						$a_service   = &$config['installedpackages']['service'];
683
						$ent['name'] = "vhosts-ssl-$x";
684
						$ent['rcfile'] = "vhosts-".$ipaddress."-".$port."-ssl.sh";
685
						$ent['executable'] = "vhosts-".$ipaddress."-".$port."-ssl";
686
						$ent['description'] = "vHosts SSL, Host: $host, IP Address: ".$ipaddress.", port: ".$port." desc: ".$description;
687
						$a_service   = $config['installedpackages']['service'];
688
						$service_id = get_service_id ($a_service, 'name', "vhosts-ssl-$x");
689
						if (is_int($service_id)) {
690
							//update
691
							$a_service[$service_id] = $ent;
692
						}
693
						else {
694
							//add
695
							$a_service[] = $ent;
696
						}
697

    
698
				} //if enabled
699
				$x++;
700
			} //end for each
701
		} //end if array count
702
		write_config();
703
		conf_mount_ro();
704

    
705
	}
706
}
707

    
708

    
709
function php_sync_package() {
710
	global $config;   
711
	vhosts_sync_package_php();
712
}
713

    
714

    
715
function php_install_command() {
716

    
717
	global $config;
718
	conf_mount_rw();
719
	config_lock();
720

    
721
	if (!is_dir('/usr/local/www/packages/')) {
722
		exec("mkdir /usr/local/www/packages/");
723
	}
724

    
725
	if (!is_dir('/usr/local/www/packages/vhosts/')) {
726
		exec("mkdir /usr/local/www/packages/vhosts/");
727
	}
728

    
729
	if (!is_dir('/usr/local/vhosts/')) {
730
		exec("mkdir /usr/local/vhosts/");
731
	}
732

    
733
	if(stristr(php_uname('r'), '7.2') == TRUE) {
734
		if (!file_exists('/usr/local/php5')) {
735
			chdir('/usr/local/');
736
			exec ("fetch http://www.pfsense.com/packages/config/vhosts/freebsd7.2/php5.tar.gz");
737
			exec("tar zxvf /usr/local/php5.tar.gz -C /usr/local/");
738
			exec("rm /usr/local/php5.tar.gz");
739
		}
740
		if (!file_exists('/usr/local/lib/libxml2.so.5')) {
741
			chdir('/usr/local/lib/');
742
			exec ("fetch http://www.pfsense.com/packages/config/vhosts/freebsd7.2/usr.local.lib/libxml2.so.5");
743
		}
744
		if (!file_exists('/usr/local/lib/libxml2.so')) {
745
			chdir('/usr/local/lib/');
746
			exec ("fetch http://www.pfsense.com/packages/config/vhosts/freebsd7.2/usr.local.lib/libxml2.so");
747
		}
748
		if (!file_exists('/usr/local/lib/libxml2.la')) {
749
			chdir('/usr/local/lib/');
750
			exec ("fetch http://www.pfsense.com/packages/config/vhosts/freebsd7.2/usr.local.lib/libxml2.la");
751
		}
752
		if (!file_exists('/usr/local/lib/libxml2.a')) {
753
			chdir('/usr/local/lib/');
754
			exec ("fetch http://www.pfsense.com/packages/config/vhosts/freebsd7.2/usr.local.lib/lib/libxml2.a");
755
		}
756
	}
757
	if(stristr(php_uname('r'), '8.0') == TRUE) {
758
		if (!file_exists('/usr/local/php5')) {
759
			chdir('/usr/local/');
760
			exec ("fetch http://www.pfsense.com/packages/config/vhosts/freebsd8.0/php5.tar.gz");
761
			exec("tar zxvf /usr/local/php5.tar.gz -C /usr/local/");
762
			exec("rm /usr/local/php5.tar.gz");
763
		}
764
	}
765

    
766
	//rename PHP files from .tmp to .php
767
	exec("cp /tmp/vhosts_php.tmp /usr/local/www/packages/vhosts/vhosts_php.php");
768
	unlink_if_exists("/tmp/vhosts_php.tmp");
769

    
770
	exec("cp /tmp/vhosts_php_edit.tmp /usr/local/www/packages/vhosts/vhosts_php_edit.php");
771
	unlink_if_exists("/tmp/vhosts_php_edit.tmp");
772

    
773
	chdir('/tmp/');
774
	exec ("fetch http://www.pfsense.com/packages/config/vhosts/system_advanced_create_certs.tmp");
775
	exec("cp /tmp/system_advanced_create_certs.tmp /usr/local/www/packages/vhosts/system_advanced_create_certs.php");
776
	unlink_if_exists("/tmp/system_advanced_create_certs.tmp");
777

    
778
	//write_config();
779

    
780
	if (!function_exists('write_rcfile')) { require("/etc/inc/service-utils.inc"); }
781
	write_rcfile(array(
782
		"file" => "vhosts-http.sh",
783
		"start" => "/usr/local/sbin/lighttpd -f /var/etc/vhosts-http.conf",
784
		"stop" => "kill `cat /var/run/vhosts-http.pid`"
785
		)
786
	);
787

    
788
	php_sync_package();
789

    
790
	//if (pkg_is_service_running('vhosts')) {  
791
		//documentation purposes
792
	//}
793

    
794
	conf_mount_ro();
795
	config_unlock();
796
  
797
}
798

    
799

    
800
function deinstall_command() 
801
{
802
	conf_mount_rw();
803
	config_lock();
804
	$handle = popen("/usr/local/etc/rc.d/vhosts.sh stop", "r");
805
	unlink_if_exists("/usr/local/pkg/vhosts.xml");
806
	unlink_if_exists("/usr/local/pkg/vhosts.inc");
807
	unlink_if_exists("/usr/local/www/vhosts.inc");
808
	unlink_if_exists("/usr/local/etc/rc.d/vhosts-http.sh");
809
	exec ("rm /usr/local/etc/rc.d/vhosts*");
810
	exec ("rm /var/etc/vhosts*");
811
	exec("rm -R /usr/local/www/packages/vhosts");
812
	exec("rm -R /usr/local/php5");
813
	conf_mount_ro();
814
	config_unlock();
815
}
816

    
817
?>
    (1-1/1)