summaryrefslogtreecommitdiffstats
path: root/etc/inc/pfsense-utils.inc
blob: 2f3b7f77bd39d12c5cade7c008abef86f1f292c5 (plain)
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
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
<?php
/****h* pfSense/pfsense-utils
 * NAME
 *   pfsense-utils.inc - Utilities specific to pfSense
 * DESCRIPTION
 *   This include contains various pfSense specific functions.
 * HISTORY
 *   $Id$
 ******
 *
 * Copyright (C) 2005 Scott Ullrich (sullrich@gmail.com)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

/****f* pfsense-utils/log_error
 * NAME
 *   log_error - Sends a string to syslog.
 * INPUTS
 *   $error	- string containing the syslog message.
 * RESULT
 *   null
 ******/
function log_error($error) {
    syslog(LOG_WARNING, $error);
    return;
}

/****f* pfsense-utils/return_dir_as_array
 * NAME
 *   return_dir_as_array - Return a directory's contents as an array.
 * INPUTS
 *   $dir	- string containing the path to the desired directory.
 * RESULT
 *   $dir_array - array containing the directory's contents. This array will be empty if the path specified is invalid.
 ******/
function return_dir_as_array($dir) {
    $dir_array = array();
    if (is_dir($dir)) {
	if ($dh = opendir($dir)) {
	    while (($file = readdir($dh)) !== false) {
		$canadd = 0;
		if($file == ".") $canadd = 1;
		if($file == "..") $canadd = 1;
		if($canadd == 0)
		    array_push($dir_array, $file);
	    }
	    closedir($dh);
	}
    }
    return $dir_array;
}

/****f* pfsense-utils/enable_hardware_offloading
 * NAME
 *   enable_hardware_offloading - Enable a NIC's supported hardware features.
 * INPUTS
 *   $interface	- string containing the physical interface to work on.
 * RESULT
 *   null
 * NOTES
 *   This function only supports the fxp driver's loadable microcode.
 ******/
function enable_hardware_offloading($interface) {
    global $config;
    global $g;
    if($g['booting']) {
		$supported_ints = array('fxp');
		foreach($supported_ints as $int) {
			if(stristr($interface,$int) != false) {
		    	mwexec("/sbin/ifconfig $interface link0");
			}
		}
    }
    return;
}

/****f* pfsense-utils/setup_microcode
 * NAME
 *   enumerates all interfaces and calls enable_hardware_offloading which
 *   enables a NIC's supported hardware features.
 * INPUTS
 *   
 * RESULT
 *   null
 * NOTES
 *   This function only supports the fxp driver's loadable microcode.
 ******/
function setup_microcode() {
   global $config;
    if($ip == "") return;
    $i = 0;
    $ifdescrs = array('wan', 'lan');
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
	$ifdescrs['opt' . $j] = "opt" . $j;
    }
    foreach($ifdescrs as $if)
	enable_hardware_offloading($if);
}

/****f* pfsense-utils/return_filename_as_array
 * NAME
 *   return_filename_as_array - Return a file's contents as an array.
 * INPUTS
 *   $filename	- string containing the path to the desired file.
 *   $strip	- array of characters to strip - default is '#'.
 * RESULT
 *   $file	- array containing the file's contents.
 * NOTES
 *   This function strips lines starting with '#' and leading/trailing whitespace by default.
 ******/
function return_filename_as_array($filename, $strip = array('#')) {
    if(file_exists($filename)) $file = file($filename);
    if(is_array($file)) {
	foreach($file as $line) $line = trim($line);
        foreach($strip as $tostrip) $file = preg_grep("/^{$tostrip}/", $file, PREG_GREP_INVERT);
    }
    return $file;
}

/****f* pfsense-utils/get_carp_status
 * NAME
 *   get_carp_status - Return whether CARP is enabled or disabled.
 * RESULT
 *   boolean	- true if CARP is enabled, false if otherwise.
 ******/
function get_carp_status() {
    /* grab the current status of carp */
    $status = `/sbin/sysctl net.inet.carp.allow | cut -d" " -f2`;
    if(intval($status) == "0") return false;
    return true;
}

/****f* pfsense-utils/return_filename_as_string
 * NAME
 *   return_filename_as_string - Return a file's contents as a string.
 * INPUTS
 *   $filename  - string containing the path to the desired file.
 * RESULT
 *   $tmp	- string containing the file's contents.
 ******/
function return_filename_as_string($filename) {
    if(file_exists($filename)) {
        return file_get_contents($filename);
    } else {
        return false;
    }
}

/****f* pfsense-utils/is_carp_defined
 * NAME
 *   is_carp_defined - Return whether CARP is detected in the kernel.
 * RESULT
 *   boolean	- true if CARP is detected, false otherwise.
 ******/
function is_carp_defined() {
    /* is carp compiled into the kernel and userland? */
    $command = "/sbin/sysctl -a | grep carp";
    $fd = popen($command . " 2>&1 ", "r");
    if(!$fd) {
	log_error("Warning, could not execute command {$command}");
	return 0;
    }
    while(!feof($fd)) {
	$tmp .= fread($fd,49);
    }
    fclose($fd);

    if($tmp == "")
	return false;
    else
	return true;
}

/****f* pfsense-utils/find_number_of_created_carp_interfaces
 * NAME
 *   find_number_of_created_carp_interfaces - Return the number of CARP interfaces.
 * RESULT
 *   $tmp	- Number of currently created CARP interfaces.
 ******/
function find_number_of_created_carp_interfaces() {
    $command = "/sbin/ifconfig | /usr/bin/grep \"carp*:\" | /usr/bin/wc -l";
    $fd = popen($command . " 2>&1 ", "r");
    if(!$fd) {
	log_error("Warning, could not execute command {$command}");
	return 0;
    }
    while(!feof($fd)) {
	$tmp .= fread($fd,49);
    }
    fclose($fd);
    $tmp = intval($tmp);
    return $tmp;
}

/****f* pfsense-utils/link_ip_to_carp_interface
 * NAME
 *   link_ip_to_carp_interface - Find where a CARP interface links to.
 * INPUTS
 *   $ip
 * RESULT
 *   $carp_ints
 ******/
function link_ip_to_carp_interface($ip) {
    global $config;
    if($ip == "") return;
    $i = 0;

    $ifdescrs = array('wan', 'lan');
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
	$ifdescrs['opt' . $j] = "opt" . $j;
    }

    $ft = split("\.", $ip);
    $ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . ".";

    $carp_ints = "";
    $num_carp_ints = find_number_of_created_carp_interfaces();
    foreach ($ifdescrs as $ifdescr => $ifname) {
	for($x=0; $x<$num_carp_ints; $x++) {
	    $carp_int = "carp{$x}";
	    $carp_ip = find_interface_ip($carp_int);
	    $carp_ft = split("\.", $carp_ip);
	    $carp_ft_ip = $carp_ft[0] . "." . $carp_ft[1] . "." . $carp_ft[2] . ".";
	    $result = does_interface_exist($carp_int);
	    if($result <> true) break;
	    $interface = filter_opt_interface_to_real($ifname);
	    if($ft_ip == $carp_ft_ip)
		if(stristr($carp_ints,$carp_int) == false)
		    $carp_ints .= " " . $carp_int;
	}
    }
    return $carp_ints;
}

/****f* pfsense-utils/exec_command
 * NAME
 *   exec_command - Execute a command and return a string of the result.
 * INPUTS
 *   $command	- String of the command to be executed.
 * RESULT
 *   String containing the command's result.
 * NOTES
 *   This function returns the command's stdout and stderr.
 ******/
function exec_command($command) {
    $output = array();
    exec($command . ' 2>&1 ', $output);
    return(implode("\n", $output));
}

/*
 * does_interface_exist($interface): return true or false if a interface is detected.
 */
function does_interface_exist($interface) {
    $ints = exec_command("/sbin/ifconfig -l");
    if(stristr($ints, $interface) !== false)
	return true;
    else
	return false;
}

/*
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
 */
function convert_ip_to_network_format($ip, $subnet) {
    $ipsplit = split('[.]', $ip);
    $string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
    return $string;
}

/*
 * find_interface_ip($interface): return the interface ip (first found)
 */
function find_interface_ip($interface) {
    if(does_interface_exist($interface) == false) return;
    $ip = exec_command("/sbin/ifconfig {$interface} | /usr/bin/grep -w \"inet\" | /usr/bin/cut -d\" \" -f 2");
    $ip = str_replace("\n","",$ip);
    return $ip;
}

function guess_interface_from_ip($ipaddress) {
    $ints = `/sbin/ifconfig -l`;
    $ints_split = split(" ", $ints);
    $ip_subnet_split = split("\.", $ipaddress);
    $ip_subnet = $ip_subnet_split[0] . "." . $ip_subnet_split[1] . "." . $ip_subnet_split[2] . ".";
    foreach($ints_split as $int) {
        $ip = find_interface_ip($int);
        $ip_split = split("\.", $ip);
        $ip_tocheck = $ip_split[0] . "." . $ip_split[1] . "." . $ip_split[2] . ".";
        if(stristr($ip_tocheck, $ip_subnet) != false) return $int;
    }
}

function filter_opt_interface_to_real($opt) {
    global $config;
    return $config['interfaces'][$opt]['if'];
}

function filter_get_opt_interface_descr($opt) {
    global $config;
    return $config['interfaces'][$opt]['descr'];
}

function get_friendly_interface_list_as_array() {
    global $config;
    $ints = array();
    $i = 0;
    $ifdescrs = array('wan', 'lan');
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
	$ifdescrs['opt' . $j] = "opt" . $j;
    }
    $ifdescrs = get_interface_list();
    foreach ($ifdescrs as $ifdescr => $ifname) {
	array_push($ints,$ifdescr);
    }
    return $ints;
}

/*
 * find_ip_interface($ip): return the interface where an ip is defined
 */
function find_ip_interface($ip) {
    global $config;
    $i = 0;
    $ifdescrs = array('wan', 'lan');
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
	$ifdescrs['opt' . $j] = "opt" . $j;
    }
    foreach ($ifdescrs as $ifdescr => $ifname) {
	$int = filter_translate_type_to_real_interface($ifname);
	$ifconfig = exec_command("/sbin/ifconfig {$int}");
	if(stristr($ifconfig,$ip) <> false)
	    return $int;
    }
    return false;
}

/*
 *  filter_translate_type_to_real_interface($interface): returns the real interface name
 *                                                       for a friendly interface.  ie: wan
 */
function filter_translate_type_to_real_interface($interface) {
    global $config;
    return $config['interfaces'][$interface]['if'];
}

/*
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
 */
function get_carp_interface_status($carpinterface) {
    /* basically cache the contents of ifconfig statement
       to speed up this routine */
    global $carp_query;
    if($carp_query == "")
	$carp_query = split("\n", `/sbin/ifconfig | /usr/bin/grep carp`);
    $found_interface = 0;
    foreach($carp_query as $int) {
	if($found_interface == 1) {
	    if(stristr($int, "MASTER") == true) return "MASTER";
	    if(stristr($int, "BACKUP") == true) return "BACKUP";
	    if(stristr($int, "INIT") == true) return "INIT";
	    return false;
	}
	if(stristr($int, $carpinterface) == true) $found_interface=1;
    }
    return $status;
}

/*
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
 */
function get_pfsync_interface_status($pfsyncinterface) {
    $result = does_interface_exist($pfsyncinterface);
    if($result <> true) return;
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5");
    return $status;
}

/*
 * find_carp_interface($ip): return the carp interface where an ip is defined
 */
function find_carp_interface($ip) {
    $num_carp_ints = find_number_of_created_carp_interfaces();
    for($x=0; $x<$num_carp_ints; $x++) {
        $result = does_interface_exist("carp{$x}");
	if($result <> true) return;
	$ifconfig = exec_command("/sbin/ifconfig carp{$x}");
	if(stristr($ifconfig,$ip))
	    return "carp" . $x;
    }
}

/*
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
 */
function add_rule_to_anchor($anchor, $rule, $label) {
    mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
}

/*
 * remove_text_from_file
 * remove $text from file $file
 */
function remove_text_from_file($file, $text) {
    global $fd_log;
    fwrite($fd_log, "Adding needed text items:\n");
    $filecontents = exec_command_and_return_text("cat " . $file);
    $textTMP = str_replace($text, "", $filecontents);
    $text .= $textTMP;
    fwrite($fd_log, $text . "\n");
    $fd = fopen($file, "w");
    fwrite($fd, $text);
    fclose($fd);
}

/*
 *  is_package_installed($packagename): returns 1 if a package is installed, 0 otherwise.
 */
function is_package_installed($packagename) {
    global $config;
    if($config['installedpackages']['package'] <> "")
	foreach ($config['installedpackages']['package'] as $pkg) {
	    if($pkg['name'] == $packagename) return 1;
	}
    return 0;
}

/*
 * lookup pkg array id#
 */
function get_pkg_id($pkg_name) {
    global $config;
    global $pkg_config;
    if(is_array($config['installedpackages']['package'])) {
	$i = 0;
	foreach ($config['installedpackages']['package'] as $pkg) {
	    if($pkg['name'] == $pkg_name) return $i;
	    $i++;
	}
	return $i;
    }
    return -1;
}

/*
 *  get_latest_package_version($pkgname): Get current version of a package. Returns latest package version or false
 *  					  if package isn't defined in the currently used pkg_config.xml.
 */
function get_latest_package_version($pkg_name) {
    global $g;
    fetch_latest_pkg_config();
    $pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
    foreach($pkg_config['packages']['package'] as $pkg) {
	if($pkg['name'] == $pkg_name) {
	    return $pkg['version'];
	}
    }
    return false;
}

/*
 * Lookup pkg_id in pkg_config.xml
 */
function get_available_pkg_id($pkg_name) {
    global $pkg_config, $g;
    if(!is_array($pkg_config)) {
	fetch_latest_pkg_config();
    }
    $pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
    $id = 0;
    foreach($pkg_config['packages']['package'] as $pkg) {
	if($pkg['name'] == $pkg_name) {
	    return $id;
	}
	$id++;
    }
    return;
}

/*
 * fetch_latest_pkg_config: download the latest pkg_config.xml to /tmp/ directory
 */
function fetch_latest_pkg_config() {
    global $g;
    global $config;
    if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
	$pkg_config_location = $g['pkg_config_location'];
	$pkg_config_base_url = $g['pkg_config_base_url'];
	if(isset($config['system']['alt_pkgconfig_url']['enabled'])) {
	    $pkg_config_location = $config['system']['alt_pkgconfig_url']['pkgconfig_base_url'] . $config['system']['alt_pkgconfig_url']['pkgconfig_filename'];
	    $pkg_config_base_url = $config['system']['alt_pkgconfig_url']['pkgconfig_base_url'];
	}
	mwexec("/usr/bin/fetch -o {$g['tmp_path']}/pkg_config.xml {$pkg_config_location}");
	if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
	    print_info_box_np("Could not download pkg_config.xml from " . $pkg_config_base_url . ". Check your DNS settings.");
	    die;
    	}
    }
    return;
}

/*
 * add_text_to_file($file, $text): adds $text to $file.
 * replaces the text if it already exists.
 */
function add_text_to_file($file, $text) {
    global $fd_log;
    fwrite($fd_log, "Adding needed text items:\n");
    $filecontents = exec_command_and_return_text("cat " . $file);
    $filecontents = str_replace($text, "", $filecontents);
    $text = $filecontents . $text;
    fwrite($fd_log, $text . "\n");
    $fd = fopen($file, "w");
    fwrite($fd, $text . "\n");
    fclose($fd);
}

/*
 * get_filename_from_url($url): converts a url to its filename.
 */
function get_filename_from_url($url) {
    $filenamesplit = split("/", $url);
    foreach($filenamesplit as $fn) $filename = $fn;
    return $filename;
}

/*
 *   update_output_window: update bottom textarea dynamically.
 */
function update_output_window($text) {
    $log = ereg_replace("\n", "\\n", $text);
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
}

/*
 *   get_dir: return an array of $dir
 */
function get_dir($dir) {
    $dir_array = array();
    $d = dir($dir);
    while (false !== ($entry = $d->read())) {
	array_push($dir_array, $entry);
    }
    $d->close();
    return $dir_array;
}

/*
 *   update_output_window: update top textarea dynamically.
 */
function update_status($status) {
    echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
}

/*
 *   exec_command_and_return_text_array: execute command and return output
 */
function exec_command_and_return_text_array($command) {
    $counter = 0;
    $fd = popen($command . " 2>&1 ", "r");
    while(!feof($fd)) {
	$tmp .= fread($fd,49);
    }
    fclose($fd);
    $temp_array = split("\n", $tmp);
    return $tmp_array;
}

/*
 *   exec_command_and_return_text: execute command and return output
 */
function exec_command_and_return_text($command) {
    return exec_command($command);
}

/*
 *   exec_command_and_return_text: execute command and update output window dynamically
 */
function execute_command_return_output($command) {
    global $fd_log;
    $fd = popen($command . " 2>&1 ", "r");
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
    $counter = 0;
    $counter2 = 0;
    while(!feof($fd)) {
	$tmp = fread($fd, 50);
	$tmp1 = ereg_replace("\n","\\n", $tmp);
	$text = ereg_replace("\"","'", $tmp1);
	if($lasttext == "..") {
	    $text = "";
	    $lasttext = "";
	    $counter=$counter-2;
	} else {
	    $lasttext .= $text;
	}
	if($counter > 51) {
	    $counter = 0;
	    $extrabreak = "\\n";
	} else {
	    $extrabreak = "";
	    $counter++;
	}
	if($counter2 > 600) {
	    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
	    $counter2 = 0;
	} else
	    $counter2++;
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
    }
    fclose($fd);
}

/*
 * convert_friendly_interface_to_real_interface_name($interface): convert WAN to FXP0
 */
function convert_friendly_interface_to_real_interface_name($interface) {
    global $config;
    $lc_interface = strtolower($interface);
    if($lc_interface == "lan") return $config['interfaces']['lan']['if'];
    if($lc_interface == "wan") return $config['interfaces']['wan']['if'];
    $i = 0;
    $ifdescrs = array();
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
	$ifdescrs['opt' . $j] = "opt" . $j;
    foreach ($ifdescrs as $ifdescr => $ifname) {
	if(strtolower($ifname) == $lc_interface)
	    return $config['interfaces'][$ifname]['if'];
	if(strtolower($config['interfaces'][$ifname]['descr']) == $lc_interface)
	    return $config['interfaces'][$ifname]['if'];
    }
    return $interface;
}

/*
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
 */
function convert_real_interface_to_friendly_interface_name($interface) {
    global $config;
    $i = 0;
    $ifdescrs = array('wan', 'lan');
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
	$ifdescrs['opt' . $j] = "opt" . $j;
    foreach ($ifdescrs as $ifdescr => $ifname) {
	$int = filter_translate_type_to_real_interface($ifname);
	if($ifname == $interface) return $ifname;
	if($int == $interface) return $ifname;
    }
    return $interface;
}

/*
 * update_progress_bar($percent): updates the javascript driven progress bar.
 */
function update_progress_bar($percent) {
    if($percent > 100) $percent = 1;
    echo "\n<script type=\"text/javascript\" language=\"javascript\">";
    echo "\ndocument.progressbar.style.width='" . $percent . "%';";
    echo "\n</script>";
}

/*
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
 * This function may also print output to the terminal indicating progress.
 */
