Project

General

Profile

Download (33.1 KB) Statistics
| Branch: | Tag: | Revision:
1 7b2274cb Scott Ullrich
<?php
2 d884b49c Colin Smith
/****h* pfSense/pfsense-utils
3
 * NAME
4
 *   pfsense-utils.inc - Utilities specific to pfSense
5
 * DESCRIPTION
6
 *   This include contains various pfSense specific functions.
7
 * HISTORY
8
 *   $Id$
9
 ******
10
 *
11
 * Copyright (C) 2005 Scott Ullrich (sullrich@gmail.com)
12
 * All rights reserved.
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *
16
 * 1. Redistributions of source code must retain the above copyright notice,
17
 * this list of conditions and the following disclaimer.
18
 *
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 * notice, this list of conditions and the following disclaimer in the
21
 * documentation and/or other materials provided with the distribution.
22 3a508fe2 Colin Smith
 *
23 d884b49c Colin Smith
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 */
35
36 b63f20c3 Colin Smith
function get_tmp_file() {
37
	return "/tmp/tmp-" . time();
38
}
39
40 ab8caecc Scott Ullrich
/****f* pfsense-utils/get_dns_servers
41
 * NAME
42
 *   get_dns_servres - get system dns servers
43
 * INPUTS
44
 *   $dns_servers - an array of the dns servers
45
 * RESULT
46
 *   null
47
 ******/
48
function get_dns_servers() {
49
	$dns_servers = array();
50
	$dns = `cat /etc/resolv.conf`;
51
	$dns_s = split("\n", $dns);
52
	foreach($dns_s as $dns) {
53
		if (preg_match("/nameserver (.*)/", $dns, $matches))
54
			$dns_servers[] = $matches[1];		
55
	}
56
	return $dns_servers;
57
}
58
59 2265659b Colin Smith
/****f* pfsense-utils/log_error
60
 * NAME
61 7e2779df Colin Smith
 *   log_error	- Sends a string to syslog.
62 25192408 Colin Smith
 * INPUTS
63
 *   $error	- string containing the syslog message.
64 2265659b Colin Smith
 * RESULT
65
 *   null
66
 ******/
67 7b2274cb Scott Ullrich
function log_error($error) {
68 98a8e831 Scott Ullrich
    syslog(LOG_WARNING, $error);
69 7b2274cb Scott Ullrich
    return;
70
}
71
72 94e8082f Scott Ullrich
/****f* pfsense-utils/get_interface_mac_address
73
 * NAME
74
 *   get_interface_mac_address - Return a interfaces mac address
75
 * INPUTS
76
 *   $interface	- interface to obtain mac address from
77
 * RESULT
78
 *   $mac - the mac address of the interface
79
 ******/
80
function get_interface_mac_address($interface) {
81 4d027492 Colin Smith
    $mac = exec("ifconfig {$interface} | awk '/ether/ {print $2}'");
82
    return trim($mac);
83 94e8082f Scott Ullrich
}
84
85 25192408 Colin Smith
/****f* pfsense-utils/return_dir_as_array
86
 * NAME
87
 *   return_dir_as_array - Return a directory's contents as an array.
88
 * INPUTS
89
 *   $dir	- string containing the path to the desired directory.
90
 * RESULT
91 775433e7 Colin Smith
 *   $dir_array - array containing the directory's contents. This array will be empty if the path specified is invalid.
92 8e362784 Colin Smith
 ******/
93 622fa35a Scott Ullrich
function return_dir_as_array($dir) {
94 174861fd Scott Ullrich
    $dir_array = array();
95 622fa35a Scott Ullrich
    if (is_dir($dir)) {
96 775433e7 Colin Smith
	if ($dh = opendir($dir)) {
97 622fa35a Scott Ullrich
	    while (($file = readdir($dh)) !== false) {
98 174861fd Scott Ullrich
		$canadd = 0;
99
		if($file == ".") $canadd = 1;
100
		if($file == "..") $canadd = 1;
101
		if($canadd == 0)
102
		    array_push($dir_array, $file);
103 622fa35a Scott Ullrich
	    }
104
	    closedir($dh);
105
	}
106
    }
107
    return $dir_array;
108
}
109 7b2274cb Scott Ullrich
110 775433e7 Colin Smith
/****f* pfsense-utils/enable_hardware_offloading
111
 * NAME
112
 *   enable_hardware_offloading - Enable a NIC's supported hardware features.
113
 * INPUTS
114
 *   $interface	- string containing the physical interface to work on.
115
 * RESULT
116
 *   null
117
 * NOTES
118
 *   This function only supports the fxp driver's loadable microcode.
119
 ******/
120 ed059571 Scott Ullrich
function enable_hardware_offloading($interface) {
121 31a82233 Scott Ullrich
    global $g;
122
    if($g['booting']) {
123 775433e7 Colin Smith
		$supported_ints = array('fxp');
124
		foreach($supported_ints as $int) {
125
			if(stristr($interface,$int) != false) {
126
		    	mwexec("/sbin/ifconfig $interface link0");
127
			}
128
		}
129 c9b4da10 Scott Ullrich
    }
130 6ac2dcdb Scott Ullrich
    mwexec("/sbin/ifconfig $interface txcsum 2>/dev/null");
131
    mwexec("/sbin/ifconfig $interface rxcsum 2>/dev/null");    
132 775433e7 Colin Smith
    return;
133 ed059571 Scott Ullrich
}
134
135 9f6b1429 Scott Ullrich
/****f* pfsense-utils/setup_microcode
136
 * NAME
137
 *   enumerates all interfaces and calls enable_hardware_offloading which
138
 *   enables a NIC's supported hardware features.
139
 * INPUTS
140
 *   
141
 * RESULT
142
 *   null
143
 * NOTES
144
 *   This function only supports the fxp driver's loadable microcode.
145
 ******/
146
function setup_microcode() {
147
   global $config;
148
    $ifdescrs = array('wan', 'lan');
149
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
150
	$ifdescrs['opt' . $j] = "opt" . $j;
151
    }
152
    foreach($ifdescrs as $if)
153
	enable_hardware_offloading($if);
154
}
155
156 775433e7 Colin Smith
/****f* pfsense-utils/return_filename_as_array
157
 * NAME
158
 *   return_filename_as_array - Return a file's contents as an array.
159
 * INPUTS
160 158dd870 Colin Smith
 *   $filename	- string containing the path to the desired file.
161
 *   $strip	- array of characters to strip - default is '#'.
162 775433e7 Colin Smith
 * RESULT
163 158dd870 Colin Smith
 *   $file	- array containing the file's contents.
164 775433e7 Colin Smith
 * NOTES
165
 *   This function strips lines starting with '#' and leading/trailing whitespace by default.
166
 ******/
167
function return_filename_as_array($filename, $strip = array('#')) {
168
    if(file_exists($filename)) $file = file($filename);
169
    if(is_array($file)) {
170 b99a7892 Scott Ullrich
	foreach($file as $line) $line = trim($line);
171 775433e7 Colin Smith
        foreach($strip as $tostrip) $file = preg_grep("/^{$tostrip}/", $file, PREG_GREP_INVERT);
172 98b77940 Scott Ullrich
    }
173 0dd62c88 Bill Marquette
    return $file;
174 3ff9d424 Scott Ullrich
}
175
176 e7f2ea12 Scott Ullrich
/****f* pfsense-utils/file_put_contents
177
 * NAME
178
 *   file_put_contents - Wrapper for file_put_contents if it doesn't exist
179
 * RESULT
180
 *   none
181
 ******/
182 5563a2c0 Scott Ullrich
if(!function_exists("file_put_contents")) {
183
    function file_put_contents($filename, $data) {
184
	$fd = fopen($filename,"w");
185
	fwrite($fd, $data);
186
	fclose($fd);
187
    }
188
}
189
190 158dd870 Colin Smith
/****f* pfsense-utils/get_carp_status
191
 * NAME
192
 *   get_carp_status - Return whether CARP is enabled or disabled.
193
 * RESULT
194
 *   boolean	- true if CARP is enabled, false if otherwise.
195
 ******/
196 b1174d02 Scott Ullrich
function get_carp_status() {
197
    /* grab the current status of carp */
198 1a97d17c Scott Ullrich
    $status = `/sbin/sysctl net.inet.carp.allow | cut -d" " -f2`;
199
    if(intval($status) == "0") return false;
200 b1174d02 Scott Ullrich
    return true;
201
}
202
203 158dd870 Colin Smith
/****f* pfsense-utils/return_filename_as_string
204
 * NAME
205
 *   return_filename_as_string - Return a file's contents as a string.
206
 * INPUTS
207
 *   $filename  - string containing the path to the desired file.
208
 * RESULT
209
 *   $tmp	- string containing the file's contents.
210
 ******/
211 023c3cc0 Scott Ullrich
function return_filename_as_string($filename) {
212 158dd870 Colin Smith
    if(file_exists($filename)) {
213
        return file_get_contents($filename);
214
    } else {
215
        return false;
216 c2c40105 Scott Ullrich
   } 
217 023c3cc0 Scott Ullrich
}
218
219 158dd870 Colin Smith
/****f* pfsense-utils/is_carp_defined
220
 * NAME
221
 *   is_carp_defined - Return whether CARP is detected in the kernel.
222
 * RESULT
223
 *   boolean	- true if CARP is detected, false otherwise.
224
 ******/
225 a8ac6c98 Scott Ullrich
function is_carp_defined() {
226 0f66804f Scott Ullrich
    /* is carp compiled into the kernel and userland? */
227
    $command = "/sbin/sysctl -a | grep carp";
228
    $fd = popen($command . " 2>&1 ", "r");
229
    if(!$fd) {
230 ef4550f8 Scott Ullrich
	log_error("Warning, could not execute command {$command}");
231
	return 0;
232 0f66804f Scott Ullrich
    }
233
    while(!feof($fd)) {
234 ef4550f8 Scott Ullrich
	$tmp .= fread($fd,49);
235 0f66804f Scott Ullrich
    }
236
    fclose($fd);
237 a8ac6c98 Scott Ullrich
238 0f66804f Scott Ullrich
    if($tmp == "")
239 ef4550f8 Scott Ullrich
	return false;
240 0f66804f Scott Ullrich
    else
241 ef4550f8 Scott Ullrich
	return true;
242 a8ac6c98 Scott Ullrich
}
243
244 158dd870 Colin Smith
/****f* pfsense-utils/find_number_of_created_carp_interfaces
245
 * NAME
246
 *   find_number_of_created_carp_interfaces - Return the number of CARP interfaces.
247
 * RESULT
248
 *   $tmp	- Number of currently created CARP interfaces.
249
 ******/
250 ed059571 Scott Ullrich
function find_number_of_created_carp_interfaces() {
251 0f66804f Scott Ullrich
    $command = "/sbin/ifconfig | /usr/bin/grep \"carp*:\" | /usr/bin/wc -l";
252
    $fd = popen($command . " 2>&1 ", "r");
253
    if(!$fd) {
254 ef4550f8 Scott Ullrich
	log_error("Warning, could not execute command {$command}");
255
	return 0;
256 0f66804f Scott Ullrich
    }
257
    while(!feof($fd)) {
258 ef4550f8 Scott Ullrich
	$tmp .= fread($fd,49);
259 0f66804f Scott Ullrich
    }
260
    fclose($fd);
261
    $tmp = intval($tmp);
262
    return $tmp;
263 ed059571 Scott Ullrich
}
264
265 158dd870 Colin Smith
/****f* pfsense-utils/link_ip_to_carp_interface
266
 * NAME
267
 *   link_ip_to_carp_interface - Find where a CARP interface links to.
268
 * INPUTS
269
 *   $ip
270
 * RESULT
271
 *   $carp_ints
272 0c84aff0 Colin Smith
 ******/
273 fa65a62b Scott Ullrich
function link_ip_to_carp_interface($ip) {
274 669e1adb Bill Marquette
	global $config;
275
	if($ip == "") return;
276 fa65a62b Scott Ullrich
277 669e1adb Bill Marquette
	$ifdescrs = array('wan', 'lan');
278
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
279
		$ifdescrs['opt' . $j] = "opt" . $j;
280
	}
281 fa65a62b Scott Ullrich
282 669e1adb Bill Marquette
	$ft = split("\.", $ip);
283
	$ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . ".";
284 0f66804f Scott Ullrich
285 669e1adb Bill Marquette
	$carp_ints = "";
286
	$num_carp_ints = find_number_of_created_carp_interfaces();
287
	foreach ($ifdescrs as $ifdescr => $ifname) {
288
		for($x=0; $x<$num_carp_ints; $x++) {
289
			$carp_int = "carp{$x}";
290
			$carp_ip = find_interface_ip($carp_int);
291
			$carp_ft = split("\.", $carp_ip);
292
			$carp_ft_ip = $carp_ft[0] . "." . $carp_ft[1] . "." . $carp_ft[2] . ".";
293
			$result = does_interface_exist($carp_int);
294
			if($result <> true) break;
295
			if($ft_ip == $carp_ft_ip)
296
			if(stristr($carp_ints,$carp_int) == false)
297
			$carp_ints .= " " . $carp_int;
298
		}
299 ef4550f8 Scott Ullrich
	}
300 669e1adb Bill Marquette
	return $carp_ints;
301 fa65a62b Scott Ullrich
}
302
303 158dd870 Colin Smith
/****f* pfsense-utils/exec_command
304
 * NAME
305
 *   exec_command - Execute a command and return a string of the result.
306
 * INPUTS
307
 *   $command	- String of the command to be executed.
308
 * RESULT
309
 *   String containing the command's result.
310
 * NOTES
311
 *   This function returns the command's stdout and stderr.
312
 ******/
313 5ccfea33 Scott Ullrich
function exec_command($command) {
314 158dd870 Colin Smith
    $output = array();
315
    exec($command . ' 2>&1 ', $output);
316
    return(implode("\n", $output));
317 5ccfea33 Scott Ullrich
}
318
319 fa65a62b Scott Ullrich
/*
320
 * does_interface_exist($interface): return true or false if a interface is detected.
321
 */
322
function does_interface_exist($interface) {
323
    $ints = exec_command("/sbin/ifconfig -l");
324 83661f77 Scott Ullrich
    if(stristr($ints, $interface) !== false)
325 fa65a62b Scott Ullrich
	return true;
326
    else
327
	return false;
328
}
329
330 5ccfea33 Scott Ullrich
/*
331
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
332
 */
333
function convert_ip_to_network_format($ip, $subnet) {
334
    $ipsplit = split('[.]', $ip);
335
    $string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
336
    return $string;
337
}
338
339 b04a6ca4 Scott Ullrich
/*
340
 * find_interface_ip($interface): return the interface ip (first found)
341
 */
342
function find_interface_ip($interface) {
343 4983ed8c Scott Ullrich
    if(does_interface_exist($interface) == false) return;
344 e3939e20 Scott Ullrich
    $ip = exec_command("/sbin/ifconfig {$interface} | /usr/bin/grep -w \"inet\" | /usr/bin/cut -d\" \" -f 2");
345 e67f187a Scott Ullrich
    $ip = str_replace("\n","",$ip);
346 b04a6ca4 Scott Ullrich
    return $ip;
347
}
348
349 3314bb92 Scott Ullrich
function guess_interface_from_ip($ipaddress) {
350
    $ints = `/sbin/ifconfig -l`;
351
    $ints_split = split(" ", $ints);
352
    $ip_subnet_split = split("\.", $ipaddress);
353
    $ip_subnet = $ip_subnet_split[0] . "." . $ip_subnet_split[1] . "." . $ip_subnet_split[2] . ".";
354
    foreach($ints_split as $int) {
355
        $ip = find_interface_ip($int);
356
        $ip_split = split("\.", $ip);
357
        $ip_tocheck = $ip_split[0] . "." . $ip_split[1] . "." . $ip_split[2] . ".";
358
        if(stristr($ip_tocheck, $ip_subnet) != false) return $int;
359
    }
360
}
361
362 fa65a62b Scott Ullrich
function filter_opt_interface_to_real($opt) {
363 0f66804f Scott Ullrich
    global $config;
364
    return $config['interfaces'][$opt]['if'];
365 fa65a62b Scott Ullrich
}
366
367
function filter_get_opt_interface_descr($opt) {
368 0f66804f Scott Ullrich
    global $config;
369
    return $config['interfaces'][$opt]['descr'];
370 fa65a62b Scott Ullrich
}
371
372 b73cc056 Scott Ullrich
function get_friendly_interface_list_as_array() {
373 0f66804f Scott Ullrich
    global $config;
374
    $ints = array();
375
    $ifdescrs = array('wan', 'lan');
376
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
377 669e1adb Bill Marquette
		$ifdescrs['opt' . $j] = "opt" . $j;
378 0f66804f Scott Ullrich
    }
379
    $ifdescrs = get_interface_list();
380
    foreach ($ifdescrs as $ifdescr => $ifname) {
381 669e1adb Bill Marquette
		array_push($ints,$ifdescr);
382 0f66804f Scott Ullrich
    }
383
    return $ints;
384 b73cc056 Scott Ullrich
}
385
386 5ccfea33 Scott Ullrich
/*
387
 * find_ip_interface($ip): return the interface where an ip is defined
388
 */
389
function find_ip_interface($ip) {
390 0f66804f Scott Ullrich
    global $config;
391
    $ifdescrs = array('wan', 'lan');
392
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
393 ef4550f8 Scott Ullrich
	$ifdescrs['opt' . $j] = "opt" . $j;
394 0f66804f Scott Ullrich
    }
395
    foreach ($ifdescrs as $ifdescr => $ifname) {
396
	$int = filter_translate_type_to_real_interface($ifname);
397
	$ifconfig = exec_command("/sbin/ifconfig {$int}");
398
	if(stristr($ifconfig,$ip) <> false)
399
	    return $int;
400
    }
401
    return false;
402 5ccfea33 Scott Ullrich
}
403
404 bc5d2a26 Scott Ullrich
/*
405
 *  filter_translate_type_to_real_interface($interface): returns the real interface name
406
 *                                                       for a friendly interface.  ie: wan
407
 */
408
function filter_translate_type_to_real_interface($interface) {
409 ef4550f8 Scott Ullrich
    global $config;
410
    return $config['interfaces'][$interface]['if'];
411 bc5d2a26 Scott Ullrich
}
412
413 591afdfd Scott Ullrich
/*
414
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
415
 */
416
function get_carp_interface_status($carpinterface) {
417 669e1adb Bill Marquette
	/* basically cache the contents of ifconfig statement
418
	to speed up this routine */
419
	global $carp_query;
420
	if($carp_query == "")
421 10655b3c Scott Ullrich
	$carp_query = split("\n", `/sbin/ifconfig | /usr/bin/grep carp`);
422 669e1adb Bill Marquette
	$found_interface = 0;
423
	foreach($carp_query as $int) {
424
		if($found_interface == 1) {
425
			if(stristr($int, "MASTER") == true) return "MASTER";
426
			if(stristr($int, "BACKUP") == true) return "BACKUP";
427
			if(stristr($int, "INIT") == true) return "INIT";
428
			return false;
429
		}
430
		if(stristr($int, $carpinterface) == true)
431
		$found_interface=1;
432 10655b3c Scott Ullrich
	}
433 669e1adb Bill Marquette
	return;
434 591afdfd Scott Ullrich
}
435
436 167bcf84 Scott Ullrich
/*
437
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
438
 */
