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)  

message.inc.php
Go to the documentation of this file.
1 <?php
2 
3 // Make sure we have necessary functions & DB connectivity
4 require_once($conf['inc_functions_db']);
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
21 // Function: message_add (string $options='')
22 //
23 // Input Options:
24 // $options = key=value pairs of options for this function.
25 // multiple sets of key=value pairs should be separated
26 // by an "&" symbol.
27 //
28 // Output:
29 // Returns a two part list:
30 // 1. The exit status of the function (0 on success, non-zero on error)
31 // 2. A textual message for display on the console or web interface.
32 //
33 // Example: list($status, $result) = message_add('host=test');
34 //
35 // Exit codes:
36 // 0 :: No error
37 // 1 :: Help text printed - Insufficient or invalid input received
38 // 4 :: SQL Query failed
39 //
40 //
41 // History:
42 //
43 //
45 function message_add($options="") {
46 
47  // The important globals
48  global $conf, $self, $onadb;
49 
50  // Version - UPDATE on every edit!
51  $version = '1.00';
52 
53  // Default expiration
54  $exp_default = "+6 week";
55  $pri_default = 3;
56 
57  // Priority is one of the following:
58  // 0 = Informational
59  // 1 = red or high
60  // 2 = yellow or medium
61  // 3 = green or low
62 
63  printmsg("DEBUG => message_add({$options}) called", 3);
64 
65  // Parse incoming options string to an array
66  $options = parse_options($options);
67 
68  // Return the usage summary if we need to
69  if ($options['help'] or
70  (!$options['subnet'] and !$options['host']) or
71  (!$options['message']) and
72  (!$options['expiration'] and
73  !$options['priority']
74  ) ) {
75  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
76  $self['error'] = 'ERROR => Insufficient parameters';
77  return(array(1,
78 <<<EOM
79 
81 Adds the provided message to the host or subnet specified
82 
83  Synopsis: message_add
84 
85  Required:
86  host=NAME[.DOMAIN]|IP hostname or IP of the host
87  OR
88  subnet=NAME|IP name or IP of the subnet
89 
90  message="STRING" the content of the message
91 
92  Optional:
93  priority=NUMBER device/model type or ID (default: {$pri_default})
94  expiration=DATE date to expire message (default: NOW {$exp_default}s)
95 
96  Notes:
97  Priority is one of the following:
98  0 = blue or Informational
99  1 = red or high
100  2 = yellow or medium
101  3 = green or low
102 
103 \n
104 EOM
105 
106  ));
107  }
108 
109 
110  // If they provided a hostname / ID let's look it up
111  if ($options['host']) {
112  list($status, $rows, $host) = ona_find_host($options['host']);
113  $table_name_ref = 'hosts';
114  $table_id_ref = $host['id'];
115  $desc = $host['fqdn'];
116  }
117 
118  // If they provided a subnet name or ip
119  else if ($options['subnet']) {
120  list($status, $rows, $subnet) = ona_find_subnet($options['subnet']);
121  $table_name_ref = 'subnets';
122  $table_id_ref = $subnet['id'];
123  $desc = $subnet['name'];
124  }
125 
126  // If we didn't get a record then exit
127  if (!$host['id'] and !$subnet['id']) {
128  printmsg("DEBUG => No host or subnet found!",3);
129  $self['error'] = "ERROR => No host or subnet found!";
130  return(array(4, $self['error'] . "\n"));
131  }
132 
133  // Set the priority
134  $priority = (array_key_exists('priority',$options)) ? $options['priority'] : $pri_default;
135 
136  if ($priority > 3 or $priority < 0 or !is_numeric($priority) ) {
137  $self['error'] = "ERROR => Priority must be a number between 0 and 3!";
138  return(array(4, $self['error'] . "\n"));
139  }
140 
141  // Get a username or "anonymous"
142  $username = (isset($_SESSION['username'])) ? $_SESSION['username'] : "anonymous";
143 
144  // Expiration date format
145  if ($options['expiration']) {
146  $expiration = date("Y-m-d G:i:s", strtotime($options['expiration']));
147  }
148  else {
149  $expiration = date("Y-m-d G:i:s", strtotime($exp_default));
150  }
151 
152  // TODO: there should probably be some sort of security checks on the message that is passed in.
153  // I suspect this could be a security issue. SQL injection etc.
154  list($status, $rows) = db_insert_record(
155  $onadb,
156  'messages',
157  array(
158  'table_name_ref' => $table_name_ref,
159  'table_id_ref' => $table_id_ref,
160  'priority' => $priority,
161  'username' => $username,
162  'expiration' => $expiration,
163  'message_text' => $options['message']
164  )
165  );
166  if ($status or !$rows) {
167  $self['error'] = "ERROR => message_add() SQL Query failed: " . $self['error'];
168  printmsg($self['error'], 0);
169  return(array(6, $self['error'] . "\n"));
170  }
171 
172 
173  $text = "INFO => Message ADDED to: {$desc}\n";
174 
175 
176  // Return the message file
177  return(array(0, $text));
178 
179 }
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 ?>
db_insert_record
db_insert_record($dbh=0, $table="", $insert="")
Definition: functions_db.inc.php:375
ona_find_subnet
ona_find_subnet($search="")
Definition: functions_db.inc.php:2003
ona_find_host
ona_find_host($search="")
Definition: functions_db.inc.php:1490
$status
$status
Definition: install.php:12
$onadb
global $onadb
Definition: 2-to-3.php:15
message_add
message_add($options="")
Definition: message.inc.php:45
$username
$username
Definition: collate-convert.php:16
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
$host
$host
Definition: collate-convert.php:19
$_SESSION
$_SESSION['ona']['auth']
Definition: login.php:14
$conf
global $conf
Definition: 2-to-3.php:15
$text
$text
Definition: install.php:11
$self
global $self
Definition: 2-to-3.php:15
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22