function resync_all_package_configs($show_message = false) {
    global $config;
    $i = 0;
    log_error("Resyncing configuration for all packages.");
    if(!$config['installedpackages']['package']) return;
    if($show_message == true) print "Syncing packages:";
    foreach($config['installedpackages']['package'] as $package) {
	if($show_message == true) print " " . $package['name'];
	sync_package($i, true, true);
	$i++;
    }
    if($show_message == true) print ".\n";
}

/*
 * sweep_package_processes(): Periodically kill a package's unnecessary processes
 *			      that may still be running (a server that does not automatically timeout, for example)
 */
function sweep_package_processes() {
    global $config;
    if(!$config['installedpackages']['package']) return;
    foreach($config['installedpackages']['package'] as $package) {
        $pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
        if($pkg_config['swept_processes'] <> "") {
            mwexec("/usr/bin/killall " . $pkg_config['swept_processes']);
            log_error("Killed " . $package['name'] . "'s unnecessary processes.");
        }
    }
}

/*
 * gather_altq_queue_stats():  gather alq queue stats and return an array that
 *                             is queuename|qlength|measured_packets
 *                             NOTE: this command takes 5 seconds to run
 */
function gather_altq_queue_stats($dont_return_root_queues) {
    mwexec("/usr/bin/killall -9 pfctl");
    $stats = `/sbin/pfctl -vvsq & /bin/sleep 5;/usr/bin/killall pfctl 2>/dev/null`;
    $stats_array = split("\n", $stats);
    $queue_stats = array();
    foreach ($stats_array as $stats_line) {
        if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
            $queue_name = $match_array[1][0];
        if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
            $speed = $match_array[1][0];
        if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
            $borrows = $match_array[1][0];
        if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
            $suspends = $match_array[1][0];
        if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
            $drops = $match_array[1][0];
        if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
            $measured = $match_array[1][0];
	    if($dont_return_root_queues == true)
		if(stristr($queue_name,"root_") == false)
		    array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
        }
    }
    return $queue_stats;
}

/*
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
 *					 Useful for finding paths and stripping file extensions.
 */
function reverse_strrchr($haystack, $needle)
{
               return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
}

/*
 * get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", return_nosync = 1):  Return a package's dependencies.
 *
 * $filetype = "all" || ".xml", ".tgz", etc.
 * $format = "files" (full filenames) || "names" (stripped / parsed depend names)
 * $return_nosync = 1 (return depends that have nosync set) | 0 (ignore packages with nosync)
 *
 */
function get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", $return_nosync = 1) {
    global $config;
    if(!is_numeric($pkg_name)) {
	$pkg_name = get_pkg_id($pkg_name);
	if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
    } else {
	if(!isset($config['installedpackages']['package'][$pkg_id])) return; // No package belongs to the pkg_id passed to this function.
    }
    $package = $config['installedpackages']['package'][$pkg_id];
    print '$package done.';
    if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) { // If the package's config file doesn't exist, log an error and fetch it.
	log_error("Fetching missing configuration XML for " . $package['name']);
	mwexec("/usr/bin/fetch -o /usr/local/pkg/" . $package['configurationfile'] . " http://www.pfsense.com/packages/config/" . $package['configurationfile']);
    }
    $pkg_xml = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
    if($pkg_xml['additional_files_needed'] != "") {
	foreach($pkg_xml['additional_files_needed'] as $item) {
	    if (($return_nosync == 0) && (isset($item['nosync']))) continue; // Do not return depends with nosync set if not required.
	    $depend_file = substr(strrchr($item['item']['0'], '/'),1); // Strip URLs down to filenames.
	    $depend_name = substr(substr($depend_file,0,strpos($depend_file,".")+1),0,-1); // Strip filename down to dependency name.
	    if (($filetype != "all") && (!preg_match("/${filetype}/i", $depend_file))) continue;
	    if ($item['prefix'] != "") {
		$prefix = $item['prefix'];
	    } else {
		$prefix = "/usr/local/pkg/";
	    }
	    if(!file_exists($prefix . $pkg_name)) {
		log_error("Fetching missing dependency (" . $depend_name . ") for " . $pkg_name);
		mwexec("/usr/local/bin/fetch -o " . $prefix . $depend_file . " " . $item['name']['0']);
		if($item['chmod'] != "")
		    chmod($prefix . $depend_file, $item['chmod']); // Handle chmods.
	    }
	    switch ($format) {
	    case "files":
		$depends[] = $depend_file;
		break;
	    case "names":
		switch ($filetype) {
		case "all":
		    if(preg_match("/\.xml/i", $depend_file)) {
			$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
			$depends[] = $depend_xml['name'];
			break;
		    } else {
			$depends[] = $depend_name; // If this dependency isn't package XML, use the stripped filename.
			break;
		    }
		case ".xml":
		    $depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
		    $depends[] = $depend_xml['name'];
		    break;
		default:
		    $depends[] = $depend_name; // If we aren't looking for XML, use the stripped filename (it's all we have).
		    break;
		}
	    }
	}
	return $depends;
    }
}

