diff options
author | Peter Wu | 2014-08-10 11:41:20 +0200 |
---|---|---|
committer | Peter Wu | 2014-08-10 12:42:51 +0200 |
commit | 57f657ddc06324416cef912dc691fffde6753206 (patch) | |
tree | 73a02a6cc49e063abeda98e3d81e061be8fb42be | |
parent | Merge (a)sync code, reformat CGP.js (diff) | |
download | apt-panopticon_cgp-57f657ddc06324416cef912dc691fffde6753206.zip apt-panopticon_cgp-57f657ddc06324416cef912dc691fffde6753206.tar.gz apt-panopticon_cgp-57f657ddc06324416cef912dc691fffde6753206.tar.bz2 apt-panopticon_cgp-57f657ddc06324416cef912dc691fffde6753206.tar.xz |
Support customized RRD URLs
Use case: I would like to bypass PHP for serving the RRD files and allow
the webserver (nginx) to compress it to reduce load on the Raspberry Pi.
I could go through all kinds of URL rewriting, but it is much easier to
set `$CONFIG['rrd_url'] = 'rrd/{file}';` instead and add a corresponding
location + alias directive to the nginx configuration.
This depends on https://github.com/manuelluis/jsrrdgraph/pull/17 to
avoid breaking on '='.
-rw-r--r-- | conf/config.php | 5 | ||||
-rw-r--r-- | rrd.php | 11 | ||||
-rw-r--r-- | type/Base.class.php | 12 |
3 files changed, 23 insertions, 5 deletions
diff --git a/conf/config.php b/conf/config.php index 66b5d0e..157012a 100644 --- a/conf/config.php +++ b/conf/config.php | |||
@@ -65,6 +65,11 @@ $CONFIG['graph_smooth'] = false; | |||
65 | # draw min/max spikes in a lighter color in graphs with type default | 65 | # draw min/max spikes in a lighter color in graphs with type default |
66 | $CONFIG['graph_minmax'] = false; | 66 | $CONFIG['graph_minmax'] = false; |
67 | 67 | ||
68 | # The URL that provides RRD files for the "canvas" graph type. Examples: | ||
69 | # 'rrd/{file}' is replaced by 'rrd/example.com/load/load.rrd' | ||
70 | # 'rrd.php?path={file_escaped}' becomes 'rrd.php?path=host%3Fload%3Fload.rrd' | ||
71 | $CONFIG['rrd_url'] = 'rrd.php?path={file_escaped}'; | ||
72 | |||
68 | # browser cache time for the graphs (in seconds) | 73 | # browser cache time for the graphs (in seconds) |
69 | $CONFIG['cache'] = 90; | 74 | $CONFIG['cache'] = 90; |
70 | 75 | ||
@@ -4,7 +4,13 @@ require_once 'conf/common.inc.php'; | |||
4 | require_once 'inc/functions.inc.php'; | 4 | require_once 'inc/functions.inc.php'; |
5 | require_once 'inc/html.inc.php'; | 5 | require_once 'inc/html.inc.php'; |
6 | 6 | ||
7 | if ( $file = validateRRDPath($CONFIG['datadir'], urldecode($_SERVER["QUERY_STRING"])) ) { | 7 | $path = filter_input(INPUT_GET, 'path'); |
8 | if (!$path) { | ||
9 | // legacy option: rrd.php?some.host/load/load.rrd | ||
10 | $path = urldecode(filter_input(INPUT_SERVER, 'QUERY_STRING')); | ||
11 | } | ||
12 | |||
13 | if ( $file = validateRRDPath($CONFIG['datadir'], $path) ) { | ||
8 | header('Content-Type: application/octet-stream'); | 14 | header('Content-Type: application/octet-stream'); |
9 | header('Content-Disposition: attachment; filename='.basename($file)); | 15 | header('Content-Disposition: attachment; filename='.basename($file)); |
10 | header("Expires: " .date(DATE_RFC822,strtotime($CONFIG['cache']." seconds"))); | 16 | header("Expires: " .date(DATE_RFC822,strtotime($CONFIG['cache']." seconds"))); |
@@ -15,10 +21,11 @@ if ( $file = validateRRDPath($CONFIG['datadir'], urldecode($_SERVER["QUERY_STRIN | |||
15 | header('HTTP/1.0 403 Forbidden'); | 21 | header('HTTP/1.0 403 Forbidden'); |
16 | 22 | ||
17 | html_start(); | 23 | html_start(); |
24 | $html_weburl = htmlentities($CONFIG['weburl']); | ||
18 | echo <<<EOT | 25 | echo <<<EOT |
19 | <fieldset id="forbidden"> | 26 | <fieldset id="forbidden"> |
20 | <legend>forbidden</legend> | 27 | <legend>forbidden</legend> |
21 | <p><a href="{$CONFIG['weburl']}">Return home...</a></p> | 28 | <p><a href="{$html_weburl}">Return home...</a></p> |
22 | </fieldset> | 29 | </fieldset> |
23 | 30 | ||
24 | EOT; | 31 | EOT; |
diff --git a/type/Base.class.php b/type/Base.class.php index 90056cf..c47aba2 100644 --- a/type/Base.class.php +++ b/type/Base.class.php | |||
@@ -6,6 +6,7 @@ class Type_Base { | |||
6 | var $datadir; | 6 | var $datadir; |
7 | var $rrdtool; | 7 | var $rrdtool; |
8 | var $rrdtool_opts = array(); | 8 | var $rrdtool_opts = array(); |
9 | var $rrd_url; | ||
9 | var $cache; | 10 | var $cache; |
10 | var $args; | 11 | var $args; |
11 | var $seconds; | 12 | var $seconds; |
@@ -44,6 +45,7 @@ class Type_Base { | |||
44 | $config['rrdtool_opts']); | 45 | $config['rrdtool_opts']); |
45 | } | 46 | } |
46 | } | 47 | } |
48 | $this->rrd_url = $config['rrd_url']; | ||
47 | $this->cache = $config['cache']; | 49 | $this->cache = $config['cache']; |
48 | $this->parse_get($_get); | 50 | $this->parse_get($_get); |
49 | $this->rrd_title = sprintf( | 51 | $this->rrd_title = sprintf( |
@@ -137,6 +139,9 @@ class Type_Base { | |||
137 | } | 139 | } |
138 | 140 | ||
139 | function rrd_escape($value) { | 141 | function rrd_escape($value) { |
142 | # In case people have really bizarre URLs in $CONFIG['rrd_url'], | ||
143 | # it should not be dropped. | ||
144 | return str_replace('\\', '\\\\', $value); | ||
140 | # http://oss.oetiker.ch/rrdtool/doc/rrdgraph_graph.en.html#IEscaping_the_colon | 145 | # http://oss.oetiker.ch/rrdtool/doc/rrdgraph_graph.en.html#IEscaping_the_colon |
141 | return str_replace(':', '\:', $value); | 146 | return str_replace(':', '\:', $value); |
142 | } | 147 | } |
@@ -144,10 +149,11 @@ class Type_Base { | |||
144 | function parse_filename($file) { | 149 | function parse_filename($file) { |
145 | if ($this->graph_type == 'canvas') { | 150 | if ($this->graph_type == 'canvas') { |
146 | $file = str_replace($this->datadir . '/', '', $file); | 151 | $file = str_replace($this->datadir . '/', '', $file); |
147 | # rawurlencode all but / | 152 | $rrd_url = str_replace('{file}', $file, $this->rrd_url); |
148 | $file = 'rrd.php?' . str_replace('%2F', '/', rawurlencode($file)); | 153 | $rrd_url = str_replace('{file_escaped}', |
154 | urlencode($file), $rrd_url); | ||
149 | } | 155 | } |
150 | return $this->rrd_escape($file); | 156 | return $this->rrd_escape($rrd_url); |
151 | } | 157 | } |
152 | 158 | ||
153 | function rrd_files() { | 159 | function rrd_files() { |