r1980 + lots of adjustments to fit the new RackCode authorization framework
[racktables] / render_image.php
1 <?php
2
3 require 'inc/init.php';
4
5 assertStringArg ('img', 'render_image');
6 switch ($_REQUEST['img'])
7 {
8 case 'minirack': // rack security context
9 assertUIntArg ('rack_id', 'render_image');
10 $pageno = 'rack';
11 $tabno = 'default';
12 fixContext();
13 if (!permitted())
14 renderAccessDeniedImage();
15 else
16 renderRackThumb ($_REQUEST['rack_id']);
17 break;
18 case 'progressbar': // no security context
19 assertUIntArg ('done', 'render_image', TRUE);
20 renderProgressBarImage ($_REQUEST['done']);
21 break;
22 default:
23 renderError();
24 }
25
26 //------------------------------------------------------------------------
27 function renderError ()
28 {
29 // A hardcoded value is worth of saving lots of code here.
30 $img = imagecreatefrompng ('pix/error.png');
31 header("Content-type: image/png");
32 imagepng ($img);
33 imagedestroy ($img);
34 }
35
36 function renderAccessDeniedImage ()
37 {
38 $img = imagecreatefrompng ('pix/pixel.png');
39 header("Content-type: image/png");
40 imagepng ($img);
41 imagedestroy ($img);
42 }
43
44 // Having a local caching array speeds things up. A little.
45 function colorFromHex ($image, $hex)
46 {
47 static $colorcache = array ();
48 if (isset ($colorcache[$hex]))
49 return $colorcache[$hex];
50 $r = hexdec ('0x' . substr ($hex, 0, 2));
51 $g = hexdec ('0x' . substr ($hex, 2, 2));
52 $b = hexdec ('0x' . substr ($hex, 4, 2));
53 $c = imagecolorallocate ($image, $r, $g, $b);
54 $colorcache[$hex] = $c;
55 return $c;
56 }
57
58 function renderRackThumb ($rack_id = 0)
59 {
60 // Don't call DB extra times, hence we are most probably not the
61 // only script wishing to acces the same data now.
62 header("Content-type: image/png");
63 $thumbcache = loadThumbCache ($rack_id);
64 if ($thumbcache !== NULL)
65 echo $thumbcache;
66 else
67 {
68 ob_start();
69 generateMiniRack ($rack_id);
70 $capture = ob_get_contents();
71 ob_end_flush();
72 saveThumbCache ($rack_id, $capture);
73 }
74 }
75
76 // Output a binary string containing the PNG minirack.
77 function generateMiniRack ($rack_id)
78 {
79 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
80 {
81 renderError();
82 return;
83 }
84 markupObjectProblems ($rackData);
85 // Cache in a local array, because we are going to use those values often.
86 $rtwidth = array
87 (
88 0 => getConfigVar ('rtwidth_0'),
89 1 => getConfigVar ('rtwidth_1'),
90 2 => getConfigVar ('rtwidth_2')
91 );
92 $offset[0] = 3;
93 $offset[1] = 3 + $rtwidth[0];
94 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
95 $totalheight = 3 + 3 + $rackData['height'] * 2;
96 $totalwidth = $offset[2] + $rtwidth[2] + 3;
97 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
98 or die("Cannot Initialize new GD image stream");
99 // cache our palette as well
100 $color = array();
101 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
102 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
103 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, colorFromHex ($img, '000000'));
104 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, colorFromHex ($img, 'c0c0c0'));
105 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, colorFromHex ($img, '000000'));
106 for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--)
107 {
108 for ($locidx = 0; $locidx < 3; $locidx++)
109 {
110 $colorcode = $rackData[$unit_no][$locidx]['state'];
111 if (isset ($rackData[$unit_no][$locidx]['hl']))
112 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
113 imagerectangle
114 (
115 $img,
116 $offset[$locidx],
117 3 + ($rackData['height'] - $unit_no) * 2,
118 $offset[$locidx] + $rtwidth[$locidx] - 1,
119 3 + ($rackData['height'] - $unit_no) * 2 + 1,
120 $color[$colorcode]
121 );
122 }
123 }
124 imagepng ($img);
125 imagedestroy ($img);
126 }
127
128 function renderProgressBarImage ($done)
129 {
130 $img = @imagecreatetruecolor (100, 10);
131 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
132 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
133 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
134 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
135 header("Content-type: image/png");
136 imagepng ($img);
137 imagedestroy ($img);
138 }
139
140 ?>