Skip to content

Instantly share code, notes, and snippets.

@algirdasc
Created June 14, 2018 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save algirdasc/e1cee394bc7fef251662d204474dc61e to your computer and use it in GitHub Desktop.
Save algirdasc/e1cee394bc7fef251662d204474dc61e to your computer and use it in GitHub Desktop.
Grafana Graph PHP Proxy
<?php
// Change to your LAN IP subnet
preg_match('/^192\.168\.0\.\d{1,3}$/', $_SERVER['REMOTE_ADDR'], $output);
if (!$output) {
die('Access denied');
}
/* To generate bearer, go to Grafana confiration page, select API Keys and create Viewer API Key */
$bearer = '<YOUR GRAFANA API KEY>';
/* Example: https://<grafana_url>/render/d-solo/blablabla/dashboard */
$base_url = '<GRAFANA DASHBOARD URL>';
/* Download Image Magician from https://github.com/Oberto/php-image-magician/blob/master/php_image_magician.php */
include('includes/php_image_magician.php');
$w = (int) $_GET['w'] ?? false;
$h = (int) $_GET['h'] ?? false;
if (!empty($_GET['w'])) { unset($_GET['w']); }
if (!empty($_GET['h'])) { unset($_GET['h']); }
$query = http_build_query($_GET);
$url = "{$base_url}?{$query}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer {$bearer}"
]);
$response = curl_exec($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$content_length = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
$image_types = ['image/png', 'image/jpg'];
if (!in_array($content_type, $image_types)) {
echo $response;
die();
}
$ext = explode('/', $content_type)[1];
$tmp_file = sys_get_temp_dir() . '/' . 'dash' . '.' . md5(time()) . '.' . $ext;
file_put_contents($tmp_file, $response);
if ($w && $h) {
$imageLib = new imageLib($tmp_file);
$imageLib->resizeImage($w, $h, 0);
$imageLib->saveImage($tmp_file, 100);
$content_lenght = filesize($tmp_file);
}
header("Content-type: {$content_type}");
//header("Content-length: {$content_length}");
echo file_get_contents($tmp_file);
unlink($tmp_file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment