Project

General

Profile

Download (3.14 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * itemid.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2009-2016 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
	$a_separators = &$config['filter']['separator'][strtolower($if)];
52
	// get rule index within interface
53
	$ridx = ifridx($if, $delete_index);
54
	$mvnrows = -1;
55
	move_separators($a_separators, $ridx, $mvnrows);
56

    
57
	return true;
58
}
59

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

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

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

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

    
85
	if ($status) {
86
		unset($array[$toggle_index]['disabled']);
87
	} else {
88
		$array[$toggle_index]['disabled'] = true;
89
	}
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
?>
(22-22/51)