Code

Updated table summary
[gosa.git] / gosa-si / modules / GosaSupportDaemon.pm
index 1301f6605f51f48e7b584d3117d6e5a2d22ad9bb..7d769c08ad6ff4e302a900203e214f955cb2b16c 100644 (file)
@@ -5,6 +5,8 @@ use Exporter;
 my @functions = (
     "create_passwd",
     "create_xml_hash",
+       "createXmlHash",
+       "myXmlHashToString",
     "get_content_from_xml_hash",
     "add_content2xml_hash",
     "create_xml_string",
@@ -44,12 +46,12 @@ use IO::Socket::INET;
 use Crypt::Rijndael;
 use Digest::MD5  qw(md5 md5_hex md5_base64);
 use MIME::Base64;
+use XML::Quote qw(:all);
 use XML::Simple;
 use Data::Dumper;
 use Net::DNS;
 use DateTime;
 
-
 my $op_hash = {
     'eq' => '=',
     'ne' => '!=',
@@ -67,7 +69,7 @@ END {}
 
 ### Start ######################################################################
 
-my $xml = new XML::Simple();
+our $xml = new XML::Simple();
 
 sub daemon_log {
     my ($msg, $level) = @_ ;
@@ -113,6 +115,91 @@ sub create_xml_hash {
     return $hash
 }
 
+sub createXmlHash {
+       my ($header, $source, $target) = @_;
+       return { header=>$header, source=>$source, target=>$target};
+}
+
+sub _transformHashToString {
+       my ($hash) = @_;
+       my $s = "";
+
+       while (my ($tag, $content) = each(%$hash)) {
+
+               if (ref $content eq "HASH") {
+                       $s .= "<$tag>".&_transformHashToString($content)."</$tag>";
+               } elsif ( ref $content eq "ARRAY") {
+                       $s .= &_transformArrayToString($tag, $content);
+               } else {
+                       $content = defined $content ? $content : "";
+                       $s .= "<$tag>".&xml_quote($content)."</$tag>";
+               }
+       }
+       return $s;
+}
+
+sub _transformArrayToString {
+       my ($tag, $contentArray) = @_;
+       my $s = "";
+       foreach my $content (@$contentArray) {
+               if (ref $content eq "HASH") {
+                       $s .= "<$tag>".&_transformHashToString($content)."</$tag>";
+               } else {
+                       $content = defined $content ? $content : "";
+                       $s .= "<$tag>".&xml_quote($content)."</$tag>";
+               }
+       }
+       return $s;
+}
+
+
+#===  FUNCTION  ================================================================
+#         NAME:  myXmlHashToString
+#   PARAMETERS:  xml_hash - hash - hash from function createXmlHash
+#      RETURNS:  xml_string - string - xml string representation of the hash
+#  DESCRIPTION:  Transforms the given hash to a xml wellformed string. I.e.:
+#                   {
+#                   'header' => 'a'
+#                   'source' => 'c',
+#                   'target' => 'b',
+#                   'hit' => [ '1',
+#                              '2',
+#                              { 
+#                                'hit31' => 'ABC',
+#                               'hit32' => 'XYZ'
+#                              }
+#                            ],
+#                   'res0' => {
+#                      'res1' => {
+#                         'res2' => 'result'
+#                      }
+#                   },
+#               };
+#           
+#                               will be transformed to 
+#                               <xml>
+#                                      <header>a</header>
+#                                      <source>c</source>
+#                                      <target>b</target>
+#                                      <hit>1</hit>
+#                                      <hit>2</hit>
+#                                      <hit>
+#                                              <hit31>ABC</hit31>
+#                                              <hit32>XYZ</hit32>
+#                                      </hit>
+#                                      <res0>
+#                                              <res1>
+#                                                      <res2>result</res2>
+#                                              </res1>
+#                                      </res0>
+#                              </xml>
+#
+#===============================================================================
+sub myXmlHashToString {
+       my ($hash) = @_;
+       return "<xml>".&_transformHashToString($hash)."</xml>";
+}
+
 
 #===  FUNCTION  ================================================================
 #         NAME:  create_xml_string
@@ -600,6 +687,15 @@ sub get_ip {
 sub get_interface_for_ip {
        my $result;
        my $ip= shift;
+
+       if($ip =~ /^[a-z]/i) {
+               my $ip_address = inet_ntoa(scalar gethostbyname($ip));
+               if(defined($ip_address) && $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
+                       # Write ip address to $source variable
+                       $ip = $ip_address;
+               }
+       }
+
        if ($ip && length($ip) > 0) {
                my @ifs= &get_interfaces();
                if($ip eq "0.0.0.0") {
@@ -848,13 +944,19 @@ sub check_opsi_res {
         if ($res->is_error) {
             my $error_string;
             if (ref $res->error_message eq "HASH") { 
+                               # for different versions
                 $error_string = $res->error_message->{'message'}; 
+                               $_ = $res->error_message->{'message'};
             } else { 
+                               # for different versions
                 $error_string = $res->error_message; 
+                               $_ = $res->error_message;
             }
             return 1, $error_string;
         }
     } else {
+               # for different versions
+               $_ = $main::opsi_client->status_line;
         return 1, $main::opsi_client->status_line;
     }
     return 0;