Data_Warehouse/warehouse_get.php

115 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2021-03-19 17:26:42 +00:00
<?php
ini_set("memory_limit","1000M");
date_default_timezone_set('America/Edmonton');
2021-03-19 17:26:42 +00:00
// if started from commandline, wrap parameters to $_POST and $_GET
if (!isset($_SERVER["HTTP_HOST"]) && isset($argv[1])) {
parse_str($argv[1], $_REQUEST);
}
$api_key = "cC7d2DHDnEwJTwiHLakhyjZGKLyWR";
$web_hook_room = "https://example.com/_matrix/maubot/plugin/botincomingwebhook/webhook/r0?room=%21xxxxxxxxxxxxx:example.com";
$web_hook_secret = "s5dXEqBThKiuwQj8etALv6A";
if( isset($_REQUEST['apiKey']) && $_REQUEST['apiKey'] == $api_key ) {
$web_result = "";
$records_inserted = 0;
$day_of_month = date("j");
$hour_of_day = date("G");
2021-03-19 17:26:42 +00:00
try {
$host = '127.0.0.1';
$user = 'dbuser';
$pass = 'password';
$database = 'datawarehouse';
// connect to the mysql database server.
$connect = mysqli_connect ( $host, $user, $pass ) ;
if ( ! $connect )
{
exit();
}
mysqli_select_db ( $connect,$database);
// Get the new queries
$web_result = file_get_contents("https://example.com/getDBLog.php?apiKey=".$api_key."&timestamp=1614301200");
// Save a copy for debugging
file_put_contents(dirname(__FILE__) . "/output_".$day_of_month."_".$hour_of_day.".json", $web_result);
2021-03-19 17:26:42 +00:00
// Parse the queries
$web_result = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($web_result));
$json_object = json_decode($web_result);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
if($json_object->message != ""){
error_log($web_result);
// Push to Matrix
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$web_hook_room);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('secret' => $web_hook_secret, 'message' => "Error on the data warehouse")));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$result = curl_exec($ch);
curl_close($ch);
exit();
}else{
foreach($json_object->data->transactions as $query) {
$result = mysqli_query ( $connect, $query->argument );
$records_inserted = $records_inserted +1;
}
// Push to Matrix
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$web_hook_room);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('secret' => $web_hook_secret, 'message' => "Data warehouse inserted ".$records_inserted." queries")));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$result = curl_exec($ch);
curl_close($ch);
}
}catch (Exception $e) {
$error_message = 'Exception Message: ' .$e->getMessage();
error_log($error_message);
error_log($web_result);
// Push to Matrix
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$web_hook_room);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('secret' => $web_hook_secret, 'message' => "Error on the data warehouse")));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$result = curl_exec($ch);
curl_close($ch);
}
}else{
echo '{ "message": "Invalid post variables", "data": {}}';
}
?>