Code

Updated SQL statement WHERE creation
[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;
31 use Data::Dumper;
33 my $op_hash = {
34     'eq' => '=',
35     'ne' => '!=',
36     'ge' => '>=',
37     'gt' => '>',
38     'le' => '<=',
39     'lt' => '<',
40     'like' => ' LIKE ',
41 };
44 BEGIN {}
46 END {}
48 ### Start ######################################################################
50 my $xml = new XML::Simple();
52 sub daemon_log {
53     my ($msg, $level) = @_ ;
54     &main::daemon_log($msg, $level);
55     return;
56 }
61 #===  FUNCTION  ================================================================
62 #         NAME:  create_xml_hash
63 #   PARAMETERS:  header - string - message header (required)
64 #                source - string - where the message come from (required)
65 #                target - string - where the message should go to (required)
66 #                [header_value] - string - something usefull (optional)
67 #      RETURNS:  hash - hash - nomen est omen
68 #  DESCRIPTION:  creates a key-value hash, all values are stored in a array
69 #===============================================================================
70 sub create_xml_hash {
71     my ($header, $source, $target, $header_value) = @_;
72     my $hash = {
73             header => [$header],
74             source => [$source],
75             target => [$target],
76             $header => [$header_value],
77     };
78     return $hash
79 }
82 #===  FUNCTION  ================================================================
83 #         NAME:  create_xml_string
84 #   PARAMETERS:  xml_hash - hash - hash from function create_xml_hash
85 #      RETURNS:  xml_string - string - xml string representation of the hash
86 #  DESCRIPTION:  transform the hash to a string using XML::Simple module
87 #===============================================================================
88 sub create_xml_string {
89     my ($xml_hash) = @_ ;
90     my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
91     #$xml_string =~ s/[\n]+//g;
92     #daemon_log("create_xml_string:",7);
93     #daemon_log("$xml_string\n", 7);
94     return $xml_string;
95 }
98 sub transform_msg2hash {
99     my ($msg) = @_ ;
100     my $hash = $xml->XMLin($msg, ForceArray=>1);
101     
102     # xml tags without a content are created as an empty hash
103     # substitute it with an empty list
104     eval {
105         while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
106             if( 1 == @{ $xml_content } ) {
107                 # there is only one element in xml_content list ...
108                 my $element = @{ $xml_content }[0];
109                 if( ref($element) eq "HASH" ) {
110                     # and this element is an hash ...
111                     my $len_element = keys %{ $element };
112                     if( $len_element == 0 ) {
113                         # and this hash is empty, then substitute the xml_content
114                         # with an empty string in list
115                         $hash->{$xml_tag} = [ "none" ];
116                     }
117                 }
118             }
119         }
120     };
121     if( $@ ) {  
122         $hash = undef;
123     }
125     return $hash;
129 #===  FUNCTION  ================================================================
130 #         NAME:  add_content2xml_hash
131 #   PARAMETERS:  xml_ref - ref - reference to a hash from function create_xml_hash
132 #                element - string - key for the hash
133 #                content - string - value for the hash
134 #      RETURNS:  nothing
135 #  DESCRIPTION:  add key-value pair to xml_ref, if key alread exists, 
136 #                then append value to list
137 #===============================================================================
138 sub add_content2xml_hash {
139     my ($xml_ref, $element, $content) = @_;
140     if(not exists $$xml_ref{$element} ) {
141         $$xml_ref{$element} = [];
142     }
143     my $tmp = $$xml_ref{$element};
144     push(@$tmp, $content);
145     return;
149 sub get_time {
150     my ($seconds, $minutes, $hours, $monthday, $month,
151             $year, $weekday, $yearday, $sommertime) = localtime(time);
152     $hours = $hours < 10 ? $hours = "0".$hours : $hours;
153     $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
154     $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
155     $month+=1;
156     $month = $month < 10 ? $month = "0".$month : $month;
157     $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
158     $year+=1900;
159     return "$year$month$monthday$hours$minutes$seconds";
164 #===  FUNCTION  ================================================================
165 #         NAME: build_msg
166 #  DESCRIPTION: Send a message to a destination
167 #   PARAMETERS: [header] Name of the header
168 #               [from]   sender ip
169 #               [to]     recipient ip
170 #               [data]   Hash containing additional attributes for the xml
171 #                        package
172 #      RETURNS:  nothing
173 #===============================================================================
174 sub build_msg ($$$$) {
175         my ($header, $from, $to, $data) = @_;
177         my $out_hash = &create_xml_hash($header, $from, $to);
179         while ( my ($key, $value) = each(%$data) ) {
180                 if(ref($value) eq 'ARRAY'){
181                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
182                 } else {
183                         &add_content2xml_hash($out_hash, $key, $value);
184                 }
185         }
186     my $out_msg = &create_xml_string($out_hash);
187     return $out_msg;
191 sub db_res2xml {
192     my ($db_res) = @_ ;
193     my $xml = "";
195     my $len_db_res= keys %{$db_res};
196     for( my $i= 1; $i<= $len_db_res; $i++ ) {
197         $xml .= "\n<answer$i>";
198         my $hash= $db_res->{$i};
199         while ( my ($column_name, $column_value) = each %{$hash} ) {
200             $xml .= "<$column_name>";
201             my $xml_content;
202             if( $column_name eq "xmlmessage" ) {
203                 $xml_content = &encode_base64($column_value);
204             } else {
205                 $xml_content = $column_value;
206             }
207             $xml .= $xml_content;
208             $xml .= "</$column_name>"; 
209         }
210         $xml .= "</answer$i>";
212     }
214     return $xml;
218 sub db_res2si_msg {
219     my ($db_res, $header, $target, $source) = @_;
221     my $si_msg = "<xml>";
222     $si_msg .= "<header>$header</header>";
223     $si_msg .= "<source>$source</source>";
224     $si_msg .= "<target>$target</target>";
225     $si_msg .= &db_res2xml;
226     $si_msg .= "</xml>";
230 sub get_where_statement {
231     my ($msg, $msg_hash) = @_;
232     my $error= 0;
233     
234     my $clause_str= "";
235     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { 
236         $error++; 
237     }
239     if( $error == 0 ) {
240         my @clause_l;
241         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
242         foreach my $clause (@where) {
243             my $connector = $clause->{'connector'}[0];
244             if( not defined $connector ) { $connector = "AND"; }
245             $connector = uc($connector);
246             delete($clause->{'connector'});
248             my @phrase_l ;
249             foreach my $phrase (@{$clause->{'phrase'}}) {
250                 my $operator = "=";
251                 if( exists $phrase->{'operator'} ) {
252                     my $op = $op_hash->{$phrase->{'operator'}[0]};
253                     if( not defined $op ) {
254                         &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
255                                 "statement to sql valid syntax. Please use 'eq', ".
256                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
257                         &main::daemon_log($msg, 8);
258                         $op = "=";
259                     }
260                     $operator = $op;
261                     delete($phrase->{'operator'});
262                 }
264                 my @xml_tags = keys %{$phrase};
265                 my $tag = $xml_tags[0];
266                 my $val = $phrase->{$tag}[0];
267                 push(@phrase_l, "$tag$operator'$val'");
268             }
269             my $clause_str .= join(" $connector ", @phrase_l);
270             push(@clause_l, "($clause_str)");
271         }
273         if( not 0 == @clause_l ) {
274             $clause_str = join(" AND ", @clause_l);
275             $clause_str = "WHERE ($clause_str) ";
276         }
277     }
279     return $clause_str;
282 sub get_select_statement {
283     my ($msg, $msg_hash)= @_;
284     my $select = "*";
285     if( exists $msg_hash->{'select'} ) {
286         my $select_l = \@{$msg_hash->{'select'}};
287         $select = join(', ', @{$select_l});
288     }
289     return $select;
293 sub get_update_statement {
294     my ($msg, $msg_hash) = @_;
295     my $error= 0;
296     my $update_str= "";
297     my @update_l; 
299     if( not exists $msg_hash->{'update'} ) { $error++; };
301     if( $error == 0 ) {
302         my $update= @{$msg_hash->{'update'}}[0];
303         while( my ($tag, $val) = each %{$update} ) {
304             my $val= @{$update->{$tag}}[0];
305             push(@update_l, "$tag='$val'");
306         }
307         if( 0 == @update_l ) { $error++; };   
308     }
310     if( $error == 0 ) { 
311         $update_str= join(', ', @update_l);
312         $update_str= "SET $update_str ";
313     }
315     return $update_str;
318 sub get_limit_statement {
319     my ($msg, $msg_hash)= @_; 
320     my $error= 0;
321     my $limit_str = "";
322     my ($from, $to);
324     if( not exists $msg_hash->{'limit'} ) { $error++; };
326     if( $error == 0 ) {
327         eval {
328             my $limit= @{$msg_hash->{'limit'}}[0];
329             $from= @{$limit->{'from'}}[0];
330             $to= @{$limit->{'to'}}[0];
331         };
332         if( $@ ) {
333             $error++;
334         }
335     }
337     if( $error == 0 ) {
338         $limit_str= "LIMIT $from, $to";
339     }   
340     
341     return $limit_str;
344 sub get_orderby_statement {
345     my ($msg, $msg_hash)= @_;
346     my $error= 0;
347     my $order_str= "";
348     my $order;
349     
350     if( not exists $msg_hash->{'orderby'} ) { $error++; };
352     if( $error == 0) {
353         eval {
354             $order= @{$msg_hash->{'orderby'}}[0];
355         };
356         if( $@ ) {
357             $error++;
358         }
359     }
361     if( $error == 0 ) {
362         $order_str= "ORDER BY $order";   
363     }
364     
365     return $order_str;
368 sub get_dns_domains() {
369         my $line;
370         my @searches;
371         open(RESOLV, "</etc/resolv.conf") or return @searches;
372         while(<RESOLV>){
373                 $line= $_;
374                 chomp $line;
375                 $line =~ s/^\s+//;
376                 $line =~ s/\s+$//;
377                 $line =~ s/\s+/ /;
378                 if ($line =~ /^domain (.*)$/ ){
379                         push(@searches, $1);
380                 } elsif ($line =~ /^search (.*)$/ ){
381                         push(@searches, split(/ /, $1));
382                 }
383         }
384         close(RESOLV);
386         my %tmp = map { $_ => 1 } @searches;
387         @searches = sort keys %tmp;
389         return @searches;
393 sub get_logged_in_users {
394     my $result = qx(/usr/bin/w -hs);
395     my @res_lines;
397     if( defined $result ) { 
398         chomp($result);
399         @res_lines = split("\n", $result);
400     }
402     my @logged_in_user_list;
403     foreach my $line (@res_lines) {
404         chomp($line);
405         my @line_parts = split(/\s+/, $line); 
406         push(@logged_in_user_list, $line_parts[0]);
407     }
409     return @logged_in_user_list;
413 1;