/*
 * is_service_running($service_name): checks to see if a service is running.
 *                                    if the service is running returns 1.
 */
function is_service_running($service_name) {
    $status = `/bin/ps ax | grep {$service_name} | grep -v grep`;
    $status_split = split("\n", $service_name);
    $counter = 0;
    foreach ($status_split as $ss) $counter++;
    if($counter > 0) return 1;
    return 0;
}

/*
 *  backup_config_section($section): returns as an xml file string of
 *                                   the configuration section
 */
function backup_config_section($section) {
    global $config;
    $new_section = &$config[$section];
    /* generate configuration XML */
    $xmlconfig = dump_xml_config($new_section, $section);
    $xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
    return $xmlconfig;
}

/*
 *  restore_config_section($section, new_contents): restore a configuration section,
 *                                                  and write the configuration out
 *                                                  to disk/cf.
 */
function restore_config_section($section, $new_contents) {
    global $config;
    $fout = fopen("{$g['tmp_path']}/tmpxml","w");
    fwrite($fout, $new_contents);
    fclose($fout);
    $section_xml = parse_xml_config_pkg($g['tmp_path'] . "/tmpxml", $section);
    $config[$section] = &$section_xml;
    unlink($g['tmp_path'] . "/tmpxml");
    write_config("Restored {$section} of config file (maybe from CARP partner)");
    return;
}

/*
 * http_post($server, $port, $url, $vars): does an http post to a web server
 *                                         posting the vars array.
 * written by nf@bigpond.net.au
 */
function http_post($server, $port, $url, $vars) {
    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
    $urlencoded = "";
    while (list($key,$value) = each($vars))
	$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
    $urlencoded = substr($urlencoded,0,-1);

    $content_length = strlen($urlencoded);

    $headers = "POST $url HTTP/1.1
Accept: */*
Accept-Language: en-au
Content-Type: application/x-www-form-urlencoded
User-Agent: $user_agent
Host: $server
Connection: Keep-Alive
Cache-Control: no-cache
Content-Length: $content_length

";

    $fp = fsockopen($server, $port, $errno, $errstr);
    if (!$fp) {
	return false;
    }

    fputs($fp, $headers);
    fputs($fp, $urlencoded);

    $ret = "";
    while (!feof($fp))
	$ret.= fgets($fp, 1024);

    fclose($fp);

    return $ret;

}

/*
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
 */
if (!function_exists('php_check_syntax')){
   function php_check_syntax($code_to_check, &$errormessage){
	return false;
        $fout = fopen("/tmp/codetocheck.php","w");
        $code = $_POST['content'];
        $code = str_replace("<?php", "", $code);
        $code = str_replace("?>", "", $code);
        fwrite($fout, "<?php\n\n");
        fwrite($fout, $code);
        fwrite($fout, "\n\n?>\n");
        fclose($fout);
        $command = "/usr/local/bin/php -l /tmp/codetocheck.php";
        $output = exec_command($command);
        if (stristr($output, "Errors parsing") == false) {
            echo "false\n";
            $errormessage = '';
            return(false);
        } else {
            $errormessage = $output;
            return(true);
        }
    }
}

/*
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
 */
if (!function_exists('php_check_syntax')){
   function php_check_syntax($code_to_check, &$errormessage){
	return false;
        $command = "/usr/local/bin/php -l " . $code_to_check;
        $output = exec_command($command);
        if (stristr($output, "Errors parsing") == false) {
            echo "false\n";
            $errormessage = '';
            return(false);
        } else {
            $errormessage = $output;
            return(true);
        }
    }
}

/*
 * sync_package($pkg_name, $sync_depends = true, $show_message = false) Force a package to setup its configuration and rc.d files.
 */
function sync_package($pkg_name, $sync_depends = true, $show_message = false) {
    global $config;

    if(!file_exists("/usr/local/pkg")) mwexec("/bin/mkdir -p /usr/local/pkg/pf");
    if(!$config['installedpackages']['package']) return;
    if(!is_numeric($pkg_name)) {
	$pkg_id = get_pkg_id($pkg_name);
	if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
    } else {
	$pkg_id = $pkg_name;
	if(!isset($config['installedpackages']['package'][$pkg_id]))
	    return;  // No package belongs to the pkg_id passed to this function.
    }
    $package = $config['installedpackages']['package'][$pkg_id];
    if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
	//if($show_message == true) print "(f)"; Don't mess with this until the package system has settled.
	log_error("Fetching missing configuration XML for " . $package['name']);
	mwexec("/usr/bin/fetch -o /usr/local/pkg/" . $package['configurationfile'] . " http://www.pfsense.com/packages/config/" . $package['configurationfile']);
    }
    $pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
    if(isset($pkg_config['nosync'])) continue;
    //if($show_message == true) print "Syncing " . $pkg_name;
    if($pkg['custom_php_global_functions'] <> "")
        eval($pkg['custom_php_global_functions']);
    if($pkg_config['custom_php_command_before_form'] <> "")
	eval($pkg_config['custom_php_command_before_form']);
    if($pkg_config['custom_php_resync_config_command'] <> "")
	eval($pkg_config['custom_php_resync_config_command']);
    if($sync_depends == true) {
	$depends = get_pkg_depends($pkg_name, ".xml", "files", 1); // Call dependency handler and do a little more error checking.
	if(is_array($depends)) {
	    foreach($depends as $item) {
		$item_config = parse_xml_config_pkg("/usr/local/pkg/" . $item, "packagegui");
		if(isset($item_config['nosync'])) continue;
		if($item_config['custom_php_command_before_form'] <> "") {
		    eval($item_config['custom_php_command_before_form']);
		    print "Evaled dependency.";
		}
		if($item_config['custom_php_resync_config_command'] <> "") {
		    eval($item_config['custom_php_resync_config_command']);
		    print "Evaled dependency.";
		}
		if($show_message == true) print " " . $item_config['name'];
	    }
	}
    }
    // if($show_message == true) print ".";
}

