cusf-standalone-predictor/predict/ajax.php

135 wiersze
4.0 KiB
PHP
Czysty Zwykły widok Historia

<?php
2010-05-30 10:06:23 +00:00
require_once("includes/functions.inc.php");
require_once("includes/config.inc.php");
2012-07-12 23:41:27 +00:00
$stats = new StatsD();
$action = $_GET['action'];
$software_available = array("gfs", "gfs_hd");
switch($action) {
case "getCSV":
$uuid = $_GET['uuid'];
2010-07-30 08:19:36 +00:00
$tryfile = PREDS_PATH . $uuid . "/" . FLIGHT_CSV;
if(!file_exists($tryfile)) return false;
$fh = fopen($tryfile, "r");
$data = array();
while (!feof($fh)) {
$line = trim(fgets($fh));
array_push($data, $line);
}
$returned = json_encode($data);
echo $returned;
2012-07-12 23:41:27 +00:00
$stats->counting('habhub.predictor.php.get_csv');
break;
2010-05-28 22:17:49 +00:00
case "JSONexists":
$uuid = $_GET['uuid'];
2010-07-30 08:19:36 +00:00
if(file_exists(PREDS_PATH . $uuid . "/" . PROGRESS_JSON)) {
2010-05-28 22:17:49 +00:00
echo true;
} else {
echo false;
}
break;
2010-06-08 16:29:38 +00:00
case "locationSave":
$lat = $_POST['req_lat'];
$lon = $_POST['req_lon'];
$alt = $_POST['req_alt'];
$locname = $_POST['req_name'];
if ( $locname == '' || !LOCATION_SAVE_ENABLE ) {
echo "false";
2010-06-08 16:29:38 +00:00
return;
}
$str = "Latitude: " . $lat . "\n" .
"Longitude: " . $lon . "\n" .
"Altitude: " . $alt . "\n" .
"Name: " . $locname . "\n";
$headers = "From: ". ADMIN_EMAIL ."\r\nReply-To:blackhole@hexoc.com\r\nX-Mailer: PHP/".phpversion();
if ( mail(ADMIN_EMAIL, "Location Request Save", $str, $headers) ) {
2010-06-08 16:29:38 +00:00
echo "true";
} else {
echo "false";
}
break;
2010-05-31 13:11:18 +00:00
case "getModelByUUID":
$uuid = ( isset($_GET['uuid']) ? $_GET['uuid'] : false );
2010-11-28 12:46:54 +00:00
if( !$uuid ) die ("No uuid given to getModelByUUID");
2010-05-31 13:11:18 +00:00
// make a new model
$pred_model = array();
2010-07-30 08:19:36 +00:00
if ( !file_exists(PREDS_PATH . $uuid . "/" . SCENARIO_FILE ) ) {
2010-05-31 13:11:18 +00:00
$pred_model['valid'] = false;
2012-07-12 23:41:27 +00:00
$stats->counting('habhub.predictor.php.couldnt_get_by_uuid');
2010-05-31 13:11:18 +00:00
} else {
// populate the array, JSON encode it and return
2010-07-30 08:19:36 +00:00
$pred_model = parse_ini_file(PREDS_PATH . $uuid . "/" . SCENARIO_FILE);
if ( verifyModel($pred_model, $software_available) ){
$pred_model['valid'] = true;
2010-05-31 13:11:18 +00:00
} else {
$pred_model['valid'] = false;
2010-05-31 13:11:18 +00:00
}
$pred_model['uuid'] = $uuid;
2012-07-12 23:41:27 +00:00
$stats->counting('habhub.predictor.php.got_by_uuid');
2010-05-31 13:11:18 +00:00
}
echo json_encode($pred_model);
break;
case "submitForm":
2010-05-30 10:06:23 +00:00
$pred_model = array();
$json_return = array();
$json_return['valid'] = "false";
2010-05-30 10:06:23 +00:00
// Make sure we have a submitted form
2010-05-30 10:06:23 +00:00
if ( isset($_POST['submit'])) {
// First, make a model from the form data
if ( !$pred_model = createModel($_POST)) {
$json_return['error'] = "Server couldn't make a model from the form
data";
echo json_encode($json_return);
2012-07-24 15:25:34 +00:00
$stats->counting('habhub.predictor.php.form_error');
break;
}
// If that worked, make sure the model is valid
$verify_dump = verifyModel($pred_model, $software_available);
if ( !$verify_dump['valid'] ) {
$json_return['error'] = $verify_dump['msg'];
echo json_encode($json_return);
2012-07-24 15:25:34 +00:00
$stats->counting('habhub.predictor.php.invalid_model');
break;
}
// If we have a valid model, try and make a UUID
if ( !$pred_model['uuid'] = makesha1hash($pred_model) ) {
$json_return['error'] = "Couldn't make the SHA1 hash";
echo json_encode($json_return);
2012-07-24 15:25:34 +00:00
$stats->counting('habhub.predictor.php.unhashable');
break;
2010-05-30 10:06:23 +00:00
}
// If all of the above worked, let's run the prediction
runPred($pred_model);
$json_return['valid'] = "true";
$json_return['uuid'] = $pred_model['uuid'];
$json_return['timestamp'] = $pred_model['timestamp'];
2012-07-12 23:41:27 +00:00
$stats->counting('habhub.predictor.php.prediction_run');
2010-05-30 10:06:23 +00:00
} else {
$json_return['error'] = "The form submit function was called without
any data";
2012-07-12 23:41:27 +00:00
$stats->counting('habhub.predictor.php.no_form_data');
2010-05-30 10:06:23 +00:00
}
echo json_encode($json_return);
break;
default:
echo "Couldn't interpret 'action' variable";
break;
}
?>