CdataStrategy.php
8 years ago
CdataStrategyAlways.php
8 years ago
CdataStrategyFactory.php
9 years ago
CdataStrategyIllegalCharacters.php
8 years ago
CdataStrategyIllegalCharactersHtmlEntities.php
8 years ago
CdataStrategyNever.php
8 years ago
XMLWriter.php
8 years ago
chunk.php
5 years ago
config.php
7 years ago
download.php
6 years ago
handler.php
10 years ago
helper.php
12 years ago
input.php
7 years ago
installer.php
9 years ago
session.php
10 years ago
wpallimport.php
4 years ago
zip.php
10 years ago
chunk.php
274 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 | break; |
| 113 | } |
| 114 | } |
| 115 | unset($reader); |
| 116 | |
| 117 | if ( ! empty($this->cloud) and empty($this->options['element']) ){ |
| 118 | |
| 119 | arsort($this->cloud); |
| 120 | |
| 121 | $main_elements = array('node', 'product', 'job', 'deal', 'entry', 'item', 'property', 'listing', 'hotel', 'record', 'article', 'post', 'book'); |
| 122 | |
| 123 | foreach ($this->cloud as $element_name => $value) { |
| 124 | if ( in_array(strtolower($element_name), $main_elements) ){ |
| 125 | $this->options['element'] = $element_name; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (empty($this->options['element'])){ |
| 131 | foreach ($this->cloud as $el => $count) { |
| 132 | $this->options['element'] = $el; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (function_exists('stream_filter_register') and $this->options['filter']){ |
| 140 | stream_filter_register('preprocessxml', 'wpae_preprocessXml_filter'); |
| 141 | $path = 'php://filter/read=preprocessxml/resource=' . $this->file; |
| 142 | } |
| 143 | else $path = $this->file; |
| 144 | |
| 145 | $this->reader = new XMLReader(); |
| 146 | @$this->reader->open($path); |
| 147 | @$this->reader->setParserProperty(XMLReader::VALIDATE, false); |
| 148 | |
| 149 | |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * __destruct |
| 154 | * |
| 155 | * Cleans up |
| 156 | * |
| 157 | * @return void |
| 158 | * @author Dom Hastings |
| 159 | * @access public |
| 160 | */ |
| 161 | public function __destruct() { |
| 162 | // close the file resource |
| 163 | unset($this->reader); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * read |
| 168 | * |
| 169 | * Reads the first available occurence of the XML element $this->options['element'] |
| 170 | * |
| 171 | * @return string The XML string from $this->file |
| 172 | * @author Dom Hastings |
| 173 | * @access public |
| 174 | */ |
| 175 | public function read($debug = false) { |
| 176 | |
| 177 | // trim it |
| 178 | $element = trim($this->options['element']); |
| 179 | |
| 180 | $xml = ''; |
| 181 | |
| 182 | try { |
| 183 | while ( @$this->reader->read() ) { |
| 184 | switch ($this->reader->nodeType) { |
| 185 | case (XMLREADER::ELEMENT): |
| 186 | $localName = str_replace("_colon_", ":", $this->reader->localName); |
| 187 | |
| 188 | if ( strtolower(str_replace(":", "_", $localName)) == strtolower($element) ) { |
| 189 | |
| 190 | if ($this->loop < $this->options['pointer']){ |
| 191 | $this->loop++; |
| 192 | continue 2; |
| 193 | } |
| 194 | |
| 195 | $xml = @$this->reader->readOuterXML(); |
| 196 | |
| 197 | break(2); |
| 198 | } |
| 199 | break; |
| 200 | default: |
| 201 | // code ... |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } catch (XmlImportException $e) { |
| 206 | $xml = false; |
| 207 | } |
| 208 | |
| 209 | return ( ! empty($xml) ) ? $this->removeColonsFromRSS(preg_replace('%xmlns.*=\s*([\'""]).*\1%sU', '', $xml)) : false; |
| 210 | |
| 211 | } |
| 212 | |
| 213 | function removeColonsFromRSS($feed) { |
| 214 | |
| 215 | $feed = str_replace("_colon_", ":", $feed); |
| 216 | |
| 217 | // pull out colons from start tags |
| 218 | // (<\w+):(\w+>) |
| 219 | $pattern = '/(<\w+):([\w+|\.|-]+[ |>]{1})/i'; |
| 220 | $replacement = '$1_$2'; |
| 221 | $feed = preg_replace($pattern, $replacement, $feed); |
| 222 | // pull out colons from end tags |
| 223 | // (<\/\w+):(\w+>) |
| 224 | $pattern = '/(<\/\w+):([\w+|\.|-]+>)/i'; |
| 225 | $replacement = '$1_$2'; |
| 226 | $feed = preg_replace($pattern, $replacement, $feed); |
| 227 | // pull out colons from attributes |
| 228 | $pattern = '/(\s+\w+):(\w+[=]{1})/i'; |
| 229 | $replacement = '$1_$2'; |
| 230 | $feed = preg_replace($pattern, $replacement, $feed); |
| 231 | // pull colons from single element |
| 232 | // (<\w+):(\w+\/>) |
| 233 | $pattern = '/(<\w+):([\w+|\.|-]+\/>)/i'; |
| 234 | $replacement = '$1_$2'; |
| 235 | $feed = preg_replace($pattern, $replacement, $feed); |
| 236 | |
| 237 | $is_preprocess_enabled = apply_filters('is_xml_preprocess_enabled', true); |
| 238 | if ($is_preprocess_enabled) |
| 239 | { |
| 240 | // replace temporary word _ampersand_ back to & symbol |
| 241 | $feed = str_replace("_ampersand_", "&", $feed); |
| 242 | } |
| 243 | |
| 244 | return $feed; |
| 245 | |
| 246 | } |
| 247 | |
| 248 | } |
| 249 | |
| 250 | class wpae_preprocessXml_filter extends php_user_filter { |
| 251 | |
| 252 | function filter($in, $out, &$consumed, $closing) |
| 253 | { |
| 254 | while ($bucket = stream_bucket_make_writeable($in)) { |
| 255 | $is_preprocess_enabled = apply_filters('is_xml_preprocess_enabled', true); |
| 256 | if ($is_preprocess_enabled) |
| 257 | { |
| 258 | // the & symbol is not valid in XML, so replace it with temporary word _ampersand_ |
| 259 | $bucket->data = str_replace("&", "_ampersand_", $bucket->data); |
| 260 | $bucket->data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $this->replace_colons($bucket->data)); |
| 261 | } |
| 262 | $consumed += $bucket->datalen; |
| 263 | stream_bucket_append($out, $bucket); |
| 264 | } |
| 265 | return PSFS_PASS_ON; |
| 266 | } |
| 267 | |
| 268 | function replace_colons($data) |
| 269 | { |
| 270 | return str_replace(":", "_colon_", $data); |
| 271 | } |
| 272 | |
| 273 | } |
| 274 |