|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2017 Michael Giesler |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Dembelo. |
|
5
|
|
|
* |
|
6
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
|
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU Affero General Public License 3 for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
|
17
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace AdminBundle\Service\TwineImport; |
|
21
|
|
|
|
|
22
|
|
|
use DembeloMain\Service\FileHandler; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class FileCheck |
|
26
|
|
|
*/ |
|
27
|
|
|
class FileCheck |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private const OPENING_STRING = '<tw-storydata '; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param FileHandler $fileHandler |
|
36
|
|
|
* @param string $filename |
|
37
|
|
|
* |
|
38
|
|
|
* @return bool |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \Exception |
|
41
|
|
|
*/ |
|
42
|
6 |
|
public function check(FileHandler $fileHandler, string $filename): bool |
|
43
|
|
|
{ |
|
44
|
6 |
|
$magicStringLength = strlen(self::OPENING_STRING); |
|
45
|
|
|
|
|
46
|
6 |
|
$peekData = $fileHandler->read(1024); |
|
47
|
|
|
|
|
48
|
6 |
|
if (false === $peekData) { |
|
|
|
|
|
|
49
|
|
|
throw new \Exception(sprintf("Failed to read data from file '%s'.", $filename)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
$peekDataLength = strlen($peekData); |
|
53
|
|
|
|
|
54
|
6 |
|
if ($peekDataLength <= 0) { |
|
55
|
1 |
|
throw new \Exception(sprintf("File '%s' seems to be empty.", $filename)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
5 |
|
for ($i = 0; $i < $peekDataLength; ++$i) { |
|
59
|
5 |
|
if ($peekData[$i] === ' ' || |
|
60
|
4 |
|
$peekData[$i] === "\n" || |
|
61
|
4 |
|
$peekData[$i] === "\r" || |
|
62
|
5 |
|
$peekData[$i] === "\t") { |
|
63
|
|
|
// Consume whitespace. |
|
64
|
2 |
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
4 |
|
if ($peekDataLength - $i < $magicStringLength) { |
|
68
|
1 |
|
throw new \Exception(sprintf("File '%s' isn't a Twine archive file.", $filename)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
if (substr($peekData, $i, $magicStringLength) !== self::OPENING_STRING) { |
|
72
|
1 |
|
throw new \Exception(sprintf("File '%s' isn't a Twine archive file.", $filename)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
if ($fileHandler->seek(0) !== 0) { |
|
76
|
|
|
throw new \Exception(sprintf("Couldn't reset reading position after the magic string in the Twine archive file '%s' was checked.", $filename)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
throw new \Exception(sprintf("File '%s' doesn't seem to be a Twine archive file.", $filename)); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|