439
function get_pfsync_interface_status($pfsyncinterface) {
440 962f7d25 Scott Ullrich
    $result = does_interface_exist($pfsyncinterface);
441 4983ed8c Scott Ullrich
    if($result <> true) return;
442 e3939e20 Scott Ullrich
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5");
443 167bcf84 Scott Ullrich
    return $status;
444
}
445
446 5ccfea33 Scott Ullrich
/*
447
 * find_carp_interface($ip): return the carp interface where an ip is defined
448
 */
449
function find_carp_interface($ip) {
450 d6caad17 Scott Ullrich
    global $find_carp_ifconfig;
451
    if($find_carp_ifconfig == "") {
452
	$find_carp_ifconfig = array();
453
	$num_carp_ints = find_number_of_created_carp_interfaces();
454
	for($x=0; $x<$num_carp_ints; $x++) {
455
	    $find_carp_ifconfig[$x] = exec_command("/sbin/ifconfig carp{$x}");
456
	}
457
    }
458
    $carps = 0;
459
    foreach($find_carp_ifconfig as $fci) {
460
	if(stristr($fci, $ip) == true)
461
	    return "carp{$carps}";
462
	$carps++;
463 5ccfea33 Scott Ullrich
    }
464
}
465
466 335978db Scott Ullrich
/*
467
 * find_number_of_created_bridges(): returns the number of currently created bridges
468
 */
469
function find_number_of_created_bridges() {
470
    return `/sbin/ifconfig | grep \"bridge[0-999]\:" | wc -l`;
471
}
472
473 5ccfea33 Scott Ullrich
/*
474
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
475
 */
476 a35f1242 Scott Ullrich
function add_rule_to_anchor($anchor, $rule, $label) {
477 8c8e6792 Scott Ullrich
    mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
478 5ccfea33 Scott Ullrich
}
479
480 06e2627e Scott Ullrich
/*
481
 * remove_text_from_file
482
 * remove $text from file $file
483
 */
484
function remove_text_from_file($file, $text) {
485
    global $fd_log;
486
    fwrite($fd_log, "Adding needed text items:\n");
487
    $filecontents = exec_command_and_return_text("cat " . $file);
488
    $textTMP = str_replace($text, "", $filecontents);
489
    $text .= $textTMP;
490
    fwrite($fd_log, $text . "\n");
491
    $fd = fopen($file, "w");
492
    fwrite($fd, $text);
493
    fclose($fd);
494
}
495
496
/*
497
 * add_text_to_file($file, $text): adds $text to $file.
498
 * replaces the text if it already exists.
499
 */
500
function add_text_to_file($file, $text) {
501 b63f20c3 Colin Smith
	if(file_exists($file) and is_writable($file)) {
502
		$filecontents = file($file);
503 ee3a5827 Colin Smith
		$filecontents[] = $text;
504 b63f20c3 Colin Smith
		$tmpfile = get_tmp_file();
505
		$fout = fopen($tmpfile, "w");
506
		foreach($filecontents as $line) {
507 ee3a5827 Colin Smith
			fwrite($fout, rtrim($line) . "\n");
508 b63f20c3 Colin Smith
		}
509 ee3a5827 Colin Smith
		fclose($fout);
510 b63f20c3 Colin Smith
		rename($tmpfile, $file);
511
		return true;
512
	} else {
513
		return false;
514
	}
515 06e2627e Scott Ullrich
}
516
517
/*
518
 * get_filename_from_url($url): converts a url to its filename.
519
 */
520 c2747f5a Colin Smith
521 06e2627e Scott Ullrich
function get_filename_from_url($url) {
522 c2747f5a Colin Smith
	$filenamesplit = split("/", $url);
523
	foreach($filenamesplit as $fn) $filename = $fn; 	 
524
	return $filename;
525 06e2627e Scott Ullrich
}
526
527
/*
528
 *   update_output_window: update bottom textarea dynamically.
529
 */
530
function update_output_window($text) {
531 0f66804f Scott Ullrich
    $log = ereg_replace("\n", "\\n", $text);
532
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
533 06e2627e Scott Ullrich
}
534
535
/*
536
 *   get_dir: return an array of $dir
537
 */
538
function get_dir($dir) {
539 0f66804f Scott Ullrich
    $dir_array = array();
540
    $d = dir($dir);
541
    while (false !== ($entry = $d->read())) {
542 ef4550f8 Scott Ullrich
	array_push($dir_array, $entry);
543 0f66804f Scott Ullrich
    }
544
    $d->close();
545
    return $dir_array;
546 06e2627e Scott Ullrich
}
547
548
/*
549
 *   update_output_window: update top textarea dynamically.
550
 */
551
function update_status($status) {
552 ef4550f8 Scott Ullrich
    echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
553 06e2627e Scott Ullrich
}
554
555
/*
556
 *   exec_command_and_return_text_array: execute command and return output
557
 */
558
function exec_command_and_return_text_array($command) {
559 669e1adb Bill Marquette
	$fd = popen($command . " 2>&1 ", "r");
560
	while(!feof($fd)) {
561
		$tmp .= fread($fd,49);
562
	}
563
	fclose($fd);
564
	$temp_array = split("\n", $tmp);
565
	return $temp_array;
566 06e2627e Scott Ullrich
}
567
568
/*
569
 *   exec_command_and_return_text: execute command and return output
570
 */
571
function exec_command_and_return_text($command) {
572 ef4550f8 Scott Ullrich
    return exec_command($command);
573 06e2627e Scott Ullrich
}
574
575
/*
576
 *   exec_command_and_return_text: execute command and update output window dynamically
577
 */
578
function execute_command_return_output($command) {
579
    global $fd_log;
580
    $fd = popen($command . " 2>&1 ", "r");
581
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
582
    $counter = 0;
583
    $counter2 = 0;
584
    while(!feof($fd)) {
585
	$tmp = fread($fd, 50);
586
	$tmp1 = ereg_replace("\n","\\n", $tmp);
587
	$text = ereg_replace("\"","'", $tmp1);
588
	if($lasttext == "..") {
589
	    $text = "";
590
	    $lasttext = "";
591
	    $counter=$counter-2;
592
	} else {
593
	    $lasttext .= $text;
594
	}
595
	if($counter > 51) {
596
	    $counter = 0;
597
	    $extrabreak = "\\n";
598
	} else {
599
	    $extrabreak = "";
600
	    $counter++;
601
	}
602
	if($counter2 > 600) {
603
	    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
604
	    $counter2 = 0;
605
	} else
606
	    $counter2++;
607
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
608
    }
609
    fclose($fd);
610
}
611
612 55be70e6 Scott Ullrich
/*
613
 * convert_friendly_interface_to_real_interface_name($interface): convert WAN to FXP0
614
 */
