Code

Fixed bug.
[gosa.git] / gosa-si / modules / GosaPackages.pm
index 88b5b65e07710b9f7e789e0cfcddf880dd37f1b3..6bf2e3658bcb077a4ca11e8fde1765462237e7cb 100644 (file)
@@ -13,14 +13,6 @@ use Data::Dumper;
 use GOSA::DBsqlite;
 use MIME::Base64;
 
-my $op_hash = {
-    'eq' => '=',
-    'ne' => '!=',
-    'ge' => '>=',
-    'gt' => '>',
-    'le' => '<=',
-    'lt' => '<',
-};
 
 BEGIN{}
 END{}
@@ -72,7 +64,7 @@ my $bus_address = "$bus_ip:$bus_port";
 my $gosa_address = "$gosa_ip:$gosa_port";
 
 # create general settings for this module
-my $gosa_cipher = &create_ciphering($gosa_passwd);
+#y $gosa_cipher = &create_ciphering($gosa_passwd);
 my $xml = new XML::Simple();
 
 
@@ -235,31 +227,6 @@ sub get_ip {
         return $result;
 }
 
-#===  FUNCTION  ================================================================
-#         NAME:  open_socket
-#   PARAMETERS:  PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
-#                [PeerPort] string necessary if port not appended by PeerAddr
-#      RETURNS:  socket IO::Socket::INET
-#  DESCRIPTION:  open a socket to PeerAddr
-#===============================================================================
-sub open_socket {
-    my ($PeerAddr, $PeerPort) = @_ ;
-    if(defined($PeerPort)){
-        $PeerAddr = $PeerAddr.":".$PeerPort;
-    }
-    my $socket;
-    $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
-            Porto => "tcp" ,
-            Type => SOCK_STREAM,
-            Timeout => 5,
-            );
-    if(not defined $socket) {
-        return;
-    }
-    &main::daemon_log("open_socket to: $PeerAddr", 7);
-    return $socket;
-}
-
 
 #===  FUNCTION  ================================================================
 #         NAME:  process_incoming_msg
@@ -268,86 +235,54 @@ sub open_socket {
 #  DESCRIPTION:  handels the proceeded distribution to the appropriated functions
 #===============================================================================
 sub process_incoming_msg {
-    my ($crypted_msg) = @_ ;
-       &main::daemon_log("Got message $crypted_msg", 8);
-       if( (not(defined($crypted_msg))) || (length($crypted_msg) <= 0)) {
-        &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
-        return;
-    }
-
-    $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
-    $crypted_msg = $1;
-       my $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
-    # collect addresses from possible incoming clients
-    # only gosa is allowd as incoming client
-    &main::daemon_log("GosaPackages: host_key: $host", 7);
-    &main::daemon_log("GosaPackages: key_passwd: $gosa_passwd", 7);
-
-    $gosa_cipher = &create_ciphering($gosa_passwd);
-
-    # determine the correct passwd for deciphering of the incoming msgs
-    my $msg = "";
-    my $msg_hash;
-    eval{
-        $msg = &decrypt_msg($crypted_msg, $gosa_cipher);
-        &main::daemon_log("GosaPackages: decrypted_msg: \n$msg", 8);
-
-        $msg_hash = $xml->XMLin($msg, ForceArray=>1);
-    };
-    if($@) {
-        &main::daemon_log("WARNING: GosaPackages do not understand the message:", 5);
-        &main::daemon_log("$@", 7);
-        return;
-    }
-
+    my ($msg, $msg_hash) = @_ ;
     my $header = @{$msg_hash->{header}}[0];
+    my $out_msg;
     
-    &main::daemon_log("GosaPackages: receive '$header' from $host", 1);
+    &main::daemon_log("GosaPackages: receive '$header'", 1);
     
-    my $out_msg;
     if ($header =~ /^job_/) {
         $out_msg = &process_job_msg($msg, $msg_hash);
-    } elsif ($header =~ /^gosa_/) {
+    } 
+    elsif ($header =~ /^gosa_/) {
         $out_msg = &process_gosa_msg($msg, $header);
-    } else {
+    } 
+    else {
         &main::daemon_log("ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix");
     }
-        
-    if (not defined $out_msg) {
-        return;
-    }
 
+    # keep job queue uptodate and save result and status
     if ($out_msg =~ /<jobdb_id>(\d*?)<\/jobdb_id>/) {
         my $job_id = $1;
         my $sql = "UPDATE '".$main::job_queue_table_name.
             "' SET status='done', result='".$out_msg.
             "' WHERE id='$job_id'";
         my $res = $main::job_db->exec_statement($sql);
-        return;
-
-    } else {
-
-        my $out_cipher = &create_ciphering($gosa_passwd);
-        $out_msg = &encrypt_msg($out_msg, $out_cipher);
-
-        return $out_msg;
-       }
+    } 
 
+    my @out_msg_l;
+    push(@out_msg_l, $out_msg);
+    return \@out_msg_l;
 }
 
