Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
archive.php
1<?php
3
7
8class Archive extends HttpResponse
9{
10 public const MOD_ZIP_HEADER_NAME = 'X-Archive-Files';
15 protected string $name;
16
21 protected array $entries = [];
22
27 public function __construct(string $name)
28 {
29 parent::__construct();
30
31 $this->name = $name;
32 $this->addHeader(self::MOD_ZIP_HEADER_NAME, 'zip');
33 }
34
39 public function addEntry($archiveEntry): void
40 {
41 if ($archiveEntry instanceof ArchiveEntry)
42 {
43 $this->entries[] = $archiveEntry;
44 }
45 elseif ($archiveEntry instanceof EntryInterface)
46 {
47 $this->entries[] = $archiveEntry;
48 }
49 }
50
51 private function convertEntryInterfaceToString(EntryInterface $entry): string
52 {
53 $crc32 = ($entry->getCrc32() !== '') ? $entry->getCrc32() : '-';
54 $name = Encoding::convertEncoding(
55 $entry->getPath(),
56 LANG_CHARSET,
57 'UTF-8'
58 );
59
60 return "{$crc32} {$entry->getSize()} {$entry->getServerRelativeUrl()} {$name}";
61 }
62
67 public function isEmpty(): bool
68 {
69 return empty($this->entries);
70 }
71
76 protected function getFileList(): string
77 {
78 $list = [];
79 foreach ($this->entries as $entry)
80 {
81 if ($entry instanceof ArchiveEntry)
82 {
83 $list[] = (string)$entry;
84 }
85 elseif ($entry instanceof EntryInterface)
86 {
87 $list[] = $this->convertEntryInterfaceToString($entry);
88 }
89 }
90
91 return implode("\n", $list);
92 }
93
94 protected function setContentDispositionHeader(): void
95 {
96 $utfName = Uri::urnEncode($this->name, 'UTF-8');
97 $translitName = \CUtil::translit($this->name, LANGUAGE_ID, [
98 'max_len' => 1024,
99 'safe_chars' => '.',
100 'replace_space' => '-',
101 ]);
102 $this->addHeader(
103 'Content-Disposition',
104 "attachment; filename=\"{$translitName}\"; filename*=utf-8''{$utfName}"
105 );
106 }
107
108 public function send(): void
109 {
110 if (!$this->isEmpty())
111 {
113 $this->setContent(
114 $this->getFileList()
115 );
116 }
117
118 parent::send();
119 }
120}
addHeader($name, $value='')
setContent($content)
Definition response.php:31
getPath()
getCrc32()