Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // connect to db
- $user = "analytics";
- $password = "C5zWQMYJ";
- $db = "corona_app_analytics";
- $host = "192.168.142.178";
- $conn = mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
- mysql_select_db($db, $conn) or die("Could not select database: " . mysql_error());
- //decode post data
- $json = json_decode($_POST['json'],true);
- // Sanitize as a function to allow recursing; original array passed by reference
- function sanitize(&$array)
- {
- foreach ($array as &$data)
- {
- if (!is_array($data))
- { // If it's not an array, clean it
- $data = mysql_real_escape_string( $data ); // escape unsafe mysql chars
- }
- else
- { // If it IS an array, call the function on it
- sanitize($data);
- }
- }
- }
- sanitize($json);
- //zero out event processing loop variables
- $i = 0;
- $count = count($json["events"]);
- //concat events into quoted CSV strings
- while( $i < $count )
- {
- $eventValuesArray[$i] = "(\"" . $json["sessionId"] . $json["appId"] . $json["deviceId"] . $json["events"][$i]["eventName"] . $json["events"][$i]["timestamp"] . $json["events"][$i]["eventData"] . "\")";
- $i++;
- }
- //concat event values into CSV string
- $eventValues = implode(",", $eventValuesArray);
- $eventSql = "INSERT INTO events (sessionId, appId, deviceId, eventName, timestamp, eventData) VALUES " . $eventValues . ";";
- // Unset events element and collapse remaining elements into single quoted CSV string
- unset($json["events"]);
- $sessionInfo = "(\"" . implode("\", \"",$json) . "\");";
- $sessionSql = "INSERT INTO sessions (sessionId, appId, deviceId, subType, mode, buildNum, appVersion, timestamp, deviceName, deviceOS, deviceOSVersion) VALUES " . $sessionInfo;
- //error checking for inserts
- $result = mysql_query($sessionSql, $conn);
- if (!$result)
- {
- echo "error: couldn't insert event values;\n";
- echo mysql_errno() . ": " . mysql_error() . "\n";
- echo $sessionSql;
- }
- $result = mysql_query($eventSql, $conn);
- if (!$result)
- {
- echo "error: couldn't insert event values;\n";
- echo mysql_errno() . ": ". mysql_error() . "\n";
- echo $eventValues;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement