PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.1.5
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.1.5
trunk 0.9.0 0.9.1 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wp-all-export / classes / chunk.php
wp-all-export / classes Last commit date
CdataStrategy.php 8 years ago CdataStrategyAlways.php 8 years ago CdataStrategyFactory.php 8 years ago CdataStrategyIllegalCharacters.php 8 years ago CdataStrategyIllegalCharactersHtmlEntities.php 8 years ago CdataStrategyNever.php 8 years ago XMLWriter.php 8 years ago chunk.php 8 years ago config.php 8 years ago download.php 8 years ago handler.php 8 years ago helper.php 8 years ago input.php 8 years ago installer.php 8 years ago session.php 8 years ago wpallimport.php 8 years ago zip.php 8 years ago
chunk.php
275 lines
1 <?php
2 /**
3 * Chunk
4 *
5 * Reads a large file in as chunks for easier parsing.
6 *
7 *
8 * @package default
9 * @author Max Tsiplyakov
10 */
11 class PMXE_Chunk {
12 /**
13 * options
14 *
15 * @var array Contains all major options
16 * @access public
17 */
18 public $options = array(
19 'path' => './', // string The path to check for $file in
20 'element' => '', // string The XML element to return
21 'type' => 'upload',
22 'encoding' => 'UTF-8',
23 'pointer' => 1,
24 'chunkSize' => 1024,
25 'filter' => true,
26 'get_cloud' => false
27 );
28
29 /**
30 * file
31 *
32 * @var string The filename being read
33 * @access public
34 */
35 public $file = '';
36 /**
37 * pointer
38 *
39 * @var integer The current position the file is being read from
40 * @access public
41 */
42 public $reader;
43 public $cloud = array();
44 public $loop = 1;
45
46 /**
47 * handle
48 *
49 * @var resource The fopen() resource
50 * @access private
51 */
52 private $handle = null;
53 /**
54 * reading
55 *
56 * @var boolean Whether the script is currently reading the file
57 * @access private
58 */
59
60 /**
61 * __construct
62 *
63 * Builds the Chunk object
64 *
65 * @param string $file The filename to work with
66 * @param array $options The options with which to parse the file
67 * @author Dom Hastings
68 * @access public
69 */
70 public function __construct($file, $options = array()) {
71
72 // merge the options together
73 $this->options = array_merge($this->options, (is_array($options) ? $options : array()));
74
75 $this->options['chunkSize'] *= 32;
76
77 // set the filename
78 $this->file = $file;
79
80 $is_html = false;
81 $f = @fopen($file, "rb");
82 while (!@feof($f)) {
83 $chunk = @fread($f, 1024);
84 if (strpos($chunk, "<!DOCTYPE") === 0) $is_html = true;
85 break;
86 }
87 @fclose($f);
88
89 if ($is_html) return;
90
91 if (empty($this->options['element']) or $this->options['get_cloud'])
92 {
93 if (function_exists('stream_filter_register') and $this->options['filter']){
94 stream_filter_register('preprocessxml', 'wpae_preprocessXml_filter');
95 $path = 'php://filter/read=preprocessxml/resource=' . $this->file;
96 }
97 else $path = $this->file;
98
99 $reader = new XMLReader();
100 $reader->open($path);
101 $reader->setParserProperty(XMLReader::VALIDATE, false);
102 while ( @$reader->read()) {
103 switch ($reader->nodeType) {
104 case (XMLREADER::ELEMENT):
105 $localName = str_replace("_colon_", ":", $reader->localName);
106 if (array_key_exists(str_replace(":", "_", $localName), $this->cloud))
107 $this->cloud[str_replace(":", "_", $localName)]++;
108 else
109 $this->cloud[str_replace(":", "_", $localName)] = 1;
110 break;
111 default:
112
113 break;
114 }
115 }
116 unset($reader);
117
118 if ( ! empty($this->cloud) and empty($this->options['element']) ){
119
120 arsort($this->cloud);
121
122 $main_elements = array('node', 'product', 'job', 'deal', 'entry', 'item', 'property', 'listing', 'hotel', 'record', 'article', 'post', 'book');
123
124 foreach ($this->cloud as $element_name => $value) {
125 if ( in_array(strtolower($element_name), $main_elements) ){
126 $this->options['element'] = $element_name;
127 break;
128 }
129 }
130
131 if (empty($this->options['element'])){
132 foreach ($this->cloud as $el => $count) {
133 $this->options['element'] = $el;
134 break;
135 }
136 }
137 }
138 }
139
140 if (function_exists('stream_filter_register') and $this->options['filter']){
141 stream_filter_register('preprocessxml', 'wpae_preprocessXml_filter');
142 $path = 'php://filter/read=preprocessxml/resource=' . $this->file;
143 }
144 else $path = $this->file;
145
146 $this->reader = new XMLReader();
147 @$this->reader->open($path);
148 @$this->reader->setParserProperty(XMLReader::VALIDATE, false);
149
150
151 }
152
153 /**
154 * __destruct
155 *
156 * Cleans up
157 *
158 * @return void
159 * @author Dom Hastings
160 * @access public
161 */
162 public function __destruct() {
163 // close the file resource
164 unset($this->reader);
165 }
166
167 /**
168 * read
169 *
170 * Reads the first available occurence of the XML element $this->options['element']
171 *
172 * @return string The XML string from $this->file
173 * @author Dom Hastings
174 * @access public
175 */
176 public function read($debug = false) {
177
178 // trim it
179 $element = trim($this->options['element']);
180
181 $xml = '';
182
183 try {
184 while ( @$this->reader->read() ) {
185 switch ($this->reader->nodeType) {
186 case (XMLREADER::ELEMENT):
187 $localName = str_replace("_colon_", ":", $this->reader->localName);
188
189 if ( strtolower(str_replace(":", "_", $localName)) == strtolower($element) ) {
190
191 if ($this->loop < $this->options['pointer']){
192 $this->loop++;
193 continue;
194 }
195
196 $xml = @$this->reader->readOuterXML();
197
198 break(2);
199 }
200 break;
201 default:
202 // code ...
203 break;
204 }
205 }
206 } catch (XmlImportException $e) {
207 $xml = false;
208 }
209
210 return ( ! empty($xml) ) ? $this->removeColonsFromRSS(preg_replace('%xmlns.*=\s*([\'"&quot;]).*\1%sU', '', $xml)) : false;
211
212 }
213
214 function removeColonsFromRSS($feed) {
215
216 $feed = str_replace("_colon_", ":", $feed);
217
218 // pull out colons from start tags
219 // (<\w+):(\w+>)
220 $pattern = '/(<\w+):([\w+|\.|-]+[ |>]{1})/i';
221 $replacement = '$1_$2';
222 $feed = preg_replace($pattern, $replacement, $feed);
223 // pull out colons from end tags
224 // (<\/\w+):(\w+>)
225 $pattern = '/(<\/\w+):([\w+|\.|-]+>)/i';
226 $replacement = '$1_$2';
227 $feed = preg_replace($pattern, $replacement, $feed);
228 // pull out colons from attributes
229 $pattern = '/(\s+\w+):(\w+[=]{1})/i';
230 $replacement = '$1_$2';
231 $feed = preg_replace($pattern, $replacement, $feed);
232 // pull colons from single element
233 // (<\w+):(\w+\/>)
234 $pattern = '/(<\w+):([\w+|\.|-]+\/>)/i';
235 $replacement = '$1_$2';
236 $feed = preg_replace($pattern, $replacement, $feed);
237
238 $is_preprocess_enabled = apply_filters('is_xml_preprocess_enabled', true);
239 if ($is_preprocess_enabled)
240 {
241 // replace temporary word _ampersand_ back to & symbol
242 $feed = str_replace("_ampersand_", "&", $feed);
243 }
244
245 return $feed;
246
247 }
248
249 }
250
251 class wpae_preprocessXml_filter extends php_user_filter {
252
253 function filter($in, $out, &$consumed, $closing)
254 {
255 while ($bucket = stream_bucket_make_writeable($in)) {
256 $is_preprocess_enabled = apply_filters('is_xml_preprocess_enabled', true);
257 if ($is_preprocess_enabled)
258 {
259 // the & symbol is not valid in XML, so replace it with temporary word _ampersand_
260 $bucket->data = str_replace("&", "_ampersand_", $bucket->data);
261 $bucket->data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $this->replace_colons($bucket->data));
262 }
263 $consumed += $bucket->datalen;
264 stream_bucket_append($out, $bucket);
265 }
266 return PSFS_PASS_ON;
267 }
268
269 function replace_colons($data)
270 {
271 return str_replace(":", "_colon_", $data);
272 }
273
274 }
275