Code

like operator for db queries, closes 392
[gosa.git] / gosa-si / modules / GosaSupportDaemon.pm
1 package GOSA::GosaSupportDaemon;
3 use Exporter;
4 @ISA = qw(Exporter);
5 my @functions = (
6     "create_xml_hash",
7     "get_content_from_xml_hash",
8     "add_content2xml_hash",
9     "create_xml_string",
10     "transform_msg2hash",
11     "get_time",
12     "build_msg",
13     "db_res2xml",
14     "db_res2si_msg",
15     "get_where_statement",
16     "get_select_statement",
17     "get_update_statement",
18     "get_limit_statement",
19     "get_orderby_statement",
20     "get_dns_domains",
21     "get_logged_in_users",
22     ); 
23 @EXPORT = @functions;
24 use strict;
25 use warnings;
26 use IO::Socket::INET;
27 use Crypt::Rijndael;
28 use Digest::MD5  qw(md5 md5_hex md5_base64);
29 use MIME::Base64;
30 use XML::Simple;
32 my $op_hash = {
33     'eq' => '=',
34     'ne' => '!=',
35     'ge' => '>=',
36     'gt' => '>',
37     'le' => '<=',
38     'lt' => '<',
39     'like' => ' LIKE ',
40 };
43 BEGIN {}
45 END {}
47 ### Start ######################################################################
49 my $xml = new XML::Simple();
51 sub daemon_log {
52     my ($msg, $level) = @_ ;
53     &main::daemon_log($msg, $level);
54     return;
55 }
60 #===  FUNCTION  ================================================================
61 #         NAME:  create_xml_hash
62 #   PARAMETERS:  header - string - message header (required)
63 #                source - string - where the message come from (required)
64 #                target - string - where the message should go to (required)
65 #                [header_value] - string - something usefull (optional)
66 #      RETURNS:  hash - hash - nomen est omen
67 #  DESCRIPTION:  creates a key-value hash, all values are stored in a array
68 #===============================================================================
69 sub create_xml_hash {
70     my ($header, $source, $target, $header_value) = @_;
71     my $hash = {
72             header => [$header],
73             source => [$source],
74             target => [$target],
75             $header => [$header_value],
76     };
77     return $hash
78 }
81 #===  FUNCTION  ================================================================
82 #         NAME:  create_xml_string
83 #   PARAMETERS:  xml_hash - hash - hash from function create_xml_hash
84 #      RETURNS:  xml_string - string - xml string representation of the hash
85 #  DESCRIPTION:  transform the hash to a string using XML::Simple module
86 #===============================================================================
87 sub create_xml_string {
88     my ($xml_hash) = @_ ;
89     my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
90     #$xml_string =~ s/[\n]+//g;
91     #daemon_log("create_xml_string:",7);
92     #daemon_log("$xml_string\n", 7);
93     return $xml_string;
94 }
97 sub transform_msg2hash {
98     my ($msg) = @_ ;
99     my $hash = $xml->XMLin($msg, ForceArray=>1);
100     
101     # xml tags without a content are created as an empty hash
102     # substitute it with an empty list
103     eval {
104         while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
105             if( 1 == @{ $xml_content } ) {
106                 # there is only one element in xml_content list ...
107                 my $element = @{ $xml_content }[0];
108                 if( ref($element) eq "HASH" ) {
109                     # and this element is an hash ...
110                     my $len_element = keys %{ $element };
111                     if( $len_element == 0 ) {
112                         # and this hash is empty, then substitute the xml_content
113                         # with an empty string in list
114                         $hash->{$xml_tag} = [ "none" ];
115                     }
116                 }
117             }
118         }
119     };
120     if( $@ ) {  
121         $hash = undef;
122     }
124     return $hash;
128 #===  FUNCTION  ================================================================
129 #         NAME:  add_content2xml_hash
130 #   PARAMETERS:  xml_ref - ref - reference to a hash from function create_xml_hash
131 #                element - string - key for the hash
132 #                content - string - value for the hash
133 #      RETURNS:  nothing
134 #  DESCRIPTION:  add key-value pair to xml_ref, if key alread exists, 
135 #                then append value to list
136 #===============================================================================
137 sub add_content2xml_hash {
138     my ($xml_ref, $element, $content) = @_;
139     if(not exists $$xml_ref{$element} ) {
140         $$xml_ref{$element} = [];
141     }
142     my $tmp = $$xml_ref{$element};
143     push(@$tmp, $content);
144     return;
148 sub get_time {
149     my ($seconds, $minutes, $hours, $monthday, $month,
150             $year, $weekday, $yearday, $sommertime) = localtime(time);
151     $hours = $hours < 10 ? $hours = "0".$hours : $hours;
152     $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
153     $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
154     $month+=1;
155     $month = $month < 10 ? $month = "0".$month : $month;
156     $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
157     $year+=1900;
158     return "$year$month$monthday$hours$minutes$seconds";
163 #===  FUNCTION  ================================================================
164 #         NAME: build_msg
165 #  DESCRIPTION: Send a message to a destination
166 #   PARAMETERS: [header] Name of the header
167 #               [from]   sender ip
168 #               [to]     recipient ip
169 #               [data]   Hash containing additional attributes for the xml
170 #                        package
171 #      RETURNS:  nothing
172 #===============================================================================
173 sub build_msg ($$$$) {
174         my ($header, $from, $to, $data) = @_;
176         my $out_hash = &create_xml_hash($header, $from, $to);
178         while ( my ($key, $value) = each(%$data) ) {
179                 if(ref($value) eq 'ARRAY'){
180                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
181                 } else {
182                         &add_content2xml_hash($out_hash, $key, $value);
183                 }
184         }
185     my $out_msg = &create_xml_string($out_hash);
186     return $out_msg;
190 sub db_res2xml {
191     my ($db_res) = @_ ;
192     my $xml = "";
194     my $len_db_res= keys %{$db_res};
195     for( my $i= 1; $i<= $len_db_res; $i++ ) {
196         $xml .= "\n<answer$i>";
197         my $hash= $db_res->{$i};
198         while ( my ($column_name, $column_value) = each %{$hash} ) {
199             $xml .= "<$column_name>";
200             my $xml_content;
201             if( $column_name eq "xmlmessage" ) {
202                 $xml_content = &encode_base64($column_value);
203             } else {
204                 $xml_content = $column_value;
205             }
206             $xml .= $xml_content;
207             $xml .= "</$column_name>"; 
208         }
209         $xml .= "</answer$i>";
211     }
213     return $xml;
217 sub db_res2si_msg {
218     my ($db_res, $header, $target, $source) = @_;
220     my $si_msg = "<xml>";
221     $si_msg .= "<header>$header</header>";
222     $si_msg .= "<source>$source</source>";
223     $si_msg .= "<target>$target</target>";
224     $si_msg .= &db_res2xml;
225     $si_msg .= "</xml>";
229 sub get_where_statement {
230     my ($msg, $msg_hash) = @_;
231     my $error= 0;
232     
233     my $clause_str= "";
234     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { 
235         $error++; 
236     }
238     if( $error == 0 ) {
239         my @clause_l;
240         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
241         foreach my $clause (@where) {
242             my $connector = $clause->{'connector'}[0];
243             if( not defined $connector ) { $connector = "AND"; }
244             $connector = uc($connector);
245             delete($clause->{'connector'});
247             my @phrase_l ;
248             foreach my $phrase (@{$clause->{'phrase'}}) {
249                 my $operator = "=";
250                 if( exists $phrase->{'operator'} ) {
251                     my $op = $op_hash->{$phrase->{'operator'}[0]};
252                     if( not defined $op ) {
253                         &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
254                                 "statement to sql valid syntax. Please use 'eq', ".
255                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
256                         &main::daemon_log($msg, 8);
257                         $op = "=";
258                     }
259                     $operator = $op;
260                     delete($phrase->{'operator'});
261                 }
263                 my @xml_tags = keys %{$phrase};
264                 my $tag = $xml_tags[0];
265                 my $val = $phrase->{$tag}[0];
266                 push(@phrase_l, "$tag$operator'$val'");
267             }
268             my $clause_str .= join(" $connector ", @phrase_l);
269             push(@clause_l, $clause_str);
270         }
272         if( not 0 == @clause_l ) {
273             $clause_str = join(" AND ", @clause_l);
274             $clause_str = "WHERE ($clause_str) ";
275         }
276     }
278     return $clause_str;
281 sub get_select_statement {
282     my ($msg, $msg_hash)= @_;
283     my $select = "*";
284     if( exists $msg_hash->{'select'} ) {
285         my $select_l = \@{$msg_hash->{'select'}};
286         $select = join(' AND ', @{$select_l});
287     }
288     return $select;
292 sub get_update_statement {
293     my ($msg, $msg_hash) = @_;
294     my $error= 0;
295     my $update_str= "";
296     my @update_l; 
298     if( not exists $msg_hash->{'update'} ) { $error++; };
300     if( $error == 0 ) {
301         my $update= @{$msg_hash->{'update'}}[0];
302         while( my ($tag, $val) = each %{$update} ) {
303             my $val= @{$update->{$tag}}[0];
304             push(@update_l, "$tag='$val'");
305         }
306         if( 0 == @update_l ) { $error++; };   
307     }
309     if( $error == 0 ) { 
310         $update_str= join(', ', @update_l);
311         $update_str= "SET $update_str ";
312     }
314     return $update_str;
317 sub get_limit_statement {
318     my ($msg, $msg_hash)= @_; 
319     my $error= 0;
320     my $limit_str = "";
321     my ($from, $to);
323     if( not exists $msg_hash->{'limit'} ) { $error++; };
325     if( $error == 0 ) {
326         eval {
327             my $limit= @{$msg_hash->{'limit'}}[0];
328             $from= @{$limit->{'from'}}[0];
329             $to= @{$limit->{'to'}}[0];
330         };
331         if( $@ ) {
332             $error++;
333         }
334     }
336     if( $error == 0 ) {
337         $limit_str= "LIMIT $from, $to";
338     }   
339     
340     return $limit_str;
343 sub get_orderby_statement {
344     my ($msg, $msg_hash)= @_;
345     my $error= 0;
346     my $order_str= "";
347     my $order;
348     
349     if( not exists $msg_hash->{'orderby'} ) { $error++; };
351     if( $error == 0) {
352         eval {
353             $order= @{$msg_hash->{'orderby'}}[0];
354         };
355         if( $@ ) {
356             $error++;
357         }
358     }
360     if( $error == 0 ) {
361         $order_str= "ORDER BY $order";   
362     }
363     
364     return $order_str;
367 sub get_dns_domains() {
368         my $line;
369         my @searches;
370         open(RESOLV, "</etc/resolv.conf") or return @searches;
371         while(<RESOLV>){
372                 $line= $_;
373                 chomp $line;
374                 $line =~ s/^\s+//;
375                 $line =~ s/\s+$//;
376                 $line =~ s/\s+/ /;
377                 if ($line =~ /^domain (.*)$/ ){
378                         push(@searches, $1);
379                 } elsif ($line =~ /^search (.*)$/ ){
380                         push(@searches, split(/ /, $1));
381                 }
382         }
383         close(RESOLV);
385         my %tmp = map { $_ => 1 } @searches;
386         @searches = sort keys %tmp;
388         return @searches;
392 sub get_logged_in_users {
393     my $result = qx(/usr/bin/w -hs);
394     my @res_lines;
396     if( defined $result ) { 
397         chomp($result);
398         @res_lines = split("\n", $result);
399     }
401     my @logged_in_user_list;
402     foreach my $line (@res_lines) {
403         chomp($line);
404         my @line_parts = split(/\s+/, $line); 
405         push(@logged_in_user_list, $line_parts[0]);
406     }
408     return @logged_in_user_list;
412 1;