<?php
/////////////////////////////////////////////////////////////////////
//
// DriveReporterServlet by M.O.B.
// Copyright (C) 2007 by Mikael O. Bonnier, Lund, Sweden.
// License: GNU GPL v3 or later, http://www.gnu.org/licenses/gpl-3.0.txt
// Donations are welcome to PayPal mikaelb@df.lth.se.
// The source code is at <http://www.df.lth.se/~mikaelb/wap/>.
//
// Revision history:
// 2007-Aug-19:     v.0.0.1 Java
// 2009-Jun-13:     v.0.0.2 PHP
//
// Suggestions, improvements, and bug-reports
// are always welcome to:
//                  Mikael Bonnier
//                  Osten Undens gata 88
//                  SE-227 62  LUND
//                  SWEDEN
//
// Or use my electronic addresses:
//     Web: http://www.df.lth.se/~mikaelb/
//     E-mail/MSN: mikaelb@df.lth.se
//     ICQ # 114635318
//     Skype: mikael4u
//              _____
//             /   / \
// ***********/   /   \***********
//           /   /     \
// *********/   /       \*********
//         /   /   / \   \
// *******/   /   /   \   \*******
//       /   /   / \   \   \
// *****/   /   /***\   \   \*****
//     /   /__ /_____\   \   \
// ***/               \   \   \***
//   /_________________\   \   \
// **\                      \  /**
//    \______________________\/
//
// Mikael O. Bonnier
/////////////////////////////////////////////////////////////////////

include "Site.php";
$TP 'DR_'// Table Prefix

header('Content-Type: text/plain; charset=ISO-8859-1');

$drs = new DriveReporterServlet();
$drs->init();
$drs->doGet();

class 
DriveReporterServlet {
    protected 
$stmt;

    function 
init() {
        
$this->stmt = new mysqli(DB_SERVERDB_USERNAMEDB_PASSWORD);
        
$this->stmt->select_db(DB_NAME);
    }

    function 
doGet() {
        
define('CREATE',        741);
        
define('DROP',          149);
        
define('INSERT',        2041);
        
define('PUT_FUELS',     91850);
        
define('PUT_VEHICLES',  918750);
        
define('PUT_CITIES',    91010);
        
define('PUT_COMPANIES'9173920);
        
define('PUT_ERRANDS',   9144210);
        
define('REFUEL',        485);
        
define('DRIVE',         148);
        
define('GET_FUELS',     71850);
        
define('GET_VEHICLES',  718750);
        
define('GET_COUNTRIES'7172140);
        
define('GET_CITIES',    71010);
        
define('GET_COMPANIES'7173920);
        
define('GET_ERRANDS',   7144210);
        
define('GET_PROJECTS',  71946710);
        
define('GET_MILEAGES',  713570);
        
define('GET_SUMMARY',   710334);

        
$cmd 0;
        if (isset(
$_GET[cmd])) {
            
$cmd $_GET[cmd];
        }

        global 
$TP;

        switch (
$cmd) {
        case 
CREATE:
            
$rows $this->executeUpdate("CREATE TABLE {$TP}Countries"
                
"(cid INTEGER NOT NULL,"
                
"country VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,"
                
"PRIMARY KEY (cid));");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}Cities"
                
"(ciid INTEGER NOT NULL,"
                
"city VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,"
                
"cid INTEGER NOT NULL,"
                
"PRIMARY KEY (ciid),"
                
"FOREIGN KEY (cid) REFERENCES {$TP}Countries);");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}Persons"
                
"(pid INTEGER NOT NULL,"
                
"name VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci,"
                
"PRIMARY KEY (pid));");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}Projects"
                
"(prid INTEGER NOT NULL,"
                
"project VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci,"
                
"PRIMARY KEY (prid));");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}PersonProjects"
                
"(prid INTEGER NOT NULL,"
                
"pid INTEGER NOT NULL,"
                
"PRIMARY KEY (prid, pid),"
                
"FOREIGN KEY (prid) REFERENCES {$TP}Projects,"
                
"FOREIGN KEY (pid) REFERENCES {$TP}Persons"
                
");");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}Companies"
                
"(comid INTEGER NOT NULL,"
                
"company VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,"
                
"PRIMARY KEY (comid));");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}Errands"
                
"(eid INTEGER NOT NULL,"
                
"errand VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,"
                
"PRIMARY KEY (eid));");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}Mileages"
                
"(mid INTEGER NOT NULL,"
                
"mileage INTEGER NOT NULL," // öre per mil
                
"PRIMARY KEY (mid));");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}Vehicles"
                
"(vid VARCHAR(7) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,"
                
"PRIMARY KEY (vid));");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}PersonVehicles"
                
"(vid VARCHAR(7) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,"
                
