Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
1533 richard 1
<?php
2
    //
3
    // vnStat PHP frontend (c)2006-2010 Bjorge Dijkstra (bjd@jooz.net)
4
    //
5
    // This program is free software; you can redistribute it and/or modify
6
    // it under the terms of the GNU General Public License as published by
7
    // the Free Software Foundation; either version 2 of the License, or
8
    // (at your option) any later version.
9
    //
10
    // This program is distributed in the hope that it will be useful,
11
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    // GNU General Public License for more details.
14
    //
15
    // You should have received a copy of the GNU General Public License
16
    // along with this program; if not, write to the Free Software
17
    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
    //
19
    //
1675 richard 20
    // see file COPYING or at http://www.gnu.org/licenses/gpl.html
1533 richard 21
    // for more information.
22
    //
23
    require 'config.php';
24
    require 'localize.php';
25
    require 'vnstat.php';
26
 
27
    validate_input();
28
 
29
    require "./themes/$style/theme.php";
30
 
31
    function allocate_color($im, $colors)
32
    {
33
	return imagecolorallocatealpha($im, $colors[0], $colors[1], $colors[2], $colors[3]);
34
    }
1675 richard 35
 
1533 richard 36
    function init_image()
37
    {
38
        global $im, $xlm, $xrm, $ytm, $ybm, $iw, $ih,$graph, $cl, $iface, $colorscheme, $style;
39
 
40
        if ($graph == 'none')
41
            return;
42
 
43
        //
44
        // image object
1675 richard 45
        //
1533 richard 46
        $xlm = 70;
47
        $xrm = 20;
48
        $ytm = 35;
49
        $ybm = 60;
50
        if ($graph == 'small')
51
        {
52
            $iw = 300 + $xrm + $xlm;
1675 richard 53
            $ih = 100 + $ytm + $ybm;
1533 richard 54
        }
55
        else
56
        {
57
            $iw = 600 + $xrm + $xlm;
58
            $ih = 200 + $ytm + $ybm;
59
        }
60
 
61
        $im = imagecreatetruecolor($iw,$ih);
62
 
63
        //
64
        // colors
65
        //
66
	$cs = $colorscheme;
67
	$cl['image_background'] = allocate_color($im, $cs['image_background']);
68
	$cl['background'] = allocate_color($im, $cs['graph_background']);
69
	$cl['background_2'] = allocate_color($im, $cs['graph_background_2']);
70
        $cl['grid_stipple_1'] = allocate_color($im, $cs['grid_stipple_1']);
71
        $cl['grid_stipple_2'] = allocate_color($im, $cs['grid_stipple_2']);
72
        $cl['text'] = allocate_color($im, $cs['text']);
73
        $cl['border'] = allocate_color($im, $cs['border']);
74
        $cl['rx'] = allocate_color($im, $cs['rx']);
75
        $cl['rx_border'] = allocate_color($im, $cs['rx_border']);
76
        $cl['tx'] = allocate_color($im, $cs['tx']);
77
        $cl['tx_border'] = allocate_color($im, $cs['tx_border']);
1675 richard 78
 
1533 richard 79
        imagefilledrectangle($im,0,0,$iw,$ih,$cl['image_background']);
80
	imagefilledrectangle($im,$xlm,$ytm,$iw-$xrm,$ih-$ybm, $cl['background']);
1675 richard 81
 
1533 richard 82
	$x_step = ($iw - $xlm - $xrm) / 12;
83
	$depth = ($x_step / 8) + 4;
84
	imagefilledpolygon($im, array($xlm, $ytm, $xlm, $ih - $ybm, $xlm - $depth, $ih - $ybm + $depth, $xlm - $depth, $ytm + $depth), 4, $cl['background_2']);
85
	imagefilledpolygon($im, array($xlm, $ih - $ybm, $xlm - $depth, $ih - $ybm + $depth, $iw - $xrm - $depth, $ih - $ybm  + $depth, $iw - $xrm, $ih - $ybm), 4, $cl['background_2']);
86
 
87
	// draw title
88
	$text = T('Traffic data for')." $iface";
89
 	$bbox = imagettfbbox(10, 0, GRAPH_FONT, $text);
90
	$textwidth = $bbox[2] - $bbox[0];
91
	imagettftext($im, 10, 0, ($iw-$textwidth)/2, ($ytm/2), $cl['text'], GRAPH_FONT, $text);
1675 richard 92
 
1533 richard 93
    }
94
 
95
    function draw_border()
