Project

General

Profile

Download (3.33 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * itemid.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2009-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2025 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2009 Janne Enberg <janne.enberg@lietu.net>
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
/****f* itemid/delete_id
26
 * NAME
27
 *   delete_id - delete an item with ['associated-rule-id'] = $id from $array
28
 * INPUTS
29
 *   $id       - int: The ID to delete
30
 *   $array    - array to delete the item from
31
 * RESULT
32
 *   boolean   - true if item was found and deleted
33
 ******/
34
function delete_id($id) {
35
	$array = config_get_path('filter/rule', []);
36

    
37
	// Search for the item in the array
38
	$delete_index = get_id($id, $array);
39

    
40
	// If we found the item, unset it
41
	if ($delete_index === false) {
42
		return false;
43
	}
44

    
45
	$if = $array[$delete_index]['interface'];
46
	unset($array[$delete_index]);
47

    
48
	// shift the separators
49
	$ridx = get_interface_ruleindex($if, $delete_index);
50
	$a_separators = config_get_path('filter/separator/' . strtolower($if), []);
51
	shift_separators($a_separators, $ridx['index'], true);
52
	config_set_path('filter/separator/' . strtolower($if), $a_separators);
53

    
54
	config_set_path('filter/rule', $array);
55
	filter_rules_sort();
56
	return true;
57
}
58

    
59
/****f* itemid/toggle_id
60
 * NAME
61
 *   toggle_id - enable/disable item with ['associated-rule-id'] = $id from $array
62
 * INPUTS
63
 *   $id       - int: The ID to delete
64
 *   $array    - array to delete the item from
65
 *   $status   - true to enable item and false to disable
66
 * RESULT
67
 *   boolean   - true if item was found and set
68
 ******/
69
function toggle_id($id, $status) {
70
	$array = config_get_path('filter/rule', []);
71

    
72
	// Search for the item in the array
73
	$toggle_index = get_id($id, $array);
74

    
75
	// If we found the item, unset it
76
	if ($toggle_index === false) {
77
		return false;
78
	}
79

    
80
	if ($status) {
81
		unset($array[$toggle_index]['disabled']);
82
	} else {
83
		$array[$toggle_index]['disabled'] = true;
84
	}
85

    
86
	config_set_path('filter/rule', $array);
87

    
88
	return true;
89
}
90

    
91
/****f* itemid/get_id
92
 * NAME
93
 *   get_id - Get an item id with ['associated-rule-id'] = $id from $array
94
 * INPUTS
95
 *   $id     - string: The ID to get
96
 *   $array  - array to get the item from
97
 * RESULT
98
 *   mixed   - The id, false if not found
99
 ******/
100
function get_id($id, $array) {
101
	// Use $foo = get_id('id', array('id'=>'value'));
102

    
103
	if (!is_array($array)) {
104
		return false;
105
	}
106

    
107
	// Search for the item in the array
108
	foreach ($array as $key => $item) {
109
		// If this item is the one we want to delete
110
		if (isset($item['associated-rule-id']) &&
111
		    $item['associated-rule-id'] == $id) {
112
			return $key;
113
		}
114
	}
115

    
116
	return false;
117
}
118

    
119
/****f* itemid/get_unique_id
120
 * NAME
121
 *   get_unique_id - get a unique identifier
122
 * RESULT
123
 *   string     - unique id
124
 ******/
125
function get_unique_id() {
126

    
127
	return uniqid("nat_", true);
128
}
129

    
130
?>
(27-27/61)