function wp_all_import_get_feed_type($url)
 {
     $type = wp_all_import_get_remote_file_name($url);
     if ($type !== false) {
         return array('Content-Type' => $type, 'Content-Encoding' => false);
     }
     $headers = get_headers($url, 1);
     $extensions = array('gzip', 'gz', 'xml', 'csv', 'json', 'sql');
     $type = false;
     $contentType = !empty($headers['Content-Type']) ? $headers['Content-Type'] : false;
     if ($contentType === false) {
         $contentType = !empty($headers['content-type']) ? $headers['content-type'] : false;
     }
     if (!empty($contentType)) {
         if (is_array($contentType)) {
             foreach ($contentType as $key => $ct) {
                 foreach ($extensions as $ext) {
                     if (strpos($ct, $ext) !== false) {
                         $type = $ext;
                         break 2;
                     }
                 }
             }
         } else {
             foreach ($extensions as $ext) {
                 if (strpos($contentType, $ext) !== false) {
                     $type = $ext;
                     break;
                 }
             }
         }
         if (!empty($headers['Content-Disposition'])) {
             foreach ($extensions as $ext) {
                 if (strpos($headers['Content-Disposition'], $ext) !== false) {
                     $type = $ext;
                     break;
                 }
             }
         }
     }
     return array('Content-Type' => $type, 'Content-Encoding' => !empty($headers['Content-Encoding']) ? $headers['Content-Encoding'] : false);
 }