+
 sub process_gosa_msg {
     my ($msg, $header) = @_ ;
     my $out_msg;
     $header =~ s/gosa_//;
 
     # decide wether msg is a core function or a event handler
-    if ( $header eq 'query_jobdb') { $out_msg = &query_jobdb }
-    elsif ($header eq 'delete_jobdb_entry') { $out_msg = &delete_jobdb_entry }
-    elsif ($header eq 'clear_jobdb') { $out_msg = &clear_jobdb }
-    elsif ($header eq 'update_status_jobdb_entry' ) { $out_msg = &update_status_jobdb_entry }
-    elsif ($header eq 'update_timestamp_jobdb_entry' ) { $out_msg = &update_timestamp_jobdb_entry }
-    else {
+    if ( $header eq 'query_jobdb') {
+       $out_msg = &query_jobdb
+    } elsif ($header eq 'delete_jobdb_entry') {
+        $out_msg = &delete_jobdb_entry
+    } elsif ($header eq 'clear_jobdb') {
+       $out_msg = &clear_jobdb
+    } elsif ($header eq 'update_status_jobdb_entry' ) {
+       $out_msg = &update_status_jobdb_entry
+    } elsif ($header eq 'count_jobdb' ) {
+        $out_msg = &count_jobdb
+    } else {
         # msg could not be assigned to core function
         # fetch all available eventhandler under $server_event_dir
         opendir (DIR, $server_event_dir) or &main::daemon_log("ERROR cannot open $server_event_dir: $!\n", 1) and return;
@@ -358,7 +293,7 @@ sub process_gosa_msg {
             # try to deliver incoming msg to eventhandler
             my $cmd = File::Spec->join($server_event_dir, $header)." '$msg'";
             &main::daemon_log("GosaPackages: execute event_handler $header", 3);
-            &main::daemon_log("GosaPackages: cmd: $cmd", 7);
+            &main::daemon_log("GosaPackages: cmd: $cmd", 8);
 
             $out_msg = "";
             open(PIPE, "$cmd 2>&1 |");
@@ -418,9 +353,11 @@ sub db_res_2_xml {
 
     my $xml = "<xml>";
 
-    while ( my ($hit, $hash) = each %{ $db_res } ) {
-        $xml .= "\n<answer$hit>";
+    my $len_db_res= keys %{$db_res};
 
+    for( my $i= 1; $i<= $len_db_res; $i++ ) {
+        $xml .= "\n<answer$i>";
+        my $hash= $db_res->{$i};
         while ( my ($column_name, $column_value) = each %{$hash} ) {
             $xml .= "<$column_name>";
             my $xml_content;
@@ -432,8 +369,8 @@ sub db_res_2_xml {
             $xml .= $xml_content;
             $xml .= "</$column_name>"; 
         }
+        $xml .= "</answer$i>";
 
-        $xml .= "</answer$hit>";
     }
 
     $xml .= "</xml>";
@@ -443,91 +380,6 @@ sub db_res_2_xml {
 
 ## CORE FUNCTIONS ############################################################
 
-sub get_where_statement {
-    my ($msg, $msg_hash)= @_;
-    my $error= 0;
-    
-    my $clause_str= "";
-    if( not exists @{$msg_hash->{'where'}}[0]->{'clause'} ) { $error++; };
-    if( $error == 0 ) {
-        my @clause_l;
-        my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
-        foreach my $clause (@where) {
-            my $connector = $clause->{'connector'}[0];
-            if( not defined $connector ) { $connector = "AND"; }
-            $connector = uc($connector);
-            delete($clause->{'connector'});
-
-            my @phrase_l ;
-            foreach my $phrase (@{$clause->{'phrase'}}) {
-                my $operator = "=";
-                if( exists $phrase->{'operator'} ) {
-                    my $op = $op_hash->{$phrase->{'operator'}[0]};
-                    if( not defined $op ) {
-                        &main::daemon_log("Can not translate operator '$operator' in where ".
-                                "statement to sql valid syntax. Please use 'eq', ".
-                                "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
-                        &main::daemon_log($msg, 8);
-                        $op = "=";
-                    }
-                    $operator = $op;
-                    delete($phrase->{'operator'});
-                }
-
-                my @xml_tags = keys %{$phrase};
-                my $tag = $xml_tags[0];
-                my $val = $phrase->{$tag}[0];
-                push(@phrase_l, "$tag$operator'$val'");
-            }
-            my $clause_str .= join(" $connector ", @phrase_l);
-            push(@clause_l, $clause_str);
-        }
-
-        if( not 0 == @clause_l ) {
-            $clause_str = join(" AND ", @clause_l);
-            $clause_str = "WHERE $clause_str ";
-        }
-    }
-
-    return $clause_str;
-}
-
-sub get_select_statement {
-    my ($msg, $msg_hash)= @_;
-    my $select = "*";
-    if( exists $msg_hash->{'select'} ) {
-        my $select_l = \@{$msg_hash->{'select'}};
-        $select = join(' AND ', @{$select_l});
-    }
-    return $select;
-}
-
-
-sub get_update_statement {
-    my ($msg, $msg_hash) = @_;
-    my $error= 0;
-    my $update_str= "";
-    my @update_l; 
-
-    if( not exists $msg_hash->{'update'} ) { $error++; };
-
-    if( $error == 0 ) {
-        my $update= @{$msg_hash->{'update'}}[0];
-        while( my ($tag, $val) = each %{$update} ) {
-            my $val= @{$update->{$tag}}[0];
-            push(@update_l, "$tag='$val'");
-        }
-        if( 0 == @update_l ) { $error++; };   
-    }
-
-    if( $error == 0 ) { 
-        $update_str= join(', ', @update_l);
-        $update_str= "SET $update_str ";
-    }
-
-    return $update_str;
-}
-
 sub query_jobdb {
     my ($msg) = @_;
     my $msg_hash = &transform_msg2hash($msg);
@@ -536,15 +388,36 @@ sub query_jobdb {
     my $select= &get_select_statement($msg, $msg_hash);
     my $table= $main::job_queue_table_name;
     my $where= &get_where_statement($msg, $msg_hash);
-    my $sql_statement= "SELECT $select FROM $table $where";
+    my $limit= &get_limit_statement($msg, $msg_hash);
+    my $orderby= &get_orderby_statement($msg, $msg_hash);
+    my $sql_statement= "SELECT $select FROM $table $where $orderby $limit";
 
     # execute db query   
     my $res_hash = $main::job_db->select_dbentry($sql_statement);
     my $out_xml = &db_res_2_xml($res_hash);
+
+    return $out_xml;
+}
+
+
+sub count_jobdb {
+    my ($msg)= @_;
+    my $out_xml= "<xml><count>error</count></xml>";
+
+    # prepare query sql statement
+    my $table= $main::job_queue_table_name;
+    my $sql_statement= "SELECT * FROM $table ";
     
+    # execute db query
+    my $res_hash = $main::job_db->select_dbentry($sql_statement);
+
+    my $count = keys(%{$res_hash});
+    $out_xml= "<xml><count>$count</count></xml>";
+
     return $out_xml;
 }
 
+
 sub delete_jobdb_entry {
     my ($msg) = @_ ;
     my $msg_hash = &transform_msg2hash($msg);