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