<?php
session_start();

// **** If user not logged, redirect to login page ****
if (!isset($_SESSION['loggedin'])) {
  header('Location: login.php');
  exit;
}

include_once dirname(__FILE__) . '../../includes/Constants.php';
 
// **** Connecting to MySQL database ****
$conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
} 

// **** Load sessions ****
$userid = $_SESSION['userid'];

// **** HTML header ****
echo $html ='<head>';
  echo $html = '<link href = "css/bootstrap.min.css" rel = "stylesheet">';
  echo $html ='<script src="https://api.mapy.cz/loader.js"></script>';
  echo $html ='<script>Loader.load()</script>';
  echo $html ='<link href="view_data.css" rel="stylesheet" type="text/css">';
echo $html ='</head>';


// **** Load user data ****
$sql1 = "SELECT * FROM `users` WHERE `userid`= '$userid'";
    $result1 = $conn->query($sql1);
    $row1 = $result1->fetch_assoc();
    
// ***************************************************************************SMS**********************************************************************************************************
if ($row1["sms"] == "sms"){

    echo $html = '<h2> SMS messages</h2>';

    $sql = "SELECT id,date,number,text,type FROM `sms` WHERE `userid`= '$userid'";
    $result = $conn->query($sql);

    while($row = $result->fetch_assoc()){
        $new_array[] = $row; // Inside while loop
    }


    if ($result->num_rows > 0) {
      function build_table($array){
        // start table
        $html = '<table border="1" class="styled-table">';
        // header row
        $html .= '<tr>';
            $html .='<th>ID</th>';
            $html .='<th>Date</th>';
            $html .='<th>Phone number</th>';
            $html .='<th>Text</th>';
            $html .='<th>Type</th>';
        $html .= '</tr>';

        $index=1;
        // data rows
        foreach( $array as $key=>$value){
            $html .= '<tr>';
            
            foreach($value as $key2=>$value2){
                if ($key2== "id"){
                    $value2 = $index++;
                }
                $html .= '<td>' . htmlspecialchars($value2) . '</td>';
            }
            $html .= '</tr>';
        }

        // finish table and return it

        $html .= '</table>';
        return $html;
      }

  echo build_table($new_array);}
  else{
    echo "<p> NO SMS MESSAGES RECEIVED FROM DEVICE </p>";
        echo "<p> - Try to refresh page - </p>";
        echo "<p> If that doesn't help, there is no SMS messages in the device.</p>";
  }
}

// ***************************************************************************Calendar**********************************************************************************************************
if ($row1["event"] == "event"){
  
    echo $html = '<h2> Events from calendar</h2>';

    $sql = "SELECT id,title,startdate,enddate,description FROM `event` WHERE  `userid`= '$userid'";
    $result = $conn->query($sql);

    while($row = $result->fetch_assoc()){
        $new_array2[] = $row; // Inside while loop
    }


    if ($result->num_rows > 0) {
  function build_table1($array){
    // start table
    $html = '<table border="1" class="styled-table">';
    // header row
    $html .= '<tr>';
        $html .='<th>ID</th>';
        $html .='<th>Title</th>';
        $html .='<th>Start</th>';
        $html .='<th>End</th>';
        $html .='<th>Description</th>';
    $html .= '</tr>';

    $index=1;
    // data rows
    foreach( $array as $key=>$value){
        $html .= '<tr>';
        
        foreach($value as $key2=>$value2){
            if ($key2== "id"){
                $value2 = $index++;
            }
            $html .= '<td>' . htmlspecialchars($value2) . '</td>';
        }
        $html .= '</tr>';
    }

    // finish table and return it

    $html .= '</table>';
    return $html;
  }

  echo build_table1($new_array2);}
  else {
    echo "<p> NO CALENDAR EVENTS RECEIVED FROM DEVICE </p>";
        echo "<p> - Try to refresh page - </p>";
        echo "<p> If that doesn't help, there is no calendar events in the device.</p>";
  }
} 

