Subversion Repositories ALCASAR

Rev

Rev 1533 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 1533 Rev 1675
Line 15... Line 15...
15
    // You should have received a copy of the GNU General Public License
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
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
17
    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
    //
18
    //
19
    //
19
    //
20
    // see file COPYING or at http://www.gnu.org/licenses/gpl.html 
20
    // see file COPYING or at http://www.gnu.org/licenses/gpl.html
21
    // for more information.
21
    // for more information.
22
    //
22
    //
23
    require 'config.php';
23
    require 'config.php';
24
    require 'localize.php';
24
    require 'localize.php';
25
    require 'vnstat.php';
25
    require 'vnstat.php';
Line 30... Line 30...
30
 
30
 
31
    function allocate_color($im, $colors)
31
    function allocate_color($im, $colors)
32
    {
32
    {
33
	return imagecolorallocatealpha($im, $colors[0], $colors[1], $colors[2], $colors[3]);
33
	return imagecolorallocatealpha($im, $colors[0], $colors[1], $colors[2], $colors[3]);
34
    }
34
    }
35
            
35
 
36
    function init_image()
36
    function init_image()
37
    {
37
    {
38
        global $im, $xlm, $xrm, $ytm, $ybm, $iw, $ih,$graph, $cl, $iface, $colorscheme, $style;
38
        global $im, $xlm, $xrm, $ytm, $ybm, $iw, $ih,$graph, $cl, $iface, $colorscheme, $style;
39
 
39
 
40
        if ($graph == 'none')
40
        if ($graph == 'none')
41
            return;
41
            return;
42
 
42
 
43
        //
43
        //
44
        // image object
44
        // image object
45
        //    
45
        //
46
        $xlm = 70;
46
        $xlm = 70;
47
        $xrm = 20;
47
        $xrm = 20;
48
        $ytm = 35;
48
        $ytm = 35;
49
        $ybm = 60;
49
        $ybm = 60;
50
        if ($graph == 'small')
50
        if ($graph == 'small')
51
        {
51
        {
52
            $iw = 300 + $xrm + $xlm;
52
            $iw = 300 + $xrm + $xlm;
53
            $ih = 100 + $ytm + $ybm;    
53
            $ih = 100 + $ytm + $ybm;
54
        }
54
        }
55
        else
55
        else
56
        {
56
        {
57
            $iw = 600 + $xrm + $xlm;
57
            $iw = 600 + $xrm + $xlm;
58
            $ih = 200 + $ytm + $ybm;
58
            $ih = 200 + $ytm + $ybm;
Line 73... Line 73...
73
        $cl['border'] = allocate_color($im, $cs['border']);
73
        $cl['border'] = allocate_color($im, $cs['border']);
74
        $cl['rx'] = allocate_color($im, $cs['rx']);
74
        $cl['rx'] = allocate_color($im, $cs['rx']);
75
        $cl['rx_border'] = allocate_color($im, $cs['rx_border']);
75
        $cl['rx_border'] = allocate_color($im, $cs['rx_border']);
76
        $cl['tx'] = allocate_color($im, $cs['tx']);
76
        $cl['tx'] = allocate_color($im, $cs['tx']);
77
        $cl['tx_border'] = allocate_color($im, $cs['tx_border']);
77
        $cl['tx_border'] = allocate_color($im, $cs['tx_border']);
78
	
78
 
79
        imagefilledrectangle($im,0,0,$iw,$ih,$cl['image_background']);
79
        imagefilledrectangle($im,0,0,$iw,$ih,$cl['image_background']);
80
	imagefilledrectangle($im,$xlm,$ytm,$iw-$xrm,$ih-$ybm, $cl['background']);
80
	imagefilledrectangle($im,$xlm,$ytm,$iw-$xrm,$ih-$ybm, $cl['background']);
81
	
81
 
82
	$x_step = ($iw - $xlm - $xrm) / 12;
82
	$x_step = ($iw - $xlm - $xrm) / 12;
83
	$depth = ($x_step / 8) + 4;
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']);
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']);
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
 
86
 
87
	// draw title
87
	// draw title
88
	$text = T('Traffic data for')." $iface";
88
	$text = T('Traffic data for')." $iface";
89
 	$bbox = imagettfbbox(10, 0, GRAPH_FONT, $text);
89
 	$bbox = imagettfbbox(10, 0, GRAPH_FONT, $text);
90
	$textwidth = $bbox[2] - $bbox[0];
90
	$textwidth = $bbox[2] - $bbox[0];
91
	imagettftext($im, 10, 0, ($iw-$textwidth)/2, ($ytm/2), $cl['text'], GRAPH_FONT, $text);
91
	imagettftext($im, 10, 0, ($iw-$textwidth)/2, ($ytm/2), $cl['text'], GRAPH_FONT, $text);
92
		
92
 
93
    }
93
    }
94
 
94
 
95
    function draw_border()
95
    function draw_border()
96
    {
96
    {
97
        global $im,$cl,$iw,$ih;
97
        global $im,$cl,$iw,$ih;
98
 
98
 
99
        imageline($im,     0,    0,$iw-1,    0, $cl['border']);
99
        imageline($im,     0,    0,$iw-1,    0, $cl['border']);
100
        imageline($im,     0,$ih-1,$iw-1,$ih-1, $cl['border']);
100
        imageline($im,     0,$ih-1,$iw-1,$ih-1, $cl['border']);
101
        imageline($im,     0,    0,    0,$ih-1, $cl['border']);  
101
        imageline($im,     0,    0,    0,$ih-1, $cl['border']);
102
        imageline($im, $iw-1,    0,$iw-1,$ih-1, $cl['border']);
102
        imageline($im, $iw-1,    0,$iw-1,$ih-1, $cl['border']);
103
    }
103
    }
104
    
104
 
105
    function draw_grid($x_ticks, $y_ticks)
105
    function draw_grid($x_ticks, $y_ticks)
106
    {
106
    {
107
        global $im, $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm;
107
        global $im, $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm;
108
        $x_step = ($iw - $xlm - $xrm) / $x_ticks;
108
        $x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
109
        $y_step = ($ih - $ytm - $ybm) / $y_ticks;
109
        $y_step = ($ih - $ytm - $ybm) / $y_ticks;
110
	
110
 
111
	$depth = 10;//($x_step / 8) + 4;
111
	$depth = 10;//($x_step / 8) + 4;
112
 
112
 
113
        $ls = array($cl['grid_stipple_1'],$cl['grid_stipple_2']);
113
        $ls = array($cl['grid_stipple_1'],$cl['grid_stipple_2']);
114
        imagesetstyle($im, $ls);
114
        imagesetstyle($im, $ls);
115
        for ($i=$xlm;$i<=($iw-$xrm); $i += $x_step)
115
        for ($i=$xlm;$i<=($iw-$xrm); $i += $x_step)
Line 117... Line 117...
117
            imageline($im, $i, $ytm, $i, $ih - $ybm, IMG_COLOR_STYLED);
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);
118
	    imageline($im, $i, $ih - $ybm, $i - $depth, $ih - $ybm + $depth, IMG_COLOR_STYLED);
119
        }
119
        }
120
        for ($i=$ytm;$i<=($ih-$ybm); $i += $y_step)
120
        for ($i=$ytm;$i<=($ih-$ybm); $i += $y_step)
121
        {
121
        {
122
            imageline($im, $xlm, $i, $iw - $xrm, $i, IMG_COLOR_STYLED); 
122
            imageline($im, $xlm, $i, $iw - $xrm, $i, IMG_COLOR_STYLED);
123
	    imageline($im, $xlm, $i, $xlm - $depth, $i + $depth, IMG_COLOR_STYLED);
123
	    imageline($im, $xlm, $i, $xlm - $depth, $i + $depth, IMG_COLOR_STYLED);
124
        }
124
        }
125
        imageline($im, $xlm, $ytm, $xlm, $ih - $ybm, $cl['border']);
125
        imageline($im, $xlm, $ytm, $xlm, $ih - $ybm, $cl['border']);
126
        imageline($im, $xlm, $ih - $ybm, $iw - $xrm, $ih - $ybm, $cl['border']);
126
        imageline($im, $xlm, $ih - $ybm, $iw - $xrm, $ih - $ybm, $cl['border']);
127
    }
