partner-discount-sdk
3 weeks ago
CdataStrategy.php
3 weeks ago
CdataStrategyAlways.php
3 weeks ago
CdataStrategyFactory.php
3 weeks ago
CdataStrategyIllegalCharacters.php
3 weeks ago
CdataStrategyIllegalCharactersHtmlEntities.php
3 weeks ago
CdataStrategyNever.php
3 weeks ago
XMLWriter.php
3 weeks ago
chunk.php
3 weeks ago
config.php
3 years ago
download.php
3 weeks ago
handler.php
3 weeks ago
helper.php
3 weeks ago
input.php
3 weeks ago
installer.php
3 weeks ago
session.php
10 years ago
wpallimport.php
3 weeks ago
zip.php
4 years ago
chunk.php
282 lines
| 1 | <?php |
| 2 | |
| 3 | // phpcs:ignoreFile WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound,WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound,WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- legitimate plugin prefixes (pmxe/PMXE/wpae/Wpae/wp_all_export/wpallexport/XmlExport/CdataStrategy/VariableProductTitle/Soflyy/GF_Export); Plugin Check does not honor phpcs.xml prefix declaration |
| 4 | defined( 'ABSPATH' ) || exit; |
| 5 | |
| 6 | /** |
| 7 | * Chunk |
| 8 | * |
| 9 | * Reads a large file in as chunks for easier parsing. |
| 10 | * |
| 11 | * |
| 12 | * @package default |
| 13 | * @author Max Tsiplyakov |
| 14 | */ |
| 15 | class PMXE_Chunk { |
| 16 | /** |
| 17 | * options |
| 18 | * |
| 19 | * @var array Contains all major options |
| 20 | * @access public |
| 21 | */ |
| 22 | public $options = array( |
| 23 | 'path' => './', // string The path to check for $file in |
| 24 | 'element' => '', // string The XML element to return |
| 25 | 'type' => 'upload', |
| 26 | 'encoding' => 'UTF-8', |
| 27 | 'pointer' => 1, |
| 28 | 'chunkSize' => 1024, |
| 29 | 'filter' => true, |
| 30 | 'get_cloud' => false |
| 31 | ); |
| 32 | |
| 33 | /** |
| 34 | * file |
| 35 | * |
| 36 | * @var string The filename being read |
| 37 | * @access public |
| 38 | */ |
| 39 | public $file = ''; |
| 40 | /** |
| 41 | * pointer |
| 42 | * |
| 43 | * @var integer The current position the file is being read from |
| 44 | * @access public |
| 45 | */ |
| 46 | public $reader; |
| 47 | public $cloud = array(); |
| 48 | public $loop = 1; |
| 49 | |
| 50 | /** |
| 51 | * handle |
| 52 | * |
| 53 | * @var resource The fopen() resource |
| 54 | * @access private |
| 55 | */ |
| 56 | private $handle = null; |
| 57 | /** |
| 58 | * reading |
| 59 | * |
| 60 | * @var boolean Whether the script is currently reading the file |
| 61 | * @access private |
| 62 | */ |
| 63 | |
| 64 | /** |
| 65 | * __construct |
| 66 | * |
| 67 | * Builds the Chunk object |
| 68 | * |
| 69 | * @param string $file The filename to work with |
| 70 | * @param array $options The options with which to parse the file |
| 71 | * @author Dom Hastings |
| 72 | * @access public |
| 73 | */ |
| 74 | public function __construct($file, $options = array()) { |
| 75 | |
| 76 | // merge the options together |
| 77 | $this->options = array_merge($this->options, (is_array($options) ? $options : array())); |
| 78 | |
| 79 | $this->options['chunkSize'] *= 32; |
| 80 | |
| 81 | // set the filename |
| 82 | $this->file = $file; |
| 83 | |
| 84 | $is_html = false; |
| 85 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 86 | $f = @fopen($file, "rb"); |
| 87 | while (!@feof($f)) { |
| 88 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 89 | $chunk = @fread($f, 1024); |
| 90 | if (strpos($chunk, "<!DOCTYPE") === 0) $is_html = true; |
| 91 | break; |
| 92 | } |
| 93 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 94 | @fclose($f); |
| 95 | |
| 96 | if ($is_html) return; |
| 97 | |
| 98 | if (empty($this->options['element']) or $this->options['get_cloud']) |
| 99 | { |
| 100 | if (function_exists('stream_filter_register') and $this->options['filter']){ |
| 101 | stream_filter_register('preprocessxml', 'wpae_preprocessXml_filter'); |
| 102 | $path = 'php://filter/read=preprocessxml/resource=' . $this->file; |
| 103 | } |
| 104 | else $path = $this->file; |
| 105 | |
| 106 | $reader = new XMLReader(); |
| 107 | $reader->open($path); |
| 108 | $reader->setParserProperty(XMLReader::VALIDATE, false); |
| 109 | while ( @$reader->read()) { |
| 110 | switch ($reader->nodeType) { |
| 111 | case (XMLREADER::ELEMENT): |
| 112 | $localName = str_replace("_colon_", ":", $reader->localName); |
| 113 | if (array_key_exists(str_replace(":", "_", $localName), $this->cloud)) |
| 114 | $this->cloud[str_replace(":", "_", $localName)]++; |
| 115 | else |
| 116 | $this->cloud[str_replace(":", "_", $localName)] = 1; |
| 117 | break; |
| 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | unset($reader); |
| 123 | |
| 124 | if ( ! empty($this->cloud) and empty($this->options['element']) ){ |
| 125 | |
| 126 | arsort($this->cloud); |
| 127 | |
| 128 | $main_elements = array('node', 'product', 'job', 'deal', 'entry', 'item', 'property', 'listing', 'hotel', 'record', 'article', 'post', 'book'); |
| 129 | |
| 130 | foreach ($this->cloud as $element_name => $value) { |
| 131 | if ( in_array(strtolower($element_name), $main_elements) ){ |
| 132 | $this->options['element'] = $element_name; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (empty($this->options['element'])){ |
| 138 | foreach ($this->cloud as $el => $count) { |
| 139 | $this->options['element'] = $el; |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (function_exists('stream_filter_register') and $this->options['filter']){ |
| 147 | stream_filter_register('preprocessxml', 'wpae_preprocessXml_filter'); |
| 148 | $path = 'php://filter/read=preprocessxml/resource=' . $this->file; |
| 149 | } |
| 150 | else $path = $this->file; |
| 151 | |
| 152 | $this->reader = new XMLReader(); |
| 153 | @$this->reader->open($path); |
| 154 | @$this->reader->setParserProperty(XMLReader::VALIDATE, false); |
| 155 | |
| 156 | |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * __destruct |
| 161 | * |
| 162 | * Cleans up |
| 163 | * |
| 164 | * @return void |
| 165 | * @author Dom Hastings |
| 166 | * @access public |
| 167 | */ |
| 168 | public function __destruct() { |
| 169 | // close the file resource |
| 170 | unset($this->reader); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * read |
| 175 | * |
| 176 | * Reads the first available occurence of the XML element $this->options['element'] |
| 177 | * |
| 178 | * @return string The XML string from $this->file |
| 179 | * @author Dom Hastings |
| 180 | * @access public |
| 181 | */ |
| 182 | public function read($debug = false) { |
| 183 | |
| 184 | // trim it |
| 185 | $element = trim($this->options['element']); |
| 186 | |
| 187 | $xml = ''; |
| 188 | |
| 189 | try { |
| 190 | while ( @$this->reader->read() ) { |
| 191 | switch ($this->reader->nodeType) { |
| 192 | case (XMLREADER::ELEMENT): |
| 193 | $localName = str_replace("_colon_", ":", $this->reader->localName); |
| 194 | |
| 195 | if ( strtolower(str_replace(":", "_", $localName)) == strtolower($element) ) { |
| 196 | |
| 197 | if ($this->loop < $this->options['pointer']){ |
| 198 | $this->loop++; |
| 199 | continue 2; |
| 200 | } |
| 201 | |
| 202 | $xml = @$this->reader->readOuterXML(); |
| 203 | |
| 204 | break(2); |
| 205 | } |
| 206 | break; |
| 207 | default: |
| 208 | // code ... |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | } catch (XmlImportException $e) { |
| 213 | $xml = false; |
| 214 | } |
| 215 | |
| 216 | return ( ! empty($xml) ) ? $this->removeColonsFromRSS(preg_replace('%xmlns.*=\s*([\'""]).*\1%sU', '', $xml)) : false; |
| 217 | |
| 218 | } |
| 219 | |
| 220 | function removeColonsFromRSS($feed) { |
| 221 | |
| 222 | $feed = str_replace("_colon_", ":", $feed); |
| 223 | |
| 224 | // pull out colons from start tags |
| 225 | // (<\w+):(\w+>) |
| 226 | $pattern = '/(<\w+):([\w+|\.|-]+[ |>]{1})/i'; |
| 227 | $replacement = '$1_$2'; |
| 228 | $feed = preg_replace($pattern, $replacement, $feed); |
| 229 | // pull out colons from end tags |
| 230 | // (<\/\w+):(\w+>) |
| 231 | $pattern = '/(<\/\w+):([\w+|\.|-]+>)/i'; |
| 232 | $replacement = '$1_$2'; |
| 233 | $feed = preg_replace($pattern, $replacement, $feed); |
| 234 | // pull out colons from attributes |
| 235 | $pattern = '/(\s+\w+):(\w+[=]{1})/i'; |
| 236 | $replacement = '$1_$2'; |
| 237 | $feed = preg_replace($pattern, $replacement, $feed); |
| 238 | // pull colons from single element |
| 239 | // (<\w+):(\w+\/>) |
| 240 | $pattern = '/(<\w+):([\w+|\.|-]+\/>)/i'; |
| 241 | $replacement = '$1_$2'; |
| 242 | $feed = preg_replace($pattern, $replacement, $feed); |
| 243 | |
| 244 | $is_preprocess_enabled = apply_filters('is_xml_preprocess_enabled', true); |
| 245 | if ($is_preprocess_enabled) |
| 246 | { |
| 247 | // replace temporary word _ampersand_ back to & symbol |
| 248 | $feed = str_replace("_ampersand_", "&", $feed); |
| 249 | } |
| 250 | |
| 251 | return $feed; |
| 252 | |
| 253 | } |
| 254 | |
| 255 | } |
| 256 | |
| 257 | class wpae_preprocessXml_filter extends php_user_filter { |
| 258 | |
| 259 | #[ReturnTypeWillChange] |
| 260 | function filter($in, $out, &$consumed, $closing) |
| 261 | { |
| 262 | while ($bucket = stream_bucket_make_writeable($in)) { |
| 263 | $is_preprocess_enabled = apply_filters('is_xml_preprocess_enabled', true); |
| 264 | if ($is_preprocess_enabled) |
| 265 | { |
| 266 | // the & symbol is not valid in XML, so replace it with temporary word _ampersand_ |
| 267 | $bucket->data = str_replace("&", "_ampersand_", $bucket->data); |
| 268 | $bucket->data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $this->replace_colons($bucket->data)); |
| 269 | } |
| 270 | $consumed += $bucket->datalen; |
| 271 | stream_bucket_append($out, $bucket); |
| 272 | } |
| 273 | return PSFS_PASS_ON; |
| 274 | } |
| 275 | |
| 276 | function replace_colons($data) |
| 277 | { |
| 278 | return str_replace(":", "_colon_", $data); |
| 279 | } |
| 280 | |
| 281 | } |
| 282 |