r3044 - fix ALTER TABLE
[racktables] / render_image.php
CommitLineData
b325120a 1<?php
e673ee24 2
90a3d6d8
DY
3
4ob_start();
5try {
e673ee24
DO
6require 'inc/init.php';
7
37e59768 8assertStringArg ('img', __FILE__);
d15b2330
DO
9switch ($_REQUEST['img'])
10{
da958e52 11 case 'minirack': // rack security context
37e59768 12 assertUIntArg ('rack_id', __FILE__);
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
37e59768 22 assertUIntArg ('done', __FILE__, TRUE);
d15b2330
DO
23 renderProgressBarImage ($_REQUEST['done']);
24 break;
0df8c52b 25 case 'preview': // file security context
37e59768
DO
26 assertUIntArg ('file_id', __FILE__);
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']);
d15b2330
DO
160 header("Content-type: image/png");
161 imagepng ($img);
162 imagedestroy ($img);
d15b2330
DO
163}
164
2d75c30b
DO
165function renderAccessDeniedImage ()
166{
167 $img = @imagecreatetruecolor (1, 1);
168 imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
169 header("Content-type: image/png");
170 imagepng ($img);
171 imagedestroy ($img);
0df8c52b 172 die;
2d75c30b
DO
173}
174
43d0585d 175function renderFilePreview ($file_id = 0, $mode = 'view')
37e59768 176{
43d0585d 177 switch ($mode)
37e59768 178 {
43d0585d
DO
179 case 'view':
180 // GFX files can become really big, if we uncompress them in memory just to
181 // provide a PNG version of a file. To keep things working, just send the
182 // contents as is for known MIME types.
183 $file = getFile ($file_id);
184 if (!in_array ($file['type'], array ('image/jpeg', 'image/png', 'image/gif')))
185 {
186 showError ('Invalid MIME type on file', __FUNCTION__);
187 break;
188 }
189 header("Content-type: ${file['type']}");
190 echo $file['contents'];
191 break;
192 case 'preview':
193 $file = getFile ($file_id);
194 $image = imagecreatefromstring ($file['contents']);
195 unset ($file);
196 $width = imagesx ($image);
197 $height = imagesy ($image);
198 if ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS'))
199 {
200 // TODO: cache thumbs for faster page generation
201 $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
202 $newwidth = $width * $ratio;
203 $newheight = $height * $ratio;
204 $resampled = imagecreatetruecolor ($newwidth, $newheight);
205 imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
206 imagedestroy ($image);
207 $image = $resampled;
208 unset ($resampled);
209 }
210 header("Content-type: image/png"); // don't announce content-length, it may have changed after resampling
211 imagepng ($image);
37e59768 212 imagedestroy ($image);
43d0585d
DO
213 break;
214 default:
215 showError ('Invalid argument', __FUNCTION__);
216 break;
37e59768 217 }
37e59768 218}
f537da2f 219
e673ee24 220?>