| 
									
										
										
										
											2010-07-24 19:14:41 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-11 13:26:14 +00:00
										 |  |  | // Push $_GET into $vars to be compatible with web interface naming
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | foreach ($_GET as $name => $value) { | 
					
						
							|  |  |  |     $vars[$name] = $value; | 
					
						
							| 
									
										
										
										
											2012-05-11 13:26:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | preg_match('/^(?P<type>[A-Za-z0-9]+)_(?P<subtype>.+)/', $vars['type'], $graphtype); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | if (is_numeric($vars['device'])) { | 
					
						
							|  |  |  |     $device = device_by_id_cache($vars['device']); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | else if (!empty($vars['device'])) { | 
					
						
							|  |  |  |     $device = device_by_name($vars['device']); | 
					
						
							| 
									
										
										
										
											2012-05-11 13:26:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-25 12:24:34 +00:00
										 |  |  | // FIXME -- remove these
 | 
					
						
							| 
									
										
										
										
											2012-05-11 13:26:14 +00:00
										 |  |  | $width    = $vars['width']; | 
					
						
							|  |  |  | $height   = $vars['height']; | 
					
						
							|  |  |  | $title    = $vars['title']; | 
					
						
							|  |  |  | $vertical = $vars['vertical']; | 
					
						
							|  |  |  | $legend   = $vars['legend']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | $from = (isset($vars['from']) ? $vars['from'] : time() - 60 * 60 * 24); | 
					
						
							|  |  |  | $to   = (isset($vars['to']) ? $vars['to'] : time()); | 
					
						
							| 
									
										
										
										
											2012-03-20 10:04:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | if ($from < 0) { | 
					
						
							|  |  |  |     $from = ($to + $from); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-03-20 10:04:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | $period = ($to - $from); | 
					
						
							| 
									
										
										
										
											2012-04-17 13:29:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | $prev_from = ($from - $period); | 
					
						
							| 
									
										
										
										
											2012-04-17 13:29:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | $graphfile = $config['temp_dir'].'/'.strgen().'.png'; | 
					
						
							| 
									
										
										
										
											2011-09-22 13:39:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | $type    = $graphtype['type']; | 
					
						
							| 
									
										
										
										
											2010-12-02 17:41:12 +00:00
										 |  |  | $subtype = $graphtype['subtype']; | 
					
						
							| 
									
										
										
										
											2010-07-24 19:14:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-30 10:48:08 +01:00
										 |  |  | if ($auth !== true && $auth != 1) { | 
					
						
							|  |  |  |     $auth = is_client_authorized($_SERVER['REMOTE_ADDR']); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-05-11 15:21:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | require $config['install_dir']."/html/includes/graphs/$type/auth.inc.php"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ($auth === true && is_file($config['install_dir']."/html/includes/graphs/$type/$subtype.inc.php")) { | 
					
						
							|  |  |  |     include $config['install_dir']."/html/includes/graphs/$type/$subtype.inc.php"; | 
					
						
							| 
									
										
										
										
											2010-12-02 17:41:12 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | else if ($auth === true && is_mib_graph($type, $subtype)) { | 
					
						
							|  |  |  |     include $config['install_dir']."/html/includes/graphs/$type/mib.inc.php"; | 
					
						
							| 
									
										
										
										
											2015-06-13 22:55:51 +10:00
										 |  |  | } | 
					
						
							|  |  |  | else { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     graph_error("$type*$subtype "); | 
					
						
							|  |  |  |     // Graph Template Missing");
 | 
					
						
							| 
									
										
										
										
											2010-07-24 19:14:41 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-14 14:28:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | function graph_error($string) { | 
					
						
							|  |  |  |     global $vars, $config, $debug, $graphfile; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $vars['bg'] = 'FFBBBB'; | 
					
						
							| 
									
										
										
										
											2011-09-18 15:38:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     include 'includes/graphs/common.inc.php'; | 
					
						
							| 
									
										
										
										
											2012-04-05 16:47:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $rrd_options .= ' HRULE:0#555555'; | 
					
						
							|  |  |  |     $rrd_options .= " --title='".$string."'"; | 
					
						
							| 
									
										
										
										
											2011-09-14 14:28:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     rrdtool_graph($graphfile, $rrd_options); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($height > '99') { | 
					
						
							|  |  |  |         shell_exec($rrd_cmd); | 
					
						
							| 
									
										
										
										
											2015-08-20 15:07:28 +02:00
										 |  |  |         d_echo('<pre>'.$rrd_cmd.'</pre>'); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (is_file($graphfile) && !$debug) { | 
					
						
							|  |  |  |             header('Content-type: image/png'); | 
					
						
							|  |  |  |             $fd = fopen($graphfile, 'r'); | 
					
						
							|  |  |  |             fpassthru($fd); | 
					
						
							|  |  |  |             fclose($fd); | 
					
						
							|  |  |  |             unlink($graphfile); | 
					
						
							|  |  |  |             exit(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         if (!$debug) { | 
					
						
							|  |  |  |             header('Content-type: image/png'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-09-14 14:28:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $im = imagecreate($width, $height); | 
					
						
							|  |  |  |         $px = ((imagesx($im) - 7.5 * strlen($string)) / 2); | 
					
						
							|  |  |  |         imagestring($im, 3, $px, ($height / 2 - 8), $string, imagecolorallocate($im, 128, 0, 0)); | 
					
						
							|  |  |  |         imagepng($im); | 
					
						
							|  |  |  |         imagedestroy($im); | 
					
						
							|  |  |  |         exit(); | 
					
						
							| 
									
										
										
										
											2011-09-15 14:19:06 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-01 17:25:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 18:17:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-20 16:05:29 +00:00
										 |  |  | if ($error_msg) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     // We have an error :(
 | 
					
						
							|  |  |  |     graph_error($graph_error); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | else if ($auth === null) { | 
					
						
							|  |  |  |     // We are unauthenticated :(
 | 
					
						
							|  |  |  |     if ($width < 200) { | 
					
						
							|  |  |  |         graph_error('No Auth'); | 
					
						
							| 
									
										
										
										
											2010-08-02 22:25:26 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     else { | 
					
						
							|  |  |  |         graph_error('No Authorisation'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | else { | 
					
						
							|  |  |  |     // $rrd_options .= " HRULE:0#999999";
 | 
					
						
							|  |  |  |     if ($no_file) { | 
					
						
							|  |  |  |         if ($width < 200) { | 
					
						
							|  |  |  |             graph_error('No RRD'); | 
					
						
							| 
									
										
										
										
											2011-09-20 15:39:24 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         else { | 
					
						
							|  |  |  |             graph_error('Missing RRD Datafile'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if ($command_only) { | 
					
						
							|  |  |  |         echo "<div class='infobox'>"; | 
					
						
							|  |  |  |         echo "<p style='font-size: 16px; font-weight: bold;'>RRDTool Command</p>"; | 
					
						
							|  |  |  |         echo "rrdtool graph $graphfile $rrd_options"; | 
					
						
							|  |  |  |         echo '</span>'; | 
					
						
							|  |  |  |         $return = rrdtool_graph($graphfile, $rrd_options); | 
					
						
							|  |  |  |         echo '<br /><br />'; | 
					
						
							|  |  |  |         echo "<p style='font-size: 16px; font-weight: bold;'>RRDTool Output</p>$return"; | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  |         unlink($graphfile); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         echo '</div>'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         if ($rrd_options) { | 
					
						
							|  |  |  |             rrdtool_graph($graphfile, $rrd_options); | 
					
						
							| 
									
										
										
										
											2015-08-20 15:07:28 +02:00
										 |  |  |             d_echo($rrd_cmd); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (is_file($graphfile)) { | 
					
						
							|  |  |  |                 if (!$debug) { | 
					
						
							|  |  |  |                     header('Content-type: image/png'); | 
					
						
							|  |  |  |                     if ($config['trim_tobias']) { | 
					
						
							|  |  |  |                         list($w, $h, $type, $attr) = getimagesize($graphfile); | 
					
						
							|  |  |  |                         $src_im                    = imagecreatefrompng($graphfile); | 
					
						
							|  |  |  |                         $src_x = '0'; | 
					
						
							|  |  |  |                         // begin x
 | 
					
						
							|  |  |  |                         $src_y = '0'; | 
					
						
							|  |  |  |                         // begin y
 | 
					
						
							|  |  |  |                         $src_w = ($w - 12); | 
					
						
							|  |  |  |                         // width
 | 
					
						
							|  |  |  |                         $src_h = $h; | 
					
						
							|  |  |  |                         // height
 | 
					
						
							|  |  |  |                         $dst_x = '0'; | 
					
						
							|  |  |  |                         // destination x
 | 
					
						
							|  |  |  |                         $dst_y = '0'; | 
					
						
							|  |  |  |                         // destination y
 | 
					
						
							|  |  |  |                         $dst_im = imagecreatetruecolor($src_w, $src_h); | 
					
						
							|  |  |  |                         imagesavealpha($dst_im, true); | 
					
						
							|  |  |  |                         $white        = imagecolorallocate($dst_im, 255, 255, 255); | 
					
						
							|  |  |  |                         $trans_colour = imagecolorallocatealpha($dst_im, 0, 0, 0, 127); | 
					
						
							|  |  |  |                         imagefill($dst_im, 0, 0, $trans_colour); | 
					
						
							|  |  |  |                         imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); | 
					
						
							|  |  |  |                         imagepng($dst_im); | 
					
						
							|  |  |  |                         imagedestroy($dst_im); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     else { | 
					
						
							|  |  |  |                         $fd = fopen($graphfile, 'r'); | 
					
						
							|  |  |  |                         fpassthru($fd); | 
					
						
							|  |  |  |                         fclose($fd); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     echo `ls -l $graphfile`; | 
					
						
							|  |  |  |                     echo '<img src="'.data_uri($graphfile, 'image/png').'" alt="graph" />'; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 unlink($graphfile); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 if ($width < 200) { | 
					
						
							|  |  |  |                     graph_error('Draw Error'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     graph_error('Error Drawing Graph'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-12-02 17:41:12 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         else { | 
					
						
							|  |  |  |             if ($width < 200) { | 
					
						
							|  |  |  |                 graph_error('Def Error'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 graph_error('Graph Definition Error'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-08-02 22:25:26 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-12-02 17:41:12 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-08-01 17:25:44 +00:00
										 |  |  | } |