Project

General

Profile

Download (2.83 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/*
4
	Copyright (C) 2009 Janne Enberg <janne.enberg@lietu.net>
5
	All rights reserved.
6

    
7
	Redistribution and use in source and binary forms, with or without
8
	modification, are permitted provided that the following conditions are met:
9

    
10
	1. Redistributions of source code must retain the above copyright notice,
11
	   this list of conditions and the following disclaimer.
12

    
13
	2. Redistributions in binary form must reproduce the above copyright
14
	   notice, this list of conditions and the following disclaimer in the
15
	   documentation and/or other materials provided with the distribution.
16

    
17
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
	POSSIBILITY OF SUCH DAMAGE.
27

    
28
*/
29

    
30
/****f* itemid/delete_id
31
 * NAME
32
 *   delete_id - delete an item with ['id'] = $id from $array
33
 * INPUTS
34
 *   $id       - int: The ID to delete
35
 *   $array    - array to delete the item from
36
 * RESULT
37
 *   boolean   - true if item was found and deleted
38
 ******/
39
function delete_id($id, &$array) {
40
	// Index to delete
41
	$delete_index = NULL;
42

    
43
	if (!is_array($array)) {
44
		return false;
45
	}
46

    
47
	// Search for the item in the array
48
	foreach ($array as $key => $item) {
49
		// If this item is the one we want to delete
50
		if (isset($item['associated-rule-id']) && $item['associated-rule-id'] == $id) {
51
			$delete_index = $key;
52
			break;
53
		}
54
	}
55

    
56
	// If we found the item, unset it
57
	if ($delete_index !== NULL) {
58
		unset($array[$delete_index]);
59
		return true;
60
	} else {
61
		return false;
62
	}
63

    
64
}
65

    
66
/****f* itemid/get_id
67
 * NAME
68
 *   get_id - Get an item id with ['associated-rule-id'] = $id from $array
69
 * INPUTS
70
 *   $id       - string: The ID to get
71
 *   $array    - array to get the item from
72
 * RESULT
73
 *   mixed   - The id, NULL if not found
74
 ******/
75
function get_id($id, &$array) {
76
	// Use $foo = &get_id('id', array('id'=>'value'));
77

    
78
	if (!is_array($array)) {
79
		return false;
80
	}
81

    
82
	// Search for the item in the array
83
	foreach ($array as $key => $item) {
84
		// If this item is the one we want to delete
85
		if (isset($item['associated-rule-id']) && $item['associated-rule-id'] == $id) {
86
			return $key;
87
		}
88
	}
89

    
90
	return false;
91
}
92

    
93
/****f* itemid/get_unique_id
94
 * NAME
95
 *   get_unique_id - get a unique identifier
96
 * RESULT
97
 *   string     - unique id
98
 ******/
99
function get_unique_id() {
100

    
101
	return uniqid("nat_", true);
102
}
103

    
104
?>
(29-29/65)