Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.56 KB | None | 0 0
  1. <?php
  2.  
  3.         // connect to db
  4.         $user = "analytics";
  5.         $password = "C5zWQMYJ";
  6.  
  7.         $db = "corona_app_analytics";
  8.         $host = "192.168.142.178";
  9.  
  10.         $conn = mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
  11.         mysql_select_db($db, $conn) or die("Could not select database: " . mysql_error());
  12.  
  13.  
  14.  
  15.         //decode post data
  16.         $json = json_decode($_POST['json'],true);
  17.  
  18.     // Sanitize as a function to allow recursing; original array passed by reference
  19.     function sanitize(&$array)
  20.     {
  21.         foreach ($array as &$data)
  22.         {
  23.             if (!is_array($data))
  24.             { // If it's not an array, clean it
  25.                             $data = mysql_real_escape_string( $data ); // escape unsafe mysql chars
  26.                     }
  27.                     else
  28.             { // If it IS an array, call the function on it
  29.                             sanitize($data);
  30.                     }
  31.             }      
  32.     }
  33.    
  34.     sanitize($json);
  35.  
  36.         //zero out event processing loop variables
  37.         $i = 0;
  38.     $count = count($json["events"]);
  39.  
  40.         //concat events into quoted CSV strings
  41.     while( $i < $count )
  42.         {
  43.                 $eventValuesArray[$i] =  "(\"" . $json["sessionId"] . $json["appId"] . $json["deviceId"] . $json["events"][$i]["eventName"] . $json["events"][$i]["timestamp"] . $json["events"][$i]["eventData"] . "\")";
  44.                 $i++;
  45.         }
  46.  
  47.         //concat event values into CSV string
  48.         $eventValues = implode(",", $eventValuesArray);
  49.  
  50.         $eventSql = "INSERT INTO events (sessionId, appId, deviceId, eventName, timestamp, eventData) VALUES " . $eventValues . ";";
  51.  
  52.         // Unset events element and collapse remaining elements into single quoted CSV string
  53.         unset($json["events"]);
  54.         $sessionInfo = "(\"" . implode("\", \"",$json) . "\");";
  55.         $sessionSql = "INSERT INTO sessions (sessionId, appId, deviceId, subType, mode, buildNum, appVersion, timestamp, deviceName, deviceOS, deviceOSVersion) VALUES " . $sessionInfo;
  56.  
  57.  
  58.         //error checking for inserts
  59.         $result = mysql_query($sessionSql, $conn);
  60.         if (!$result)
  61.         {
  62.                 echo "error: couldn't insert event values;\n";
  63.                 echo mysql_errno() . ": " . mysql_error() . "\n";
  64.                 echo $sessionSql;
  65.         }
  66.  
  67.         $result = mysql_query($eventSql, $conn);
  68.         if (!$result)
  69.         {
  70.                 echo "error: couldn't insert event values;\n";
  71.                 echo  mysql_errno() . ": ". mysql_error() . "\n";
  72.                 echo $eventValues;
  73.         }
  74. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement