r3888 Michael Tiernan: If anyone is interested, I modified the
[racktables] / render_image.php
CommitLineData
b325120a 1<?php
e673ee24 2
90a3d6d8
DY
3
4ob_start();
5try {
e673ee24
DO
6require 'inc/init.php';
7
0cc24e9a 8assertStringArg ('img');
d15b2330
DO
9switch ($_REQUEST['img'])
10{
da958e52 11 case 'minirack': // rack security context
0cc24e9a 12 assertUIntArg ('rack_id');
da958e52 13 $pageno = 'rack';
d15b2330 14 $tabno = 'default';
da958e52
DO
15 fixContext();
16 if (!permitted())
17 renderAccessDeniedImage();
18 else
19 renderRackThumb ($_REQUEST['rack_id']);
d15b2330 20 break;
da958e52 21 case 'progressbar': // no security context
0cc24e9a 22 assertUIntArg ('done', TRUE);
d15b2330
DO
23 renderProgressBarImage ($_REQUEST['done']);
24 break;
0df8c52b 25 case 'preview': // file security context
0cc24e9a 26 assertUIntArg ('file_id');
37e59768 27 $pageno = 'file';
0df8c52b 28 $tabno = 'download';
37e59768
DO
29 fixContext();
30 if (!permitted())
31 renderAccessDeniedImage();
32 else
43d0585d 33 renderFilePreview ($_REQUEST['file_id'], $_REQUEST['img']);
37e59768 34 break;
d15b2330
DO
35 default:
36 renderError();
37}
e673ee24 38
90a3d6d8
DY
39ob_end_flush();
40}
41catch (Exception $e)
42{
43 ob_end_clean();
3a089a44 44 renderError();
90a3d6d8
DY
45}
46
e673ee24
DO
47//------------------------------------------------------------------------
48function renderError ()
49{
738363f6
DO
50 // A hardcoded value is worth of saving lots of code here.
51 $img = imagecreatefrompng ('pix/error.png');
e673ee24 52 header("Content-type: image/png");
da958e52
DO
53 imagepng ($img);
54 imagedestroy ($img);
55}
56
9c0b0016 57// Having a local caching array speeds things up. A little.
e673ee24
DO
58function colorFromHex ($image, $hex)
59{
9c0b0016
DO
60 static $colorcache = array ();
61 if (isset ($colorcache[$hex]))
62 return $colorcache[$hex];
e673ee24
DO
63 $r = hexdec ('0x' . substr ($hex, 0, 2));
64 $g = hexdec ('0x' . substr ($hex, 2, 2));
65 $b = hexdec ('0x' . substr ($hex, 4, 2));
9c0b0016
DO
66 $c = imagecolorallocate ($image, $r, $g, $b);
67 $colorcache[$hex] = $c;
68 return $c;
e673ee24
DO
69}
70
71function renderRackThumb ($rack_id = 0)
35ee7bd0
DO
72{
73 // Don't call DB extra times, hence we are most probably not the
74 // only script wishing to acces the same data now.
61a1d996
DO
75 if (NULL !== ($thumbcache = loadThumbCache ($rack_id)))
76 {
77 header("Content-type: image/png");
35ee7bd0 78 echo $thumbcache;
61a1d996
DO
79 return;
80 }
81 ob_start();
82 if (FALSE !== generateMiniRack ($rack_id))
35ee7bd0 83 {
61a1d996
DO
84 $capture = ob_get_clean();
85 header("Content-type: image/png");
86 echo $capture;
35ee7bd0 87 saveThumbCache ($rack_id, $capture);
61a1d996 88 return;
35ee7bd0 89 }
61a1d996
DO
90 // error text in the buffer
91 ob_end_flush();
35ee7bd0
DO
92}
93
61a1d996
DO
94// Output a binary string containing the PNG minirack. Indicate error with return code.
95function generateMiniRack ($rack_id)
e673ee24 96{
61a1d996
DO
97 if (NULL === ($rackData = spotEntity ('rack', $rack_id)))
98 return FALSE;
99 amplifyCell ($rackData);
e673ee24 100 markupObjectProblems ($rackData);
529eac25 101 global $rtwidth;
d5157018 102 $rtdepth = 9;
e673ee24
DO
103 $offset[0] = 3;
104 $offset[1] = 3 + $rtwidth[0];
105 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
106 $totalheight = 3 + 3 + $rackData['height'] * 2;
107 $totalwidth = $offset[2] + $rtwidth[2] + 3;
aa0e4c55
DO
108 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
109 or die("Cannot Initialize new GD image stream");
9c0b0016
DO
110 // cache our palette as well
111 $color = array();
112 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
d1adb55a 113 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
d5157018
DO
114 $color['black'] = colorFromHex ($img, '000000');
115 $color['gray'] = colorFromHex ($img, 'c0c0c0');
116 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
117 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
118 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
119 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
e673ee24
DO
120 {
121 for ($locidx = 0; $locidx < 3; $locidx++)
122 {
123 $colorcode = $rackData[$unit_no][$locidx]['state'];
124 if (isset ($rackData[$unit_no][$locidx]['hl']))
125 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
126 imagerectangle
127 (
128 $img,
129 $offset[$locidx],
130 3 + ($rackData['height'] - $unit_no) * 2,
131 $offset[$locidx] + $rtwidth[$locidx] - 1,
132 3 + ($rackData['height'] - $unit_no) * 2 + 1,
d1adb55a 133 $color[$colorcode]
e673ee24
DO
134 );
135 }
136 }
e673ee24
DO
137 imagepng ($img);
138 imagedestroy ($img);
61a1d996 139 return TRUE;
e673ee24
DO
140}
141
d15b2330
DO
142function renderProgressBarImage ($done)
143{
d1adb55a 144 $img = @imagecreatetruecolor (100, 10);
fec0c8da
DO
145 switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
146 {
147 case 'sparenetwork':
148 $color['T'] = colorFromHex ($img, '808080');
149 $color['F'] = colorFromHex ($img, 'c0c0c0');
150 break;
151 case 'rackspace': // teal
152 default:
153 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
154 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
155 }
d1adb55a
DO
156 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
157 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
074106cf
DO
158 for ($x = 20; $x <= 80; $x += 20)
159 {
160 $cc = $x > $done ? $color['T'] : $color['F'];
161 imagesetpixel ($img, $x, 0, $cc);
162 imagesetpixel ($img, $x, 1, $cc);
163 imagesetpixel ($img, $x, 4, $cc);
164 imagesetpixel ($img, $x, 5, $cc);
165 imagesetpixel ($img, $x, 8, $cc);
166 imagesetpixel ($img, $x, 9, $cc);
167 }
d15b2330
DO
168 header("Content-type: image/png");
169 imagepng ($img);
170 imagedestroy ($img);
d15b2330
DO
171}
172
2d75c30b
DO
173function renderAccessDeniedImage ()
174{
175 $img = @imagecreatetruecolor (1, 1);
176 imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
177 header("Content-type: image/png");
178 imagepng ($img);
179 imagedestroy ($img);
0df8c52b 180 die;
2d75c30b
DO
181}
182
43d0585d 183function renderFilePreview ($file_id = 0, $mode = 'view')
37e59768 184{
43d0585d 185 switch ($mode)
37e59768 186 {
43d0585d
DO
187 case 'view':
188 // GFX files can become really big, if we uncompress them in memory just to
189 // provide a PNG version of a file. To keep things working, just send the
190 // contents as is for known MIME types.
191 $file = getFile ($file_id);
192 if (!in_array ($file['type'], array ('image/jpeg', 'image/png', 'image/gif')))
193 {
194 showError ('Invalid MIME type on file', __FUNCTION__);
195 break;
196 }
197 header("Content-type: ${file['type']}");
198 echo $file['contents'];
199 break;
200 case 'preview':
201 $file = getFile ($file_id);
202 $image = imagecreatefromstring ($file['contents']);
203 unset ($file);
204 $width = imagesx ($image);
205 $height = imagesy ($image);
206 if ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS'))
207 {
208 // TODO: cache thumbs for faster page generation
209 $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
210 $newwidth = $width * $ratio;
211 $newheight = $height * $ratio;
212 $resampled = imagecreatetruecolor ($newwidth, $newheight);
213 imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
214 imagedestroy ($image);
215 $image = $resampled;
216 unset ($resampled);
217 }
218 header("Content-type: image/png"); // don't announce content-length, it may have changed after resampling
219 imagepng ($image);
37e59768 220 imagedestroy ($image);
43d0585d
DO
221 break;
222 default:
223 showError ('Invalid argument', __FUNCTION__);
224 break;
37e59768 225 }
37e59768 226}
f537da2f 227
e673ee24 228?>