127
    }
128
    
-
 
129
    
128
 
130
    function draw_data($data)
129
    function draw_data($data)
131
    {
130
    {
132
        global $im,$cl,$iw,$ih,$xlm,$xrm,$ytm,$ybm;
131
        global $im,$cl,$iw,$ih,$xlm,$xrm,$ytm,$ybm;
133
 
132
 
134
        sort($data);
133
        sort($data);
Line 138... Line 137...
138
        $y_scale = 1;
137
        $y_scale = 1;
139
        $prescale = 1;
138
        $prescale = 1;
140
        $unit = 'K';
139
        $unit = 'K';
141
        $offset = 0;
140
        $offset = 0;
142
        $gr_h = $ih - $ytm - $ybm;
141
        $gr_h = $ih - $ytm - $ybm;
143
        $x_step = ($iw - $xlm - $xrm) / $x_ticks;
142
        $x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
144
        $y_step = ($ih - $ytm - $ybm) / $y_ticks;
143
        $y_step = ($ih - $ytm - $ybm) / $y_ticks;
145
        $bar_w = ($x_step / 2) ;
144
        $bar_w = ($x_step / 2) ;
146
 
145
 
147
        //
146
        //
148
        // determine scale
147
        // determine scale
Line 166... Line 165...
166
            $y_scale = $y_scale * 2;
165
            $y_scale = $y_scale * 2;
167
            if ($y_scale >= 1024)
166
            if ($y_scale >= 1024)
168
            {
167
            {
169
            $prescale = $prescale * 1024;
168
            $prescale = $prescale * 1024;
170
            $y_scale = $y_scale / 1024;
169
            $y_scale = $y_scale / 1024;
171
            if ($unit == 'K') 
170
            if ($unit == 'K')
172
                $unit = 'M';
171
                $unit = 'M';
173
            else if ($unit == 'M')
172
            else if ($unit == 'M')
174
                $unit = 'G';
173
                $unit = 'G';
175
            else if ($unit == 'G')
174
            else if ($unit == 'G')
176
                $unit = 'T';
175
                $unit = 'T';
177
            }
176
            }
178
        }
177
        }
