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