class_graph.inc 6.1 KB

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