PluginProbe ʕ •ᴥ•ʔ
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets / trunk
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets vtrunk
3.9.5 3.9.6 4.0.0 4.0.1 4.1.0 trunk 2.12 2.13 2.14 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9 3.7.0 3.7.1 3.7.2 3.7.3 3.7.3-beta-1.0 3.7.4 3.7.4-beta-1.0 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8.0 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4
wp-all-import / classes / upload.php
wp-all-import / classes Last commit date
XmlStreamReader 3 weeks ago partner-discount-sdk 3 weeks ago api.php 3 weeks ago arraytoxml.php 3 weeks ago chunk.php 3 weeks ago config.php 2 years ago download.php 3 weeks ago error.php 3 weeks ago handler.php 3 weeks ago helper.php 3 weeks ago input.php 3 weeks ago nested.php 3 weeks ago rapidaddon.php 3 weeks ago render.php 3 weeks ago session.php 9 months ago upload.php 3 weeks ago zip.php 10 years ago
upload.php
671 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit;
3 if ( ! class_exists('PMXI_Upload')){
4
5 class PMXI_Upload{
6
7 protected $file;
8 protected $errors;
9 protected $root_element = '';
10 protected $is_csv = false;
11
12 protected $uploadsPath;
13
14 function __construct( $file, $errors, $targetDir = false ){
15
16 $this->file = $file;
17 $this->errors = $errors;
18
19 $uploads = wp_upload_dir();
20
21 $input = new PMXI_Input();
22 $import_id = $input->get('id');
23 // Get import ID from cron processing URL.
24 if (empty($import_id)) {
25 $import_id = $input->get('import_id');
26 }
27 // Get import ID from CLI arguments.
28 if ( empty( $import_id ) && PMXI_Plugin::getInstance()->isCli() ) {
29 $import_id = wp_all_import_get_import_id();
30 }
31 if ( $uploads['error'] ) {
32 $this->uploadsPath = false;
33 } else {
34 $this->uploadsPath = wp_all_import_get_absolute_path( ( ! $targetDir ) ? wp_all_import_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY, $import_id, true) : $targetDir );
35 }
36 }
37
38 public function upload() {
39
40 $this->file = wp_all_import_get_absolute_path($this->file);
41
42 $templates = false;
43
44 $bundle = array();
45
46 $bundleFiles = array();
47
48 $csv_path = '';
49
50 if (empty($this->file)) {
51 $this->errors->add('form-validation', __('Please specify a file to import.<br/><br/>If you are uploading the file from your computer, please wait for it to finish uploading (progress bar at 100%), before trying to continue.', 'wp-all-import'));
52 } elseif (!is_file($this->file)) {
53 $this->errors->add('form-validation', __('Uploaded file is empty', 'wp-all-import'));
54 } elseif ( ! preg_match('%\W(xml|gzip|zip|csv|tsv|gz|json|txt|dat|psv|sql|xls|xlsx)$%i', trim(basename($this->file)))) {
55 $this->errors->add('form-validation', __('Uploaded file must be XML, CSV, ZIP, GZIP, GZ, JSON, SQL, TXT, DAT or PSV', 'wp-all-import'));
56 } elseif (preg_match('%\W(zip)$%i', trim(basename($this->file)))) {
57
58 if (!class_exists('WpaiPclZip')) include_once(PMXI_Plugin::ROOT_DIR.'/libraries/wpaipclzip.lib.php');
59
60 $archive = new WpaiPclZip($this->file);
61
62 // Get allowed file extensions (whitelist approach)
63 $allowed_extensions = wp_all_import_get_allowed_zip_extensions();
64
65 // Attempt to extract files with whitelist restrictions
66 $v_result_list = $archive->extract(WPAI_PCLZIP_OPT_PATH, $this->uploadsPath, WPAI_PCLZIP_OPT_REPLACE_NEWER, WPAI_PCLZIP_OPT_EXTRACT_DIR_RESTRICTION, $this->uploadsPath, WPAI_PCLZIP_OPT_EXTRACT_WHITELIST_RESTRICTIONS, $allowed_extensions);
67
68 if (empty($v_result_list) || !is_array($v_result_list) && $v_result_list < 1) {
69 $this->errors->add('form-validation', __('WP All Import couldn\'t find a file to import inside your ZIP.<br/><br/>Either the .ZIP file is broken, or doesn\'t contain a file with an extension of XML, CSV, PSV, DAT, or TXT. <br/>Please attempt to unzip your .ZIP file on your computer to ensure it is a valid .ZIP file which can actually be unzipped, and that it contains a file which WP All Import can import.', 'wp-all-import'));
70 } else {
71 $filePath = '';
72 $decodedTemplates = array();
73 if ( ! empty($v_result_list) ) {
74 foreach ($v_result_list as $unzipped_file) {
75 if ($unzipped_file['status'] == 'ok' and preg_match('%\W(php)$%i', trim($unzipped_file['stored_filename']))) {
76 wp_delete_file($unzipped_file['filename']);
77 continue;
78 }
79 if ($unzipped_file['status'] == 'ok' and preg_match('%\W(xml|csv|txt|dat|psv|json|xls|xlsx|gz)$%i', trim($unzipped_file['stored_filename'])) and strpos($unzipped_file['stored_filename'], 'readme.txt') === false ) {
80 if ( strpos(basename($unzipped_file['stored_filename']), 'WP All Import Template') === 0 || strpos(basename($unzipped_file['stored_filename']), 'templates_') === 0 ) {
81 $templates = file_get_contents($unzipped_file['filename']);
82 $decodedTemplates = json_decode($templates, true);
83 $templateOptions = empty($decodedTemplates[0]) ? current($decodedTemplates) : $decodedTemplates;
84 if ( ! empty($templateOptions) and isset($templateOptions[0]['_import_type']) and $templateOptions[0]['_import_type'] == 'url' ) {
85 $options = \pmxi_maybe_unserialize($templateOptions[0]['options']);
86 return array(
87 'filePath' => $templateOptions[0]['_import_url'],
88 'bundle' => $bundle,
89 'template' => json_encode($templateOptions),
90 'templates' => $templates,
91 'post_type' => (!empty($options)) ? $options['custom_type'] : false,
92 'taxonomy_type' => (!empty($options['taxonomy_type'])) ? $options['taxonomy_type'] : false,
93 'is_empty_bundle_file' => true
94 );
95 }
96 } else {
97 if ($filePath == '') {
98 $filePath = $unzipped_file['filename'];
99 }
100 if ( ! in_array($unzipped_file['filename'], $bundleFiles) ) {
101 $bundleFiles[basename($unzipped_file['filename'])] = $unzipped_file['filename'];
102 }
103 }
104 }
105 }
106 }
107
108 if (count($bundleFiles) > 1) {
109 if ( ! empty($decodedTemplates) ) {
110 foreach ($decodedTemplates as $cpt => $tpl) {
111 $fileFormats = $this->get_xml_file( $bundleFiles[basename($tpl[0]['source_file_name'])] );
112 $bundle[$cpt] = $fileFormats['xml'];
113 }
114 }
115 if ( ! empty($bundle)) $filePath = current($bundle);
116 }
117
118 if ( $this->uploadsPath === false ){
119 $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'wp-all-import'));
120 }
121
122 if (empty($filePath)) {
123 $zip = new \ZipArchive();
124 $result = $zip->open(trim($this->file));
125 if ($result) {
126 for ($i = 0; $i < $zip->numFiles; $i++) {
127 $fileName = $zip->getNameIndex($i);
128 if (preg_match('%\W(xml|csv|txt|dat|psv|json|xls|xlsx|gz)$%i', trim($fileName))) {
129 $filePath = $this->uploadsPath . "/" . $fileName;
130 $fp = fopen($filePath, "w"); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
131 fwrite($fp, $zip->getFromIndex($i)); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
132 fclose($fp); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
133 break;
134 }
135 }
136 $zip->close();
137 } else {
138 $this->errors->add('form-validation', __('WP All Import couldn\'t find a file to import inside your ZIP.<br/><br/>Either the .ZIP file is broken, or doesn\'t contain a file with an extension of XML, CSV, PSV, DAT, or TXT. <br/>Please attempt to unzip your .ZIP file on your computer to ensure it is a valid .ZIP file which can actually be unzipped, and that it contains a file which WP All Import can import.', 'wp-all-import'));
139 }
140 }
141 // Detect if file is very large
142 $source = array(
143 'name' => basename($this->file),
144 'type' => 'upload',
145 'path' => $this->file,
146 );
147 $fileFormats = $this->get_xml_file( $filePath );
148 $filePath = $fileFormats['xml'];
149 $csv_path = $fileFormats['csv'];
150 }
151 } elseif ( preg_match('%\W(csv|txt|dat|psv|tsv)$%i', trim($this->file))) { // If CSV file uploaded
152
153 if ( $this->uploadsPath === false ){
154 $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'wp-all-import'));
155 }
156 $filePath = $this->file;
157 $source = array(
158 'name' => basename($this->file),
159 'type' => 'upload',
160 'path' => $filePath,
161 );
162
163 include_once(PMXI_Plugin::ROOT_DIR.'/libraries/XmlImportCsvParse.php');
164
165 $csv = new PMXI_CsvParser( array( 'filename' => $this->file, 'targetDir' => $this->uploadsPath ) );
166 //@unlink($filePath);
167 $csv_path = $filePath;
168 $filePath = $csv->xml_path;
169 $this->is_csv = $csv->is_csv;
170 $this->root_element = 'node';
171
172 } elseif(preg_match('%\W(gz)$%i', trim($this->file))){ // If gz file uploaded
173 $fileInfo = wp_all_import_get_gz($this->file, 0, $this->uploadsPath);
174 if ( ! is_wp_error($fileInfo) ){
175 $filePath = $fileInfo['localPath'];
176 // Detect if file is very large
177 $source = array(
178 'name' => basename($this->file),
179 'type' => 'upload',
180 'path' => $this->file,
181 );
182 // detect CSV or XML
183 if ( $fileInfo['type'] == 'csv') { // it is CSV file
184
185 include_once(PMXI_Plugin::ROOT_DIR.'/libraries/XmlImportCsvParse.php');
186 $csv = new PMXI_CsvParser( array( 'filename' => $filePath, 'targeDir' => $this->uploadsPath ) ); // create chunks
187 //@unlink($filePath);
188 $csv_path = $filePath;
189 $filePath = $csv->xml_path;
190 $this->is_csv = $csv->is_csv;
191 $this->root_element = 'node';
192 }
193 }
194 else $this->errors->add('form-validation', $fileInfo->get_error_message());
195 } elseif (preg_match('%\W(json)$%i', trim($this->file))){
196
197 // Detect if file is very large
198 $source = array(
199 'name' => basename($this->file),
200 'type' => 'upload',
201 'path' => $this->file,
202 );
203
204 $json_str = trim(file_get_contents($this->file));
205 $json_str = str_replace("\xEF\xBB\xBF",'', $json_str);
206 $is_json = wp_all_import_is_json($json_str);
207
208 if( is_wp_error($is_json)){
209 $this->errors->add('form-validation', $is_json->get_error_message(), 'wp-all-import');
210 } else {
211 $xml_data = wp_all_import_json_to_xml( json_decode($json_str, true) );
212 if ( empty($xml_data) ){
213 $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'wp-all-import'));
214 } else{
215 $jsontmpname = $this->uploadsPath .'/'. wp_all_import_url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($this->file))));
216 //@unlink($this->file);
217 file_put_contents($jsontmpname, $xml_data);
218 $filePath = $jsontmpname;
219
220 }
221 }
222 } elseif (preg_match('%\W(sql)$%i', trim($this->file))) {
223 $source = array(
224 'name' => basename($this->file),
225 'type' => 'upload',
226 'path' => $this->file,
227 );
228 include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php' );
229 $sql = new PMXI_SQLParser( $this->file, $this->uploadsPath );
230 $filePath = $sql->parse();
231 } elseif (preg_match('%\W(xls|xlsx)$%i', trim($this->file))){
232 $source = array(
233 'name' => basename($this->file),
234 'type' => 'upload',
235 'path' => $this->file,
236 );
237
238 include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportXLSParse.php' );
239 $xls = new PMXI_XLSParser( $this->file, $this->uploadsPath );
240 $filePath = $xls->parse();
241 } else { // If XML file uploaded
242 $filePath = $this->file;
243 $source = array(
244 'name' => basename($this->file),
245 'type' => 'upload',
246 'path' => $filePath,
247 );
248 }
249
250 if ( $this->errors->get_error_codes() ) return $this->errors;
251
252 $decodedTemplates = empty($templates) ? false : json_decode($templates, true);
253
254 $source['path'] = wp_all_import_get_relative_path($source['path']);
255
256 $templateOptions = "";
257
258 if ( is_array($decodedTemplates) ) {
259 $templateOptions = empty($decodedTemplates[0]) ? current($decodedTemplates) : $decodedTemplates;
260 }
261
262 $options = (empty($templateOptions[0]['options'])) ? false : \pmxi_maybe_unserialize($templateOptions[0]['options']);
263
264 if ( ! empty($options['root_element'])) $this->root_element = $options['root_element'];
265
266 return array(
267 'filePath' => $filePath,
268 'bundle' => $bundle, // sub imports [cpt => filepath]
269 'source' => $source,
270 'root_element' => $this->root_element,
271 'is_csv' => $this->is_csv,
272 'csv_path' => $csv_path,
273 'template' => empty($templateOptions) ? "" : json_encode($templateOptions),
274 'templates' => $templates,
275 'post_type' => (!empty($options)) ? $options['custom_type'] : false,
276 'taxonomy_type' => (!empty($options['taxonomy_type'])) ? $options['taxonomy_type'] : false,
277 );
278 }
279
280 public function url( $feed_type = '', $feed_xpath = '', $importTemplate = ''){
281
282 $uploads = wp_upload_dir();
283
284 $templates = false;
285
286 $bundle = array();
287
288 $bundleFiles = array();
289
290 if (empty($this->file)) {
291 $this->errors->add('form-validation', __('Please specify a file to import.', 'wp-all-import'));
292 } elseif ( ! preg_match('%^https?://%i', $this->file)) {
293 $this->errors->add('form-validation', __('The URL to your file is not valid.<br/><br/>Please make sure the URL starts with http:// or https://. To import from https://, your server must have OpenSSL installed.', 'wp-all-import'));
294 } elseif( ! is_writeable($this->uploadsPath)){ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writeable
295 /* translators: %s: uploads folder path */
296 $this->errors->add('form-validation', sprintf(__('Uploads folder %s is not writable.', 'wp-all-import'), $this->uploadsPath));
297 }
298
299 $this->file = trim($this->file);
300
301 $csv_path = '';
302
303 if ( empty($this->errors->errors) ){
304
305 if( '' == $feed_type and ! preg_match('%\W(xml|csv|zip|gz|xls|xlsx)$%i', trim($this->file))) $feed_type = wp_all_import_get_remote_file_name(trim($this->file));
306
307 if ('zip' == $feed_type or empty($feed_type) and preg_match('%\W(zip)$%i', trim($this->file))) {
308
309 $tmpname = $this->uploadsPath . '/' . wp_unique_filename($this->uploadsPath, md5(basename($this->file)) . '.zip');
310
311 @copy($this->file, $tmpname);
312
313 if (!file_exists($tmpname)) {
314 $request = get_file_curl($this->file, $tmpname);
315 if (is_wp_error($request)) $this->errors->add('form-validation', $request->get_error_message());
316 if (!file_exists($tmpname)) $this->errors->add('form-validation', __('Failed upload ZIP archive', 'wp-all-import'));
317 }
318
319 if (!class_exists('WpaiPclZip')) include_once(PMXI_Plugin::ROOT_DIR.'/libraries/wpaipclzip.lib.php');
320
321 $archive = new WpaiPclZip($tmpname);
322
323 // Get allowed file extensions (whitelist approach)
324 $allowed_extensions = wp_all_import_get_allowed_zip_extensions();
325
326 // Attempt to extract files with whitelist restrictions
327 $v_result_list = $archive->extract(WPAI_PCLZIP_OPT_PATH, $this->uploadsPath, WPAI_PCLZIP_OPT_REPLACE_NEWER, WPAI_PCLZIP_OPT_EXTRACT_DIR_RESTRICTION, $this->uploadsPath, WPAI_PCLZIP_OPT_EXTRACT_WHITELIST_RESTRICTIONS, $allowed_extensions);
328
329 if (empty($v_result_list) || !is_array($v_result_list) && $v_result_list < 1) {
330 $this->errors->add('form-validation', __('WP All Import couldn\'t find a file to import inside your ZIP.<br/><br/>Either the .ZIP file is broken, or doesn\'t contain a file with an extension of XML, CSV, PSV, DAT, or TXT. <br/>Please attempt to unzip your .ZIP file on your computer to ensure it is a valid .ZIP file which can actually be unzipped, and that it contains a file which WP All Import can import.', 'wp-all-import'));
331 } else {
332 $filePath = '';
333 if (!empty($v_result_list)) {
334 foreach ($v_result_list as $unzipped_file) {
335 if ($unzipped_file['status'] == 'ok' and preg_match('%\W(php)$%i', trim($unzipped_file['stored_filename']))) {
336 wp_delete_file($unzipped_file['filename']);
337 continue;
338 }
339 if ($unzipped_file['status'] == 'ok' and preg_match('%\W(xml|csv|txt|dat|psv|json|xls|xlsx|gz)$%i', trim($unzipped_file['stored_filename'])) and strpos($unzipped_file['stored_filename'], 'readme.txt') === false ) {
340 if ( strpos(basename($unzipped_file['stored_filename']), 'WP All Import Template') === 0 || strpos(basename($unzipped_file['stored_filename']), 'templates_') === 0) {
341 $templates = file_get_contents($unzipped_file['filename']);
342 $decodedTemplates = json_decode($templates, true);
343 $templateOptions = empty($decodedTemplates[0]) ? current($decodedTemplates) : $decodedTemplates;
344 }
345 else {
346 if ($filePath == '') {
347 $filePath = $unzipped_file['filename'];
348 }
349 if ( ! in_array($unzipped_file['filename'], $bundleFiles) ) {
350 $bundleFiles[basename($unzipped_file['filename'])] = $unzipped_file['filename'];
351 }
352 }
353 }
354 }
355 }
356
357 if ( count($bundleFiles) > 1 ) {
358 if ( ! empty($decodedTemplates) ) {
359 foreach ($decodedTemplates as $cpt => $tpl) {
360 $fileFormats = $this->get_xml_file( $bundleFiles[basename($tpl[0]['source_file_name'])] );
361 $bundle[$cpt] = $fileFormats['xml'];
362 }
363 }
364 if ( ! empty($bundle)) $filePath = current($bundle);
365 }
366
367 if($this->uploadsPath === false){
368 $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'wp-all-import'));
369 }
370
371 if(empty($filePath)){
372 $zip = new \ZipArchive();
373 $result = $zip->open(trim($tmpname));
374 if ($result) {
375 for ($i = 0; $i < $zip->numFiles; $i++) {
376 $fileName = $zip->getNameIndex($i);
377 if (preg_match('%\W(xml|csv|txt|dat|psv|json|xls|xlsx|gz)$%i', trim($fileName))) {
378 $filePath = $this->uploadsPath . "/" . $fileName;
379 $fp = fopen($filePath, "w"); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
380 fwrite($fp, $zip->getFromIndex($i)); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
381 fclose($fp); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
382 break;
383 }
384 }
385 $zip->close();
386 } else {
387 $this->errors->add('form-validation', __('WP All Import couldn\'t find a file to import inside your ZIP.<br/><br/>Either the .ZIP file is broken, or doesn\'t contain a file with an extension of XML, CSV, PSV, DAT, or TXT. <br/>Please attempt to unzip your .ZIP file on your computer to ensure it is a valid .ZIP file which can actually be unzipped, and that it contains a file which WP All Import can import.', 'wp-all-import'));
388 }
389 }
390 // Detect if file is very large
391 $source = array(
392 'name' => basename(wp_parse_url($this->file, PHP_URL_PATH)),
393 'type' => 'url',
394 'path' => $feed_xpath,
395 );
396 $fileFormats = $this->get_xml_file( $filePath );
397 $csv_path = $fileFormats['csv'];
398 $filePath = $fileFormats['xml'];
399 }
400 if (file_exists($tmpname)) wp_all_import_remove_source($tmpname, false);
401 } elseif ('csv' == $feed_type or '' == $feed_type and preg_match('%\W(csv|txt|dat|psv|tsv)$%i', trim($this->file))) {
402
403 $source = array(
404 'name' => basename(wp_parse_url($this->file, PHP_URL_PATH)),
405 'type' => 'url',
406 'path' => $feed_xpath,
407 );
408 // copy remote file in binary mode
409 $filePath = wp_all_import_get_url($this->file, $this->uploadsPath, 'csv');
410 if ( ! is_wp_error($filePath) ){
411 if ( ! file_exists($filePath)) {
412 $this->errors->add('form-validation', __('WP All Import was not able to download your file.<br/><br/>Please make sure the URL to your file is valid.<br/>You can test this by pasting it into your browser.<br/>Other reasons for this error can include some server setting on your host restricting access to this particular URL or external URLs in general, or some setting on the server hosting the file you are trying to access preventing your server from accessing it.', 'wp-all-import'));
413 }
414 // Detect if file is very large
415 include_once(PMXI_Plugin::ROOT_DIR.'/libraries/XmlImportCsvParse.php');
416 $csv = new PMXI_CsvParser( array( 'filename' => $filePath, 'targetDir' => $this->uploadsPath ) ); // create chunks
417 //wp_all_import_remove_source($filePath, false);
418 $csv_path = $filePath;
419 $filePath = $csv->xml_path;
420 $this->is_csv = $csv->is_csv;
421 $this->root_element = 'node';
422 }
423 else $this->errors->add('form-validation', $filePath->get_error_message());
424
425 } elseif ('json' == $feed_type or preg_match('%\W(json)$%i', trim($this->file))){
426
427 $source = array(
428 'name' => basename(wp_parse_url($this->file, PHP_URL_PATH)),
429 'type' => 'url',
430 'path' => $feed_xpath,
431 );
432 // copy remote file in binary mode
433 $filePath = wp_all_import_get_url($this->file, $this->uploadsPath, 'json');
434 $json_str = file_get_contents($filePath);
435 $json_str = str_replace("\xEF\xBB\xBF",'', $json_str);
436 $is_json = wp_all_import_is_json($json_str);
437 if ( is_wp_error($is_json)){
438 $this->errors->add('form-validation', $is_json->get_error_message(), 'wp-all-import');
439 } else {
440 $xml_data = wp_all_import_json_to_xml( json_decode($json_str, true) );
441 if ( empty($xml_data) ){
442 $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'wp-all-import'));
443 } else {
444 $tmpname = $this->uploadsPath .'/'. wp_all_import_url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($filePath))));
445 file_put_contents($tmpname, $xml_data);
446 wp_all_import_remove_source($filePath, false);
447 $filePath = $tmpname;
448 }
449 }
450 } elseif ('sql' == $feed_type or preg_match('%\W(sql)$%i', trim($this->file))){
451 $source = array(
452 'name' => basename($this->file),
453 'type' => 'url',
454 'path' => $feed_xpath,
455 );
456 // copy remote file in binary mode
457 $localSQLPath = wp_all_import_get_url($this->file, $this->uploadsPath, 'sql');
458 include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php' );
459 $sql = new PMXI_SQLParser( $localSQLPath, $this->uploadsPath );
460 $filePath = $sql->parse();
461 wp_all_import_remove_source($localSQLPath, false);
462 } elseif (preg_match('%\W(xls|xlsx)$%i', $feed_type) || preg_match('%\W(xls|xlsx)$%i', strtok(trim($this->file), "?")) || preg_match('%\W(xls|xlsx)$%i', trim($this->file))) {
463
464 $source = array(
465 'name' => basename($this->file),
466 'type' => 'url',
467 'path' => $feed_xpath,
468 );
469 // copy remote file in binary mode
470 $localXLSPath = wp_all_import_get_url($this->file, $this->uploadsPath, 'xls');
471 include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportXLSParse.php' );
472 $xls = new PMXI_XLSParser( $localXLSPath, $this->uploadsPath );
473 $filePath = $xls->parse();
474 wp_all_import_remove_source($localXLSPath, false);
475 } else {
476 if ('gz' == $feed_type or '' == $feed_type and preg_match('%\W(gz|gzip)$%i', trim($this->file))){
477 $fileInfo = wp_all_import_get_gz($this->file, 0, $this->uploadsPath);
478 } else {
479 $headers = wp_all_import_get_feed_type($this->file);
480 if ($headers['Content-Type'] and in_array($headers['Content-Type'], array('gz', 'gzip')) or $headers['Content-Encoding'] and in_array($headers['Content-Encoding'], array('gz', 'gzip'))){
481 $fileInfo = wp_all_import_get_gz($this->file, 0, $this->uploadsPath, $headers);
482 } else {
483 $fileInfo = wp_all_import_get_url($this->file, $this->uploadsPath, $headers['Content-Type'], $headers['Content-Encoding'], true);
484 }
485 }
486
487 if ( ! is_wp_error($fileInfo) ){
488 $filePath = $fileInfo['localPath'];
489 if ( ! file_exists($filePath)) {
490 $this->errors->add('form-validation', __('WP All Import was not able to download your file.<br/><br/>Please make sure the URL to your file is valid.<br/>You can test this by pasting it into your browser.<br/>Other reasons for this error can include some server setting on your host restricting access to this particular URL or external URLs in general, or some setting on the server hosting the file you are trying to access preventing your server from accessing it.', 'wp-all-import'));
491 }
492 // Detect if file is very large
493 $source = array(
494 'name' => basename(wp_parse_url($this->file, PHP_URL_PATH)),
495 'type' => 'url',
496 'path' => $feed_xpath,
497 );
498 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
499 $fileInfo['type'] = apply_filters('wp_all_import_feed_type', $fileInfo['type'], $this->file);
500 // detect CSV or XML
501 switch ($fileInfo['type']) {
502 case 'csv':
503 include_once(PMXI_Plugin::ROOT_DIR.'/libraries/XmlImportCsvParse.php');
504 $csv = new PMXI_CsvParser( array( 'filename' => $filePath, 'targetDir' => $this->uploadsPath ) ); // create chunks
505 $csv_path = $filePath;
506 //wp_all_import_remove_source($filePath, false);
507 $filePath = $csv->xml_path;
508 $this->is_csv = $csv->is_csv;
509 $this->root_element = 'node';
510 break;
511 case 'json':
512 $json_str = file_get_contents($filePath);
513 $json_str = str_replace("\xEF\xBB\xBF",'', $json_str);
514 $is_json = wp_all_import_is_json($json_str);
515
516 if( is_wp_error($is_json)){
517 $this->errors->add('form-validation', $is_json->get_error_message(), 'wp-all-import');
518 } else {
519 $xml_data = wp_all_import_json_to_xml( json_decode($json_str, true) );
520 if ( empty($xml_data) ) {
521 $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'wp-all-import'));
522 } else {
523 $tmpname = $this->uploadsPath .'/'. wp_all_import_url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($filePath))));
524 file_put_contents($tmpname, $xml_data);
525 wp_all_import_remove_source($filePath, false);
526 $filePath = $tmpname;
527 }
528 }
529 break;
530 case 'sql':
531 include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php' );
532 $sql = new PMXI_SQLParser( $filePath, $this->uploadsPath );
533 $filePath = $sql->parse();
534 break;
535 default:
536 # code...
537 break;
538 }
539 }
540 else $this->errors->add('form-validation', $fileInfo->get_error_message());
541 }
542 }
543
544 if ( $this->errors->get_error_codes() ) return $this->errors;
545
546 $decodedTemplates = empty($templates) ? json_decode($importTemplate, true) : json_decode($templates, true);
547
548 $templateOptions = "";
549
550 if ( is_array($decodedTemplates) and ! empty($decodedTemplates)) {
551 $templateOptions = empty($decodedTemplates[0]) ? current($decodedTemplates) : $decodedTemplates;
552 }
553
554 $options = (empty($templateOptions[0]['options'])) ? false : \pmxi_maybe_unserialize($templateOptions[0]['options']);
555
556 if ( ! empty($options['root_element'])) $this->root_element = $options['root_element'];
557
558 return array(
559 'filePath' => $filePath,
560 'bundle' => $bundle,
561 'source' => $source,
562 'root_element' => $this->root_element,
563 'feed_type' => $feed_type,
564 'is_csv' => $this->is_csv,
565 'csv_path' => $csv_path,
566 'template' => empty($templateOptions) ? "" : json_encode($templateOptions),
567 'templates' => $templates,
568 'post_type' => (!empty($options)) ? $options['custom_type'] : false,
569 'taxonomy_type' => (!empty($options['taxonomy_type'])) ? $options['taxonomy_type'] : false,
570 );
571 }
572
573 protected function get_xml_file( $filePath )
574 {
575 $csv_path = '';
576
577 if (preg_match('%\W(csv|txt|dat|psv|tsv)$%i', trim($filePath))){ // If CSV file found in archieve
578
579 if($this->uploadsPath === false){
580 $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'wp-all-import'));
581 }
582
583 include_once(PMXI_Plugin::ROOT_DIR.'/libraries/XmlImportCsvParse.php');
584 $csv = new PMXI_CsvParser( array( 'filename' => $filePath, 'targetDir' => $this->uploadsPath ) ); // create chunks
585
586 $csv_path = $filePath;
587
588 $filePath = $csv->xml_path;
589 $this->is_csv = $csv->is_csv;
590 $this->root_element = 'node';
591
592 } elseif (preg_match('%\W(json)$%i', trim($filePath))){
593
594 $json_str = file_get_contents($filePath);
595 $json_str = str_replace("\xEF\xBB\xBF",'', $json_str);
596 $is_json = wp_all_import_is_json($json_str);
597
598 if( is_wp_error($is_json)){
599 $this->errors->add('form-validation', $is_json->get_error_message(), 'wp-all-import');
600 }
601 else{
602
603 $xml_data = wp_all_import_json_to_xml( json_decode($json_str, true) );
604
605 if ( empty($xml_data) ){
606 $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'wp-all-import'));
607 }
608 else{
609 $jsontmpname = $this->uploadsPath .'/'. wp_all_import_url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($filePath))));
610 file_put_contents($jsontmpname, $xml_data);
611 wp_all_import_remove_source($filePath, false);
612 $filePath = $jsontmpname;
613 }
614 }
615
616 }
617 elseif (preg_match('%\W(sql)$%i', trim($filePath))){
618
619 include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php' );
620
621 $localSQLPath = $filePath;
622 $sql = new PMXI_SQLParser( $localSQLPath, $this->uploadsPath );
623 $filePath = $sql->parse();
624 wp_all_import_remove_source($localSQLPath, false);
625 }
626 elseif (preg_match('%\W(xls|xlsx)$%i', trim($filePath))){
627
628 include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportXLSParse.php' );
629
630 $localXLSPath = $filePath;
631 $xls = new PMXI_XLSParser( $localXLSPath, $this->uploadsPath );
632 $filePath = $xls->parse();
633 wp_all_import_remove_source($localXLSPath, false);
634
635 }
636 elseif(preg_match('%\W(gz)$%i', trim($filePath))) { // If gz file uploaded
637
638 $fileInfo = wp_all_import_get_gz($filePath, 0, $this->uploadsPath);
639
640 if (!is_wp_error($fileInfo)) {
641
642 $filePath = $fileInfo['localPath'];
643
644 // detect CSV or XML
645 if ($fileInfo['type'] == 'csv') { // it is CSV file
646
647 include_once(PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php');
648 $csv_path = $filePath;
649 $csv = new PMXI_CsvParser(array(
650 'filename' => $filePath,
651 'targeDir' => $this->uploadsPath
652 )); // create chunks
653 //@unlink($filePath);
654 $filePath = $csv->xml_path;
655 $this->is_csv = $csv->is_csv;
656 $this->root_element = 'node';
657
658 }
659
660 }
661 else $this->errors->add('form-validation', $fileInfo->get_error_message());
662 }
663
664 return array(
665 'csv' => $csv_path,
666 'xml' => $filePath
667 );
668 }
669 }
670 }
671