96
    {
97
        global $im,$cl,$iw,$ih;
98
 
99
        imageline($im,     0,    0,$iw-1,    0, $cl['border']);
100
        imageline($im,     0,$ih-1,$iw-1,$ih-1, $cl['border']);
1675 richard 101
        imageline($im,     0,    0,    0,$ih-1, $cl['border']);
1533 richard 102
        imageline($im, $iw-1,    0,$iw-1,$ih-1, $cl['border']);
103
    }
1675 richard 104
 
1533 richard 105
    function draw_grid($x_ticks, $y_ticks)
106
    {
107
        global $im, $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm;
1675 richard 108
        $x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
1533 richard 109
        $y_step = ($ih - $ytm - $ybm) / $y_ticks;
1675 richard 110
 
1533 richard 111
	$depth = 10;//($x_step / 8) + 4;
112
 
113
        $ls = array($cl['grid_stipple_1'],$cl['grid_stipple_2']);
114
        imagesetstyle($im, $ls);
115
        for ($i=$xlm;$i<=($iw-$xrm); $i += $x_step)
116
        {
117
            imageline($im, $i, $ytm, $i, $ih - $ybm, IMG_COLOR_STYLED);
118
	    imageline($im, $i, $ih - $ybm, $i - $depth, $ih - $ybm + $depth, IMG_COLOR_STYLED);
119
        }
120
        for ($i=$ytm;$i<=($ih-$ybm); $i += $y_step)
121
        {
1675 richard 122
            imageline($im, $xlm, $i, $iw - $xrm, $i, IMG_COLOR_STYLED);
1533 richard 123
	    imageline($im, $xlm, $i, $xlm - $depth, $i + $depth, IMG_COLOR_STYLED);
124
        }
125
        imageline($im, $xlm, $ytm, $xlm, $ih - $ybm, $cl['border']);
126
        imageline($im, $xlm, $ih - $ybm, $iw - $xrm, $ih - $ybm, $cl['border']);
127
    }
1675 richard 128
 
1533 richard 129
    function draw_data($data)
130
    {
131
        global $im,$cl,$iw,$ih,$xlm,$xrm,$ytm,$ybm;
132
 
133
        sort($data);
134
 
135
        $x_ticks = count($data);
136
        $y_ticks = 10;
137
        $y_scale = 1;
138
        $prescale = 1;
139
        $unit = 'K';
140
        $offset = 0;
141
        $gr_h = $ih - $ytm - $ybm;
1675 richard 142
        $x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
1533 richard 143
        $y_step = ($ih - $ytm - $ybm) / $y_ticks;
144
        $bar_w = ($x_step / 2) ;
145
 
146
        //
147
        // determine scale
148
        //
149
        $low = 99999999999;
150
        $high = 0;
151
        for ($i=0; $i<$x_ticks; $i++)
152
        {
153
            if ($data[$i]['rx'] < $low)
154
            $low = $data[$i]['rx'];
155
            if ($data[$i]['tx'] < $low)
156
            $low = $data[$i]['tx'];
157
            if ($data[$i]['rx'] > $high)
158
            $high = $data[$i]['rx'];
159
            if ($data[$i]['tx'] > $high)
160
            $high = $data[$i]['tx'];
161
        }
162
 
163
        while ($high > ($prescale * $y_scale * $y_ticks))
164
        {
165
            $y_scale = $y_scale * 2;
166
            if ($y_scale >= 1024)
167
            {
168
            $prescale = $prescale * 1024;
169
            $y_scale = $y_scale / 1024;
1675 richard 170
            if ($unit == 'K')
1533 richard 171
                $unit = 'M';
172
            else if ($unit == 'M')
173
                $unit = 'G';
174
            else if ($unit == 'G')
175
                $unit = 'T';
176
            }
177
        }
178
 
179
        draw_grid($x_ticks, $y_ticks);
1675 richard 180
 
1533 richard 181
        //
182
        // graph scale factor (per pixel)
183
        //
184
	imagesetthickness($im, 1);
185
        $sf = ($prescale * $y_scale * $y_ticks) / $gr_h;
186
 
1675 richard 187
        if (count($data) == 0)
1533 richard 188
        {
1675 richard 189
            $text = T('no data available');
1533 richard 190
	    $bbox = imagettfbbox(10, 0, GRAPH_FONT, $text);
191
	    $textwidth = $bbox[2] - $bbox[0];
192
	    imagettftext($im, 10, 0, ($iw-$textwidth)/2, $ytm + 80, $cl['text'], GRAPH_FONT, $text);
193
        }
194
        else
195
        {
196
            //
197
            // draw bars
1675 richard 198
            //
1533 richard 199
            for ($i=0; $i<$x_ticks; $i++)
200
            {
201
        	$x = $xlm + ($i * $x_step);
202
        	$y = $ytm + ($ih - $ytm - $ybm) - (($data[$i]['rx'] - $offset) / $sf);
1675 richard 203
 
1533 richard 204
		$depth = $x_step / 8;
205
		$space = 0;
1675 richard 206
 
1533 richard 207
		$x1 = $x;
208
		$y1 = $y;
209
		$x2 = $x + $bar_w - $space;
210
		$y2 = $ih - $ybm;
1675 richard 211
 
1533 richard 212
        	imagefilledrectangle($im, $x1, $y1, $x2, $y2, $cl['rx']);
213
		imagerectangle($im, $x1, $y1, $x2, $y2, $cl['rx_border']);
1675 richard 214
 
1533 richard 215
		imagefilledrectangle($im, $x1 - $depth, $y1 + $depth, $x2 -$depth, $y2 + $depth, $cl['rx']);
216
		imagerectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['rx_border']);
1675 richard 217
 
1533 richard 218
		imagefilledpolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['rx']);
219
		imagepolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['rx_border']);