615
function convert_friendly_interface_to_real_interface_name($interface) {
616 54f4caed Scott Ullrich
    global $config;
617 a7f5febb Scott Ullrich
    $lc_interface = strtolower($interface);
618 303831c6 Scott Ullrich
    if($lc_interface == "lan") return $config['interfaces']['lan']['if'];
619
    if($lc_interface == "wan") return $config['interfaces']['wan']['if'];
620
    $ifdescrs = array();
621
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
622
	$ifdescrs['opt' . $j] = "opt" . $j;
623 401e59a9 Scott Ullrich
    foreach ($ifdescrs as $ifdescr => $ifname) {
624 303831c6 Scott Ullrich
	if(strtolower($ifname) == $lc_interface)
625
	    return $config['interfaces'][$ifname]['if'];
626
	if(strtolower($config['interfaces'][$ifname]['descr']) == $lc_interface)
627 401e59a9 Scott Ullrich
	    return $config['interfaces'][$ifname]['if'];
628
    }
629
    return $interface;
630 55be70e6 Scott Ullrich
}
631
632 cc869478 Scott Ullrich
/*
633
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
634
 */
635
function convert_real_interface_to_friendly_interface_name($interface) {
636 0f66804f Scott Ullrich
    global $config;
637
    $ifdescrs = array('wan', 'lan');
638
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
639
	$ifdescrs['opt' . $j] = "opt" . $j;
640
    foreach ($ifdescrs as $ifdescr => $ifname) {
641
	$int = filter_translate_type_to_real_interface($ifname);
642
	if($ifname == $interface) return $ifname;
643
	if($int == $interface) return $ifname;
644
    }
645
    return $interface;
646 cc869478 Scott Ullrich
}
647
648 06e2627e Scott Ullrich
/*
649
 * update_progress_bar($percent): updates the javascript driven progress bar.
650
 */
651
function update_progress_bar($percent) {
652 0f66804f Scott Ullrich
    if($percent > 100) $percent = 1;
653
    echo "\n<script type=\"text/javascript\" language=\"javascript\">";
654
    echo "\ndocument.progressbar.style.width='" . $percent . "%';";
655
    echo "\n</script>";
656 06e2627e Scott Ullrich
}
657
658 b45ea709 Scott Ullrich
/*
659 76177335 Scott Ullrich
 * gather_altq_queue_stats():  gather alq queue stats and return an array that
660 b45ea709 Scott Ullrich
 *                             is queuename|qlength|measured_packets
661 fbc24b62 Colin Smith
 *                             NOTE: this command takes 5 seconds to run
662 b45ea709 Scott Ullrich
 */
663 6a153733 Scott Ullrich
function gather_altq_queue_stats($dont_return_root_queues) {
664 e90bc39f Scott Ullrich
    mwexec("/usr/bin/killall -9 pfctl");
665 9c0ad35c Scott Ullrich
    $stats = `/sbin/pfctl -vvsq & /bin/sleep 5;/usr/bin/killall pfctl 2>/dev/null`;
666 b45ea709 Scott Ullrich
    $stats_array = split("\n", $stats);
667
    $queue_stats = array();
668
    foreach ($stats_array as $stats_line) {
669
        if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
670
            $queue_name = $match_array[1][0];
671 fccb044c Scott Ullrich
        if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
672
            $speed = $match_array[1][0];
673 e90bc39f Scott Ullrich
        if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
674
            $borrows = $match_array[1][0];
675 4bed294c Bill Marquette
        if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
676
            $suspends = $match_array[1][0];
677 04e6e7b7 Bill Marquette
        if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
678
            $drops = $match_array[1][0];
679 b45ea709 Scott Ullrich
        if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
680
            $measured = $match_array[1][0];
681 6a153733 Scott Ullrich
	    if($dont_return_root_queues == true)
682
		if(stristr($queue_name,"root_") == false)
683 122a9c39 Scott Ullrich
		    array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
684 b45ea709 Scott Ullrich
        }
685
    }
686
    return $queue_stats;
687
}
688 fa35df12 Scott Ullrich
689 fbc24b62 Colin Smith
/*
690 0ad0f98c Scott Ullrich
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
691 fbc24b62 Colin Smith
 *					 Useful for finding paths and stripping file extensions.
692
 */
693 29c3c942 Colin Smith
function reverse_strrchr($haystack, $needle)
694
{
695
               return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
696 fbc24b62 Colin Smith
}
697
698 f8891a0f Scott Ullrich
/*
699 832f1b83 Scott Ullrich
 *  backup_config_section($section): returns as an xml file string of
700
 *                                   the configuration section
701 f8891a0f Scott Ullrich
 */
702
function backup_config_section($section) {
703
    global $config;
704 6d501961 Scott Ullrich
    $new_section = &$config[$section];
705 832f1b83 Scott Ullrich
    /* generate configuration XML */
706
    $xmlconfig = dump_xml_config($new_section, $section);
707 8602b9f6 Scott Ullrich
    $xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
708 014beac3 Scott Ullrich
    return $xmlconfig;
709 f8891a0f Scott Ullrich
}
710
711
/*
712
 *  restore_config_section($section, new_contents): restore a configuration section,
713
 *                                                  and write the configuration out
714
 *                                                  to disk/cf.
715
 */
716
function restore_config_section($section, $new_contents) {
717
    global $config;
718 ee096757 Scott Ullrich
    conf_mount_rw();
719 832f1b83 Scott Ullrich
    $fout = fopen("{$g['tmp_path']}/tmpxml","w");
720 014beac3 Scott Ullrich
    fwrite($fout, $new_contents);
721 832f1b83 Scott Ullrich
    fclose($fout);
722 bb4f8f8a Colin Smith
    $section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section);
723 832f1b83 Scott Ullrich
    $config[$section] = &$section_xml;
724
    unlink($g['tmp_path'] . "/tmpxml");
725 782c32a7 Bill Marquette
    write_config("Restored {$section} of config file (maybe from CARP partner)");
726 ee096757 Scott Ullrich
    conf_mount_ro();
727 46624b94 Scott Ullrich
    return;
728 f8891a0f Scott Ullrich
}
729
730 e99d11e3 Scott Ullrich
/*
731 c70c4e8a Colin Smith
 * http_post($server, $port, $url, $vars): does an http post to a web server
732
 *                                         posting the vars array.
733 e99d11e3 Scott Ullrich
 * written by nf@bigpond.net.au
734
 */
735
function http_post($server, $port, $url, $vars) {
736 0f66804f Scott Ullrich
    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
737
    $urlencoded = "";
738
    while (list($key,$value) = each($vars))
739 ef4550f8 Scott Ullrich
	$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
740 0f66804f Scott Ullrich
    $urlencoded = substr($urlencoded,0,-1);
741 e99d11e3 Scott Ullrich
742 0f66804f Scott Ullrich
    $content_length = strlen($urlencoded);
743 e99d11e3 Scott Ullrich
744 0f66804f Scott Ullrich
    $headers = "POST $url HTTP/1.1
745 e99d11e3 Scott Ullrich
Accept: */*
746
Accept-Language: en-au
747
Content-Type: application/x-www-form-urlencoded
748
User-Agent: $user_agent
749
Host: $server
750
Connection: Keep-Alive
751
Cache-Control: no-cache
752
Content-Length: $content_length
753
754
";
755
756 0f66804f Scott Ullrich
    $fp = fsockopen($server, $port, $errno, $errstr);
757
    if (!$fp) {
758 ef4550f8 Scott Ullrich
	return false;
759 0f66804f Scott Ullrich
    }
760 e99d11e3 Scott Ullrich
761 0f66804f Scott Ullrich
    fputs($fp, $headers);
762
    fputs($fp, $urlencoded);
763 e99d11e3 Scott Ullrich
764 0f66804f Scott Ullrich
    $ret = "";
765
    while (!feof($fp))
766 ef4550f8 Scott Ullrich
	$ret.= fgets($fp, 1024);
767 e99d11e3 Scott Ullrich
768 0f66804f Scott Ullrich
    fclose($fp);
769 e99d11e3 Scott Ullrich
770 0f66804f Scott Ullrich
    return $ret;
771 e99d11e3 Scott Ullrich
772
}
773 f8891a0f Scott Ullrich
774 ee8f4a58 Scott Ullrich
/*
775
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
776
 */
777
if (!function_exists('php_check_syntax')){
778
   function php_check_syntax($code_to_check, &$errormessage){
779 c177d6ad Scott Ullrich
	return false;
780 7197df7d Scott Ullrich
        $fout = fopen("/tmp/codetocheck.php","w");
781
        $code = $_POST['content'];
782
        $code = str_replace("<?php", "", $code);
783
        $code = str_replace("?>", "", $code);
784
        fwrite($fout, "<?php\n\n");
785 669e1adb Bill Marquette
        fwrite($fout, $code_to_check);
786 7197df7d Scott Ullrich
        fwrite($fout, "\n\n?>\n");
787
        fclose($fout);
788
        $command = "/usr/local/bin/php -l /tmp/codetocheck.php";
789
        $output = exec_command($command);
790
        if (stristr($output, "Errors parsing") == false) {
791
            echo "false\n";
792
            $errormessage = '';
793
            return(false);
794
        } else {
795
            $errormessage = $output;
796
            return(true);
797
        }
798 ee8f4a58 Scott Ullrich
    }
799
}
800
801
/*
802
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
803
 */
804 7197df7d Scott Ullrich
if (!function_exists('php_check_syntax')){
805
   function php_check_syntax($code_to_check, &$errormessage){
806 c177d6ad Scott Ullrich
	return false;
807 7197df7d Scott Ullrich
        $command = "/usr/local/bin/php -l " . $code_to_check;
808
        $output = exec_command($command);
809
        if (stristr($output, "Errors parsing") == false) {
810
            echo "false\n";
811
            $errormessage = '';
812
            return(false);
813
        } else {
814
            $errormessage = $output;
815
            return(true);
816
        }
817
    }
818 ee8f4a58 Scott Ullrich
}
819
820 bebb9d4d Bill Marquette
/*
821 7eb2e498 Colin Smith
 * rmdir_recursive($path,$follow_links=false)
822 6f2e9d7f Bill Marquette
 * Recursively remove a directory tree (rm -rf path)
823
 * This is for directories _only_
824
 */
825
function rmdir_recursive($path,$follow_links=false) {
826 7eb2e498 Colin Smith
	$to_do = glob($path);
827 ed919b11 Colin Smith
	if(!is_array($to_do)) $to_do = array($to_do);
828
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
829
		if(file_exists($workingdir)) {
830
			if(is_dir($workingdir)) {
831 0fdf1eaa Colin Smith
				$dir = opendir($workingdir);
832
				while ($entry = readdir($dir)) {
833
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
834 ed919b11 Colin Smith
						unlink("$workingdir/$entry");
835 0fdf1eaa Colin Smith
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
836 ed919b11 Colin Smith
						rmdir_recursive("$workingdir/$entry");
837 0fdf1eaa Colin Smith
				}
838
				closedir($dir);
839
				rmdir($workingdir);
840 ed919b11 Colin Smith
			} elseif (is_file($workingdir)) {
841
				unlink($workingdir);
842
			}
843
               	}
844 6f2e9d7f Bill Marquette
	}
845 0a44a421 Colin Smith
	return;
846 6f2e9d7f Bill Marquette
}
847
848 1682dc1e Bill Marquette
/*
849
 * safe_mkdir($path, $mode = 0755)
850
 * create directory if it doesn't already exist and isn't a file!
851
 */
852
function safe_mkdir($path, $mode=0755) {
853 aaea2643 Scott Ullrich
	global $g;
854 29f9dd52 Scott Ullrich
855
	/* cdrom is ro. */
856 7316a702 Colin Smith
	if($g['platform'] == "cdrom")
857 3fb1a788 Scott Ullrich
		return false;
858 27273870 Colin Smith
	
859
	if (!is_file($path) && !is_dir($path))
860
		return mkdir($path, $mode);
861
	else
862
		return false;
863 1682dc1e Bill Marquette
}
864
865
/*
866
 * make_dirs($path, $mode = 0755)
867
 * create directory tree recursively (mkdir -p)
868
 */
869 3fb1a788 Scott Ullrich
function make_dirs($path, $mode = 0755) {
870 cc5ae6bc Scott Ullrich
	/* is dir already created? */
871
	if(is_dir($path)) return;
872
	/* create directory in question */
873 27273870 Colin Smith
	$to_create = explode("/", $path);
874 29f9dd52 Scott Ullrich
	foreach($to_create as $tc) 
875
	    if(!is_dir($tc))
876 27273870 Colin Smith
		safe_mkdir($path, $mode);
877 1682dc1e Bill Marquette
}
878
879 c7934653 Colin Smith
/*
880
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
881
 */
