source: subversion/branches/release-0.2-beta/bin/quotaimg.php @ 2162

Last change on this file since 2162 was 2162, checked in by thomasb, 4 years ago

Fix vulnerable bin scripts

File size: 6.5 KB
Line 
1<?php
2/*
3 +-----------------------------------------------------------------------+
4 | bin/quotaimg.php                                                      |
5 |                                                                       |
6 | This file is part of the RoundCube Webmail client                     |
7 | Copyright (C) 2005-2008, RoundCube Dev. - Switzerland                 |
8 | Licensed under the GNU GPL                                            |
9 |                                                                       |
10 | PURPOSE:                                                              |
11 |   Create a GIF image showing the mailbox quot as bar                  |
12 |                                                                       |
13 +-----------------------------------------------------------------------+
14 | Author: Brett Patterson <brett2@umbc.edu>                             |
15 +-----------------------------------------------------------------------+
16
17 $Id$
18
19*/
20
21$used   = isset($_GET['u']) ? intval($_GET['u']) : '??';
22$quota  = isset($_GET['q']) ? intval($_GET['q']) : '??';
23$width  = empty($_GET['w']) ? 100 : min(300, intval($_GET['w']));
24$height = empty($_GET['h']) ? 14  : min(50,  intval($_GET['h']));
25
26/**
27 * Quota display
28 *
29 *      Modify the following few elements to change the display of the image.
30 *      Modifiable attributes are:
31 *      bool    border  ::      Defines whether you want to show a border around it?
32 *      bool    unknown ::      Leave default; Defines whether quota is "unknown"
33 *
34 *      int             height  ::      Defines height of the image
35 *      int             width   ::      Defines width of the image
36 *      int             font    ::      Changes the font size & font used in the GD library.
37 *                                              Available values are from 1 to 5.
38 *      int             padding ::      Changes the offset (in pixels) from the top of the image
39 *                      to where the top of the text will be aligned. User
40 *                      greater than 0 to ensure text is off the border.
41 *      array   limit   ::      Holds the integer values of in an associative array as
42 *                      to what defines the upper and lower levels for quota
43 *                      display.
44 *                                              High - Quota is nearing capacity.
45 *                                              Mid  - Quota is around the middle
46 *                                              Low  - Currently not used.
47 *      array   color   ::      An associative array of strings of comma separated
48 *                      values (R,G,B) for use in color creation.  Define the
49 *                      RGB values you'd like to use. A list of colors (and
50 *                      their RGB values) can be found here:
51 *                                              http://www.december.com/html/spec/colorcodes.html
52 *
53 * @return void
54 *
55 * @param mixed $used   The amount used, or ?? if unknown.
56 * @param mixed $total  The total available, or ?? if unknown.
57 * @param int   $width  Width of the image.
58 * @param int   $height Height of the image.
59 *
60 * @see rcube_imap::get_quota()
61 * @see iil_C_GetQuota()
62 *
63 * @todo Make colors a config option.
64 */
65function genQuota($used, $total, $width, $height)
66{
67        $unknown = false;
68        $border  = 0;
69
70        $font    = 2;
71        $padding = 0;
72
73        $limit['high'] = 70;
74        $limit['mid']  = 45;
75        $limit['low']  = 0;
76
77        // Fill Colors
78        $color['fill']['high'] = '215, 13, 13';   // Near quota fill color
79        $color['fill']['mid']  = '126, 192, 238'; // Mid-area of quota fill color
80        $color['fill']['low']  = '147, 225, 100'; // Far from quota fill color
81
82        // Background colors
83        $color['bg']['OL']      = '215, 13, 13';   // Over limit bbackground
84        $color['bg']['Unknown'] = '238, 99, 99';   // Unknown background
85        $color['bg']['quota']   = '255, 255, 255'; // Normal quota background
86
87        // Misc. Colors
88        $color['border'] = '0, 0, 0';
89        $color['text']   = '102, 102, 102';
90
91
92        /************************************
93         *****  DO NOT EDIT BELOW HERE  *****
94         ***********************************/
95
96    // @todo: Set to "??" instead?
97        if (ereg("^[^0-9?]*$", $used) || ereg("^[^0-9?]*$", $total)) {
98                return false; 
99        }
100
101        if (strpos($used, '?') !== false || strpos($total, '?') !== false && $used != 0) {
102                $unknown = true; 
103        }
104
105        $im = imagecreate($width, $height);
106
107        if ($border) {
108                list($r, $g, $b) = explode(',', $color['border']);
109       
110                $borderc = imagecolorallocate($im, $r, $g, $b);
111       
112                imageline($im, 0, 0, $width, 0, $borderc);
113                imageline($im, 0, $height-$border, 0, 0, $borderc);
114                imageline($im, $width-1, 0, $width-$border, $height, $borderc);
115                imageline($im, $width, $height-$border, 0, $height-$border, $borderc);
116        }
117               
118        list($r, $g, $b) = explode(',', $color['text']);
119        $text = imagecolorallocate($im, $r, $g, $b);
120
121        if ($unknown) {
122                list($r, $g, $b) = explode(',', $color['bg']['Unknown']);
123                $background = imagecolorallocate($im, $r, $g, $b);
124                imagefilledrectangle($im, 0, 0, $width, $height, $background);
125
126                $string = 'Unknown';
127                $mid    = floor(($width-(strlen($string)*imagefontwidth($font)))/2)+1;
128                imagestring($im, $font, $mid, $padding, $string, $text);
129        } else if ($used > $total) {
130                list($r, $g, $b) = explode(',', $color['bg']['OL']);
131       
132                $background = imagecolorallocate($im, $r, $g, $b);
133       
134                imagefilledrectangle($im, 0, 0, $width, $height, $background);
135
136                $string = 'Over Limit';
137                $mid    = floor(($width-(strlen($string)*imagefontwidth($font)))/2)+1;
138                imagestring($im, $font, $mid, $padding, $string, $text);
139        } else {
140                list($r, $g, $b) = explode(',', $color['bg']['quota']);
141       
142                $background = imagecolorallocate($im, $r, $b, $g);
143       
144                imagefilledrectangle($im, 0, 0, $width, $height, $background);
145               
146                $quota = ($used==0)?0:(round($used/$total, 2)*100);
147
148                if ($quota >= $limit['high']) {
149                        list($r, $g, $b) = explode(',', $color['fill']['high']);
150                        $fill = imagecolorallocate($im, $r, $g, $b);
151                } elseif($quota >= $limit['mid']) {
152                        list($r, $g, $b) = explode(',', $color['fill']['mid']);
153                        $fill = imagecolorallocate($im, $r, $g, $b);
154                } else {
155                        // if($quota >= $limit['low'])
156                        list($r, $g, $b) = explode(',', $color['fill']['low']);
157                        $fill = imagecolorallocate($im, $r, $g, $b);
158                }
159
160                $quota_width = $quota / 100 * $width;
161                imagefilledrectangle($im, $border, 0, $quota_width, $height-2*$border, $fill);
162
163                $string = $quota . '%';
164                $mid    = floor(($width-(strlen($string)*imagefontwidth($font)))/2)+1;
165                // Print percent in black
166                imagestring($im, $font, $mid, $padding, $string, $text); 
167        }
168
169        header('Content-Type: image/gif');
170
171        // cache for 1 hour
172        $maxage = 3600;
173        header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$maxage). ' GMT');
174        header('Cache-Control: max-age=' . $maxage);
175       
176        imagegif($im);
177        imagedestroy($im);
178}
179
180if ($width > 1 && $height > 1) {
181        genQuota($used, $quota, $width, $height); 
182}
183else {
184        header("HTTP/1.0 404 Not Found");
185}
186
187exit;
188?>
Note: See TracBrowser for help on using the repository browser.