"pid INTEGER NOT NULL,"
                
"PRIMARY KEY (vid, pid),"
                
"FOREIGN KEY (vid) REFERENCES {$TP}Vehicles,"
                
"FOREIGN KEY (pid) REFERENCES {$TP}Persons);");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}Fuels"
                
"(fid INTEGER NOT NULL,"
                
"fuel VARCHAR(15) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,"
                
"PRIMARY KEY (fid));");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}Refuels"
                
"(rid INTEGER NOT NULL,"
                
"vid VARCHAR(7) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,"
                
"pid INTEGER NOT NULL,"
                
"rftime DATETIME,"
                
"price INTEGER," // öre
                
"volume INTEGER," // cl
                
"level INTEGER," // percent
                
"fid INTEGER NOT NULL,"
                
"meter LONG," // 100 m
                
"PRIMARY KEY (rid),"
                
"FOREIGN KEY (vid) REFERENCES {$TP}Vehicles,"
                
"FOREIGN KEY (pid) REFERENCES {$TP}Persons,"
                
"FOREIGN KEY (fid) REFERENCES {$TP}Fuels);");
            
$rows += $this->executeUpdate("CREATE TABLE {$TP}Drives"
                
"(did INTEGER NOT NULL,"
                
"vid VARCHAR(7) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,"
                
"depciid INTEGER NOT NULL,"
                
"depmeter LONG," // 100 m
                
"deptime DATETIME,"
                
"arrciid INTEGER NOT NULL,"
                
"arrmeter LONG," // 100 m
                
"arrtime DATETIME,"
                
"mileage INTEGER," // öre per mil
                
"pid INTEGER NOT NULL,"
                
"prid INTEGER NOT NULL,"
                
"comid INTEGER NOT NULL,"
                
"eid INTEGER NOT NULL,"
                
"comment VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci,"
                
"PRIMARY KEY (did),"
                
"FOREIGN KEY (vid) REFERENCES {$TP}Vehicles,"
                
"FOREIGN KEY (depciid) REFERENCES {$TP}Cities,"
                
"FOREIGN KEY (arrciid) REFERENCES {$TP}Cities,"
                
"FOREIGN KEY (pid) REFERENCES {$TP}Persons,"
                
"FOREIGN KEY (prid) REFERENCES {$TP}Projects,"
                
"FOREIGN KEY (comid) REFERENCES {$TP}Companies,"
                
"FOREIGN KEY (eid) REFERENCES {$TP}Errands);");
            print 
"$rows;\n";
            break;
        case 
DROP:
            
$rows $this->executeUpdate("DROP TABLE {$TP}Drives;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}Refuels;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}Fuels;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}PersonVehicles;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}Vehicles;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}Mileages;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}Errands;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}Companies;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}PersonProjects;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}Projects;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}Persons;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}Cities;");
            
$rows += $this->executeUpdate("DROP TABLE {$TP}Countries;");
            print 
"$rows;\n";
            break;
        case 
INSERT:
            
$rows $this->executeUpdate("INSERT INTO {$TP}Persons (pid, name) VALUES (1, 'Kalle Anka');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Persons (pid, name) VALUES (2, 'Kajsa Anka');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Mileages (mid, mileage) VALUES (1, 1300);");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Vehicles (vid) VALUES ('ABC123');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Vehicles (vid) VALUES ('XYZ789');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}PersonVehicles (vid, pid) VALUES ('ABC123', 1);");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}PersonVehicles (vid, pid) VALUES ('ABC123', 2);");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}PersonVehicles (vid, pid) VALUES ('XYZ789', 2);");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Fuels (fid, fuel) VALUES (95, '95');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Fuels (fid, fuel) VALUES (98, '98');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Fuels (fid, fuel) VALUES (85, 'E85');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Fuels (fid, fuel) VALUES (99, 'V-Power');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Fuels (fid, fuel) VALUES (105, 'Diesel');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Refuels (rid, vid, pid, rftime, price, volume, level, fid, meter)"
                
" VALUES (1, 'ABC123', 1, '2007-07-27 18:55:00', 1100, 500, 100, 98, 78661);");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Countries (cid, country) VALUES (1, 'se');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Countries (cid, country) VALUES (2, 'dk');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Cities (ciid, city, cid) VALUES (1, 'Lund', 1);");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Cities (ciid, city, cid) VALUES (2, 'Göteborg', 1);");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Cities (ciid, city, cid) VALUES (3, 'Köpenhamn', 2);");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Companies (comid, company) VALUES (1, 'Ericsson');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Companies (comid, company) VALUES (2, 'Lunds univ');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Companies (comid, company) VALUES (3, 'Sun');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Errands (eid, errand) VALUES (1, 'Sälja');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Errands (eid, errand) VALUES (2, 'Köpa');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Errands (eid, errand) VALUES (3, 'Serva');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Projects (prid, project) VALUES (123456, 'Terraform Mars');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Projects (prid, project) VALUES (9876, 'Just Reward');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}Projects (prid, project) VALUES (54637, 'Quantum Leap');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}PersonProjects (prid, pid) VALUES (123456, 1);");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}PersonProjects (prid, pid) VALUES (9876, 1);");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}PersonProjects (prid, pid) VALUES (54637, 2);");
            print 
