r4178 bugfix: PHP warnings were appearing before rackspace on SLB object page, defaul...
[racktables] / render_image.php
CommitLineData
b325120a 1<?php
e673ee24 2
21d40daa
DO
3define ('CACHE_DURATION', 604800); // 7 * 24 * 3600
4if ( // 'progressbar's never change, force cache hit before loading init.php
5 isset ($_SERVER['HTTP_IF_MODIFIED_SINCE'])
6 && $_REQUEST['img'] == 'progressbar'
7)
8{
b6993903 9 $client_time = HTTPDateToUnixTime ($_SERVER['HTTP_IF_MODIFIED_SINCE']);
21d40daa
DO
10 if ($client_time !== FALSE && $client_time !== -1) // readable
11 {
12 $server_time = time();
13 // not in future and not yet expired
14 if ($client_time <= $server_time && $client_time + CACHE_DURATION >= $server_time)
15 {
16 header ('Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'], TRUE, 304);
17 exit;
18 }
19 }
20}
90a3d6d8
DY
21
22ob_start();
23try {
e673ee24
DO
24require 'inc/init.php';
25
0cc24e9a 26assertStringArg ('img');
d15b2330
DO
27switch ($_REQUEST['img'])
28{
da958e52 29 case 'minirack': // rack security context
0cc24e9a 30 assertUIntArg ('rack_id');
da958e52 31 $pageno = 'rack';
d15b2330 32 $tabno = 'default';
da958e52
DO
33 fixContext();
34 if (!permitted())
35 renderAccessDeniedImage();
36 else
37 renderRackThumb ($_REQUEST['rack_id']);
d15b2330 38 break;
da958e52 39 case 'progressbar': // no security context
0cc24e9a 40 assertUIntArg ('done', TRUE);
21d40daa
DO
41 // 'progressbar's never change, make browser cache the result
42 header ('Cache-Control: private, max-age=' . CACHE_DURATION . ', pre-check=' . CACHE_DURATION);
4bbc4560 43 header ('Last-Modified: ' . gmdate (DATE_RFC1123));
d15b2330
DO
44 renderProgressBarImage ($_REQUEST['done']);
45 break;
0df8c52b 46 case 'preview': // file security context
0cc24e9a 47 assertUIntArg ('file_id');
37e59768 48 $pageno = 'file';
0df8c52b 49 $tabno = 'download';
37e59768
DO
50 fixContext();
51 if (!permitted())
52 renderAccessDeniedImage();
53 else
43d0585d 54 renderFilePreview ($_REQUEST['file_id'], $_REQUEST['img']);
37e59768 55 break;
d15b2330
DO
56 default:
57 renderError();
58}
e673ee24 59
90a3d6d8
DY
60ob_end_flush();
61}
62catch (Exception $e)
63{
64 ob_end_clean();
3a089a44 65 renderError();
90a3d6d8
DY
66}
67
e673ee24 68//------------------------------------------------------------------------
b6993903
DO
69function HTTPDateToUnixTime ($string)
70{
71 //Written per RFC 2616 3.3.1 - Full Date
72 //http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
73 $month_number = array
74 (
75 'Jan' => 1,
76 'Feb' => 2,
77 'Mar' => 3,
78 'Apr' => 4,
79 'May' => 5,
80 'Jun' => 6,
81 'Jul' => 7,
82 'Aug' => 8,
83 'Sep' => 9,
84 'Oct' => 10,
85 'Nov' => 11,
86 'Dec' => 12,
87 );
88
89 $formats = array();
90 $formats['rfc1123'] = '/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat), (\d{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d{2}):(\d{2}):(\d{2}) GMT$/';
91 $formats['rfc850'] = '/^(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday), (\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{2}) (\d{2}):(\d{2}):(\d{2}) GMT$/';
92 $formats['asctime'] = '/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{2}|\d{1}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/';
93
94 $matches = array();
95 if (preg_match ($formats['rfc1123'], $string, $matches)) {
96 $hours = $matches[5];
97 $minutes = $matches[6];
98 $seconds = $matches[7];
99 $month = $month_number[$matches[3]];
100 $day = $matches[2];
101 $year = $matches[4];
102 } elseif (preg_match ($formats['rfc850'], $string, $matches)) {
103 $hours = $matches[5];
104 $minutes = $matches[6];
105 $seconds = $matches[7];
106 $month = $month_number[substr($matches[3],0,3)];
107 $day = $matches[2];
108 $year = $matches[4];
109 } elseif (preg_match ($formats['asctime'], $string, $matches)) {
110 $hours = $matches[4];
111 $minutes = $matches[5];
112 $seconds = $matches[6];
113 $month = $month_number[$matches[2]];
114 $day = $matches[3];
115 $year = $matches[7];
116 } else
117 return false;
118 return gmmktime ($hours, $minutes, $seconds, $month, $day, $year);
119}
120
e673ee24
DO
121function renderError ()
122{
e673ee24 123 header("Content-type: image/png");
d5f8fa19
DO
124 // "ERROR", 76x17, red on white
125 echo base64_decode
126 (
127 'iVBORw0KGgoAAAANSUhEUgAAAEwAAAARCAYAAAB3h0oCAAAAAXNSR0IArs4c6QAAALBJREFUWMPt' .
128 'WFsOwCAIG4v3vzL7WEyWxQdVwM1A4l/F2iHVETPzESGOMyTAInURRP0suUhb2FIho/jWXO38w4KN' .
129 'LPDGEt2jlgPBZxFKc2o8UT7Lj6SkAmfw1nx+28MkVWQlcjT9EOwjLqnpaNImi+I1j/YSl5RY/gx+' .
130 'VCCF/MnkCz4JZQtvEUXx1nyW9jCUlPVLbTJ/3MO2dsnWRq2Nwl2wTarM51rhsVEnDhT/w7C4APaJ' .
131 'ZhkIGYaUAAAAAElFTkSuQmCC'
132 );
da958e52
DO
133}
134
9c0b0016 135// Having a local caching array speeds things up. A little.
e673ee24
DO
136function colorFromHex ($image, $hex)
137{
9c0b0016
DO
138 static $colorcache = array ();
139 if (isset ($colorcache[$hex]))
140 return $colorcache[$hex];
e673ee24
DO
141 $r = hexdec ('0x' . substr ($hex, 0, 2));
142 $g = hexdec ('0x' . substr ($hex, 2, 2));
143 $b = hexdec ('0x' . substr ($hex, 4, 2));
9c0b0016
DO
144 $c = imagecolorallocate ($image, $r, $g, $b);
145 $colorcache[$hex] = $c;
146 return $c;
e673ee24
DO
147}
148
149function renderRackThumb ($rack_id = 0)
35ee7bd0
DO
150{
151 // Don't call DB extra times, hence we are most probably not the
6b06a019 152 // only script wishing to access the same data now.
61a1d996
DO
153 if (NULL !== ($thumbcache = loadThumbCache ($rack_id)))
154 {
155 header("Content-type: image/png");
35ee7bd0 156 echo $thumbcache;
61a1d996
DO
157 return;
158 }
159 ob_start();
160 if (FALSE !== generateMiniRack ($rack_id))
35ee7bd0 161 {
61a1d996
DO
162 $capture = ob_get_clean();
163 header("Content-type: image/png");
164 echo $capture;
68a0bd8a 165 usePreparedUpdateBlade ('Rack', array ('thumb_data' => base64_encode ($capture)), array ('id' => $rack_id));
61a1d996 166 return;
35ee7bd0 167 }
61a1d996
DO
168 // error text in the buffer
169 ob_end_flush();
35ee7bd0
DO
170}
171
61a1d996
DO
172// Output a binary string containing the PNG minirack. Indicate error with return code.
173function generateMiniRack ($rack_id)
e673ee24 174{
61a1d996
DO
175 if (NULL === ($rackData = spotEntity ('rack', $rack_id)))
176 return FALSE;
177 amplifyCell ($rackData);
e673ee24 178 markupObjectProblems ($rackData);
529eac25 179 global $rtwidth;
d5157018 180 $rtdepth = 9;
e673ee24
DO
181 $offset[0] = 3;
182 $offset[1] = 3 + $rtwidth[0];
183 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
184 $totalheight = 3 + 3 + $rackData['height'] * 2;
185 $totalwidth = $offset[2] + $rtwidth[2] + 3;
aa0e4c55
DO
186 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
187 or die("Cannot Initialize new GD image stream");
9c0b0016
DO
188 // cache our palette as well
189 $color = array();
190 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
d1adb55a 191 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
d5157018
DO
192 $color['black'] = colorFromHex ($img, '000000');
193 $color['gray'] = colorFromHex ($img, 'c0c0c0');
194 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
195 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
196 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
197 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
e673ee24
DO
198 {
199 for ($locidx = 0; $locidx < 3; $locidx++)
200 {
201 $colorcode = $rackData[$unit_no][$locidx]['state'];
202 if (isset ($rackData[$unit_no][$locidx]['hl']))
203 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
204 imagerectangle
205 (
206 $img,
207 $offset[$locidx],
208 3 + ($rackData['height'] - $unit_no) * 2,
209 $offset[$locidx] + $rtwidth[$locidx] - 1,
210 3 + ($rackData['height'] - $unit_no) * 2 + 1,
d1adb55a 211 $color[$colorcode]
e673ee24
DO
212 );
213 }
214 }
e673ee24
DO
215 imagepng ($img);
216 imagedestroy ($img);
61a1d996 217 return TRUE;
e673ee24
DO
218}
219
d15b2330
DO
220function renderProgressBarImage ($done)
221{
d1adb55a 222 $img = @imagecreatetruecolor (100, 10);
fec0c8da
DO
223 switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
224 {
225 case 'sparenetwork':
226 $color['T'] = colorFromHex ($img, '808080');
227 $color['F'] = colorFromHex ($img, 'c0c0c0');
228 break;
229 case 'rackspace': // teal
230 default:
231 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
232 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
233 }
d1adb55a
DO
234 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
235 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
074106cf
DO
236 for ($x = 20; $x <= 80; $x += 20)
237 {
238 $cc = $x > $done ? $color['T'] : $color['F'];
239 imagesetpixel ($img, $x, 0, $cc);
240 imagesetpixel ($img, $x, 1, $cc);
241 imagesetpixel ($img, $x, 4, $cc);
242 imagesetpixel ($img, $x, 5, $cc);
243 imagesetpixel ($img, $x, 8, $cc);
244 imagesetpixel ($img, $x, 9, $cc);
245 }
d15b2330
DO
246 header("Content-type: image/png");
247 imagepng ($img);
248 imagedestroy ($img);
d15b2330
DO
249}
250
2d75c30b
DO
251function renderAccessDeniedImage ()
252{
253 $img = @imagecreatetruecolor (1, 1);
254 imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
255 header("Content-type: image/png");
256 imagepng ($img);
257 imagedestroy ($img);
0df8c52b 258 die;
2d75c30b
DO
259}
260
43d0585d 261function renderFilePreview ($file_id = 0, $mode = 'view')
37e59768 262{
43d0585d 263 switch ($mode)
37e59768 264 {
43d0585d
DO
265 case 'view':
266 // GFX files can become really big, if we uncompress them in memory just to
267 // provide a PNG version of a file. To keep things working, just send the
268 // contents as is for known MIME types.
269 $file = getFile ($file_id);
270 if (!in_array ($file['type'], array ('image/jpeg', 'image/png', 'image/gif')))
271 {
6b06a019 272 renderError();
43d0585d
DO
273 break;
274 }
275 header("Content-type: ${file['type']}");
276 echo $file['contents'];
277 break;
278 case 'preview':
6b06a019
DO
279 if ($image = getFileCache ($file_id)) //Cache Hit
280 {
d3346ce2
DO
281 header("Content-type: image/jpeg");
282 echo $image;
283 break;
284 }
d3346ce2 285 //Cache Miss
43d0585d
DO
286 $file = getFile ($file_id);
287 $image = imagecreatefromstring ($file['contents']);
d3346ce2 288 unset ($file['contents']);
43d0585d
DO
289 $width = imagesx ($image);
290 $height = imagesy ($image);
d3346ce2 291 header ('Content-type: image/jpeg');
43d0585d
DO
292 if ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS'))
293 {
43d0585d
DO
294 $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
295 $newwidth = $width * $ratio;
296 $newheight = $height * $ratio;
297 $resampled = imagecreatetruecolor ($newwidth, $newheight);
298 imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
299 imagedestroy ($image);
300 $image = $resampled;
d3346ce2
DO
301
302 //TODO: Find a better way to save the stream of the image... Output buffer seems silly.
303 ob_start();
304 imagejpeg ($image);
305 commitAddFileCache ($file_id, ob_get_flush());
306 imagedestroy ($image);
307 unset ($file);
43d0585d
DO
308 unset ($resampled);
309 }
43d0585d
DO
310 break;
311 default:
6b06a019 312 renderError();
43d0585d 313 break;
37e59768 314 }
37e59768 315}
f537da2f 316
e673ee24 317?>