alecpl
2009-06-10 e20e31016215ae6d1ac759ff448ac9d1d71a7cd1
commit | author | age
3ea0e3 1 <?php
T 2 /*
3  +-----------------------------------------------------------------------+
155bbb 4  | bin/quotaimg.php                                                      |
3ea0e3 5  |                                                                       |
T 6  | This file is part of the RoundCube Webmail client                     |
cbbef3 7  | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland                 |
3ea0e3 8  | Licensed under the GNU GPL                                            |
T 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
155bbb 17  $Id$
3ea0e3 18
T 19 */
20
1608f4 21 define('INSTALL_PATH', realpath(dirname(__FILE__).'/..') . '/');
T 22 require INSTALL_PATH . 'program/include/iniset.php';
23
24 $RCMAIL = rcmail::get_instance();
25
7ce193 26 $used   = isset($_GET['u']) ? intval($_GET['u']) : '??';
T 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']));
3ea0e3 30
15062e 31 /**
T 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()
f8895e 67  * 
T 68  * @todo Make colors a config option.
15062e 69  */
fda695 70 function genQuota($used, $total, $width, $height)
3ea0e3 71 {
T 72     $unknown = false;
f8895e 73     $border  = 0;
3ea0e3 74
f8895e 75     $font    = 2;
fda695 76     $padding = 0;
3ea0e3 77
fe3e67 78     $limit['high'] = 80;
A 79     $limit['mid']  = 55;
f8895e 80     $limit['low']  = 0;
3ea0e3 81
T 82     // Fill Colors
fe3e67 83     $color['fill']['high'] = '243, 49, 49';      // Near quota fill color
A 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
3ea0e3 86
T 87     // Background colors
f8895e 88     $color['bg']['OL']      = '215, 13, 13';   // Over limit bbackground
T 89     $color['bg']['Unknown'] = '238, 99, 99';   // Unknown background
90     $color['bg']['quota']   = '255, 255, 255'; // Normal quota background
3ea0e3 91
T 92     // Misc. Colors
93     $color['border'] = '0, 0, 0';
fe3e67 94     $color['text']['high'] = '255, 255, 255';  // white text for red background
A 95     $color['text']['mid'] = '102, 102, 102';
96     $color['text']['low'] = '102, 102, 102';
97     $color['text']['normal'] = '102, 102, 102';
3ea0e3 98
T 99
15062e 100     /************************************
3ea0e3 101      *****    DO NOT EDIT BELOW HERE    *****
15062e 102      ***********************************/
3ea0e3 103
fe3e67 104     // @todo: Set to "??" instead?
23a2ee 105     if (preg_match('/^[^0-9?]*$/', $used) || preg_match('/^[^0-9?]*$/', $total)) {
3ea0e3 106         return false; 
cb15aa 107     }
f11541 108
cb15aa 109     if (strpos($used, '?') !== false || strpos($total, '?') !== false && $used != 0) {
3ea0e3 110         $unknown = true; 
cb15aa 111     }
3ea0e3 112
T 113     $im = imagecreate($width, $height);
fda695 114
f8895e 115     if ($border) {
fda695 116         list($r, $g, $b) = explode(',', $color['border']);
f8895e 117         
fda695 118         $borderc = imagecolorallocate($im, $r, $g, $b);
f8895e 119         
fda695 120         imageline($im, 0, 0, $width, 0, $borderc);
T 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);
f11541 124     }
fda695 125         
f8895e 126     if ($unknown) {
fe3e67 127         list($r, $g, $b) = explode(',', $color['text']['normal']);
A 128         $text = imagecolorallocate($im, $r, $g, $b);
fda695 129         list($r, $g, $b) = explode(',', $color['bg']['Unknown']);
T 130         $background = imagecolorallocate($im, $r, $g, $b);
fe3e67 131
fda695 132         imagefilledrectangle($im, 0, 0, $width, $height, $background);
T 133
134         $string = 'Unknown';
f8895e 135         $mid    = floor(($width-(strlen($string)*imagefontwidth($font)))/2)+1;
fda695 136         imagestring($im, $font, $mid, $padding, $string, $text);
f8895e 137     } else if ($used > $total) {
fe3e67 138         list($r, $g, $b) = explode(',', $color['text']['normal']);
A 139         $text = imagecolorallocate($im, $r, $g, $b);
fda695 140         list($r, $g, $b) = explode(',', $color['bg']['OL']);
T 141         $background = imagecolorallocate($im, $r, $g, $b);
f8895e 142         
fda695 143         imagefilledrectangle($im, 0, 0, $width, $height, $background);
T 144
145         $string = 'Over Limit';
f8895e 146         $mid    = floor(($width-(strlen($string)*imagefontwidth($font)))/2)+1;
fda695 147         imagestring($im, $font, $mid, $padding, $string, $text);
f8895e 148     } else {
fda695 149         list($r, $g, $b) = explode(',', $color['bg']['quota']);
T 150         $background = imagecolorallocate($im, $r, $b, $g);
f8895e 151         
fda695 152         imagefilledrectangle($im, 0, 0, $width, $height, $background);
T 153         
154         $quota = ($used==0)?0:(round($used/$total, 2)*100);
155
f8895e 156         if ($quota >= $limit['high']) {
fe3e67 157             list($r, $g, $b) = explode(',', $color['text']['high']);
A 158             $text = imagecolorallocate($im, $r, $g, $b);
fda695 159             list($r, $g, $b) = explode(',', $color['fill']['high']);
T 160             $fill = imagecolorallocate($im, $r, $g, $b);
f8895e 161         } elseif($quota >= $limit['mid']) {
fe3e67 162             list($r, $g, $b) = explode(',', $color['text']['mid']);
A 163             $text = imagecolorallocate($im, $r, $g, $b);
fda695 164             list($r, $g, $b) = explode(',', $color['fill']['mid']);
T 165             $fill = imagecolorallocate($im, $r, $g, $b);
f8895e 166         } else {
cb15aa 167             // if($quota >= $limit['low'])
fe3e67 168             list($r, $g, $b) = explode(',', $color['text']['low']);
A 169             $text = imagecolorallocate($im, $r, $g, $b);
fda695 170             list($r, $g, $b) = explode(',', $color['fill']['low']);
T 171             $fill = imagecolorallocate($im, $r, $g, $b);
f11541 172         }
fda695 173
T 174         $quota_width = $quota / 100 * $width;
b659c3 175         if ($quota_width)
A 176             imagefilledrectangle($im, $border, 0, $quota_width, $height-2*$border, $fill);
fda695 177
f8895e 178         $string = $quota . '%';
T 179         $mid    = floor(($width-(strlen($string)*imagefontwidth($font)))/2)+1;
cb15aa 180         // Print percent in black
f8895e 181         imagestring($im, $font, $mid, $padding, $string, $text); 
f11541 182     }
3ea0e3 183
T 184     header('Content-Type: image/gif');
cb15aa 185
T 186     // cache for 1 hour
187     $maxage = 3600;
188     header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$maxage). ' GMT');
189     header('Cache-Control: max-age=' . $maxage);
fda695 190     
3ea0e3 191     imagegif($im);
T 192     imagedestroy($im);
193 }
194
1608f4 195 if (!empty($RCMAIL->user->ID) && $width > 1 && $height > 1) {
T 196     genQuota($used, $quota, $width, $height);
cb15aa 197 }
T 198 else {
1608f4 199     header("HTTP/1.0 403 Forbidden");
T 200     echo "Requires a valid user session and positive values";
cb15aa 201 }
T 202
3ea0e3 203 exit;
fe7618 204 ?>