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