r4093 Finalise the work on issue #369 by means of new function HTTPDateToUnixTime(),
[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{
738363f6
DO
123 // A hardcoded value is worth of saving lots of code here.
124 $img = imagecreatefrompng ('pix/error.png');
e673ee24 125 header("Content-type: image/png");
da958e52
DO
126 imagepng ($img);
127 imagedestroy ($img);
128}
129
9c0b0016 130// Having a local caching array speeds things up. A little.
e673ee24
DO
131function colorFromHex ($image, $hex)
132{
9c0b0016
DO
133 static $colorcache = array ();
134 if (isset ($colorcache[$hex]))
135 return $colorcache[$hex];
e673ee24
DO
136 $r = hexdec ('0x' . substr ($hex, 0, 2));
137 $g = hexdec ('0x' . substr ($hex, 2, 2));
138 $b = hexdec ('0x' . substr ($hex, 4, 2));
9c0b0016
DO
139 $c = imagecolorallocate ($image, $r, $g, $b);
140 $colorcache[$hex] = $c;
141 return $c;
e673ee24
DO
142}
143
144function renderRackThumb ($rack_id = 0)
35ee7bd0
DO
145{
146 // Don't call DB extra times, hence we are most probably not the
147 // only script wishing to acces the same data now.
61a1d996
DO
148 if (NULL !== ($thumbcache = loadThumbCache ($rack_id)))
149 {
150 header("Content-type: image/png");
35ee7bd0 151 echo $thumbcache;
61a1d996
DO
152 return;
153 }
154 ob_start();
155 if (FALSE !== generateMiniRack ($rack_id))
35ee7bd0 156 {
61a1d996
DO
157 $capture = ob_get_clean();
158 header("Content-type: image/png");
159 echo $capture;
35ee7bd0 160 saveThumbCache ($rack_id, $capture);
61a1d996 161 return;
35ee7bd0 162 }
61a1d996
DO
163 // error text in the buffer
164 ob_end_flush();
35ee7bd0
DO
165}
166
61a1d996
DO
167// Output a binary string containing the PNG minirack. Indicate error with return code.
168function generateMiniRack ($rack_id)
e673ee24 169{
61a1d996
DO
170 if (NULL === ($rackData = spotEntity ('rack', $rack_id)))
171 return FALSE;
172 amplifyCell ($rackData);
e673ee24 173 markupObjectProblems ($rackData);
529eac25 174 global $rtwidth;
d5157018 175 $rtdepth = 9;
e673ee24
DO
176 $offset[0] = 3;
177 $offset[1] = 3 + $rtwidth[0];
178 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
179 $totalheight = 3 + 3 + $rackData['height'] * 2;
180 $totalwidth = $offset[2] + $rtwidth[2] + 3;
aa0e4c55
DO
181 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
182 or die("Cannot Initialize new GD image stream");
9c0b0016
DO
183 // cache our palette as well
184 $color = array();
185 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
d1adb55a 186 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
d5157018
DO
187 $color['black'] = colorFromHex ($img, '000000');
188 $color['gray'] = colorFromHex ($img, 'c0c0c0');
189 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
190 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
191 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
192 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
e673ee24
DO
193 {
194 for ($locidx = 0; $locidx < 3; $locidx++)
195 {
196 $colorcode = $rackData[$unit_no][$locidx]['state'];
197 if (isset ($rackData[$unit_no][$locidx]['hl']))
198 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
199 imagerectangle
200 (
201 $img,
202 $offset[$locidx],
203 3 + ($rackData['height'] - $unit_no) * 2,
204 $offset[$locidx] + $rtwidth[$locidx] - 1,
205 3 + ($rackData['height'] - $unit_no) * 2 + 1,
d1adb55a 206 $color[$colorcode]
e673ee24
DO
207 );
208 }
209 }
e673ee24
DO
210 imagepng ($img);
211 imagedestroy ($img);
61a1d996 212 return TRUE;
e673ee24
DO
213}
214
d15b2330
DO
215function renderProgressBarImage ($done)
216{
d1adb55a 217 $img = @imagecreatetruecolor (100, 10);
fec0c8da
DO
218 switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
219 {
220 case 'sparenetwork':
221 $color['T'] = colorFromHex ($img, '808080');
222 $color['F'] = colorFromHex ($img, 'c0c0c0');
223 break;
224 case 'rackspace': // teal
225 default:
226 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
227 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
228 }
d1adb55a
DO
229 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
230 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
074106cf
DO
231 for ($x = 20; $x <= 80; $x += 20)
232 {
233 $cc = $x > $done ? $color['T'] : $color['F'];
234 imagesetpixel ($img, $x, 0, $cc);
235 imagesetpixel ($img, $x, 1, $cc);
236 imagesetpixel ($img, $x, 4, $cc);
237 imagesetpixel ($img, $x, 5, $cc);
238 imagesetpixel ($img, $x, 8, $cc);
239 imagesetpixel ($img, $x, 9, $cc);
240 }
d15b2330
DO
241 header("Content-type: image/png");
242 imagepng ($img);
243 imagedestroy ($img);
d15b2330
DO
244}
245
2d75c30b
DO
246function renderAccessDeniedImage ()
247{
248 $img = @imagecreatetruecolor (1, 1);
249 imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
250 header("Content-type: image/png");
251 imagepng ($img);
252 imagedestroy ($img);
0df8c52b 253 die;
2d75c30b
DO
254}
255
43d0585d 256function renderFilePreview ($file_id = 0, $mode = 'view')
37e59768 257{
43d0585d 258 switch ($mode)
37e59768 259 {
43d0585d
DO
260 case 'view':
261 // GFX files can become really big, if we uncompress them in memory just to
262 // provide a PNG version of a file. To keep things working, just send the
263 // contents as is for known MIME types.
264 $file = getFile ($file_id);
265 if (!in_array ($file['type'], array ('image/jpeg', 'image/png', 'image/gif')))
266 {
08408472 267 showError ('Invalid MIME type on file', 'inline');
43d0585d
DO
268 break;
269 }
270 header("Content-type: ${file['type']}");
271 echo $file['contents'];
272 break;
273 case 'preview':
d3346ce2
DO
274 if($image = getFileCache($file_id)){ //Cache Hit
275 header("Content-type: image/jpeg");
276 echo $image;
277 break;
278 }
279
280 //Cache Miss
43d0585d
DO
281 $file = getFile ($file_id);
282 $image = imagecreatefromstring ($file['contents']);
d3346ce2 283 unset ($file['contents']);
43d0585d
DO
284 $width = imagesx ($image);
285 $height = imagesy ($image);
d3346ce2 286 header ('Content-type: image/jpeg');
43d0585d
DO
287 if ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS'))
288 {
43d0585d
DO
289 $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
290 $newwidth = $width * $ratio;
291 $newheight = $height * $ratio;
292 $resampled = imagecreatetruecolor ($newwidth, $newheight);
293 imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
294 imagedestroy ($image);
295 $image = $resampled;
d3346ce2
DO
296
297 //TODO: Find a better way to save the stream of the image... Output buffer seems silly.
298 ob_start();
299 imagejpeg ($image);
300 commitAddFileCache ($file_id, ob_get_flush());
301 imagedestroy ($image);
302 unset ($file);
43d0585d
DO
303 unset ($resampled);
304 }
43d0585d
DO
305 break;
306 default:
08408472 307 showError ('Invalid argument', 'inline');
43d0585d 308 break;
37e59768 309 }
37e59768 310}
f537da2f 311
e673ee24 312?>