+ $legendlines = '';
+ foreach ($graphrows as $grow) {
+ $legendline = isset($grow['desc'])?$grow['desc']:(isset($grow['legend'])?$grow['legend']:$grow['name']);
+ $legendline .= '|'.@$grow['color'];
+ $legendline .= '|'.(isset($grow['color_bg'])?$grow['color_bg']:'');
+ $legendline .= '|'.(isset($grow['legend_long'])?$grow['legend_long']:'');
+ $legendlines .= 'legend:'.$legendline."\n";
+ }
+ $return = 'file:'.$fname."\n".$legendlines.$return;
+ return $return;
+ }
+
+ function graph_plus($timeframe = 'day', $sub = null, $extra = null) {
+ // create a RRD graph and return meta info as a ready-to-use array
+ $gmeta = array('filename'=>null,'legends_long'=>false,'default_colorize'=>false);
+ $ret = $this->graph($timeframe, $sub, $extra);
+ if (strpos($ret, "\n\n") !== false) { $gmeta['graph_cmd'] = substr($ret, 0, strpos($ret, "\n\n")); $ret = substr($ret, strpos($ret, "\n\n")+2); }
+ else { $gmeta['graph_cmd'] = null; }
+ $grout = explode("\n", $ret);
+ foreach ($grout as $gline) {
+ if (preg_match('/^file:(.+)$/', $gline, $regs)) {
+ $gmeta['filename'] = $regs[1];
+ }
+ elseif (preg_match('/^legend:([^\|]+)\|([^|]*)\|([^\|]*)\|(.*)$/', $gline, $regs)) {
+ $gmeta['legend'][$regs[1]] = array('color'=>$regs[2], 'color_bg'=>$regs[3], 'desc_long'=>$regs[4]);
+ if (strlen($regs[4])) { $gmeta['legends_long'] = true; }
+ if (strlen($regs[3]) || strlen($regs[4])) { $gmeta['default_colorize'] = true; }
+ }
+ elseif (preg_match('/^(\d+)x(\d+)$/', $gline, $regs)) {
+ $gmeta['width'] = $regs[1]; $gmeta['height'] = $regs[2];
+ }
+ elseif (preg_match('/^([^\|]+)\|([^|]+)\|([^\|]*)$/', $gline, $regs)) {
+ $gmeta['data'][$regs[1]][$regs[2]] = $regs[3];
+ }
+ elseif (preg_match('/^([^\|]+)\|([^\|]*)$/', $gline, $regs)) {
+ $gmeta['var'][$regs[1]] = $regs[2];
+ }
+ elseif (strlen(trim($gline))) {
+ $gmeta['info'][] = $gline;
+ }
+ }
+ if (is_null($gmeta['filename'])) {
+ $gmeta['filename'] = $this->basename.(!is_null($sub)?'-'.$sub:'').'-'.$timeframe.'.png';
+ }
+ return $gmeta;
+ }
+
+ function page($sub = null, $page_extras = null, $graph_extras = null) {
+ // create a (HTML) page and return it in a string
+
+ // assemble configuration
+ $pconf = (array)$page_extras;
+ if (!is_null($sub) && is_array($this->config_raw['page.'.$sub])) {
+ $pconf = $pconf + $this->config_raw['page.'.$sub];
+ }
+ $pconf = $pconf + (array)$this->config_page;
+
+ $return = null;
+ switch (@$pconf['type']) {
+ case 'index':
+ $return = $this->page_index($pconf);
+ break;
+ case 'overview':
+ $return = $this->page_overview($pconf, $graph_extras);
+ break;
+ case 'simple':
+ default:
+ $return = $this->page_simple($pconf, $graph_extras);
+ break;
+ }
+ return $return;
+ }
+
+ function simple_html($sub = null, $page_extras = null, $graph_extras = null) {
+ // create a simple (MRTG-like) HTML page and return it in a string
+ // XXX: this is here temporarily for compat only, it's preferred to use page()!
+
+ // assemble configuration
+ $pconf = (array)$page_extras;
+ if (!is_null($sub) && is_array($this->config_raw['page.'.$sub])) {
+ $pconf = $pconf + $this->config_raw['page.'.$sub];
+ }
+ $pconf = $pconf + (array)$this->config_page;
+
+ return $this->page_simple($pconf, $graph_extras);
+ }
+
+ function page_index($pconf) {
+ // create a bare, very simple index list HTML page and return it in a string
+ $td = $this->mod_textdomain;
+ $ptitle = isset($pconf['title_page'])?$pconf['title_page']:dgettext($td, 'RRD statistics index');
+
+ $out = '<html><head>';
+ $out .= '<title>'.$ptitle.'</title>';
+ $out .= '<style>';
+ if (isset($pconf['style_base'])) { $out .= $pconf['style_base']; }
+ else {
+ $out .= 'h1 { font-weight: bold; font-size: 1.5em; }';
+ $out .= '.footer { font-size: 0.75em; margin: 0.5em 0; }';
+ $out .= 'li.scanfile { font-style: italic; }';
+ }
+ if (isset($pconf['style'])) { $out .= $pconf['style']; }
+ $out .= '</style>';
+ $out .= '</head>';
+ $out .= '<body>';
+
+ $out .= '<h1>'.$ptitle.'</h1>';
+ if (isset($pconf['text_intro']) && strlen($pconf['text_intro'])) {
+ $out .= '<p class="intro">'.$pconf['text_intro'].'</p>';
+ }
+ elseif (!isset($pconf['text_intro'])) {
+ $out .= '<p class="intro">'.dgettext($td, 'The following RRD stats are available:').'</p>';
+ }
+
+ $stats = $this->h_page_statsArray($pconf);
+
+ if (isset($pconf['stats_url'])) { $sURL_base = $pconf['stats_url']; }
+ else { $sURL_base = '?stat=%i%a'; }
+
+ if (isset($pconf['stats_url_add'])) { $sURL_add = $pconf['stats_url_add']; }
+ else { $sURL_add = '&sub=%s'; }
+
+ $out .= '<ul class="indexlist">';
+ foreach ($stats as $stat) {
+ $out .= '<li'.(isset($stat['class'])?' class="'.$stat['class'].'"':'').'>';
+ $sURL = str_replace('%i', $stat['name'], $sURL_base);
+ $sURL = str_replace('%a', '', $sURL);
+ $sURL = str_replace('%s', '', $sURL);
+ $out .= '<a href="'.$sURL.'">'.$stat['name'].'</a>';
+ if (isset($stat['sub']) && count($stat['sub'])) {
+ $sprt = array();
+ foreach ($stat['sub'] as $ssub) {
+ $sURL = str_replace('%i', $stat['name'], $sURL_base);
+ $sURL = str_replace('%a', $sURL_add, $sURL);
+ $sURL = str_replace('%s', $ssub, $sURL);
+ $sprt[] = '<a href="'.$sURL.'">'.$ssub.'</a>';
+ }
+ $out .= ' <span="subs">('.implode(', ', $sprt).')</span>';
+ }
+ $out .= '</li>';
+ }
+ $out .= '</ul>';
+
+ $out .= $this->h_page_footer();
+ $out .= '</body></html>';
+ return $out;
+ }
+
+ function page_overview($pconf, $graph_extras = null) {
+ // create an overview HTML page (including graphs) and return it in a string
+ $td = $this->mod_textdomain;
+ $ptitle = isset($pconf['title_page'])?$pconf['title_page']:dgettext($td, 'RRD statistics overview');
+
+ $out = '<html><head>';
+ $out .= '<title>'.$ptitle.'</title>';
+ $out .= '<style>';
+ if (isset($pconf['style_base'])) { $out .= $pconf['style_base']; }
+ else {
+ $out .= 'h1 { font-weight: bold; font-size: 1.5em; }';
+ $out .= 'h2 { font-weight: bold; font-size: 1em; margin: 0.5em 0; }';
+ $out .= '.footer { font-size: 0.75em; margin: 0.5em 0; }';
+ $out .= 'img.rrdgraph { border: none; }';
+ }
+ if (isset($pconf['style'])) { $out .= $pconf['style']; }
+ $out .= '</style>';
+ $out .= '</head>';
+ $out .= '<body>';
+
+ $out .= '<h1>'.$ptitle.'</h1>';
+ if (isset($pconf['text_intro']) && strlen($pconf['text_intro'])) { $out .= '<p class="intro">'.$pconf['text_intro'].'</p>'; }
+
+ $stats = $this->h_page_statsArray($pconf);
+
+ if (isset($pconf['stats_url'])) { $sURL_base = $pconf['stats_url']; }
+ else { $sURL_base = '?stat=%i%a'; }
+
+ if (isset($pconf['stats_url_add'])) { $sURL_add = $pconf['stats_url_add']; }
+ else { $sURL_add = '&sub=%s'; }
+
+ $num_rows = is_numeric($pconf['num_rows'])?$pconf['num_rows']:2;
+ $num_cols = ceil(count($stats)/$num_rows);
+
+ $out .= '<table class="overview">';
+ for ($col = 0; $col < $num_cols; $col++) {
+ $out .= '<tr>';
+ for ($row = 0; $row < $num_rows; $row++) {
+ $idx = $col * $num_rows + $row;
+ $out .= '<td>';
+ if ($idx < count($stats)) {
+ @list($sname, $s_psub) = explode('|', $stats[$idx]['name'], 2);
+ $s_psname = 'page'.(isset($s_psub)?'.'.$s_psub:'');
+ $g_sub = @$this->config_all[$sname][$s_psname]['graph_sub'];
+
+ if (isset($this->config_all[$sname][$s_psname]['title_page'])) {
+ $s_ptitle = $this->config_all[$sname][$s_psname]['title_page'];
+ }
+ elseif (isset($this->config_all[$sname]['page']['title_page'])) {
+ $s_ptitle = $this->config_all[$sname]['page']['title_page'];
+ }
+ else {
+ $s_ptitle = isset($s_psub)?sprintf(dgettext($td, '%s (%s) statistics'), $sname, $s_psub):sprintf(dgettext($td, '%s statistics'), $sname);
+ }
+ if (!isset($pconf['hide_titles']) || !$pconf['hide_titles']) {
+ $out .= '<h2>'.$s_ptitle.'</h2>';
+ }
+
+ $s_rrd = new rrdstat($this->config_all, $sname);
+ if (in_array($s_rrd->status, array('ok','readonly','graphonly'))) {
+ $tframe = isset($pconf['graph_timeframe'])?$pconf['graph_timeframe']:'day';
+ $gmeta = $s_rrd->graph_plus($tframe, $g_sub);
+ if (isset($pconf['graph_url'])) {
+ $gURL = $pconf['graph_url'];
+ $gURL = str_replace('%f', basename($gmeta['filename']), $gURL);
+ $gURL = str_replace('%p', $gmeta['filename'], $gURL);
+ if (substr($gURL, -1) == '/') { $gURL .= $gmeta['filename']; }
+ }
+ else {
+ $gURL = $gmeta['filename'];
+ }
+ $sURL = str_replace('%i', $sname, $sURL_base);
+ $sURL = str_replace('%a', isset($s_psub)?$sURL_add:'', $sURL);
+ $sURL = str_replace('%s', isset($s_psub)?$s_psub:'', $sURL);
+ $out .= '<a href="'.$sURL.'">';
+ $out .= '<img src="'.$gURL.'"';
+ $out .= ' alt="'.$s_rrd->basename.(!is_null($g_sub)?' - '.$g_sub:'').' - '.$tframe.'" class="rrdgraph"';
+ if (isset($gmeta['width']) && isset($gmeta['height'])) { $out .= ' style="width:'.$gmeta['width'].'px;height:'.$gmeta['height'].'px;"'; }
+ $out .= '></a>';
+ }
+ else {
+ $out .= sprintf(dgettext($td, 'RRD error: status is "%s"'), $s_rrd->status);
+ }
+ }
+ else {
+ $out .= ' ';
+ }
+ $out .= '</td>';
+ }
+ $out .= '</tr>';
+ }
+ $out .= '</table>';
+
+ $out .= $this->h_page_footer();
+ $out .= '</body></html>';
+ return $out;