Imported Upstream version 0.4.8
[platform/upstream/libsmi.git] / tools / mib2svg.cgi.in
1 #!/usr/bin/perl -w -T
2 use strict;
3 use CGI qw(:standard);
4 #set this for testing purposes only
5 #use CGI::Carp qw(fatalsToBrowser);
6 use File::Temp;
7
8 #some security-settings
9 $CGI::POST_MAX = 1024 * 100;
10 $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
11
12 #gather all mibs in smipath into @mibnames
13 my $smipath = "@smipath@";
14 my @smidirs = split(/:/,$smipath);
15 my @mibnames;
16 foreach my $dir (@smidirs) {
17     opendir(MIBDIR,$dir);
18     my @entries = grep !/^\.\.?\z/, readdir(MIBDIR);
19     closedir(MIBDIR);
20     @mibnames = (@mibnames, @entries);
21 }
22
23 if (param()) {
24     #params present.
25     my @options;
26     my $localfh;
27     my $localfn;
28     my @mibs = param('mibs');
29     my $width = param('width');
30     my $height = param('height');
31
32     #parse options and add safe strings to the options-array
33     if (param('deprobs')) {
34         if (param('deprobs') eq "deprecated") {
35             @options = (@options, "--svg-show-deprecated");
36         } elsif (param('deprobs') eq "obsolete") {
37             @options = (@options, "--svg-show-depr-obsolete");
38         }
39     }
40
41     if (param('static')) {
42         @options = (@options, "--svg-static-output");
43     }
44
45     if ($width =~ /(\d+)/) {
46         if ($1 <= 2147483647) {
47             @options = (@options, "--svg-width=$1");
48         }
49     }
50     if ($height =~ /(\d+)/) {
51         if ($1 <= 2147483647) {
52             @options = (@options, "--svg-height=$1");
53         }
54     }
55
56     #parse selected MIBs
57     foreach my $mibname (@mibnames) {
58         foreach my $mib (@mibs) {
59             if ($mibname eq $mib) {
60                 $mibname =~ /([\w\-]+)/;
61                 @options = (@options, "$1");
62             }
63         }
64     }
65
66     #handle file upload
67     if (param('uploadmib')) {
68         my $remotefh = upload('uploadmib');
69         ($localfh, $localfn)
70             = File::Temp->tempfile('tempMIBXXXX', DIR => '/tmp', UNLINK => 1)
71                 or die "Error opening outfile\n";
72         while (<$remotefh>) {
73             print $localfh $_;
74         }
75         close $remotefh;
76         close $localfh;
77         @options = (@options, $localfn);
78     }
79
80     #call smidump
81     my $res = open (SMIDUMP, "-|");
82     die "Couldn't open pipe to subprocess" unless defined($res);
83     exec "@prefix@/bin/smidump",'-u','-f','svg',@options
84         or die "Couldn't exec smidump" if $res == 0;
85     my @svg = <SMIDUMP>;
86     close (SMIDUMP);
87
88     #serve svg
89     my $svglength = @svg;
90     if ($svglength eq 0) {
91         print header;
92         print start_html("MIB to SVG");
93         print h2("Sorry, smidump output contained no data.");
94         print end_html;
95     } else {
96         #FIXME - or + ?
97         print header(-TYPE => "image/svg-xml");
98         print "@svg";
99     }
100
101 } else {
102     #no params present
103     #send form
104     print header;
105     print start_html("MIB to SVG");
106     print h2("Generate a SVG Diagram from MIB Modules");
107     print start_multipart_form();
108
109     print p("select one or more MIBs: ", scrolling_list(
110         -NAME => "mibs",
111         -VALUES => [@mibnames],
112         -SIZE => 10,
113         -MULTIPLE => 1,
114     ));
115
116     print p("or upload a MIB: ", filefield(
117         -NAME => "uploadmib"
118     ));
119
120     print p("diagram width: ", textfield(
121         -NAME => "width",
122         -DEFAULT => "1100"
123     ));
124     print p("diagram height: ", textfield(
125         -NAME => "height",
126         -DEFAULT => "700"
127     ));
128
129     print p(radio_group(
130         -NAME => "deprobs",
131         -VALUES => [ qw(none deprecated obsolete) ],
132         -LINEBREAK => 1,
133         -LABELS => {
134             none => "show only current objects",
135             deprecated => "show current and deprecated objects",
136             obsolete => "show all objects",
137         },
138     ));
139
140     print p(checkbox(
141         -NAME => "static",
142         -LABEL => "generate a smaller, non-interactive SVG diagram",
143     ));
144
145     print p(submit("generate SVG"), reset("reset form"));
146     print end_form;
147     print end_html;
148 }