r4093 Finalise the work on issue #369 by means of new function HTTPDateToUnixTime(),
[racktables] / 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 $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
121 function renderError ()
122 {
123 // A hardcoded value is worth of saving lots of code here.
124 $img = imagecreatefrompng ('pix/error.png');
125 header("Content-type: image/png");
126 imagepng ($img);
127 imagedestroy ($img);
128 }
129
130 // Having a local caching array speeds things up. A little.
131 function colorFromHex ($image, $hex)
132 {
133 static $colorcache = array ();
134 if (isset ($colorcache[$hex]))
135 return $colorcache[$hex];
136 $r = hexdec ('0x' . substr ($hex, 0, 2));
137 $g = hexdec ('0x' . substr ($hex, 2, 2));
138 $b = hexdec ('0x' . substr ($hex, 4, 2));
139 $c = imagecolorallocate ($image, $r, $g, $b);
140 $colorcache[$hex] = $c;
141 return $c;
142 }
143
144 function renderRackThumb ($rack_id = 0)
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.
148 if (NULL !== ($thumbcache = loadThumbCache ($rack_id)))
149 {
150 header("Content-type: image/png");
151 echo $thumbcache;
152 return;
153 }
154 ob_start();
155 if (FALSE !== generateMiniRack ($rack_id))
156 {
157 $capture = ob_get_clean();
158 header("Content-type: image/png");
159 echo $capture;
160 saveThumbCache ($rack_id, $capture);
161 return;
162 }
163 // error text in the buffer
164 ob_end_flush();
165 }
166
167 // Output a binary string containing the PNG minirack. Indicate error with return code.
168 function generateMiniRack ($rack_id)
169 {
170 if (NULL === ($rackData = spotEntity ('rack', $rack_id)))
171 return FALSE;
172 amplifyCell ($rackData);
173 markupObjectProblems ($rackData);
174 global $rtwidth;
175 $rtdepth = 9;
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;
181 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
182 or die("Cannot Initialize new GD image stream");
183 // cache our palette as well
184 $color = array();
185 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
186 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
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++)
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,
206 $color[$colorcode]
207 );
208 }
209 }
210 imagepng ($img);
211 imagedestroy ($img);
212 return TRUE;
213 }
214
215 function renderProgressBarImage ($done)
216 {
217 $img = @imagecreatetruecolor (100, 10);
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 }
229 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
230 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
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 }
241 header("Content-type: image/png");
242 imagepng ($img);
243 imagedestroy ($img);
244 }
245
246 function 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);
253 die;
254 }
255
256 function renderFilePreview ($file_id = 0, $mode = 'view')
257 {
258 switch ($mode)
259 {
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 {
267 showError ('Invalid MIME type on file', 'inline');
268 break;
269 }
270 header("Content-type: ${file['type']}");
271 echo $file['contents'];
272 break;
273 case 'preview':
274 if($image = getFileCache($file_id)){ //Cache Hit
275 header("Content-type: image/jpeg");
276 echo $image;
277 break;
278 }
279
280 //Cache Miss
281 $file = getFile ($file_id);
282 $image = imagecreatefromstring ($file['contents']);
283 unset ($file['contents']);
284 $width = imagesx ($image);
285 $height = imagesy ($image);
286 header ('Content-type: image/jpeg');
287 if ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS'))
288 {
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;
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);
303 unset ($resampled);
304 }
305 break;
306 default:
307 showError ('Invalid argument', 'inline');
308 break;
309 }
310 }
311
312 ?>