Last active
August 28, 2016 16:33
-
-
Save Leen15/5b0556e0c68c9543939ccbbd8af01560 to your computer and use it in GitHub Desktop.
Simple PHP script for expose metabase datasets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-type: text/plain'); | |
$base_url = "https://metabase.domain.com/api/"; | |
$auth_label = "IS_PUBLIC"; | |
$user_email= "METABASE_USER"; | |
$user_pass= "METABASE_PASS"; | |
$card_id = 0; | |
if (isset($_GET['card_id'])) | |
{ | |
$card_id = $_GET['card_id']; | |
} | |
else | |
{ | |
die("no card_id in params"); | |
} | |
function get_card_query($session_id, $card_id, $base_url) | |
{ | |
$url = $base_url . "card/" . $card_id; | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/json\r\n" . | |
"x-metabase-session: $session_id\r\n", | |
'method' => 'GET' | |
) | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
if (strpos($http_response_header[0], "200")) { | |
// | |
$response = json_decode($result); | |
return $response; | |
} | |
else | |
{ | |
$response = new \stdClass(); | |
$response->error = $http_response_header[0]; | |
return $response; | |
} | |
} | |
function get_dataset($session_id, $card, $base_url) | |
{ | |
$url = $base_url . "dataset"; | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/json\r\n" . | |
"x-metabase-session: $session_id\r\n", | |
'method' => 'POST', | |
'content' => json_encode( $card->dataset_query ) | |
) | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
if (strpos($http_response_header[0], "200")) { | |
// | |
$response = json_decode($result); | |
return $response; | |
} | |
else | |
{ | |
$response = new \stdClass(); | |
$response->error = $http_response_header[0]; | |
return $response; | |
} | |
} | |
function login($user_email, $user_pass, $base_url) | |
{ | |
$url = $base_url . "session"; | |
$content_data = new \stdClass(); | |
$content_data->email = $user_email; | |
$content_data->password = $user_pass; | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/json\r\n" , | |
'method' => 'POST', | |
'content' => json_encode( $content_data ) | |
) | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
if (strpos($http_response_header[0], "200")) { | |
// | |
$response = json_decode($result); | |
if (strlen($response->id) > 0) | |
{ | |
$cookie_name = 'MB_ID'; | |
$cookie_value = $response->id; | |
setcookie($cookie_name, $cookie_value, time() + (86400 * 7), '/'); // 86400 = 1 day | |
$_COOKIE[$cookie_name] = $cookie_value; | |
} | |
return $response->id; | |
} | |
else | |
{ | |
var_dump($http_response_header); | |
} | |
return ""; | |
} | |
$card = new \stdClass(); | |
$session_id = ""; | |
//echo "Check Cookie" . PHP_EOL; | |
if(isset($_COOKIE['MB_ID'])){ | |
$session_id = $_COOKIE['MB_ID']; | |
//echo "Cookie Exists, get card data" . PHP_EOL; | |
$card = get_card_query($session_id, $card_id, $base_url); | |
if (isset($card->error)) | |
{ | |
//echo "Error retriving card, get another session_id" . PHP_EOL; | |
$session_id = login($user_email, $user_pass, $base_url); | |
} | |
} | |
else{ | |
//echo "Cookie is not set, get one" . PHP_EOL; | |
$session_id = login($user_email, $user_pass, $base_url); | |
} | |
if (!isset($card->id)) | |
{ | |
//echo "get card data" . PHP_EOL; | |
$card = get_card_query($session_id, $card_id, $base_url); | |
} | |
if (isset($card->id)) | |
{ | |
$is_public = false; | |
foreach ($card->labels as $label) | |
{ | |
if ($label->name == $auth_label) | |
{ | |
$is_public = true; | |
break; | |
} | |
} | |
if ($is_public) | |
{ | |
//echo "card exists, get dataset" . PHP_EOL; | |
$dataset = get_dataset($session_id, $card, $base_url); | |
// $dataset->card = $card; | |
// echo json_encode($dataset); | |
echo json_encode($dataset->data); | |
} | |
else | |
{ | |
http_response_code(401); | |
die("this card is not public"); | |
} | |
} | |
else | |
{ | |
http_response_code(400); | |
die("error retrieving card"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment