67b4ccd4a832939f129cb482dcd97e4382b9b23c
[racktables] / render_image.php
1 <?php
2
3 $_REQUEST['page'] = 'rackspace';
4 $_REQUEST['tab'] = 'default';
5
6 require 'inc/init.php';
7
8 assertStringArg ('img', __FILE__);
9 switch ($_REQUEST['img'])
10 {
11 case 'minirack': // rack security context
12 assertUIntArg ('rack_id', __FILE__);
13 $pageno = 'rack';
14 $tabno = 'default';
15 fixContext();
16 if (!permitted())
17 renderAccessDeniedImage();
18 else
19 renderRackThumb ($_REQUEST['rack_id']);
20 break;
21 case 'progressbar': // no security context
22 assertUIntArg ('done', __FILE__, TRUE);
23 renderProgressBarImage ($_REQUEST['done']);
24 break;
25 case 'view': // file security context
26 case 'preview':
27 assertUIntArg ('file_id', __FILE__);
28 $pageno = 'file';
29 $tabno = 'default';
30 fixContext();
31 if (!permitted())
32 renderAccessDeniedImage();
33 else
34 renderFilePreview ($_REQUEST['file_id']);
35 break;
36 default:
37 renderError();
38 }
39
40 //------------------------------------------------------------------------
41 function renderError ()
42 {
43 // A hardcoded value is worth of saving lots of code here.
44 $img = imagecreatefrompng ('pix/error.png');
45 header("Content-type: image/png");
46 imagepng ($img);
47 imagedestroy ($img);
48 }
49
50 // Having a local caching array speeds things up. A little.
51 function colorFromHex ($image, $hex)
52 {
53 static $colorcache = array ();
54 if (isset ($colorcache[$hex]))
55 return $colorcache[$hex];
56 $r = hexdec ('0x' . substr ($hex, 0, 2));
57 $g = hexdec ('0x' . substr ($hex, 2, 2));
58 $b = hexdec ('0x' . substr ($hex, 4, 2));
59 $c = imagecolorallocate ($image, $r, $g, $b);
60 $colorcache[$hex] = $c;
61 return $c;
62 }
63
64 function renderRackThumb ($rack_id = 0)
65 {
66 // Don't call DB extra times, hence we are most probably not the
67 // only script wishing to acces the same data now.
68 header("Content-type: image/png");
69 $thumbcache = loadThumbCache ($rack_id);
70 if ($thumbcache !== NULL)
71 echo $thumbcache;
72 else
73 {
74 ob_start();
75 generateMiniRack ($rack_id);
76 $capture = ob_get_contents();
77 ob_end_flush();
78 saveThumbCache ($rack_id, $capture);
79 }
80 }
81
82 // Output a binary string containing the PNG minirack.
83 function generateMiniRack ($rack_id = 0)
84 {
85 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
86 {
87 renderError();
88 return;
89 }
90 markupObjectProblems ($rackData);
91 // Cache in a local array, because we are going to use those values often.
92 $rtwidth = array
93 (
94 0 => getConfigVar ('rtwidth_0'),
95 1 => getConfigVar ('rtwidth_1'),
96 2 => getConfigVar ('rtwidth_2')
97 );
98 $rtdepth = 9;
99 $offset[0] = 3;
100 $offset[1] = 3 + $rtwidth[0];
101 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
102 $totalheight = 3 + 3 + $rackData['height'] * 2;
103 $totalwidth = $offset[2] + $rtwidth[2] + 3;
104 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
105 or die("Cannot Initialize new GD image stream");
106 // cache our palette as well
107 $color = array();
108 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
109 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
110 $color['black'] = colorFromHex ($img, '000000');
111 $color['gray'] = colorFromHex ($img, 'c0c0c0');
112 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
113 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
114 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
115 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
116 {
117 for ($locidx = 0; $locidx < 3; $locidx++)
118 {
119 $colorcode = $rackData[$unit_no][$locidx]['state'];
120 if (isset ($rackData[$unit_no][$locidx]['hl']))
121 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
122 imagerectangle
123 (
124 $img,
125 $offset[$locidx],
126 3 + ($rackData['height'] - $unit_no) * 2,
127 $offset[$locidx] + $rtwidth[$locidx] - 1,
128 3 + ($rackData['height'] - $unit_no) * 2 + 1,
129 $color[$colorcode]
130 );
131 }
132 }
133 imagepng ($img);
134 imagedestroy ($img);
135 }
136
137 function renderProgressBarImage ($done)
138 {
139 $img = @imagecreatetruecolor (100, 10);
140 switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
141 {
142 case 'sparenetwork':
143 $color['T'] = colorFromHex ($img, '808080');
144 $color['F'] = colorFromHex ($img, 'c0c0c0');
145 break;
146 case 'rackspace': // teal
147 default:
148 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
149 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
150 }
151 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
152 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
153 header("Content-type: image/png");
154 imagepng ($img);
155 imagedestroy ($img);
156 }
157
158 function renderAccessDeniedImage ()
159 {
160 $img = @imagecreatetruecolor (1, 1);
161 imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
162 header("Content-type: image/png");
163 imagepng ($img);
164 imagedestroy ($img);
165 }
166
167 function renderFilePreview ($file_id = 0)
168 {
169 $file = getFile ($file_id);
170 $image = imagecreatefromstring ($file['contents']);
171 $width = imagesx ($image);
172 $height = imagesy ($image);
173 if ($_REQUEST['img'] == 'preview' and ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS')))
174 {
175 // TODO: cache thumbs for faster page generation
176 $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
177 $newwidth = $width * $ratio;
178 $newheight = $height * $ratio;
179 $resampled = imagecreatetruecolor ($newwidth, $newheight);
180 imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
181 imagedestroy ($image);
182 $image = $resampled;
183 unset ($resampled);
184 }
185 header("Content-type: image/png"); // don't announce content-length, it may have changed after resampling
186 imagepng ($image);
187 imagedestroy ($image);
188 }
189
190 ?>