File: Internals.pm

package info (click to toggle)
ocsinventory-server 2.8.1%2Bdfsg1-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,684 kB
  • sloc: php: 37,167; javascript: 28,347; perl: 8,234; sql: 2,725; sh: 1,636; xml: 1,071; python: 77; makefile: 29
file content (237 lines) | stat: -rw-r--r-- 7,247 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
###############################################################################
## Copyright 2005-2016 OCSInventory-NG/OCSInventory-Server contributors.
## See the Contributors file for more details about them.
##
## This file is part of OCSInventory-NG/OCSInventory-ocsreports.
##
## OCSInventory-NG/OCSInventory-Server is free software: you can redistribute
## it and/or modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation, either version 2 of the License,
## or (at your option) any later version.
##
## OCSInventory-NG/OCSInventory-Server is distributed in the hope that it
## will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with OCSInventory-NG/OCSInventory-ocsreports. if not, write to the
## Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
## MA 02110-1301, USA.
################################################################################
package Apache::Ocsinventory::Interface::Internals;

use strict;

require Exporter;

# If SOAP lite doesn't decode xml entities
eval {
  require XML::Entities;
};
if($@){
  print STDERR "[".localtime()."] OCSINVENTORY: (SOAP): Cannot find XML::Entities\n";
}

use Apache::Ocsinventory::Map;
use Apache::Ocsinventory::Interface::Database;
use XML::Simple;

our @ISA = qw /Exporter/;

our @EXPORT = qw /
  build_xml_standard_section
  decode_xml
  encode_xml
  search_engine
  send_error
  get_custom_field_name_map
  get_custom_fields_values_map
/;

sub decode_xml{
  my $data = shift;
  unless( $data =~ /^</ ){
    return XML::Entities::decode('all', $data);
  }
  return $data;
}

sub search_engine{
  # Available search engines
  my $engine = shift;
  my %search_engines = (
    'first'  => \&engine_first,
  );
  &{ $search_engines{ (lc $engine) } }( @_ );
}

sub engine_first {
  my ($request, $ids, $begin, $main_table, $accountinfo_table, $deviceid_column, $pk, $sort_by, $sort_dir) = @_;
  my $parsed_request = XML::Simple::XMLin( $request, ForceArray => ['ID', 'EXCLUDE_ID', 'TAG', 'EXCLUDE_TAG', 'USERID'], SuppressEmpty => 1 ) or die;
  my ($id, $name, $userid, $checksum, $tag);

  # Database ids criteria
  die("BAD_REQUEST") if ( $parsed_request->{ID} and $parsed_request->{EXCLUDE_ID} );

  if( $parsed_request->{ID} ){
    if( my @ids = untaint_int_lst( @{ $parsed_request->{ID} } )){
      $id .= ' AND';
      $id .= ' '.$main_table.'.ID IN('.join(',', @ids ).')';
    }
  }

  if( $parsed_request->{EXCLUDE_ID} ){
    if( my @exclude_ids = untaint_int_lst( @{ $parsed_request->{EXCLUDE_ID} } )){
      $id .= ' AND';
      $id .= ' '.$main_table.'.ID NOT IN('.join(',', @exclude_ids ).')';
    }
  }

  # Tag criteria
  die("BAD_REQUEST") if ( $parsed_request->{TAG} and $parsed_request->{EXCLUDE_TAG} );

  if( $parsed_request->{TAG} ){
    if( my @tags = untaint_dbstring_lst( @{ $parsed_request->{TAG} } )){
      $tag .= ' AND';
      $tag .= ' '.$accountinfo_table.'.TAG IN("'.join('","', @tags ).'")';
    }
  }

  if( $parsed_request->{EXCLUDE_TAG} ){
    if( my @exclude_tags = untaint_dbstring_lst( @{ $parsed_request->{EXCLUDE_TAG} } )){
      $tag .= ' AND';
      $tag .= ' '.$accountinfo_table.'.TAG NOT IN("'.join('","', @exclude_tags ).'")';
    }
  }

  # Checksum criteria (only positive "&" will match
  if( $parsed_request->{CHECKSUM} ){
    die("BAD_CHECKSUM") if !untaint_int( $parsed_request->{CHECKSUM} );
    $checksum = ' AND ('.$parsed_request->{CHECKSUM}.' & '.$main_table.'.CHECKSUM)';
  }
  # Associated user criteria
  if( $parsed_request->{USERID} && $main_table =~ /^hardware$/){
    if( my @users_id =  untaint_dbstring_lst( @{ $parsed_request->{USERID} } ) ){
      $userid .= ' AND';
      $userid .= ' '.$main_table.'.USERID IN("'.join('","', @users_id ).'")';
    }
  }
  # Generate sql string
  my $search_string = "SELECT DISTINCT $main_table.ID FROM $main_table,$accountinfo_table WHERE $main_table.$deviceid_column NOT LIKE '\\_%' AND $main_table.ID=$accountinfo_table.$pk $id $name $userid $checksum $tag ORDER BY hardware.$sort_by $sort_dir limit $begin,$ENV{OCS_OPT_WEB_SERVICE_RESULTS_LIMIT}";
  # Play it
  my $sth = get_sth($search_string);
  # Get ids
  while( my $row = $sth->fetchrow_hashref() ){
    push @{$ids}, $row->{ID};
  }
  # Destroy request object
  $sth->finish();
}


