r2221 - introduce and use IPV4_JAYWALK option
[racktables] / render_image.php
CommitLineData
b325120a 1<?php
e673ee24 2
e673ee24
DO
3require 'inc/init.php';
4
da958e52 5assertStringArg ('img', 'render_image');
d15b2330
DO
6switch ($_REQUEST['img'])
7{
da958e52
DO
8 case 'minirack': // rack security context
9 assertUIntArg ('rack_id', 'render_image');
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
DO
18 case 'progressbar': // no security context
19 assertUIntArg ('done', 'render_image', TRUE);
d15b2330
DO
20 renderProgressBarImage ($_REQUEST['done']);
21 break;
22 default:
23 renderError();
24}
e673ee24
DO
25
26//------------------------------------------------------------------------
27function renderError ()
28{
738363f6
DO
29 // A hardcoded value is worth of saving lots of code here.
30 $img = imagecreatefrompng ('pix/error.png');
e673ee24 31 header("Content-type: image/png");
da958e52
DO
32 imagepng ($img);
33 imagedestroy ($img);
34}
35
36function renderAccessDeniedImage ()
37{
38 $img = imagecreatefrompng ('pix/pixel.png');
39 header("Content-type: image/png");
e673ee24
DO
40 imagepng ($img);
41 imagedestroy ($img);
42}
43
9c0b0016 44// Having a local caching array speeds things up. A little.
e673ee24
DO
45function colorFromHex ($image, $hex)
46{
9c0b0016
DO
47 static $colorcache = array ();
48 if (isset ($colorcache[$hex]))
49 return $colorcache[$hex];
e673ee24
DO
50 $r = hexdec ('0x' . substr ($hex, 0, 2));
51 $g = hexdec ('0x' . substr ($hex, 2, 2));
52 $b = hexdec ('0x' . substr ($hex, 4, 2));
9c0b0016
DO
53 $c = imagecolorallocate ($image, $r, $g, $b);
54 $colorcache[$hex] = $c;
55 return $c;
e673ee24
DO
56}
57
58function renderRackThumb ($rack_id = 0)
35ee7bd0
DO
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
6874300e 76// Output a binary string containing the PNG minirack.
d5157018 77function generateMiniRack ($rack_id = 0)
e673ee24
DO
78{
79 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
80 {
81 renderError();
82 return;
83 }
84 markupObjectProblems ($rackData);
9c0b0016
DO
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 );
d5157018 92 $rtdepth = 9;
e673ee24
DO
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;
aa0e4c55
DO
98 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
99 or die("Cannot Initialize new GD image stream");
9c0b0016
DO
100 // cache our palette as well
101 $color = array();
102 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
d1adb55a 103 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
d5157018
DO
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++)
e673ee24
DO
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,
d1adb55a 123 $color[$colorcode]
e673ee24
DO
124 );
125 }
126 }
e673ee24
DO
127 imagepng ($img);
128 imagedestroy ($img);
129}
130
d15b2330
DO
131function renderProgressBarImage ($done)
132{
d1adb55a 133 $img = @imagecreatetruecolor (100, 10);
fec0c8da
DO
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 }
d1adb55a
DO
145 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
146 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
d15b2330
DO
147 header("Content-type: image/png");
148 imagepng ($img);
149 imagedestroy ($img);
d15b2330
DO
150}
151
e673ee24 152?>