Project

General

Profile

Download (3.34 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-2022 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
	global $config;
36

    
37
	$array = config_get_path('filter/rule', []);
38

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

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

    
47
	$if = $array[$delete_index]['interface'];
48
	unset($array[$delete_index]);
49

    
50
	// Update the separators
51
	init_config_arr(array('filter', 'separator', strtolower($if)));
52
	$a_separators = &$config['filter']['separator'][strtolower($if)];
53
	// get rule index within interface
54
	$ridx = ifridx($if, $delete_index);
55
	$mvnrows = -1;
56
	move_separators($a_separators, $ridx, $mvnrows);
57

    
58
	config_set_path('filter/rule', $array);
59
	return true;
60
}
61

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

    
75
	// Search for the item in the array
76
	$toggle_index = get_id($id, $array);
77

    
78
	// If we found the item, unset it
79
	if ($toggle_index === false) {
80
		return false;
81
	}
82

    
83
	if ($status) {
84
		unset($array[$toggle_index]['disabled']);
85
	} else {
86
		$array[$toggle_index]['disabled'] = true;
87
	}
88

    
89
	config_set_path('filter/rule', $array);
90

    
91
	return true;
92
}
93

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

    
106
	if (!is_array($array)) {
107
		return false;
108
	}
109

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

    
119
	return false;
120
}
121

    
122
/****f* itemid/get_unique_id
123
 * NAME
124
 *   get_unique_id - get a unique identifier
125
 * RESULT
126
 *   string     - unique id
127
 ******/
128
function get_unique_id() {
129

    
130
	return uniqid("nat_", true);
131
}
132

    
133
?>
(28-28/62)