Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
archive.php
1<?php
3
4use \Bitrix\Main\Context;
5use \Bitrix\Main\Loader;
6
7class Archive
8{
13 protected $name;
14
19 protected $entries = [];
20
25 public function __construct($name)
26 {
27 $this->name = $name;
28 }
29
34 public function addEntry($archiveEntry)
35 {
36 if ($archiveEntry instanceof ArchiveEntry)
37 {
38 $this->entries[] = $archiveEntry;
39 }
40 }
41
46 public function isEmpty()
47 {
48 return empty($this->entries);
49 }
50
55 protected function getFileList()
56 {
57 $list = [];
58 foreach ($this->entries as $entry)
59 {
60 $list[] = (string)$entry;
61 }
62 unset($entry);
63
64 return implode("\n", $list);
65 }
66
72 protected function addHeaders()
73 {
74 $httpResponse = Context::getCurrent()->getResponse();
75 $httpResponse->addHeader('X-Archive-Files', 'zip');
76
77 $utfName = \CHTTP::urnEncode($this->name, 'UTF-8');
78 $translitName = \CUtil::translit($this->name, LANGUAGE_ID, [
79 'max_len' => 1024,
80 'safe_chars' => '.',
81 'replace_space' => '-',
82 ]);
83 $httpResponse->addHeader(
84 'Content-Disposition',
85 "attachment; filename=\"" . $translitName . "\"; filename*=utf-8''" . $utfName
86 );
87
88 unset($utfName, $translitName, $httpResponse);
89 }
90
95 public function send()
96 {
97 if (!$this->isEmpty())
98 {
99 $this->disableCompression();
100 $this->addHeaders();
101 Context::getCurrent()->getResponse()->flush(
102 $this->getFileList()
103 );
104 }
105 }
106
110 protected function disableCompression()
111 {
112 if (Loader::includeModule('compression'))
113 {
114 \CCompress::disableCompression();
115 }
116 }
117}
static getCurrent()
Definition context.php:241