179
 
178
 
180
        draw_grid($x_ticks, $y_ticks);
179
        draw_grid($x_ticks, $y_ticks);
181
	
180
 
182
        //
181
        //
183
        // graph scale factor (per pixel)
182
        // graph scale factor (per pixel)
184
        //
183
        //
185
	imagesetthickness($im, 1);
184
	imagesetthickness($im, 1);
186
        $sf = ($prescale * $y_scale * $y_ticks) / $gr_h;
185
        $sf = ($prescale * $y_scale * $y_ticks) / $gr_h;
187
 
186
 
188
        if ($data[0] == 'nodata')
187
        if (count($data) == 0)
189
        {
188
        {
190
            $text = 'no data available';
189
            $text = T('no data available');
191
	    $bbox = imagettfbbox(10, 0, GRAPH_FONT, $text);
190
	    $bbox = imagettfbbox(10, 0, GRAPH_FONT, $text);
192
	    $textwidth = $bbox[2] - $bbox[0];
191
	    $textwidth = $bbox[2] - $bbox[0];
193
	    imagettftext($im, 10, 0, ($iw-$textwidth)/2, $ytm + 80, $cl['text'], GRAPH_FONT, $text);
192
	    imagettftext($im, 10, 0, ($iw-$textwidth)/2, $ytm + 80, $cl['text'], GRAPH_FONT, $text);
194
        }
193
        }
195
        else
194
        else
196
        {
195
        {
197
            //
196
            //
198
            // draw bars
197
            // draw bars
199
            //      
198
            //
200
            for ($i=0; $i<$x_ticks; $i++)
199
            for ($i=0; $i<$x_ticks; $i++)
201
            {
200
            {
202
        	$x = $xlm + ($i * $x_step);
201
        	$x = $xlm + ($i * $x_step);
203
        	$y = $ytm + ($ih - $ytm - $ybm) - (($data[$i]['rx'] - $offset) / $sf);
202
        	$y = $ytm + ($ih - $ytm - $ybm) - (($data[$i]['rx'] - $offset) / $sf);
204
		
203
 
205
		$depth = $x_step / 8;
204
		$depth = $x_step / 8;
206
		$space = 0;
205
		$space = 0;
207
		
206
 
208
		$x1 = $x;
207
		$x1 = $x;
209
		$y1 = $y;
208
		$y1 = $y;
210
		$x2 = $x + $bar_w - $space;
209
		$x2 = $x + $bar_w - $space;
211
		$y2 = $ih - $ybm;
210
		$y2 = $ih - $ybm;
212
		
211
 
213
        	imagefilledrectangle($im, $x1, $y1, $x2, $y2, $cl['rx']);
212
        	imagefilledrectangle($im, $x1, $y1, $x2, $y2, $cl['rx']);
214
		imagerectangle($im, $x1, $y1, $x2, $y2, $cl['rx_border']);
213
		imagerectangle($im, $x1, $y1, $x2, $y2, $cl['rx_border']);
215
		
214
 
216
		imagefilledrectangle($im, $x1 - $depth, $y1 + $depth, $x2 -$depth, $y2 + $depth, $cl['rx']);
215
		imagefilledrectangle($im, $x1 - $depth, $y1 + $depth, $x2 -$depth, $y2 + $depth, $cl['rx']);
217
		imagerectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['rx_border']);
216
		imagerectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['rx_border']);
218
		
217
 
219
		imagefilledpolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['rx']);
218
		imagefilledpolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['rx']);