// ***************************************************************************Call logs**********************************************************************************************************
if ($row1["callLog"] == "callLog"){
  
    echo $html = '<h2> Call logs</h2>';

    $sql = "SELECT id,phoneNumber, date, duration, type FROM `callLog` WHERE  `userid`= '$userid'";
    $result = $conn->query($sql);

    while($row = $result->fetch_assoc()){
        $new_array3[] = $row; // Inside while loop
    }


    if ($result->num_rows > 0) {
  function build_table2($array){
    // start table
    $html = '<table border="1" class="styled-table">';
    // header row
    $html .= '<tr>';
        $html .='<th>ID</th>';
        $html .='<th>Phone number</th>';
        $html .='<th>Date</th>';
        $html .='<th>Duration [s]</th>';
        $html .='<th>Type</th>';
    $html .= '</tr>';

    $index=1;
    // data rows
    foreach( $array as $key=>$value){
        $html .= '<tr>';
        
        foreach($value as $key2=>$value2){
            if ($key2== "id"){
                $value2 = $index++;
            }
            $html .= '<td>' . htmlspecialchars($value2) . '</td>';
        }
        $html .= '</tr>';
    }

    // finish table and return it

    $html .= '</table>';
    return $html;
  }

  echo build_table2($new_array3);}
  else {
     echo "<p> NO CALL LOGS RECEIVED FROM DEVICE </p>";
        echo "<p> - Try to refresh page - </p>";
        echo "<p> If that doesn't help, there is no call logs in the device.</p>";
  }
}

// ***************************************************************************Camera**********************************************************************************************************
if ($row1["camera"] == "camera"){
  
    echo $html = '<h2> Photos from camera</h2>';


    $sql = "SELECT id,photoName FROM `camera` WHERE `userid`= '$userid'";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();

    if ($result->num_rows > 0) {

    $photoName = $row["photoName"];
    $photoPath = "../images/".$userid."/".$photoName."";

    echo $html='<img src="'.$photoPath.'" class="styled-image" width="500" height="500" >';}

    else {
         echo "<p> NO PHOTOS RECEIVED FROM DEVICE </p>";
            echo "<p> - Try to refresh page - </p>";
            echo "<p> If that doesn't help, there is no photo take from camera in the device.</p>";
      } 
} 

// ***************************************************************************Contact**********************************************************************************************************
if ($row1["contact"] == "contact"){
 
    echo $html = '<h2> List of contacts</h2>';

    $sql = "SELECT id,name,phoneNumber FROM `contact` WHERE `userid`= '$userid'";
    $result = $conn->query($sql);

    while($row = $result->fetch_assoc()){
        $new_array4[] = $row; // Inside while loop
    }


    if ($result->num_rows > 0) {
  function build_table3($array){
    // start table
    $html = '<table border="1" class="styled-table">';
    // header row
    $html .= '<tr>';
        $html .='<th>ID</th>';
        $html .='<th>Name</th>';
        $html .='<th>Phone number</th>';
    $html .= '</tr>';

    $index=1;
    // data rows
    foreach( $array as $key=>$value){
        $html .= '<tr>';
        
        foreach($value as $key2=>$value2){
            if ($key2== "id"){
                $value2 = $index++;
            }
            $html .= '<td>' . htmlspecialchars($value2) . '</td>';
        }
        $html .= '</tr>';
    }

    // finish table and return it

    $html .= '</table>';
    return $html;
  }

  echo build_table3($new_array4);}
  else{
        echo "<p> NO CONTACTS RECEIVED FROM DEVICE </p>";
        echo "<p> - Try to refresh page - </p>";
        echo "<p> If that doesn't help, there is no contacts saved in the device.</p>";
  }
}

