r2207 - tree collapse/expand ops seem to work
[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 = 0)
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 $rtdepth = 9;
93 $offset[0] = 3;
94 $offset[1] = 3 + $rtwidth[0];
95 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
96 $totalheight = 3 + 3 + $rackData['height'] * 2;
97 $totalwidth = $offset[2] + $rtwidth[2] + 3;
98 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
99 or die("Cannot Initialize new GD image stream");
100 // cache our palette as well
101 $color = array();
102 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
103 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
104 $color['black'] = colorFromHex ($img, '000000');
105 $color['gray'] = colorFromHex ($img, 'c0c0c0');
106 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
107 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
108 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
109 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
110 {
111 for ($locidx = 0; $locidx < 3; $locidx++)
112 {
113 $colorcode = $rackData[$unit_no][$locidx]['state'];
114 if (isset ($rackData[$unit_no][$locidx]['hl']))
115 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
116 imagerectangle
117 (
118 $img,
119 $offset[$locidx],
120 3 + ($rackData['height'] - $unit_no) * 2,
121 $offset[$locidx] + $rtwidth[$locidx] - 1,
122 3 + ($rackData['height'] - $unit_no) * 2 + 1,
123 $color[$colorcode]
124 );
125 }
126 }
127 imagepng ($img);
128 imagedestroy ($img);
129 }
130
131 function renderProgressBarImage ($done)
132 {
133 $img = @imagecreatetruecolor (100, 10);
134 switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
135 {
136 case 'sparenetwork':
137 $color['T'] = colorFromHex ($img, '808080');
138 $color['F'] = colorFromHex ($img, 'c0c0c0');
139 break;
140 case 'rackspace': // teal
141 default:
142 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
143 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
144 }
145 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
146 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
147 header("Content-type: image/png");
148 imagepng ($img);
149 imagedestroy ($img);
150 }
151
152 ?>