Example #2
0
 public function url($feed_type = '', $feed_xpath = '')
 {
     $uploads = wp_upload_dir();
     $template = false;
     if (empty($this->file)) {
         $this->errors->add('form-validation', __('Please specify a file to import.', 'wp_all_import_plugin'));
     } elseif (!preg_match('%^https?://%i', $this->file)) {
         $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');
     } elseif (!is_writeable($this->uploadsPath)) {
         $this->errors->add('form-validation', __('Uploads folder ' . $this->uploadsPath . ' is not writable.'), 'wp_all_import_plugin');
     }
     $this->file = trim($this->file);
     $csv_path = '';
     if (empty($this->errors->errors)) {
         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));
         }
         if ('zip' == $feed_type or empty($feed_type) and preg_match('%\\W(zip)$%i', trim($this->file))) {
             $tmpname = $this->uploadsPath . '/' . wp_unique_filename($this->uploadsPath, basename($this->file));
             @copy($this->file, $tmpname);
             if (!file_exists($tmpname)) {
                 $request = get_file_curl($this->file, $tmpname);
                 if (is_wp_error($request)) {
                     $this->errors->add('form-validation', $request->get_error_message());
                 }
                 if (!file_exists($tmpname)) {
                     $this->errors->add('form-validation', __('Failed upload ZIP archive', 'wp_all_import_plugin'));
                 }
             }
             if (!class_exists('PclZip')) {
                 include_once PMXI_Plugin::ROOT_DIR . '/libraries/pclzip.lib.php';
             }
             $archive = new PclZip($tmpname);
             if (($v_result_list = $archive->extract(PCLZIP_OPT_PATH, $this->uploadsPath, PCLZIP_OPT_REPLACE_NEWER)) == 0) {
                 $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'));
             } else {
                 $filePath = '';
                 if (!empty($v_result_list)) {
                     foreach ($v_result_list as $unzipped_file) {
                         if ($unzipped_file['status'] == 'ok' and preg_match('%\\W(xml|csv|txt|dat|psv|json|xls|xlsx)$%i', trim($unzipped_file['stored_filename'])) and strpos($unzipped_file['stored_filename'], 'readme.txt') === false) {
                             if (strpos(basename($unzipped_file['stored_filename']), 'WP All Import Template') === 0 || strpos(basename($unzipped_file['stored_filename']), 'templates_') === 0) {
                                 $template = file_get_contents($unzipped_file['filename']);
                             } elseif ($filePath == '') {
                                 $filePath = $unzipped_file['filename'];
                             }
                         }
                     }
                 }
                 if ($this->uploadsPath === false) {
                     $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'wp_all_import_plugin'));
                 }
                 if (empty($filePath)) {
                     $zip = zip_open(trim($tmpname));
                     if (is_resource($zip)) {
                         while ($zip_entry = zip_read($zip)) {
                             $filePath = zip_entry_name($zip_entry);
                             $fp = fopen($this->uploadsPath . "/" . $filePath, "w");
                             if (zip_entry_open($zip, $zip_entry, "r")) {
                                 $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                                 fwrite($fp, "{$buf}");
                                 zip_entry_close($zip_entry);
                                 fclose($fp);
                             }
                             break;
                         }
                         zip_close($zip);
                     } else {
                         $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'));
                     }
                 }
                 // Detect if file is very large
                 $source = array('name' => basename(parse_url($this->file, PHP_URL_PATH)), 'type' => 'url', 'path' => $feed_xpath);
                 if (preg_match('%\\W(csv|txt|dat|psv)$%i', trim($filePath))) {
                     include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                     $csv = new PMXI_CsvParser(array('filename' => $filePath, 'targetDir' => $this->uploadsPath));
                     // create chunks
                     //wp_all_import_remove_source($filePath, false);
                     $csv_path = $filePath;
                     $filePath = $csv->xml_path;
                     $this->is_csv = $csv->is_csv;
                     $this->root_element = 'node';
                 } elseif (preg_match('%\\W(json)$%i', trim($filePath))) {
                     $json_str = file_get_contents($filePath);
                     $is_json = wp_all_import_is_json($json_str);
                     if (is_wp_error($is_json)) {
                         $this->errors->add('form-validation', $is_json->get_error_message(), 'wp_all_import_plugin');
                     } else {
                         $xml_data = wp_all_import_json_to_xml(json_decode($json_str, true));
                         if (empty($xml_data)) {
                             $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'wp_all_import_plugin'));
                         } else {
                             $jsontmpname = $this->uploadsPath . '/' . wp_all_import_url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($filePath))));
                             file_put_contents($jsontmpname, $xml_data);
                             wp_all_import_remove_source($filePath, false);
                             $filePath = $jsontmpname;
                         }
                     }
                 } elseif (preg_match('%\\W(sql)$%i', trim($filePath))) {
                     include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php';
                     $localSQLPath = $filePath;
                     $sql = new PMXI_SQLParser($localSQLPath, $this->uploadsPath);
                     $filePath = $sql->parse();
                     wp_all_import_remove_source($localSQLPath, false);
                 } elseif (preg_match('%\\W(xls|xlsx)$%i', trim($filePath))) {
                     include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportXLSParse.php';
                     $localXLSPath = $filePath;
                     $xls = new PMXI_XLSParser($localXLSPath, $this->uploadsPath);
                     $filePath = $xls->parse();
                     wp_all_import_remove_source($localXLSPath, false);
                 }
             }
             if (file_exists($tmpname)) {
                 wp_all_import_remove_source($tmpname, false);
             }
         } elseif ('csv' == $feed_type or '' == $feed_type and preg_match('%\\W(csv|txt|dat|psv)$%i', trim($this->file))) {
             $source = array('name' => basename(parse_url($this->file, PHP_URL_PATH)), 'type' => 'url', 'path' => $feed_xpath);
             // copy remote file in binary mode
             $filePath = wp_all_import_get_url($this->file, $this->uploadsPath, 'csv');
             if (!is_wp_error($filePath)) {
                 if (!file_exists($filePath)) {
                     $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'));
                 }
                 // Detect if file is very large
                 include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                 $csv = new PMXI_CsvParser(array('filename' => $filePath, 'targetDir' => $this->uploadsPath));
                 // create chunks
                 //wp_all_import_remove_source($filePath, false);
                 $csv_path = $filePath;
                 $filePath = $csv->xml_path;
                 $this->is_csv = $csv->is_csv;
                 $this->root_element = 'node';
             } else {
                 $this->errors->add('form-validation', $filePath->get_error_message());
             }
         } elseif (preg_match('%\\W(json)$%i', trim($this->file))) {
             $source = array('name' => basename(parse_url($this->file, PHP_URL_PATH)), 'type' => 'url', 'path' => $feed_xpath);
             // copy remote file in binary mode
             $filePath = wp_all_import_get_url($this->file, $this->uploadsPath, 'json');
             $json_str = file_get_contents($filePath);
             $is_json = wp_all_import_is_json($json_str);
             if (is_wp_error($is_json)) {
                 $this->errors->add('form-validation', $is_json->get_error_message(), 'wp_all_import_plugin');
             } else {
                 $xml_data = wp_all_import_json_to_xml(json_decode($json_str, true));
                 if (empty($xml_data)) {
                     $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'wp_all_import_plugin'));
                 } else {
                     $tmpname = $this->uploadsPath . '/' . wp_all_import_url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($filePath))));
                     file_put_contents($tmpname, $xml_data);
                     wp_all_import_remove_source($filePath, false);
                     $filePath = $tmpname;
                 }
             }
         } elseif (preg_match('%\\W(sql)$%i', trim($this->file))) {
             $source = array('name' => basename($this->file), 'type' => 'url', 'path' => $feed_xpath);
             // copy remote file in binary mode
             $localSQLPath = wp_all_import_get_url($this->file, $this->uploadsPath, 'sql');
             include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php';
             $sql = new PMXI_SQLParser($localSQLPath, $this->uploadsPath);
             $filePath = $sql->parse();
             wp_all_import_remove_source($localSQLPath, false);
         } elseif (preg_match('%\\W(xls|xlsx)$%i', trim($this->file))) {
             $source = array('name' => basename($this->file), 'type' => 'url', 'path' => $feed_xpath);
             // copy remote file in binary mode
             $localXLSPath = wp_all_import_get_url($this->file, $this->uploadsPath, 'xls');
             include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportXLSParse.php';
             $xls = new PMXI_XLSParser($localXLSPath, $this->uploadsPath);
             $filePath = $xls->parse();
             wp_all_import_remove_source($localXLSPath, false);
         } else {
             if ('gz' == $feed_type or '' == $feed_type and preg_match('%\\W(gz|gzip)$%i', trim($this->file))) {
                 $fileInfo = wp_all_import_get_gz($this->file, 0, $this->uploadsPath);
             } else {
                 $headers = wp_all_import_get_feed_type($this->file);
                 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'))) {
                     $fileInfo = wp_all_import_get_gz($this->file, 0, $this->uploadsPath);
                 } else {
                     $fileInfo = wp_all_import_get_url($this->file, $this->uploadsPath, $headers['Content-Type'], $headers['Content-Encoding'], true);
                 }
             }
             if (!is_wp_error($fileInfo)) {
                 $filePath = $fileInfo['localPath'];
                 if (!file_exists($filePath)) {
                     $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'));
                 }
                 // Detect if file is very large
                 $source = array('name' => basename(parse_url($this->file, PHP_URL_PATH)), 'type' => 'url', 'path' => $feed_xpath);
                 $fileInfo['type'] = apply_filters('wp_all_import_feed_type', $fileInfo['type'], $this->file);
                 // detect CSV or XML
                 switch ($fileInfo['type']) {
                     case 'csv':
                         include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                         $csv = new PMXI_CsvParser(array('filename' => $filePath, 'targetDir' => $this->uploadsPath));
                         // create chunks
                         $csv_path = $filePath;
                         //wp_all_import_remove_source($filePath, false);
                         $filePath = $csv->xml_path;
                         $this->is_csv = $csv->is_csv;
                         $this->root_element = 'node';
                         break;
                     case 'json':
                         $json_str = file_get_contents($filePath);
                         $is_json = wp_all_import_is_json($json_str);
                         if (is_wp_error($is_json)) {
                             $this->errors->add('form-validation', $is_json->get_error_message(), 'wp_all_import_plugin');
                         } else {
                             $xml_data = wp_all_import_json_to_xml(json_decode($json_str, true));
                             if (empty($xml_data)) {
                                 $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'wp_all_import_plugin'));
                             } else {
                                 $tmpname = $this->uploadsPath . '/' . wp_all_import_url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($filePath))));
                                 file_put_contents($tmpname, $xml_data);
                                 wp_all_import_remove_source($filePath, false);
                                 $filePath = $tmpname;
                             }
                         }
                         break;
                     case 'sql':
                         include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php';
                         $sql = new PMXI_SQLParser($filePath, $this->uploadsPath);
                         $filePath = $sql->parse();
                         break;
                     default:
                         # code...
                         break;
                 }
             } else {
                 $this->errors->add('form-validation', $fileInfo->get_error_message());
             }
         }
     }
     if ($this->errors->get_error_codes()) {
         return $this->errors;
     }
     $templateOptions = json_decode($template, true);
     $options = maybe_unserialize($templateOptions[0]['options']);
     return array('filePath' => $filePath, 'source' => $source, 'root_element' => $this->root_element, 'feed_type' => $feed_type, 'is_csv' => $this->is_csv, 'csv_path' => $csv_path, 'template' => $template, 'post_type' => !empty($options) ? $options['custom_type'] : false);
 }