# Build a database mapped inventory section
sub build_xml_standard_section{
  my ($id, $main_table, $xml_ref, $section) = @_;
  my %element;
  my @tmp;

  my %get_table_pk_functions = (
    'hardware' => \&get_hardware_table_pk,
    'snmp' => \&get_snmp_table_pk
  );

  # Request database
  my $deviceid = &{ $get_table_pk_functions{ $main_table } }($section);
  my $sth = get_sth("SELECT * FROM $section WHERE $deviceid=?", $id);

  # Build data structure...
  while ( my $row = $sth->fetchrow_hashref() ){
    for( keys(%{$DATA_MAP{ $section }->{fields}}) ){
      next if $DATA_MAP{ $section }->{fields}->{$_}->{noSql};
      # New DB schema support
      if( $DATA_MAP{ $section }->{fields}->{$_}->{type} ){
        my $field = $_;
        $field =~ s/_ID//g;    #We delete the _ID pattern to be in concordance with table name
        $row->{ $_ } = get_type_name($section, $field, $row->{ $_ });
        $element{$field} = [ $row->{ $_ } ];
      }
      else {
        $element{$_} = [ $row->{ $_ } ];
      }
    }

    push @tmp, { %element };
    %element = ();
  }
  $section =~ s/$section/\U$&/g;
  $xml_ref->{$section}=[ @tmp ];
  @tmp = ();
  $sth->finish;
}

sub reset_checksum {
  my( $checksum, $ref ) = @_;
  my $where = join(',', @$ref);
  return do_sql("UPDATE hardware SET CHECKSUM=? WHERE ID IN ($where)", $checksum);
}
sub send_error{
  my $error = shift;
  return XMLout (
    { 'ERROR' => [ $error ] },
    RootName => 'RESULT'
  );
}

sub untaint_int_lst{
  my @list = @_;
  my @cleared;
  for (@list){
    push @cleared, $_ if untaint_int($_);
  }
  return @cleared;
}

sub untaint_int{
  my $int = shift;
  return $int =~ /^\d+$/;
}

# Helper for resolving custom field names in ACCOUNTINFO
sub get_custom_field_name_map {
    my $account_type = shift;
    my $name_map = {};

    # Request database
    my $sth = get_sth('SELECT ID, NAME FROM accountinfo_config WHERE NAME_ACCOUNTINFO IS NULL AND ACCOUNT_TYPE=?', $account_type);
    # Build data structure...
    my $rows = $sth->fetchall_arrayref();
    foreach my $row ( @$rows ) {
      $name_map->{ "fields_" . $row->[0] } = $row->[1];
    }
    $sth->finish;
    return $name_map;
}

# Helper for resolving textual values of custom fields
# of type CHECKBOX, RADIOBUTTON, SELECT
sub get_custom_fields_values_map {
  my $account_value = shift;
  my $values_map = {};

  # Request database
  my $sth = get_sth('SELECT NAME, IVALUE, TVALUE FROM config WHERE NAME LIKE ?', $account_value."_%_%");
  my $rows = $sth->fetchall_arrayref();

  my $regexp = $account_value."_(.*)_[0-9]+";

  foreach my $row ( @$rows ) {
    if ($row->[0] =~ /^$regexp$/) {
      $values_map->{ $1 }->{ $row->[1] } = $row->[2];
    }
  }
  $sth->finish;
  return $values_map;
}

1;