$maxlength) {
$length = $maxlength;
}
$i = 0;
while ($i < $length) {
$char = substr($possible, mt_rand(0, $maxlength-1), 1);
if (!strstr($passwd, $char)) {
$passwd .= $char;
$i++;
}
}
return $passwd;
}
function GetPostValues() {
if (!empty($_POST)) {
$kv = array();
foreach ($_POST as $key => $value) {
$kv[] = "$key=$value";
}
$q = join("&", $kv);
}
else {
$q = 0;
}
return $q;
}
$file = fopen($mylogfile, "a");
$mydate = date("YmdHis");
$myfunc = $_GET['s'];
$q = GetPostValues();
fputs($file, "$mydate $myfunc IN $q\r\n");
if ($myfunc == 'once') {
$output = '{"status":0,"body":{"once":"'. generateString(8).'-'.generateString(8) .'"}}';
}
if ($myfunc == 'session')
{
if ($_POST['action']=='new') {
$mytime = date("U");
$battery = $_POST['batterylvl'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO wi_measure(value,unit,type)
VALUES ($battery,1,1)";
if ($conn->query($sql) != TRUE) {
echo "Error: " . $sql . "
" . $conn->error;
}
$conn->close();
$output = '{"status":0,"body":{"sessionid":"'. generateString(4) . '-' . generateString(8) . '-' . generateString(8) .'","sp":
{"users":[{"id":'.$myuserid.',"sn":"'.$myshortname.'","wt":'.$myweight.',"ht":'.$myheight.',"agt":'.$myage.',"sx":0,"fm":3,"cr":1453317502,"att":0,"cs":"11,6,1,10,4"}]},"ind":{"lg":"fr_FR","imt":1,"stp":1,"f":0,"g":98086,"tmp":11,"w":1},"syp":{"utc":'.$mytime.'},"ctp":{"goff":3600,"dst":1459040400,"ngoff":7200},"color":"white"}}';
}
if ($_POST['action']=='delete') {
$output = '{"status":0}';
}
}
if ($myfunc == 'maint') {
$output = '{"status":0}';
}
if ($myfunc == 'measure') {
$output = '{"status":0}';
if (!empty($_POST['macaddress'])) {
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$str = $_POST['measuregrps'];
$userid = $_POST['userid'];
if ($userid == '') {
$userid = 0;
}
$obj = json_decode($str);
foreach ($obj->measuregrps as $mgrp) {
$meastime = $mgrp->meastime;
foreach ($mgrp->measures as $measure) {
$value = $measure->value;
$type = $measure->type;
$unit = $measure->unit;
$sql = "insert into wi_measure (value, unit, type,meastime,userid) values ($value, $unit, $type, from_unixtime($meastime),$userid);";
if ($conn->query($sql) != TRUE) {
echo "Error: " . $sql . "
" . $conn->error;
}
}
}
$conn->close();
}
}
if ($myfunc == 'v2measure') {
$output = '{"status":0}';
$str = $_POST['measuregrps'];
$obj = json_decode($str);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach ($obj as $mgrp) {
$meastime = $mgrp->meastime;
foreach ($mgrp->measures as $measure) {
$mantissa = $measure->data->mantissa;
$exponent = $measure->data->exponent;
$unit = $measure->data->unit;
$type = $measure->type;
$sql = "insert into wi_measure ( unit, type,meastime,mantissa,exponent) values ($unit, $type, from_unixtime($meastime),$mantissa,$exponent);";
if ($conn->query($sql) != TRUE) {
echo "Error: " . $sql . "
" . $conn->error;
}
}
}
$conn->close();
}
echo $output;
fputs($file, "$mydate $myfunc OUT $output\r\n");
fclose($file);
?>