-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocation.class.php
executable file
·180 lines (158 loc) · 5.77 KB
/
Location.class.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
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
<?php
require_once("RESTObject.class.php");
require_once 'Measurement.class.php';
class Location extends RESTObject
{
public $latitude = '';
public $longitude = '';
public $name = '';
public $userid = '';
public $rain = Array();
public $mintemp = Array();
function apiLink() {
$useridString = empty($this->userid) ? "" : "/users/".$this->userid;
$linkString = "https://".apiDB::$servername."/".apiDB::dirname().$useridString."/locations/" . $this->id ;
return "<a href=\"".$linkString."\">".$this->name."</a>";
}
public function get_array_instance() {
$array = Array();
$array["latitude"] = $this->latitude;
$array["longitude"] = $this->longitude;
$array["name"] = $this->name;
$array["userid"] = $this->userid;
$array["id"] = $this->id;
$array["rain"] = $this->rain;
$array["mintemp"] = $this->mintemp;
return $array;
}
public function get_array_all() {
if (empty($this->userid)) {
//if ($this->access > 1) {
// return apiDB::getLocations();
//} else {
$user = apiDB::getUserByEmail( $_SERVER['PHP_AUTH_USER'] );
return apiDB::getUserLocations($user->id);
//}
} else {
return apiDB::getUserLocations($this->userid);
}
}
public function put_array($array) {
if ($this->access < 1) {
return "Not authorized to make any updates : guest account";
}
$location = new Location();
if (!empty($array["id"])) {
$location = apiDB::getLocation($array["id"]);
if (empty($location->id)) {
return "Location with id $location->id not found for PUT update";
}
} // otherwise we'll just add a new location
$location->name = empty($array["name"]) ? $location->name : $array["name"];
$location->latitude = empty($array["latitude"]) ? $location->latitude : $array["latitude"];
$location->longitude = empty($array["longitude"]) ? $location->longitude : $array["longitude"];
$location->userid = empty($array["userid"]) ? $location->userid : $array["userid"];
$user = apiDB::getUser($location->userid);
if (($_SERVER['PHP_AUTH_USER'] != $user->email) && ($this->access <= 1)) {
return "Not authorized to update location for User ".$location->userid;
}
if (empty($location->id)) {
return apiDB::addLocation($location);
} else {
return apiDB::updateLocation($location->id, $location);
}
}
public function post_array($array, &$message) {
$location = new Location();
$location->name = $array["name"];
$location->latitude = $array["latitude"];
$location->longitude = $array["longitude"];
$location->userid = $array["userid"];
if (empty($location->userid)) { // add to logged in user by default
$user = apiDB::getUserByEmail($_SERVER['PHP_AUTH_USER']);
$location->userid = $user->id;
} else { // check permission ...
$user = apiDB::getUser($location->userid);
if (($_SERVER['PHP_AUTH_USER'] != $user->email) && ($this->access <= 1)) {
$message = "Not authorized to update location for User ".$location->userid;
return 401;
}
}
if ($this->access < 1) {
$message = "Not authorized to add any locations: guest or disabled account";
return 401;
}
return apiDB::addLocation($location, $message);
}
public function delete_array($array) {
if (!empty($array["id"])) {
$user = apiDB::getUserByLocationId($array["id"]);
if (($_SERVER['PHP_AUTH_USER'] != $user->email) && ($this->access <= 1)) {
return "Not authorized to delete location for User ".$user->id;
}
return apiDB::deleteLocation($array["id"]);
}
return "ERROR: No location ID specified for deletion";
}
public function getInstanceDetails($id) {
$location = empty($this->userid) ? apiDB::getLocation($id) : apiDB::getUserLocation($id, $this->userid);
if (empty($location->id)) {
return self::NO_SUCH_ID;
}
$user = apiDB::getUser($location->userid);
if (($_SERVER['PHP_AUTH_USER'] != $user->email) && ($this->access <= 1)) {
return self::ACCESS_DENIED;
}
$this->latitude = $location->latitude;
$this->longitude = $location->longitude;
$this->name = $location->name;
$this->userid = $location->userid;
$this->id = $location->id;
$this->rain = $location->rain;
$this->mintemp = $location->mintemp;
// Preserving $this->access however, to retain admin rights.
return self::SETUP_OK;
}
/**
* rainfall Endpoint
*/
protected function rain() {
if (empty($this->id)) {
return $this->_response("Location id not set. Cannot run \"rainfall\" without a valid location id.", 404);
}
$rain = new Rain();
$rain->setupClass($this->args, $this->access, $this->extension);
$rain->userid = $this->userid;
$rain->locationid = $this->id;
return $rain->process();
}
/**
* mintemp Endpoint
*/
protected function mintemp() {
if (empty($this->id)) {
return $this->_response("Location id not set. Cannot run \"mintemp\" without a valid location id.", 404);
}
$mintemp = new MinTemp();
$mintemp->setupClass($this->args, $this->access, $this->extension);
$mintemp->userid = $this->userid;
$mintemp->locationid = $this->id;
return $mintemp->process();
}
protected function latestdate() {
if (empty($this->id)) {
return $this->_response("Location id not set. Cannot run \"latestdate\" without a valid location id.", 404);
}
return apiDB::getMaxMeasurementDate($this->id, get_class($this));
}
protected function graph() {
$type = empty($_GET["type"]) ? "rain" : $_GET["type"];
$period = empty($_GET["period"]) ? 30 : $_GET["period"];
$graph = empty($_GET["graph"]) ? "bar" : $_GET["graph"];
if (empty($this->id)) {
return $this->_response("Location id not set. Cannot run \"graph\" without a valid location id.", 404);
}
return $this->display(apiDB::graphData($type, $this->id, $period, $graph, 'Africa/Windhoek'));
}
}
?>