/*
 * rmdir_recursive($path,$follow_links=false)
 * Recursively remove a directory tree (rm -rf path)
 * This is for directories _only_
 */
function rmdir_recursive($path,$follow_links=false) {
	$to_do = glob($path);
	if(!is_array($to_do)) {
		if(file_exists($to_do)) {
			$dir = opendir($path);
			while ($entry = readdir($dir)) {
				if (is_file("$path/$entry") || ((!$follow_links) && is_link("$path/$entry")))
					unlink("$path/$entry");
      	 			elseif (is_dir("$path/$entry") && $entry!='.' && $entry!='..')
					rmdir_recursive("$path/$entry");
			}
			closedir($dir);
			rmdir($path);
			return;
		}
	} else {
		foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
			if(file_exists($workingdir)) {
				$dir = opendir($workingdir);
				while ($entry = readdir($dir)) {
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
					unlink("$workingdir/$entry");
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
					rmdir_recursive("$workingdir/$entry");
				}
				closedir($dir);
				rmdir($workingdir);
                	}
		}
		return;
	}
	return;
}

/*
 * safe_mkdir($path, $mode = 0755)
 * create directory if it doesn't already exist and isn't a file!
 */
function safe_mkdir($path, $mode=0755) {
	if (!is_file($path) && !is_dir($path))
		return mkdir($path, $mode);
	else
		return false;
}

/*
 * make_dirs($path, $mode = 0755)
 * create directory tree recursively (mkdir -p)
 */
function make_dirs($path, $mode = 0755)
{
	return is_dir($path) || (make_dirs(dirname($path), $mode) && safe_mkdir($path, $mode));
}

/****f* pfsense-utils/auto_upgrade
 * NAME
 *   auto_upgrade - pfSense autoupdate handler.
 * FUNCTION
 *   Begin the pfSense autoupdate process. This function calls check_firmware_version to get
 *   a list of current versions and then loops through them, applying binary diffs etc.
 * RESULT
 *   null
 * BUGS
 *   This function needs to have logic in place to automatically switch over to full updates
 *   if a certain amount of binary diffs do not apply successfully.
 * SEE ALSO
 *   pfsense.utils/check_firmware_version
 ******/
function auto_upgrade() {
        global $config, $g;
	if (isset($config['system']['alt_firmware_url']['enabled'])) {
                $firmwareurl=$config['system']['alt_firmware_url']['firmware_base_url'];
                $firmwarepath=$config['system']['alt_firmware_url']['firmware_filename'];
        } else {
                $firmwareurl=$g['firmwarebaseurl'];
                $firmwarepath=$g['firmwarefilename'];
        }
        if($config['system']['proxy_auth_username'] <> "")
	    $http_auth_username = $config['system']['proxy_auth_username'];
        if($config['system']['proxy_auth_password'] <> "")
	    $http_auth_password = $config['system']['proxy_auth_password'];
        if (isset($config['system']['alt_firmware_url']['enabled'])) {
                $firmwareurl=$config['system']['alt_firmware_url']['firmware_base_url'];
                $firmwarename=$config['system']['alt_firmware_url']['firmware_filename'];
        } else {
                $firmwareurl=$g['firmwarebaseurl'];
                $firmwarename=$g['firmwarefilename'];
        }
        exec_rc_script_async("/etc/rc.firmware_auto {$firmwareurl} {$firmwarename} {$http_auth_username} {$http_auth_password}");
	return;
}

/*
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
 */
