function pmxi_wp_ajax_nested_xpath() { if (!check_ajax_referer('wp_all_import_secure', 'security', false)) { exit(json_encode(array('result' => array(), 'msg' => __('Security check', 'wp_all_import_plugin')))); } if (!current_user_can(PMXI_Plugin::$capabilities)) { exit(json_encode(array('result' => array(), 'msg' => __('Security check', 'wp_all_import_plugin')))); } extract($_POST); $result = array(); if (@file_exists($filePath)) { $file = new PMXI_Chunk($filePath, array('element' => $root_element, 'encoding' => 'UTF-8')); $tagno = 0; $loop = 0; $count = 0; $xml_tree = ''; while ($xml = $file->read()) { if (!empty($xml)) { PMXI_Import_Record::preprocessXml($xml); $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . $xml; if ('' != $customXpath) { $dom = new DOMDocument('1.0', 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = $xpath->query($customXpath) and $elements->length) { $loop++; $count += $elements->length; if (!$tagno or $loop == $tagno) { ob_start(); PMXI_Render::render_xml_element($elements->item(0), true); $xml_tree = ob_get_clean(); $tagno = 1; } } } else { exit(json_encode(array('success' => false, 'msg' => __('XPath is required', 'wp_all_import_plugin')))); die; } } } unset($file); } else { exit(json_encode(array('success' => false, 'msg' => 'File path is required', 'wp_all_import_plugin'))); die; } exit(json_encode(array('success' => true, 'xml_tree' => $xml_tree, 'count' => $count ? sprintf("<p class='green pmxi_counter'>" . __('Elements found', 'pmxi_pligun') . " <strong>%s</strong></p>", $count) : "<p class='red pmxi_counter'>" . __('Elements not found', 'pmxi_pligun') . "</p>"))); die; }
/** * Re-run import */ public function update() { $id = $this->input->get('id'); PMXI_Plugin::$session->clean_session($id); $action_type = false; $this->data['import'] = $item = new PMXI_Import_Record(); if (!$id or $item->getById($id)->isEmpty()) { wp_redirect($this->baseUrl); die; } $this->data['isWizard'] = false; $default = PMXI_Plugin::get_default_import_options(); $DefaultOptions = $item->options + $default; foreach (PMXI_Admin_Addons::get_active_addons() as $class) { if (class_exists($class)) { $DefaultOptions += call_user_func(array($class, "get_default_import_options")); } } $this->data['post'] =& $DefaultOptions; $this->data['source'] = array('path' => $item->path, 'root_element' => $item->root_element); $this->data['xpath'] = $item->xpath; $this->data['count'] = $item->count; $history = new PMXI_File_List(); $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $item->id), 'id DESC'); if ($history->count()) { foreach ($history as $file) { if (@file_exists($file['path'])) { $this->data['locfilePath'] = $file['path']; break; } } } $chunks = 0; if ($this->input->post('is_confirmed') and check_admin_referer('confirm', '_wpnonce_confirm')) { $continue = $this->input->post('is_continue', 'no'); // mark action type ad continue if ($continue == 'yes') { $action_type = 'continue'; } $filePath = ''; // upload new file in case when import is not continue if (empty(PMXI_Plugin::$session->chunk_number)) { if ($item->type == 'upload') { // retrieve already uploaded file $uploader = new PMXI_Upload(trim($item->path), $this->errors, rtrim(str_replace(basename($item->path), '', $item->path), '/')); $upload_result = $uploader->upload(); if ($upload_result instanceof WP_Error) { $this->errors = $upload_result; } else { $filePath = $upload_result['filePath']; } } if (empty($item->options['encoding'])) { $currentOptions = $item->options; $currentOptions['encoding'] = 'UTF-8'; $item->set(array('options' => $currentOptions))->update(); } @set_time_limit(0); $local_paths = !empty($local_paths) ? $local_paths : array($filePath); foreach ($local_paths as $key => $path) { if (!empty($action_type) and $action_type == 'continue') { $chunks = $item->count; } else { $file = new PMXI_Chunk($path, array('element' => $item->root_element, 'encoding' => $item->options['encoding'])); while ($xml = $file->read()) { if (!empty($xml)) { PMXI_Import_Record::preprocessXml($xml); $xml = "<?xml version=\"1.0\" encoding=\"" . $item->options['encoding'] . "\"?>" . "\n" . $xml; $dom = new DOMDocument('1.0', !empty($item->options['encoding']) ? $item->options['encoding'] : 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); // FIX: libxml xpath doesn't handle default namespace properly, so remove it upon XML load libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = @$xpath->query($item->xpath) and !empty($elements) and !empty($elements->length)) { $chunks += $elements->length; } unset($dom, $xpath, $elements); } } unset($file); } !$key and $filePath = $path; } if (empty($chunks)) { $this->errors->add('form-validation', __('No matching elements found for Root element and XPath expression specified', 'wp_all_import_plugin')); } } if ($chunks) { // xml is valid if (!PMXI_Plugin::is_ajax() and empty(PMXI_Plugin::$session->chunk_number)) { // compose data to look like result of wizard steps $sesson_data = array('filePath' => $filePath, 'source' => array('name' => $item->name, 'type' => $item->type, 'path' => $item->path, 'root_element' => $item->root_element), 'feed_type' => $item->feed_type, 'update_previous' => $item->id, 'parent_import_id' => $item->parent_import_id, 'xpath' => $item->xpath, 'options' => $item->options, 'encoding' => !empty($item->options['encoding']) ? $item->options['encoding'] : 'UTF-8', 'is_csv' => !empty($item->options['delimiter']) ? $item->options['delimiter'] : PMXI_Plugin::$is_csv, 'csv_path' => PMXI_Plugin::$csv_path, 'chunk_number' => 1, 'log' => '', 'warnings' => 0, 'errors' => 0, 'start_time' => 0, 'pointer' => 1, 'count' => isset($chunks) ? $chunks : 0, 'local_paths' => !empty($local_paths) ? $local_paths : array(), 'action' => (!empty($action_type) and $action_type == 'continue') ? 'continue' : 'update', 'nonce' => wp_create_nonce('import')); foreach ($sesson_data as $key => $value) { PMXI_Plugin::$session->set($key, $value); } PMXI_Plugin::$session->save_data(); } $item->set(array('canceled' => 0, 'failed' => 0))->update(); // deligate operation to other controller $controller = new PMXI_Admin_Import(); $controller->data['update_previous'] = $item; $controller->process(); return; } } $this->render('admin/import/confirm'); }
function pmxi_wp_ajax_upload_resource() { if (!check_ajax_referer('wp_all_import_secure', 'security', false)) { exit(json_encode(array('success' => false, 'errors' => '<div class="error inline"><p>' . __('Security check', 'wp_all_import_plugin') . '</p></div>'))); } if (!current_user_can('manage_options')) { exit(json_encode(array('success' => false, 'errors' => '<div class="error inline"><p>' . __('Security check', 'wp_all_import_plugin') . '</p></div>'))); } $input = new PMXI_Input(); $post = $input->post(array('type' => '', 'file' => '')); $response = array('success' => true, 'errors' => false, 'upload_result' => '', 'filesize' => 0); if ($post['type'] == 'url') { $filesXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<data><node></node></data>"; $files = XmlImportParser::factory($filesXML, '/data/node', $post['file'], $file)->parse(); $tmp_files[] = $file; foreach ($tmp_files as $tmp_file) { // remove all temporary files created @unlink($tmp_file); } $file_to_import = $post['file']; if (!empty($files) and is_array($files)) { $file_to_import = array_shift($files); } $errors = new WP_Error(); $uploader = new PMXI_Upload(trim($file_to_import), $errors); $upload_result = $uploader->url('', $post['file']); if ($upload_result instanceof WP_Error) { $errors = $upload_result; $msgs = $errors->get_error_messages(); ob_start(); ?> <?php foreach ($msgs as $msg) { ?> <div class="error inline"><p><?php echo $msg; ?> </p></div> <?php } ?> <?php $response = array('success' => false, 'errors' => ob_get_clean()); } else { // validate XML $file = new PMXI_Chunk($upload_result['filePath'], array('element' => $upload_result['root_element'])); $is_valid = true; if (!empty($file->options['element'])) { $defaultXpath = "/" . $file->options['element']; } else { $is_valid = false; } if ($is_valid) { while ($xml = $file->read()) { if (!empty($xml)) { PMXI_Import_Record::preprocessXml($xml); $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . $xml; $dom = new DOMDocument('1.0', 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = $xpath->query($defaultXpath) and $elements->length) { break; } } /*else { $is_valid = false; break; }*/ } if (empty($xml)) { $is_valid = false; } } unset($file); if (!$is_valid) { ob_start(); ?> <div class="error inline"><p><?php _e('Please confirm you are importing a valid feed.<br/> Often, feed providers distribute feeds with invalid data, improperly wrapped HTML, line breaks where they should not be, faulty character encodings, syntax errors in the XML, and other issues.<br/><br/>WP All Import has checks in place to automatically fix some of the most common problems, but we can’t catch every single one.<br/><br/>It is also possible that there is a bug in WP All Import, and the problem is not with the feed.<br/><br/>If you need assistance, please contact support – <a href="mailto:support@wpallimport.com">support@wpallimport.com</a> – with your XML/CSV file. We will identify the problem and release a bug fix if necessary.', 'wp_all_import_plugin'); ?> </p></div> <?php $response = array('success' => false, 'errors' => ob_get_clean()); } else { $response['upload_result'] = $upload_result; $response['filesize'] = filesize($upload_result['filePath']); } } } exit(json_encode($response)); }
protected function get_xml($tagno = 0, $debug = false) { $xml = ''; $update_previous = new PMXI_Import_Record(); if (!empty(PMXI_Plugin::$session->update_previous)) { $update_previous->getById(PMXI_Plugin::$session->update_previous); } $local_paths = empty(PMXI_Plugin::$session->local_paths) ? array() : PMXI_Plugin::$session->local_paths; if (empty($local_paths) and !$update_previous->isEmpty()) { $history_file = new PMXI_File_Record(); $history_file->getBy(array('import_id' => $update_previous->id), 'id DESC'); $local_paths = !$history_file->isEmpty() ? array(wp_all_import_get_absolute_path($history_file->path)) : array(); } if (!empty($local_paths)) { $loop = 0; foreach ($local_paths as $key => $path) { if (@file_exists($path)) { $root_element = !$update_previous->isEmpty() ? $update_previous->root_element : PMXI_Plugin::$session->source['root_element']; $file = new PMXI_Chunk($path, array('element' => $root_element, 'encoding' => PMXI_Plugin::$session->encoding)); while ($xml = $file->read()) { if (!empty($xml)) { PMXI_Import_Record::preprocessXml($xml); $xml = "<?xml version=\"1.0\" encoding=\"" . PMXI_Plugin::$session->encoding . "\"?>" . "\n" . $xml; if (PMXI_Plugin::$session->xpath) { $dom = new DOMDocument('1.0', PMXI_Plugin::$session->encoding); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = $xpath->query(PMXI_Plugin::$session->xpath) and $elements->length) { $this->data['dom'] = $dom; $loop++; if (!$tagno or $loop == $tagno) { break; } } } else { break; } } } unset($file); } } } return $xml; }
/** * upload.php * * Copyright 2009, Moxiecode Systems AB * Released under GPL License. * * License: http://www.plupload.com/license * Contributing: http://www.plupload.com/contributing */ public function upload() { if (!check_ajax_referer('wp_all_import_secure', '_wpnonce', false)) { exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 100, "message" => __('Security check', 'wp_all_import_plugin')), "id" => "id"))); } // HTTP headers for no cache etc // header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // header("Cache-Control: no-store, no-cache, must-revalidate"); // header("Cache-Control: post-check=0, pre-check=0", false); // header("Pragma: no-cache"); // Settings //$targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload"; //$uploads = wp_upload_dir(); $targetDir = self::$path; if (!is_dir($targetDir) || !is_writable($targetDir)) { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 100, "message" => __("Uploads folder is not writable.", "wp_all_import_plugin")), "id" => "id"))); } $cleanupTargetDir = true; // Remove old files $maxFileAge = 5 * 3600; // Temp file age in seconds // 5 minutes execution time @set_time_limit(5 * 60); // Uncomment this one to fake upload time // usleep(5000); // Get parameters $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0; $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0; $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : ''; // Clean the fileName for security reasons $fileName = preg_replace('/[^\\w\\._]+/', '_', $fileName); if (!preg_match('%\\W(xml|gzip|zip|csv|gz|json|txt|dat|psv|sql|xls|xlsx)$%i', trim(basename($fileName)))) { exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 100, "message" => __("Uploaded file must be XML, CSV, ZIP, GZIP, GZ, JSON, SQL, TXT, DAT or PSV", "wp_all_import_plugin")), "id" => "id"))); } // Make sure the fileName is unique but only if chunking is disabled if ($chunks < 2 && file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) { $ext = strrpos($fileName, '.'); $fileName_a = substr($fileName, 0, $ext); $fileName_b = substr($fileName, $ext); $count = 1; while (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b)) { $count++; } $fileName = $fileName_a . '_' . $count . $fileName_b; } $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName; // Create target dir if (!file_exists($targetDir)) { @mkdir($targetDir); } // Remove old temp files if ($cleanupTargetDir && is_dir($targetDir) && ($dir = opendir($targetDir))) { while (($file = readdir($dir)) !== false) { $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file; // Remove temp file if it is older than the max age and is not the current file if (preg_match('/\\.part$/', $file) && filemtime($tmpfilePath) < time() - $maxFileAge && $tmpfilePath != "{$filePath}.part") { @unlink($tmpfilePath); } } closedir($dir); } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 100, "message" => __("Failed to open temp directory.", "wp_all_import_plugin")), "id" => "id"))); } // Look for the content type header if (isset($_SERVER["HTTP_CONTENT_TYPE"])) { $contentType = $_SERVER["HTTP_CONTENT_TYPE"]; } if (isset($_SERVER["CONTENT_TYPE"])) { $contentType = $_SERVER["CONTENT_TYPE"]; } // Handle non multipart uploads older WebKit versions didn't support multipart in HTML5 if (strpos($contentType, "multipart") !== false) { if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) { // Open temp file $out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab"); if ($out) { // Read binary input stream and append it to temp file $in = fopen($_FILES['file']['tmp_name'], "rb"); if ($in) { while ($buff = fread($in, 4096)) { fwrite($out, $buff); } } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 101, "message" => __("Failed to open input stream.", "wp_all_import_plugin")), "id" => "id"))); } fclose($in); fclose($out); @unlink($_FILES['file']['tmp_name']); } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 102, "message" => __("Failed to open output stream.", "wp_all_import_plugin")), "id" => "id"))); } } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 103, "message" => __("Failed to move uploaded file.", "wp_all_import_plugin")), "id" => "id"))); } } else { // Open temp file $out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab"); if ($out) { // Read binary input stream and append it to temp file $in = fopen("php://input", "rb"); if ($in) { while ($buff = fread($in, 4096)) { fwrite($out, $buff); } } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 101, "message" => __("Failed to open input stream.", "wp_all_import_plugin")), "id" => "id"))); } fclose($in); fclose($out); } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 102, "message" => __("Failed to open output stream.", "wp_all_import_plugin")), "id" => "id"))); } } $post_type = false; // Check if file has been uploaded if (!$chunks || $chunk == $chunks - 1) { // Strip the temp .part suffix off rename("{$filePath}.part", $filePath); chmod($filePath, 0755); delete_transient(self::$upload_transient); $errors = new WP_Error(); $uploader = new PMXI_Upload($filePath, $errors, rtrim(str_replace(basename($filePath), '', $filePath), '/')); $upload_result = $uploader->upload(); if ($upload_result instanceof WP_Error) { $errors = $upload_result; $msgs = $errors->get_error_messages(); ob_start(); ?> <?php foreach ($msgs as $msg) { ?> <div class="error inline"><p><?php echo $msg; ?> </p></div> <?php } ?> <?php $response = ob_get_clean(); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 102, "message" => $response), "id" => "id"))); } else { // validate XML $file = new PMXI_Chunk($upload_result['filePath'], array('element' => $upload_result['root_element'])); $is_valid = true; if (!empty($file->options['element'])) { $defaultXpath = "/" . $file->options['element']; } else { $is_valid = false; } if ($is_valid) { while ($xml = $file->read()) { if (!empty($xml)) { PMXI_Import_Record::preprocessXml($xml); $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . $xml; $dom = new DOMDocument('1.0', 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = $xpath->query($defaultXpath) and $elements->length) { break; } } /*else { $is_valid = false; break; }*/ } if (empty($xml)) { $is_valid = false; } } unset($file); if (!preg_match('%\\W(xml)$%i', trim($upload_result['source']['path']))) { @unlink($upload_result['filePath']); } if (!$is_valid) { ob_start(); ?> <div class="error inline"><p><?php _e('Please confirm you are importing a valid feed.<br/> Often, feed providers distribute feeds with invalid data, improperly wrapped HTML, line breaks where they should not be, faulty character encodings, syntax errors in the XML, and other issues.<br/><br/>WP All Import has checks in place to automatically fix some of the most common problems, but we can’t catch every single one.<br/><br/>It is also possible that there is a bug in WP All Import, and the problem is not with the feed.<br/><br/>If you need assistance, please contact support – <a href="mailto:support@wpallimport.com">support@wpallimport.com</a> – with your XML/CSV file. We will identify the problem and release a bug fix if necessary.', 'wp_all_import_plugin'); ?> </p></div> <?php $response = ob_get_clean(); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 102, "message" => $response), "id" => "id"))); } else { $wp_uploads = wp_upload_dir(); $uploads = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR; if (!file_exists($uploads . basename($filePath))) { @copy($filePath, $uploads . basename($filePath)); } } if (!empty($upload_result['post_type'])) { $post_type = $upload_result['post_type']; } } } // Return JSON-RPC response exit(json_encode(array("jsonrpc" => "2.0", "error" => null, "result" => null, "id" => "id", "name" => $filePath, "post_type" => $post_type))); }
/** * * Ajax action that will parse nested XML/CSV files * */ function pmxi_wp_ajax_parse_nested_file() { extract($_POST); $result = array(); $wp_uploads = wp_upload_dir(); if (!empty($_POST['nested_type'])) { $root_element = ''; $feed_type = ''; $errors = new WP_Error(); switch ($_POST['nested_type']) { case 'upload': $uploader = new PMXI_Upload($_POST['nested_filepath'], $errors); $upload_result = $uploader->upload(); if ($upload_result instanceof WP_Error) { $errors = $upload_result; } else { $source = $upload_result['source']; $filePath = $upload_result['filePath']; if (!empty($upload_result['root_element'])) { $root_element = $upload_result['root_element']; } } break; case 'url': $uploader = new PMXI_Upload($_POST['nested_url'], $errors); $upload_result = $uploader->url(); if ($upload_result instanceof WP_Error) { $errors = $upload_result; } else { $source = $upload_result['source']; $filePath = $upload_result['filePath']; if (!empty($upload_result['root_element'])) { $root_element = $upload_result['root_element']; } $feed_type = $upload_result['feed_type']; } break; case 'file': $uploader = new PMXI_Upload($_POST['nested_file'], $errors); $upload_result = $uploader->file(); if ($upload_result instanceof WP_Error) { $errors = $upload_result; } else { $source = $upload_result['source']; $filePath = $upload_result['filePath']; if (!empty($upload_result['root_element'])) { $root_element = $upload_result['root_element']; } } break; } } if ($errors->get_error_codes()) { $msgs = $errors->get_error_messages(); ob_start(); ?> <?php foreach ($msgs as $msg) { ?> <div class="error"><p><?php echo $msg; ?> </p></div> <?php } ?> <?php exit(json_encode(array('success' => false, 'errors' => ob_get_clean()))); die; } else { $xml_tree = ''; if (@file_exists($filePath)) { $file = new PMXI_Chunk($filePath, array('element' => $root_element)); if (!empty($file->options['element'])) { $customXpath = "/" . $file->options['element']; $elements_cloud = $file->cloud; } $root_element = $file->options['element']; $file = new PMXI_Chunk($filePath, array('element' => $root_element, 'encoding' => 'UTF-8')); $tagno = 0; $loop = 0; $count = 0; while ($xml = $file->read()) { if (!empty($xml)) { PMXI_Import_Record::preprocessXml($xml); $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . $xml; if ('' != $customXpath) { $dom = new DOMDocument('1.0', 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = $xpath->query($customXpath) and $elements->length) { $loop++; $count += $elements->length; if (!$tagno or $loop == $tagno) { ob_start(); PMXI_Render::render_xml_element($elements->item(0), true); $xml_tree = ob_get_clean(); $tagno = 1; } } } else { break; } } } unset($file); } exit(json_encode(array('success' => true, 'source' => $source, 'realpath' => $source['path'], 'filePath' => $filePath, 'root_element' => $root_element, 'xml_tree' => $xml_tree, 'xpath' => $customXpath, 'count' => $count ? sprintf("<p class='green pmxi_counter'>" . __('Elements founded', 'pmxi_pligun') . " <strong>%s</strong></p>", $count) : "<p class='red pmxi_counter'>" . __('Elements not found', 'pmxi_pligun') . "</p>"))); die; } }
/** * Import all files matched by path * @param callback[optional] $logger Method where progress messages are submmitted * @return PMXI_Import_Record * @chainable */ public function execute($logger = NULL, $cron = true, $history_log_id = false) { $uploads = wp_upload_dir(); if ($this->path) { $files = array($this->path); foreach ($files as $ind => $path) { $filePath = ''; if ($this->queue_chunk_number == 0 and $this->processing == 0) { $this->set(array('processing' => 1))->update(); // lock cron requests if ($this->type == 'ftp') { $this->set(array('processing' => 0))->update(); return array('status' => 500, 'message' => sprintf(__('This import appears to be using FTP. Unfortunately WP All Import no longer supports the FTP protocol. Please contact <a href="mailto:support@wpallimport.com">%s</a> if you have any questions.', 'wp_all_import_plugin'), '*****@*****.**')); //$logger and call_user_func($logger, sprintf(__('This import appears to be using FTP. Unfortunately WP All Import no longer supports the FTP protocol. Please contact <a href="mailto:support@wpallimport.com">%s</a> if you have any questions.', 'wp_all_import_plugin'), '*****@*****.**')); die; } elseif ($this->type == 'url') { $filesXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<data><node></node></data>"; $filePaths = XmlImportParser::factory($filesXML, '/data/node', $this->path, $file)->parse(); $tmp_files[] = $file; foreach ($tmp_files as $tmp_file) { // remove all temporary files created @unlink($tmp_file); } $file_to_import = $this->path; if (!empty($filePaths) and is_array($filePaths)) { $file_to_import = array_shift($filePaths); } $uploader = new PMXI_Upload(trim($file_to_import), $this->errors); $upload_result = $uploader->url($this->feed_type, $this->path); if ($upload_result instanceof WP_Error) { $this->errors = $upload_result; } else { $filePath = $upload_result['filePath']; } } elseif ($this->type == 'file') { $uploader = new PMXI_Upload(trim(basename($this->path)), $this->errors); $upload_result = $uploader->file(); if ($upload_result instanceof WP_Error) { $this->errors = $upload_result; } else { $filePath = $upload_result['filePath']; } } elseif (!in_array($this->type, array('ftp'))) { // retrieve already uploaded file $uploader = new PMXI_Upload(trim($this->path), $this->errors, rtrim(str_replace(basename($this->path), '', $this->path), '/')); $upload_result = $uploader->upload(); if ($upload_result instanceof WP_Error) { $this->errors = $upload_result; } else { $filePath = $upload_result['filePath']; } } if (!$this->errors->get_error_codes() and "" != $filePath) { $this->set(array('queue_chunk_number' => 1))->update(); } elseif ($this->errors->get_error_codes()) { $msgs = $this->errors->get_error_messages(); if (!is_array($msgs)) { $msgs = array($msgs); } // foreach ($msgs as $msg){ // $logger and call_user_func($logger, sprintf(__('ERROR: %s', 'wp_all_import_plugin'), $msg)); // } $this->set(array('processing' => 0))->update(); return array('status' => 500, 'message' => $msgs); die; } $this->set(array('processing' => 0))->update(); // unlock cron requests } // if empty file path, than it's mean feed in cron process. Take feed path from history. if (empty($filePath)) { $history = new PMXI_File_List(); $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $this->id), 'id DESC'); if ($history->count()) { $history_file = new PMXI_File_Record(); $history_file->getBy('id', $history[0]['id']); $filePath = wp_all_import_get_absolute_path($history_file->path); } } // if feed path found if (!empty($filePath) and @file_exists($filePath)) { if ($this->queue_chunk_number === 1 and $this->processing == 0) { // import first cron request $this->set(array('processing' => 1))->update(); // lock cron requests if (empty($this->options['encoding'])) { $currentOptions = $this->options; $currentOptions['encoding'] = 'UTF-8'; $this->set(array('options' => $currentOptions))->update(); } set_time_limit(0); $file = new PMXI_Chunk($filePath, array('element' => $this->root_element, 'encoding' => $this->options['encoding'])); // chunks counting $chunks = 0; $history_xml = ''; while ($xml = $file->read()) { if (!empty($xml)) { PMXI_Import_Record::preprocessXml($xml); $xml = "<?xml version=\"1.0\" encoding=\"" . $this->options['encoding'] . "\"?>" . "\n" . $xml; $dom = new DOMDocument('1.0', !empty($this->options['encoding']) ? $this->options['encoding'] : 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = @$xpath->query($this->xpath) and $elements->length) { $chunks += $elements->length; if ("" == $history_xml) { $history_xml = $xml; } } unset($dom, $xpath, $elements); } } unset($file); if (!$chunks) { //$logger and call_user_func($logger, sprintf(__('#%s No matching elements found for Root element and XPath expression specified', 'wp_all_import_plugin'), $this->id)); $this->set(array('queue_chunk_number' => 0, 'processing' => 0, 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'triggered' => 0))->update(); return array('status' => 500, 'message' => sprintf(__('#%s No matching elements found for Root element and XPath expression specified', 'wp_all_import_plugin'), $this->id)); die; } // unlick previous files $history = new PMXI_File_List(); $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $this->id), 'id DESC'); if ($history->count()) { foreach ($history as $file) { $history_file_path = wp_all_import_get_absolute_path($file['path']); if (@file_exists($history_file_path) and $history_file_path != $filePath) { if (in_array($this->type, array('upload'))) { wp_all_import_remove_source($history_file_path, false); } else { wp_all_import_remove_source($history_file_path); } } $history_file = new PMXI_File_Record(); $history_file->getBy('id', $file['id']); if (!$history_file->isEmpty()) { $history_file->delete($history_file_path != $filePath); } } } // update history $history_file = new PMXI_File_Record(); $history_file->set(array('name' => $this->name, 'import_id' => $this->id, 'path' => wp_all_import_get_relative_path($filePath), 'registered_on' => date('Y-m-d H:i:s')))->insert(); do_action('pmxi_before_xml_import', $this->id); $this->set(array('count' => $chunks, 'processing' => 0))->update(); // set pointer to the first chunk, updates feed elements count and unlock cron process } // compose data to look like result of wizard steps if ($this->queue_chunk_number and $this->processing == 0) { $this->set(array('processing' => 1))->update(); // lock cron requests @set_time_limit(0); $file = new PMXI_Chunk($filePath, array('element' => $this->root_element, 'encoding' => $this->options['encoding'], 'pointer' => $this->queue_chunk_number)); $feed = "<?xml version=\"1.0\" encoding=\"" . $this->options['encoding'] . "\"?>" . "\n" . "<pmxi_records>"; $loop = 0; $xml = ''; $processing_time_limit = PMXI_Plugin::getInstance()->getOption('cron_processing_time_limit') ? PMXI_Plugin::getInstance()->getOption('cron_processing_time_limit') : 120; $start_rocessing_time = time(); $chunk_number = $this->queue_chunk_number; $functions = $uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY . DIRECTORY_SEPARATOR . 'functions.php'; if (@file_exists($functions)) { require_once $functions; } while ($xml = $file->read() and $this->processing == 1 and time() - $start_rocessing_time <= $processing_time_limit) { if (!empty($xml)) { $chunk_number++; PMXI_Import_Record::preprocessXml($xml); $xml_chunk = "<?xml version=\"1.0\" encoding=\"" . $this->options['encoding'] . "\"?>" . "\n" . $xml; $dom = new DOMDocument('1.0', !empty($this->options['encoding']) ? $this->options['encoding'] : 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml_chunk); libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = @$xpath->query($this->xpath) and $elements->length) { $feed .= $xml; $loop += $elements->length; } unset($dom, $xpath, $elements); } if ($loop == $this->options['records_per_request'] or $this->count == $this->imported + $this->skipped or $this->count == $loop + $this->imported + $this->skipped) { // skipping scheduled imports if any for the next hit $feed .= "</pmxi_records>"; $this->process($feed, $logger, $chunk_number, $cron, '/pmxi_records', $loop); // set last update $this->set(array('registered_on' => date('Y-m-d H:i:s'), 'queue_chunk_number' => $chunk_number))->update(); $loop = 0; $feed = "<?xml version=\"1.0\" encoding=\"" . $this->options['encoding'] . "\"?>" . "\n" . "<pmxi_records>"; } } unset($file); // delect, if cron process if finished if ((int) $this->count <= (int) $this->imported + (int) $this->skipped) { // Delete posts that are no longer present in your file if (!empty($this->options['is_delete_missing']) and $this->options['duplicate_matching'] == 'auto') { $postList = new PMXI_Post_List(); $missing_ids = array(); $missingPosts = $postList->getBy(array('import_id' => $this->id, 'iteration !=' => $this->iteration)); if (!$missingPosts->isEmpty()) { foreach ($missingPosts as $missingPost) { $missing_ids[] = $missingPost['post_id']; } } // Delete posts from database if (!empty($missing_ids) && is_array($missing_ids)) { $missing_ids_arr = array_chunk($missing_ids, 100); foreach ($missing_ids_arr as $key => $ids) { if (!empty($ids)) { foreach ($ids as $k => $id) { $to_delete = true; // Instead of deletion, set Custom Field if ($this->options['is_update_missing_cf']) { update_post_meta($id, $this->options['update_missing_cf_name'], $this->options['update_missing_cf_value']); $to_delete = false; } // Instead of deletion, change post status to Draft $final_post_type = get_post_type($pid); if ($this->options['set_missing_to_draft'] and $final_post_type != 'product_variation') { $this->wpdb->update($this->wpdb->posts, array('post_status' => 'draft'), array('ID' => $id)); $to_delete = false; } // Delete posts that are no longer present in your file if ($to_delete) { // Remove attachments empty($this->options['is_keep_attachments']) and wp_delete_attachments($id, true, 'files'); // Remove images empty($this->options['is_keep_imgs']) and wp_delete_attachments($id, true, 'images'); // Clear post's relationships if ($this->options['custom_type'] != "import_users") { wp_delete_object_term_relationships($id, get_object_taxonomies('' != $this->options['custom_type'] ? $this->options['custom_type'] : 'post')); } } else { unset($ids[$k]); } } if (!empty($ids)) { do_action('pmxi_delete_post', $ids); if ($this->options['custom_type'] == "import_users") { $sql = "delete a,b\n\t\t\t\t\t\t\t\t\t\t\t\t\tFROM " . $this->wpdb->users . " a\n\t\t\t\t\t\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->usermeta . " b ON ( a.ID = b.user_id )\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");"; } else { $sql = "delete a,b,c\n\t\t\t\t\t\t\t\t\t\t\t\t\tFROM " . $this->wpdb->posts . " a\n\t\t\t\t\t\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->term_relationships . " b ON ( a.ID = b.object_id )\n\t\t\t\t\t\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->postmeta . " c ON ( a.ID = c.post_id )\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");"; } $this->wpdb->query($sql); // Delete record form pmxi_posts $sql = "DELETE FROM " . PMXI_Plugin::getInstance()->getTablePrefix() . "posts WHERE post_id IN (" . implode(',', $ids) . ") AND import_id = %d"; $this->wpdb->query($this->wpdb->prepare($sql, $this->id)); $this->set(array('deleted' => $this->deleted + count($ids)))->update(); } } } } } // Set out of stock status for missing records [Woocommerce add-on option] if (empty($this->options['is_delete_missing']) and $this->options['custom_type'] == "product" and class_exists('PMWI_Plugin') and !empty($this->options['missing_records_stock_status'])) { $postList = new PMXI_Post_List(); $missingPosts = $postList->getBy(array('import_id' => $this->id, 'iteration !=' => $this->iteration)); if (!$missingPosts->isEmpty()) { foreach ($missingPosts as $missingPost) { update_post_meta($missingPost['post_id'], '_stock_status', 'outofstock'); update_post_meta($missingPost['post_id'], '_stock', 0); $postRecord = new PMXI_Post_Record(); $postRecord->getBy('id', $missingPost['id']); if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $this->iteration))->update(); } unset($postRecord); } } } $this->set(array('processing' => 0, 'triggered' => 0, 'queue_chunk_number' => 0, 'registered_on' => date('Y-m-d H:i:s'), 'iteration' => ++$this->iteration))->update(); wp_cache_flush(); foreach (get_taxonomies() as $tax) { delete_option("{$tax}_children"); _get_term_hierarchy($tax); } if ($history_log_id) { $history_log = new PMXI_History_Record(); $history_log->getById($history_log_id); if (!$history_log->isEmpty()) { $custom_type = get_post_type_object($this->options['custom_type']); $history_log->set(array('time_run' => time() - strtotime($history_log->date), 'summary' => sprintf(__("import finished & cron un-triggered<br>%s %s created %s updated %s deleted %s skipped", "pmxi_plugin"), $this->created, $custom_type->labels->name, $this->updated, $this->deleted, $this->skipped)))->save(); } } do_action('pmxi_after_xml_import', $this->id); return array('status' => 200, 'message' => sprintf(__('Import #%s complete', 'wp_all_import_plugin'), $this->id)); //$logger and call_user_func($logger, sprintf(__('Import #%s complete', 'wp_all_import_plugin'), $this->id)); } else { $this->set(array('processing' => 0))->update(); if ($history_log_id) { $history_log = new PMXI_History_Record(); $history_log->getById($history_log_id); if (!$history_log->isEmpty()) { $custom_type = get_post_type_object($this->options['custom_type']); $history_log->set(array('time_run' => time() - strtotime($history_log->date), 'summary' => sprintf(__("%d %s created %d updated %d deleted %d skipped", "pmxi_plugin"), $this->created, $custom_type->labels->name, $this->updated, $this->deleted, $this->skipped)))->save(); } } return array('status' => 200, 'message' => sprintf(__('Records Processed %s. Records Count %s.', 'wp_all_import_plugin'), (int) $this->queue_chunk_number, (int) $this->count)); // $logger and call_user_func($logger, sprintf(__('Records Count %s', 'wp_all_import_plugin'), (int) $this->count)); // $logger and call_user_func($logger, sprintf(__('Records Processed %s', 'wp_all_import_plugin'), (int) $this->imported + (int) $this->skipped)); } } } else { $this->set(array('processing' => 0, 'triggered' => 0, 'queue_chunk_number' => 0, 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0))->update(); return array('status' => 500, 'message' => sprintf(__('#%s source file not found', 'wp_all_import_plugin'), $this->id)); //$logger and call_user_func($logger, sprintf(__('#%s source file not found', 'wp_all_import_plugin'), $this->id)); die; } } } return $this; }
/** * upload.php * * Copyright 2009, Moxiecode Systems AB * Released under GPL License. * * License: http://www.plupload.com/license * Contributing: http://www.plupload.com/contributing */ public function upload() { if (!check_ajax_referer('wp_all_import_secure', '_wpnonce', false)) { exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 100, "message" => __('Security check', 'wp_all_import_plugin')), "id" => "id"))); } // HTTP headers for no cache etc // header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // header("Cache-Control: no-store, no-cache, must-revalidate"); // header("Cache-Control: post-check=0, pre-check=0", false); // header("Pragma: no-cache"); // Settings //$targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload"; //$uploads = wp_upload_dir(); $targetDir = self::$path; if (!is_dir($targetDir) || !is_writable($targetDir)) { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 100, "message" => __("Uploads folder is not writable.", "wp_all_import_plugin")), "id" => "id"))); } $cleanupTargetDir = true; // Remove old files $maxFileAge = 5 * 3600; // Temp file age in seconds // 5 minutes execution time @set_time_limit(5 * 60); // Uncomment this one to fake upload time // usleep(5000); // Get parameters $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0; $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0; $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : ''; // Clean the fileName for security reasons $fileName = preg_replace('/[^\\w\\._]+/', '_', $fileName); if (!preg_match('%\\W(xml|gzip|zip|csv|gz|json|txt|dat|psv|sql|xls|xlsx)$%i', trim(basename($fileName)))) { exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 100, "message" => __("Uploaded file must be XML, CSV, ZIP, GZIP, GZ, JSON, SQL, TXT, DAT or PSV", "wp_all_import_plugin")), "id" => "id"))); } // Make sure the fileName is unique but only if chunking is disabled if ($chunks < 2 && file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) { $ext = strrpos($fileName, '.'); $fileName_a = substr($fileName, 0, $ext); $fileName_b = substr($fileName, $ext); $count = 1; while (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b)) { $count++; } $fileName = $fileName_a . '_' . $count . $fileName_b; } $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName; // Create target dir if (!file_exists($targetDir)) { @mkdir($targetDir); } // Remove old temp files if ($cleanupTargetDir && is_dir($targetDir) && ($dir = opendir($targetDir))) { while (($file = readdir($dir)) !== false) { $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file; // Remove temp file if it is older than the max age and is not the current file if (preg_match('/\\.part$/', $file) && filemtime($tmpfilePath) < time() - $maxFileAge && $tmpfilePath != "{$filePath}.part") { @unlink($tmpfilePath); } } closedir($dir); } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 100, "message" => __("Failed to open temp directory.", "wp_all_import_plugin")), "id" => "id"))); } // Look for the content type header if (isset($_SERVER["HTTP_CONTENT_TYPE"])) { $contentType = $_SERVER["HTTP_CONTENT_TYPE"]; } if (isset($_SERVER["CONTENT_TYPE"])) { $contentType = $_SERVER["CONTENT_TYPE"]; } // Handle non multipart uploads older WebKit versions didn't support multipart in HTML5 if (strpos($contentType, "multipart") !== false) { if (isset($_FILES['async-upload']['tmp_name']) && is_uploaded_file($_FILES['async-upload']['tmp_name'])) { // Open temp file $out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab"); if ($out) { // Read binary input stream and append it to temp file $in = fopen($_FILES['async-upload']['tmp_name'], "rb"); if ($in) { while ($buff = fread($in, 4096)) { fwrite($out, $buff); } } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 101, "message" => __("Failed to open input stream.", "wp_all_import_plugin")), "id" => "id"))); } fclose($in); fclose($out); @unlink($_FILES['async-upload']['tmp_name']); } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 102, "message" => __("Failed to open output stream.", "wp_all_import_plugin")), "id" => "id"))); } } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 103, "message" => __("Failed to move uploaded file.", "wp_all_import_plugin")), "id" => "id"))); } } else { // Open temp file $out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab"); if ($out) { // Read binary input stream and append it to temp file $in = fopen("php://input", "rb"); if ($in) { while ($buff = fread($in, 4096)) { fwrite($out, $buff); } } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 101, "message" => __("Failed to open input stream.", "wp_all_import_plugin")), "id" => "id"))); } fclose($in); fclose($out); } else { delete_transient(self::$upload_transient); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 102, "message" => __("Failed to open output stream.", "wp_all_import_plugin")), "id" => "id"))); } } $post_type = false; $notice = false; $warning = false; // Check if file has been uploaded if (!$chunks || $chunk == $chunks - 1) { // Strip the temp .part suffix off rename("{$filePath}.part", $filePath); chmod($filePath, 0755); delete_transient(self::$upload_transient); $errors = new WP_Error(); $uploader = new PMXI_Upload($filePath, $errors, rtrim(str_replace(basename($filePath), '', $filePath), '/')); $upload_result = $uploader->upload(); if ($upload_result instanceof WP_Error) { $errors = $upload_result; $msgs = $errors->get_error_messages(); ob_start(); ?> <?php foreach ($msgs as $msg) { ?> <p><?php echo $msg; ?> </p> <?php } ?> <?php $response = ob_get_clean(); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 102, "message" => $response), "id" => "id"))); } else { if (!empty($upload_result['post_type'])) { $post_type = $upload_result['post_type']; if (!empty($upload_result['template'])) { $template = json_decode($upload_result['template'], true); if (!empty($template[0]['options'])) { $is_show_cf_notice = !empty($template[0]['options']['custom_name']) ? true : false; $is_show_images_notice = false; if ($post_type != 'product' && ($template[0]['options']['download_featured_image'] != '' || $template[0]['options']['gallery_featured_image'] != '' || $template[0]['options']['featured_image'] != '')) { $is_show_images_notice = true; } if ($is_show_cf_notice && $is_show_images_notice) { $warning = __('<a class="upgrade_link" target="_blank" href="http://www.wpallimport.com/upgrade-to-pro/?utm_source=free-plugin&utm_medium=in-plugin&utm_campaign=custom-fields">Upgrade to the Pro edition of WP All Import to import images and custom fields.</a> <p>If you already own it, remove the free edition and install the Pro edition.</p>', 'wp_all_import_plugin'); } else { if ($is_show_cf_notice) { $warning = __('<a class="upgrade_link" target="_blank" href="http://www.wpallimport.com/upgrade-to-pro/?utm_source=free-plugin&utm_medium=in-plugin&utm_campaign=custom-fields">Upgrade to the Pro edition of WP All Import to import custom fields.</a> <p>If you already own it, remove the free edition and install the Pro edition.</p>', 'wp_all_import_plugin'); } else { if ($is_show_images_notice) { $warning = __('<a class="upgrade_link" target="_blank" href="http://www.wpallimport.com/upgrade-to-pro/?utm_source=free-plugin&utm_medium=in-plugin&utm_campaign=custom-fields">Upgrade to the Pro edition of WP All Import to import images.</a> <p>If you already own it, remove the free edition and install the Pro edition.</p>', 'wp_all_import_plugin'); } } } } } switch ($post_type) { case 'product': case 'shop_order': if (!class_exists('WooCommerce')) { $notice = __('<p class="wpallimport-bundle-notice">The import bundle you are using requires WooCommerce.</p><a class="upgrade_link" href="https://wordpress.org/plugins/woocommerce/" target="_blank">Get WooCommerce</a>.', 'wp_all_import_plugin'); } else { if (!defined('PMWI_EDITION')) { $notice = __('<p class="wpallimport-bundle-notice">The import bundle you are using requires the Pro version of the WooCommerce Add-On.</p><a href="http://www.wpallimport.com/checkout/?edd_action=add_to_cart&download_id=1529&edd_options%5Bprice_id%5D=1" class="upgrade_link" target="_blank">Purchase the WooCommerce Add-On</a>.', 'wp_all_import_plugin'); } elseif (PMWI_EDITION != 'paid') { $notice = __('<p class="wpallimport-bundle-notice">The import bundle you are using requires the Pro version of the WooCommerce Add-On, but you have the free version installed.</p><a href="http://www.wpallimport.com/checkout/?edd_action=add_to_cart&download_id=1529&edd_options%5Bprice_id%5D=1" target="_blank" class="upgrade_link">Purchase the WooCommerce Add-On</a>.', 'wp_all_import_plugin'); } } break; case 'import_users': if (!class_exists('PMUI_Plugin')) { $notice = __('<p class="wpallimport-bundle-notice">The import bundle you are using requires the User Import Add-On.</p><a href="http://www.wpallimport.com/checkout/?edd_action=add_to_cart&download_id=1921&edd_options%5Bprice_id%5D=1" target="_blank" class="upgrade_link">Purchase the User Import Add-On</a>.', 'wp_all_import_plugin'); } break; default: # code... break; } } if (!empty($upload_result['is_empty_bundle_file'])) { // Return JSON-RPC response exit(json_encode(array("jsonrpc" => "2.0", "error" => null, "result" => null, "id" => "id", "name" => $upload_result['filePath'], "post_type" => $post_type, "notice" => $notice, "template" => $upload_result['template'], "url_bundle" => true))); } else { // $root_element = wp_all_import_get_reader_engine( array($upload_result['filePath']), array('root_element' => $upload_result['root_element']) ); // if ( ! empty($root_element) and empty($upload_result['root_element'])) // { // $upload_result['root_element'] = $root_element; // } // validate XML $file = new PMXI_Chunk($upload_result['filePath'], array('element' => $upload_result['root_element'])); $is_valid = true; if (!empty($file->options['element'])) { $defaultXpath = "/" . $file->options['element']; } else { $is_valid = false; } if ($is_valid) { while ($xml = $file->read()) { if (!empty($xml)) { //PMXI_Import_Record::preprocessXml($xml); $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . $xml; $dom = new DOMDocument('1.0', 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = $xpath->query($defaultXpath) and $elements->length) { break; } } /*else { $is_valid = false; break; }*/ } if (empty($xml)) { $is_valid = false; } } unset($file); if (!preg_match('%\\W(xml)$%i', trim($upload_result['source']['path']))) { @unlink($upload_result['filePath']); } if (!$is_valid) { ob_start(); ?> <div class="error inline"><p><?php _e('Please confirm you are importing a valid feed.<br/> Often, feed providers distribute feeds with invalid data, improperly wrapped HTML, line breaks where they should not be, faulty character encodings, syntax errors in the XML, and other issues.<br/><br/>WP All Import has checks in place to automatically fix some of the most common problems, but we can’t catch every single one.<br/><br/>It is also possible that there is a bug in WP All Import, and the problem is not with the feed.<br/><br/>If you need assistance, please contact support – <a href="mailto:support@wpallimport.com">support@wpallimport.com</a> – with your XML/CSV file. We will identify the problem and release a bug fix if necessary.', 'wp_all_import_plugin'); ?> </p></div> <?php $response = ob_get_clean(); $file_type = strtoupper(pmxi_getExtension($upload_result['source']['path'])); $error_message = sprintf(__("Please verify that the file you uploading is a valid %s file.", "wp_all_import_plugin"), $file_type); exit(json_encode(array("jsonrpc" => "2.0", "error" => array("code" => 102, "message" => $error_message), "is_valid" => false, "id" => "id"))); } else { $wp_uploads = wp_upload_dir(); $uploads = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR; if (!file_exists($uploads . basename($filePath))) { @copy($filePath, $uploads . basename($filePath)); } } } } } // Return JSON-RPC response exit(json_encode(array("jsonrpc" => "2.0", "error" => null, "result" => null, "id" => "id", "name" => $filePath, "post_type" => $post_type, "notice" => $notice, "warning" => $warning))); }
function wp_all_import_get_url($filePath, $targetDir = false, $contentType = false, $contentEncoding = false, $detect = false) { $type = $contentType; $uploads = wp_upload_dir(); $targetDir = !$targetDir ? wp_all_import_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY) : $targetDir; $tmpname = wp_unique_filename($targetDir, ($type and strlen(basename($filePath)) < 30) ? basename($filePath) : time()); $localPath = $targetDir . '/' . urldecode(sanitize_file_name($tmpname)) . (!$type ? '.tmp' : ''); if ($contentEncoding == 'gzip') { $file = @fopen($filePath); } else { $file = @fopen($filePath, "rb"); } $is_valid = false; if (is_resource($file)) { $fp = @fopen($localPath, 'w'); $first_chunk = true; while (!@feof($file)) { $chunk = @fread($file, 1024); if (!$type and $first_chunk and (strpos($chunk, "<?") !== false or strpos($chunk, "<rss") !== false) or strpos($chunk, "xmlns") !== false) { $type = 'xml'; } elseif (!$type and $first_chunk) { $type = 'csv'; } // if it's a 1st chunk, then chunk <? symbols to detect XML file $first_chunk = false; @fwrite($fp, $chunk); } @fclose($file); @fclose($fp); $chunk = new PMXI_Chunk($localPath); $is_valid = true; if (!empty($chunk->options['element'])) { $defaultXpath = "/" . $chunk->options['element']; } else { $is_valid = false; } if ($is_valid) { while ($xml = $chunk->read()) { if (!empty($xml)) { PMXI_Import_Record::preprocessXml($xml); $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . $xml; $dom = new DOMDocument('1.0', 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = $xpath->query($defaultXpath) and $elements->length) { break; } } } if (empty($xml)) { $is_valid = false; } } unset($chunk); } if (!$is_valid) { $request = get_file_curl($filePath, $localPath); if (!is_wp_error($request)) { if (!$type) { if ($contentEncoding == 'gzip') { $file = @fopen($localPath); } else { $file = @fopen($localPath, "rb"); } while (!@feof($file)) { $chunk = @fread($file, 1024); if (strpos($chunk, "<?") !== false or strpos($chunk, "<rss") !== false or strpos($chunk, "xmlns") !== false) { $type = 'xml'; } else { $type = 'csv'; } // if it's a 1st chunk, then chunk <? symbols to detect XML file break; } @fclose($file); } } else { return $request; } } if (!preg_match('%\\W(' . $type . ')$%i', basename($localPath))) { if (@rename($localPath, $localPath . '.' . $type)) { $localPath = $localPath . '.' . $type; } } return $detect ? array('type' => $type, 'localPath' => $localPath) : $localPath; }
/** * Reimport */ public function update() { $id = $this->input->get('id'); $action_type = $this->input->get('type'); $this->data['item'] = $item = new PMXI_Import_Record(); if (!$id or $item->getById($id)->isEmpty()) { wp_redirect($this->baseUrl); die; } pmxi_session_unset(); $chunks = 0; if ($this->input->post('is_confirmed')) { check_admin_referer('update-import', '_wpnonce_update-import'); $uploads = wp_upload_dir(); if (empty(PMXI_Plugin::$session->data['pmxi_import']['chunk_number'])) { if (in_array($item->type, array('upload'))) { // if import type NOT URL if (preg_match('%\\W(zip)$%i', trim(basename($item->path)))) { include_once PMXI_Plugin::ROOT_DIR . '/libraries/pclzip.lib.php'; $archive = new PclZip(trim($item->path)); if (($v_result_list = $archive->extract(PCLZIP_OPT_PATH, $uploads['path'], PCLZIP_OPT_REPLACE_NEWER)) == 0) { $this->errors->add('form-validation', 'Failed to open uploaded ZIP archive : ' . $archive->errorInfo(true)); } 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)$%i', trim($unzipped_file['stored_filename']))) { $filePath = $unzipped_file['filename']; break; } } } if ($uploads['error']) { $this->errors->add('form-validation', __('Can not create upload folder. Permision denied', 'pmxi_plugin')); } if (empty($filePath)) { $zip = zip_open(trim($item->path)); if (is_resource($zip)) { while ($zip_entry = zip_read($zip)) { $filePath = zip_entry_name($zip_entry); $fp = fopen($uploads['path'] . "/" . $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', __('Failed to open uploaded ZIP archive. Can\'t extract files.', 'pmxi_plugin')); } } if (preg_match('%\\W(csv|txt|dat|psv)$%i', trim($filePath))) { // If CSV file found in archieve if ($uploads['error']) { $this->errors->add('form-validation', __('Can not create upload folder. Permision denied', 'pmxi_plugin')); } include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php'; $csv = new PMXI_CsvParser($filePath, true, '', !empty($item->options['delimiter']) ? $item->options['delimiter'] : '', !empty($item->options['encoding']) ? $item->options['encoding'] : ''); // create chunks $filePath = $csv->xml_path; } } } elseif (preg_match('%\\W(csv|txt|dat|psv)$%i', trim($item->path))) { // If CSV file uploaded if ($uploads['error']) { $this->errors->add('form-validation', __('Can not create upload folder. Permision denied', 'pmxi_plugin')); } include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php'; $csv = new PMXI_CsvParser($item->path, true, '', !empty($item->options['delimiter']) ? $item->options['delimiter'] : '', !empty($item->options['encoding']) ? $item->options['encoding'] : ''); $filePath = $csv->xml_path; } elseif (preg_match('%\\W(gz)$%i', trim($item->path))) { // If gz file uploaded $fileInfo = pmxi_gzfile_get_contents($item->path); if (!is_wp_error($fileInfo)) { $filePath = $fileInfo['localPath']; // detect CSV or XML if ($fileInfo['type'] == 'csv') { // it is CSV file include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php'; $csv = new PMXI_CsvParser($filePath, true, '', !empty($item->options['delimiter']) ? $item->options['delimiter'] : '', !empty($item->options['encoding']) ? $item->options['encoding'] : ''); // create chunks $filePath = $csv->xml_path; } } else { $this->errors->add('form-validation', $fileInfo->get_error_message()); } } else { // If XML file uploaded $filePath = $item->path; } } @set_time_limit(0); $local_paths = !empty($local_paths) ? $local_paths : array($filePath); foreach ($local_paths as $key => $path) { if (!empty($action_type) and $action_type == 'continue') { $chunks = $item->count; } else { $file = new PMXI_Chunk($path, array('element' => $item->root_element, 'encoding' => $item->options['encoding'])); while ($xml = $file->read()) { if (!empty($xml)) { PMXI_Import_Record::preprocessXml($xml); $xml = "<?xml version=\"1.0\" encoding=\"" . $item->options['encoding'] . "\"?>" . "\n" . $xml; $dom = new DOMDocument('1.0', !empty($item->options['encoding']) ? $item->options['encoding'] : 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); // FIX: libxml xpath doesn't handle default namespace properly, so remove it upon XML load libxml_use_internal_errors($old); $xpath = new DOMXPath($dom); if ($elements = @$xpath->query($item->xpath) and !empty($elements) and !empty($elements->length)) { $chunks += $elements->length; } unset($dom, $xpath, $elements); } } unset($file); } !$key and $filePath = $path; } if (empty($chunks)) { $this->errors->add('form-validation', __('No matching elements found for Root element and XPath expression specified', 'pmxi_plugin')); } } if ($chunks) { // xml is valid if (!PMXI_Plugin::is_ajax() and empty(PMXI_Plugin::$session->data['pmxi_import']['chunk_number'])) { // compose data to look like result of wizard steps PMXI_Plugin::$session['pmxi_import'] = array('filePath' => $filePath, 'source' => array('name' => $item->name, 'type' => $item->type, 'path' => $item->path, 'root_element' => $item->root_element), 'feed_type' => $item->feed_type, 'update_previous' => $item->id, 'parent_import_id' => $item->parent_import_id, 'xpath' => $item->xpath, 'template' => $item->template, 'options' => $item->options, 'encoding' => !empty($item->options['encoding']) ? $item->options['encoding'] : 'UTF-8', 'is_csv' => !empty($item->options['delimiter']) ? $item->options['delimiter'] : PMXI_Plugin::$is_csv, 'csv_path' => PMXI_Plugin::$csv_path, 'scheduled' => $item->scheduled, 'chunk_number' => 1, 'log' => '', 'warnings' => 0, 'errors' => 0, 'start_time' => 0, 'pointer' => 1, 'count' => isset($chunks) ? $chunks : 0, 'local_paths' => !empty($local_paths) ? $local_paths : array(), 'action' => (!empty($action_type) and $action_type == 'continue') ? 'continue' : 'update'); pmxi_session_commit(); } // deligate operation to other controller $controller = new PMXI_Admin_Import(); $controller->data['update_previous'] = $item; $controller->process(); return; } } $this->render(); }
function wp_all_import_get_reader_engine($local_paths, $post, $import_id = 0) { $xml_reader_engine = 'xmlreader'; $is_auto_detect_xml_reader = apply_filters('wp_all_import_auto_detect_reader_engine', true); // auto detect xml reader engine disabled if ($is_auto_detect_xml_reader === false) { update_option('wpai_parser_type', 'xmlreader'); return false; } $root_element = ''; // auto-detect XML reader engine foreach ($local_paths as $key => $path) { if (@file_exists($path)) { $file = new PMXI_Chunk($path, array('element' => $post['root_element']), 'xmlreader'); $xmlreader_count = 0; if (!empty($file->options['element'])) { $root_element = $file->options['element']; $xpath = "/" . $file->options['element']; $start_time = time(); // loop through the file until all lines are read while ($xml = $file->read()) { if (!empty($xml)) { $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . $xml; $dom = new DOMDocument('1.0', 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $dxpath = new DOMXPath($dom); if ($elements = @$dxpath->query($xpath) and $elements->length) { $xmlreader_count += $elements->length; unset($dom, $dxpath, $elements); } } $execution_time = time() - $start_time; // if stream reader takes longer than 30 seconds just stop using it // if ( $execution_time > 30 ) { // break; // } } } unset($file); // count element using xml streamer if (!empty($post['root_element'])) { $root_element = $post['root_element']; } $file = new PMXI_Chunk($path, array('element' => $root_element), 'xmlstreamer'); $xmlstreamer_count = 0; if (!empty($file->options['element'])) { $xpath = "/" . $file->options['element']; $start_time = time(); // loop through the file until all lines are read while ($xml = $file->read()) { if (!empty($xml)) { $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . $xml; $dom = new DOMDocument('1.0', 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $dxpath = new DOMXPath($dom); if ($elements = @$dxpath->query($xpath) and $elements->length) { $xmlstreamer_count += $elements->length; unset($dom, $dxpath, $elements); } } $execution_time = time() - $start_time; // if stream reader takes longer than 30 seconds just stop using it // if ( $execution_time > 30 ) { // break; // } } } unset($file); $xml_reader_engine = $xmlreader_count >= $xmlstreamer_count ? 'xmlreader' : 'xmlstreamer'; update_option('wpai_parser_type', $xml_reader_engine); } } return $root_element; }