ona  18.1.1
About: OpenNetAdmin provides a database managed inventory of your IP network (with Web and CLI interface).
  Fossies Dox: ona-18.1.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

tag.inc.php
Go to the documentation of this file.
1 <?php
2 // DON'T put whitespace at the beginning or end of this file!!!
3 
4 
5 
7 // Function: tag_add (string $options='')
8 //
9 // $options = key=value pairs of options for this function.
10 // multiple sets of key=value pairs should be separated
11 // by an "&" symbol.
12 //
13 // Input Options:
14 // name=STRING
15 // type=STRING
16 // reference=NUMBER
17 //
18 // Output:
19 // Adds an tag into the database called 'name'
20 // Returns a two part list:
21 // 1. The exit status of the function. 0 on success, non-zero on
22 // error. All errors messages are stored in $self['error'].
23 // 2. A textual message for display on the console or web interface.
24 //
25 // Example: list($status, $result) = tag_add('name=test&type=blah&reference=1');
27 function tag_add($options="") {
28 
29  // The important globals
30  global $conf, $self, $onadb;
31 
32  // Version - UPDATE on every edit!
33  $version = '1.01';
34 
35  printmsg("DEBUG => tag_add({$options}) called", 3);
36 
37  // Parse incoming options string to an array
38  $options = parse_options($options);
39 
40  // Possible types
41  $allowed_types = array('subnet', 'host');
42 
43  $typetext=implode(', ',$allowed_types);
44  // Return the usage summary if we need to
45  if ($options['help'] or !($options['type'] and $options['name'] and $options['reference']) ) {
46  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
47  $self['error'] = 'ERROR => Insufficient parameters';
48  return(array(1,
49 <<<EOM
50 
52 Adds a tag into the database assigned to the specified type of data.
53 
54  Synopsis: tag_add [KEY=VALUE] ...
55 
56  Required:
57  name=STRING Name of new tag.
58  type=STRING Type of thing to tag, Possible types listed below.
59  reference=ID|STRING Reference to apply the tag to. ID or name used to find
60  a record to attatch to.
61 
62  Possible types of tags:
63  {$typetext}
64 \n
65 EOM
66 
67  ));
68  }
69 
70  // Check if provided type is in the allowed types
71  $options['type'] = strtolower(trim($options['type']));
72  if (!in_array($options['type'], $allowed_types)) {
73  $self['error'] = "ERROR => Invalid tag type: {$options['type']}";
74  printmsg($self['error'], 0);
75  return(array(1, $self['error'] . "\n"));
76  }
77 
78  // The formatting rule on tag input
79  $options['name'] = preg_replace('/\s+/', '-', trim($options['name']));
80  if (preg_match('/[@$%^*!\|,`~<>{}]+/', $options['name'])) {
81  $self['error'] = "ERROR => Invalid character in tag name";
82  printmsg($self['error'], 0);
83  return(array(1, $self['error'] . "\n"));
84  }
85  $options['reference'] = (trim($options['reference']));
86 
87  // Use the find functions based on the type
88  // this requires allowed types to have an 'ona_find_' related function
89  eval("list(\$status, \$rows, \$reference) = ona_find_".$options['type']."('".$options['reference']."');");
90 
91  if ($status or !$rows) {
92  $self['error'] = "ERROR => Unable to find a {$options['type']} matching {$options['reference']}";
93  printmsg($self['error'], 0);
94  return(array(1, $self['error'] . "\n"));
95  }
96 
97  // Validate that there isn't already an tag of this type associated to the reference
98  list($status, $rows, $tags) = db_get_records($onadb, 'tags',array('type' => $options['type'],'reference' => $reference['id']));
99 
100  foreach ($tags as $t) {
101  if (in_array($options['name'], $t)) {
102  printmsg("DEBUG => The tag {$options['name']} is already associated with this {$options['type']}!",3);
103  $self['error'] = "ERROR => The tag {$options['name']} is already associated with this {$options['type']}!";
104  return(array(3, $self['error'] . "\n"));
105  }
106  }
107 
108 
109  // Check permissions
110  if (! (auth('subnet_add') or auth('host_add')) ) {
111  $self['error'] = "Permission denied!";
112  printmsg($self['error'], 0);
113  return(array(10, $self['error'] . "\n"));
114  }
115 
116  // Get the next ID for the new tag
117  $id = ona_get_next_id('tags');
118  if (!$id) {
119  $self['error'] = "ERROR => The ona_get_next_id() call failed!";
120  printmsg($self['error'],0);
121  return(array(5, $self['error'] . "\n"));
122  }
123  printmsg("DEBUG => ID for new tag: $id", 3);
124 
125  // Add the tag
126  list($status, $rows) =
128  $onadb,
129  'tags',
130  array(
131  'id' => $id,
132  'name' => $options['name'],
133  'type' => $options['type'],
134  'reference' => $reference['id']
135  )
136  );
137  if ($status or !$rows) {
138  $self['error'] = "ERROR => tag_add() SQL Query failed: " . $self['error'];
139  printmsg($self['error'], 0);
140  return(array(6, $self['error'] . "\n"));
141  }
142 
143  // Return the success notice
144  $self['error'] = "INFO => {$options['type']} TAG ADDED: {$options['name']} to {$reference['name']}({$reference['id']}).";
145  printmsg($self['error'],0);
146  return(array(0, $self['error'] . "\n"));
147 }
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
161 // Function: tag_del (string $options='')
162 //
163 // $options = key=value pairs of options for this function.
164 // multiple sets of key=value pairs should be separated
165 // by an "&" symbol.
166 //
167 // Input Options:
168 // tag=ID
169 //
170 // Output:
171 // Deletes an tag from the database.
172 // Returns a two part list:
173 // 1. The exit status of the function. 0 on success, non-zero on
174 // error. All errors messages are stored in $self['error'].
175 // 2. A textual message for display on the console or web interface.
176 //
177 // Example: list($status, $result) = tag_del('tag=19328');
179 function tag_del($options="") {
180 
181  // The important globals
182  global $conf, $self, $onadb;
183 
184  // Version - UPDATE on every edit!
185  $version = '1.00';
186 
187  printmsg("DEBUG => tag_del({$options}) called", 3);
188 
189  // Parse incoming options string to an array
190  $options = parse_options($options);
191 
192  // Return the usage summary if we need to
193  if ($options['help'] or !$options['tag'] ) {
194  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
195  $self['error'] = 'ERROR => Insufficient parameters';
196  return(array(1,
197 <<<EOM
198 
199 tag_del-v{$version}
200 Deletes an tag from the database
201 
202  Synopsis: tag_del [KEY=VALUE] ...
203 
204  Required:
205  tag=ID ID of the tag to delete
206 
207  Optional:
208  commit=[yes|no] commit db transaction (no)
209 
210 \n
211 EOM
212 
213  ));
214  }
215 
216 
217  // Sanitize options[commit] (default is no)
218  $options['commit'] = sanitize_YN($options['commit'], 'N');
219 
220  // If the tag provided is numeric, check to see if it's an tag
221  if (is_numeric($options['tag'])) {
222  // See if it's a tag_id
223  list($status, $rows, $tag) = db_get_record($onadb,'tags', array('id' => $options['tag']));
224  }
225 
226  if (!$tag['id']) {
227  printmsg("DEBUG => Unable to find tag ({$options['tag']})!",3);
228  $self['error'] = "ERROR => Unable to find tag ({$options['tag']})!";
229  return(array(2, $self['error'] . "\n"));
230  }
231 
232 
233  // If "commit" is yes, delete the record
234  if ($options['commit'] == 'Y') {
235 
236  // Check permissions
237  if (! (auth('host_del') or auth('subnet_del')) ) {
238  $self['error'] = "Permission denied!";
239  printmsg($self['error'], 0);
240  return(array(10, $self['error'] . "\n"));
241  }
242 
243  list($status, $rows) = db_delete_records($onadb, 'tags', array('id' => $tag['id']));
244  if ($status or !$rows) {
245  $self['error'] = "ERROR => tag_del() SQL Query failed: " . $self['error'];
246  printmsg($self['error'], 0);
247  return(array(4, $self['error'] . "\n"));
248  }
249 
250  // Return the success notice
251  $self['error'] = "INFO => TAG DELETED: {$tag['name']} from {$tag['type']}[{$tag['reference']}]";
252  printmsg($self['error'],0);
253  return(array(0, $self['error'] . "\n"));
254  }
255 
256 
257  // Otherwise display the record that would have been deleted
258  $text = <<<EOL
259 Record(s) NOT DELETED (see "commit" option)
260 Displaying record(s) that would have been deleted:
261 
262  NAME: {$tag['name']}
263  TYPE: {$tag['type']}
264  REFERENCE: {$tag['reference']}
265 
266 
267 EOL;
268 
269  return(array(6, $text));
270 
271 }
272 
273 
274 
275 
276 
277 
278 // DON'T put whitespace at the beginning or end of this file!!!
279 ?>
db_insert_record
db_insert_record($dbh=0, $table="", $insert="")
Definition: functions_db.inc.php:375
sanitize_YN
sanitize_YN($string="", $default="Y")
Definition: functions_general.inc.php:1637
ona_get_next_id
ona_get_next_id($tablename)
Definition: functions_db.inc.php:1369
Name
Full Name
Definition: app_user_info.inc.php:101
db_get_records
db_get_records($dbh=0, $table="", $where="", $order="", $rows=-1, $offset=-1)
Definition: functions_db.inc.php:891
$status
$status
Definition: install.php:12
$onadb
global $onadb
Definition: 2-to-3.php:15
db_delete_records
db_delete_records($dbh=0, $table="", $where="")
Definition: functions_db.inc.php:582
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
tag_del
tag_del($options="")
Definition: tag.inc.php:179
EOL
< a style="text-decoration: none;" href="/">< input class='edit' type="button" value="I don't like free stuff?" onclick=""/></a ></center ></div > EOL
Definition: install.php:40
tag_add
tag_add($options="")
Definition: tag.inc.php:27
$conf
global $conf
Definition: 2-to-3.php:15
$text
$text
Definition: install.php:11
$self
global $self
Definition: 2-to-3.php:15
db_get_record
db_get_record($dbh=0, $table="", $where="", $order="")
Definition: functions_db.inc.php:708
auth
auth($resource, $msg_level=1)
Definition: functions_general.inc.php:1330
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22