"$rows;\n";
            break;
        case 
PUT_FUELS:
            
$fuel $_GET["fuel"];
            
$rs $this->executeQuery("SELECT MAX(fid) FROM {$TP}Fuels;");
            
$fid 0;
            while (
$row $rs->fetch_row()) {
                
$fid $row[0];
            }
            ++
$fid;
            
$rows $this->executeUpdate("INSERT INTO {$TP}Fuels (fid, fuel) "
                
"VALUES ($fid, '$fuel');");
            print 
"$fid;\n";
            break;
        case 
PUT_VEHICLES:
            
$vid $_GET["vid"];
            
$pid $_GET["pid"];
            
$rows $this->executeUpdate("INSERT INTO {$TP}Vehicles (vid) "
                
"VALUES ('$vid');");
            
$rows += $this->executeUpdate("INSERT INTO {$TP}PersonVehicles (vid, pid) "
                
"VALUES ('$vid', $pid);");
            print 
"$rows;\n";
            break;
        case 
PUT_CITIES:
            
$city $_GET["city"];
            
$cid $_GET["cid"];
            
$rs $this->executeQuery("SELECT MAX(ciid) FROM {$TP}Cities;");
            
$ciid 0;
            while (
$row $rs->fetch_row()) {
                
$ciid $row[0];
            }
            ++
$ciid;
            
$rows $this->executeUpdate("INSERT INTO {$TP}Cities (ciid, city, cid) "
                
"VALUES ($ciid, '$city', $cid);");
            print 
"$ciid;\n";
            break;
        case 
PUT_COMPANIES:
            
$company $_GET["company"];
            
$rs $this->executeQuery("SELECT MAX(comid) FROM {$TP}Companies;");
            
$comid 0;
            while (
$row $rs->fetch_row()) {
                
$comid $row[0];
            }
            ++
$comid;
            
$rows $this->executeUpdate("INSERT INTO {$TP}Companies (comid, company) "
                
"VALUES ($comid, '$company');");
            print 
"$comid;\n";
            break;
        case 
PUT_ERRANDS:
            
$errand $_GET["errand"];
            
$rs $this->executeQuery("SELECT MAX(eid) FROM {$TP}Errands;");
            
$eid 0;
            while (
$row $rs->fetch_row()) {
                
$eid $row[0];
            }
            ++
$eid;
            
$rows $this->executeUpdate("INSERT INTO {$TP}Errands (eid, errand) "
                
"VALUES ($eid, '$errand');");
            print 
"$eid;\n";
            break;
        case 
REFUEL:
            
$vid $_GET["vid"];
            
$pid $_GET["pid"];
            
$rftime floor($_GET["rftime"]/1000);
            
$price $_GET["price"];
            
$volume $_GET["volume"];
            
$level $_GET["level"];
            
$fid $_GET["fid"];
            
$meter $_GET["meter"];
            
$rs $this->executeQuery("SELECT MAX(rid) FROM {$TP}Refuels;");
            
$rid 0;
            while (
$row $rs->fetch_row()) {
                
$rid $row[0];
            }
            ++
$rid;
            
$rows $this->executeUpdate("INSERT INTO {$TP}Refuels (rid, vid, pid, rftime, price, volume, level, fid, meter) "
                
"VALUES ($rid, '$vid', $pid, FROM_UNIXTIME($rftime), $price, $volume, $level, $fid, $meter);");
            print 
"$rows;\n";
            break;
        case 
DRIVE:
            
$vid $_GET["vid"];
            
$depciid $_GET["depciid"];
            
$depmeter $_GET["depmeter"];
            
$deptime floor($_GET["deptime"]/1000);
            
$arrciid $_GET["arrciid"];
            
$arrmeter $_GET["arrmeter"];
            
$arrtime floor($_GET["arrtime"]/1000);
            
$mileage $_GET["mileage"];
            
$pid $_GET["pid"];
            
$prid $_GET["prid"];
            
$comid $_GET["comid"];
            
$eid $_GET["eid"];
            
$comment $_GET["comment"];

            
$rs $this->executeQuery("SELECT MAX(did) FROM {$TP}Drives;");
            
$did 0;
            while (
$row $rs->fetch_row()) {
                
$did $row[0];
            }
            ++
$did;
            
$rows $this->executeUpdate("INSERT INTO {$TP}Drives (did, vid, depciid, depmeter, deptime, arrciid, arrmeter, arrtime, mileage, pid, prid, comid, eid, comment)"
                
" VALUES ($did, '$vid', $depciid, $depmeter,"
                
" FROM_UNIXTIME($deptime), $arrciid, $arrmeter, FROM_UNIXTIME($arrtime),"
                
" $mileage, $pid, $prid, $comid,"
                
" $eid, '$comment');");
            print 
$rows ";\n";
            break;
        case 
GET_FUELS:
            
$rs $this->executeQuery("SELECT fid, fuel FROM {$TP}Fuels;");
            while (
$obj $rs->fetch_object()) {
                
$fid $obj->fid;
                
$fuel $obj->fuel;
                print 
"$fid;";
                print 
"$fuel;\n";
            }
            break;
        case 
GET_VEHICLES:
            
$pid $_GET["pid"];
            
$rs $this->executeQuery("SELECT vid FROM {$TP}PersonVehicles WHERE pid = $pid;");
            while (
$obj $rs->fetch_object()) {
                
$vid $obj->vid;
                print 
"$vid;\n";
            }
            break;
        case 
GET_COUNTRIES:
            
$rs $this->executeQuery("SELECT cid, country FROM {$TP}Countries;");
            while (
$obj $rs->fetch_object()) {
                
$cid $obj->cid;
                
$country $obj->country;
                print 
"$cid;";
                print 
"$country;\n";
            }
            break;
        case 
GET_CITIES:
            
$rs $this->executeQuery("SELECT ciid, city, cid FROM {$TP}Cities;");
            while (
$obj $rs->fetch_object()) {
                
$ciid $obj->ciid;
                
$city $obj->city;
                
$cid $obj->cid;
                print 
"$ciid;";
                print 
"$city;";
                print 
"$cid;\n";
            }
            break;
        case 
GET_COMPANIES:
            
$rs $this->executeQuery("SELECT comid, company FROM {$TP}Companies;");
            while (
$obj $rs->fetch_object()) {
                
$comid $obj->comid;
                
$company $obj->company;
                print 
"$comid;";
                print 
"$company;\n";
            }
            break;
        case 
GET_ERRANDS:
            
$rs $this->executeQuery("SELECT eid, errand FROM {$TP}Errands;");
            while (
$obj $rs->fetch_object()) {
                
$eid $obj->eid;
                
$errand $obj->errand;
                print 
"$eid;";
                print 
"$errand;\n";
            }
            break;
        case 
GET_PROJECTS:
            
$pid $_GET["pid"];
            
$rs $this->executeQuery("SELECT prid FROM {$TP}PersonProjects WHERE pid=$pid;");
            while (
$obj $rs->fetch_object()) {
                
$prid $obj->prid;
                print 
"$prid;\n";
            }
            break;
        case 
GET_MILEAGES:
            
$rs $this->executeQuery("SELECT mid, mileage FROM {$TP}Mileages ORDER BY mid DESC;");
            while (
$obj $rs->fetch_object()) {
                
$mileage $obj->mileage;
                print 
"$mileage;";
                break;
            }
            print 
"\n";
            break;
        case 
GET_SUMMARY:
            
$pid $_GET["pid"];
            
$rs $this->executeQuery("SELECT YEAR(deptime) AS year, MONTH(deptime) AS month, SUM((arrmeter-depmeter)/10) AS distance, SUM(mileage*(arrmeter-depmeter)/10000) AS cost"
                
" FROM {$TP}Drives"
                
" WHERE pid=$pid"
                
" GROUP BY YEAR(deptime), MONTH(deptime);");
            while (
$obj $rs->fetch_object()) {
                
$year $obj->year;
                print 
"$year;";
                
$month $obj->month;
                print 
"$month;";
                
$distance $obj->distance// km
                
print "$distance;";
                
$cost $obj->cost// kr
                
print "$cost;";
                break;
            }
            print 
"\n";
            break;
        default:
            print 
"WARNING: All cracking attempts are automatically logged!\n";
            break;
        }
        if (
$rs)
            
$rs->close();
        
$this->stmt->close();
    }

    function 
executeUpdate($sSql) {
        
//    $rows = -1;
        
$res $this->stmt->query($sSql);
        if (
strncmp($sSql'INSERT'6) == 0)
            
$rows $this->stmt->affected_rows;
        else
            
$rows $res 0;
        return 
$rows;
    }

    function 
executeQuery($sSql) {
        
//    $rs = null;
        
$rs $this->stmt->query($sSql);
        return 
$rs;
    }

    function 
getServletInfo() {
        return 
"DriveReporterServlet";
    }
}
?>