forked from owncloud-archive/maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locationcontroller.php
executable file
·110 lines (99 loc) · 3.49 KB
/
locationcontroller.php
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
<?php
/**
* ownCloud - maps
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Sander Brand <brantje@gmail.com>
* @copyright Sander Brand 2014
*/
namespace OCA\Maps\Controller;
use \OCP\IRequest;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\AppFramework\Http\JSONResponse;
use \OCP\AppFramework\ApiController;
class LocationController extends ApiController {
private $userId;
private $cacheManager;
private $locationManager;
public function __construct($appName, IRequest $request,$locationManager,$userId) {
parent::__construct($appName, $request);
$this->locationManager = $locationManager;
$this->userId = $userId;
}
/**
* CAUTION: the @Stuff turn off security checks, for this page no admin is
* required and no CSRF check. If you don't know what CSRF is, read
* it up in the docs or you might create a security hole. This is
* basically the only required method to add this exemption, don't
* add it to any other method if you don't exactly know what it does
*
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
* @PublicPage
*/
public function update() {
$params = array('user' => $this -> userId);
$location['lat'] = $this->params('lat');
$location['lng'] = $this->params('lon');
if(((string)(float)$this->params('timestamp') === $this->params('timestamp'))) {
if(strtotime(date('d-m-Y H:i:s',$this->params('timestamp'))) === (int)$this->params('timestamp')) {
$location['timestamp'] = (int)$this->params('timestamp');
} elseif(strtotime(date('d-m-Y H:i:s',$this->params('timestamp')/1000)) === (int)floor($this->params('timestamp')/1000)) {
$location['timestamp'] = (int)floor($this->params('timestamp')/1000);
}
} else {
$location['timestamp'] = strtotime($this->params('timestamp'));
}
$location['hdop'] = $this->params('hdop');
$location['altitude'] = $this->params('altitude');
$location['speed'] = $this->params('speed');
$location['device_hash'] = $this->params('hash');
/**
* @TODO check if hash exists
*/
$this->locationManager->save($location);
}
/**
* @NoAdminRequired
*/
public function addDevice(){
$deviceName = $this->params('name');
$hash = uniqid();
$deviceId = $this->locationManager->addDevice($deviceName,$hash,$this->userId);
$response = array('id'=> $deviceId,'hash'=>$hash);
return new JSONResponse($response);
}
/**
* @NoAdminRequired
*/
public function loadDevices(){
$response = $this->locationManager->loadAll($this->userId);
return new JSONResponse($response);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function loadLocations(){
$deviceIds = explode(',',$this->params('devices'));
$from = ($this->params('from')) ? strtotime($this->params('from')) : null;
$till = ($this->params('till')!='') ? strtotime($this->params('till')) : strtotime('now');
$limit = ($this->params('limit')!='') ? (int) $this->params('limit') : 2000;
$response = array();
foreach($deviceIds as $device){
$response[$device] = $this->locationManager->loadHistory($device,$from,$till,$limit);
}
return new JSONResponse($response);
}
/**
* @NoAdminRequired
*/
public function removeDevice(){
$deviceId = $this->params('deviceId');
$response = $this->locationManager->remove($deviceId,$this->userId);
return new JSONResponse($response);
}
}