-
Notifications
You must be signed in to change notification settings - Fork 5
/
Locory.php
204 lines (170 loc) · 5.49 KB
/
Locory.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
use Location\Coordinate;
use Location\Distance\Vincenty;
class Locory
{
private static $DbConnection;
/**
* @return PDO
*/
public static function GetDbConnection($doMigrations = false)
{
if ($doMigrations === true)
{
self::$DbConnection = null;
}
if (self::$DbConnection == null)
{
self::$DbConnection = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
self::$DbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($doMigrations === true)
{
self::$DbConnection->exec("CREATE TABLE IF NOT EXISTS migrations (migration SMALLINT NOT NULL, execution_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (migration))");
LocoryDbMigrator::MigrateDb(self::$DbConnection);
if (self::IsDemoInstallation())
{
LocoryDemoDataGenerator::PopulateDemoData(self::$DbConnection);
}
}
}
return self::$DbConnection;
}
public static function AddLocationPoint($time, $latitude, $longitude, $accuracy)
{
$db = self::GetDbConnection();
$statement = $db->prepare('INSERT INTO locationpoints (time, latitude, longitude, accuracy) VALUES (:time, :latitude, :longitude, :accuracy)');
$statement->bindValue(':time', $time);
$statement->bindValue(':latitude', $latitude);
$statement->bindValue(':longitude', $longitude);
$statement->bindValue(':accuracy', $accuracy);
$statement->execute();
}
public static function AddCsvData($csvString)
{
$lines = explode(PHP_EOL, $csvString);
foreach ($lines as $line)
{
if (!empty($line))
{
$parsedLine = str_getcsv($line);
self::AddLocationPoint($parsedLine[0], $parsedLine[1], $parsedLine[2], $parsedLine[3]);
}
}
}
public static function GetLocationPoints($from, $to)
{
$db = self::GetDbConnection();
$statement = $db->prepare('SELECT * FROM locationpoints WHERE time >= :from AND time <= :to');
$statement->bindValue(':from', $from);
$statement->bindValue(':to', $to);
$statement->execute();
$rows = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC))
{
$rows[] = $row;
}
return $rows;
}
public static function GetLocationPointStatistics($from, $to)
{
$db = self::GetDbConnection();
$statement = $db->prepare('SELECT MIN(accuracy) AS AccuracyMin, MAX(accuracy) AS AccuracyMax, AVG(accuracy) AS AccuracyAverage, SUM(distance_to_point_before) AS Distance FROM locationpoints WHERE time >= :from AND time <= :to');
$statement->bindValue(':from', $from);
$statement->bindValue(':to', $to);
$statement->execute();
return $statement->fetch(PDO::FETCH_ASSOC);
}
public static function CalculateLocationPointDistances()
{
$db = self::GetDbConnection();
$distanceCalculator = new Vincenty();
$statementNotCalculatedRows = $db->prepare('SELECT id, time, latitude, longitude FROM locationpoints WHERE distance_to_point_before IS NULL ORDER BY time');
$statementNotCalculatedRows->execute();
$idPreviousRow = null;
while ($row = $statementNotCalculatedRows->fetch(PDO::FETCH_ASSOC))
{
if ($idPreviousRow == null)
{
//Try to get the row before, should only happen once when starting a new calculation
$statementRowBefore = $db->prepare('SELECT id, latitude, longitude FROM locationpoints WHERE time < :time ORDER BY time DESC LIMIT 1');
$statementRowBefore->bindValue(':time', $row['time']);
$statementRowBefore->execute();
$rowBefore = $statementRowBefore->fetch(PDO::FETCH_ASSOC);
$idPreviousRow = $rowBefore['id'];
$latitudePreviousRow = $rowBefore['latitude'];
$longitudePreviousRow = $rowBefore['longitude'];
}
if ($idPreviousRow != null)
{
$coordinatePrevious = new Coordinate($latitudePreviousRow, $longitudePreviousRow);
$coordinateCurrent = new Coordinate($row['latitude'], $row['longitude']);
$distance = $distanceCalculator->getDistance($coordinatePrevious, $coordinateCurrent);
$statementUpdate = $db->prepare('UPDATE locationpoints SET distance_to_point_before = :distance WHERE id = :id');
$statementUpdate->bindValue(':distance', $distance);
$statementUpdate->bindValue(':id', $row['id']);
$statementUpdate->execute();
}
$idPreviousRow = $row['id'];
$latitudePreviousRow = $row['latitude'];
$longitudePreviousRow = $row['longitude'];
}
}
/**
* @return boolean
*/
public static function IsDemoInstallation()
{
return file_exists(__DIR__ . '/data/demo.txt');
}
private static $InstalledVersion;
/**
* @return string
*/
public static function GetInstalledVersion()
{
if (self::$InstalledVersion == null)
{
self::$InstalledVersion = file_get_contents(__DIR__ . '/version.txt');
}
return self::$InstalledVersion;
}
/**
* @return boolean
*/
public static function IsValidSession($sessionKey)
{
if ($sessionKey === null || empty($sessionKey))
{
return false;
}
else
{
return file_exists(__DIR__ . "/data/sessions/$sessionKey.txt");
}
}
/**
* @return string
*/
public static function CreateSession()
{
if (!file_exists(__DIR__ . '/data/sessions'))
{
mkdir(__DIR__ . '/data/sessions');
}
$now = time();
foreach (new FilesystemIterator(__DIR__ . '/data/sessions') as $file)
{
if ($now - $file->getCTime() >= 2678400) //31 days
{
unlink(__DIR__ . '/data/sessions/' . $file->getFilename());
}
}
$newSessionKey = uniqid() . uniqid() . uniqid();
file_put_contents(__DIR__ . "/data/sessions/$newSessionKey.txt", '');
return $newSessionKey;
}
public static function RemoveSession($sessionKey)
{
unlink(__DIR__ . "/data/sessions/$sessionKey.txt");
}
}