function check_firmware_version($tocheck = "all", $return_php = true) {
        global $g;
	$versioncheck_base_url = $g['versioncheckbaseurl'];
        $versioncheck_path = $g['versioncheckpath'];
        if(isset($config['system']['alt_firmware_url']['enabled']) and isset($config['system']['alt_firmware_url']['versioncheck_base_url'])) {
                $versioncheck_base_url = $config['system']['alt_firmware_url']['versioncheck_base_url'];
	}
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
			"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
			"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
			"platform" => trim(file_get_contents('/etc/platform'))
		);
	if($tocheck = "all") {
		$params = $rawparams;
	} else {
		foreach($tocheck as $check) {
			$params['check'] = $rawparams['check'];
			$params['platform'] = $rawparams['platform'];
		}
	}
	if(isset($config['system']['firmwarebranch'])) {
		$params['branch'] = $config['system']['firmwarebranch'];
	}
	$xmlparams = php_value_to_xmlrpc($params);
        $msg = new XML_RPC_Message('pfsense.get_firmware_version', array($xmlparams));
        $cli = new XML_RPC_Client($versioncheck_path, $versioncheck_base_url);
	$resp = $cli->send($msg, 10);
	if(!$resp or $resp->faultCode()) {
		$raw_versions = false;
	} else {
		$raw_versions = xmlrpc_value_to_php($resp->value());
		$raw_versions["current"] = $params;
	}
	return $raw_versions;
}

function pkg_fetch_recursive($pkgname, $filename, $dependlevel = 0, $base_url = 'http://ftp2.freebsd.org/pub/FreeBSD/ports/i386/packages-5.4-release/Latest') {
        global $pkgent, $static_status, $static_output, $g, $fd_log;
        $pkg_extension = strrchr($filename, '.');
        $static_output .= "\n" . str_repeat(" ", $dependlevel * 2) . $pkgname . " ";
        $fetchto = "/tmp/apkg_" . $pkgname . $pkg_extension;
        download_file_with_progress_bar($base_url . "/" . $filename, $fetchto);
//      update_output_window($static_output . "\n\n" . $pkg_progress);
        exec("/usr/bin/bzcat {$fetchto} | /usr/bin/tar -O -f - -x +CONTENTS", $slaveout);
        $workingdir = preg_grep("/instmp/", $slaveout);
        $workingdir = $workingdir[0];
        $raw_depends_list = array_values(preg_grep("/\@pkgdep/", $slaveout));
        if($raw_depends_list != "") {
                if($pkgent['exclude_dependency'] != "")
                        $raw_depends_list = array_values(preg_grep($pkent['exclude_dependency'], PREG_GREP_INVERT));
                foreach($raw_depends_list as $adepend) {
                        $working_depend = explode(" ", $adepend);
                        //$working_depend = explode("-", $working_depend[1]);
                        $depend_filename = $working_depend[1] . $pkg_extension;
                        exec("ls /var/db/pkg", $is_installed);
                        $pkg_installed = false;
                        foreach($is_installed as $is_inst) {
                                if($is_inst == $working_depend[1]) {
                                        $pkg_installed = true;
                                        break;
                                }
                        }
//                      $is_installed = array_values(preg_grep("/\b{$working_depend[0]}\b/i", $is_installed));
                        if($pkg_installed === false) {
                                pkg_fetch_recursive($working_depend[1], $depend_filename, $dependlevel + 1, $base_url);
                        } else {
                                $dependlevel++;
                                $static_output .= "\n" . str_repeat(" ", $dependlevel * 2) . $working_depend[1] . " ";
                                fwrite($fd_log, $working_depend[1] . "\n");
                        }
                }
        }
        exec("cat {$g['tmp_path']}/y | /usr/sbin/pkg_add -fv {$fetchto} 2>&1", $pkgaddout);
        fwrite($fd_log, $pkgname . " " . print_r($pkgaddout, true) . "\n");
        return true;
}

function download_file_with_progress_bar($url_file, $destination_file) {
        global $ch, $fout, $file_size, $downloaded, $counter;
        $file_size  = 1;
        $downloaded = 1;
        /* open destination file */
        $fout = fopen($destination_file, "wb");

        /*
                Originally by Author: Keyvan Minoukadeh
                Modified by Scott Ullrich to return Content-Length size
        */
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_file);
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
        curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
                        
        curl_exec($ch);
        fclose($fout);
        curl_close($ch);
         
        return 1;
}

function read_header($ch, $string) {
        global $file_size, $ch, $fout;
        $length = strlen($string);
        ereg("(Content-Length:) (.*)", $string, $regs);
        if($regs[2] <> "") {
                $file_size = intval($regs[2]);
        }
        return $length;
}
 
function read_body($ch, $string) {
        global $fout, $file_size, $downloaded, $counter, $sendto, $static_output, $lastseen;
        $length = strlen($string);
        $downloaded += intval($length);
        $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
        $downloadProgress = 100 - $downloadProgress;
        /*
           lastseen is used to prevent from spamming firefox with hundreds of
           unnecessary javascript update messages which sends the clients
           firefox utilization to 100%
        */
        if($lastseen <> $downloadProgress and $downloadProgress < 101) {
                if($sendto == "status") {
                        $tostatus = $static_status . $downloadProgress . "%";
                        update_status($tostatus);
                } else {
                        $tooutput = $static_output . $downloadProgress . "%";
                        update_output_window($tooutput);
                }
                update_progress_bar($downloadProgress);
                $lastseen = $downloadProgress;
        }
        fwrite($fout, $string);
        return $length;
}

?>
OpenPOWER on IntegriCloud