-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathclass-trackserver-owntracks.php
More file actions
247 lines (212 loc) · 6.69 KB
/
Copy pathclass-trackserver-owntracks.php
File metadata and controls
247 lines (212 loc) · 6.69 KB
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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! defined( 'TRACKSERVER_PLUGIN_DIR' ) ) {
exit;
}
require_once TRACKSERVER_PLUGIN_DIR . 'class-trackserver-track.php';
require_once TRACKSERVER_PLUGIN_DIR . 'class-trackserver-location.php';
class Trackserver_Owntracks {
protected static $instance; // Singleton
private $trackserver; // Reference to the calling object
private $user; // WP_User doing the request
private $tbl_tracks;
private $tbl_locations;
/**
* Constructor.
*
* @since 5.0
*/
public function __construct( $trackserver ) {
global $wpdb;
$this->trackserver = $trackserver;
$this->tbl_tracks = $wpdb->prefix . 'ts_tracks';
$this->tbl_locations = $wpdb->prefix . 'ts_locations';
}
/**
* Create a singleton if it doesn't exist and return it.
*
* @since 5.0
*/
public static function get_instance( $trackserver ) {
if ( ! self::$instance ) {
self::$instance = new self( $trackserver );
}
return self::$instance;
}
/**
* Handle a request from OwnTracks
*
* After validating the HTTP basic authentication, a track name is
* constructed from the strftime() format string in the settings and the
* track is created if it doesn't exist. The JSON payload from OwnTracks is
* parsed and the location data is stored. A response containing Locations
* and Cards for Friends is returned to the client.
*
* @since 4.0
*/
public function handle_request() {
// If this returns, we're OK
$this->user = $this->trackserver->validate_http_basicauth( false, 'object' );
$payload = file_get_contents( 'php://input' );
// phpcs:ignore
$json = @json_decode( $payload, true );
//Array
//(
// [_type] => location
// [tid] => te
// [acc] => 21
// [batt] => 24
// [conn] => w
// [lat] => 51.448285
// [lon] => 5.4727492
// [t] => u
// [tst] => 1505766127
//)
//Array
//(
// [_type] => waypoint
// [desc] => Thuis
// [lat] => 51.4481325
// [lon] => 5.47281640625
// [tst] => 1505766855
//)
if ( isset( $json['_type'] ) && $json['_type'] === 'location' ) {
// Largely copied from SendLocation handling
$ts = $json['tst'];
$offset = $this->trackserver->utc_to_local_offset( $ts );
$ts += $offset;
$occurred = date( 'Y-m-d H:i:s', $ts ); // phpcs:ignore
// Get track name from strftime format string
$trackname = Trackserver\strftime( $this->trackserver->options['owntracks_trackname_format'], $ts );
if ( ! empty( $trackname ) ) {
$track = new Trackserver_Track( $this->trackserver, $trackname, $this->user->ID, 'name' );
if ( is_null( $track->id ) ) {
$track->set( 'name', $trackname );
$track->set( 'source', 'OwnTracks' );
$track->save();
if ( empty( $track->id ) ) {
$this->trackserver->http_terminate( 501, 'Database error (track)' );
}
}
$latitude = $json['lat'];
$longitude = $json['lon'];
$now = $occurred;
if ( intval( $track->id ) > 0 ) {
if ( is_numeric( $latitude ) && is_numeric( $longitude ) ) {
$loc = new Trackserver_Location( $this->trackserver, $track->id, $this->user->ID );
$loc->set( 'latitude', $latitude );
$loc->set( 'longitude', $longitude );
$loc->set( 'occurred', $occurred );
if ( is_numeric( $json['alt'] ) ) {
$loc->set( 'altitude', $json['alt'] );
}
if ( $loc->save() ) {
$this->trackserver->calculate_distance( $track->id );
} else {
$this->trackserver->http_terminate( 501, 'Database error' );
}
}
}
}
}
$response = $this->create_response();
$this->trackserver->http_terminate( 200, $response );
}
/**
* Create a response for the OwnTracks request.
*
* For all of our Friends (users that share with us, minus the users that
* we do not follow), construct a location object and a card object, put it
* all in a list and return the result as JSON.
*
* @since 4.1
*/
private function create_response() {
global $wpdb;
$friends_ids = $this->get_friends();
$friends_track_ids = $this->trackserver->get_live_tracks( $friends_ids );
$objects = array();
foreach ( $friends_track_ids as $track_id ) {
// phpcs:disable
$sql = $wpdb->prepare( 'SELECT trip_id, latitude, longitude, altitude, speed, UNIX_TIMESTAMP(occurred) AS tst, t.user_id, t.name, t.distance, t.comment FROM ' .
$this->tbl_locations . ' l INNER JOIN ' . $this->tbl_tracks .
' t ON l.trip_id = t.id WHERE trip_id=%d AND l.hidden = 0 ORDER BY occurred DESC LIMIT 0,1', $track_id
);
$res = $wpdb->get_row( $sql, ARRAY_A );
// phpcs:enable
$friend = get_user_by( 'id', $res['user_id'] );
$tid = $this->get_tid( $friend );
$objects[] = array(
'_type' => 'location',
'lat' => $res['latitude'],
'lon' => $res['longitude'],
'tid' => $tid,
'tst' => $res['tst'],
'topic' => 'owntracks/' . $res['user_id'] . '/mobile',
);
$card = array(
'_type' => 'card',
'name' => $friend->display_name,
'tid' => $tid,
);
$face = $this->get_avatar( $friend );
if ( $face ) {
$card['face'] = $face;
}
$objects[] = $card;
}
return wp_json_encode( $objects );
}
/**
* Get image data for WP user avatar
*
* @since 4.1
*/
private function get_avatar( $user ) {
// TODO: cache the image in usermeta and serve it from there if available
$avatar_data = get_avatar_data( $user->ID, array( 'size' => 40 ) );
$url = $avatar_data['url'] . '&d=404'; // ask for a 404 if there is no image
$options = array(
'httpversion' => '1.1',
'user-agent' => 'WordPress/Trackserver ' . TRACKSERVER_VERSION . '; https://github.com/tinuzz/wp-plugin-trackserver',
);
$response = wp_remote_get( $url, $options );
if ( is_array( $response ) ) {
$rc = (int) wp_remote_retrieve_response_code( $response );
if ( $rc === 200 ) {
return base64_encode( wp_remote_retrieve_body( $response ) );
}
}
return false;
}
/**
* Return a 2 character string, to be used as the Tracker ID (TID) in OwnTracks.
*
* Depending on what data is available, we use the user's first name, last name
* or login name to create the TID.
*
* @since 4.1
*/
private function get_tid( $user ) {
if ( $user->first_name && $user->last_name ) {
$tid = $user->first_name[0] . $user->last_name[0];
} elseif ( $user->first_name ) {
$tid = substr( $user->first_name, 0, 2 );
} elseif ( $user->last_name ) {
$tid = substr( $user->last_name, 0, 2 );
} else {
$tid = substr( $user->user_login, 0, 2 );
}
return $tid;
}
/**
* Return a list of friends' user-IDs
*
* @since 4.1
*/
private function get_friends() {
return $this->trackserver->get_followed_users( $this->user );
}
}