summaryrefslogtreecommitdiff
path: root/requests.php
blob: b112412f20c93871dd01d763b673f48fb6759193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/* phpTrackme
 *
 * Copyright(C) 2013 Bartek Fabiszewski (www.fabiszewski.net)
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
// TrackMe API
// http://forum.xda-developers.com/showpost.php?p=3250539&postcount=2

require_once("config.php");
$user = (isset($_REQUEST['u']) ? $_REQUEST['u'] : "");
$pass = (isset($_REQUEST['p']) ? md5($salt.$_REQUEST['p']) : "");
$requireddb = (isset($_REQUEST['db']) ? $_REQUEST['db'] : 0);
$tripname = (isset($_REQUEST['tn']) ? $_REQUEST['tn'] : "");
$action = (isset($_REQUEST['a']) ? $_REQUEST['a'] : "");

// If the client uses Backitude then define the tripname as user-date
if ($requireddb == 'backitude') {
   $tripname = $user.'-'.date("Ymd");
}
// FIXME what is it for?
elseif ($requireddb<8) {
  //Result:5 Incompatible database.
  quit(5);
}

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
  //Result:4 Unable to connect database.
  quit(4);
}

if ((!$user) || (!$pass)){
  //Result:3 User or password not specified.
  quit(3);
}

$query = $mysqli->prepare("SELECT ID,username,password FROM users WHERE username=? LIMIT 1");
$query->bind_param('s', $user);
$query->execute();
$query->store_result();
$query->bind_result($userid, $rec_user, $rec_pass);
$query->fetch();
$num = $query->num_rows;
$query->free_result();
$query->close();
if ($num) {
  if (($user==$rec_user) && ($pass!=$rec_pass)) {
    //Result:1 User correct, invalid password.
    quit(1);
  }
}
else {
  if ($allow_registration) {
    // User unknown, let's create it
    $query = $mysqli->prepare("INSERT INTO users (username,password) VALUES (?,?)");
    $query->bind_param('ss', $user, $pass);
    $query->execute();
    $userid = $mysqli->insert_id;
    $query->close();
    if (!$userid) {
      //Result:2 User did not exist but after being created couldn't be found.
      // Or rather something went wrong while updating database
      quit(2);
    }
  }
  else {
    // User unknown, we don't allow autoregistration
    // Let's use this one:
    //Result:1 User correct, invalid password.
    quit(1);
  }
}

switch($action) {
  // action: noop
  case "noop":
    // test
    quit(0);
    break;

  // action: deletetrip
  case "deletetrip":
    if ($tripname) {
      $sql = "DELETE FROM positions LEFT JOIN trips ON positions.FK_Trips_ID=trips.ID "
            ."WHERE positions.FK_Users_ID=? AND trips.Name=?";
      $query = $mysqli->prepare($sql);
      if ($query) {
        $query->bind_param('is', $userid, $tripname);
        $query->execute();
        $query->close();
      }
      $sql = "DELETE FROM trips WHERE FK_Users_ID=? AND Name=?";
      $query = $mysqli->prepare($sql);
      $query->bind_param('is', $userid, $tripname);
      $query->execute();
      $rows = $mysqli->affected_rows;
      $query->close();
      if ($rows) {
        quit(0);
      }
      else {
        //Result:7 Trip not found
        quit(7);
      }
    }
    else {
      //Result:6 Trip not specified.
      quit(6);
    }
    break;

  // action: addtrip
  case "addtrip":
    if ($tripname) {
      $sql = "INSERT INTO trips (FK_Users_ID,Name) VALUES (?,?)";
      $query = $mysqli->prepare($sql);
      $query->bind_param('is', $userid, $tripname);
      $query->execute();
      $query->close();
    }
    else {
      //Result:6 Trip not specified.
      quit(6);
    }
    break;

  // action: gettriplist
  case "gettriplist":
    $sql = "SELECT a1.Name,(SELECT MIN(a2.DateOccurred) FROM positions a2 "
        ."WHERE a2.FK_Trips_ID=a1.ID) AS startdate "
        ."FROM trips a1 WHERE a1.FK_Users_ID=? ORDER BY Name";
    $query = $mysqli->prepare($sql);
    $query->bind_param('i', $userid);
    $query->execute();
    $query->store_result();
    $query->bind_result($tripname,$startdate);
    $num = $query->num_rows;
    $triplist = array();
    if ($num) {
      while ($query->fetch()) {
        $triplist[] = $tripname."|".$startdate;
      }
    }
    $query->free_result();
    $query->close();
    $param = implode("\n",$triplist);
    quit(0,$param);
    break;

  // action: upload
  case "upload":
    $lat = isset($_REQUEST["lat"]) ? $_REQUEST["lat"] : NULL;
    $long = isset($_REQUEST["long"]) ? $_REQUEST["long"] : NULL;
    // If the client uses Backitude then convert the date into handled format
    $dateoccurred = isset($_REQUEST["do"]) ? $_REQUEST["do"] : NULL;
    $altitude = isset($_REQUEST["alt"]) ? $_REQUEST["alt"] : NULL;
    $angle = isset($_REQUEST["ang"]) ? $_REQUEST["ang"] : NULL;
    $speed = isset($_REQUEST["sp"]) ? $_REQUEST["sp"] : NULL;
    $iconname = isset($_REQUEST["iconname"]) ? $_REQUEST["iconname"] : NULL;
    $comments = isset($_REQUEST["comments"]) ? $_REQUEST["comments"] : NULL;
    $imageurl = isset($_REQUEST["imageurl"]) ? $_REQUEST["imageurl"] : NULL;
    $cellid = isset($_REQUEST["cid"]) ? $_REQUEST["cid"] : NULL;
    $signalstrength = isset($_REQUEST["ss"]) ? $_REQUEST["ss"] : NULL;
    $signalstrengthmax = isset($_REQUEST["ssmax"]) ? $_REQUEST["ssmax"] : NULL;
    $signalstrengthmin = isset($_REQUEST["ssmin"]) ? $_REQUEST["ssmin"] : NULL;
    $batterystatus = isset($_REQUEST["bs"]) ? $_REQUEST["bs"] : NULL;
    $uploadss = isset($_REQUEST["upss"]) ? $_REQUEST["upss"] : NULL; // FIXME is it needed?
    $iconid = NULL;
    if ($iconname) {
      $sql = "SELECT ID FROM icons WHERE Name=? LIMIT 1";
      $query = $mysqli->prepare($sql);
      $query->bind_param('s', $iconname);
      $query->execute();
      $query->store_result();
      $query->bind_result($id);
      $query->fetch();
      $num = $query->num_rows;
      $query->free_result();
      $query->close();
      if ($num) {
        $iconid = $id;
      }
    }
    $tripid = NULL; // FIXME: not sure what trips with null id are
    if ($tripname) {
      // get tripid
      $query = $mysqli->prepare("SELECT ID FROM trips WHERE FK_Users_ID=? AND Name=? LIMIT 1");
      $query->bind_param('is', $userid, $tripname);
      $query->execute();
      $query->store_result();
      $query->bind_result($tripid);
      $query->fetch();
      $num = $query->num_rows;
      $query->free_result();
      $query->close();
      if (!$num) {
        // create trip
        $query = $mysqli->prepare("INSERT INTO trips (FK_Users_ID,Name) VALUES (?,?)");
        $query->bind_param('is', $userid, $tripname);
        $query->execute();
        $tripid = $mysqli->insert_id;
        $query->close();
        if (!$tripid) {
          //Result:6 Trip didn't exist and system was unable to create it.
          quit(6);
        }
      }
    }
     if ($requireddb == 'backitude') {
         $sql = "INSERT INTO positions "
               ."(FK_Users_ID,FK_Trips_ID,Latitude,Longitude,DateOccurred,FK_Icons_ID,"
               ."Speed,Altitude,Comments,ImageURL,Angle,SignalStrength,SignalStrengthMax,"
               ."SignalStrengthMin,BatteryStatus) VALUES (?,?,?,?,FROM_UNIXTIME(?),?,?,?,?,?,?,?,?,?,?)";
     } else {
         $sql = "INSERT INTO positions "
               ."(FK_Users_ID,FK_Trips_ID,Latitude,Longitude,DateOccurred,FK_Icons_ID,"
               ."Speed,Altitude,Comments,ImageURL,Angle,SignalStrength,SignalStrengthMax,"
               ."SignalStrengthMin,BatteryStatus) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
     }

    $query = $mysqli->prepare($sql);
    $query->bind_param('iiddsiddssdiiii',
            $userid,$tripid,$lat,$long,$dateoccurred,$iconid,
            $speed,$altitude,$comments,$imageurl,$angle,$signalstrength,$signalstrengthmax,
            $signalstrengthmin,$batterystatus);
    $query->execute();
    $query->close();
    if ($mysqli->errno) {
      //Result:7|SQLERROR   Insert statement failed.
      quit(7,$mysqli->error);
    }
    //FIXME Are cellids used in Android client?
    $upcellext = isset($_REQUEST["upcellext"]) ? $_REQUEST["upcellext"] : NULL;
    if ($upcellext==1 && $cellid) {
      $sql = "INSERT INTO cellids (CellID,Latitude,Longitude,SignalStrength,SignalStrengthMax,SignalStrengthMin) "
            ."VALUES (?,?,?,?,?,?)";
      $query = $mysqli->prepare($sql);
      $query->bind_param('sddiii',$cellid,$lat,$long,$signalstrength,$signalstrengthmax,$signalstrengthmin);
      $query->execute();
      $query->close();
      if ($mysqli->errno) {
        //Result:7|SQLERROR   Insert statement failed.
        quit(7,$mysqli->error);
      }
    }
    quit(0);
    break;

  // action: geticonlist
  // action: renametrip
  // action: findclosestbuddy
  // action: delete
  // action: sendemail
  // action: updateimageurl
  // action: findclosestpositionbytime
  // action: findclosestpositionbyposition
  // action: gettripinfo
  // action: gettriphighlights
}

function quit($errno,$param=""){
  print "Result:".$errno.(($param)?"|$param":"");
  exit();
}

$mysqli->close();
?>