File: mini-graph.cgi

package info (click to toggle)
cricket 1.0.5-19
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,936 kB
  • sloc: perl: 8,276; ansic: 329; sh: 162; makefile: 63; sql: 16
file content (161 lines) | stat: -rwxr-xr-x 4,169 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/local/bin/perl -w
# -*- perl -*-

# Cricket: a configuration, polling and data display wrapper for RRD files
#
#    Copyright (C) 1998 Jeff R. Allen and WebTV Networks, Inc.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

BEGIN {
    # This magic attempts to guess the install directory based
    # on how the script was called. If it fails for you, just
    # hardcode it.
    require '/etc/cricket/cricket-conf.pl';

    # Necessary for set[ug]id operation
    $ENV{PATH} = '/bin:/usr/bin';
}

use lib "$Common::global::gInstallRoot/lib";

use CGI qw(fatalsToBrowser);
use Digest::MD5;

use Common::global;
use Common::Log;
use Common::Util;
Common::Log::setLevel('warn');

# Set a safe path. Necessary for set[ug]id operation.
$ENV{PATH} = "/bin:/usr/bin";

# cache cleaning params
#
$gPollingInterval = 5 * 60;     # defaults to 5 minutes

$Common::global::gUrlStyle ||= "classic";
my $gUsePathInfo = 0;
if ($Common::global::gUrlStyle eq "pathinfo") {
    $gUsePathInfo = 1;
}

$main::gQ = new CGI;

doGraph();

sub doGraph {
    my($imageName) = generateImageName($main::gQ);

    # check the image's existance (i.e. no error from stat()) and age

    my($mtime);
    if (defined($imageName)) {
        $mtime = (stat($imageName))[9];
    }

    if (!defined($mtime) || ((time() - $mtime) > $main::gPollingInterval)) {
        # this request is actually going to need work... pass it on
        if (Common::Util::isWin32()) {
            exec("perl $Common::global::gInstallRoot/grapher.cgi");
        } else {
            exec("$Common::global::gInstallRoot/grapher.cgi");
        }
    } else {
        Debug("Cached image exists: $imageName. Using that.");
        sprayPng($imageName);
    }
}

sub tryPng {
    my($png) = @_;

    # we need to make certain there are no buffering problems here.
    local($|) = 1;

    if (! open(PNG, "<$png")) {
        return;
    } else {
        my($stuff, $len);
        binmode(PNG);
        while ($len = read(PNG, $stuff, 8192)) {
            print $stuff;
        }
        close(PNG);
    }
    return 1;
}

sub sprayPng {
    my($png) = @_;

    print $main::gQ->header('image/png');

    if (! tryPng($png)) {
        Warn("Could not open $png: $!");
        if (! tryPng("/usr/share/cricket/images/failed.png")) {
            Warn("Could not send failure png: $!");
            return;
        }
    }

    return 1;
}

sub generateImageName {
    my($q) = @_;
    my($param, $md5);

    $md5 = new Digest::MD5;

    # make sure to munge $target correctly if $gUrlStyle = pathinfo
    $md5->add(urlTarget($q));

    foreach $param ($q->param()) {
        next if ($param eq 'rand');
        next if ($param eq 'target');
        if ($param eq 'cache') {
            if (lc($q->param($param)) eq 'no') {
                return;
            }
        }
        $md5->add($param, $q->param($param));
    }
    my($hash) = unpack("H8", $md5->digest());

    return "$Common::global::gCacheDir/cricket-$hash.png";
}

# Get or set the target from the $cgi object.
sub urlTarget {
    my $cgi = shift;
    my $target = shift;
    return $cgi->param('target', $target) if !$gUsePathInfo;
    if (!defined($target)) {
        $target = $cgi->path_info();
        $target =~ s/\/+$//;  # Zonk any trailing slashes
        $target ||= "/";      # but we name the root explicitly
        return $target;
    }
    $cgi->path_info($target);
}


# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# tab-width: 4
# perl-indent-level: 4
# End: