-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.php
231 lines (185 loc) · 5.67 KB
/
export.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
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
<?php
/**
* Created by Serge Titov for mooServer project
* 2014 - 2015
*/
define('IN_MOOSE', true);
require_once "config.php";
require_once "php/auth.php";
require_once "php/dbase.php";
require_once "php/common.php";
global $auth;
global $db;
$db = new CMooseDb();
$auth = new CMooseAuth($db);
if (!isset($_GET['m']))
die('no method name');
$mName = $_GET['m'];
try
{
if ($mName == 'tracks')
exportTracks();
else if ($mName == 'beacons')
exportBeacons();
else if ($mName == 'activity')
exportActivity();
else if ($mName == 'backup')
exportBackup();
else
die('unknown method');
}
catch (Exception $e)
{
die($e->getMessage()."\n\n".$e->getTraceAsString());
}
// that's all!
function prepareIds()
{
if (!isset($_POST['ids']))
die('not enough params');
$_POST['ids'] = explode(",", $_POST['ids']);
}
function csvHeaders($fName)
{
header("Content-Type: text/csv csv; charset=UTF-8");
header('Content-Disposition: attachment; filename="' . $fName. '"');
return "\xEF\xBB\xBF"; // utf-8 magic bytes
}
function csvTimeHeader()
{
$res = '';
$start = @$_POST['start'];
$end = @$_POST['end'];
if ($start != null)
$res .= "с;" . CMooseTools::csvEscape($start) . ";". CMooseTools::csvEscape(gmdate('Y-m-d H:i:s', strtotime($start))) . "\n";
if ($end != null)
$res .= "по;" . CMooseTools::csvEscape($end) . "\n";
return $res;
}
function mooseName($mooses, $id)
{
foreach($mooses as $m)
if ($m['id'] == $id)
return $m['name'];
return $id;
}
function zuluTime($stamp)
{
return gmdate('Y-m-d\TH:i:s\Z', $stamp);
}
function exportBeacons()
{
prepareIds();
$data = getBeaconData(true);
if ($data == null)
die('no data');
$rows = [];
$phones = [];
foreach($data as $phoneData)
{
$phone = $phoneData['phone'];
if (array_search($phone, $phones) == null)
$phones[] = $phone;
foreach($phoneData['data'] as $row)
$rows[] = CMooseTools::csvEscape($phone, true) .";". implode(";", array_map('CMooseTools::csvEscape', $row));
}
$header = "Экспорт маяков\n";
$header .= csvTimeHeader();
$header .="\nМаяк;Получено;последняя точка;вн. id;V;T C;GPS on мин;GSM tries;rawSms id;текст смс;id животного;кличка";
$fname = count($phones) == 1 ? preg_replace('/[^\dx]/', '', $phones[0]) : 'beacons';
echo csvHeaders("$fname.csv");
echo "$header\n" . implode("\n", $rows);
}
function exportActivity()
{
global $db, $auth;
$moo = $db->GetMooses($auth, false);
prepareIds();
$data = getActivity();
$rows = [];
$names = [];
$qNames = [];
if ($data != null)
foreach($data as $moose)
{
$name = mooseName($moo, $moose['id']);
$names[] = $name;
$name = CMooseTools::csvEscape($name);
$qNames[] = $name;
foreach($moose['activity'] as $mark)
{
$mark[0] = zuluTime($mark[0]); // тайное знание, что 0-эелемент -- unix-timestamp
$rows[] = $name . ";" . implode(";", array_map("CMooseTools::csvEscape", $mark));
}
}
$header = "Экспорт активности для:;" . implode(";", $qNames) . "\n";
$header .= csvTimeHeader();
$header .= "\nживотное;время;активен;валиден";
$fname = implode('_', $names);
if ($fname == '')
$fname = 'activity';
echo csvHeaders("$fname.csv");
echo "$header\n" . implode("\n", $rows);
}
function exportTracks()
{
global $db, $auth;
$moo = $db->GetMooses($auth, false);
prepareIds();
$data = getData(false, true);
if ($data == null)
die('no data');
$tracks = '';
$names = [];
foreach ($data as $track)
{
$pts = '';
foreach($track['track'] as $point)
if ($point[3] == 1) // is valid
$pts.= "<trkpt lat=\"$point[0]\" lon=\"$point[1]\"><time>" . zuluTime($point[2]) . "</time></trkpt>\n";
$moose = mooseName($moo, $track['id']);
$names[] = $moose;
$tracks .= " <trk><name>$moose</name><trkseg>\n$pts</trkseg></trk>";
}
$tm = gmdate('c', time());
$fname = implode('_', $names);
if ($fname == '')
$fname = 'empty';
$start = @$_POST['start'];
$end = @$_POST['end'];
$times = '';
if ($start != null)
$times .= ' с ' . gmdate("m M Y", strtotime($start));
if ($end != null)
$times .= ' по ' . gmdate("m M Y", strtotime($end));
header("Content-Type: application/gpx+xml gpx; charset=UTF-8");
header('Content-Disposition: attachment; filename="'.$fname.'.gpx"');
$header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"MooServer\" version=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">
<metadata>
<name>Треки " . implode(', ', $names) . $times . "</name>
<author>
<name>MooServer</name>
</author>
<time>$tm</time>
</metadata>";
$footer = "</gpx>";
echo "$header\n $tracks\n$footer";
}
function exportBackup()
{
global $auth, $mooSett;
if (!$auth->isSuper())
{
header("Content-Type: text/html html; charset=UTF-8");
die(CTinyDb::ErrCRights);
}
$fname = "mooserver-" . date('Y-m-d--H-i'). ".sql.gz";
header("Content-Type: text/sql sql; charset=UTF-8");
header('Content-Disposition: attachment; filename="' . $fname . '"');
$parts = explode(':', $mooSett['host'], 2);
$host = "-h $parts[0]" . (count($parts) > 1 ? " -P$parts[1]" : '');
$req = sprintf("mysqldump -p%s %s -u%s -E --triggers -R %s version stamp users usergroups moose phone activity logs position raw_sms sms session | gzip -c", $mooSett['pwd'], $host, $mooSett['user'], $mooSett['base']);
echo system($req);
}
?>