<?php
// Utility sample for Aspicore GSM Tracker
// Show the tracking data from the MySQL database
//
// You can use this sample to see, what data exists in the database for a certain phone
//
// Usage: Go to the URL of this page with your Internet browser,
//        e.g. http://<your_web_server>/queyphone.php?imei=<your_imei>
//
// (c) Aspicore Ltd 2005, www.aspicore.com
//
// Tested with PHP 5.0.3, Windows XP, IIS 5.1, MySQL 4.1.10a-nt and with Windows Server 2003, IIS 6.0
// The mysqli extension of PHP requires version 4.1.3 or above of MySQL
//
// NB. If you are using PHP with IIS, set output_buffering = On in php.ini
// In Windows, remember to enable extension=php_mysqli.dll in php.ini
//             make also sure, that php_mysqli.dll is at location php.ini/extension_dir
//             and that libmySQL.dll is along the system path
// ------------------------------
// Change History:
// 2005-04-01 jje - SQL script created

header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Content-Type: text/html; charset=ISO-8859-1");
header('Content-Disposition: inline; filename="response.html"');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <style type="text/css">
  <!--
  td { font-family: verdana,arial,helvetica,sans-serif; font-size: 9pt;
       border: 1px solid black;
       vertical-align: baseline }
  th { font-family: verdana,arial,helvetica,sans-serif; font-size: 7pt; font-weight: bold;
       border: 1px solid black;
       border-style: solid; border-width: 1px;
       vertical-align: baseline }
  td.small { font-family: verdana,arial,helvetica,sans-serif; font-size: 6pt }
  body         { font-family: Courier New,MONOSPACE; font-size: 9pt }
  base         { font-family: Courier,MONOSPACE; font-size: 10pt }
  table        { border-collapse: collapse; border: solid thin black }
  caption { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold }
  -->
  </style>
  <title>Query phone</title>
</head>
<body>
<?php
// Show time in the response to the HTTP client

$timestamp = gmdate("D, d M Y H:i:s");
echo
"<p>\n" ;
echo
"Report Date:\n";
echo
$timestamp . "\n";
echo
"Greenwich Mean Time (GMT)\n";
echo
"</p>\n" ;

//printf("<p>_SERVER:%s</p>\n", print_r($_SERVER, true));

if (!array_key_exists('imei', $_GET)) {
   
/* Show error information to the HTTP client */
   
printf("<p>You must give an IMEI code as an URL argument</p>\n");
   
printf("<p>Example:</p>\n");
   
printf("<p>http://%s%s?", $_SERVER["HTTP_HOST"], $_SERVER["PHP_SELF"]);
   if (
array_key_exists('QUERY_STRING', $_SERVER)) {
    if (
strlen($_SERVER["QUERY_STRING"]) > 0)
      
printf("%s&", $_SERVER["QUERY_STRING"]);
   }
   
printf("imei=123456789012345</p>\n");
   exit();
}

$imei = $_GET['imei'];

if(!
preg_match("/^\d{15}$/",$imei,$matches)) {
   
/* Show error information to the HTTP client */
   
printf("<p>You gave an illegal IMEI code as an URL argument</p>\n");
   
printf("<p>The IMEI code must be exactly 15 digits.</p>\n");
   
printf("<p>The current imei argument is: %s.</p>\n", $imei);
   exit();
}

printf("<p><b>Selected phone: IMEI %s</b></p>\n", $_GET['imei']);


//------------------------
// Make a SELECT query to the MySQL database
// (Create database 'gsmtrack' first with script GSM_Tracker_MySQL_tab.sql)
//------------------------

// Connecting, selecting database

// ***********************************************************************
// mysqli_connect parameters: host, username, passwd, dbname
$link = mysqli_connect("localhost", "root", "newpwd", "gsmtrack");
// ***********************************************************************

/* check connection */
if (!$link) {
   
printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

//echo 'Connected successfully';

// Performing SQL query

/* Prepare the query */
//$query = 'SELECT * FROM GPS WHERE PHONE = ' . $imei . ' ORDER BY GPSMSGID DESC LIMIT 100';
$query = 'SELECT GPSMSGID, TIME_RECEIVED, STATUS, LATITUDE, LONGITUDE, SPEED_KNOTS, COURSE_DEG, '
       
. 'UTCTIME, UTCDATE, LABEL, REMOTE_IP FROM GPS '
       
. 'WHERE PHONE = ' . $imei
       
. ' ORDER BY GPSMSGID DESC LIMIT 100';
$result = mysqli_query($link, $query);

if (!
$result) {
   
printf("<p>Query failed: %s</p>\n", mysqli_error($link));
   
printf("<p>Tried to execute the following SQL query:</p>\n");
   
printf("<p>%s</p>\n", $query );
   exit();
}

/* determine number of rows result set */
$row_cnt = mysqli_num_rows($result);

if (
$row_cnt === 0) {
    echo
"<p>\n" ;
    echo
"No messages in the database from Aspicore GSM Tracker running on the selected phone.\n";
    echo
"</p>\n" ;
}
else {
    echo
"<p>\n" ;
    echo
"The 100 most recent messages from Aspicore GSM Tracker running on the selected phone.\n";
    echo
"</p>\n" ;

    
// Printing results in HTML
    
echo '<table border="1">' . "\n";

    
// Table header
    
echo "\t<tr>\n";
    
$finfo = mysqli_fetch_fields($result);
    for (
$i=0; $i < count($finfo); $i++) {
        
$fieldname = str_replace ("_", " ", $finfo[$i]->name);    
        echo
"\t\t<th>" . $fieldname . "</th>\n";
    }    
    echo
"\t</tr>\n";

    
// Table content
    
while ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
       echo
"\t<tr>\n";
       foreach (
$line as $col_value) {
           echo
"\t\t<td>$col_value</td>\n";
       }
       echo
"\t</tr>\n";
    }
    echo
"</table>\n";
}

// Free resultset
mysqli_free_result($result);

/* close connection */    
mysqli_close($link);


//------------------------
?>
</body>
</html>