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