882 bd21e2b7 Colin Smith
function check_firmware_version($tocheck = "all", $return_php = true) {
883 335c7b2a Colin Smith
        global $g, $config;
884 bb4f8f8a Colin Smith
	$xmlrpc_base_url = $g['xmlrpcbaseurl'];
885
        $xmlrpc_path = $g['xmlrpcpath'];
886 bd21e2b7 Colin Smith
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
887 79020a5e Colin Smith
			"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
888
			"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
889
			"platform" => trim(file_get_contents('/etc/platform'))
890
		);
891 39a7c0c3 Bill Marquette
	if($tocheck == "all") {
892 bd21e2b7 Colin Smith
		$params = $rawparams;
893
	} else {
894
		foreach($tocheck as $check) {
895
			$params['check'] = $rawparams['check'];
896
			$params['platform'] = $rawparams['platform'];
897
		}
898
	}
899 335c7b2a Colin Smith
	if($config['system']['firmware']['branch']) {
900
		$params['branch'] = $config['system']['firmware']['branch'];
901 52621d32 Colin Smith
	}
902 79020a5e Colin Smith
	$xmlparams = php_value_to_xmlrpc($params);
903
        $msg = new XML_RPC_Message('pfsense.get_firmware_version', array($xmlparams));
904 bb4f8f8a Colin Smith
        $cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url);
905 335c7b2a Colin Smith
	//$cli->setDebug(1);
906 8ac0a4de Colin Smith
	$resp = $cli->send($msg, 10);
907
	if(!$resp or $resp->faultCode()) {
908
		$raw_versions = false;
909
	} else {
910
		$raw_versions = xmlrpc_value_to_php($resp->value());
911
		$raw_versions["current"] = $params;
912
	}
913 79020a5e Colin Smith
	return $raw_versions;
914 c7934653 Colin Smith
}
915
916 12eb7056 Colin Smith
function get_disk_info() {
917
        exec("df -h | grep -w '/' | awk '{ print $2, $3, $4, $5 }'", $diskout);
918
        return explode(' ', $diskout[0]);
919
        // $size, $used, $avail, $cap
920 64092c34 Scott Ullrich
}
921
922 90fd355f Bill Marquette
/****f* pfsense-utils/display_top_tabs
923
 * NAME
924
 *   display_top_tabs - display tabs with rounded edges
925
 * INPUTS
926
 *   $text	- array of tabs
927
 * RESULT
928
 *   null
929
 ******/
930 a8726a3d Scott Ullrich
    function display_top_tabs($tab_array) {
931
	    echo "<table cellpadding='0' cellspacing='0'>\n";
932
	    echo " <tr height='1'>\n";
933
	    $tabscounter = 0;
934
	    foreach ($tab_array as $ta) {
935
		    if($ta[1] == true) {
936 6948fa96 Bill Marquette
			    echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\"><div id='tabactive'></div></td>\n";
937 a8726a3d Scott Ullrich
		    } else {
938 6948fa96 Bill Marquette
			    echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\"><div id='tabdeactive{$tabscounter}'></div></td>\n";
939 a8726a3d Scott Ullrich
		    }
940
		    $tabscounter++;
941
	    }
942 0366b748 Scott Ullrich
	    echo "</tr>\n<tr>\n";
943 a8726a3d Scott Ullrich
	    foreach ($tab_array as $ta) {
944
		    if($ta[1] == true) {
945 6948fa96 Bill Marquette
			    echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\"><B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
946 63586b79 Scott Ullrich
			    echo "&nbsp;&nbsp;&nbsp;";
947 2f4809ea Bill Marquette
			    echo "<font size='-12'>&nbsp;</td>\n";
948 a8726a3d Scott Ullrich
		    } else {
949 6948fa96 Bill Marquette
			    echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\"><B>&nbsp;&nbsp;&nbsp;<a href='{$ta[2]}'>";
950 63586b79 Scott Ullrich
			    echo "<font color='white'>{$ta[0]}</a>&nbsp;&nbsp;&nbsp;";
951 2f4809ea Bill Marquette
			    echo "<font size='-12'>&nbsp;</td>\n";
952 a8726a3d Scott Ullrich
		    }
953
	    }
954 2f4809ea Bill Marquette
	    echo "</tr>\n<tr height='5px'>\n";
955
	    foreach ($tab_array as $ta) {
956
		    if($ta[1] == true) {
957
			    echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\"></td>\n";
958
		    } else {
959
			    echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\"></td>\n";
960
		    }
961
		    $tabscounter++;
962
	    }
963 a8726a3d Scott Ullrich
	    echo " </tr>\n";
964
	    echo "</table>\n";
965
	    
966
	    echo "<script type=\"text/javascript\">";
967
	    echo "NiftyCheck();\n";
968 0366b748 Scott Ullrich
	    echo "Rounded(\"div#tabactive\",\"top\",\"#FFF\",\"#EEEEEE\",\"smooth\");\n";
969 a8726a3d Scott Ullrich
	    for($x=0; $x<$tabscounter; $x++) 
970 0366b748 Scott Ullrich
		    echo "Rounded(\"div#tabdeactive{$x}\",\"top\",\"#FFF\",\"#777777\",\"smooth\");\n";
971 a8726a3d Scott Ullrich
	    echo "</script>";
972
    }
973 305eae3c Bill Marquette
974
975 90fd355f Bill Marquette
/****f* pfsense-utils/display_topbar
976
 * NAME
977
 *   display_topbar - top a table off with rounded edges
978
 * INPUTS
979
 *   $text	- (optional) Text to include in bar
980
 * RESULT
981
 *   null
982
 ******/
