c3703eb02cb300929fa936ef588cad023fd841ce
[racktables] / wwwroot / inc / render_image.php
1 <?php
2
3 function dispatchImageRequest()
4 {
5 genericAssertion ('img', 'string');
6 global $pageno, $tabno;
7 switch ($_REQUEST['img'])
8 {
9 case 'minirack': // rack security context
10 $pageno = 'rack';
11 $tabno = 'default';
12 fixContext();
13 if (!permitted())
14 renderAccessDeniedImage();
15 else
16 renderRackThumb (getBypassValue());
17 break;
18 case 'progressbar': // no security context
19 assertUIntArg ('done', TRUE);
20 // 'progressbar's never change, make browser cache the result
21 renderProgressBarImage ($_REQUEST['done']);
22 break;
23 case 'preview': // file security context
24 $pageno = 'file';
25 $tabno = 'download';
26 fixContext();
27 if (!permitted())
28 renderAccessDeniedImage();
29 else
30 renderFilePreview (getBypassValue());
31 break;
32 default:
33 renderErrorImage();
34 }
35 }
36
37 function renderErrorImage ()
38 {
39 header("Content-type: image/png");
40 // "ERROR", 76x17, red on white
41 echo base64_decode
42 (
43 'iVBORw0KGgoAAAANSUhEUgAAAEwAAAARCAYAAAB3h0oCAAAAAXNSR0IArs4c6QAAALBJREFUWMPt' .
44 'WFsOwCAIG4v3vzL7WEyWxQdVwM1A4l/F2iHVETPzESGOMyTAInURRP0suUhb2FIho/jWXO38w4KN' .
45 'LPDGEt2jlgPBZxFKc2o8UT7Lj6SkAmfw1nx+28MkVWQlcjT9EOwjLqnpaNImi+I1j/YSl5RY/gx+' .
46 'VCCF/MnkCz4JZQtvEUXx1nyW9jCUlPVLbTJ/3MO2dsnWRq2Nwl2wTarM51rhsVEnDhT/w7C4APaJ' .
47 'ZhkIGYaUAAAAAElFTkSuQmCC'
48 );
49 }
50
51 // Having a local caching array speeds things up. A little.
52 function colorFromHex ($image, $hex)
53 {
54 static $colorcache = array ();
55 if (isset ($colorcache[$hex]))
56 return $colorcache[$hex];
57 $r = hexdec ('0x' . substr ($hex, 0, 2));
58 $g = hexdec ('0x' . substr ($hex, 2, 2));
59 $b = hexdec ('0x' . substr ($hex, 4, 2));
60 $c = imagecolorallocate ($image, $r, $g, $b);
61 $colorcache[$hex] = $c;
62 return $c;
63 }
64
65 function renderRackThumb ($rack_id = 0)
66 {
67 // Don't call DB extra times, hence we are most probably not the
68 // only script wishing to access the same data now.
69 if (NULL !== ($thumbcache = loadThumbCache ($rack_id)))
70 {
71 header("Content-type: image/png");
72 echo $thumbcache;
73 return;
74 }
75 ob_start();
76 if (FALSE !== generateMiniRack ($rack_id))
77 {
78 $capture = ob_get_clean();
79 header("Content-type: image/png");
80 echo $capture;
81 usePreparedUpdateBlade ('Rack', array ('thumb_data' => base64_encode ($capture)), array ('id' => $rack_id));
82 return;
83 }
84 // error text in the buffer
85 ob_end_flush();
86 }
87
88 // Output a binary string containing the PNG minirack. Indicate error with return code.
89 function generateMiniRack ($rack_id)
90 {
91 if (NULL === ($rackData = spotEntity ('rack', $rack_id)))
92 return FALSE;
93 amplifyCell ($rackData);
94 markupObjectProblems ($rackData);
95 global $rtwidth;
96 $rtdepth = 9;
97 $offset[0] = 3;
98 $offset[1] = 3 + $rtwidth[0];
99 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
100 $totalheight = 3 + 3 + $rackData['height'] * 2;
101 $totalwidth = $offset[2] + $rtwidth[2] + 3;
102 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
103 or die("Cannot Initialize new GD image stream");
104 // cache our palette as well
105 $color = array();
106 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
107 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
108 $color['black'] = colorFromHex ($img, '000000');
109 $color['gray'] = colorFromHex ($img, 'c0c0c0');
110 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
111 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
112 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
113 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
114 {
115 for ($locidx = 0; $locidx < 3; $locidx++)
116 {
117 $colorcode = $rackData[$unit_no][$locidx]['state'];
118 if (isset ($rackData[$unit_no][$locidx]['hl']))
119 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
120 imagerectangle
121 (
122 $img,
123 $offset[$locidx],
124 3 + ($rackData['height'] - $unit_no) * 2,
125 $offset[$locidx] + $rtwidth[$locidx] - 1,
126 3 + ($rackData['height'] - $unit_no) * 2 + 1,
127 $color[$colorcode]
128 );
129 }
130 }
131 imagepng ($img);
132 imagedestroy ($img);
133 return TRUE;
134 }
135
136 function renderProgressBarImage ($done)
137 {
138 $img = @imagecreatetruecolor (100, 10);
139 switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
140 {
141 case 'sparenetwork':
142 $color['T'] = colorFromHex ($img, '808080');
143 $color['F'] = colorFromHex ($img, 'c0c0c0');
144 break;
145 case 'rackspace': // teal
146 default:
147 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
148 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
149 }
150 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
151 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
152 for ($x = 20; $x <= 80; $x += 20)
153 {
154 $cc = $x > $done ? $color['T'] : $color['F'];
155 imagesetpixel ($img, $x, 0, $cc);
156 imagesetpixel ($img, $x, 1, $cc);
157 imagesetpixel ($img, $x, 4, $cc);
158 imagesetpixel ($img, $x, 5, $cc);
159 imagesetpixel ($img, $x, 8, $cc);
160 imagesetpixel ($img, $x, 9, $cc);
161 }
162 header("Content-type: image/png");
163 imagepng ($img);
164 imagedestroy ($img);
165 }
166
167 function renderAccessDeniedImage ()
168 {
169 $img = @imagecreatetruecolor (1, 1);
170 imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
171 header("Content-type: image/png");
172 imagepng ($img);
173 imagedestroy ($img);
174 die;
175 }
176
177 function renderFilePreview ($file_id)
178 {
179 if ($image = getFileCache ($file_id)) //Cache Hit
180 {
181 header("Content-type: image/jpeg");
182 echo $image;
183 return;
184 }
185 //Cache Miss
186 $file = getFile ($file_id);
187 $image = imagecreatefromstring ($file['contents']);
188 unset ($file);
189 $width = imagesx ($image);
190 $height = imagesy ($image);
191 if ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS'))
192 {
193 $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
194 $newwidth = $width * $ratio;
195 $newheight = $height * $ratio;
196 $resampled = imagecreatetruecolor ($newwidth, $newheight);
197 imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
198 imagedestroy ($image);
199 $image = $resampled;
200 unset ($resampled);
201 }
202 header ('Content-type: image/jpeg');
203 ob_start();
204 imagejpeg ($image);
205 imagedestroy ($image);
206 commitAddFileCache ($file_id, ob_get_flush());
207 }
208
209 ?>