220
		imagepolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['rx_border']);
219
		imagepolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['rx_border']);
221
		imagefilledpolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['rx']);
220
		imagefilledpolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['rx']);
222
		imagepolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['rx_border']);
221
		imagepolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['rx_border']);
223
 
222
 
Line 225... Line 224...
225
		$x1 = $x1 + $bar_w;
224
		$x1 = $x1 + $bar_w;
226
		$x2 = $x2 + $bar_w;
225
		$x2 = $x2 + $bar_w;
227
 
226
 
228
        	imagefilledrectangle($im, $x1, $y1, $x2, $y2, $cl['tx']);
227
        	imagefilledrectangle($im, $x1, $y1, $x2, $y2, $cl['tx']);
229
		imagerectangle($im, $x1, $y1, $x2, $y2, $cl['tx_border']);
228
		imagerectangle($im, $x1, $y1, $x2, $y2, $cl['tx_border']);
230
		
229
 
231
        	imagefilledrectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['tx']);
230
        	imagefilledrectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['tx']);
232
		imagerectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['tx_border']);		
231
		imagerectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['tx_border']);
233
		
232
 
234
		imagefilledpolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['tx']);
233
		imagefilledpolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['tx']);
235
		imagepolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['tx_border']);
234
		imagepolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['tx_border']);
236
		imagefilledpolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['tx']);
235
		imagefilledpolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['tx']);
237
		imagepolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['tx_border']);
236
		imagepolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['tx_border']);
238
            }
237
            }
239
    
238
 
240
            //
239
            //
241
            // axis labels
240
            // axis labels
242
            //
241
            //
243
            for ($i=0; $i<=$y_ticks; $i++)
242
            for ($i=0; $i<=$y_ticks; $i++)
244
            {
243
            {
Line 263... Line 262...
263
        //
262
        //
264
        // legend
263
        // legend
265
        //
264
        //
266
        imagefilledrectangle($im, $xlm, $ih-$ybm+39, $xlm+8,$ih-$ybm+47,$cl['rx']);
265
        imagefilledrectangle($im, $xlm, $ih-$ybm+39, $xlm+8,$ih-$ybm+47,$cl['rx']);
267
        imagerectangle($im, $xlm, $ih-$ybm+39, $xlm+8,$ih-$ybm+47,$cl['text']);
266
        imagerectangle($im, $xlm, $ih-$ybm+39, $xlm+8,$ih-$ybm+47,$cl['text']);
268
	imagettftext($im, 8,0, $xlm+14, $ih-$ybm+48,$cl['text'], GRAPH_FONT,'bytes in');
267
	imagettftext($im, 8,0, $xlm+14, $ih-$ybm+48,$cl['text'], GRAPH_FONT,T('bytes in'));
269
 
268
 
270
        imagefilledrectangle($im, $xlm+120 , $ih-$ybm+39, $xlm+128,$ih-$ybm+47,$cl['tx']);
269
        imagefilledrectangle($im, $xlm+120 , $ih-$ybm+39, $xlm+128,$ih-$ybm+47,$cl['tx']);
271
        imagerectangle($im, $xlm+120, $ih-$ybm+39, $xlm+128,$ih-$ybm+47,$cl['text']);
270
        imagerectangle($im, $xlm+120, $ih-$ybm+39, $xlm+128,$ih-$ybm+47,$cl['text']);
272
	imagettftext($im, 8,0, $xlm+134, $ih-$ybm+48,$cl['text'], GRAPH_FONT,'bytes out'); 
271
	imagettftext($im, 8,0, $xlm+134, $ih-$ybm+48,$cl['text'], GRAPH_FONT,T('bytes out'));
273
    }
272
    }
274
 
273
 
275
    function output_image()
274
    function output_image()
276
    {
275
    {
277
        global $page,$hour,$day,$month,$im,$iface;
276
        global $page,$hour,$day,$month,$im,$iface;
Line 291... Line 290...
291
        }
290
        }
292
        else if ($page == 'm')
291
        else if ($page == 'm')
293
        {
292
        {
294
            draw_data($month);
293
            draw_data($month);
295
        }
294
        }
296
	
295
 
297
        header('Content-type: image/png');	
296
        header('Content-type: image/png');
298
        imagepng($im);
297
        imagepng($im);
299
    }
298
    }
300
 
299
 
301
    get_vnstat_data();
300
    get_vnstat_data();
302
    output_image();
301
    output_image();
303
?>        
302
?>