Project

General

Profile

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

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

    
35
	if (!is_array($array)) {
36
		return false;
37
	}
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
	return true;
59
}
60

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

    
74
	if (!is_array($array)) {
75
		return false;
76
	}
77

    
78
	// Search for the item in the array
79
	$toggle_index = get_id($id, $array);
80

    
81
	// If we found the item, unset it
82
	if ($toggle_index === false) {
83
		return false;
84
	}
85

    
86
	if ($status) {
87
		unset($array[$toggle_index]['disabled']);
88
	} else {
89
		$array[$toggle_index]['disabled'] = true;
90
	}
91

    
92
	return true;
93
}
94

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

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

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

    
120
	return false;
121
}
122

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

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

    
134
?>
(28-28/60)