983 411528e9 Scott Ullrich
function display_topbar($text = "", $bg_color="#990000", $replace_color="#FFFFFF", $rounding_style="smooth") {	    
984 305eae3c Bill Marquette
	echo "     <table width='100%' cellpadding='0' cellspacing='0'>\n";
985
	echo "       <tr height='1'>\n";
986 411528e9 Scott Ullrich
	echo "         <td width='100%' valign='top' color='{$bg_color}' bgcolor='{$bg_color}'>";
987
	echo "		<div id='topbar'></div></td>\n";
988 305eae3c Bill Marquette
	echo "       </tr>\n";
989
	echo "       <tr height='1'>\n";
990
	if ($text != "")
991
		echo "         <td height='1' class='listtopic'>{$text}</td>\n";
992
	else
993
		echo "         <td height='1' class='listtopic'></td>\n";
994
	echo "       </tr>\n";
995
	echo "     </table>";
996
	echo "<script type=\"text/javascript\">";
997
	echo "NiftyCheck();\n";
998 411528e9 Scott Ullrich
	echo "Rounded(\"div#topbar\",\"top\",\"{$replace_color}\",\"{$bg_color}\",\"{$rounding_style}\");\n";
999 305eae3c Bill Marquette
	echo "</script>";
1000
}
1001 730768f7 Colin Smith
1002 94e8082f Scott Ullrich
/****f* pfsense-utils/generate_random_mac
1003
 * NAME
1004
 *   generate_random_mac - generates a random mac address
1005
 * INPUTS
1006
 *   none
1007
 * RESULT
1008
 *   $mac - a random mac address
1009
 ******/
1010 730768f7 Colin Smith
function generate_random_mac() {
1011 e8dd289e Scott Ullrich
	$mac = "00:a0:8e";
1012
	for($x=0; $x<3; $x++) 
1013 a57a45cb Colin Smith
	    $mac .= ":" . dechex(rand(16, 255));
1014 ac1bb69d Scott Ullrich
1015 730768f7 Colin Smith
	return $mac;
1016
}
1017 94e8082f Scott Ullrich
1018 d6fbd4ca Bill Marquette
/****f* pfsense-utils/strncpy
1019
 * NAME
1020
 *   strncpy - copy strings
1021
 * INPUTS
1022
 *   &$dst, $src, $length
1023
 * RESULT
1024
 *   none
1025
 ******/
1026
function strncpy(&$dst, $src, $length) {
1027
	if (strlen($src) > $length) {
1028
		$dst = substr($src, 0, $length);
1029
	} else {
1030
		$dst = $src;
1031
	}
1032
}
1033
1034 39a85258 Scott Ullrich
/****f* pfsense-utils/reload_interfaces_sync
1035 182c30de Scott Ullrich
 * NAME
1036
 *   reload_interfaces - reload all interfaces
1037
 * INPUTS
1038
 *   none
1039
 * RESULT
1040
 *   none
1041
 ******/
1042 39a85258 Scott Ullrich
function reload_interfaces_sync() {
1043 4f1252d5 Scott Ullrich
	global $config, $g;
1044
	
1045
	if(file_exists("{$g['tmp_path']}/config.cache"))
1046
		unlink("{$g['tmp_path']}/config.cache");
1047
	
1048
	/* parse config.xml again */
1049 12886b41 Scott Ullrich
	$config = parse_config(true);
1050 4f1252d5 Scott Ullrich
	
1051 182c30de Scott Ullrich
	/* set up LAN interface */
1052
	interfaces_lan_configure();
1053
1054
	/* set up WAN interface */
1055
	interfaces_wan_configure();
1056
1057
	/* set up Optional interfaces */
1058
	interfaces_optional_configure();
1059
        
1060
	/* set up static routes */
1061
	system_routing_configure();
1062 835c9f4a Scott Ullrich
	
1063
	/* bring up carp interfaces */
1064
	interfaces_carp_bringup();
1065 182c30de Scott Ullrich
1066
	/* enable routing */
1067
	system_routing_enable();
1068
}
1069
1070
/****f* pfsense-utils/reload_all
1071
 * NAME
1072 39a85258 Scott Ullrich
 *   reload_all - triggers a reload of all settings
1073 182c30de Scott Ullrich
 *   * INPUTS
1074
 *   none
1075
 * RESULT
1076
 *   none
1077
 ******/
1078
function reload_all() {
1079 39a85258 Scott Ullrich
	touch("/tmp/reload_all");
1080
}
1081
1082
/****f* pfsense-utils/reload_interfaces
1083
 * NAME
1084
 *   reload_interfaces - triggers a reload of all interfaces
1085
 * INPUTS
1086
 *   none
1087
 * RESULT
1088
 *   none
1089
 ******/
1090
function reload_interfaces() {
1091
	touch("/tmp/reload_interfaces");
1092
}
1093
1094
/****f* pfsense-utils/reload_all_sync
1095
 * NAME
1096
 *   reload_all - reload all settings
1097
 *   * INPUTS
1098
 *   none
1099
 * RESULT
1100
 *   none
1101
 ******/
1102
function reload_all_sync() {
1103 4f1252d5 Scott Ullrich
	global $config, $g;
1104
	
1105
	if(file_exists("{$g['tmp_path']}/config.cache"))
1106
		unlink("{$g['tmp_path']}/config.cache");
1107
	
1108
	/* parse config.xml again */
1109 12886b41 Scott Ullrich
	$config = parse_config(true);
1110
1111 182c30de Scott Ullrich
	/* set up our timezone */
1112
	system_timezone_configure();
1113
1114
	/* set up our hostname */
1115
	system_hostname_configure();
1116
1117
	/* make hosts file */
1118
	system_hosts_generate();
1119
1120
	/* generate resolv.conf */
1121
	system_resolvconf_generate();
1122
1123
	/* set up LAN interface */
1124
	interfaces_lan_configure();
1125
1126
	/* set up WAN interface */
1127
	interfaces_wan_configure();
1128
1129
	/* set up Optional interfaces */
1130
	interfaces_optional_configure();
1131
        
1132
	/* bring up carp interfaces */
1133 835c9f4a Scott Ullrich
	interfaces_carp_configure();
1134 182c30de Scott Ullrich
	
1135
	/* set up static routes */
1136
	system_routing_configure();
1137
1138
	/* enable routing */
1139
	system_routing_enable();
1140
	
1141
	/* ensure passwords are sync'd */
1142
	system_password_configure();
1143
1144
	/* start dnsmasq service */
1145
	services_dnsmasq_configure();
1146
1147
	/* start dyndns service */
1148
	services_dyndns_configure();
1149
1150
	/* start DHCP service */
1151
	services_dhcpd_configure();
1152
1153
	/* start the NTP client */
1154
	system_ntp_configure();
1155
1156
	/* start ftp proxy helpers if they are enabled */
1157
	system_start_ftp_helpers();
1158 835c9f4a Scott Ullrich
1159
	/* bring up carp interfaces */
1160
	interfaces_carp_bringup();
1161
1162 182c30de Scott Ullrich
        /* reload the filter */
1163
	filter_configure();	
1164
}
1165
1166
?>