r2373 fix invalid link, rename vars for consistency
[racktables] / render_image.php
CommitLineData
b325120a 1<?php
e673ee24 2
e673ee24
DO
3require 'inc/init.php';
4
37e59768 5assertStringArg ('img', __FILE__);
d15b2330
DO
6switch ($_REQUEST['img'])
7{
da958e52 8 case 'minirack': // rack security context
37e59768 9 assertUIntArg ('rack_id', __FILE__);
da958e52 10 $pageno = 'rack';
d15b2330 11 $tabno = 'default';
da958e52
DO
12 fixContext();
13 if (!permitted())
14 renderAccessDeniedImage();
15 else
16 renderRackThumb ($_REQUEST['rack_id']);
d15b2330 17 break;
da958e52 18 case 'progressbar': // no security context
37e59768 19 assertUIntArg ('done', __FILE__, TRUE);
d15b2330
DO
20 renderProgressBarImage ($_REQUEST['done']);
21 break;
37e59768
DO
22 case 'view': // file security context
23 case 'preview':
24 assertUIntArg ('file_id', __FILE__);
25 $pageno = 'file';
26 $tabno = 'default';
27 fixContext();
28 if (!permitted())
29 renderAccessDeniedImage();
30 else
31 renderFilePreview ($_REQUEST['file_id']);
32 break;
d15b2330
DO
33 default:
34 renderError();
35}
e673ee24
DO
36
37//------------------------------------------------------------------------
38function renderError ()
39{
738363f6
DO
40 // A hardcoded value is worth of saving lots of code here.
41 $img = imagecreatefrompng ('pix/error.png');
e673ee24 42 header("Content-type: image/png");
da958e52
DO
43 imagepng ($img);
44 imagedestroy ($img);
45}
46
9c0b0016 47// Having a local caching array speeds things up. A little.
e673ee24
DO
48function colorFromHex ($image, $hex)
49{
9c0b0016
DO
50 static $colorcache = array ();
51 if (isset ($colorcache[$hex]))
52 return $colorcache[$hex];
e673ee24
DO
53 $r = hexdec ('0x' . substr ($hex, 0, 2));
54 $g = hexdec ('0x' . substr ($hex, 2, 2));
55 $b = hexdec ('0x' . substr ($hex, 4, 2));
9c0b0016
DO
56 $c = imagecolorallocate ($image, $r, $g, $b);
57 $colorcache[$hex] = $c;
58 return $c;
e673ee24
DO
59}
60
61function renderRackThumb ($rack_id = 0)
35ee7bd0
DO
62{
63 // Don't call DB extra times, hence we are most probably not the
64 // only script wishing to acces the same data now.
65 header("Content-type: image/png");
66 $thumbcache = loadThumbCache ($rack_id);
67 if ($thumbcache !== NULL)
68 echo $thumbcache;
69 else
70 {
71 ob_start();
72 generateMiniRack ($rack_id);
73 $capture = ob_get_contents();
74 ob_end_flush();
75 saveThumbCache ($rack_id, $capture);
76 }
77}
78
6874300e 79// Output a binary string containing the PNG minirack.
d5157018 80function generateMiniRack ($rack_id = 0)
e673ee24
DO
81{
82 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
83 {
84 renderError();
85 return;
86 }
87 markupObjectProblems ($rackData);
9c0b0016
DO
88 // Cache in a local array, because we are going to use those values often.
89 $rtwidth = array
90 (
91 0 => getConfigVar ('rtwidth_0'),
92 1 => getConfigVar ('rtwidth_1'),
93 2 => getConfigVar ('rtwidth_2')
94 );
d5157018 95 $rtdepth = 9;
e673ee24
DO
96 $offset[0] = 3;
97 $offset[1] = 3 + $rtwidth[0];
98 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
99 $totalheight = 3 + 3 + $rackData['height'] * 2;
100 $totalwidth = $offset[2] + $rtwidth[2] + 3;
aa0e4c55
DO
101 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
102 or die("Cannot Initialize new GD image stream");
9c0b0016
DO
103 // cache our palette as well
104 $color = array();
105 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
d1adb55a 106 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
d5157018
DO
107 $color['black'] = colorFromHex ($img, '000000');
108 $color['gray'] = colorFromHex ($img, 'c0c0c0');
109 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
110 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
111 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
112 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
e673ee24
DO
113 {
114 for ($locidx = 0; $locidx < 3; $locidx++)
115 {
116 $colorcode = $rackData[$unit_no][$locidx]['state'];
117 if (isset ($rackData[$unit_no][$locidx]['hl']))
118 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
119 imagerectangle
120 (
121 $img,
122 $offset[$locidx],
123 3 + ($rackData['height'] - $unit_no) * 2,
124 $offset[$locidx] + $rtwidth[$locidx] - 1,
125 3 + ($rackData['height'] - $unit_no) * 2 + 1,
d1adb55a 126 $color[$colorcode]
e673ee24
DO
127 );
128 }
129 }
e673ee24
DO
130 imagepng ($img);
131 imagedestroy ($img);
132}
133
d15b2330
DO
134function renderProgressBarImage ($done)
135{
d1adb55a 136 $img = @imagecreatetruecolor (100, 10);
fec0c8da
DO
137 switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
138 {
139 case 'sparenetwork':
140 $color['T'] = colorFromHex ($img, '808080');
141 $color['F'] = colorFromHex ($img, 'c0c0c0');
142 break;
143 case 'rackspace': // teal
144 default:
145 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
146 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
147 }
d1adb55a
DO
148 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
149 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
d15b2330
DO
150 header("Content-type: image/png");
151 imagepng ($img);
152 imagedestroy ($img);
d15b2330
DO
153}
154
2d75c30b
DO
155function renderAccessDeniedImage ()
156{
157 $img = @imagecreatetruecolor (1, 1);
158 imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
159 header("Content-type: image/png");
160 imagepng ($img);
161 imagedestroy ($img);
162}
163
37e59768
DO
164function renderFilePreview ($file_id = 0)
165{
166 $file = getFile ($file_id);
167 $image = imagecreatefromstring ($file['contents']);
168 $width = imagesx ($image);
169 $height = imagesy ($image);
170 if ($_REQUEST['img'] == 'preview' and ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS')))
171 {
172 // TODO: cache thumbs for faster page generation
173 $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
174 $newwidth = $width * $ratio;
175 $newheight = $height * $ratio;
176 $resampled = imagecreatetruecolor ($newwidth, $newheight);
177 imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
178 imagedestroy ($image);
179 $image = $resampled;
180 unset ($resampled);
181 }
182 header("Content-type: image/png"); // don't announce content-length, it may have changed after resampling
183 imagepng ($image);
184 imagedestroy ($image);
185}
186
e673ee24 187?>