-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiHelper.php
419 lines (363 loc) · 12.2 KB
/
ApiHelper.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
class LogoGenerator {
public static function claimData($id, $session_id, $image_type, $logo_left, $logo_right, $logo_right_second, $claim_left, $claim_right, $color) {
return [
$id,
$session_id,
$image_type,
$logo_left,
$logo_right,
$logo_right_second,
$claim_left,
$claim_right,
$color
];
}
public static function shareData($id, $session_id, $logo_left, $logo_right, $logo_right_second, $claim_left, $claim_right, $color) {
return [
$id,
$session_id,
$logo_left,
$logo_right,
$logo_right_second,
$claim_left,
$claim_right,
$color
];
}
public static function logoData($id, $session_id, $image_type, $logo_left, $logo_right, $logo_right_second, $color) {
return [
$id,
$session_id,
$image_type,
$logo_left,
$logo_right,
$logo_right_second,
$color
];
}
public static function sessionData($session_id, $time) {
return [
$session_id,
$time
];
}
}
class ApiHelper
{
private $csv_filename_session = 'csv/session_entries.csv';
private $csv_filename_logo = 'csv/logo_entries.csv';
private $csv_filename_claim = 'csv/claim_entries.csv';
private $csv_filename_share = 'csv/share_entries.csv';
private $csv_separator = ';';
// timeout for the api: 30 minutes (1800 seconds)
private $timeout = 1800;
/**
* Generate a random string of 0-9a-zA-Z
*
* @param int $length
* @return string
*/
private function generateRandomString($length = 12) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
/**
* Get Session Timestamps
* ----------------------
* - Get all session timestamps
* - Gett line count of the csv-file with the sessions
*
* @return array
* [
* line_count => x,
* timestamps => [192384932, 129384928, ...]
* ]
*/
private function getSessionTimestamps() {
$handle = fopen('../' . $this->csv_filename_session, "r");
$timestamps = [];
$lineCount = 0;
while(!feof($handle)){
$line = fgets($handle);
if ($lineCount > 0) {
// split at ";" or "\n" (end of $line).
$lineContent = preg_split("/;|\\n/", $line);
if (isset($lineContent[1]) && $lineContent[1]) {
$timestamps[] = intval($lineContent[1]);
}
}
$lineCount++;
}
fclose($handle);
return [
'line_count' => $lineCount,
'timestamps' => $timestamps
];
}
private function countFileLines($filePath) {
$lineCount = 0;
$handle = fopen($filePath, "r");
while(!feof($handle)){
$line = fgets($handle);
$lineCount++;
}
fclose($handle);
return $lineCount;
}
private function countImageTypes($filename) {
$fp = fopen('../' . $filename, "r");
$data = [
'count_svg' => 0,
'count_jpg' => 0,
'count_png' => 0
];
// first line: header data
fgetcsv($fp, 0, $this->csv_separator);
while (($line = fgetcsv($fp, 0, $this->csv_separator)) !== FALSE) {
// update image_type counter
$data['count_' . $line[2]] += 1;
}
fclose($fp);
return $data;
}
/**
* Get record length
* Problem: Not all session-numbers have the same length.
* Look at the last line of the file, read it to find out how long one line is exactly.
* @see https://stackoverflow.com/a/24483261/2241151
*/
private function getRecordLength($file_path) {
$line = '';
$f = fopen($file_path, 'r');
$cursor = -1;
fseek($f, $cursor, SEEK_END);
$char = fgetc($f);
/**
* Trim trailing newline chars of the file
*/
while ($char === "\n" || $char === "\r") {
fseek($f, $cursor--, SEEK_END);
$char = fgetc($f);
}
/**
* Read until the start of file or first newline char
*/
while ($char !== false && $char !== "\n" && $char !== "\r") {
/**
* Prepend the new char
*/
$line = $char . $line;
fseek($f, $cursor--, SEEK_END);
$char = fgetc($f);
}
return strlen($line);
}
/**
* Search in CSV for a special $value.
*
* @param $file string filename & path of the csv file
* @param $value string search-value which should be found
* @param $position integer position of the value
* @return array|false
* array: full csv line
* false: $value is not available.
*/
private function searchInCsv($file, $value, $position = 0) {
$fp = fopen($file, "r");
$recordLength = $this->getRecordLength($file) + 1;
$fileSize = filesize($file);
$cursor = $fileSize;
fseek($fp, $cursor);
while ($cursor >= 0)
{
$record = fgetcsv($fp, 0, $this->csv_separator);
if ($record && isset($record[$position])) {
if ($record[$position] == $value) {
return $record;
}
}
$cursor = $cursor - $recordLength;
fseek($fp, $cursor);
}
fclose($fp);
return false;
}
private function getLatestId($file) {
$file = new SplFileObject($file, 'r');
$file->seek(PHP_INT_MAX);
return $file->key();
}
/**
* Check if the csv_filename_session-file exists already.
*
* @return bool
*/
private function checkIfCsvExists($filename) {
return file_exists($filename);
}
/**
* Prepare all the necessary CSV-Files at the beginning of saving all image entries.
*/
private function prepareCsvFile() {
$csv_header_logo = LogoGenerator::logoData('id', 'session_id', 'image_type', 'logo_left', 'logo_right', 'logo_right_second', 'color');
$csv_header_claim = LogoGenerator::claimData('id', 'session_id', 'image_type', 'logo_left', 'logo_right', 'logo_right_second', 'claim_left', 'claim_right', 'color');
$csv_header_share = LogoGenerator::shareData('id', 'session_id', 'logo_left', 'logo_right', 'logo_right_second', 'claim_left', 'claim_right', 'color');
$csv_header_session = LogoGenerator::sessionData('session_id', 'time');
if (!file_exists('csv')) {
mkdir('csv', 0755, true);
}
if (!file_exists($this->csv_filename_session)) {
$file = new SplFileObject($this->csv_filename_session, 'a');
$file->fputcsv($csv_header_session, $this->csv_separator);
$file = null;
}
if (!file_exists($this->csv_filename_logo)) {
$file = new SplFileObject($this->csv_filename_logo, 'a');
$file->fputcsv($csv_header_logo, $this->csv_separator);
$file = null;
}
if (!file_exists($this->csv_filename_claim)) {
$file = new SplFileObject($this->csv_filename_claim, 'a');
$file->fputcsv($csv_header_claim, $this->csv_separator);
$file = null;
}
if (!file_exists($this->csv_filename_share)) {
$file = new SplFileObject($this->csv_filename_share, 'a');
$file->fputcsv($csv_header_share, $this->csv_separator);
$file = null;
}
}
private function generateId() {
session_start();
$session_id = session_id();
if (strlen($session_id) < 1) {
$session_id = $this->generateRandomString(32);
}
return $session_id . '-' . $this->generateRandomString(8);
}
/**
* Save the id to the sessions-file.
*
* @param $id
*/
private function saveId($id) {
if (!$this->checkIfCsvExists($this->csv_filename_session)) {
$this->prepareCsvFile();
}
$time = time();
$entry = LogoGenerator::sessionData(
$id,
$time
);
$file = new SplFileObject($this->csv_filename_session, 'a');
$file->fputcsv($entry, $this->csv_separator);
$file = null;
}
/**
* Get a new session-id to save to the browser of the current user.
* @return string
*/
function getNewSessionId() {
$id = $this->generateId();
$this->saveId($id);
return $id;
}
/**
* Check if session id is valid:
* - $id is in the entries
* - $timeout is not yet reached
*
* @param $id
* @return bool
*/
function checkSessionId($id) {
// check if the record is found in the csv-file
if (!$record = $this->searchInCsv($this->csv_filename_session, $id)) {
return false;
}
// check if the entry is too old for a new entry in the csv-files.
if ($record[1] + $this->timeout < time()) {
return false;
}
return true;
}
function saveDataLogo($data) {
$id = $this->getLatestId($this->csv_filename_logo);
$logoData = LogoGenerator::logoData(
$id,
$data['session_id'],
$data['image_type'],
$data['logo_left'],
$data['logo_right'],
$data['logo_right_second'],
$data['color']
);
$file = new SplFileObject($this->csv_filename_logo, 'a');
$file->fputcsv($logoData, $this->csv_separator);
$file = null;
}
function saveDataClaim($data) {
$id = $this->getLatestId($this->csv_filename_claim);
$claimData = LogoGenerator::claimData(
$id,
$data['session_id'],
$data['image_type'],
$data['logo_left'],
$data['logo_right'],
$data['logo_right_second'],
$data['claim_left'],
$data['claim_right'],
$data['color']
);
$file = new SplFileObject($this->csv_filename_claim, 'a');
$file->fputcsv($claimData, $this->csv_separator);
$file = null;
}
function saveDataShare($data) {
if (!$this->checkIfCsvExists($this->csv_filename_share)) {
$this->prepareCsvFile();
}
$id = $this->getLatestId($this->csv_filename_share);
$shareData = LogoGenerator::shareData(
$id,
$data['session_id'],
$data['logo_left'],
$data['logo_right'],
$data['logo_right_second'],
$data['claim_left'],
$data['claim_right'],
$data['color']
);
$file = new SplFileObject($this->csv_filename_share, 'a');
$file->fputcsv($shareData, $this->csv_separator);
$file = null;
}
function adminGetVisitors() {
$sessionData = $this->getSessionTimestamps();
$sessionIdCounter = $sessionData['line_count'] - 2;
$claimCounter = $this->countFileLines('../' . $this->csv_filename_claim) - 2;
$logoCounter = $this->countFileLines('../' . $this->csv_filename_logo) - 2;
$shareCounter = $this->countFileLines('../' . $this->csv_filename_share) - 2;
return [
'session' => $sessionIdCounter,
'claim' => $claimCounter,
'logo' => $logoCounter,
'share' => $shareCounter,
'timestamps' => $sessionData['timestamps'],
];
}
function adminGetLogoData() {
$imageTypeLogo = $this->countImageTypes($this->csv_filename_logo);
$imageTypeClaim = $this->countImageTypes($this->csv_filename_claim);
return [
'image_type_logo' => $imageTypeLogo,
'image_type_claim' => $imageTypeClaim,
];
}
}