1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\PluginBundle\XApi\Importer; |
8
|
|
|
|
9
|
|
|
use DocumentManager; |
10
|
|
|
use Exception; |
11
|
|
|
use PclZip; |
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ZipImporter. |
16
|
|
|
*/ |
17
|
|
|
class ZipPackageImporter extends PackageImporter |
18
|
|
|
{ |
19
|
|
|
public function import(): string |
20
|
|
|
{ |
21
|
|
|
$zipFile = new PclZip($this->packageFileInfo['tmp_name']); |
22
|
|
|
$zipContent = $zipFile->listContent(); |
23
|
|
|
|
24
|
|
|
$packageSize = array_reduce( |
25
|
|
|
$zipContent, |
26
|
|
|
function ($accumulator, $zipEntry) { |
27
|
|
|
if (preg_match('~.(php.*|phtml)$~i', $zipEntry['filename'])) { |
28
|
|
|
throw new Exception("File \"{$zipEntry['filename']}\" contains a PHP script"); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (\in_array($zipEntry['filename'], ['tincan.xml', 'cmi5.xml'])) { |
32
|
|
|
$this->packageType = explode('.', $zipEntry['filename'], 2)[0]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $accumulator + $zipEntry['size']; |
36
|
|
|
} |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
if (empty($this->packageType)) { |
40
|
|
|
throw new Exception('Invalid package'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->validateEnoughSpace($packageSize); |
44
|
|
|
|
45
|
|
|
$pathInfo = pathinfo($this->packageFileInfo['name']); |
46
|
|
|
|
47
|
|
|
$packageDirectoryPath = $this->generatePackageDirectory($pathInfo['filename']); |
48
|
|
|
|
49
|
|
|
$zipFile->extract($packageDirectoryPath); |
50
|
|
|
|
51
|
|
|
return "$packageDirectoryPath/{$this->packageType}.xml"; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws Exception |
56
|
|
|
*/ |
57
|
|
|
protected function validateEnoughSpace(int $packageSize): void |
58
|
|
|
{ |
59
|
|
|
$courseSpaceQuota = DocumentManager::get_course_quota($this->course->getCode()); |
60
|
|
|
|
61
|
|
|
if (!enough_size($packageSize, $this->courseDirectoryPath, $courseSpaceQuota)) { |
|
|
|
|
62
|
|
|
throw new Exception('Not enough space to storage package.'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function generatePackageDirectory(string $name): string |
67
|
|
|
{ |
68
|
|
|
$directoryPath = implode( |
69
|
|
|
'/', |
70
|
|
|
[ |
71
|
|
|
$this->courseDirectoryPath, |
72
|
|
|
$this->packageType, |
73
|
|
|
api_replace_dangerous_char($name), |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$fs = new Filesystem(); |
78
|
|
|
$fs->mkdir( |
79
|
|
|
$directoryPath, |
80
|
|
|
api_get_permissions_for_new_directories() |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return $directoryPath; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|