class_graph.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. // $Id: class_graph.inc,v 1.7 2002/11/29 10:45:46 dijkstra Exp $
  3. //
  4. // Base class for all graph classes.
  5. class Graph {
  6. var $debug;
  7. var $graphdefaults;
  8. var $graphsettings;
  9. var $options;
  10. var $shortcut;
  11. function Graph() {
  12. $this->init();
  13. }
  14. function enable_debug() {
  15. $this->debug = 1;
  16. }
  17. function debug($string) {
  18. if ($this->debug) {
  19. print "<hr><br>$string<br><hr>";
  20. }
  21. }
  22. function dump_vars() {
  23. print "<br><hr><br><b>graphsettings</b>:<br><pre>";
  24. print_r($this->graphsettings);
  25. print "</pre><br><hr><br><b>options</b>:<br><pre>";
  26. print_r($this->options);
  27. print "</pre><br><hr><br>";
  28. }
  29. // fill meta parameters
  30. // this function will most likely not be overloaded by child classes
  31. function init() {
  32. $this->debug = 0;
  33. $this->graphdefaults = array(
  34. "width" => array("max" => 2048, "min" => 32, "default" => 300),
  35. "height" => array("max" => 2048, "min" => 32, "default" => 225),
  36. "start" => array("max" => 429467296, "min" => -4294967296, "default" => -86400),
  37. "end" => array("max" => 429467296, "min" => -4294967296, "default" => -1),
  38. "imagetype" => "PNG",
  39. "rrdtool" => "/usr/local/bin/rrdtool graph -",
  40. "rrdfile" => "",
  41. "name" => "unnamed graph");
  42. $this->options = array(
  43. "width" => "-w",
  44. "height" => "-h",
  45. "imagetype" => "-a",
  46. "start" => "-s",
  47. "end" => "-e" );
  48. $this->shortcut = array(
  49. // date shortcuts
  50. "timespan" => array(
  51. "today" => array(
  52. "start" => -86400 ),
  53. "lastweek" => array(
  54. "start" => -604800 ),
  55. "lastmonth" => array(
  56. "start" => -2419200 )),
  57. // size shortcuts
  58. "size" => array(
  59. "tiny" => array(
  60. "width" => 100,
  61. "height" => 75 ),
  62. "small" => array(
  63. "width" => 200,
  64. "height" => 125 ),
  65. "normal" => array(
  66. "width" => 300,
  67. "height" => 225 ),
  68. "big" => array(
  69. "width" => 600,
  70. "height" => 450 ),
  71. "huge" => array(
  72. "width" => 1024,
  73. "height" => 640 )));
  74. $this->graphsettings = array(
  75. "graphdefinition" => array(),
  76. "dsdefinition" => array(),
  77. "rawcmdline" => "");
  78. }
  79. // parseoptions will not be overloaded by child classes
  80. function parseoptions (&$params) {
  81. $myparams = array();
  82. // look for shortcuts
  83. if (!empty($params) && is_array($params)) {
  84. reset($params);
  85. while (list($key, $value) = each($params)) {
  86. if (isset($this->shortcut[$key][$value])) {
  87. $ar = $this->shortcut[$key][$value];
  88. while (list($itemkey,$itemvalue) = each($ar)) {
  89. if (is_array($this->shortcut[$key][$value][$itemkey])) {
  90. $myparams[$itemkey] = array_merge(array(),$this->shortcut[$key][$value][$itemkey]);
  91. } else if (is_scalar($itemvalue)) {
  92. $myparams[$itemkey] = $itemvalue;
  93. }
  94. }
  95. } else {
  96. $myparams[$key] = $value;
  97. }
  98. }
  99. }
  100. // copy parameters, filling in defaults and constraining to acceptable
  101. // values where necessary
  102. reset($this->graphdefaults);
  103. reset($myparams);
  104. while (list($key, $value) = each($this->graphdefaults)) {
  105. if (isset($myparams[$key])) {
  106. if (is_array($myparams[$key])) {
  107. $value = $myparams[$key];
  108. } else {
  109. $value = escapeshellcmd($myparams[$key]);
  110. }
  111. } else {
  112. if (isset($this->graphsettings[$key])) {
  113. // is this set already, is current value ..
  114. if ((is_array($this->graphsettings[$key]) &&
  115. count($this->graphsettings[$key]) != 0) // an non empty array?
  116. || (is_scalar($this->graphsettings[$key]))) // or a scalar
  117. {
  118. // yes: keep current set value
  119. $value = $this->graphsettings[$key];
  120. }
  121. // no: implicit; set to default
  122. } else if (is_array($value) && isset($value["default"])) {
  123. // - check if it is an min/max/default array
  124. $value = $value["default"];
  125. }
  126. }
  127. // normalize parameters
  128. if (is_array($this->graphdefaults[$key])
  129. && isset($this->graphdefaults[$key]["max"])
  130. && $value > $this->graphdefaults[$key]["max"]) {
  131. $value = $this->graphdefaults[$key]["max"];
  132. }
  133. if (is_array($this->graphdefaults[$key])
  134. && isset($this->graphdefaults[$key]["min"])
  135. && $value < $this->graphdefaults[$key]["min"]) {
  136. $value = $this->graphdefaults[$key]["min"];
  137. }
  138. $this->graphsettings[$key] = $value;
  139. }
  140. }
  141. // will not be overloaded by child classes
  142. function cmdline() {
  143. $cmdline = $this->graphsettings["rrdtool"];
  144. // get options
  145. reset($this->options);
  146. while (list($key, $value) = each($this->options)) {
  147. if (isset($this->graphsettings[$key])) {
  148. $cmdline .= ' '.$value.' '.$this->graphsettings[$key];
  149. }
  150. }
  151. $cmdline .= $this->graphsettings["rawcmdline"];
  152. while (list($key, $value) = each($this->graphsettings["dsdefinition"])) {
  153. $cmdline .= ' '.$value;
  154. }
  155. while (list($key, $value) = each($this->graphsettings["graphdefinition"])) {
  156. $cmdline .= ' '.$value;
  157. }
  158. return $cmdline;
  159. }
  160. function sendimage() {
  161. $cmd = $this->cmdline();
  162. $this->debug($cmd);
  163. header("content-type: image/png");
  164. passthru($cmd);
  165. }
  166. function url2options($url) {
  167. $options = array();
  168. $url = urldecode($url);
  169. $args = explode('&', $url);
  170. $i = 0;
  171. while ($i < count($args)) {
  172. $operand = split('=', $args[$i]);
  173. if (!empty($operand)
  174. && is_array($operand)
  175. && count($operand) >= 2) {
  176. $key = htmlspecialchars(urldecode($operand[0]));
  177. if ($key == "opcode") {
  178. $this->graphsettings =
  179. unserialize(
  180. gzuncompress(
  181. base64_decode(
  182. urldecode($operand[1]))));
  183. } else {
  184. $value = htmlspecialchars(urldecode($operand[1]));
  185. $options[$key] = $value;
  186. }
  187. }
  188. $i++;
  189. }
  190. $this->parseoptions($options);
  191. }
  192. function settings2url() {
  193. $settings = urlencode(
  194. base64_encode(
  195. gzcompress(
  196. serialize($this->graphsettings),9)));
  197. return "opcode=".$settings;
  198. }
  199. function add($setting, $valuearray) {
  200. $this->graphsettings[$setting] =
  201. array_merge($this->graphsettings[$setting], $valuearray);
  202. }
  203. function set($setting, $value) {
  204. $this->graphsettings[$setting] = $value;
  205. }
  206. function get($setting) {
  207. return $this->graphsettings[$setting];
  208. }
  209. }
  210. ?>