Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
xmlwriter.php
1<?php
2namespace Bitrix\Main;
3
5{
6 private $file = '';
7 private $charset = '';
8 private $tab = 0;
9 private $f = null;
10 private $lowercaseTag = false;
11 private $errors = array();
12
17 public function __construct(array $params)
18 {
19 if (isset($params['file']))
20 {
21 $server = \Bitrix\Main\Application::getInstance()->getContext()->getServer();
22 $this->file = $server->getDocumentRoot() . trim($params['file']);
23 // create new file
24 if (
25 isset($params['create_file']) &&
26 $params['create_file'] === true &&
27 is_writable($this->file)
28 )
29 {
30 unlink($this->file);
31 }
32 }
33 if (isset($params['charset']))
34 {
35 $this->charset = trim($params['charset']);
36 }
37 else
38 {
39 $this->charset = SITE_CHARSET;
40 }
41 if (isset($params['lowercase']) && $params['lowercase'] === true)
42 {
43 $this->lowercaseTag = true;
44 }
45 if (isset($params['tab']))
46 {
47 $this->tab = (int)$params['tab'];
48 }
49 }
50
56 private function prepareTag($tag)
57 {
58 if ($this->lowercaseTag)
59 {
60 $tag = mb_strtolower($tag);
61 }
62 return $tag;
63 }
64
70 private function prepareValue($value)
71 {
72 $value = strtr(
73 $value,
74 array(
75 '<' => '&lt;',
76 '>' => '&gt;',
77 '"' => '&quot;',
78 '\'' => '&apos;',
79 '&' => '&amp;',
80 )
81 );
82 $value = preg_replace('/[\x01-\x08\x0B-\x0C\x0E-\x1F]/', '', $value);
83 return $value;
84 }
85
91 public function writeBeginTag($code)
92 {
93 if ($this->f)
94 {
95 fwrite($this->f, str_repeat("\t", $this->tab) . '<' . $this->prepareTag($code) . '>' . PHP_EOL);
96 $this->tab++;
97 }
98 }
99
105 public function writeEndTag($code)
106 {
107 if ($this->f)
108 {
109 $this->tab--;
110 fwrite($this->f, str_repeat("\t", $this->tab) . '</' . $this->prepareTag($code) . '>' . PHP_EOL);
111 }
112 }
113
120 public function writeFullTag($code, $value)
121 {
122 if ($this->f)
123 {
124 $code = $this->prepareTag($code);
125 fwrite($this->f,
126 str_repeat("\t", $this->tab) .
127 (
128 trim($value) == ''
129 ? '<' . $code . ' />' . PHP_EOL
130 : '<' . $code . '>' .
131 $this->prepareValue($value) .
132 '</' . $code . '>' . PHP_EOL
133 )
134 );
135 }
136 }
137
144 private function addError($message, $code)
145 {
146 $this->errors[] = new Error($message, $code);
147 }
148
153 public function getErrors()
154 {
155 return $this->errors;
156 }
157
162 public function openFile()
163 {
164 if ($this->file == '')
165 {
166 $this->addError('File not accessible.', 'XML_FILE_NOT_ACCESSIBLE');
167 }
168 else
169 {
170 \CheckDirPath($this->file);
171 $newFile = !file_exists($this->file);
172 if (file_exists($this->file) && !is_writable($this->file))
173 {
174 chmod($this->file, BX_FILE_PERMISSIONS);
175 }
176 if (($this->f = fopen($this->file, 'ab')))
177 {
178 chmod($this->file, BX_FILE_PERMISSIONS);
179 if ($newFile)
180 {
181 fwrite($this->f, '<?xml version="1.0" encoding="'. $this->charset .'"?>' . PHP_EOL);
182 }
183 }
184 else
185 {
186 $this->addError('File not accessible.', 'XML_FILE_NOT_ACCESSIBLE');
187 }
188 }
189 }
190
195 public function closeFile()
196 {
197 if ($this->f)
198 {
199 fclose($this->f);
200 }
201 }
202
209 public function writeItem(array $item, $wrapperTag = '')
210 {
211 if ($wrapperTag != '')
212 {
213 $this->writeBeginTag($wrapperTag);
214 }
215 foreach ($item as $tag => $value)
216 {
217 if (is_array($value))
218 {
219 $this->writeItem($value, $tag);
220 }
221 else
222 {
223 $this->writeFullTag($tag, $value);
224 }
225 }
226 if ($wrapperTag != '')
227 {
228 $this->writeEndTag($wrapperTag);
229 }
230 }
231}
writeItem(array $item, $wrapperTag='')
__construct(array $params)
Definition xmlwriter.php:17
writeFullTag($code, $value)