220
		imagefilledpolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['rx']);
221
		imagepolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['rx_border']);
222
 
223
        	$y1 = $ytm + ($ih - $ytm - $ybm) - (($data[$i]['tx'] - $offset) / $sf);
224
		$x1 = $x1 + $bar_w;
225
		$x2 = $x2 + $bar_w;
226
 
227
        	imagefilledrectangle($im, $x1, $y1, $x2, $y2, $cl['tx']);
228
		imagerectangle($im, $x1, $y1, $x2, $y2, $cl['tx_border']);
1675 richard 229
 
1533 richard 230
        	imagefilledrectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['tx']);
1675 richard 231
		imagerectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['tx_border']);
232
 
1533 richard 233
		imagefilledpolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['tx']);
234
		imagepolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['tx_border']);
235
		imagefilledpolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['tx']);
236
		imagepolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['tx_border']);
237
            }
1675 richard 238
 
1533 richard 239
            //
240
            // axis labels
241
            //
242
            for ($i=0; $i<=$y_ticks; $i++)
243
            {
244
                $label = ($i * $y_scale).$unit;
245
		$bbox = imagettfbbox(8, 0, GRAPH_FONT, $label);
246
		$textwidth = $bbox[2] - $bbox[0];
247
		imagettftext($im, 8, 0, $xlm - $textwidth - 16, ($ih - $ybm) - ($i * $y_step) + 8 + $depth, $cl['text'], GRAPH_FONT, $label);
248
            }
249
 
250
            for ($i=0; $i<$x_ticks; $i++)
251
            {
252
                $label = $data[$i]['img_label'];
253
		$bbox = imagettfbbox(9, 0, GRAPH_FONT, $label);
254
		$textwidth = $bbox[2] - $bbox[0];
255
		imagettftext($im, 9, 0, $xlm + ($i * $x_step) + ($x_step / 2) - ($textwidth / 2) - $depth - 4, $ih - $ybm + 20 + $depth, $cl['text'], GRAPH_FONT, $label);
256
            }
257
        }
258
 
259
        draw_border();
260
 
261
 
262
        //
263
        // legend
264
        //
265
        imagefilledrectangle($im, $xlm, $ih-$ybm+39, $xlm+8,$ih-$ybm+47,$cl['rx']);
266
        imagerectangle($im, $xlm, $ih-$ybm+39, $xlm+8,$ih-$ybm+47,$cl['text']);
1675 richard 267
	imagettftext($im, 8,0, $xlm+14, $ih-$ybm+48,$cl['text'], GRAPH_FONT,T('bytes in'));
1533 richard 268
 
269
        imagefilledrectangle($im, $xlm+120 , $ih-$ybm+39, $xlm+128,$ih-$ybm+47,$cl['tx']);
270
        imagerectangle($im, $xlm+120, $ih-$ybm+39, $xlm+128,$ih-$ybm+47,$cl['text']);
1675 richard 271
	imagettftext($im, 8,0, $xlm+134, $ih-$ybm+48,$cl['text'], GRAPH_FONT,T('bytes out'));
1533 richard 272
    }
273
 
274
    function output_image()
275
    {
276
        global $page,$hour,$day,$month,$im,$iface;
277
 
278
        if ($page == 'summary')
279
            return;
280
 
281
        init_image();
282
 
283
        if ($page == 'h')
284
        {
285
            draw_data($hour);
286
        }
287
        else if ($page == 'd')
288
        {
289
            draw_data($day);
290
        }
291
        else if ($page == 'm')
292
        {
293
            draw_data($month);
294
        }
1675 richard 295
 
296
        header('Content-type: image/png');
1533 richard 297
        imagepng($im);
298
    }
299
 
300
    get_vnstat_data();
301
    output_image();
1675 richard 302
?>