// ***************************************************************************Location**********************************************************************************************************
if ($row1["location"] == "location"){

    echo $html = '<h2> Last known location</h2>';

    $sql = "SELECT id,latitude,longtitude,accuracy,altitude FROM `location` WHERE `userid`= '$userid'";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();

    if ($result->num_rows > 0) {
    $latitude = $row["latitude"];
    $longtitude = $row["longtitude"];

      function build_map($latitude,$longtitude){
        $html ='<div id="mapa" 
        style=
        "width:600px;
        height:400px;
        margin-left: auto;
        margin-right: auto;
        border-collapse: collapse;
        ">
        </div>';

        $html .='<script type="text/javascript">';
        $html .='var stred = SMap.Coords.fromWGS84('.$longtitude.','.$latitude.');';
        $html .='var mapa = new SMap(JAK.gel("mapa"), stred, 13);';
        $html .='mapa.addDefaultLayer(SMap.DEF_BASE).enable();';
        $html .='mapa.addDefaultControls(); ';

        $html .='var layer = new SMap.Layer.Marker();';
        $html .='mapa.addLayer(layer);';
        $html .='layer.enable();';

        $html .='var options = {};';
        $html .='var marker = new SMap.Marker(stred, "myMarker", options);';
        $html .='layer.addMarker(marker);';
        $html .='</script>';
        return $html;
      } 
        echo build_map($latitude,$longtitude);
    }
    else{
        echo "<p> NO LOCATION DATA RECEIVED FROM DEVICE </p>";
        echo "<p> - Try to refresh page - </p>";
        echo "<p> If that doesn't help, there is no location data on the device.</p>";
    }
}

// ***************************************************************************Phone state**********************************************************************************************************
if ($row1["phoneState"] == "phoneState"){

    echo $html = '<h2> Data from SIM card</h2>';

    $sql = "SELECT phoneNumber,dataNetworkType,operator FROM `phoneState` WHERE `userid`= '$userid'";
    $result = $conn->query($sql);

    while($row = $result->fetch_assoc()){
        $new_array5[] = $row; // Inside while loop
    }


    if ($result->num_rows > 0) {
  function build_table4($array){
    // start table
    $html = '<table border="1" class="styled-table">';
    // header row
    $html .= '<tr>';
        $html .='<th>Phone number</th>';
        $html .='<th>Network type</th>';
        $html .='<th>Mobile operator</th>';
    $html .= '</tr>';

    // data rows
    foreach( $array as $key=>$value){
        $html .= '<tr>';
        foreach($value as $key2=>$value2){
            $html .= '<td>' . htmlspecialchars($value2) . '</td>';
        }
        $html .= '</tr>';
    }

    // finish table and return it

    $html .= '</table>';
    return $html;
  }

  echo build_table4($new_array5);
    }
    else{
        echo "<p> NO SIM DATA RECEIVED FROM DEVICE </p>";
        echo "<p> - Try to refresh page - </p>";
        echo "<p> If that doesn't help, there is no SIM data in the device.</p>";
    }
}

// ***************************************************************************storage**********************************************************************************************************
if ($row1["storage"] == "storage"){
    echo $html = '<h2> Images from external storage</h2>';

    $sql = "SELECT id,imagePath FROM `storage` WHERE `userid`= '$userid'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()){
        $photoName = $row["imagePath"];
        $photoPath = "../images/".$userid."/".$photoName.".jpg";

        echo $html='<img src="'.$photoPath.'" class="styled-image" width="500" height="500" ">';
    }
    }
    else{
            echo "<p> NO IMAGES RECEIVED FROM DEVICE </p>";
            echo "<p> - Try to refresh page - </p>";
            echo "<p> If that doesn't help, there is no images in the device.</p>";
        }
}

/***************************************************************************************************************************************************************************************************/

// **** Link to logout ****
echo $html="<p></p>";
echo $html="<a href=logout.php><i class=fas fa-sign-out-alt></i>LOGOUT</a>";

// **** Close connection to MySQL Database ****
$conn->close();
?> 
             