/
process-pull.php
162 lines (133 loc) · 4.07 KB
/
process-pull.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
<?php
use Flatbase\Flatbase;
use EMFCamp\Medical\ePrf;
use Flatbase\Storage\Filesystem;
/**
* Expects the path to look like:
*
* Path
* |
* ├── combined
* ├── nooks
* | ├── nook1
* | ├── nook2
* | ├── nook3
* ├── stats-data
*/
require 'vendor/autoload.php';
$options = getopt("", [
"input:",
"debug"
]);
if (!isset($options['input'])) {
echo("Need an input argument to continue;");
die;
} elseif (!file_exists($options['input'])) {
echo("Input path doesn't exist");
die;
} else {
$inputPath = $options['input'];
if ($inputPath[strlen($inputPath) - 1] !== "/") {
$inputPath .= "/";
}
}
$debug = false;
if (isset($options['debug'])) {
$debug = true;
}
$prfList = checkForPrfs($inputPath . 'nooks/');
if (count($prfList) === 0) {
echo "No PRFs found in input path";
die;
}
// ok, we have data to deal with, so spin up the flatfile database
$storage = new Filesystem($inputPath . 'stats-data/');
$flatbase = new Flatbase($storage);
$newPath = $inputPath . 'combined/';
foreach ($prfList as $prfPaths) {
$encrypted = new SplFileInfo($prfPaths['encrypted']);
$decrypted = new SplFileInfo($prfPaths['decrypted']);
// move encrypted to combined folder
// but check that it doesn't name clash
$newFile = $newPath . $encrypted->getBasename();
$count = 1;
while(file_exists($newFile)) {
$count++;
$newFile = $newPath . $encrypted->getBasename(".prf") . "(" . $count .").prf";
}
if ($debug) {
echo("Not moving: " . $encrypted->getPathname() . " to " . $newFile . "\n");
} else {
rename($encrypted->getPathname(), $newFile);
}
// build PRF object from decrypted
$prf = new ePrf();
$contents = file_get_contents($decrypted->getPathname());
// get rid of newlines
$contents = str_replace("\n", '', $contents);
// get rid of embedded JPEG
$xml = preg_replace('/\*\*\*JPEG_SIGNATURE\*\*\*.*/', '', $contents);
if ($prf->createFromXML($xml) === false) {
echo("There was an issue with a PRF and it was skipped:\n" . $decrypted->getPathname() . "\n");
continue;
}
if ($debug) {
var_dump($prf);
}
// save PRF object in stats database
if (!$debug) {
$flatbase->insert()->in('prfs')->set([
'id' => $prf->getId(),
'date' => $prf->getDateString(),
'year' => $prf->getYear(),
'month' => $prf->getMonth(),
'day' => $prf->getDay(),
'hour' => $prf->getHour(),
'category' => $prf->getCategory(),
'severity' => $prf->getSeverity(),
'riddor' => $prf->getRiddor(),
'prf' => $prf
])->execute();
}
// remove decrypted
if ($debug) {
echo("Not removing: " . $decrypted->getPathname() . "\n");
} else {
unlink($decrypted->getPathname());
}
}
echo "All PRFs have been processed";
function checkForPrfs($path)
{
// we are expecting the path to have multiple folders, each with
// encrypted and decrypted prfs in it
$prfList = [];
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
if ($fileinfo->isDir()) {
$prfList = array_merge($prfList, getPrfs($fileinfo->getPathname()));
}
}
}
return $prfList;
}
function getPrfs($path)
{
$prfList = [];
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if (strpos($fileinfo->getBasename(), '.prf')) {
$id = str_replace(".prf", "", $fileinfo->getBasename(".txt"));
if (!isset($prfList[$id])) {
$prfList[$id] = [];
}
if ($fileinfo->getExtension() === "txt") {
$prfList[$id]['decrypted'] = $fileinfo->getPathname();
} elseif ($fileinfo->getExtension() === "prf") {
$prfList[$id]['encrypted'] = $fileinfo->getPathname();
}
}
}
return $prfList;
}